selftests/mm: Fix thread state check in uffd-unit-tests
Subsystems (4)
PATCH 1/1 selftests/mm: Fix thread state check in uffd-unit-tests
In the thread_state_get() function, the logic to find the thread's state character was using sizeof(header) - 1 to calculate the offset from the "State:\t" string.
The header variable is a const char * pointer. sizeof() on a pointer returns the size of the pointer itself, not the length of the string literal it points to. This makes the code's behavior dependent on the architecture's pointer size.
This bug was identified on a 32-bit ARM build (gsi_tv_arm) for Android, running on an ARMv8-based device, compiled with Clang 19.0.1.
On this 32-bit architecture, sizeof(char *) is 4. The expression sizeof(header) - 1 resulted in an incorrect offset of 3, causing the test to read the wrong character from /proc/[tid]/status and fail.
On 64-bit architectures, sizeof(char *) is 8, so the expression coincidentally evaluates to 7, which matches the length of "State:\t". This is why the bug likely remained hidden on 64-bit builds.
To fix this and make the code portable and correct across all architectures, this patch replaces sizeof(header) - 1 with strlen(header). The strlen() function correctly calculates the string's length, ensuring the correct offset is always used.
--- tools/testing/selftests/mm/uffd-unit-tests.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/uffd-unit-tests.c +1 -1
@@ -1317,7 +1317,7 @@ static thread_state thread_state_get(pid_t tid) p =strstr (tmp, header);if (p) {/* For example, "State:\tD (disk sleep)" */ - c = *(p +sizeof (header) -1 ); + c = *(p +strlen (header));return c =='D' ?THR_STATE_UNINTERRUPTIBLE :THR_STATE_UNKNOWN ; }
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Quoted 20 lines · Wake Liu
In the
thread_state_get()function, the logic to find the thread's state character was usingsizeof(header) - 1to calculate the offset from the "State:\t" string.The
headervariable is aconst char *pointer.sizeof()on a pointer returns the size of the pointer itself, not the length of the string literal it points to. This makes the code's behavior dependent on the architecture's pointer size.This bug was identified on a 32-bit ARM build (
gsi_tv_arm) for Android, running on an ARMv8-based device, compiled with Clang 19.0.1.On this 32-bit architecture,
sizeof(char *)is 4. The expressionsizeof(header) - 1resulted in an incorrect offset of 3, causing the test to read the wrong character from/proc/[tid]/statusand fail.On 64-bit architectures,
sizeof(char *)is 8, so the expression coincidentally evaluates to 7, which matches the length of "State:\t". This is why the bug likely remained hidden on 64-bit builds.
Quoted 6 lines
To fix this and make the code portable and correct across all architectures, this patch replaces
sizeof(header) - 1withstrlen(header). Thestrlen()function correctly calculates the string's length, ensuring the correct offset is always used.Signed-off-by Wake Liu <[email protected]>
Oops, thanks for spotting it. It was an accident the size of array is 8 here.. What I should have meant was:
const char header[] = "State:\t";
That should also work with sizeof(). But your fix works, so it's all fine.
Thanks,
Quoted 20 lines
--- tools/testing/selftests/mm/uffd-unit-tests.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/uffd-unit-tests.c +1
-1@@ -1317,7 +1317,7 @@ static thread_state thread_state_get(pid_t tid) p =strstr (tmp, header);if (p) {/* For example, "State:\tD (disk sleep)" */ - c = *(p +sizeof (header) -1 ); + c = *(p +strlen (header));return c =='D' ?THR_STATE_UNINTERRUPTIBLE :THR_STATE_UNKNOWN ; }Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Signature · 1 line
Peter Xu
Quoted 20 lines · Wake Liu
In the
thread_state_get()function, the logic to find the thread's state character was usingsizeof(header) - 1to calculate the offset from the "State:\t" string.The
headervariable is aconst char *pointer.sizeof()on a pointer returns the size of the pointer itself, not the length of the string literal it points to. This makes the code's behavior dependent on the architecture's pointer size.This bug was identified on a 32-bit ARM build (
gsi_tv_arm) for Android, running on an ARMv8-based device, compiled with Clang 19.0.1.On this 32-bit architecture,
sizeof(char *)is 4. The expressionsizeof(header) - 1resulted in an incorrect offset of 3, causing the test to read the wrong character from/proc/[tid]/statusand fail.On 64-bit architectures,
sizeof(char *)is 8, so the expression coincidentally evaluates to 7, which matches the length of "State:\t". This is why the bug likely remained hidden on 64-bit builds.
Quoted 6 lines
To fix this and make the code portable and correct across all architectures, this patch replaces
sizeof(header) - 1withstrlen(header). Thestrlen()function correctly calculates the string's length, ensuring the correct offset is always used.Signed-off-by Wake Liu <[email protected]>
Quoted 20 lines
--- tools/testing/selftests/mm/uffd-unit-tests.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/uffd-unit-tests.c +1
-1@@ -1317,7 +1317,7 @@ static thread_state thread_state_get(pid_t tid) p =strstr (tmp, header);if (p) {/* For example, "State:\tD (disk sleep)" */ - c = *(p +sizeof (header) -1 ); + c = *(p +strlen (header));return c =='D' ?THR_STATE_UNINTERRUPTIBLE :THR_STATE_UNKNOWN ; }Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Signature · 2 lines
Sincerely yours, Mike.