Skip to contentSkip to conversation

libarena/selftests: use ARRAY_SIZE macro in st_asan_buddy.bpf.c

PATCH In review longlong yan 3 participants 3 messages Jul 16 – Jul 17

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libarena/selftests: use ARRAY_SIZE macro in st_asan_buddy.bpf.c

The ARRAY_SIZE macro is more compact and more formal in linux source.

Fixes b1487dc1b181 ("selftests/bpf: Add selftests for libarena buddy allocator")
---
 .../selftests/bpf/libarena/selftests/test_asan_buddy.bpf.c    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/libarena/selftests/test_asan_buddy.bpf.c +2 -2
index 3266a28f53d7..9c94a5167a50 100644
@@ -163,7 +163,7 @@ __weak int asan_test_buddy_oob(void)
 		return ret;
 	}
 
-	for (i = zero; i < sizeof(sizes) / sizeof(sizes[0]) && can_loop; i++) {
+	for (i = zero; i < ARRAY_SIZE(sizes) && can_loop; i++) {
 		barrier_var(i);
 		ret = asan_test_buddy_oob_single(sizes[i]);
 		if (ret) {
@@ -201,7 +201,7 @@ __weak int asan_test_buddy_uaf(void)
 		return ret;
 	}
 
-	for (i = zero; i < sizeof(sizes) / sizeof(sizes[0]) && can_loop; i++) {
+	for (i = zero; i < ARRAY_SIZE(sizes) && can_loop; i++) {
 		barrier_var(i);
 		ret = asan_test_buddy_uaf_single(sizes[i]);
 		if (ret) {
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The commit introduces the `ARRAY_SIZE` macro into a BPF program without defining or including it…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The commit introduces the ARRAY_SIZE macro into a BPF program without defining or including it, causing a compilation failure. --

--- Patch [1]: [PATCH] libarena/selftests: use ARRAY_SIZE macro in st_asan_buddy.bpf.c --- commit 70521e8b40248839f7100def222aa4fb06724c32
Author: longlong yan <[email protected]>
libarena/selftests: use ARRAY_SIZE macro in st_asan_buddy.bpf.c

This commit replaces manual array size calculations with the ARRAY_SIZE macro in the asan_test_buddy_oob and asan_test_buddy_uaf functions to make the code more compact.

tools/testing/selftests/bpf/libarena/selftests/test_asan_buddy.bpf.c __weak int asan_test_buddy_oob(void) · line 166

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_asan_buddy.bpf.c +1 -1
index 3266a28f53d76..9c94a5167a503 100644
@@ -163,7 +163,7 @@ __weak int asan_test_buddy_oob(void)
 		return ret;
 	}
 
-	for (i = zero; i < sizeof(sizes) / sizeof(sizes[0]) && can_loop; i++) {
+	for (i = zero; i < ARRAY_SIZE(sizes) && can_loop; i++) {

[Severity: Low]
Does this cause a compilation failure when building the BPF selftests?

It appears that ARRAY_SIZE is not defined in this BPF program or its currently included headers. Would we need to define ARRAY_SIZE locally in this file, or include a header that provides it specifically for BPF environments?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to longlong yan
Quoted 22 lines · longlong yan

The ARRAY_SIZE macro is more compact and more formal in linux source.

Fixes b1487dc1b181 ("selftests/bpf: Add selftests for libarena buddy allocator")
---
 .../selftests/bpf/libarena/selftests/test_asan_buddy.bpf.c    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/libarena/selftests/test_asan_buddy.bpf.c +1 -1
index 3266a28f53d7..9c94a5167a50 100644
@@ -163,7 +163,7 @@ __weak int asan_test_buddy_oob(void)
 		return ret;
 	}
 
-	for (i = zero; i < sizeof(sizes) / sizeof(sizes[0]) && can_loop; i++) {
+	for (i = zero; i < ARRAY_SIZE(sizes) && can_loop; i++) {
 		barrier_var(i);
 		ret = asan_test_buddy_oob_single(sizes[i]);
 		if (ret) {

tools/testing/selftests/bpf/libarena/selftests/test_asan_buddy.bpf.c __weak int asan_test_buddy_uaf(void) · line 207

Quoted 8 lines
tools/testing/selftests/bpf/libarena/selftests/test_asan_buddy.bpf.c +1 -1
@@ -201,7 +201,7 @@ __weak int asan_test_buddy_uaf(void)
 		return ret;
 	}
 
-	for (i = zero; i < sizeof(sizes) / sizeof(sizes[0]) && can_loop; i++) {
+	for (i = zero; i < ARRAY_SIZE(sizes) && can_loop; i++) {
 		barrier_var(i);
 		ret = asan_test_buddy_uaf_single(sizes[i]);
 		if (ret) {

As the bot says, this doesn't compile. The sizeof(array) / sizeof(array[0]) is only present in libarena for selftests, so I it's not worth bringing in the macro or adding extra headers even if it makes the code nicer.

pw-bot: cr