2023-03-08 19:59:44

by Nick Desaulniers

[permalink] [raw]
Subject: [PATCH] selftests: sigaltstack: fix -Wuninitialized

Building sigaltstack with clang via:
$ ARCH=x86 make LLVM=1 -C tools/testing/selftests/sigaltstack/

produces the following warning:
warning: variable 'sp' is uninitialized when used here [-Wuninitialized]
if (sp < (unsigned long)sstack ||
^~

Clang expects these to be declared at global scope; we've fixed this in
the kernel proper by using the macro `current_stack_pointer`. This is
defined in different headers for different target architectures, so just
create a new header that defines the arch-specific register names for
the stack pointer register, and define it for more targets (at least the
ones that support current_stack_pointer/ARCH_HAS_CURRENT_STACK_POINTER).

Reported-by: Linux Kernel Functional Testing <[email protected]>
Link: https://lore.kernel.org/lkml/CA+G9fYsi3OOu7yCsMutpzKDnBMAzJBCPimBp86LhGBa0eCnEpA@mail.gmail.com/
Signed-off-by: Nick Desaulniers <[email protected]>
---
Cc: Naresh Kamboju <[email protected]>
Cc: KERNEL SELFTEST FRAMEWORK <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: Shuah Khan <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: "Chang S. Bae" <[email protected]>
Cc: Len Brown <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Stas Sergeev <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Anders Roxell <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: [email protected]
---
.../sigaltstack/current_stack_pointer.h | 23 +++++++++++++++++++
tools/testing/selftests/sigaltstack/sas.c | 7 +-----
2 files changed, 24 insertions(+), 6 deletions(-)
create mode 100644 tools/testing/selftests/sigaltstack/current_stack_pointer.h

diff --git a/tools/testing/selftests/sigaltstack/current_stack_pointer.h b/tools/testing/selftests/sigaltstack/current_stack_pointer.h
new file mode 100644
index 000000000000..ea9bdf3a90b1
--- /dev/null
+++ b/tools/testing/selftests/sigaltstack/current_stack_pointer.h
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#if __alpha__
+register unsigned long sp asm("$30");
+#elif __arm__ || __aarch64__ || __csky__ || __m68k__ || __mips__ || __riscv
+register unsigned long sp asm("sp");
+#elif __i386__
+register unsigned long sp asm("esp");
+#elif __loongarch64
+register unsigned long sp asm("$sp");
+#elif __ppc__
+register unsigned long sp asm("r1");
+#elif __s390x__
+register unsigned long sp asm("%15");
+#elif __sh__
+register unsigned long sp asm("r15");
+#elif __x86_64__
+register unsigned long sp asm("rsp");
+#elif __XTENSA__
+register unsigned long sp asm("a1");
+#else
+#error "implement current_stack_pointer equivalent"
+#endif
diff --git a/tools/testing/selftests/sigaltstack/sas.c b/tools/testing/selftests/sigaltstack/sas.c
index c53b070755b6..98d37cb744fb 100644
--- a/tools/testing/selftests/sigaltstack/sas.c
+++ b/tools/testing/selftests/sigaltstack/sas.c
@@ -20,6 +20,7 @@
#include <sys/auxv.h>

#include "../kselftest.h"
+#include "current_stack_pointer.h"

#ifndef SS_AUTODISARM
#define SS_AUTODISARM (1U << 31)
@@ -46,12 +47,6 @@ void my_usr1(int sig, siginfo_t *si, void *u)
stack_t stk;
struct stk_data *p;

-#if __s390x__
- register unsigned long sp asm("%15");
-#else
- register unsigned long sp asm("sp");
-#endif
-
if (sp < (unsigned long)sstack ||
sp >= (unsigned long)sstack + stack_size) {
ksft_exit_fail_msg("SP is not on sigaltstack\n");
--
2.40.0.rc0.216.gc4246ad0f0-goog



2023-03-09 00:11:53

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] selftests: sigaltstack: fix -Wuninitialized

On Wed, Mar 08, 2023 at 11:59:33AM -0800, Nick Desaulniers wrote:
> Building sigaltstack with clang via:
> $ ARCH=x86 make LLVM=1 -C tools/testing/selftests/sigaltstack/
>
> produces the following warning:
> warning: variable 'sp' is uninitialized when used here [-Wuninitialized]
> if (sp < (unsigned long)sstack ||
> ^~
>
> Clang expects these to be declared at global scope; we've fixed this in
> the kernel proper by using the macro `current_stack_pointer`. This is
> defined in different headers for different target architectures, so just
> create a new header that defines the arch-specific register names for
> the stack pointer register, and define it for more targets (at least the
> ones that support current_stack_pointer/ARCH_HAS_CURRENT_STACK_POINTER).
>
> Reported-by: Linux Kernel Functional Testing <[email protected]>
> Link: https://lore.kernel.org/lkml/CA+G9fYsi3OOu7yCsMutpzKDnBMAzJBCPimBp86LhGBa0eCnEpA@mail.gmail.com/
> Signed-off-by: Nick Desaulniers <[email protected]>

Looks good!

Reviewed-by: Kees Cook <[email protected]>

--
Kees Cook

2023-03-13 09:50:55

by Naresh Kamboju

[permalink] [raw]
Subject: Re: [PATCH] selftests: sigaltstack: fix -Wuninitialized

On Thu, 9 Mar 2023 at 01:29, Nick Desaulniers <[email protected]> wrote:
>
> Building sigaltstack with clang via:
> $ ARCH=x86 make LLVM=1 -C tools/testing/selftests/sigaltstack/
>
> produces the following warning:
> warning: variable 'sp' is uninitialized when used here [-Wuninitialized]
> if (sp < (unsigned long)sstack ||
> ^~
>
> Clang expects these to be declared at global scope; we've fixed this in
> the kernel proper by using the macro `current_stack_pointer`. This is
> defined in different headers for different target architectures, so just
> create a new header that defines the arch-specific register names for
> the stack pointer register, and define it for more targets (at least the
> ones that support current_stack_pointer/ARCH_HAS_CURRENT_STACK_POINTER).
>
> Reported-by: Linux Kernel Functional Testing <[email protected]>
> Link: https://lore.kernel.org/lkml/CA+G9fYsi3OOu7yCsMutpzKDnBMAzJBCPimBp86LhGBa0eCnEpA@mail.gmail.com/
> Signed-off-by: Nick Desaulniers <[email protected]>

Build and boot tested with clang-16 and tested sigaltstack
on arm64, armv7, FVP, x86_64 and i386 [1] & [2].
These tests were performed at Linaro test farm by Anders
with the help of tuxplan / tuxmake and tuxrun.

Tested-by: Linux Kernel Functional Testing <[email protected]>
Tested-by: Anders Roxell <[email protected]>


Build log:
-------
clang --target=aarch64-linux-gnu -fintegrated-as
-Werror=unknown-warning-option -Werror=ignored-optimization-argument
-Werror=option-ignored -Werror=unused-command-line-argument
--target=aarch64-linux-gnu -fintegrated-as -Wall sas.c -o
/home/tuxbuild/.cache/tuxmake/builds/1/build/kselftest/sigaltstack/sas

Test log:
----------

Linux version 6.3.0-rc1-next-20230310 (tuxmake@tuxmake) (Debian clang
version 16.0.0 (++20230228093516+60692a66ced6-1~exp1~20230228093525.41),
Debian LLD 16.0.0) #1 SMP PREEMPT @1678519789
...

[ 56.327569] kselftest: Running tests in sigaltstack
TAP version 13
1..1
# selftests: sigaltstack: sas
# # [NOTE] the stack size is 26400
# TAP version 13
# 1..3
# ok 1 Initial sigaltstack state was SS_DISABLE
# # [RUN] signal USR1
# ok 2 sigaltstack is disabled in sighandler
# # [RUN] switched to user ctx
# # [RUN] signal USR2
# # [OK] Stack preserved
# ok 3 sigaltstack is still SS_AUTODISARM after signal
# # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0
ok 1 selftests: sigaltstack: sas

Details of test log links provided [3].

> ---
> Cc: Naresh Kamboju <[email protected]>
> Cc: KERNEL SELFTEST FRAMEWORK <[email protected]>
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: Shuah Khan <[email protected]>
> Cc: Thomas Gleixner <[email protected]>
> Cc: "Chang S. Bae" <[email protected]>
> Cc: Len Brown <[email protected]>
> Cc: Borislav Petkov <[email protected]>
> Cc: Stas Sergeev <[email protected]>
> Cc: Arnd Bergmann <[email protected]>
> Cc: Anders Roxell <[email protected]>
> Cc: Andy Lutomirski <[email protected]>
> Cc: Kees Cook <[email protected]>
> Cc: Nathan Chancellor <[email protected]>
> Cc: [email protected]
> ---
> .../sigaltstack/current_stack_pointer.h | 23 +++++++++++++++++++
> tools/testing/selftests/sigaltstack/sas.c | 7 +-----
> 2 files changed, 24 insertions(+), 6 deletions(-)
> create mode 100644 tools/testing/selftests/sigaltstack/current_stack_pointer.h
>
> diff --git a/tools/testing/selftests/sigaltstack/current_stack_pointer.h b/tools/testing/selftests/sigaltstack/current_stack_pointer.h
> new file mode 100644
> index 000000000000..ea9bdf3a90b1
> --- /dev/null
> +++ b/tools/testing/selftests/sigaltstack/current_stack_pointer.h
> @@ -0,0 +1,23 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +
> +#if __alpha__
> +register unsigned long sp asm("$30");
> +#elif __arm__ || __aarch64__ || __csky__ || __m68k__ || __mips__ || __riscv
> +register unsigned long sp asm("sp");
> +#elif __i386__
> +register unsigned long sp asm("esp");
> +#elif __loongarch64
> +register unsigned long sp asm("$sp");
> +#elif __ppc__
> +register unsigned long sp asm("r1");
> +#elif __s390x__
> +register unsigned long sp asm("%15");
> +#elif __sh__
> +register unsigned long sp asm("r15");
> +#elif __x86_64__
> +register unsigned long sp asm("rsp");
> +#elif __XTENSA__
> +register unsigned long sp asm("a1");
> +#else
> +#error "implement current_stack_pointer equivalent"
> +#endif
> diff --git a/tools/testing/selftests/sigaltstack/sas.c b/tools/testing/selftests/sigaltstack/sas.c
> index c53b070755b6..98d37cb744fb 100644
> --- a/tools/testing/selftests/sigaltstack/sas.c
> +++ b/tools/testing/selftests/sigaltstack/sas.c
> @@ -20,6 +20,7 @@
> #include <sys/auxv.h>
>
> #include "../kselftest.h"
> +#include "current_stack_pointer.h"
>
> #ifndef SS_AUTODISARM
> #define SS_AUTODISARM (1U << 31)
> @@ -46,12 +47,6 @@ void my_usr1(int sig, siginfo_t *si, void *u)
> stack_t stk;
> struct stk_data *p;
>
> -#if __s390x__
> - register unsigned long sp asm("%15");
> -#else
> - register unsigned long sp asm("sp");
> -#endif
> -
> if (sp < (unsigned long)sstack ||
> sp >= (unsigned long)sstack + stack_size) {
> ksft_exit_fail_msg("SP is not on sigaltstack\n");
> --
> 2.40.0.rc0.216.gc4246ad0f0-goog

log link:
-----
[1] https://qa-reports.linaro.org/~anders.roxell/linux-mainline-patches/build/lore_kernel_org_linux-kselftest_20230308195933_806917-1-ndesaulniers_google_com/?failures_only=false#!?filter-tests=kselftest-sigaltstack&details=#test-results
[2] https://qa-reports.linaro.org/~anders.roxell/linux-mainline-patches/build/lore_kernel_org_linux-kselftest_20230308195933_806917-1-ndesaulniers_google_com/testrun/15468761/suite/kselftest-sigaltstack/test/sigaltstack_sas/log
[3] https://tuxapi.tuxsuite.com/v1/groups/linaro/projects/anders/tests/2MrJ2e4bDCC4iZjIrnRqmnE7KfC


--
Linaro LKFT
https://lkft.linaro.org

2023-03-20 18:49:25

by Nick Desaulniers

[permalink] [raw]
Subject: Re: [PATCH] selftests: sigaltstack: fix -Wuninitialized

Hi Shuah,
Did this get picked up? Just checking it doesn't fall through the cracks.
EOM

On Mon, Mar 13, 2023 at 2:50 AM Naresh Kamboju
<[email protected]> wrote:
>
> On Thu, 9 Mar 2023 at 01:29, Nick Desaulniers <[email protected]> wrote:
> >
> > Building sigaltstack with clang via:
> > $ ARCH=x86 make LLVM=1 -C tools/testing/selftests/sigaltstack/
> >
> > produces the following warning:
> > warning: variable 'sp' is uninitialized when used here [-Wuninitialized]
> > if (sp < (unsigned long)sstack ||
> > ^~
> >
> > Clang expects these to be declared at global scope; we've fixed this in
> > the kernel proper by using the macro `current_stack_pointer`. This is
> > defined in different headers for different target architectures, so just
> > create a new header that defines the arch-specific register names for
> > the stack pointer register, and define it for more targets (at least the
> > ones that support current_stack_pointer/ARCH_HAS_CURRENT_STACK_POINTER).
> >
> > Reported-by: Linux Kernel Functional Testing <[email protected]>
> > Link: https://lore.kernel.org/lkml/CA+G9fYsi3OOu7yCsMutpzKDnBMAzJBCPimBp86LhGBa0eCnEpA@mail.gmail.com/
> > Signed-off-by: Nick Desaulniers <[email protected]>
>
> Build and boot tested with clang-16 and tested sigaltstack
> on arm64, armv7, FVP, x86_64 and i386 [1] & [2].
> These tests were performed at Linaro test farm by Anders
> with the help of tuxplan / tuxmake and tuxrun.
>
> Tested-by: Linux Kernel Functional Testing <[email protected]>
> Tested-by: Anders Roxell <[email protected]>
>
>
> Build log:
> -------
> clang --target=aarch64-linux-gnu -fintegrated-as
> -Werror=unknown-warning-option -Werror=ignored-optimization-argument
> -Werror=option-ignored -Werror=unused-command-line-argument
> --target=aarch64-linux-gnu -fintegrated-as -Wall sas.c -o
> /home/tuxbuild/.cache/tuxmake/builds/1/build/kselftest/sigaltstack/sas
>
> Test log:
> ----------
>
> Linux version 6.3.0-rc1-next-20230310 (tuxmake@tuxmake) (Debian clang
> version 16.0.0 (++20230228093516+60692a66ced6-1~exp1~20230228093525.41),
> Debian LLD 16.0.0) #1 SMP PREEMPT @1678519789
> ...
>
> [ 56.327569] kselftest: Running tests in sigaltstack
> TAP version 13
> 1..1
> # selftests: sigaltstack: sas
> # # [NOTE] the stack size is 26400
> # TAP version 13
> # 1..3
> # ok 1 Initial sigaltstack state was SS_DISABLE
> # # [RUN] signal USR1
> # ok 2 sigaltstack is disabled in sighandler
> # # [RUN] switched to user ctx
> # # [RUN] signal USR2
> # # [OK] Stack preserved
> # ok 3 sigaltstack is still SS_AUTODISARM after signal
> # # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0
> ok 1 selftests: sigaltstack: sas
>
> Details of test log links provided [3].
>
> > ---
> > Cc: Naresh Kamboju <[email protected]>
> > Cc: KERNEL SELFTEST FRAMEWORK <[email protected]>
> > Cc: [email protected]
> > Cc: [email protected]
> > Cc: [email protected]
> > Cc: Shuah Khan <[email protected]>
> > Cc: Thomas Gleixner <[email protected]>
> > Cc: "Chang S. Bae" <[email protected]>
> > Cc: Len Brown <[email protected]>
> > Cc: Borislav Petkov <[email protected]>
> > Cc: Stas Sergeev <[email protected]>
> > Cc: Arnd Bergmann <[email protected]>
> > Cc: Anders Roxell <[email protected]>
> > Cc: Andy Lutomirski <[email protected]>
> > Cc: Kees Cook <[email protected]>
> > Cc: Nathan Chancellor <[email protected]>
> > Cc: [email protected]
> > ---
> > .../sigaltstack/current_stack_pointer.h | 23 +++++++++++++++++++
> > tools/testing/selftests/sigaltstack/sas.c | 7 +-----
> > 2 files changed, 24 insertions(+), 6 deletions(-)
> > create mode 100644 tools/testing/selftests/sigaltstack/current_stack_pointer.h
> >
> > diff --git a/tools/testing/selftests/sigaltstack/current_stack_pointer.h b/tools/testing/selftests/sigaltstack/current_stack_pointer.h
> > new file mode 100644
> > index 000000000000..ea9bdf3a90b1
> > --- /dev/null
> > +++ b/tools/testing/selftests/sigaltstack/current_stack_pointer.h
> > @@ -0,0 +1,23 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +
> > +#if __alpha__
> > +register unsigned long sp asm("$30");
> > +#elif __arm__ || __aarch64__ || __csky__ || __m68k__ || __mips__ || __riscv
> > +register unsigned long sp asm("sp");
> > +#elif __i386__
> > +register unsigned long sp asm("esp");
> > +#elif __loongarch64
> > +register unsigned long sp asm("$sp");
> > +#elif __ppc__
> > +register unsigned long sp asm("r1");
> > +#elif __s390x__
> > +register unsigned long sp asm("%15");
> > +#elif __sh__
> > +register unsigned long sp asm("r15");
> > +#elif __x86_64__
> > +register unsigned long sp asm("rsp");
> > +#elif __XTENSA__
> > +register unsigned long sp asm("a1");
> > +#else
> > +#error "implement current_stack_pointer equivalent"
> > +#endif
> > diff --git a/tools/testing/selftests/sigaltstack/sas.c b/tools/testing/selftests/sigaltstack/sas.c
> > index c53b070755b6..98d37cb744fb 100644
> > --- a/tools/testing/selftests/sigaltstack/sas.c
> > +++ b/tools/testing/selftests/sigaltstack/sas.c
> > @@ -20,6 +20,7 @@
> > #include <sys/auxv.h>
> >
> > #include "../kselftest.h"
> > +#include "current_stack_pointer.h"
> >
> > #ifndef SS_AUTODISARM
> > #define SS_AUTODISARM (1U << 31)
> > @@ -46,12 +47,6 @@ void my_usr1(int sig, siginfo_t *si, void *u)
> > stack_t stk;
> > struct stk_data *p;
> >
> > -#if __s390x__
> > - register unsigned long sp asm("%15");
> > -#else
> > - register unsigned long sp asm("sp");
> > -#endif
> > -
> > if (sp < (unsigned long)sstack ||
> > sp >= (unsigned long)sstack + stack_size) {
> > ksft_exit_fail_msg("SP is not on sigaltstack\n");
> > --
> > 2.40.0.rc0.216.gc4246ad0f0-goog
>
> log link:
> -----
> [1] https://qa-reports.linaro.org/~anders.roxell/linux-mainline-patches/build/lore_kernel_org_linux-kselftest_20230308195933_806917-1-ndesaulniers_google_com/?failures_only=false#!?filter-tests=kselftest-sigaltstack&details=#test-results
> [2] https://qa-reports.linaro.org/~anders.roxell/linux-mainline-patches/build/lore_kernel_org_linux-kselftest_20230308195933_806917-1-ndesaulniers_google_com/testrun/15468761/suite/kselftest-sigaltstack/test/sigaltstack_sas/log
> [3] https://tuxapi.tuxsuite.com/v1/groups/linaro/projects/anders/tests/2MrJ2e4bDCC4iZjIrnRqmnE7KfC
>
>
> --
> Linaro LKFT
> https://lkft.linaro.org



--
Thanks,
~Nick Desaulniers

2023-03-20 23:31:16

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH] selftests: sigaltstack: fix -Wuninitialized

On 3/20/23 12:41, Nick Desaulniers wrote:
> Hi Shuah,
> Did this get picked up? Just checking it doesn't fall through the cracks.
> EOM
>

Thanks for the ping. Now applied Applied to linux-kselftest fixes
branch for rc4.

Somehow this one didn't show up in my inbox and responses made it
fine. I have to check what's happening to emails on my end.

thanks,
-- Shuah