Skip to contentSkip to conversation

selftests/user_events: Avoid taking address of packed member in perf_test

PATCH In review Ankit Khushwaha 2 participants 6 messages Oct 27, 2025 – Nov 1, 2025 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/user_events: Avoid taking address of packed member in perf_test

Accessing 'reg.write_index' directly triggers a -Waddress-of-packed-member warning due to potential unaligned pointer access:

perf_test.c:239:38: warning: taking address of packed member 'write_index' of class or structure 'user_reg' may result in an unaligned pointer value [-Waddress-of-packed-member]
239 | ASSERT_NE(-1, write(self->data_fd, &reg.write_index,

      |                                             ^~~~~~~~~~~~~~~ 

Use memcpy() instead to safely copy the value and avoid unaligned pointer access across architectures.

---
 tools/testing/selftests/user_events/perf_test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/user_events/perf_test.c +3 -1
index 201459d8094d..e4385f4aa231 100644
@@ -201,6 +201,7 @@ TEST_F(user, perf_empty_events) {
 	struct perf_event_mmap_page *perf_page;
 	int page_size = sysconf(_SC_PAGESIZE);
 	int id, fd;
+	__u32 write_index;
 	__u32 *val;
 
 	reg.size = sizeof(reg);
@@ -236,7 +237,8 @@ TEST_F(user, perf_empty_events) {
 	ASSERT_EQ(1 << reg.enable_bit, self->check);
 
 	/* Ensure write shows up at correct offset */
-	ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
+	memcpy(&write_index, &reg.write_index, sizeof(reg.write_index));
+	ASSERT_NE(-1, write(self->data_fd, &write_index,
 	                    sizeof(reg.write_index)));
 	val = (void *)(((char *)perf_page) + perf_page->data_offset);
 	ASSERT_EQ(PERF_RECORD_SAMPLE, *val);
Signature · 1 line
2.51.0
#1 lore raw
Quoted 25 lines · On Mon, 27 Oct 2025 17:04:39 +0530 Ankit Khushwaha…

Accessing 'reg.write_index' directly triggers a -Waddress-of-packed-member warning due to potential unaligned pointer access:

perf_test.c:239:38: warning: taking address of packed member 'write_index' of class or structure 'user_reg' may result in an unaligned pointer value [-Waddress-of-packed-member]
239 | ASSERT_NE(-1, write(self->data_fd, &reg.write_index,

      |                                             ^~~~~~~~~~~~~~~ 

Use memcpy() instead to safely copy the value and avoid unaligned pointer access across architectures.

...

tools/testing/selftests/user_events/perf_test.c +1 -0
@@ -201,6 +201,7 @@ TEST_F(user, perf_empty_events) {
 	struct perf_event_mmap_page *perf_page;
 	int page_size = sysconf(_SC_PAGESIZE);
 	int id, fd;
+	__u32 write_index;
 	__u32 *val;
 
 	reg.size = sizeof(reg);

tools/testing/selftests/user_events/perf_test.c TEST_F(user, perf_empty_events) { · line 242

Quoted 7 lines
tools/testing/selftests/user_events/perf_test.c +2 -1
@@ -236,7 +237,8 @@ TEST_F(user, perf_empty_events) {
 	ASSERT_EQ(1 << reg.enable_bit, self->check);
 
 	/* Ensure write shows up at correct offset */
-	ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
+	memcpy(&write_index, &reg.write_index, sizeof(reg.write_index));
+	ASSERT_NE(-1, write(self->data_fd, &write_index,
 	                    sizeof(reg.write_index)));

Simply casting &write_index to void* would fix this?

Quoted 2 lines
tools/testing/selftests/user_events/perf_test.c +0 -0
 	val = (void *)(((char *)perf_page) + perf_page->data_offset);
 	ASSERT_EQ(PERF_RECORD_SAMPLE, *val);
#2 lore raw
Quoted 29 lines · Andrew Morton
Quoted 27 lines · On Mon, 27 Oct 2025 17:04:39 +0530 Ankit Khushwaha…

Accessing 'reg.write_index' directly triggers a -Waddress-of-packed-member warning due to potential unaligned pointer access:

perf_test.c:239:38: warning: taking address of packed member 'write_index' of class or structure 'user_reg' may result in an unaligned pointer value [-Waddress-of-packed-member]
239 | ASSERT_NE(-1, write(self->data_fd, &reg.write_index,

      |                                             ^~~~~~~~~~~~~~~ 

Use memcpy() instead to safely copy the value and avoid unaligned pointer access across architectures.

...

tools/testing/selftests/user_events/perf_test.c +1 -0
@@ -201,6 +201,7 @@ TEST_F(user, perf_empty_events) {
 	struct perf_event_mmap_page *perf_page;
 	int page_size = sysconf(_SC_PAGESIZE);
 	int id, fd;
+	__u32 write_index;
 	__u32 *val;
 
 	reg.size = sizeof(reg);
@@ -236,7 +237,8 @@ TEST_F(user, perf_empty_events) {
 	ASSERT_EQ(1 << reg.enable_bit, self->check);
 
Quoted 7 lines

tools/testing/selftests/user_events/perf_test.c TEST_F(user, perf_empty_events) { · line 242

Quoted 5 lines
tools/testing/selftests/user_events/perf_test.c +2 -1
 	/* Ensure write shows up at correct offset */
-	ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
+	memcpy(&write_index, &reg.write_index, sizeof(reg.write_index));
+	ASSERT_NE(-1, write(self->data_fd, &write_index,
 	                    sizeof(reg.write_index)));

Simply casting &write_index to void* would fix this?

yes, this hides the type mismatch from the compiler. But i think casting to void * will not fix the alignment mismatch for packed struct. It works on x86, but might break on other platform.

Quoted 3 lines

tools/testing/selftests/user_events/perf_test.c TEST_F(user, perf_empty_events) { · line 244

Quoted 3 lines
tools/testing/selftests/user_events/perf_test.c +0 -0
 	val = (void *)(((char *)perf_page) + perf_page->data_offset);
 	ASSERT_EQ(PERF_RECORD_SAMPLE, *val);

Thanks
Ankit

#3 lore raw
Quoted 11 lines · On Tue, 28 Oct 2025 22:28:10 +0530 Ankit Khushwaha…
Quoted 10 lines

tools/testing/selftests/user_events/perf_test.c TEST_F(user, perf_empty_events) { · line 242

Quoted 8 lines
tools/testing/selftests/user_events/perf_test.c +2 -1
@@ -236,7 +237,8 @@ TEST_F(user, perf_empty_events) {
 	ASSERT_EQ(1 << reg.enable_bit, self->check);
 
 	/* Ensure write shows up at correct offset */
-	ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
+	memcpy(&write_index, &reg.write_index, sizeof(reg.write_index));
+	ASSERT_NE(-1, write(self->data_fd, &write_index,
 	                    sizeof(reg.write_index)));

Simply casting &write_index to void* would fix this?

Quoted 3 lines

yes, this hides the type mismatch from the compiler. But i think casting to void * will not fix the alignment mismatch for packed struct. It works on x86, but might break on other platform.

It's the second argument to write(2)! write(2) expects a const char *, but void* will work.

#4 lore raw
Quoted 13 lines · Andrew Morton
Quoted 11 lines · On Tue, 28 Oct 2025 22:28:10 +0530 Ankit Khushwaha…
Quoted 10 lines

tools/testing/selftests/user_events/perf_test.c TEST_F(user, perf_empty_events) { · line 242

Quoted 8 lines
tools/testing/selftests/user_events/perf_test.c +2 -1
@@ -236,7 +237,8 @@ TEST_F(user, perf_empty_events) {
 	ASSERT_EQ(1 << reg.enable_bit, self->check);
 
 	/* Ensure write shows up at correct offset */
-	ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
+	memcpy(&write_index, &reg.write_index, sizeof(reg.write_index));
+	ASSERT_NE(-1, write(self->data_fd, &write_index,
 	                    sizeof(reg.write_index)));

Simply casting &write_index to void* would fix this?

Quoted 6 lines
Quoted 3 lines

yes, this hides the type mismatch from the compiler. But i think casting to void * will not fix the alignment mismatch for packed struct. It works on x86, but might break on other platform.

It's the second argument to write(2)! write(2) expects a const char *, but void* will work.

Hi Andrew,
Indeed
`ASSERT_NE(-1, write(self->data_fd, (void *)&reg.write_index,

		     sizeof(reg.write_index)));`

would work. However since reg is packed struct, directly taking the address of its member &reg.write_index may lead to unaligned access on some architectures. as indicated by the compiler warning

perf_test.c:239:38: warning: taking address of packed member 'write_index' of class or structure 'user_reg' may result in an unaligned pointer value [-Waddress-of-packed-member]
239 | ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
| ^~~~~~~~~~~~~~~

Using memcpy avoids this by performing a byte-wise copy, which is safe to use for packed structures.

Thanks
-- Ankit

#5 lore raw
Quoted 20 lines · On Wed, 29 Oct 2025 20:50:38 +0530 Ankit Khushwaha…
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

tools/testing/selftests/user_events/perf_test.c TEST_F(user, perf_empty_events) { · line 242

Quoted 5 lines
tools/testing/selftests/user_events/perf_test.c +2 -1
 	/* Ensure write shows up at correct offset */
-	ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
+	memcpy(&write_index, &reg.write_index, sizeof(reg.write_index));
+	ASSERT_NE(-1, write(self->data_fd, &write_index,
 	                    sizeof(reg.write_index)));

Simply casting &write_index to void* would fix this?

Quoted 6 lines
Quoted 3 lines

yes, this hides the type mismatch from the compiler. But i think casting to void * will not fix the alignment mismatch for packed struct. It works on x86, but might break on other platform.

It's the second argument to write(2)! write(2) expects a const char *, but void* will work.

Hi Andrew,
Indeed
`ASSERT_NE(-1, write(self->data_fd, (void *)&reg.write_index,

		     sizeof(reg.write_index)));`
Quoted 7 lines

would work. However since reg is packed struct, directly taking the address of its member &reg.write_index may lead to unaligned access on some architectures. as indicated by the compiler warning

perf_test.c:239:38: warning: taking address of packed member 'write_index' of class or structure 'user_reg' may result in an unaligned pointer value [-Waddress-of-packed-member]

Well sure, we might get an unaligned pointer value and it would be an error to dereference that pointer.

But we don't dereference it! We pass that pointer to write(2), which is happy with any alignment.

The warning is accurate. It "may" indeed "result in an unaligned pointer value". But there is nothing at all wrong with this code. OK, let's find some way to suppress the warning (preferably without adding a pointless memcpy) and let's make the changelog and code comments be clear about what's going on here.