kselftest/arm64: various compilation fixes
cover kselftest/arm64: various compilation fixes
This fixes several smaller issues I faced when compiling the arm64 kselftests on my machine.
Patch 1 avoids a warning about the double definition of GNU_SOURCE, for the arm64/signal tests. Patch 2 fixes a typo, where the f8dp2 hwcap feature test was looking at the f8dp*4* cpuinfo name. Patch 3 adjusts the output of the MTE tests when MTE is not available, so that tools parsing the TAP output don't get confused and report errors. The remaining patches are about wrong printf format specifiers. I grouped them by type of error, in patch 4-8.
Please have a look!
Cheers,
Andre
Andre Przywara (8):
kselftest/arm64: signal: drop now redundant GNU_SOURCE definition kselftest/arm64: hwcap: fix f8dp2 cpuinfo name kselftest/arm64: mte: use proper SKIP syntax kselftest/arm64: mte: use string literal for printf-style functions kselftest/arm64: mte: fix printf type warning about mask kselftest/arm64: mte: fix printf type warnings about __u64 kselftest/arm64: mte: fix printf type warnings about pointers kselftest/arm64: mte: fix printf type warnings about longs
tools/testing/selftests/arm64/abi/hwcap.c | 2 +- .../selftests/arm64/mte/check_buffer_fill.c | 4 ++-- tools/testing/selftests/arm64/mte/check_prctl.c | 4 ++-- .../selftests/arm64/mte/check_tags_inclusion.c | 4 ++-- .../testing/selftests/arm64/mte/mte_common_util.c | 15 +++++++-------- .../testing/selftests/arm64/mte/mte_common_util.h | 6 +++--- tools/testing/selftests/arm64/signal/Makefile | 2 +- 7 files changed, 18 insertions(+), 19 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/8 kselftest/arm64: signal: drop now redundant GNU_SOURCE definition
The definition of GNU_SOURCE was recently centralised in an upper layer kselftest Makefile, so the definition in the arm64 signal tests Makefile is no longer needed. To make things worse, since both definitions are not strictly identical, the compiler warns about it:
<command-line>: warning: "_GNU_SOURCE" redefined <command-line>: note: this is the location of the previous definition
Drop the definition in the arm64/signal Makefile.
--- tools/testing/selftests/arm64/signal/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/arm64/signal/Makefile +1 -1
@@ -2,7 +2,7 @@ # Copyright (C) 2019 ARM Limited # Additional include paths needed by kselftest.h and local headers -CFLAGS += -D_GNU_SOURCE -std=gnu99 -I. +CFLAGS += -std=gnu99 -I. SRCS := $(filter-out testcases/testcases.c,$(wildcard testcases/*.c)) PROGS := $(patsubst %.c,%,$(SRCS))
Signature · 1 line
2.25.1
Re: [PATCH 1/8] kselftest/arm64: signal: drop now redundant GNU_SOURCE definition
Quoted 6 lines · Andre Przywara
The definition of
GNU_SOURCEwas recently centralised in an upper layer kselftestMakefile, so the definition in the arm64 signal testsMakefileis no longer needed. To make things worse, since both definitions are not strictly identical, the compiler warns about it:
<command-line>: warning: "_GNU_SOURCE" redefined <command-line>: note: this is the location of the previous definition
PATCH 2/8 kselftest/arm64: hwcap: fix f8dp2 cpuinfo name
In reply to Andre PrzywaraThe F8DP2 DPISA extension has a separate cpuinfo field, named accordingly.
Change the erroneously placed name of "f8dp4" to "f8dp2".
--- tools/testing/selftests/arm64/abi/hwcap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/arm64/abi/hwcap.c +1 -1
@@ -484,7 +484,7 @@ static const struct hwcap_data { .name = "F8DP2", .at_hwcap = AT_HWCAP2, .hwcap_bit = HWCAP2_F8DP2, - .cpuinfo = "f8dp4", + .cpuinfo = "f8dp2", .sigill_fn = f8dp2_sigill, }, {
Signature · 1 line
2.25.1
Re: [PATCH 2/8] kselftest/arm64: hwcap: fix f8dp2 cpuinfo name
Quoted 3 lines · Andre Przywara
The F8DP2 DPISA extension has a separate cpuinfo field, named accordingly.
Change the erroneously placed name of "f8dp4" to "f8dp2".
PATCH 3/8 kselftest/arm64: mte: use proper SKIP syntax
In reply to Andre PrzywaraIf MTE is not available on a system, we detect this early and skip all the MTE selftests. However this happens before we print the TAP plan, so tools parsing the TAP output get confused and report an error.
Use the existing ksft_exit_skip() function to handle this, which uses a dummy plan to work with tools expecting proper TAP syntax, as described in the TAP specification.
--- tools/testing/selftests/arm64/mte/mte_common_util.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
tools/testing/selftests/arm64/mte/mte_common_util.c +3 -4
@@ -319,10 +319,9 @@ int mte_default_setup(void)unsigned long en =0 ;int ret; -if (!(hwcaps2 &HWCAP2_MTE )) { -ksft_print_msg ("SKIP: MTE features unavailable\n" ); -return KSFT_SKIP ; - } +if (!(hwcaps2 &HWCAP2_MTE )) +ksft_exit_skip ("MTE features unavailable\n" ); +/* Get current mte mode */ ret =prctl (PR_GET_TAGGED_ADDR_CTRL , en,0 ,0 ,0 );if (ret <0 ) {
Signature · 1 line
2.25.1
Re: [PATCH 3/8] kselftest/arm64: mte: use proper SKIP syntax
Quoted 3 lines · Andre Przywara
If MTE is not available on a system, we detect this early and skip all the MTE selftests. However this happens before we print the TAP plan, so tools parsing the TAP output get confused and report an error.
PATCH 4/8 kselftest/arm64: mte: use string literal for printf-style functions
In reply to Andre PrzywaraUsing pointers for the format specifier strings in printf-style functions can create potential security problems, as the number of arguments to be parsed could vary from call to call. Most compilers consequently warn about those:
"format not a string literal and no format arguments [-Wformat-security]"
If we only want to print a constant string, we can just use a fixed "%s" format instead, and pass the string as an argument.
--- tools/testing/selftests/arm64/mte/mte_common_util.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/arm64/mte/mte_common_util.h +3 -3
@@ -77,13 +77,13 @@ static inline void evaluate_test(int err, const char *msg) {switch (err) {case KSFT_PASS : -ksft_test_result_pass (msg); +ksft_test_result_pass ("%s" , msg);break ;case KSFT_FAIL : -ksft_test_result_fail (msg); +ksft_test_result_fail ("%s" , msg);break ;case KSFT_SKIP : -ksft_test_result_skip (msg); +ksft_test_result_skip ("%s" , msg);break ;default : ksft_test_result_error("Unknown return code %d from %s",
Signature · 1 line
2.25.1
Re: [PATCH 4/8] kselftest/arm64: mte: use string literal for printf-style functions
Quoted 10 lines · Andre Przywara
Using pointers for the format specifier strings in printf-style functions can create potential security problems, as the number of arguments to be parsed could vary from call to call. Most compilers consequently warn about those:
"format not a string literal and no format arguments [-Wformat-security]"If we only want to print a constant string, we can just use a fixed "%s" format instead, and pass the string as an argument.
Fixes e9b60476bea0 ("kselftest/arm64: Add utilities and a test to validate mte memory")
I'm not sure this qualifies as a fix given that all the strings we're passing in here are trusted... otheriwse this looks good.
PATCH 5/8 kselftest/arm64: mte: fix printf type warning about mask
In reply to Andre PrzywaraWhen masking the return value of a prctl, which is clearly an "int", we use a uapi header provided mask, which is defined using an "UL" modifier, so the whole expression is promoted to a long. This upsets the compiler's printf type checker, because we use "%x" in the format string.
While we could simply upgrade this to a "%lx", it sounds wrong to promote the "ret" variable, that is clearly an int. Downcast the mask instead, to keep the type correct.
--- tools/testing/selftests/arm64/mte/check_prctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/arm64/mte/check_prctl.c +2 -2
@@ -81,11 +81,11 @@ void set_mode_test(const char *name, int hwcap2, int mask)return ; } -if ((ret &PR_MTE_TCF_MASK ) == mask) { +if ((ret & (int )PR_MTE_TCF_MASK ) == mask) {ksft_test_result_pass ("%s\n" , name); }else {ksft_print_msg ("Got %x, expected %x\n" , - (ret &PR_MTE_TCF_MASK ), mask); + (ret & (int )PR_MTE_TCF_MASK ), mask);ksft_test_result_fail ("%s\n" , name); } }
Signature · 1 line
2.25.1
Re: [PATCH 5/8] kselftest/arm64: mte: fix printf type warning about mask
Quoted 8 lines · Andre Przywara
When masking the return value of a prctl, which is clearly an "int", we use a uapi header provided mask, which is defined using an "UL" modifier, so the whole expression is promoted to a long. This upsets the compiler's printf type checker, because we use "%x" in the format string.
While we could simply upgrade this to a "%lx", it sounds wrong to promote the "ret" variable, that is clearly an int. Downcast the mask instead, to keep the type correct.
This suggests that we've got some confusion with the UAPI, these flags need to go through a prctl() return so they shouldn't be unsigned long... That said, it's UAPI so I'm not sure that's fixable.
tools/testing/selftests/arm64/mte/check_prctl.c void set_mode_test(const char *name, int hwcap2, int mask) · line 89
Quoted 8 lines
tools/testing/selftests/arm64/mte/check_prctl.c +2
-2-if ((ret &PR_MTE_TCF_MASK ) == mask) { +if ((ret & (int )PR_MTE_TCF_MASK ) == mask) {ksft_test_result_pass ("%s\n" , name); }else {ksft_print_msg ("Got %x, expected %x\n" , - (ret &PR_MTE_TCF_MASK ), mask); + (ret & (int )PR_MTE_TCF_MASK ), mask);ksft_test_result_fail ("%s\n" , name);
TBH my inclination is that this is worse than letting the value be promoted, casts (particularly casts of constants) are just obviously suspect in a way that printf() formats aren't.
Re: [PATCH 5/8] kselftest/arm64: mte: fix printf type warning about mask
On Fri, 16 Aug 2024 17:30:14 +0100
Mark Brown <[email protected]> wrote:
Hi Mark,
thanks for having a look!
Quoted 6 lines · Andre Przywara
Quoted 5 lines
When masking the return value of a prctl, which is clearly an "int", we use a uapi header provided mask, which is defined using an "UL" modifier, so the whole expression is promoted to a long. This upsets the compiler's printf type checker, because we use "%x" in the format string.
Quoted 7 lines
Quoted 3 lines
While we could simply upgrade this to a "%lx", it sounds wrong to promote the "ret" variable, that is clearly an int. Downcast the mask instead, to keep the type correct.
This suggests that we've got some confusion with the UAPI, these flags need to go through a
prctl()return so they shouldn't be unsigned long... That said, it's UAPI so I'm not sure that's fixable.
My thoughts, exactly. Not sure if this mask is used on some larger types somewhere else, but it's indeed moot since it's UAPI.
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/arm64/mte/check_prctl.c +1
-1-if ((ret &PR_MTE_TCF_MASK ) == mask) { +if ((ret & (int )PR_MTE_TCF_MASK ) == mask) {ksft_test_result_pass ("%s\n" , name); }else {
Quoted 8 lines
tools/testing/selftests/arm64/mte/check_prctl.c void set_mode_test(const char *name, int hwcap2, int mask) · line 89
Quoted 4 lines
tools/testing/selftests/arm64/mte/check_prctl.c +1
-1ksft_print_msg ("Got %x, expected %x\n" , - (ret &PR_MTE_TCF_MASK ), mask); + (ret & (int )PR_MTE_TCF_MASK ), mask);ksft_test_result_fail ("%s\n" , name);TBH my inclination is that this is worse than letting the value be promoted, casts (particularly casts of constants) are just obviously suspect in a way that
printf()formats aren't.
Fair enough, I wasn't sure about this either, and indeed this down-cast of a constant smells dodgy. So shall I just use %lx, and rely on the promotion (so the MASK being defined as UL), or cast "ret" to long?
Cheers,
Andre
Re: [PATCH 5/8] kselftest/arm64: mte: fix printf type warning about mask
Quoted 11 lines · Andre Przywara
Quoted 9 lines · Mark Brown
tools/testing/selftests/arm64/mte/check_prctl.c void set_mode_test(const char *name, int hwcap2, int mask) · line 88
Quoted 3 lines · Andre Przywara
tools/testing/selftests/arm64/mte/check_prctl.c +1
-1ksft_print_msg ("Got %x, expected %x\n" , - (ret &PR_MTE_TCF_MASK ), mask); + (ret & (int )PR_MTE_TCF_MASK ), mask);TBH my inclination is that this is worse than letting the value be promoted, casts (particularly casts of constants) are just obviously suspect in a way that
printf()formats aren't.
Quoted 3 lines
Fair enough, I wasn't sure about this either, and indeed this down-cast of a constant smells dodgy. So shall I just use %lx, and rely on the promotion (so the MASK being defined as UL), or cast "ret" to long?
My inclination would be to go with the former.
PATCH 6/8 kselftest/arm64: mte: fix printf type warnings about __u64
In reply to Andre PrzywaraWhen printing the signal context's PC, we use a "%lx" format specifier, which matches the common userland (glibc's) definition of uint64_t as an "unsigned long". However the structure in question is defined in a kernel uapi header, which uses a self defined __u64 type, and the arm64 kernel headers define this using "int-ll64.h", so it becomes an "unsigned long long". This mismatch leads to the usual compiler warning.
The common fix would be to use "PRIx64", but because this is defined by the userland's toolchain libc headers, it wouldn't match as well. Since we know the exact type of __u64, just use "%llx" here instead, to silence this warning.
This also fixes a more severe typo: "$lx" is not a valid format specifier.
--- tools/testing/selftests/arm64/mte/mte_common_util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/arm64/mte/mte_common_util.c +2 -2
@@ -38,7 +38,7 @@ void mte_default_handler(int signum, siginfo_t *si, void *uc)if (cur_mte_cxt.trig_si_code == si->si_code) cur_mte_cxt.fault_valid = true;else -ksft_print_msg ("Got unexpected SEGV_MTEAERR at pc=$lx, fault addr=%lx\n" , +ksft_print_msg ("Got unexpected SEGV_MTEAERR at pc=%llx, fault addr=%lx\n" , ((ucontext_t *)uc)->uc_mcontext.pc, addr);return ;
@@ -64,7 +64,7 @@ void mte_default_handler(int signum, siginfo_t *si, void *uc)exit (1 ); } }else if (signum ==SIGBUS ) { -ksft_print_msg ("INFO: SIGBUS signal at pc=%lx, fault addr=%lx, si_code=%lx\n" , +ksft_print_msg ("INFO: SIGBUS signal at pc=%llx, fault addr=%lx, si_code=%x\n" , ((ucontext_t *)uc)->uc_mcontext.pc, addr, si->si_code); if ((cur_mte_cxt.trig_range >= 0 && addr >= MT_CLEAR_TAG(cur_mte_cxt.trig_addr) &&
Signature · 1 line
2.25.1
Re: [PATCH 6/8] kselftest/arm64: mte: fix printf type warnings about __u64
Quoted 6 lines · Andre Przywara
When printing the signal context's PC, we use a "%lx" format specifier, which matches the common userland (glibc's) definition of
uint64_tas an "unsigned long". However the structure in question is defined in a kernel uapi header, which uses a self defined__u64type, and the arm64 kernel headers define this using "int-ll64.h", so it becomes an "unsigned long long". This mismatch leads to the usual compiler warning.
PATCH 7/8 kselftest/arm64: mte: fix printf type warnings about pointers
In reply to Andre PrzywaraWhen printing the value of a pointer, we should not use an integer format specifier, but the dedicated "%p" instead.
--- tools/testing/selftests/arm64/mte/check_buffer_fill.c | 4 ++-- tools/testing/selftests/arm64/mte/mte_common_util.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/arm64/mte/check_buffer_fill.c +2 -2
@@ -91,7 +91,7 @@ static int check_buffer_underflow_by_byte(int mem_type, int mode,for (j =0 ; j < sizes[i]; j++) {if (ptr[j] !='1' ) { err = true; -ksft_print_msg ("Buffer is not filled at index:%d of ptr:0x%lx\n" , +ksft_print_msg ("Buffer is not filled at index:%d of ptr:0x%p\n" , j, ptr);break ; }
@@ -189,7 +189,7 @@ static int check_buffer_overflow_by_byte(int mem_type, int mode,for (j =0 ; j < sizes[i]; j++) {if (ptr[j] !='1' ) { err = true; -ksft_print_msg ("Buffer is not filled at index:%d of ptr:0x%lx\n" , +ksft_print_msg ("Buffer is not filled at index:%d of ptr:0x%p\n" , j, ptr);break ; }
tools/testing/selftests/arm64/mte/mte_common_util.c +2 -2
@@ -100,7 +100,7 @@ void *mte_insert_tags(void *ptr, size_t size)int align_size;if (!ptr || (unsigned long )(ptr) &MT_ALIGN_GRANULE ) { -ksft_print_msg ("FAIL: Addr=%lx: invalid\n" , ptr); +ksft_print_msg ("FAIL: Addr=%p: invalid\n" , ptr);return NULL ; } align_size =MT_ALIGN_UP (size);
@@ -112,7 +112,7 @@ void *mte_insert_tags(void *ptr, size_t size)void mte_clear_tags (void *ptr,size_t size) {if (!ptr || (unsigned long )(ptr) &MT_ALIGN_GRANULE ) { -ksft_print_msg ("FAIL: Addr=%lx: invalid\n" , ptr); +ksft_print_msg ("FAIL: Addr=%p: invalid\n" , ptr);return ; } size =MT_ALIGN_UP (size);
Signature · 1 line
2.25.1
Re: [PATCH 7/8] kselftest/arm64: mte: fix printf type warnings about pointers
Quoted 4 lines · Andre Przywara
When printing the value of a pointer, we should not use an integer format specifier, but the dedicated "%p" instead.
Fixes e9b60476bea0 ("kselftest/arm64: Add utilities and a test to validate mte memory")
This is another one where calling it a fix seems like it's pushing it, it's a modernisation rather than a correctness thing. Otherwise
Re: [PATCH 7/8] kselftest/arm64: mte: fix printf type warnings about pointers
Quoted 8 lines · On Fri, 16 Aug 2024 17:32:39 +0100 Mark Brown <[email protected]> wrote:
Quoted 4 lines · Andre Przywara
When printing the value of a pointer, we should not use an integer format specifier, but the dedicated "%p" instead.
Fixes e9b60476bea0 ("kselftest/arm64: Add utilities and a test to validate mte memory")This is another one where calling it a fix seems like it's pushing it, it's a modernisation rather than a correctness thing.
Well, I get compiler warnings, so I thought "fix" would be adequate. But in general this confusion between pointers and integers sounds not good, and not using %p looks like a genuine bug to me (though it's admittedly working fine (TM) for now).
Quoted 3 lines
Otherwise
Reviewed-by Mark Brown <[email protected]>
Thanks for that!
Cheers,
Andre
Re: [PATCH 7/8] kselftest/arm64: mte: fix printf type warnings about pointers
Quoted 5 lines · Andre Przywara
Quoted 3 lines · Mark Brown
Quoted 1 line · Andre Przywara
Fixes e9b60476bea0 ("kselftest/arm64: Add utilities and a test to validate mte memory")
Quoted 7 lines
Quoted 2 lines
This is another one where calling it a fix seems like it's pushing it, it's a modernisation rather than a correctness thing.
Well, I get compiler warnings, so I thought "fix" would be adequate. But in general this confusion between pointers and integers sounds not good, and not using %p looks like a genuine bug to me (though it's admittedly working fine (TM) for now).
Those compiler warnings are a relatively new thing - they won't have been triggered when the code was written, and practically speaking on arm64 it's more of a C language correctness thing than a meaningful change.
PATCH 8/8 kselftest/arm64: mte: fix printf type warnings about longs
In reply to Andre PrzywaraWhen checking MTE tags, we print some diagnostic messages when the tests fail. Some variables uses there are "longs", however we only use "%x" for the format specifier.
Update the format specifiers to "%lx", to match the variable types they are supposed to print.
--- tools/testing/selftests/arm64/mte/check_tags_inclusion.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/arm64/mte/check_tags_inclusion.c +2 -2
@@ -65,7 +65,7 @@ static int check_single_included_tags(int mem_type, int mode) ptr =mte_insert_tags (ptr,BUFFER_SIZE );/* Check tag value */ if (MT_FETCH_TAG ((uintptr_t )ptr) == tag) { -ksft_print_msg ("FAIL: wrong tag = 0x%x with include mask=0x%x\n" , +ksft_print_msg ("FAIL: wrong tag = 0x%lx with include mask=0x%x\n" ,MT_FETCH_TAG ((uintptr_t )ptr),MT_INCLUDE_VALID_TAG (tag)); result =KSFT_FAIL ;
@@ -97,7 +97,7 @@ static int check_multiple_included_tags(int mem_type, int mode) ptr =mte_insert_tags (ptr,BUFFER_SIZE );/* Check tag value */ if (MT_FETCH_TAG ((uintptr_t )ptr) < tag) { -ksft_print_msg ("FAIL: wrong tag = 0x%x with include mask=0x%x\n" , +ksft_print_msg ("FAIL: wrong tag = 0x%lx with include mask=0x%lx\n" ,MT_FETCH_TAG ((uintptr_t )ptr),MT_INCLUDE_VALID_TAGS (excl_mask)); result =KSFT_FAIL ;
Signature · 1 line
2.25.1
Re: [PATCH 8/8] kselftest/arm64: mte: fix printf type warnings about longs
Quoted 3 lines · Andre Przywara
When checking MTE tags, we print some diagnostic messages when the tests fail. Some variables uses there are "longs", however we only use "%x" for the format specifier.
Catalin Marinas
maintainerRe: (subset) [PATCH 0/8] kselftest/arm64: various compilation fixes
In reply to Andre PrzywaraQuoted 11 lines · Andre Przywara
This fixes several smaller issues I faced when compiling the arm64 kselftests on my machine.
Patch 1 avoids a warning about the double definition ofGNU_SOURCE, for the arm64/signal tests. Patch 2 fixes a typo, where the f8dp2 hwcap feature test was looking at the f8dp*4* cpuinfo name. Patch 3 adjusts the output of the MTE tests when MTE is not available, so that tools parsing the TAP output don't get confused and report errors. The remaining patches are about wrong printf format specifiers. I grouped them by type of error, in patch 4-8.[...]
Applied to arm64 (for-next/kselftest), thanks!
I skiped patch 5 since Mark wanted it handled differently. Andre, would you mind respinning that patch?
[1/8] kselftest/arm64: signal: drop now redundant GNU_SOURCE definition
https://git.kernel.org/arm64/c/a2aa5dcc6393 [2/8] kselftest/arm64: hwcap: fix f8dp2 cpuinfo name https://git.kernel.org/arm64/c/b0d80dbc378d [3/8] kselftest/arm64: mte: use proper SKIP syntax https://git.kernel.org/arm64/c/bf52ca5912c0 [4/8] kselftest/arm64: mte: use string literal for printf-style functions https://git.kernel.org/arm64/c/0f995f22a03f [6/8] kselftest/arm64: mte: fix printf type warnings about __u64 https://git.kernel.org/arm64/c/7e893dc81de3 [7/8] kselftest/arm64: mte: fix printf type warnings about pointers https://git.kernel.org/arm64/c/4716f719202e [8/8] kselftest/arm64: mte: fix printf type warnings about longs https://git.kernel.org/arm64/c/96dddb7b9406
Signature · 1 line
Catalin