* [PATCH] selftests/mm: Fix thread state check in uffd-unit-tests
@ 2025-12-10 9:14 Wake Liu
2025-12-10 19:51 ` Peter Xu
2025-12-16 6:15 ` Mike Rapoport
0 siblings, 2 replies; 3+ messages in thread
From: Wake Liu @ 2025-12-10 9:14 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Shuah Khan, Nathan Chancellor
Cc: Peter Xu, Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka,
Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
Nick Desaulniers, Bill Wendling, Justin Stitt, linux-mm,
linux-kselftest, linux-kernel, llvm, Wake Liu
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.
Signed-off-by: Wake Liu <wakel@google.com>
---
tools/testing/selftests/mm/uffd-unit-tests.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c
index f4807242c5b2..6f5e404a446c 100644
--- a/tools/testing/selftests/mm/uffd-unit-tests.c
+++ b/tools/testing/selftests/mm/uffd-unit-tests.c
@@ -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;
}
--
2.52.0.223.gf5cc29aaa4-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/mm: Fix thread state check in uffd-unit-tests
2025-12-10 9:14 [PATCH] selftests/mm: Fix thread state check in uffd-unit-tests Wake Liu
@ 2025-12-10 19:51 ` Peter Xu
2025-12-16 6:15 ` Mike Rapoport
1 sibling, 0 replies; 3+ messages in thread
From: Peter Xu @ 2025-12-10 19:51 UTC (permalink / raw)
To: Wake Liu
Cc: Andrew Morton, David Hildenbrand, Shuah Khan, Nathan Chancellor,
Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka,
Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
Nick Desaulniers, Bill Wendling, Justin Stitt, linux-mm,
linux-kselftest, linux-kernel, llvm
On Wed, Dec 10, 2025 at 05:14:08PM +0800, Wake Liu wrote:
> 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.
>
> Signed-off-by: Wake Liu <wakel@google.com>
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.
Acked-by: Peter Xu <peterx@redhat.com>
Thanks,
> ---
> tools/testing/selftests/mm/uffd-unit-tests.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c
> index f4807242c5b2..6f5e404a446c 100644
> --- a/tools/testing/selftests/mm/uffd-unit-tests.c
> +++ b/tools/testing/selftests/mm/uffd-unit-tests.c
> @@ -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;
> }
> --
> 2.52.0.223.gf5cc29aaa4-goog
>
--
Peter Xu
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/mm: Fix thread state check in uffd-unit-tests
2025-12-10 9:14 [PATCH] selftests/mm: Fix thread state check in uffd-unit-tests Wake Liu
2025-12-10 19:51 ` Peter Xu
@ 2025-12-16 6:15 ` Mike Rapoport
1 sibling, 0 replies; 3+ messages in thread
From: Mike Rapoport @ 2025-12-16 6:15 UTC (permalink / raw)
To: Wake Liu
Cc: Andrew Morton, David Hildenbrand, Shuah Khan, Nathan Chancellor,
Peter Xu, Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka,
Suren Baghdasaryan, Michal Hocko, Nick Desaulniers,
Bill Wendling, Justin Stitt, linux-mm, linux-kselftest,
linux-kernel, llvm
On Wed, Dec 10, 2025 at 05:14:08PM +0800, Wake Liu wrote:
> 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.
>
> Signed-off-by: Wake Liu <wakel@google.com>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> ---
> tools/testing/selftests/mm/uffd-unit-tests.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c
> index f4807242c5b2..6f5e404a446c 100644
> --- a/tools/testing/selftests/mm/uffd-unit-tests.c
> +++ b/tools/testing/selftests/mm/uffd-unit-tests.c
> @@ -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;
> }
> --
> 2.52.0.223.gf5cc29aaa4-goog
>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-12-16 6:15 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-10 9:14 [PATCH] selftests/mm: Fix thread state check in uffd-unit-tests Wake Liu
2025-12-10 19:51 ` Peter Xu
2025-12-16 6:15 ` Mike Rapoport
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox