nolibc updates for v6.5]
Subsystems (3)
cover nolibc updates for v6.5]
Hello!
This series contains nolibc updates for v6.5:
1. tools/nolibc: tests: use volatile to force stack smashing,
courtesy of Thomas Weißschuh.
2. tools/nolibc: tests: fix build on non-c99 compliant compilers,
courtesy of Willy Tarreau.
3. tools/nolibc: fix build of the test case using glibc, courtesy
of Willy Tarreau.
4. tools/nolibc: add libc-test binary, courtesy of
Thomas Weißschuh.
5. tools/nolibc: add wrapper for memfd_create, courtesy of
Thomas Weißschuh.
6. tools/nolibc: implement fd-based FILE streams, courtesy of
Thomas Weißschuh.
7. tools/nolibc: add testcases for vfprintf, courtesy of
Thomas Weißschuh.
8. tools/nolibc: Fix build of stdio.h due to header ordering,
courtesy of Mark Brown.
9. tools/nolibc: use standard __asm__ statements, courtesy of
Thomas Weißschuh.
10. tools/nolibc: use __inline__ syntax, courtesy of
Thomas Weißschuh.
11. tools/nolibc: use C89 comment syntax, courtesy of
Thomas Weißschuh.
12. tools/nolibc: validate C89 compatibility, courtesy of
Thomas Weißschuh.
13. tools/nolibc: s390: provide custom implementation for sys_fork,
courtesy of Thomas Weißschuh.
14. tools/nolibc: add testcase for fork()/waitpid(), courtesy of
Thomas Weißschuh.
15. tools/nolibc: remove LINUX_REBOOT_ constants, courtesy of
Thomas Weißschuh.
Thanx, Paul
------------------------------------------------------------------------
b/tools/include/nolibc/arch-aarch64.h | 32 +++--- b/tools/include/nolibc/arch-arm.h | 44 ++++---- b/tools/include/nolibc/arch-i386.h | 40 ++++---- b/tools/include/nolibc/arch-loongarch.h | 38 +++---- b/tools/include/nolibc/arch-mips.h | 56 +++++------ b/tools/include/nolibc/arch-riscv.h | 40 ++++---- b/tools/include/nolibc/arch-s390.h | 2 b/tools/include/nolibc/arch-x86_64.h | 34 +++--- b/tools/include/nolibc/nolibc.h | 2 b/tools/include/nolibc/stackprotector.h | 4 b/tools/include/nolibc/stdio.h | 95 +++++++++++++------ b/tools/include/nolibc/stdlib.h | 18 +-- b/tools/include/nolibc/string.h | 4 b/tools/include/nolibc/sys.h | 23 ++++ b/tools/include/nolibc/types.h | 8 - b/tools/testing/selftests/nolibc/.gitignore | 1 b/tools/testing/selftests/nolibc/Makefile | 6 + b/tools/testing/selftests/nolibc/nolibc-test.c | 8 - tools/include/nolibc/arch-s390.h | 9 + tools/include/nolibc/sys.h | 11 +- tools/testing/selftests/nolibc/Makefile | 2 tools/testing/selftests/nolibc/nolibc-test.c | 125 +++++++++++++++++++++++-- 22 files changed, 391 insertions(+), 211 deletions(-)
PATCH v2 1/15 tools/nolibc: tests: use volatile to force stack smashing
From: Thomas Weißschuh <[email protected]>
Use a volatile pointer to write outside the buffer so the compiler can't optimize it away.
--- tools/testing/selftests/nolibc/nolibc-test.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +2 -6
@@ -667,17 +667,13 @@ int run_stdlib(int min, int max)return ret; } -#if defined(__clang__) -__attribute__((optnone)) -#elif defined(__GNUC__) -__attribute__((optimize("O0"))) -#endif static int smash_stack (void ) {char buf[100 ]; +volatile char *ptr = buf;for (size_t i =0 ; i <200 ; i++) - buf[i] ='P' ; + ptr[i] ='P' ;return 1 ; }
Signature · 1 line
2.40.1
PATCH v2 2/15 tools/nolibc: tests: fix build on non-c99 compliant compilers
In reply to Paul E. McKenneyFrom: Willy Tarreau <[email protected]>
Commit 9735716830f2 ("tools/nolibc: tests: add test for -fstack-protector") brought a declaration inside the initialization statement of a for loop, which breaks the build on compilers that do not default to c99 compatibility, making it more difficult to validate that the lib still builds on such compilers. The fix is trivial, so let's move the declaration to the variables block of the function instead. No backport is needed.
--- tools/testing/selftests/nolibc/nolibc-test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +2 -1
@@ -671,8 +671,9 @@ static int smash_stack(void) {char buf[100 ];volatile char *ptr = buf; +size_t i; -for (size_t i =0 ; i <200 ; i++) +for (i =0 ; i <200 ; i++) ptr[i] ='P' ;return 1 ;
Signature · 1 line
2.40.1
PATCH v2 3/15 tools/nolibc: fix build of the test case using glibc
In reply to Paul E. McKenneyFrom: Willy Tarreau <[email protected]>
Some extra tests for various integer types and limits were added by commit d1209597ff00 ("tools/nolibc: add tests for the integer limits in stdint.h"), but we forgot to retest with glibc. Stddef and stdint are now needed for the program to build there.
--- tools/testing/selftests/nolibc/nolibc-test.c | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
@@ -34,6 +34,8 @@#include <sched.h> #include <signal.h> #include <stdarg.h> +#include <stddef.h> +#include <stdint.h> #include <unistd.h> #endif #endif
Signature · 1 line
2.40.1
PATCH v2 4/15 tools/nolibc: add libc-test binary
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
This can be used to easily compare the behavior of nolibc to the system libc.
--- tools/testing/selftests/nolibc/.gitignore | 1 + tools/testing/selftests/nolibc/Makefile | 6 ++++++ 2 files changed, 7 insertions(+)
tools/testing/selftests/nolibc/.gitignore +1 -0
@@ -1,4 +1,5 @@ /initramfs/ +/libc-test /nolibc-test /run.out /sysroot/
tools/testing/selftests/nolibc/Makefile +6 -0
@@ -94,6 +94,7 @@ help: @echo " help this help" @echo " sysroot create the nolibc sysroot here (uses \$$ARCH)" @echo " nolibc-test build the executable (uses \$$CC and \$$CROSS_COMPILE)" + @echo " libc-test build an executable using the compiler's default libc instead" @echo " run-user runs the executable under QEMU (uses \$$ARCH, \$$TEST)" @echo " initramfs prepare the initramfs with nolibc-test" @echo " defconfig create a fresh new default config (uses \$$ARCH)"
@@ -128,6 +129,9 @@ nolibc-test: nolibc-test.c sysroot/$(ARCH)/include $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ -nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc +libc-test: nolibc-test.c + $(QUIET_CC)$(CC) -o $@ $< + # qemu user-land test run-user: nolibc-test $(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || :
@@ -159,6 +163,8 @@ clean: $(Q)rm -rf sysroot $(call QUIET_CLEAN, nolibc-test) $(Q)rm -f nolibc-test + $(call QUIET_CLEAN, libc-test) + $(Q)rm -f libc-test $(call QUIET_CLEAN, initramfs) $(Q)rm -rf initramfs $(call QUIET_CLEAN, run.out)
Signature · 1 line
2.40.1
PATCH v2 5/15 tools/nolibc: add wrapper for memfd_create
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
This is useful for users and will also be used by a future testcase.
--- tools/include/nolibc/sys.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
tools/include/nolibc/sys.h +23 -0
@@ -1365,6 +1365,29 @@ ssize_t write(int fd, const void *buf, size_t count)return ret; } + +/* +* int memfd_create(const char *name, unsigned int flags); +*/ + +static __attribute__((unused)) +int sys_memfd_create (const char *name,unsigned int flags) +{ +return my_syscall2 (__NR_memfd_create, name, flags); +} + +static __attribute__((unused)) +int memfd_create (const char *name,unsigned int flags) +{ +ssize_t ret =sys_memfd_create (name, flags); + +if (ret <0 ) { +SET_ERRNO (-ret); + ret =-1 ; + } +return ret; +} +/* make sure to include all global symbols */ #include "nolibc.h"
Signature · 1 line
2.40.1
PATCH v2 6/15 tools/nolibc: implement fd-based FILE streams
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
This enables the usage of the stream APIs with arbitrary filedescriptors.
It will be used by a future testcase.
--- tools/include/nolibc/stdio.h | 95 ++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 27 deletions(-)
tools/include/nolibc/stdio.h +68 -27
@@ -21,17 +21,75 @@#define EOF (-1)#endif -/* just define FILE as a non-empty type */ +/* just define FILE as a non-empty type. The value of the pointer gives +* the FD: FILE=~fd for fd>=0 or NULL for fd<0. This way positive FILE +* are immediately identified as abnormal entries (i.e. possible copies +* of valid pointers to something else). +*/ typedef struct FILE {char dummy[1 ]; }FILE ; -/* We define the 3 common stdio files as constant invalid pointers that -* are easily recognized. -*/ -static __attribute__((unused))FILE *const stdin = (FILE *)-3 ; -static __attribute__((unused))FILE *const stdout = (FILE *)-2 ; -static __attribute__((unused))FILE *const stderr = (FILE *)-1 ; +static __attribute__((unused))FILE *const stdin = (FILE *)(intptr_t )~STDIN_FILENO ; +static __attribute__((unused))FILE *const stdout = (FILE *)(intptr_t )~STDOUT_FILENO ; +static __attribute__((unused))FILE *const stderr = (FILE *)(intptr_t )~STDERR_FILENO ; + +/* provides a FILE* equivalent of fd. The mode is ignored. */ +static __attribute__((unused)) +FILE *fdopen (int fd,const char *mode __attribute__((unused))) +{ +if (fd <0 ) { +SET_ERRNO (EBADF ); +return NULL ; + } +return (FILE *)(intptr_t )~fd; +} + +/* provides the fd of stream. */ +static __attribute__((unused)) +int fileno (FILE *stream) +{ +intptr_t i = (intptr_t )stream; + +if (i >=0 ) { +SET_ERRNO (EBADF ); +return -1 ; + } +return ~i; +} + +/* flush a stream. */ +static __attribute__((unused)) +int fflush (FILE *stream) +{ +intptr_t i = (intptr_t )stream; + +/* NULL is valid here. */ +if (i >0 ) { +SET_ERRNO (EBADF ); +return -1 ; + } + +/* Don't do anything, nolibc does not support buffering. */ +return 0 ; +} + +/* flush a stream. */ +static __attribute__((unused)) +int fclose (FILE *stream) +{ +intptr_t i = (intptr_t )stream; + +if (i >=0 ) { +SET_ERRNO (EBADF ); +return -1 ; + } + +if (close (~i)) +return EOF ; + +return 0 ; +}/* getc(), fgetc(), getchar() */
@@ -41,14 +99,8 @@ static __attribute__((unused))int fgetc (FILE * stream) {unsigned char ch; -int fd; -if (stream < stdin || stream > stderr) -return EOF ; - - fd =3 + (long )stream; - -if (read (fd, &ch,1 ) <=0 ) +if (read (fileno (stream), &ch,1 ) <=0 )return EOF ;return ch; }
@@ -68,14 +120,8 @@ static __attribute__((unused))int fputc (int c,FILE * stream) {unsigned char ch = c; -int fd; - -if (stream < stdin || stream > stderr) -return EOF ; - - fd =3 + (long )stream; -if (write (fd, &ch,1 ) <=0 ) +if (write (fileno (stream), &ch,1 ) <=0 )return EOF ;return ch; }
@@ -96,12 +142,7 @@ static __attribute__((unused))int _fwrite (const void *buf,size_t size,FILE *stream) {ssize_t ret; -int fd; - -if (stream < stdin || stream > stderr) -return EOF ; - - fd =3 + (long )stream; +int fd =fileno (stream);while (size) { ret =write (fd, buf, size);
Signature · 1 line
2.40.1
PATCH v2 7/15 tools/nolibc: add testcases for vfprintf
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
vfprintf() is complex and so far did not have proper tests.
--- tools/testing/selftests/nolibc/nolibc-test.c | 86 ++++++++++++++++++++ 1 file changed, 86 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +86 -0
@@ -20,6 +20,7 @@#include <linux/reboot.h> #include <sys/io.h> #include <sys/ioctl.h> +#include <sys/mman.h> #include <sys/mount.h> #include <sys/reboot.h> #include <sys/stat.h>
@@ -669,6 +670,90 @@ int run_stdlib(int min, int max)return ret; } +#define EXPECT_VFPRINTF (c, expected, fmt, ...) \ + ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) + +static int expect_vfprintf (int llen,size_t c,const char *expected,const char *fmt, ...) +{ +int ret, fd, w, r; +char buf[100 ]; +FILE *memfile; +va_list args; + + fd =memfd_create ("vfprintf" ,0 ); +if (fd ==-1 ) { +pad_spc (llen,64 ,"[FAIL]\n" ); +return 1 ; + } + + memfile =fdopen (fd,"w+" ); +if (!memfile) { +pad_spc (llen,64 ,"[FAIL]\n" ); +return 1 ; + } + +va_start (args, fmt); + w =vfprintf (memfile, fmt, args); +va_end (args); + +if (w != c) { + llen +=printf (" written(%d) != %d" , w, (int ) c); +pad_spc (llen,64 ,"[FAIL]\n" ); +return 1 ; + } + +fflush (memfile); +lseek (fd,0 ,SEEK_SET ); + + r =read (fd, buf,sizeof (buf) -1 ); + buf[r] ='\0' ; + +fclose (memfile); + +if (r != w) { + llen +=printf (" written(%d) != read(%d)" , w, r); +pad_spc (llen,64 ,"[FAIL]\n" ); +return 1 ; + } + + llen +=printf (" \"%s\" = \"%s\"" , expected, buf); + ret =strncmp (expected, buf, c); + +pad_spc (llen,64 , ret ?"[FAIL]\n" :" [OK]\n" ); +return ret; +} + +static int run_vfprintf (int min,int max) +{ +int test; +int tmp; +int ret =0 ; +void *p1, *p2; + +for (test = min; test >=0 && test <= max; test++) { +int llen =0 ;// line length + +/* avoid leaving empty lines below, this will insert holes into +* test numbers. +*/ +switch (test + __LINE__ +1 ) { +CASE_TEST (empty);EXPECT_VFPRINTF (0 ,"" ,"" );break ; +CASE_TEST (simple);EXPECT_VFPRINTF (3 ,"foo" ,"foo" );break ; +CASE_TEST (string);EXPECT_VFPRINTF (3 ,"foo" ,"%s" ,"foo" );break ; +CASE_TEST (number);EXPECT_VFPRINTF (4 ,"1234" ,"%d" ,1234 );break ; +CASE_TEST (negnumber);EXPECT_VFPRINTF (5 ,"-1234" ,"%d" ,-1234 );break ; +CASE_TEST (unsigned);EXPECT_VFPRINTF (5 ,"12345" ,"%u" ,12345 );break ; +CASE_TEST (char);EXPECT_VFPRINTF (1 ,"c" ,"%c" ,'c' );break ; +CASE_TEST (hex);EXPECT_VFPRINTF (1 ,"f" ,"%x" ,0xf );break ; +CASE_TEST (pointer);EXPECT_VFPRINTF (3 ,"0x1" ,"%p" , (void *)0x1 );break ; +case __LINE__: +return ret;/* must be last */ +/* note: do not set any defaults so as to permit holes above */ + } + } +return ret; +} +static int smash_stack (void ) {char buf[100 ];
@@ -777,6 +862,7 @@ static const struct test test_names[] = {/* add new tests here */ { .name = "syscall", .func = run_syscall }, { .name = "stdlib", .func = run_stdlib }, + { .name = "vfprintf", .func = run_vfprintf }, { .name = "protection", .func = run_protection }, { 0 } };
Signature · 1 line
2.40.1
PATCH v2 8/15 tools/nolibc: Fix build of stdio.h due to header ordering
In reply to Paul E. McKenneyFrom: Mark Brown <[email protected]>
When we added fd based file streams we created references to STx_FILENO in stdio.h but these constants are declared in unistd.h which is the last file included by the top level nolibc.h meaning those constants are not defined when we try to build stdio.h. This causes programs using nolibc.h to fail to build.
Reorder the headers to avoid this issue.
--- tools/include/nolibc/nolibc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/nolibc.h +1 -1
@@ -99,11 +99,11 @@#include "sys.h" #include "ctype.h" #include "signal.h" +#include "unistd.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "time.h" -#include "unistd.h" #include "stackprotector.h" /* Used by programs to avoid std includes */
Signature · 1 line
2.40.1
PATCH v2 9/15 tools/nolibc: use standard __asm__ statements
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
Most of the code was migrated to C99-conformant __asm__ statements before. It seems string.h was missed.
--- tools/include/nolibc/string.h | 4 ++-- tools/testing/selftests/nolibc/nolibc-test.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
tools/include/nolibc/string.h +2 -2
@@ -90,7 +90,7 @@ void *memset(void *dst, int b, size_t len)while (len--) {/* prevent gcc from recognizing memset() here */ - asmvolatile ("" ); + __asm__volatile ("" ); *(p++) = b; }return dst;
@@ -139,7 +139,7 @@ size_t strlen(const char *str)size_t len;for (len =0 ; str[len]; len++) - asm("" ); + __asm__("" );return len; }
tools/testing/selftests/nolibc/nolibc-test.c +1 -1
@@ -984,7 +984,7 @@ int main(int argc, char **argv, char **envp)#else else if (ioperm(0x501, 1, 1) == 0) #endif - asm volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0)); + __asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));/* if it does nothing, fall back to the regular panic */ #endif }
Signature · 1 line
2.40.1
PATCH v2 10/15 tools/nolibc: use __inline__ syntax
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
When building in strict C89 mode the "inline" keyword is unknown. While "__inline__" is non-standard it is used by the kernel headers themselves.
So the used compilers would have to support it or the users shim it with a #define.
--- tools/include/nolibc/stdlib.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
tools/include/nolibc/stdlib.h +9 -9
@@ -102,7 +102,7 @@ char *_getenv(const char *name, char **environ)return NULL ; } -static inline __attribute__((unused,always_inline)) +static __inline__ __attribute__((unused,always_inline))char *getenv (const char *name) {extern char **environ;
@@ -231,7 +231,7 @@ int utoh_r(unsigned long in, char *buffer)/* converts unsigned long <in> to an hex string using the static itoa_buffer * and returns the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *utoh (unsigned long in) {utoh_r (in, itoa_buffer);
@@ -293,7 +293,7 @@ int itoa_r(long in, char *buffer)/* for historical compatibility, same as above but returns the pointer to the * buffer. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *ltoa_r (long in,char *buffer) {itoa_r (in, buffer);
@@ -303,7 +303,7 @@ char *ltoa_r(long in, char *buffer)/* converts long integer <in> to a string using the static itoa_buffer and * returns the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *itoa (long in) {itoa_r (in, itoa_buffer);
@@ -313,7 +313,7 @@ char *itoa(long in)/* converts long integer <in> to a string using the static itoa_buffer and * returns the pointer to that string. Same as above, for compatibility. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *ltoa (long in) {itoa_r (in, itoa_buffer);
@@ -323,7 +323,7 @@ char *ltoa(long in)/* converts unsigned long integer <in> to a string using the static itoa_buffer * and returns the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *utoa (unsigned long in) {utoa_r (in, itoa_buffer);
@@ -367,7 +367,7 @@ int u64toh_r(uint64_t in, char *buffer)/* converts uint64_t <in> to an hex string using the static itoa_buffer and * returns the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *u64toh (uint64_t in) {u64toh_r (in, itoa_buffer);
@@ -429,7 +429,7 @@ int i64toa_r(int64_t in, char *buffer)/* converts int64_t <in> to a string using the static itoa_buffer and returns * the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *i64toa (int64_t in) {i64toa_r (in, itoa_buffer);
@@ -439,7 +439,7 @@ char *i64toa(int64_t in)/* converts uint64_t <in> to a string using the static itoa_buffer and returns * the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *u64toa (uint64_t in) {u64toa_r (in, itoa_buffer);
Signature · 1 line
2.40.1
PATCH v2 11/15 tools/nolibc: use C89 comment syntax
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
Most of nolibc is already using C89 comments.
--- tools/include/nolibc/arch-aarch64.h | 32 +++++------ tools/include/nolibc/arch-arm.h | 44 +++++++-------- tools/include/nolibc/arch-i386.h | 40 +++++++------- tools/include/nolibc/arch-loongarch.h | 38 ++++++------- tools/include/nolibc/arch-mips.h | 56 ++++++++++---------- tools/include/nolibc/arch-riscv.h | 40 +++++++------- tools/include/nolibc/arch-s390.h | 2 +- tools/include/nolibc/arch-x86_64.h | 34 ++++++------ tools/include/nolibc/stackprotector.h | 4 +- tools/include/nolibc/sys.h | 8 +-- tools/testing/selftests/nolibc/nolibc-test.c | 12 ++--- 11 files changed, 155 insertions(+), 155 deletions(-)
tools/include/nolibc/arch-aarch64.h +16 -16
@@ -176,24 +176,24 @@ const unsigned long *_auxv __attribute__((weak));void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) { __asm__volatile ( -"ldr x0, [sp]\n" // argc (x0) was in the stack -"add x1, sp, 8\n" // argv (x1) = sp -"lsl x2, x0, 3\n" // envp (x2) = 8*argc ... -"add x2, x2, 8\n" // + 8 (skip null) -"add x2, x2, x1\n" // + argv -"adrp x3, environ\n" // x3 = &environ (high bits) -"str x2, [x3, #:lo12:environ]\n" // store envp into environ -"mov x4, x2\n" // search for auxv (follows NULL after last env) +"ldr x0, [sp]\n" /* argc (x0) was in the stack */ +"add x1, sp, 8\n" /* argv (x1) = sp */ +"lsl x2, x0, 3\n" /* envp (x2) = 8*argc ... */ +"add x2, x2, 8\n" /* + 8 (skip null) */ +"add x2, x2, x1\n" /* + argv */ +"adrp x3, environ\n" /* x3 = &environ (high bits) */ +"str x2, [x3, #:lo12:environ]\n" /* store envp into environ */ +"mov x4, x2\n" /* search for auxv (follows NULL after last env) */ "0:\n" -"ldr x5, [x4], 8\n" // x5 = *x4; x4 += 8 -"cbnz x5, 0b\n" // and stop at NULL after last env -"adrp x3, _auxv\n" // x3 = &_auxv (high bits) -"str x4, [x3, #:lo12:_auxv]\n" // store x4 into _auxv -"and sp, x1, -16\n" // sp must be 16-byte aligned in the callee -"bl main\n" // main() returns the status code, we'll exit with it. -"mov x8, 93\n" // NR_exit == 93 +"ldr x5, [x4], 8\n" /* x5 = *x4; x4 += 8 */ +"cbnz x5, 0b\n" /* and stop at NULL after last env */ +"adrp x3, _auxv\n" /* x3 = &_auxv (high bits) */ +"str x4, [x3, #:lo12:_auxv]\n" /* store x4 into _auxv */ +"and sp, x1, -16\n" /* sp must be 16-byte aligned in the callee */ +"bl main\n" /* main() returns the status code, we'll exit with it. */ +"mov x8, 93\n" /* NR_exit == 93 */ "svc #0\n" );__builtin_unreachable (); } -#endif // _NOLIBC_ARCH_AARCH64_H +#endif /* _NOLIBC_ARCH_AARCH64_H */
tools/include/nolibc/arch-arm.h +22 -22
60 lines not on this page · raw message
tools/include/nolibc/arch-i386.h +20 -20
@@ -194,31 +194,31 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protec { __asm__volatile ( #ifdef NOLIBC_STACKPROTECTOR - "call __stack_chk_init\n" // initialize stack protector + "call __stack_chk_init\n" /* initialize stack protector */ #endif -"pop %eax\n" // argc (first arg, %eax) -"mov %esp, %ebx\n" // argv[] (second arg, %ebx) -"lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) -"mov %ecx, environ\n" // save environ -"xor %ebp, %ebp\n" // zero the stack frame -"mov %ecx, %edx\n" // search for auxv (follows NULL after last env) +"pop %eax\n" /* argc (first arg, %eax) */ +"mov %esp, %ebx\n" /* argv[] (second arg, %ebx) */ +"lea 4(%ebx,%eax,4),%ecx\n" /* then a NULL then envp (third arg, %ecx) */ +"mov %ecx, environ\n" /* save environ */ +"xor %ebp, %ebp\n" /* zero the stack frame */ +"mov %ecx, %edx\n" /* search for auxv (follows NULL after last env) */ "0:\n" -"add $4, %edx\n" // search for auxv using edx, it follows the -"cmp -4(%edx), %ebp\n" // ... NULL after last env (ebp is zero here) +"add $4, %edx\n" /* search for auxv using edx, it follows the */ +"cmp -4(%edx), %ebp\n" /* ... NULL after last env (ebp is zero here) */ "jnz 0b\n" -"mov %edx, _auxv\n" // save it into _auxv -"and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned before -"sub $4, %esp\n" // the call instruction (args are aligned) -"push %ecx\n" // push all registers on the stack so that we -"push %ebx\n" // support both regparm and plain stack modes +"mov %edx, _auxv\n" /* save it into _auxv */ +"and $-16, %esp\n" /* x86 ABI : esp must be 16-byte aligned before */ +"sub $4, %esp\n" /* the call instruction (args are aligned) */ +"push %ecx\n" /* push all registers on the stack so that we */ +"push %ebx\n" /* support both regparm and plain stack modes */ "push %eax\n" -"call main\n" // main() returns the status code in %eax -"mov %eax, %ebx\n" // retrieve exit code (32-bit int) -"movl $1, %eax\n" // NR_exit == 1 -"int $0x80\n" // exit now -"hlt\n" // ensure it does not +"call main\n" /* main() returns the status code in %eax */ +"mov %eax, %ebx\n" /* retrieve exit code (32-bit int) */ +"movl $1, %eax\n" /* NR_exit == 1 */ +"int $0x80\n" /* exit now */ +"hlt\n" /* ensure it does not */ );__builtin_unreachable (); } -#endif // _NOLIBC_ARCH_I386_H +#endif /* _NOLIBC_ARCH_I386_H */
tools/include/nolibc/arch-loongarch.h +19 -19
@@ -158,7 +158,7 @@ const unsigned long *_auxv __attribute__((weak));#define LONG_ADDI "addi.w"#define LONG_SLL "slli.w"#define LONG_BSTRINS "bstrins.w" -#else // __loongarch_grlen == 64 +#else /* __loongarch_grlen == 64 */ #define LONGLOG "3"#define SZREG "8"#define REG_L "ld.d"
@@ -173,28 +173,28 @@ const unsigned long *_auxv __attribute__((weak));void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) { __asm__volatile ( -REG_L " $a0, $sp, 0\n" // argc (a0) was in the stack -LONG_ADDI " $a1, $sp, " SZREG "\n" // argv (a1) = sp + SZREG -LONG_SLL " $a2, $a0, " LONGLOG "\n" // envp (a2) = SZREG*argc ... -LONG_ADDI " $a2, $a2, " SZREG "\n" // + SZREG (skip null) -LONG_ADD " $a2, $a2, $a1\n" // + argv +REG_L " $a0, $sp, 0\n" /* argc (a0) was in the stack */ +LONG_ADDI " $a1, $sp, " SZREG "\n" /* argv (a1) = sp + SZREG */ +LONG_SLL " $a2, $a0, " LONGLOG "\n" /* envp (a2) = SZREG*argc ... */ +LONG_ADDI " $a2, $a2, " SZREG "\n" /* + SZREG (skip null) */ +LONG_ADD " $a2, $a2, $a1\n" /* + argv */ -"move $a3, $a2\n" // iterate a3 over envp to find auxv (after NULL) -"0:\n" // do { -REG_L " $a4, $a3, 0\n" // a4 = *a3; -LONG_ADDI " $a3, $a3, " SZREG "\n" // a3 += sizeof(void*); -"bne $a4, $zero, 0b\n" // } while (a4); -"la.pcrel $a4, _auxv\n" // a4 = &_auxv -LONG_S " $a3, $a4, 0\n" // store a3 into _auxv +"move $a3, $a2\n" /* iterate a3 over envp to find auxv (after NULL) */ +"0:\n" /* do { */ +REG_L " $a4, $a3, 0\n" /* a4 = *a3; */ +LONG_ADDI " $a3, $a3, " SZREG "\n" /* a3 += sizeof(void*); */ +"bne $a4, $zero, 0b\n" /* } while (a4); */ +"la.pcrel $a4, _auxv\n" /* a4 = &_auxv */ +LONG_S " $a3, $a4, 0\n" /* store a3 into _auxv */ -"la.pcrel $a3, environ\n" // a3 = &environ -LONG_S " $a2, $a3, 0\n" // store envp(a2) into environ -LONG_BSTRINS " $sp, $zero, 3, 0\n" // sp must be 16-byte aligned -"bl main\n" // main() returns the status code, we'll exit with it. -"li.w $a7, 93\n" // NR_exit == 93 +"la.pcrel $a3, environ\n" /* a3 = &environ */ +LONG_S " $a2, $a3, 0\n" /* store envp(a2) into environ */ +LONG_BSTRINS " $sp, $zero, 3, 0\n" /* sp must be 16-byte aligned */ +"bl main\n" /* main() returns the status code, we'll exit with it. */ +"li.w $a7, 93\n" /* NR_exit == 93 */ "syscall 0\n" );__builtin_unreachable (); } -#endif // _NOLIBC_ARCH_LOONGARCH_H +#endif /* _NOLIBC_ARCH_LOONGARCH_H */
tools/include/nolibc/arch-mips.h +28 -28
70 lines not on this page · raw message
tools/include/nolibc/arch-riscv.h +20 -20
@@ -181,28 +181,28 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(void)".option norelax\n" "lla gp, __global_pointer$\n" ".option pop\n" -"lw a0, 0(sp)\n" // argc (a0) was in the stack -"add a1, sp, " SZREG "\n" // argv (a1) = sp -"slli a2, a0, " PTRLOG "\n" // envp (a2) = SZREG*argc ... -"add a2, a2, " SZREG "\n" // + SZREG (skip null) -"add a2,a2,a1\n" // + argv - -"add a3, a2, zero\n" // iterate a3 over envp to find auxv (after NULL) -"0:\n" // do { -"ld a4, 0(a3)\n" // a4 = *a3; -"add a3, a3, " SZREG "\n" // a3 += sizeof(void*); -"bne a4, zero, 0b\n" // } while (a4); -"lui a4, %hi(_auxv)\n" // a4 = &_auxv (high bits) -"sd a3, %lo(_auxv)(a4)\n" // store a3 into _auxv - -"lui a3, %hi(environ)\n" // a3 = &environ (high bits) -"sd a2,%lo(environ)(a3)\n" // store envp(a2) into environ -"andi sp,a1,-16\n" // sp must be 16-byte aligned -"call main\n" // main() returns the status code, we'll exit with it. -"li a7, 93\n" // NR_exit == 93 +"lw a0, 0(sp)\n" /* argc (a0) was in the stack */ +"add a1, sp, " SZREG "\n" /* argv (a1) = sp */ +"slli a2, a0, " PTRLOG "\n" /* envp (a2) = SZREG*argc ... */ +"add a2, a2, " SZREG "\n" /* + SZREG (skip null) */ +"add a2,a2,a1\n" /* + argv */ + +"add a3, a2, zero\n" /* iterate a3 over envp to find auxv (after NULL) */ +"0:\n" /* do { */ +"ld a4, 0(a3)\n" /* a4 = *a3; */ +"add a3, a3, " SZREG "\n" /* a3 += sizeof(void*); */ +"bne a4, zero, 0b\n" /* } while (a4); */ +"lui a4, %hi(_auxv)\n" /* a4 = &_auxv (high bits) */ +"sd a3, %lo(_auxv)(a4)\n" /* store a3 into _auxv */ + +"lui a3, %hi(environ)\n" /* a3 = &environ (high bits) */ +"sd a2,%lo(environ)(a3)\n" /* store envp(a2) into environ */ +"andi sp,a1,-16\n" /* sp must be 16-byte aligned */ +"call main\n" /* main() returns the status code, we'll exit with it. */ +"li a7, 93\n" /* NR_exit == 93 */ "ecall\n" ); __builtin_unreachable(); } -#endif // _NOLIBC_ARCH_RISCV_H +#endif /* _NOLIBC_ARCH_RISCV_H */
tools/include/nolibc/arch-s390.h +1 -1
@@ -223,4 +223,4 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, return (void *)my_syscall1(__NR_mmap, &args); }#define sys_mmap sys_mmap -#endif // _NOLIBC_ARCH_S390_H +#endif /* _NOLIBC_ARCH_S390_H */
tools/include/nolibc/arch-x86_64.h +17 -17
@@ -194,27 +194,27 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(void) { __asm__volatile ( #ifdef NOLIBC_STACKPROTECTOR - "call __stack_chk_init\n" // initialize stack protector + "call __stack_chk_init\n" /* initialize stack protector */ #endif -"pop %rdi\n" // argc (first arg, %rdi) -"mov %rsp, %rsi\n" // argv[] (second arg, %rsi) -"lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) -"mov %rdx, environ\n" // save environ -"xor %ebp, %ebp\n" // zero the stack frame -"mov %rdx, %rax\n" // search for auxv (follows NULL after last env) +"pop %rdi\n" /* argc (first arg, %rdi) */ +"mov %rsp, %rsi\n" /* argv[] (second arg, %rsi) */ +"lea 8(%rsi,%rdi,8),%rdx\n" /* then a NULL then envp (third arg, %rdx) */ +"mov %rdx, environ\n" /* save environ */ +"xor %ebp, %ebp\n" /* zero the stack frame */ +"mov %rdx, %rax\n" /* search for auxv (follows NULL after last env) */ "0:\n" -"add $8, %rax\n" // search for auxv using rax, it follows the -"cmp -8(%rax), %rbp\n" // ... NULL after last env (rbp is zero here) +"add $8, %rax\n" /* search for auxv using rax, it follows the */ +"cmp -8(%rax), %rbp\n" /* ... NULL after last env (rbp is zero here) */ "jnz 0b\n" -"mov %rax, _auxv\n" // save it into _auxv -"and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned before call -"call main\n" // main() returns the status code, we'll exit with it. -"mov %eax, %edi\n" // retrieve exit code (32 bit) -"mov $60, %eax\n" // NR_exit == 60 -"syscall\n" // really exit -"hlt\n" // ensure it does not return +"mov %rax, _auxv\n" /* save it into _auxv */ +"and $-16, %rsp\n" /* x86 ABI : esp must be 16-byte aligned before call */ +"call main\n" /* main() returns the status code, we'll exit with it. */ +"mov %eax, %edi\n" /* retrieve exit code (32 bit) */ +"mov $60, %eax\n" /* NR_exit == 60 */ +"syscall\n" /* really exit */ +"hlt\n" /* ensure it does not return */ );__builtin_unreachable (); } -#endif // _NOLIBC_ARCH_X86_64_H +#endif /* _NOLIBC_ARCH_X86_64_H */
tools/include/nolibc/stackprotector.h +2 -2
@@ -48,6 +48,6 @@ void __stack_chk_init(void)/* a bit more randomness in case getrandom() fails */ __stack_chk_guard ^= (uintptr_t ) &__stack_chk_guard; } -#endif // defined(NOLIBC_STACKPROTECTOR) +#endif /* defined(NOLIBC_STACKPROTECTOR) */ -#endif // _NOLIBC_STACKPROTECTOR_H +#endif /* _NOLIBC_STACKPROTECTOR_H */
tools/include/nolibc/sys.h +4 -4
@@ -12,15 +12,15 @@/* system includes */ #include <asm/unistd.h> -#include <asm/signal.h> // for SIGCHLD +#include <asm/signal.h> /* for SIGCHLD */ #include <asm/ioctls.h> #include <asm/mman.h> #include <linux/fs.h> #include <linux/loop.h> #include <linux/time.h> #include <linux/auxvec.h> -#include <linux/fcntl.h> // for O_* and AT_* -#include <linux/stat.h> // for statx() +#include <linux/fcntl.h> /* for O_* and AT_* */ +#include <linux/stat.h> /* for statx() */ #include "arch.h" #include "errno.h"
@@ -322,7 +322,7 @@ static __attribute__((noreturn,unused))void sys_exit (int status) {my_syscall1 (__NR_exit, status &255 ); -while (1 );// shut the "noreturn" warnings. +while (1 );/* shut the "noreturn" warnings. */ } static __attribute__((noreturn,unused))
tools/testing/selftests/nolibc/nolibc-test.c +6 -6
@@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0 +/* SPDX-License-Identifier: GPL-2.0 */ #define _GNU_SOURCE
@@ -46,8 +46,8 @@ char **environ;/* definition of a series of tests */ struct test { -const char *name;// test name -int (*func)(int min,int max);// handler +const char *name;/* test name */ +int (*func)(int min,int max);/* handler */ };#ifndef _NOLIBC_STDLIB_H
@@ -494,7 +494,7 @@ int run_syscall(int min, int max) euid0 =geteuid () ==0 ;for (test = min; test >=0 && test <= max; test++) { -int llen =0 ;// line length +int llen =0 ;/* line length */ /* avoid leaving empty lines below, this will insert holes into * test numbers.
@@ -584,7 +584,7 @@ int run_stdlib(int min, int max)void *p1, *p2;for (test = min; test >=0 && test <= max; test++) { -int llen =0 ;// line length +int llen =0 ;/* line length */ /* avoid leaving empty lines below, this will insert holes into * test numbers.
@@ -731,7 +731,7 @@ static int run_vfprintf(int min, int max)void *p1, *p2;for (test = min; test >=0 && test <= max; test++) { -int llen =0 ;// line length +int llen =0 ;/* line length */ /* avoid leaving empty lines below, this will insert holes into * test numbers.
Signature · 1 line
2.40.1
PATCH v2 12/15 tools/nolibc: validate C89 compatibility
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
To make sure no non-compatible changes are introduced accidentally validate the language standard when building the tests.
--- tools/testing/selftests/nolibc/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/nolibc/Makefile +1 -1
@@ -83,7 +83,7 @@ CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 -CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ +CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \ $(call cc-option,-fno-stack-protector) \ $(CFLAGS_STKP_$(ARCH)) $(CFLAGS_$(ARCH)) LDFLAGS := -s
Signature · 1 line
2.40.1
PATCH v2 13/15 tools/nolibc: s390: provide custom implementation for sys_fork
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
On s390 the first two arguments to the clone() syscall are swapped, as documented in clone(2).
--- tools/include/nolibc/arch-s390.h | 9 +++++++++ tools/include/nolibc/sys.h | 2 ++ 2 files changed, 11 insertions(+)
tools/include/nolibc/arch-s390.h +9 -0
@@ -5,6 +5,7 @@#ifndef _NOLIBC_ARCH_S390_H#define _NOLIBC_ARCH_S390_H +#include <asm/signal.h> #include <asm/unistd.h> /* The struct returned by the stat() syscall, equivalent to stat64(). The
@@ -223,4 +224,12 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, return (void *)my_syscall1(__NR_mmap, &args); }#define sys_mmap sys_mmap + +static __attribute__((unused)) +pid_t sys_fork (void ) +{ +return my_syscall5 (__NR_clone,0 ,SIGCHLD ,0 ,0 ,0 ); +} +#define sys_fork sys_fork +#endif /* _NOLIBC_ARCH_S390_H */
tools/include/nolibc/sys.h +2 -0
@@ -336,6 +336,7 @@ void exit(int status)* pid_t fork(void); */ +#ifndef sys_forkstatic __attribute__((unused))pid_t sys_fork (void ) {
@@ -351,6 +352,7 @@ pid_t sys_fork(void)#error Neither __NR_clone nor __NR_fork defined, cannot implement sys_fork() #endif } +#endif static __attribute__((unused))pid_t fork (void )
Signature · 1 line
2.40.1
PATCH v2 14/15 tools/nolibc: add testcase for fork()/waitpid()
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
On s390 the arguments to clone() which is used by fork() are different than other archs.
Make sure everything works correctly.
--- tools/testing/selftests/nolibc/nolibc-test.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +20 -0
@@ -474,6 +474,25 @@ static int test_getpagesize(void)return !c; } +static int test_fork (void ) +{ +int status; +pid_t pid =fork (); + +switch (pid) { +case -1 : +return 1 ; + +case 0 : +exit (123 ); + +default : + pid =waitpid (pid, &status,0 ); + +return pid ==-1 || !WIFEXITED (status) ||WEXITSTATUS (status) !=123 ; + } +} +/* Run syscall tests between IDs <min> and <max>. * Return 0 on success, non-zero on failure. */
@@ -530,6 +549,7 @@ int run_syscall(int min, int max)CASE_TEST (dup3_0); tmp =dup3 (0 ,100 ,0 );EXPECT_SYSNE (1 , tmp,-1 );close (tmp);break ;CASE_TEST (dup3_m1); tmp =dup3 (-1 ,100 ,0 );EXPECT_SYSER (1 , tmp,-1 ,EBADF );if (tmp !=-1 )close (tmp);break ;CASE_TEST (execve_root);EXPECT_SYSER (1 ,execve ("/" , (char *[]){ [0 ] ="/" , [1 ] =NULL },NULL ),-1 ,EACCES );break ; +CASE_TEST (fork);EXPECT_SYSZR (1 ,test_fork ());break ;CASE_TEST (getdents64_root);EXPECT_SYSNE (1 ,test_getdents64 ("/" ),-1 );break ;CASE_TEST (getdents64_null);EXPECT_SYSER (1 ,test_getdents64 ("/dev/null" ),-1 ,ENOTDIR );break ;CASE_TEST (gettimeofday_null);EXPECT_SYSZR (1 ,gettimeofday (NULL ,NULL ));break ;
Signature · 1 line
2.40.1
PATCH v2 15/15 tools/nolibc: remove LINUX_REBOOT_ constants
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
The same constants and some more have been exposed to userspace via linux/reboot.h for a long time.
To avoid conflicts and trim down nolibc a bit drop the custom definitions.
--- tools/include/nolibc/sys.h | 1 + tools/include/nolibc/types.h | 8 -------- 2 files changed, 1 insertion(+), 8 deletions(-)
tools/include/nolibc/sys.h +1 -0
@@ -21,6 +21,7 @@#include <linux/auxvec.h> #include <linux/fcntl.h> /* for O_* and AT_* */ #include <linux/stat.h> /* for statx() */ +#include <linux/reboot.h> /* for LINUX_REBOOT_* */ #include "arch.h" #include "errno.h"
tools/include/nolibc/types.h +0 -8
@@ -86,14 +86,6 @@#define SEEK_CUR 1#define SEEK_END 2 -/* cmd for reboot() */ -#define LINUX_REBOOT_MAGIC1 0xfee1dead -#define LINUX_REBOOT_MAGIC2 0x28121969 -#define LINUX_REBOOT_CMD_HALT 0xcdef0123 -#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc -#define LINUX_REBOOT_CMD_RESTART 0x01234567 -#define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2 -/* Macros used on waitpid()'s return status */ #define WEXITSTATUS (status) (((status) & 0xff00) >> 8)#define WIFEXITED (status) (((status) & 0x7f) == 0)
Signature · 1 line
2.40.1
v2 cover nolibc updates for v6.5]
In reply to Paul E. McKenneyHello!
This series contains a second version of the nolibc updates for v6.5:
1. tools/nolibc: tests: use volatile to force stack smashing,
courtesy of Thomas Weißschuh.
2. tools/nolibc: tests: fix build on non-c99 compliant compilers,
courtesy of Willy Tarreau.
3. tools/nolibc: fix build of the test case using glibc, courtesy
of Willy Tarreau.
4. tools/nolibc: add libc-test binary, courtesy of Thomas Weißschuh.
5. tools/nolibc: add wrapper for memfd_create, courtesy of Thomas
Weißschuh.
6. tools/nolibc: implement fd-based FILE streams, courtesy of
Thomas Weißschuh.
7. tools/nolibc: add testcases for vfprintf, courtesy of Thomas
Weißschuh.
8. tools/nolibc: Fix build of stdio.h due to header ordering,
courtesy of Mark Brown.
9. tools/nolibc: use standard __asm__ statements, courtesy of
Thomas Weißschuh.
10. tools/nolibc: use __inline__ syntax, courtesy of Thomas
Weißschuh.
11. tools/nolibc: use C89 comment syntax, courtesy of Thomas
Weißschuh.
12. tools/nolibc: validate C89 compatibility, courtesy of Thomas
Weißschuh.
13. tools/nolibc: s390: provide custom implementation for sys_fork,
courtesy of Thomas Weißschuh.
14. tools/nolibc: add testcase for fork()/waitpid(), courtesy of
Thomas Weißschuh.
15. tools/nolibc: remove LINUX_REBOOT_ constants, courtesy of Thomas
Weißschuh.
16. tools/nolibc: riscv: Fix up load/store instructions for rv32,
courtesy of Zhangjin Wu.
17. tools/nolibc/unistd: add syscall(), courtesy of Thomas Weißschuh.
18. selftests/nolibc: syscall_args: use generic __NR_statx, courtesy
of Zhangjin Wu.
19. selftests/nolibc: reduce syscalls during space padding, courtesy
of Thomas Weißschuh.
20. tools/nolibc: aarch64: add stackprotector support, courtesy of
Thomas Weißschuh.
21. tools/nolibc: arm: add stackprotector support, courtesy of
Thomas Weißschuh.
22. tools/nolibc: loongarch: add stackprotector support, courtesy
of Thomas Weißschuh.
23. tools/nolibc: mips: add stackprotector support, courtesy of
Thomas Weißschuh.
24. tools/nolibc: riscv: add stackprotector support, courtesy of
Thomas Weißschuh.
25. tools/nolibc: fix typo pint -> point, courtesy of Thomas
Weißschuh.
26. tools/nolibc: x86_64: disable stack protector for _start,
courtesy of Thomas Weißschuh.
27. tools/nolibc: ensure stack protector guard is never zero,
courtesy of Thomas Weißschuh.
28. tools/nolibc: add test for __stack_chk_guard initialization,
courtesy of Thomas Weißschuh.
29. tools/nolibc: reformat list of headers to be installed, courtesy
of Thomas Weißschuh.
30. tools/nolibc: add autodetection for stackprotector support,
courtesy of Thomas Weißschuh.
31. tools/nolibc: simplify stackprotector compiler flags, courtesy
of Thomas Weißschuh.
32. tools/nolibc: fix segfaults on compilers without attribute
no_stack_protector, courtesy of Thomas Weißschuh.
33. tools/nolibc: s390: disable stackprotector in _start, courtesy
of Thomas Weißschuh.
34. tools/nolibc: add support for prctl(), courtesy of Thomas
Weißschuh.
35. selftests/nolibc: prevent coredumps during test execution,
courtesy of Thomas Weißschuh.
36. tools/nolibc: support nanoseconds in stat(), courtesy of
Thomas Weißschuh.
37. selftests/nolibc: print name instead of number for EOVERFLOW,
courtesy of Zhangjin Wu.
38. selftests/nolibc: remove the duplicated gettimeofday_bad2,
courtesy of Zhangjin Wu.
39. tools/nolibc: ppoll/ppoll_time64: add a missing argument,
courtesy of Zhangjin Wu.
40. selftests/nolibc: test_fork: fix up duplicated print, courtesy
of Zhangjin Wu.
41. tools/nolibc: ensure fast64 integer types have 64 bits, courtesy
of Thomas Weißschuh.
42. selftests/nolibc: remove test gettimeofday_null, courtesy of
Thomas Weißschuh.
43. selftests/nolibc: allow specify extra arguments for qemu,
courtesy of Zhangjin Wu.
44. selftests/nolibc: fix up compile warning with glibc on x86_64,
courtesy of Zhangjin Wu.
45. selftests/nolibc: not include limits.h for nolibc, courtesy of
Zhangjin Wu.
46. selftests/nolibc: use INT_MAX instead of __INT_MAX__, courtesy
of Zhangjin Wu.
47. tools/nolibc: arm: add missing my_syscall6, courtesy of
Zhangjin Wu.
48. tools/nolibc: open: fix up compile warning for arm, courtesy of
Zhangjin Wu.
49. selftests/nolibc: support two errnos with EXPECT_SYSER2(),
courtesy of Zhangjin Wu.
50. selftests/nolibc: remove gettimeofday_bad1/2 completely, courtesy
of Zhangjin Wu.
51. selftests/nolibc: add new gettimeofday test cases, courtesy of
Zhangjin Wu.
52. selftests/nolibc: also count skipped and failed tests in output,
courtesy of Willy Tarreau.
53. selftests/nolibc: make sure gcc always use little endian on MIPS,
courtesy of Willy Tarreau.
Thanx, Paul
------------------------------------------------------------------------
b/tools/include/nolibc/Makefile | 18 + b/tools/include/nolibc/arch-aarch64.h | 32 +-- b/tools/include/nolibc/arch-arm.h | 44 ++-- b/tools/include/nolibc/arch-i386.h | 40 ++-- b/tools/include/nolibc/arch-loongarch.h | 38 ++-- b/tools/include/nolibc/arch-mips.h | 56 +++--- b/tools/include/nolibc/arch-riscv.h | 40 ++-- b/tools/include/nolibc/arch-s390.h | 2 b/tools/include/nolibc/arch-x86_64.h | 34 +-- b/tools/include/nolibc/arch.h | 2 b/tools/include/nolibc/compiler.h | 15 + b/tools/include/nolibc/nolibc.h | 2 b/tools/include/nolibc/stackprotector.h | 4 b/tools/include/nolibc/stdint.h | 10 - b/tools/include/nolibc/stdio.h | 95 +++++++--- b/tools/include/nolibc/stdlib.h | 18 - b/tools/include/nolibc/string.h | 4 b/tools/include/nolibc/sys.h | 23 ++ b/tools/include/nolibc/types.h | 8 b/tools/include/nolibc/unistd.h | 15 + b/tools/testing/selftests/nolibc/.gitignore | 1 b/tools/testing/selftests/nolibc/Makefile | 6 b/tools/testing/selftests/nolibc/nolibc-test.c | 8 tools/include/nolibc/Makefile | 1 tools/include/nolibc/arch-aarch64.h | 15 + tools/include/nolibc/arch-arm.h | 38 +++- tools/include/nolibc/arch-i386.h | 8 tools/include/nolibc/arch-loongarch.h | 15 + tools/include/nolibc/arch-mips.h | 16 + tools/include/nolibc/arch-riscv.h | 29 ++- tools/include/nolibc/arch-s390.h | 13 + tools/include/nolibc/arch-x86_64.h | 10 - tools/include/nolibc/compiler.h | 10 + tools/include/nolibc/stackprotector.h | 17 - tools/include/nolibc/stdint.h | 14 + tools/include/nolibc/sys.h | 108 +++++++---- tools/include/nolibc/types.h | 6 tools/testing/selftests/nolibc/Makefile | 39 ++-- tools/testing/selftests/nolibc/nolibc-test.c | 231 +++++++++++++++++++++---- 39 files changed, 738 insertions(+), 347 deletions(-)
PATCH v2 1/53 tools/nolibc: tests: use volatile to force stack smashing
From: Thomas Weißschuh <[email protected]>
Use a volatile pointer to write outside the buffer so the compiler can't optimize it away.
--- tools/testing/selftests/nolibc/nolibc-test.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +2 -6
@@ -667,17 +667,13 @@ int run_stdlib(int min, int max)return ret; } -#if defined(__clang__) -__attribute__((optnone)) -#elif defined(__GNUC__) -__attribute__((optimize("O0"))) -#endif static int smash_stack (void ) {char buf[100 ]; +volatile char *ptr = buf;for (size_t i =0 ; i <200 ; i++) - buf[i] ='P' ; + ptr[i] ='P' ;return 1 ; }
Signature · 1 line
2.40.1
PATCH v2 2/53 tools/nolibc: tests: fix build on non-c99 compliant compilers
In reply to Paul E. McKenneyFrom: Willy Tarreau <[email protected]>
Commit 9735716830f2 ("tools/nolibc: tests: add test for -fstack-protector") brought a declaration inside the initialization statement of a for loop, which breaks the build on compilers that do not default to c99 compatibility, making it more difficult to validate that the lib still builds on such compilers. The fix is trivial, so let's move the declaration to the variables block of the function instead. No backport is needed.
--- tools/testing/selftests/nolibc/nolibc-test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +2 -1
@@ -671,8 +671,9 @@ static int smash_stack(void) {char buf[100 ];volatile char *ptr = buf; +size_t i; -for (size_t i =0 ; i <200 ; i++) +for (i =0 ; i <200 ; i++) ptr[i] ='P' ;return 1 ;
Signature · 1 line
2.40.1
PATCH v2 3/53 tools/nolibc: fix build of the test case using glibc
In reply to Paul E. McKenneyFrom: Willy Tarreau <[email protected]>
Some extra tests for various integer types and limits were added by commit d1209597ff00 ("tools/nolibc: add tests for the integer limits in stdint.h"), but we forgot to retest with glibc. Stddef and stdint are now needed for the program to build there.
--- tools/testing/selftests/nolibc/nolibc-test.c | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
@@ -34,6 +34,8 @@#include <sched.h> #include <signal.h> #include <stdarg.h> +#include <stddef.h> +#include <stdint.h> #include <unistd.h> #endif #endif
Signature · 1 line
2.40.1
PATCH v2 4/53 tools/nolibc: add libc-test binary
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
This can be used to easily compare the behavior of nolibc to the system libc.
--- tools/testing/selftests/nolibc/.gitignore | 1 + tools/testing/selftests/nolibc/Makefile | 6 ++++++ 2 files changed, 7 insertions(+)
tools/testing/selftests/nolibc/.gitignore +1 -0
@@ -1,4 +1,5 @@ /initramfs/ +/libc-test /nolibc-test /run.out /sysroot/
tools/testing/selftests/nolibc/Makefile +6 -0
@@ -94,6 +94,7 @@ help: @echo " help this help" @echo " sysroot create the nolibc sysroot here (uses \$$ARCH)" @echo " nolibc-test build the executable (uses \$$CC and \$$CROSS_COMPILE)" + @echo " libc-test build an executable using the compiler's default libc instead" @echo " run-user runs the executable under QEMU (uses \$$ARCH, \$$TEST)" @echo " initramfs prepare the initramfs with nolibc-test" @echo " defconfig create a fresh new default config (uses \$$ARCH)"
@@ -128,6 +129,9 @@ nolibc-test: nolibc-test.c sysroot/$(ARCH)/include $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ -nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc +libc-test: nolibc-test.c + $(QUIET_CC)$(CC) -o $@ $< + # qemu user-land test run-user: nolibc-test $(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || :
@@ -159,6 +163,8 @@ clean: $(Q)rm -rf sysroot $(call QUIET_CLEAN, nolibc-test) $(Q)rm -f nolibc-test + $(call QUIET_CLEAN, libc-test) + $(Q)rm -f libc-test $(call QUIET_CLEAN, initramfs) $(Q)rm -rf initramfs $(call QUIET_CLEAN, run.out)
Signature · 1 line
2.40.1
PATCH v2 5/53 tools/nolibc: add wrapper for memfd_create
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
This is useful for users and will also be used by a future testcase.
--- tools/include/nolibc/sys.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
tools/include/nolibc/sys.h +23 -0
@@ -1365,6 +1365,29 @@ ssize_t write(int fd, const void *buf, size_t count)return ret; } + +/* +* int memfd_create(const char *name, unsigned int flags); +*/ + +static __attribute__((unused)) +int sys_memfd_create (const char *name,unsigned int flags) +{ +return my_syscall2 (__NR_memfd_create, name, flags); +} + +static __attribute__((unused)) +int memfd_create (const char *name,unsigned int flags) +{ +ssize_t ret =sys_memfd_create (name, flags); + +if (ret <0 ) { +SET_ERRNO (-ret); + ret =-1 ; + } +return ret; +} +/* make sure to include all global symbols */ #include "nolibc.h"
Signature · 1 line
2.40.1
PATCH v2 6/53 tools/nolibc: implement fd-based FILE streams
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
This enables the usage of the stream APIs with arbitrary filedescriptors.
It will be used by a future testcase.
--- tools/include/nolibc/stdio.h | 95 ++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 27 deletions(-)
tools/include/nolibc/stdio.h +68 -27
@@ -21,17 +21,75 @@#define EOF (-1)#endif -/* just define FILE as a non-empty type */ +/* just define FILE as a non-empty type. The value of the pointer gives +* the FD: FILE=~fd for fd>=0 or NULL for fd<0. This way positive FILE +* are immediately identified as abnormal entries (i.e. possible copies +* of valid pointers to something else). +*/ typedef struct FILE {char dummy[1 ]; }FILE ; -/* We define the 3 common stdio files as constant invalid pointers that -* are easily recognized. -*/ -static __attribute__((unused))FILE *const stdin = (FILE *)-3 ; -static __attribute__((unused))FILE *const stdout = (FILE *)-2 ; -static __attribute__((unused))FILE *const stderr = (FILE *)-1 ; +static __attribute__((unused))FILE *const stdin = (FILE *)(intptr_t )~STDIN_FILENO ; +static __attribute__((unused))FILE *const stdout = (FILE *)(intptr_t )~STDOUT_FILENO ; +static __attribute__((unused))FILE *const stderr = (FILE *)(intptr_t )~STDERR_FILENO ; + +/* provides a FILE* equivalent of fd. The mode is ignored. */ +static __attribute__((unused)) +FILE *fdopen (int fd,const char *mode __attribute__((unused))) +{ +if (fd <0 ) { +SET_ERRNO (EBADF ); +return NULL ; + } +return (FILE *)(intptr_t )~fd; +} + +/* provides the fd of stream. */ +static __attribute__((unused)) +int fileno (FILE *stream) +{ +intptr_t i = (intptr_t )stream; + +if (i >=0 ) { +SET_ERRNO (EBADF ); +return -1 ; + } +return ~i; +} + +/* flush a stream. */ +static __attribute__((unused)) +int fflush (FILE *stream) +{ +intptr_t i = (intptr_t )stream; + +/* NULL is valid here. */ +if (i >0 ) { +SET_ERRNO (EBADF ); +return -1 ; + } + +/* Don't do anything, nolibc does not support buffering. */ +return 0 ; +} + +/* flush a stream. */ +static __attribute__((unused)) +int fclose (FILE *stream) +{ +intptr_t i = (intptr_t )stream; + +if (i >=0 ) { +SET_ERRNO (EBADF ); +return -1 ; + } + +if (close (~i)) +return EOF ; + +return 0 ; +}/* getc(), fgetc(), getchar() */
@@ -41,14 +99,8 @@ static __attribute__((unused))int fgetc (FILE * stream) {unsigned char ch; -int fd; -if (stream < stdin || stream > stderr) -return EOF ; - - fd =3 + (long )stream; - -if (read (fd, &ch,1 ) <=0 ) +if (read (fileno (stream), &ch,1 ) <=0 )return EOF ;return ch; }
@@ -68,14 +120,8 @@ static __attribute__((unused))int fputc (int c,FILE * stream) {unsigned char ch = c; -int fd; - -if (stream < stdin || stream > stderr) -return EOF ; - - fd =3 + (long )stream; -if (write (fd, &ch,1 ) <=0 ) +if (write (fileno (stream), &ch,1 ) <=0 )return EOF ;return ch; }
@@ -96,12 +142,7 @@ static __attribute__((unused))int _fwrite (const void *buf,size_t size,FILE *stream) {ssize_t ret; -int fd; - -if (stream < stdin || stream > stderr) -return EOF ; - - fd =3 + (long )stream; +int fd =fileno (stream);while (size) { ret =write (fd, buf, size);
Signature · 1 line
2.40.1
PATCH v2 7/53 tools/nolibc: add testcases for vfprintf
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
vfprintf() is complex and so far did not have proper tests.
--- tools/testing/selftests/nolibc/nolibc-test.c | 86 ++++++++++++++++++++ 1 file changed, 86 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +86 -0
@@ -20,6 +20,7 @@#include <linux/reboot.h> #include <sys/io.h> #include <sys/ioctl.h> +#include <sys/mman.h> #include <sys/mount.h> #include <sys/reboot.h> #include <sys/stat.h>
@@ -669,6 +670,90 @@ int run_stdlib(int min, int max)return ret; } +#define EXPECT_VFPRINTF (c, expected, fmt, ...) \ + ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) + +static int expect_vfprintf (int llen,size_t c,const char *expected,const char *fmt, ...) +{ +int ret, fd, w, r; +char buf[100 ]; +FILE *memfile; +va_list args; + + fd =memfd_create ("vfprintf" ,0 ); +if (fd ==-1 ) { +pad_spc (llen,64 ,"[FAIL]\n" ); +return 1 ; + } + + memfile =fdopen (fd,"w+" ); +if (!memfile) { +pad_spc (llen,64 ,"[FAIL]\n" ); +return 1 ; + } + +va_start (args, fmt); + w =vfprintf (memfile, fmt, args); +va_end (args); + +if (w != c) { + llen +=printf (" written(%d) != %d" , w, (int ) c); +pad_spc (llen,64 ,"[FAIL]\n" ); +return 1 ; + } + +fflush (memfile); +lseek (fd,0 ,SEEK_SET ); + + r =read (fd, buf,sizeof (buf) -1 ); + buf[r] ='\0' ; + +fclose (memfile); + +if (r != w) { + llen +=printf (" written(%d) != read(%d)" , w, r); +pad_spc (llen,64 ,"[FAIL]\n" ); +return 1 ; + } + + llen +=printf (" \"%s\" = \"%s\"" , expected, buf); + ret =strncmp (expected, buf, c); + +pad_spc (llen,64 , ret ?"[FAIL]\n" :" [OK]\n" ); +return ret; +} + +static int run_vfprintf (int min,int max) +{ +int test; +int tmp; +int ret =0 ; +void *p1, *p2; + +for (test = min; test >=0 && test <= max; test++) { +int llen =0 ;// line length + +/* avoid leaving empty lines below, this will insert holes into +* test numbers. +*/ +switch (test + __LINE__ +1 ) { +CASE_TEST (empty);EXPECT_VFPRINTF (0 ,"" ,"" );break ; +CASE_TEST (simple);EXPECT_VFPRINTF (3 ,"foo" ,"foo" );break ; +CASE_TEST (string);EXPECT_VFPRINTF (3 ,"foo" ,"%s" ,"foo" );break ; +CASE_TEST (number);EXPECT_VFPRINTF (4 ,"1234" ,"%d" ,1234 );break ; +CASE_TEST (negnumber);EXPECT_VFPRINTF (5 ,"-1234" ,"%d" ,-1234 );break ; +CASE_TEST (unsigned);EXPECT_VFPRINTF (5 ,"12345" ,"%u" ,12345 );break ; +CASE_TEST (char);EXPECT_VFPRINTF (1 ,"c" ,"%c" ,'c' );break ; +CASE_TEST (hex);EXPECT_VFPRINTF (1 ,"f" ,"%x" ,0xf );break ; +CASE_TEST (pointer);EXPECT_VFPRINTF (3 ,"0x1" ,"%p" , (void *)0x1 );break ; +case __LINE__: +return ret;/* must be last */ +/* note: do not set any defaults so as to permit holes above */ + } + } +return ret; +} +static int smash_stack (void ) {char buf[100 ];
@@ -777,6 +862,7 @@ static const struct test test_names[] = {/* add new tests here */ { .name = "syscall", .func = run_syscall }, { .name = "stdlib", .func = run_stdlib }, + { .name = "vfprintf", .func = run_vfprintf }, { .name = "protection", .func = run_protection }, { 0 } };
Signature · 1 line
2.40.1
PATCH v2 8/53 tools/nolibc: Fix build of stdio.h due to header ordering
In reply to Paul E. McKenneyFrom: Mark Brown <[email protected]>
When we added fd based file streams we created references to STx_FILENO in stdio.h but these constants are declared in unistd.h which is the last file included by the top level nolibc.h meaning those constants are not defined when we try to build stdio.h. This causes programs using nolibc.h to fail to build.
Reorder the headers to avoid this issue.
--- tools/include/nolibc/nolibc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/nolibc.h +1 -1
@@ -99,11 +99,11 @@#include "sys.h" #include "ctype.h" #include "signal.h" +#include "unistd.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "time.h" -#include "unistd.h" #include "stackprotector.h" /* Used by programs to avoid std includes */
Signature · 1 line
2.40.1
PATCH v2 9/53 tools/nolibc: use standard __asm__ statements
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
Most of the code was migrated to C99-conformant __asm__ statements before. It seems string.h was missed.
--- tools/include/nolibc/string.h | 4 ++-- tools/testing/selftests/nolibc/nolibc-test.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
tools/include/nolibc/string.h +2 -2
@@ -90,7 +90,7 @@ void *memset(void *dst, int b, size_t len)while (len--) {/* prevent gcc from recognizing memset() here */ - asmvolatile ("" ); + __asm__volatile ("" ); *(p++) = b; }return dst;
@@ -139,7 +139,7 @@ size_t strlen(const char *str)size_t len;for (len =0 ; str[len]; len++) - asm("" ); + __asm__("" );return len; }
tools/testing/selftests/nolibc/nolibc-test.c +1 -1
@@ -984,7 +984,7 @@ int main(int argc, char **argv, char **envp)#else else if (ioperm(0x501, 1, 1) == 0) #endif - asm volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0)); + __asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));/* if it does nothing, fall back to the regular panic */ #endif }
Signature · 1 line
2.40.1
PATCH v2 10/53 tools/nolibc: use __inline__ syntax
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
When building in strict C89 mode the "inline" keyword is unknown. While "__inline__" is non-standard it is used by the kernel headers themselves.
So the used compilers would have to support it or the users shim it with a #define.
--- tools/include/nolibc/stdlib.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
tools/include/nolibc/stdlib.h +9 -9
@@ -102,7 +102,7 @@ char *_getenv(const char *name, char **environ)return NULL ; } -static inline __attribute__((unused,always_inline)) +static __inline__ __attribute__((unused,always_inline))char *getenv (const char *name) {extern char **environ;
@@ -231,7 +231,7 @@ int utoh_r(unsigned long in, char *buffer)/* converts unsigned long <in> to an hex string using the static itoa_buffer * and returns the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *utoh (unsigned long in) {utoh_r (in, itoa_buffer);
@@ -293,7 +293,7 @@ int itoa_r(long in, char *buffer)/* for historical compatibility, same as above but returns the pointer to the * buffer. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *ltoa_r (long in,char *buffer) {itoa_r (in, buffer);
@@ -303,7 +303,7 @@ char *ltoa_r(long in, char *buffer)/* converts long integer <in> to a string using the static itoa_buffer and * returns the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *itoa (long in) {itoa_r (in, itoa_buffer);
@@ -313,7 +313,7 @@ char *itoa(long in)/* converts long integer <in> to a string using the static itoa_buffer and * returns the pointer to that string. Same as above, for compatibility. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *ltoa (long in) {itoa_r (in, itoa_buffer);
@@ -323,7 +323,7 @@ char *ltoa(long in)/* converts unsigned long integer <in> to a string using the static itoa_buffer * and returns the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *utoa (unsigned long in) {utoa_r (in, itoa_buffer);
@@ -367,7 +367,7 @@ int u64toh_r(uint64_t in, char *buffer)/* converts uint64_t <in> to an hex string using the static itoa_buffer and * returns the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *u64toh (uint64_t in) {u64toh_r (in, itoa_buffer);
@@ -429,7 +429,7 @@ int i64toa_r(int64_t in, char *buffer)/* converts int64_t <in> to a string using the static itoa_buffer and returns * the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *i64toa (int64_t in) {i64toa_r (in, itoa_buffer);
@@ -439,7 +439,7 @@ char *i64toa(int64_t in)/* converts uint64_t <in> to a string using the static itoa_buffer and returns * the pointer to that string. */ -static inline __attribute__((unused)) +static __inline__ __attribute__((unused))char *u64toa (uint64_t in) {u64toa_r (in, itoa_buffer);
Signature · 1 line
2.40.1
PATCH v2 11/53 tools/nolibc: use C89 comment syntax
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
Most of nolibc is already using C89 comments.
--- tools/include/nolibc/arch-aarch64.h | 32 +++++------ tools/include/nolibc/arch-arm.h | 44 +++++++-------- tools/include/nolibc/arch-i386.h | 40 +++++++------- tools/include/nolibc/arch-loongarch.h | 38 ++++++------- tools/include/nolibc/arch-mips.h | 56 ++++++++++---------- tools/include/nolibc/arch-riscv.h | 40 +++++++------- tools/include/nolibc/arch-s390.h | 2 +- tools/include/nolibc/arch-x86_64.h | 34 ++++++------ tools/include/nolibc/stackprotector.h | 4 +- tools/include/nolibc/sys.h | 8 +-- tools/testing/selftests/nolibc/nolibc-test.c | 12 ++--- 11 files changed, 155 insertions(+), 155 deletions(-)
tools/include/nolibc/arch-aarch64.h +16 -16
@@ -176,24 +176,24 @@ const unsigned long *_auxv __attribute__((weak));void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) { __asm__volatile ( -"ldr x0, [sp]\n" // argc (x0) was in the stack -"add x1, sp, 8\n" // argv (x1) = sp -"lsl x2, x0, 3\n" // envp (x2) = 8*argc ... -"add x2, x2, 8\n" // + 8 (skip null) -"add x2, x2, x1\n" // + argv -"adrp x3, environ\n" // x3 = &environ (high bits) -"str x2, [x3, #:lo12:environ]\n" // store envp into environ -"mov x4, x2\n" // search for auxv (follows NULL after last env) +"ldr x0, [sp]\n" /* argc (x0) was in the stack */ +"add x1, sp, 8\n" /* argv (x1) = sp */ +"lsl x2, x0, 3\n" /* envp (x2) = 8*argc ... */ +"add x2, x2, 8\n" /* + 8 (skip null) */ +"add x2, x2, x1\n" /* + argv */ +"adrp x3, environ\n" /* x3 = &environ (high bits) */ +"str x2, [x3, #:lo12:environ]\n" /* store envp into environ */ +"mov x4, x2\n" /* search for auxv (follows NULL after last env) */ "0:\n" -"ldr x5, [x4], 8\n" // x5 = *x4; x4 += 8 -"cbnz x5, 0b\n" // and stop at NULL after last env -"adrp x3, _auxv\n" // x3 = &_auxv (high bits) -"str x4, [x3, #:lo12:_auxv]\n" // store x4 into _auxv -"and sp, x1, -16\n" // sp must be 16-byte aligned in the callee -"bl main\n" // main() returns the status code, we'll exit with it. -"mov x8, 93\n" // NR_exit == 93 +"ldr x5, [x4], 8\n" /* x5 = *x4; x4 += 8 */ +"cbnz x5, 0b\n" /* and stop at NULL after last env */ +"adrp x3, _auxv\n" /* x3 = &_auxv (high bits) */ +"str x4, [x3, #:lo12:_auxv]\n" /* store x4 into _auxv */ +"and sp, x1, -16\n" /* sp must be 16-byte aligned in the callee */ +"bl main\n" /* main() returns the status code, we'll exit with it. */ +"mov x8, 93\n" /* NR_exit == 93 */ "svc #0\n" );__builtin_unreachable (); } -#endif // _NOLIBC_ARCH_AARCH64_H +#endif /* _NOLIBC_ARCH_AARCH64_H */
tools/include/nolibc/arch-arm.h +22 -22
60 lines not on this page · raw message
tools/include/nolibc/arch-i386.h +20 -20
@@ -194,31 +194,31 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protec { __asm__volatile ( #ifdef NOLIBC_STACKPROTECTOR - "call __stack_chk_init\n" // initialize stack protector + "call __stack_chk_init\n" /* initialize stack protector */ #endif -"pop %eax\n" // argc (first arg, %eax) -"mov %esp, %ebx\n" // argv[] (second arg, %ebx) -"lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) -"mov %ecx, environ\n" // save environ -"xor %ebp, %ebp\n" // zero the stack frame -"mov %ecx, %edx\n" // search for auxv (follows NULL after last env) +"pop %eax\n" /* argc (first arg, %eax) */ +"mov %esp, %ebx\n" /* argv[] (second arg, %ebx) */ +"lea 4(%ebx,%eax,4),%ecx\n" /* then a NULL then envp (third arg, %ecx) */ +"mov %ecx, environ\n" /* save environ */ +"xor %ebp, %ebp\n" /* zero the stack frame */ +"mov %ecx, %edx\n" /* search for auxv (follows NULL after last env) */ "0:\n" -"add $4, %edx\n" // search for auxv using edx, it follows the -"cmp -4(%edx), %ebp\n" // ... NULL after last env (ebp is zero here) +"add $4, %edx\n" /* search for auxv using edx, it follows the */ +"cmp -4(%edx), %ebp\n" /* ... NULL after last env (ebp is zero here) */ "jnz 0b\n" -"mov %edx, _auxv\n" // save it into _auxv -"and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned before -"sub $4, %esp\n" // the call instruction (args are aligned) -"push %ecx\n" // push all registers on the stack so that we -"push %ebx\n" // support both regparm and plain stack modes +"mov %edx, _auxv\n" /* save it into _auxv */ +"and $-16, %esp\n" /* x86 ABI : esp must be 16-byte aligned before */ +"sub $4, %esp\n" /* the call instruction (args are aligned) */ +"push %ecx\n" /* push all registers on the stack so that we */ +"push %ebx\n" /* support both regparm and plain stack modes */ "push %eax\n" -"call main\n" // main() returns the status code in %eax -"mov %eax, %ebx\n" // retrieve exit code (32-bit int) -"movl $1, %eax\n" // NR_exit == 1 -"int $0x80\n" // exit now -"hlt\n" // ensure it does not +"call main\n" /* main() returns the status code in %eax */ +"mov %eax, %ebx\n" /* retrieve exit code (32-bit int) */ +"movl $1, %eax\n" /* NR_exit == 1 */ +"int $0x80\n" /* exit now */ +"hlt\n" /* ensure it does not */ );__builtin_unreachable (); } -#endif // _NOLIBC_ARCH_I386_H +#endif /* _NOLIBC_ARCH_I386_H */
tools/include/nolibc/arch-loongarch.h +19 -19
@@ -158,7 +158,7 @@ const unsigned long *_auxv __attribute__((weak));#define LONG_ADDI "addi.w"#define LONG_SLL "slli.w"#define LONG_BSTRINS "bstrins.w" -#else // __loongarch_grlen == 64 +#else /* __loongarch_grlen == 64 */ #define LONGLOG "3"#define SZREG "8"#define REG_L "ld.d"
@@ -173,28 +173,28 @@ const unsigned long *_auxv __attribute__((weak));void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) { __asm__volatile ( -REG_L " $a0, $sp, 0\n" // argc (a0) was in the stack -LONG_ADDI " $a1, $sp, " SZREG "\n" // argv (a1) = sp + SZREG -LONG_SLL " $a2, $a0, " LONGLOG "\n" // envp (a2) = SZREG*argc ... -LONG_ADDI " $a2, $a2, " SZREG "\n" // + SZREG (skip null) -LONG_ADD " $a2, $a2, $a1\n" // + argv +REG_L " $a0, $sp, 0\n" /* argc (a0) was in the stack */ +LONG_ADDI " $a1, $sp, " SZREG "\n" /* argv (a1) = sp + SZREG */ +LONG_SLL " $a2, $a0, " LONGLOG "\n" /* envp (a2) = SZREG*argc ... */ +LONG_ADDI " $a2, $a2, " SZREG "\n" /* + SZREG (skip null) */ +LONG_ADD " $a2, $a2, $a1\n" /* + argv */ -"move $a3, $a2\n" // iterate a3 over envp to find auxv (after NULL) -"0:\n" // do { -REG_L " $a4, $a3, 0\n" // a4 = *a3; -LONG_ADDI " $a3, $a3, " SZREG "\n" // a3 += sizeof(void*); -"bne $a4, $zero, 0b\n" // } while (a4); -"la.pcrel $a4, _auxv\n" // a4 = &_auxv -LONG_S " $a3, $a4, 0\n" // store a3 into _auxv +"move $a3, $a2\n" /* iterate a3 over envp to find auxv (after NULL) */ +"0:\n" /* do { */ +REG_L " $a4, $a3, 0\n" /* a4 = *a3; */ +LONG_ADDI " $a3, $a3, " SZREG "\n" /* a3 += sizeof(void*); */ +"bne $a4, $zero, 0b\n" /* } while (a4); */ +"la.pcrel $a4, _auxv\n" /* a4 = &_auxv */ +LONG_S " $a3, $a4, 0\n" /* store a3 into _auxv */ -"la.pcrel $a3, environ\n" // a3 = &environ -LONG_S " $a2, $a3, 0\n" // store envp(a2) into environ -LONG_BSTRINS " $sp, $zero, 3, 0\n" // sp must be 16-byte aligned -"bl main\n" // main() returns the status code, we'll exit with it. -"li.w $a7, 93\n" // NR_exit == 93 +"la.pcrel $a3, environ\n" /* a3 = &environ */ +LONG_S " $a2, $a3, 0\n" /* store envp(a2) into environ */ +LONG_BSTRINS " $sp, $zero, 3, 0\n" /* sp must be 16-byte aligned */ +"bl main\n" /* main() returns the status code, we'll exit with it. */ +"li.w $a7, 93\n" /* NR_exit == 93 */ "syscall 0\n" );__builtin_unreachable (); } -#endif // _NOLIBC_ARCH_LOONGARCH_H +#endif /* _NOLIBC_ARCH_LOONGARCH_H */
tools/include/nolibc/arch-mips.h +28 -28
70 lines not on this page · raw message
tools/include/nolibc/arch-riscv.h +20 -20
@@ -181,28 +181,28 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(void)".option norelax\n" "lla gp, __global_pointer$\n" ".option pop\n" -"lw a0, 0(sp)\n" // argc (a0) was in the stack -"add a1, sp, " SZREG "\n" // argv (a1) = sp -"slli a2, a0, " PTRLOG "\n" // envp (a2) = SZREG*argc ... -"add a2, a2, " SZREG "\n" // + SZREG (skip null) -"add a2,a2,a1\n" // + argv - -"add a3, a2, zero\n" // iterate a3 over envp to find auxv (after NULL) -"0:\n" // do { -"ld a4, 0(a3)\n" // a4 = *a3; -"add a3, a3, " SZREG "\n" // a3 += sizeof(void*); -"bne a4, zero, 0b\n" // } while (a4); -"lui a4, %hi(_auxv)\n" // a4 = &_auxv (high bits) -"sd a3, %lo(_auxv)(a4)\n" // store a3 into _auxv - -"lui a3, %hi(environ)\n" // a3 = &environ (high bits) -"sd a2,%lo(environ)(a3)\n" // store envp(a2) into environ -"andi sp,a1,-16\n" // sp must be 16-byte aligned -"call main\n" // main() returns the status code, we'll exit with it. -"li a7, 93\n" // NR_exit == 93 +"lw a0, 0(sp)\n" /* argc (a0) was in the stack */ +"add a1, sp, " SZREG "\n" /* argv (a1) = sp */ +"slli a2, a0, " PTRLOG "\n" /* envp (a2) = SZREG*argc ... */ +"add a2, a2, " SZREG "\n" /* + SZREG (skip null) */ +"add a2,a2,a1\n" /* + argv */ + +"add a3, a2, zero\n" /* iterate a3 over envp to find auxv (after NULL) */ +"0:\n" /* do { */ +"ld a4, 0(a3)\n" /* a4 = *a3; */ +"add a3, a3, " SZREG "\n" /* a3 += sizeof(void*); */ +"bne a4, zero, 0b\n" /* } while (a4); */ +"lui a4, %hi(_auxv)\n" /* a4 = &_auxv (high bits) */ +"sd a3, %lo(_auxv)(a4)\n" /* store a3 into _auxv */ + +"lui a3, %hi(environ)\n" /* a3 = &environ (high bits) */ +"sd a2,%lo(environ)(a3)\n" /* store envp(a2) into environ */ +"andi sp,a1,-16\n" /* sp must be 16-byte aligned */ +"call main\n" /* main() returns the status code, we'll exit with it. */ +"li a7, 93\n" /* NR_exit == 93 */ "ecall\n" ); __builtin_unreachable(); } -#endif // _NOLIBC_ARCH_RISCV_H +#endif /* _NOLIBC_ARCH_RISCV_H */
tools/include/nolibc/arch-s390.h +1 -1
@@ -223,4 +223,4 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, return (void *)my_syscall1(__NR_mmap, &args); }#define sys_mmap sys_mmap -#endif // _NOLIBC_ARCH_S390_H +#endif /* _NOLIBC_ARCH_S390_H */
tools/include/nolibc/arch-x86_64.h +17 -17
@@ -194,27 +194,27 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(void) { __asm__volatile ( #ifdef NOLIBC_STACKPROTECTOR - "call __stack_chk_init\n" // initialize stack protector + "call __stack_chk_init\n" /* initialize stack protector */ #endif -"pop %rdi\n" // argc (first arg, %rdi) -"mov %rsp, %rsi\n" // argv[] (second arg, %rsi) -"lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) -"mov %rdx, environ\n" // save environ -"xor %ebp, %ebp\n" // zero the stack frame -"mov %rdx, %rax\n" // search for auxv (follows NULL after last env) +"pop %rdi\n" /* argc (first arg, %rdi) */ +"mov %rsp, %rsi\n" /* argv[] (second arg, %rsi) */ +"lea 8(%rsi,%rdi,8),%rdx\n" /* then a NULL then envp (third arg, %rdx) */ +"mov %rdx, environ\n" /* save environ */ +"xor %ebp, %ebp\n" /* zero the stack frame */ +"mov %rdx, %rax\n" /* search for auxv (follows NULL after last env) */ "0:\n" -"add $8, %rax\n" // search for auxv using rax, it follows the -"cmp -8(%rax), %rbp\n" // ... NULL after last env (rbp is zero here) +"add $8, %rax\n" /* search for auxv using rax, it follows the */ +"cmp -8(%rax), %rbp\n" /* ... NULL after last env (rbp is zero here) */ "jnz 0b\n" -"mov %rax, _auxv\n" // save it into _auxv -"and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned before call -"call main\n" // main() returns the status code, we'll exit with it. -"mov %eax, %edi\n" // retrieve exit code (32 bit) -"mov $60, %eax\n" // NR_exit == 60 -"syscall\n" // really exit -"hlt\n" // ensure it does not return +"mov %rax, _auxv\n" /* save it into _auxv */ +"and $-16, %rsp\n" /* x86 ABI : esp must be 16-byte aligned before call */ +"call main\n" /* main() returns the status code, we'll exit with it. */ +"mov %eax, %edi\n" /* retrieve exit code (32 bit) */ +"mov $60, %eax\n" /* NR_exit == 60 */ +"syscall\n" /* really exit */ +"hlt\n" /* ensure it does not return */ );__builtin_unreachable (); } -#endif // _NOLIBC_ARCH_X86_64_H +#endif /* _NOLIBC_ARCH_X86_64_H */
tools/include/nolibc/stackprotector.h +2 -2
@@ -48,6 +48,6 @@ void __stack_chk_init(void)/* a bit more randomness in case getrandom() fails */ __stack_chk_guard ^= (uintptr_t ) &__stack_chk_guard; } -#endif // defined(NOLIBC_STACKPROTECTOR) +#endif /* defined(NOLIBC_STACKPROTECTOR) */ -#endif // _NOLIBC_STACKPROTECTOR_H +#endif /* _NOLIBC_STACKPROTECTOR_H */
tools/include/nolibc/sys.h +4 -4
@@ -12,15 +12,15 @@/* system includes */ #include <asm/unistd.h> -#include <asm/signal.h> // for SIGCHLD +#include <asm/signal.h> /* for SIGCHLD */ #include <asm/ioctls.h> #include <asm/mman.h> #include <linux/fs.h> #include <linux/loop.h> #include <linux/time.h> #include <linux/auxvec.h> -#include <linux/fcntl.h> // for O_* and AT_* -#include <linux/stat.h> // for statx() +#include <linux/fcntl.h> /* for O_* and AT_* */ +#include <linux/stat.h> /* for statx() */ #include "arch.h" #include "errno.h"
@@ -322,7 +322,7 @@ static __attribute__((noreturn,unused))void sys_exit (int status) {my_syscall1 (__NR_exit, status &255 ); -while (1 );// shut the "noreturn" warnings. +while (1 );/* shut the "noreturn" warnings. */ } static __attribute__((noreturn,unused))
tools/testing/selftests/nolibc/nolibc-test.c +6 -6
@@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0 +/* SPDX-License-Identifier: GPL-2.0 */ #define _GNU_SOURCE
@@ -46,8 +46,8 @@ char **environ;/* definition of a series of tests */ struct test { -const char *name;// test name -int (*func)(int min,int max);// handler +const char *name;/* test name */ +int (*func)(int min,int max);/* handler */ };#ifndef _NOLIBC_STDLIB_H
@@ -494,7 +494,7 @@ int run_syscall(int min, int max) euid0 =geteuid () ==0 ;for (test = min; test >=0 && test <= max; test++) { -int llen =0 ;// line length +int llen =0 ;/* line length */ /* avoid leaving empty lines below, this will insert holes into * test numbers.
@@ -584,7 +584,7 @@ int run_stdlib(int min, int max)void *p1, *p2;for (test = min; test >=0 && test <= max; test++) { -int llen =0 ;// line length +int llen =0 ;/* line length */ /* avoid leaving empty lines below, this will insert holes into * test numbers.
@@ -731,7 +731,7 @@ static int run_vfprintf(int min, int max)void *p1, *p2;for (test = min; test >=0 && test <= max; test++) { -int llen =0 ;// line length +int llen =0 ;/* line length */ /* avoid leaving empty lines below, this will insert holes into * test numbers.
Signature · 1 line
2.40.1
PATCH v2 12/53 tools/nolibc: validate C89 compatibility
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
To make sure no non-compatible changes are introduced accidentally validate the language standard when building the tests.
--- tools/testing/selftests/nolibc/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/nolibc/Makefile +1 -1
@@ -83,7 +83,7 @@ CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 -CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ +CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \ $(call cc-option,-fno-stack-protector) \ $(CFLAGS_STKP_$(ARCH)) $(CFLAGS_$(ARCH)) LDFLAGS := -s
Signature · 1 line
2.40.1
PATCH v2 13/53 tools/nolibc: s390: provide custom implementation for sys_fork
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
On s390 the first two arguments to the clone() syscall are swapped, as documented in clone(2).
--- tools/include/nolibc/arch-s390.h | 9 +++++++++ tools/include/nolibc/sys.h | 2 ++ 2 files changed, 11 insertions(+)
tools/include/nolibc/arch-s390.h +9 -0
@@ -5,6 +5,7 @@#ifndef _NOLIBC_ARCH_S390_H#define _NOLIBC_ARCH_S390_H +#include <asm/signal.h> #include <asm/unistd.h> /* The struct returned by the stat() syscall, equivalent to stat64(). The
@@ -223,4 +224,12 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, return (void *)my_syscall1(__NR_mmap, &args); }#define sys_mmap sys_mmap + +static __attribute__((unused)) +pid_t sys_fork (void ) +{ +return my_syscall5 (__NR_clone,0 ,SIGCHLD ,0 ,0 ,0 ); +} +#define sys_fork sys_fork +#endif /* _NOLIBC_ARCH_S390_H */
tools/include/nolibc/sys.h +2 -0
@@ -336,6 +336,7 @@ void exit(int status)* pid_t fork(void); */ +#ifndef sys_forkstatic __attribute__((unused))pid_t sys_fork (void ) {
@@ -351,6 +352,7 @@ pid_t sys_fork(void)#error Neither __NR_clone nor __NR_fork defined, cannot implement sys_fork() #endif } +#endif static __attribute__((unused))pid_t fork (void )
Signature · 1 line
2.40.1
PATCH v2 14/53 tools/nolibc: add testcase for fork()/waitpid()
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
On s390 the arguments to clone() which is used by fork() are different than other archs.
Make sure everything works correctly.
--- tools/testing/selftests/nolibc/nolibc-test.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +20 -0
@@ -474,6 +474,25 @@ static int test_getpagesize(void)return !c; } +static int test_fork (void ) +{ +int status; +pid_t pid =fork (); + +switch (pid) { +case -1 : +return 1 ; + +case 0 : +exit (123 ); + +default : + pid =waitpid (pid, &status,0 ); + +return pid ==-1 || !WIFEXITED (status) ||WEXITSTATUS (status) !=123 ; + } +} +/* Run syscall tests between IDs <min> and <max>. * Return 0 on success, non-zero on failure. */
@@ -530,6 +549,7 @@ int run_syscall(int min, int max)CASE_TEST (dup3_0); tmp =dup3 (0 ,100 ,0 );EXPECT_SYSNE (1 , tmp,-1 );close (tmp);break ;CASE_TEST (dup3_m1); tmp =dup3 (-1 ,100 ,0 );EXPECT_SYSER (1 , tmp,-1 ,EBADF );if (tmp !=-1 )close (tmp);break ;CASE_TEST (execve_root);EXPECT_SYSER (1 ,execve ("/" , (char *[]){ [0 ] ="/" , [1 ] =NULL },NULL ),-1 ,EACCES );break ; +CASE_TEST (fork);EXPECT_SYSZR (1 ,test_fork ());break ;CASE_TEST (getdents64_root);EXPECT_SYSNE (1 ,test_getdents64 ("/" ),-1 );break ;CASE_TEST (getdents64_null);EXPECT_SYSER (1 ,test_getdents64 ("/dev/null" ),-1 ,ENOTDIR );break ;CASE_TEST (gettimeofday_null);EXPECT_SYSZR (1 ,gettimeofday (NULL ,NULL ));break ;
Signature · 1 line
2.40.1
PATCH v2 15/53 tools/nolibc: remove LINUX_REBOOT_ constants
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
The same constants and some more have been exposed to userspace via linux/reboot.h for a long time.
To avoid conflicts and trim down nolibc a bit drop the custom definitions.
--- tools/include/nolibc/sys.h | 1 + tools/include/nolibc/types.h | 8 -------- 2 files changed, 1 insertion(+), 8 deletions(-)
tools/include/nolibc/sys.h +1 -0
@@ -21,6 +21,7 @@#include <linux/auxvec.h> #include <linux/fcntl.h> /* for O_* and AT_* */ #include <linux/stat.h> /* for statx() */ +#include <linux/reboot.h> /* for LINUX_REBOOT_* */ #include "arch.h" #include "errno.h"
tools/include/nolibc/types.h +0 -8
@@ -86,14 +86,6 @@#define SEEK_CUR 1#define SEEK_END 2 -/* cmd for reboot() */ -#define LINUX_REBOOT_MAGIC1 0xfee1dead -#define LINUX_REBOOT_MAGIC2 0x28121969 -#define LINUX_REBOOT_CMD_HALT 0xcdef0123 -#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc -#define LINUX_REBOOT_CMD_RESTART 0x01234567 -#define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2 -/* Macros used on waitpid()'s return status */ #define WEXITSTATUS (status) (((status) & 0xff00) >> 8)#define WIFEXITED (status) (((status) & 0x7f) == 0)
Signature · 1 line
2.40.1
PATCH v2 16/53 tools/nolibc: riscv: Fix up load/store instructions for rv32
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
When compile nolibc application for rv32, we got such errors:
nolibc/sysroot/riscv/include/arch.h:190: Error: unrecognized opcode ld a4,0(a3)' nolibc/sysroot/riscv/include/arch.h:194: Error: unrecognized opcode sd a3,%lo(_auxv)(a4)' nolibc/sysroot/riscv/include/arch.h:196: Error: unrecognized opcode `sd a2,%lo(environ)(a3)'
Refer to arch/riscv/include/asm/asm.h and add REG_L/REG_S macros here to let rv32 uses its own lw/sw instructions.
--- tools/include/nolibc/arch-riscv.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
tools/include/nolibc/arch-riscv.h +9 -5
@@ -33,9 +33,13 @@ struct sys_stat_struct {#if __riscv_xlen ==64 #define PTRLOG "3"#define SZREG "8" +#define REG_L "ld" +#define REG_S "sd"#elif __riscv_xlen ==32 #define PTRLOG "2"#define SZREG "4" +#define REG_L "lw" +#define REG_S "sw"#endif /* Syscalls for RISCV :
@@ -181,7 +185,7 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(void)".option norelax\n" "lla gp, __global_pointer$\n" ".option pop\n" -"lw a0, 0(sp)\n" /* argc (a0) was in the stack */ +REG_L " a0, 0(sp)\n" /* argc (a0) was in the stack */ "add a1, sp, " SZREG "\n" /* argv (a1) = sp */ "slli a2, a0, " PTRLOG "\n" /* envp (a2) = SZREG*argc ... */ "add a2, a2, " SZREG "\n" /* + SZREG (skip null) */
@@ -189,14 +193,14 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(void)"add a3, a2, zero\n" /* iterate a3 over envp to find auxv (after NULL) */ "0:\n" /* do { */ -"ld a4, 0(a3)\n" /* a4 = *a3; */ +REG_L " a4, 0(a3)\n" /* a4 = *a3; */ "add a3, a3, " SZREG "\n" /* a3 += sizeof(void*); */ "bne a4, zero, 0b\n" /* } while (a4); */ "lui a4, %hi(_auxv)\n" /* a4 = &_auxv (high bits) */ -"sd a3, %lo(_auxv)(a4)\n" /* store a3 into _auxv */ +REG_S " a3, %lo(_auxv)(a4)\n" /* store a3 into _auxv */ -"lui a3, %hi(environ)\n" /* a3 = &environ (high bits) */ -"sd a2,%lo(environ)(a3)\n" /* store envp(a2) into environ */ +"lui a3, %hi(environ)\n" /* a3 = &environ (high bits) */ +REG_S " a2,%lo(environ)(a3)\n" /* store envp(a2) into environ */ "andi sp,a1,-16\n" /* sp must be 16-byte aligned */ "call main\n" /* main() returns the status code, we'll exit with it. */ "li a7, 93\n" /* NR_exit == 93 */
Signature · 1 line
2.40.1
PATCH v2 17/53 tools/nolibc/unistd: add syscall()
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
syscall() is used by "normal" libcs to allow users to directly call syscalls.
By having the same syntax inside nolibc users can more easily write code that works with different libcs.
The macro logic is adapted from systemtaps STAP_PROBEV() macro that is released in the public domain / CC0.
--- tools/include/nolibc/unistd.h | 15 +++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 2 ++ 2 files changed, 17 insertions(+)
tools/include/nolibc/unistd.h +15 -0
@@ -56,6 +56,21 @@ int tcsetpgrp(int fd, pid_t pid)return ioctl (fd,TIOCSPGRP , &pid); } +#define _syscall (N , ...) \ +({ \ + long _ret = my_syscall##N(__VA_ARGS__); \ + if (_ret < 0) { \ + SET_ERRNO(-_ret); \ + _ret = -1; \ + } \ + _ret; \ +}) + +#define _syscall_narg (...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0) +#define __syscall_narg (_0, _1, _2, _3, _4, _5, _6,N , ...) N +#define _syscall_n (N , ...) _syscall(N, __VA_ARGS__) +#define syscall (...) _syscall_n(_syscall_narg(__VA_ARGS__), ##__VA_ARGS__) +/* make sure to include all global symbols */ #include "nolibc.h"
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
@@ -588,6 +588,8 @@ int run_syscall(int min, int max)CASE_TEST (waitpid_child);EXPECT_SYSER (1 ,waitpid (getpid (), &tmp,WNOHANG ),-1 ,ECHILD );break ;CASE_TEST (write_badf);EXPECT_SYSER (1 ,write (-1 , &tmp,1 ),-1 ,EBADF );break ;CASE_TEST (write_zero);EXPECT_SYSZR (1 ,write (1 , &tmp,0 ));break ; +CASE_TEST (syscall_noargs);EXPECT_SYSEQ (1 ,syscall (__NR_getpid),getpid ());break ; +CASE_TEST (syscall_args);EXPECT_SYSER (1 ,syscall (__NR_fstat,0 ,NULL ),-1 ,EFAULT );break ;case __LINE__:return ret;/* must be last */ /* note: do not set any defaults so as to permit holes above */
Signature · 1 line
2.40.1
PATCH v2 18/53 selftests/nolibc: syscall_args: use generic __NR_statx
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
Compiling nolibc-test.c for rv32 got such error:
tools/testing/selftests/nolibc/nolibc-test.c:599:57: error: ‘__NR_fstat’ undeclared (first use in this function)
599 | CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_fstat, 0, NULL), -1, EFAULT); break;
The generic include/uapi/asm-generic/unistd.h used by rv32 doesn't support __NR_fstat, use the more generic __NR_statx instead:
Running test 'syscall'
69 syscall_noargs = 1 [OK]
70 syscall_args = -1 EFAULT [OK]
__NR_statx has been added from v4.10:
commit a528d35e8bfc ("statx: Add a system call to make enhanced file info available")
It has been supported by all of the platforms since at least from v4.20.
--- tools/testing/selftests/nolibc/nolibc-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +1 -1
@@ -589,7 +589,7 @@ int run_syscall(int min, int max)CASE_TEST (write_badf);EXPECT_SYSER (1 ,write (-1 , &tmp,1 ),-1 ,EBADF );break ;CASE_TEST (write_zero);EXPECT_SYSZR (1 ,write (1 , &tmp,0 ));break ;CASE_TEST (syscall_noargs);EXPECT_SYSEQ (1 ,syscall (__NR_getpid),getpid ());break ; -CASE_TEST (syscall_args);EXPECT_SYSER (1 ,syscall (__NR_fstat,0 ,NULL ),-1 ,EFAULT );break ; +CASE_TEST (syscall_args);EXPECT_SYSER (1 ,syscall (__NR_statx,0 ,NULL ,0 ,0 ,NULL ),-1 ,EFAULT );break ;case __LINE__:return ret;/* must be last */ /* note: do not set any defaults so as to permit holes above */
Signature · 1 line
2.40.1
PATCH v2 19/53 selftests/nolibc: reduce syscalls during space padding
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
Previously each space character used for alignment during test execution was written in a single write() call.
This would make the output from strace fairly unreadable. Coalesce all spaces into a single call to write().
--- tools/testing/selftests/nolibc/nolibc-test.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +11 -4
@@ -111,19 +111,26 @@ const char *errorname(int err) } } +static void putcharn (char c,size_t n) +{ +char buf[64 ]; + +memset (buf, c, n); + buf[n] ='\0' ; +fputs (buf, stdout); +} +static int pad_spc (int llen,int cnt,const char *fmt, ...) {va_list args; -int len;int ret; -for (len =0 ; len < cnt - llen; len++) -putchar (' ' ); +putcharn (' ' , cnt - llen);va_start (args, fmt); ret =vfprintf (stdout, fmt, args);va_end (args); -return ret <0 ? ret : ret + len; +return ret <0 ? ret : ret + cnt - llen; }/* The tests below are intended to be used by the macroes, which evaluate
Signature · 1 line
2.40.1
PATCH v2 20/53 tools/nolibc: aarch64: add stackprotector support
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
--- tools/include/nolibc/arch-aarch64.h | 7 ++++++- tools/testing/selftests/nolibc/Makefile | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)
tools/include/nolibc/arch-aarch64.h +6 -1
@@ -172,10 +172,15 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); +#define __ARCH_SUPPORTS_STACK_PROTECTOR +/* startup code */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) +void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( +#ifdef NOLIBC_STACKPROTECTOR + "bl __stack_chk_init\n" /* initialize stack protector */ +#endif"ldr x0, [sp]\n" /* argc (x0) was in the stack */ "add x1, sp, 8\n" /* argv (x1) = sp */ "lsl x2, x0, 3\n" /* envp (x2) = 8*argc ... */
tools/testing/selftests/nolibc/Makefile +1 -0
@@ -82,6 +82,7 @@ CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_arm64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \ $(call cc-option,-fno-stack-protector) \
Signature · 1 line
2.40.1
PATCH v2 21/53 tools/nolibc: arm: add stackprotector support
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
--- tools/include/nolibc/arch-arm.h | 7 ++++++- tools/testing/selftests/nolibc/Makefile | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)
tools/include/nolibc/arch-arm.h +6 -1
@@ -199,10 +199,15 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); +#define __ARCH_SUPPORTS_STACK_PROTECTOR +/* startup code */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) +void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( +#ifdef NOLIBC_STACKPROTECTOR + "bl __stack_chk_init\n" /* initialize stack protector */ +#endif"pop {%r0}\n" /* argc was in the stack */ "mov %r1, %sp\n" /* argv = sp */
tools/testing/selftests/nolibc/Makefile +1 -0
@@ -83,6 +83,7 @@ CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_arm64 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_arm = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \ $(call cc-option,-fno-stack-protector) \
Signature · 1 line
2.40.1
PATCH v2 22/53 tools/nolibc: loongarch: add stackprotector support
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
--- tools/include/nolibc/arch-loongarch.h | 7 ++++++- tools/testing/selftests/nolibc/Makefile | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)
tools/include/nolibc/arch-loongarch.h +6 -1
@@ -149,6 +149,8 @@ char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); +#define __ARCH_SUPPORTS_STACK_PROTECTOR +#if __loongarch_grlen ==32 #define LONGLOG "2"#define SZREG "4"
@@ -170,9 +172,12 @@ const unsigned long *_auxv __attribute__((weak));#endif /* startup code */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) +void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( +#ifdef NOLIBC_STACKPROTECTOR + "bl __stack_chk_init\n" /* initialize stack protector */ +#endifREG_L " $a0, $sp, 0\n" /* argc (a0) was in the stack */ LONG_ADDI " $a1, $sp, " SZREG "\n" /* argv (a1) = sp + SZREG */ LONG_SLL " $a2, $a0, "LONGLOG"\n" /* envp (a2) = SZREG*argc ... */
tools/testing/selftests/nolibc/Makefile +1 -0
@@ -84,6 +84,7 @@ CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_arm64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_arm = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_loongarch = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \ $(call cc-option,-fno-stack-protector) \
Signature · 1 line
2.40.1
PATCH v2 23/53 tools/nolibc: mips: add stackprotector support
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
--- tools/include/nolibc/arch-mips.h | 8 +++++++- tools/testing/selftests/nolibc/Makefile | 1 + 2 files changed, 8 insertions(+), 1 deletion(-)
tools/include/nolibc/arch-mips.h +7 -1
@@ -179,14 +179,20 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); +#define __ARCH_SUPPORTS_STACK_PROTECTOR +/* startup code, note that it's called __start on MIPS */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))__start (void ) +void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))__start (void ) { __asm__volatile (/*".set nomips16\n"*/ ".set push\n" ".set noreorder\n" ".option pic0\n" +#ifdef NOLIBC_STACKPROTECTOR + "jal __stack_chk_init\n" /* initialize stack protector */ + "nop\n" /* delayed slot */ +#endif/*".ent __start\n"*/ /*"__start:\n"*/ "lw $a0,($sp)\n" /* argc was in the stack */
tools/testing/selftests/nolibc/Makefile +1 -0
@@ -84,6 +84,7 @@ CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_arm64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_arm = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_mips = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_loongarch = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \
Signature · 1 line
2.40.1
PATCH v2 24/53 tools/nolibc: riscv: add stackprotector support
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
--- tools/include/nolibc/arch-riscv.h | 7 ++++++- tools/testing/selftests/nolibc/Makefile | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)
tools/include/nolibc/arch-riscv.h +6 -1
@@ -177,14 +177,19 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); +#define __ARCH_SUPPORTS_STACK_PROTECTOR +/* startup code */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) +void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( ".option push\n" ".option norelax\n" "lla gp, __global_pointer$\n" ".option pop\n" +#ifdef NOLIBC_STACKPROTECTOR + "call __stack_chk_init\n" /* initialize stack protector */ +#endifREG_L " a0, 0(sp)\n" /* argc (a0) was in the stack */ "add a1, sp, " SZREG "\n" /* argv (a1) = sp */ "slli a2, a0, "PTRLOG"\n" /* envp (a2) = SZREG*argc ... */
tools/testing/selftests/nolibc/Makefile +1 -0
@@ -85,6 +85,7 @@ CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_arm64 = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_arm = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_mips = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_riscv = $(CFLAGS_STACKPROTECTOR) CFLAGS_STKP_loongarch = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \
Signature · 1 line
2.40.1
PATCH v2 25/53 tools/nolibc: fix typo pint -> point
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
--- tools/include/nolibc/arch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/arch.h +1 -1
@@ -7,7 +7,7 @@* the syscall declarations and the _start code definition. This is the only * global part. On all architectures the kernel puts everything in the stack * before jumping to _start just above us, without any return address (_start -* is not a function but an entry pint). So at the stack pointer we find argc. +* is not a function but an entry point). So at the stack pointer we find argc. * Then argv[] begins, and ends at the first NULL. Then we have envp which * starts and ends with a NULL as well. So envp=argv+argc+1. */
Signature · 1 line
2.40.1
PATCH v2 26/53 tools/nolibc: x86_64: disable stack protector for _start
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
This was forgotten in the original submission.
It is unknown why it worked for x86_64 on some compiler without this attribute.
--- tools/include/nolibc/arch-x86_64.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/arch-x86_64.h +1 -1
@@ -190,7 +190,7 @@ const unsigned long *_auxv __attribute__((weak));* 2) The deepest stack frame should be zero (the %rbp). * */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) +void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__ volatile (#ifdef NOLIBC_STACKPROTECTOR
Signature · 1 line
2.40.1
PATCH v2 27/53 tools/nolibc: ensure stack protector guard is never zero
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
The all-zero pattern is one of the more probable out-of-bound writes so add a special case to not accidentally accept it.
Also it enables the reliable detection of stack protector initialization during testing.
--- tools/include/nolibc/stackprotector.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/include/nolibc/stackprotector.h +3 -2
@@ -45,8 +45,9 @@ __attribute__((weak,no_stack_protector,section(".text.nolibc_stack_chk")))void __stack_chk_init (void ) {my_syscall3 (__NR_getrandom, &__stack_chk_guard,sizeof (__stack_chk_guard),0 ); -/* a bit more randomness in case getrandom() fails */ - __stack_chk_guard ^= (uintptr_t ) &__stack_chk_guard; +/* a bit more randomness in case getrandom() fails, ensure the guard is never 0 */ +if (__stack_chk_guard != (uintptr_t ) &__stack_chk_guard) + __stack_chk_guard ^= (uintptr_t ) &__stack_chk_guard; }#endif /* defined(NOLIBC_STACKPROTECTOR) */
Signature · 1 line
2.40.1
PATCH v2 28/53 tools/nolibc: add test for __stack_chk_guard initialization
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
--- tools/testing/selftests/nolibc/nolibc-test.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +8 -0
@@ -808,6 +808,14 @@ static int run_protection(int min, int max)return 0 ;#endif +#if defined(NOLIBC_STACKPROTECTOR ) +if (!__stack_chk_guard) { + llen +=printf ("__stack_chk_guard not initialized" ); +pad_spc (llen,64 ,"[FAIL]\n" ); +return 1 ; + } +#endif + pid =-1 ; pid =fork ();
Signature · 1 line
2.40.1
PATCH v2 29/53 tools/nolibc: reformat list of headers to be installed
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
This makes it easier to add and remove more entries in the future without creating spurious diff hunks.
--- tools/include/nolibc/Makefile | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
tools/include/nolibc/Makefile +16 -2
@@ -25,8 +25,22 @@ endif nolibc_arch := $(patsubst arm64,aarch64,$(ARCH)) arch_file := arch-$(nolibc_arch).h -all_files := ctype.h errno.h nolibc.h signal.h stackprotector.h std.h stdint.h \ - stdio.h stdlib.h string.h sys.h time.h types.h unistd.h +all_files := \ + ctype.h \ + errno.h \ + nolibc.h \ + signal.h \ + stackprotector.h \ + std.h \ + stdint.h \ + stdlib.h \ + string.h \ + sys.h \ + time.h \ + types.h \ + unistd.h \ + stdio.h \ + # install all headers needed to support a bare-metal compiler all: headers
Signature · 1 line
2.40.1
PATCH v2 30/53 tools/nolibc: add autodetection for stackprotector support
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
The stackprotector support in nolibc should be enabled iff it is also enabled in the compiler.
Use the preprocessor defines added by gcc and clang if stackprotector support is enable to automatically do so in nolibc.
This completely removes the need for any user-visible API.
To avoid inlining the lengthy preprocessor check into every user introduce a new header compiler.h that abstracts the logic away.
As the define NOLIBC_STACKPROTECTOR is now not user-relevant anymore prefix it with an underscore.
--- tools/include/nolibc/Makefile | 1 + tools/include/nolibc/arch-aarch64.h | 6 +++--- tools/include/nolibc/arch-arm.h | 6 +++--- tools/include/nolibc/arch-i386.h | 6 +++--- tools/include/nolibc/arch-loongarch.h | 6 +++--- tools/include/nolibc/arch-mips.h | 6 +++--- tools/include/nolibc/arch-riscv.h | 6 +++--- tools/include/nolibc/arch-x86_64.h | 6 +++--- tools/include/nolibc/compiler.h | 15 +++++++++++++++ tools/include/nolibc/stackprotector.h | 10 +++------- tools/testing/selftests/nolibc/nolibc-test.c | 4 ++-- 11 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 tools/include/nolibc/compiler.h
tools/include/nolibc/Makefile +1 -0
@@ -26,6 +26,7 @@ endif nolibc_arch := $(patsubst arm64,aarch64,$(ARCH)) arch_file := arch-$(nolibc_arch).h all_files := \ + compiler.h \ ctype.h \ errno.h \ nolibc.h \
tools/include/nolibc/arch-aarch64.h +3 -3
@@ -7,6 +7,8 @@#ifndef _NOLIBC_ARCH_AARCH64_H#define _NOLIBC_ARCH_AARCH64_H +#include "compiler.h" +/* The struct returned by the newfstatat() syscall. Differs slightly from the * x86_64's stat one by field ordering, so be careful. */
@@ -172,13 +174,11 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); -#define __ARCH_SUPPORTS_STACK_PROTECTOR -/* startup code */ void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( -#ifdef NOLIBC_STACKPROTECTOR +#ifdef _NOLIBC_STACKPROTECTOR "bl __stack_chk_init\n" /* initialize stack protector */ #endif "ldr x0, [sp]\n" /* argc (x0) was in the stack */
tools/include/nolibc/arch-arm.h +3 -3
@@ -7,6 +7,8 @@#ifndef _NOLIBC_ARCH_ARM_H#define _NOLIBC_ARCH_ARM_H +#include "compiler.h" +/* The struct returned by the stat() syscall, 32-bit only, the syscall returns * exactly 56 bytes (stops before the unused array). In big endian, the format * differs as devices are returned as short only.
@@ -199,13 +201,11 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); -#define __ARCH_SUPPORTS_STACK_PROTECTOR -/* startup code */ void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( -#ifdef NOLIBC_STACKPROTECTOR +#ifdef _NOLIBC_STACKPROTECTOR "bl __stack_chk_init\n" /* initialize stack protector */ #endif "pop {%r0}\n" /* argc was in the stack */
tools/include/nolibc/arch-i386.h +3 -3
@@ -7,6 +7,8 @@#ifndef _NOLIBC_ARCH_I386_H#define _NOLIBC_ARCH_I386_H +#include "compiler.h" +/* The struct returned by the stat() syscall, 32-bit only, the syscall returns * exactly 56 bytes (stops before the unused array). */
@@ -181,8 +183,6 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); -#define __ARCH_SUPPORTS_STACK_PROTECTOR -/* startup code */ /* * i386 System V ABI mandates:
@@ -193,7 +193,7 @@ const unsigned long *_auxv __attribute__((weak));void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( -#ifdef NOLIBC_STACKPROTECTOR +#ifdef _NOLIBC_STACKPROTECTOR "call __stack_chk_init\n" /* initialize stack protector */ #endif "pop %eax\n" /* argc (first arg, %eax) */
tools/include/nolibc/arch-loongarch.h +3 -3
@@ -7,6 +7,8 @@#ifndef _NOLIBC_ARCH_LOONGARCH_H#define _NOLIBC_ARCH_LOONGARCH_H +#include "compiler.h" +/* Syscalls for LoongArch : * - stack is 16-byte aligned * - syscall number is passed in a7
@@ -149,8 +151,6 @@ char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); -#define __ARCH_SUPPORTS_STACK_PROTECTOR -#if __loongarch_grlen ==32 #define LONGLOG "2"#define SZREG "4"
@@ -175,7 +175,7 @@ const unsigned long *_auxv __attribute__((weak));void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( -#ifdef NOLIBC_STACKPROTECTOR +#ifdef _NOLIBC_STACKPROTECTOR "bl __stack_chk_init\n" /* initialize stack protector */ #endif REG_L " $a0, $sp, 0\n" /* argc (a0) was in the stack */
tools/include/nolibc/arch-mips.h +3 -3
@@ -7,6 +7,8 @@#ifndef _NOLIBC_ARCH_MIPS_H#define _NOLIBC_ARCH_MIPS_H +#include "compiler.h" +/* The struct returned by the stat() syscall. 88 bytes are returned by the * syscall. */
@@ -179,8 +181,6 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); -#define __ARCH_SUPPORTS_STACK_PROTECTOR -/* startup code, note that it's called __start on MIPS */ void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))__start (void ) {
@@ -189,7 +189,7 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protec".set push\n" ".set noreorder\n" ".option pic0\n" -#ifdef NOLIBC_STACKPROTECTOR +#ifdef _NOLIBC_STACKPROTECTOR"jal __stack_chk_init\n" /* initialize stack protector */ "nop\n" /* delayed slot */ #endif
tools/include/nolibc/arch-riscv.h +3 -3
@@ -7,6 +7,8 @@ #ifndef _NOLIBC_ARCH_RISCV_H #define _NOLIBC_ARCH_RISCV_H +#include "compiler.h" + struct sys_stat_struct { unsigned long st_dev; /* Device. */ unsigned long st_ino; /* File serial number. */
@@ -177,8 +179,6 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); -#define __ARCH_SUPPORTS_STACK_PROTECTOR -/* startup code */ void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) {
@@ -187,7 +187,7 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protec ".option norelax\n" "lla gp, __global_pointer$\n" ".option pop\n" -#ifdef NOLIBC_STACKPROTECTOR +#ifdef _NOLIBC_STACKPROTECTOR "call __stack_chk_init\n" /* initialize stack protector */ #endifREG_L " a0, 0(sp)\n" /* argc (a0) was in the stack */
tools/include/nolibc/arch-x86_64.h +3 -3
@@ -7,6 +7,8 @@#ifndef _NOLIBC_ARCH_X86_64_H#define _NOLIBC_ARCH_X86_64_H +#include "compiler.h" +/* The struct returned by the stat() syscall, equivalent to stat64(). The * syscall returns 116 bytes and stops in the middle of __unused. */
@@ -181,8 +183,6 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); -#define __ARCH_SUPPORTS_STACK_PROTECTOR -/* startup code */ /* * x86-64 System V ABI mandates:
@@ -193,7 +193,7 @@ const unsigned long *_auxv __attribute__((weak));void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( -#ifdef NOLIBC_STACKPROTECTOR +#ifdef _NOLIBC_STACKPROTECTOR "call __stack_chk_init\n" /* initialize stack protector */ #endif "pop %rdi\n" /* argc (first arg, %rdi) */
tools/include/nolibc/compiler.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ +/* +* NOLIBC compiler support header +* Copyright (C) 2023 Thomas Weißschuh <[email protected]> +*/ +#ifndef _NOLIBC_COMPILER_H +#define _NOLIBC_COMPILER_H + +#if defined(__SSP__) || defined(__SSP_STRONG__) || defined(__SSP_ALL__) || defined(__SSP_EXPLICIT__) + +#define _NOLIBC_STACKPROTECTOR + +#endif /* defined(__SSP__) ... */ + +#endif /* _NOLIBC_COMPILER_H */
tools/include/nolibc/stackprotector.h +3 -7
@@ -7,13 +7,9 @@#ifndef _NOLIBC_STACKPROTECTOR_H#define _NOLIBC_STACKPROTECTOR_H -#include "arch.h" +#include "compiler.h" -#if defined(NOLIBC_STACKPROTECTOR ) - -#if !defined(__ARCH_SUPPORTS_STACK_PROTECTOR) -#error "nolibc does not support stack protectors on this arch" -#endif +#if defined(_NOLIBC_STACKPROTECTOR)#include "sys.h" #include "stdlib.h"
@@ -49,6 +45,6 @@ void __stack_chk_init(void)if (__stack_chk_guard != (uintptr_t ) &__stack_chk_guard) __stack_chk_guard ^= (uintptr_t ) &__stack_chk_guard; } -#endif /* defined(NOLIBC_STACKPROTECTOR) */ +#endif /* defined(_NOLIBC_STACKPROTECTOR) */ #endif /* _NOLIBC_STACKPROTECTOR_H */
tools/testing/selftests/nolibc/nolibc-test.c +2 -2
@@ -802,13 +802,13 @@ static int run_protection(int min, int max) llen +=printf ("0 -fstackprotector " ); -#if !defined(NOLIBC_STACKPROTECTOR ) +#if !defined(_NOLIBC_STACKPROTECTOR) llen +=printf ("not supported" );pad_spc (llen,64 ,"[SKIPPED]\n" );return 0 ;#endif -#if defined(NOLIBC_STACKPROTECTOR ) +#if defined(_NOLIBC_STACKPROTECTOR)if (!__stack_chk_guard) { llen +=printf ("__stack_chk_guard not initialized" );pad_spc (llen,64 ,"[FAIL]\n" );
Signature · 1 line
2.40.1
PATCH v2 31/53 tools/nolibc: simplify stackprotector compiler flags
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
Now that nolibc enable stackprotector support automatically when the compiler enables it we only have to get the -fstack-protector flags correct.
The cc-options are structured so that -fstack-protector-all is only enabled if -mstack-protector=guard works, as that is the only mode supported by nolibc.
--- tools/testing/selftests/nolibc/Makefile | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
tools/testing/selftests/nolibc/Makefile +2 -12
@@ -76,21 +76,11 @@ else Q=@ endif -CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ - $(call cc-option,-mstack-protector-guard=global) \ - $(call cc-option,-fstack-protector-all) -CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR) -CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR) -CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) -CFLAGS_STKP_arm64 = $(CFLAGS_STACKPROTECTOR) -CFLAGS_STKP_arm = $(CFLAGS_STACKPROTECTOR) -CFLAGS_STKP_mips = $(CFLAGS_STACKPROTECTOR) -CFLAGS_STKP_riscv = $(CFLAGS_STACKPROTECTOR) -CFLAGS_STKP_loongarch = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 +CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all)) CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \ $(call cc-option,-fno-stack-protector) \ - $(CFLAGS_STKP_$(ARCH)) $(CFLAGS_$(ARCH)) + $(CFLAGS_$(ARCH)) $(CFLAGS_STACKPROTECTOR) LDFLAGS := -s help:
Signature · 1 line
2.40.1
PATCH v2 32/53 tools/nolibc: fix segfaults on compilers without attribute no_stack_protector
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
Not all compilers, notably GCC < 10, have support for __attribute__((no_stack_protector)).
Fall back to a mechanism that also works there.
Tested with GCC 9.5.0 from kernel.org crosstools.
--- tools/include/nolibc/arch-aarch64.h | 2 +- tools/include/nolibc/arch-arm.h | 2 +- tools/include/nolibc/arch-i386.h | 2 +- tools/include/nolibc/arch-loongarch.h | 2 +- tools/include/nolibc/arch-mips.h | 2 +- tools/include/nolibc/arch-riscv.h | 2 +- tools/include/nolibc/arch-x86_64.h | 2 +- tools/include/nolibc/compiler.h | 10 ++++++++++ tools/include/nolibc/stackprotector.h | 2 +- 9 files changed, 18 insertions(+), 8 deletions(-)
tools/include/nolibc/arch-aarch64.h +1 -1
@@ -175,7 +175,7 @@ char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak));/* startup code */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__ volatile (#ifdef _NOLIBC_STACKPROTECTOR
tools/include/nolibc/arch-arm.h +1 -1
@@ -202,7 +202,7 @@ char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak));/* startup code */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__ volatile (#ifdef _NOLIBC_STACKPROTECTOR
tools/include/nolibc/arch-i386.h +1 -1
@@ -190,7 +190,7 @@ const unsigned long *_auxv __attribute__((weak));* 2) The deepest stack frame should be set to zero * */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__ volatile (#ifdef _NOLIBC_STACKPROTECTOR
tools/include/nolibc/arch-loongarch.h +1 -1
@@ -172,7 +172,7 @@ const unsigned long *_auxv __attribute__((weak));#endif /* startup code */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__ volatile (#ifdef _NOLIBC_STACKPROTECTOR
tools/include/nolibc/arch-mips.h +1 -1
@@ -182,7 +182,7 @@ char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak));/* startup code, note that it's called __start on MIPS */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))__start (void ) +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector __start(void) { __asm__ volatile (/*".set nomips16\n"*/
tools/include/nolibc/arch-riscv.h +1 -1
@@ -180,7 +180,7 @@ char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak));/* startup code */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__volatile ( ".option push\n"
tools/include/nolibc/arch-x86_64.h +1 -1
@@ -190,7 +190,7 @@ const unsigned long *_auxv __attribute__((weak));* 2) The deepest stack frame should be zero (the %rbp). * */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__ volatile (#ifdef _NOLIBC_STACKPROTECTOR
tools/include/nolibc/compiler.h +10 -0
@@ -12,4 +12,14 @@#endif /* defined(__SSP__) ... */ +#if defined(__has_attribute) +# if __has_attribute (no_stack_protector) +# define __no_stack_protector __attribute__((no_stack_protector)) +# else +# define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector"))) +# endif +#else +# define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector"))) +#endif /* defined(__has_attribute) */ +#endif /* _NOLIBC_COMPILER_H */
tools/include/nolibc/stackprotector.h +1 -1
@@ -37,7 +37,7 @@ void __stack_chk_fail_local(void) __attribute__((weak,section (".data.nolibc_stack_chk" )))uintptr_t __stack_chk_guard; -__attribute__((weak,no_stack_protector,section (".text.nolibc_stack_chk" ))) +__attribute__((weak,section (".text.nolibc_stack_chk" )))__no_stack_protector void __stack_chk_init(void) {my_syscall3 (__NR_getrandom, &__stack_chk_guard,sizeof (__stack_chk_guard),0 );
Signature · 1 line
2.40.1
PATCH v2 33/53 tools/nolibc: s390: disable stackprotector in _start
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
s390 does not support the "global" stack protector mode that is implemented in nolibc.
Now that nolibc detects if stack protectors are enabled at runtime it could happen that a future compiler does indeed use global mode on and nolibc would compile but segfault at runtime.
To avoid this hypothetic case and to align s390 with the other architectures disable stack protectors when compiling _start().
--- tools/include/nolibc/arch-s390.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/include/nolibc/arch-s390.h +3 -1
@@ -8,6 +8,8 @@#include <asm/signal.h> #include <asm/unistd.h> +#include "compiler.h" +/* The struct returned by the stat() syscall, equivalent to stat64(). The * syscall returns 116 bytes and stops in the middle of __unused. */
@@ -164,7 +166,7 @@ char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak));/* startup code */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__volatile ( "lg %r2,0(%r15)\n" /* argument count */
Signature · 1 line
2.40.1
PATCH v2 34/53 tools/nolibc: add support for prctl()
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
It will be used to disable core dumps from the child spawned to validate the stack protector functionality.
--- tools/include/nolibc/sys.h | 27 ++++++++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 2 ++ 2 files changed, 29 insertions(+)
tools/include/nolibc/sys.h +27 -0
@@ -22,6 +22,7 @@#include <linux/fcntl.h> /* for O_* and AT_* */ #include <linux/stat.h> /* for statx() */ #include <linux/reboot.h> /* for LINUX_REBOOT_* */ +#include <linux/prctl.h> #include "arch.h" #include "errno.h"
@@ -875,6 +876,32 @@ int open(const char *path, int flags, ...) } +/* +* int prctl(int option, unsigned long arg2, unsigned long arg3, +* unsigned long arg4, unsigned long arg5); +*/ + +static __attribute__((unused)) +int sys_prctl (int option,unsigned long arg2,unsigned long arg3, +unsigned long arg4,unsigned long arg5) +{ +return my_syscall5 (__NR_prctl, option, arg2, arg3, arg4, arg5); +} + +static __attribute__((unused)) +int prctl (int option,unsigned long arg2,unsigned long arg3, +unsigned long arg4,unsigned long arg5) +{ +int ret =sys_prctl (option, arg2, arg3, arg4, arg5); + +if (ret <0 ) { +SET_ERRNO (-ret); + ret =-1 ; + } +return ret; +} + +/* * int pivot_root(const char *new, const char *old); */
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
@@ -22,6 +22,7 @@#include <sys/ioctl.h> #include <sys/mman.h> #include <sys/mount.h> +#include <sys/prctl.h> #include <sys/reboot.h> #include <sys/stat.h> #include <sys/syscall.h>
@@ -580,6 +581,7 @@ int run_syscall(int min, int max)CASE_TEST (poll_null);EXPECT_SYSZR (1 ,poll (NULL ,0 ,0 ));break ;CASE_TEST (poll_stdout);EXPECT_SYSNE (1 , ({struct pollfd fds = {1 ,POLLOUT ,0 };poll (&fds,1 ,0 ); }),-1 );break ;CASE_TEST (poll_fault);EXPECT_SYSER (1 ,poll ((void *)1 ,1 ,0 ),-1 ,EFAULT );break ; +CASE_TEST (prctl);EXPECT_SYSER (1 ,prctl (PR_SET_NAME , (unsigned long )NULL ,0 ,0 ,0 ),-1 ,EFAULT );break ;CASE_TEST (read_badf);EXPECT_SYSER (1 ,read (-1 , &tmp,1 ),-1 ,EBADF );break ;CASE_TEST (sched_yield);EXPECT_SYSZR (1 ,sched_yield ());break ;CASE_TEST (select_null);EXPECT_SYSZR (1 , ({struct timeval tv = {0 };select (0 ,NULL ,NULL ,NULL , &tv); }));break ;
Signature · 1 line
2.40.1
PATCH v2 35/53 selftests/nolibc: prevent coredumps during test execution
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
The child process forked during stackprotector tests intentionally gets killed with SIGABRT. By default this will trigger writing a coredump. The writing of the coredump can spam the systems coredump machinery and take some time.
Timings for the full run of nolibc-test:
Before: 200ms
After: 20ms
This is on a desktop x86 system with systemd-coredumpd enabled.
--- tools/testing/selftests/nolibc/nolibc-test.c | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/nolibc/nolibc-test.c +1 -0
@@ -831,6 +831,7 @@ static int run_protection(int min, int max)close (STDOUT_FILENO );close (STDERR_FILENO ); +prctl (PR_SET_DUMPABLE ,0 ,0 ,0 ,0 );smash_stack ();return 1 ;
Signature · 1 line
2.40.1
PATCH v2 36/53 tools/nolibc: support nanoseconds in stat()
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
Keep backwards compatibility through unions.
The compatibility macros like
#define st_atime st_atim.tv_sec
as documented in stat(3type) don't work for nolibc because it would break with other stat-like structures that contain the field st_atime.
The stx_atime, stx_mtime, stx_ctime are in type of 'struct statx_timestamp', which is incompatible with 'struct timespec', should be converted explicitly.
/* include/uapi/linux/stat.h */
struct statx_timestamp {
__s64 tv_sec;
__u32 tv_nsec;
__s32 __reserved;
};
/* include/uapi/linux/time.h */
struct timespec {
__kernel_old_time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};--- tools/include/nolibc/sys.h | 66 +++++++++++--------- tools/include/nolibc/types.h | 6 +- tools/testing/selftests/nolibc/nolibc-test.c | 23 +++++++ 3 files changed, 62 insertions(+), 33 deletions(-)
tools/include/nolibc/sys.h +36 -30
@@ -1161,23 +1161,26 @@ int sys_stat(const char *path, struct stat *buf)long ret; ret =sys_statx (AT_FDCWD , path,AT_NO_AUTOMOUNT ,STATX_BASIC_STATS , &statx); - buf->st_dev = ((statx.stx_dev_minor &0xff ) - | (statx.stx_dev_major <<8 ) - | ((statx.stx_dev_minor & ~0xff ) <<12 )); - buf->st_ino = statx.stx_ino; - buf->st_mode = statx.stx_mode; - buf->st_nlink = statx.stx_nlink; - buf->st_uid = statx.stx_uid; - buf->st_gid = statx.stx_gid; - buf->st_rdev = ((statx.stx_rdev_minor &0xff ) - | (statx.stx_rdev_major <<8 ) - | ((statx.stx_rdev_minor & ~0xff ) <<12 )); - buf->st_size = statx.stx_size; - buf->st_blksize = statx.stx_blksize; - buf->st_blocks = statx.stx_blocks; - buf->st_atime = statx.stx_atime.tv_sec; - buf->st_mtime = statx.stx_mtime.tv_sec; - buf->st_ctime = statx.stx_ctime.tv_sec; + buf->st_dev = ((statx.stx_dev_minor &0xff ) + | (statx.stx_dev_major <<8 ) + | ((statx.stx_dev_minor & ~0xff ) <<12 )); + buf->st_ino = statx.stx_ino; + buf->st_mode = statx.stx_mode; + buf->st_nlink = statx.stx_nlink; + buf->st_uid = statx.stx_uid; + buf->st_gid = statx.stx_gid; + buf->st_rdev = ((statx.stx_rdev_minor &0xff ) + | (statx.stx_rdev_major <<8 ) + | ((statx.stx_rdev_minor & ~0xff ) <<12 )); + buf->st_size = statx.stx_size; + buf->st_blksize = statx.stx_blksize; + buf->st_blocks = statx.stx_blocks; + buf->st_atim.tv_sec = statx.stx_atime.tv_sec; + buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec; + buf->st_mtim.tv_sec = statx.stx_mtime.tv_sec; + buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec; + buf->st_ctim.tv_sec = statx.stx_ctime.tv_sec; + buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec; return ret; }#else
@@ -1195,19 +1198,22 @@ int sys_stat(const char *path, struct stat *buf)#else #error Neither __NR_newfstatat nor __NR_stat defined, cannot implement sys_stat()#endif - buf->st_dev = stat.st_dev; - buf->st_ino = stat.st_ino; - buf->st_mode = stat.st_mode; - buf->st_nlink = stat.st_nlink; - buf->st_uid = stat.st_uid; - buf->st_gid = stat.st_gid; - buf->st_rdev = stat.st_rdev; - buf->st_size = stat.st_size; - buf->st_blksize = stat.st_blksize; - buf->st_blocks = stat.st_blocks; - buf->st_atime = stat.st_atime; - buf->st_mtime = stat.st_mtime; - buf->st_ctime = stat.st_ctime; + buf->st_dev = stat.st_dev; + buf->st_ino = stat.st_ino; + buf->st_mode = stat.st_mode; + buf->st_nlink = stat.st_nlink; + buf->st_uid = stat.st_uid; + buf->st_gid = stat.st_gid; + buf->st_rdev = stat.st_rdev; + buf->st_size = stat.st_size; + buf->st_blksize = stat.st_blksize; + buf->st_blocks = stat.st_blocks; + buf->st_atim.tv_sec = stat.st_atime; + buf->st_atim.tv_nsec = stat.st_atime_nsec; + buf->st_mtim.tv_sec = stat.st_mtime; + buf->st_mtim.tv_nsec = stat.st_mtime_nsec; + buf->st_ctim.tv_sec = stat.st_ctime; + buf->st_ctim.tv_nsec = stat.st_ctime_nsec; return ret; }#endif
tools/include/nolibc/types.h +3 -3
@@ -198,9 +198,9 @@ struct stat {off_t st_size;/* total size, in bytes */ blksize_t st_blksize;/* blocksize for file system I/O */ blkcnt_t st_blocks;/* number of 512B blocks allocated */ -time_t st_atime;/* time of last access */ -time_t st_mtime;/* time of last modification */ -time_t st_ctime;/* time of last status change */ +union {time_t st_atime;struct timespec st_atim; };/* time of last access */ +union {time_t st_mtime;struct timespec st_mtim; };/* time of last modification */ +union {time_t st_ctime;struct timespec st_ctim; };/* time of last status change */ };/* WARNING, it only deals with the 4096 first majors and 256 first minors */
tools/testing/selftests/nolibc/nolibc-test.c +23 -0
@@ -501,6 +501,28 @@ static int test_fork(void) } } +static int test_stat_timestamps (void ) +{ +struct stat st; + +if (sizeof (st.st_atim.tv_sec) !=sizeof (st.st_atime)) +return 1 ; + +if (stat ("/proc/self/" , &st)) +return 1 ; + +if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec >1000000000 ) +return 1 ; + +if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec >1000000000 ) +return 1 ; + +if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec >1000000000 ) +return 1 ; + +return 0 ; +} +/* Run syscall tests between IDs <min> and <max>. * Return 0 on success, non-zero on failure. */
@@ -589,6 +611,7 @@ int run_syscall(int min, int max)CASE_TEST (select_fault);EXPECT_SYSER (1 ,select (1 , (void *)1 ,NULL ,NULL ,0 ),-1 ,EFAULT );break ;CASE_TEST (stat_blah);EXPECT_SYSER (1 ,stat ("/proc/self/blah" , &stat_buf),-1 ,ENOENT );break ;CASE_TEST (stat_fault);EXPECT_SYSER (1 ,stat (NULL , &stat_buf),-1 ,EFAULT );break ; +CASE_TEST (stat_timestamps);EXPECT_SYSZR (1 ,test_stat_timestamps ());break ;CASE_TEST (symlink_root);EXPECT_SYSER (1 ,symlink ("/" ,"/" ),-1 ,EEXIST );break ;CASE_TEST (unlink_root);EXPECT_SYSER (1 ,unlink ("/" ),-1 ,EISDIR );break ;CASE_TEST (unlink_blah);EXPECT_SYSER (1 ,unlink ("/proc/self/blah" ),-1 ,ENOENT );break ;
Signature · 1 line
2.40.1
PATCH v2 37/53 selftests/nolibc: print name instead of number for EOVERFLOW
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
EOVERFLOW will be used in the coming time64 syscalls support.
--- tools/testing/selftests/nolibc/nolibc-test.c | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/nolibc/nolibc-test.c +1 -0
@@ -107,6 +107,7 @@ const char *errorname(int err)CASE_ERR (EDOM );CASE_ERR (ERANGE );CASE_ERR (ENOSYS ); +CASE_ERR (EOVERFLOW );default :return itoa (err); }
Signature · 1 line
2.40.1
PATCH v2 38/53 selftests/nolibc: remove the duplicated gettimeofday_bad2
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
There were two exactly similar occurrences of this test.
--- tools/testing/selftests/nolibc/nolibc-test.c | 1 - 1 file changed, 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +0 -1
@@ -587,7 +587,6 @@ int run_syscall(int min, int max)#ifdef NOLIBC CASE_TEST (gettimeofday_bad1);EXPECT_SYSER (1 ,gettimeofday ((void *)1 ,NULL ),-1 ,EFAULT );break ;CASE_TEST (gettimeofday_bad2);EXPECT_SYSER (1 ,gettimeofday (NULL , (void *)1 ),-1 ,EFAULT );break ; -CASE_TEST (gettimeofday_bad2);EXPECT_SYSER (1 ,gettimeofday (NULL , (void *)1 ),-1 ,EFAULT );break ;#endif CASE_TEST (getpagesize);EXPECT_SYSZR (1 ,test_getpagesize ());break ;CASE_TEST (ioctl_tiocinq);EXPECT_SYSZR (1 ,ioctl (0 ,TIOCINQ , &tmp));break ;
Signature · 1 line
2.40.1
PATCH v2 39/53 tools/nolibc: ppoll/ppoll_time64: add a missing argument
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
The ppoll and ppoll_time64 syscalls have 5 arguments, but we only provide 4, align with kernel and add the missing sigsetsize argument.
Because the sigmask is NULL, the last sigsetsize argument is ignored, keep it as 0 here is safe enough.
--- tools/include/nolibc/sys.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/sys.h +1 -1
@@ -939,7 +939,7 @@ int sys_poll(struct pollfd *fds, int nfds, int timeout) t.tv_sec = timeout /1000 ; t.tv_nsec = (timeout % 1000) * 1000000; } -return my_syscall4 (__NR_ppoll, fds, nfds, (timeout >=0 ) ? &t :NULL ,NULL ); +return my_syscall5 (__NR_ppoll, fds, nfds, (timeout >=0 ) ? &t :NULL ,NULL ,0 );#elif defined(__NR_poll)return my_syscall3 (__NR_poll, fds, nfds, timeout);#else
Signature · 1 line
2.40.1
PATCH v2 40/53 selftests/nolibc: test_fork: fix up duplicated print
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
running nolibc-test with glibc on x86_64 got such print issue:
29 execve_root = -1 EACCES [OK]
30 fork30 fork = 0 [OK]
31 getdents64_root = 712 [OK]
The fork test case has three printf calls:
(1) llen += printf("%d %s", test, #name);
(2) llen += printf(" = %d %s ", expr, errorname(errno));
(3) llen += pad_spc(llen, 64, "[FAIL]\n"); --> vfprintf()In the following scene, the above issue happens:
(a) The parent calls (1)
(b) The parent calls fork()
(c) The child runs and shares the print buffer of (1)
(d) The child exits, flushs the print buffer and closes its own stdout/stderr
* "30 fork" is printed at the first time.
(e) The parent calls (2) and (3), with "\n" in (3), it flushs the whole buffer
* "30 fork = 0 ..." is printed
Therefore, there are two "30 fork" in the stdout.
Between (a) and (b), if flush the stdout (and the sterr), the child in stage (c) will not be able to 'see' the print buffer.
--- tools/testing/selftests/nolibc/nolibc-test.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +7 -1
@@ -486,7 +486,13 @@ static int test_getpagesize(void)static int test_fork (void ) {int status; -pid_t pid =fork (); +pid_t pid; + +/* flush the printf buffer to avoid child flush it */ +fflush (stdout); +fflush (stderr); + + pid =fork ();switch (pid) {case -1 :
Signature · 1 line
2.40.1
PATCH v2 41/53 tools/nolibc: ensure fast64 integer types have 64 bits
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
On 32bit platforms size_t is not enough to represent [u]int_fast64_t.
--- tools/include/nolibc/stdint.h | 10 +++++----- tools/testing/selftests/nolibc/nolibc-test.c | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-)
tools/include/nolibc/stdint.h +5 -5
@@ -36,8 +36,8 @@ typedef ssize_t int_fast16_t;typedef size_t uint_fast16_t ;typedef ssize_t int_fast32_t ;typedef size_t uint_fast32_t ; -typedef ssize_t int_fast64_t ; -typedef size_t uint_fast64_t ; +typedef int64_t int_fast64_t ; +typedef uint64_t uint_fast64_t ;typedef int64_t intmax_t ;typedef uint64_t uintmax_t ;
@@ -84,16 +84,16 @@ typedef uint64_t uintmax_t;#define INT_FAST8_MIN INT8_MIN#define INT_FAST16_MIN INTPTR_MIN#define INT_FAST32_MIN INTPTR_MIN -#define INT_FAST64_MIN INTPTR_MIN +#define INT_FAST64_MIN INT64_MIN#define INT_FAST8_MAX INT8_MAX#define INT_FAST16_MAX INTPTR_MAX#define INT_FAST32_MAX INTPTR_MAX -#define INT_FAST64_MAX INTPTR_MAX +#define INT_FAST64_MAX INT64_MAX#define UINT_FAST8_MAX UINT8_MAX#define UINT_FAST16_MAX SIZE_MAX#define UINT_FAST32_MAX SIZE_MAX -#define UINT_FAST64_MAX SIZE_MAX +#define UINT_FAST64_MAX UINT64_MAX#endif /* _NOLIBC_STDINT_H */
tools/testing/selftests/nolibc/nolibc-test.c +3 -3
@@ -702,9 +702,9 @@ int run_stdlib(int min, int max)CASE_TEST (limit_int_fast32_min);EXPECT_EQ (1 ,INT_FAST32_MIN , (int_fast32_t )INTPTR_MIN );break ;CASE_TEST (limit_int_fast32_max);EXPECT_EQ (1 ,INT_FAST32_MAX , (int_fast32_t )INTPTR_MAX );break ;CASE_TEST (limit_uint_fast32_max);EXPECT_EQ (1 ,UINT_FAST32_MAX , (uint_fast32_t )UINTPTR_MAX );break ; -CASE_TEST (limit_int_fast64_min);EXPECT_EQ (1 ,INT_FAST64_MIN , (int_fast64_t )INTPTR_MIN );break ; -CASE_TEST (limit_int_fast64_max);EXPECT_EQ (1 ,INT_FAST64_MAX , (int_fast64_t )INTPTR_MAX );break ; -CASE_TEST (limit_uint_fast64_max);EXPECT_EQ (1 ,UINT_FAST64_MAX , (uint_fast64_t )UINTPTR_MAX );break ; +CASE_TEST (limit_int_fast64_min);EXPECT_EQ (1 ,INT_FAST64_MIN , (int_fast64_t )INT64_MIN );break ; +CASE_TEST (limit_int_fast64_max);EXPECT_EQ (1 ,INT_FAST64_MAX , (int_fast64_t )INT64_MAX );break ; +CASE_TEST (limit_uint_fast64_max);EXPECT_EQ (1 ,UINT_FAST64_MAX , (uint_fast64_t )UINT64_MAX );break ;#if __SIZEOF_LONG__ ==8 CASE_TEST (limit_intptr_min);EXPECT_EQ (1 ,INTPTR_MIN , (intptr_t )0x8000000000000000LL );break ;CASE_TEST (limit_intptr_max);EXPECT_EQ (1 ,INTPTR_MAX , (intptr_t )0x7fffffffffffffffLL );break ;
Signature · 1 line
2.40.1
PATCH v2 42/53 selftests/nolibc: remove test gettimeofday_null
In reply to Paul E. McKenneyFrom: Thomas Weißschuh <[email protected]>
gettimeofday() is not guaranteed by posix to handle a NULL value as first argument gracefully.
On glibc for example it crashes. (When not going through the vdso)
--- tools/testing/selftests/nolibc/nolibc-test.c | 1 - 1 file changed, 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +0 -1
@@ -589,7 +589,6 @@ int run_syscall(int min, int max)CASE_TEST (fork);EXPECT_SYSZR (1 ,test_fork ());break ;CASE_TEST (getdents64_root);EXPECT_SYSNE (1 ,test_getdents64 ("/" ),-1 );break ;CASE_TEST (getdents64_null);EXPECT_SYSER (1 ,test_getdents64 ("/dev/null" ),-1 ,ENOTDIR );break ; -CASE_TEST (gettimeofday_null);EXPECT_SYSZR (1 ,gettimeofday (NULL ,NULL ));break ;#ifdef NOLIBC CASE_TEST (gettimeofday_bad1);EXPECT_SYSER (1 ,gettimeofday ((void *)1 ,NULL ),-1 ,EFAULT );break ;CASE_TEST (gettimeofday_bad2);EXPECT_SYSER (1 ,gettimeofday (NULL , (void *)1 ),-1 ,EFAULT );break ;
Signature · 1 line
2.40.1
PATCH v2 43/53 selftests/nolibc: allow specify extra arguments for qemu
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
The opensbi package from Ubuntu 20.04 only provides rv64 firmwares:
$ dpkg -S opensbi | grep -E "fw_.*bin|fw_.*elf" | uniq
opensbi: /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_dynamic.bin
opensbi: /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.bin
opensbi: /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_dynamic.elf
opensbi: /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elfTo run this nolibc test for rv32, users must build opensbi or download a prebuilt one from qemu repository:
https://gitlab.com/qemu-project/qemu/-/blob/master/pc-bios/opensbi-riscv32-generic-fw_dynamic.bin
And then use -bios to tell qemu use it to avoid such failure:
$ qemu-system-riscv32 -display none -no-reboot -kernel /path/to/arch/riscv/boot/Image -serial stdio -M virt -append "console=ttyS0 panic=-1"
qemu-system-riscv32: Unable to load the RISC-V firmware "opensbi-riscv32-generic-fw_dynamic.bin"To run from makefile, QEMU_ARGS_EXTRA is added to allow pass extra arguments like -bios:
$ make run QEMU_ARGS_EXTRA="-bios /path/to/opensbi-riscv32-generic-fw_dynamic.bin" ...
--- tools/testing/selftests/nolibc/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/nolibc/Makefile +1 -1
@@ -64,7 +64,7 @@ QEMU_ARGS_mips = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" QEMU_ARGS_riscv = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" QEMU_ARGS_s390 = -M s390-ccw-virtio -m 1G -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" QEMU_ARGS_loongarch = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)" -QEMU_ARGS = $(QEMU_ARGS_$(ARCH)) +QEMU_ARGS = $(QEMU_ARGS_$(ARCH)) $(QEMU_ARGS_EXTRA) # OUTPUT is only set when run from the main makefile, otherwise # it defaults to this nolibc directory.
Signature · 1 line
2.40.1
PATCH v2 44/53 selftests/nolibc: fix up compile warning with glibc on x86_64
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
Compiling nolibc-test.c with gcc on x86_64 got such warning:
tools/testing/selftests/nolibc/nolibc-test.c: In function ‘expect_eq’:tools/testing/selftests/nolibc/nolibc-test.c:177:24: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘uint64_t’ {aka ‘long unsigned int’} [-Wformat=]
177 | llen += printf(" = %lld ", expr);
| ~~~^ ~~~~
| | |
| | uint64_t {aka long unsigned int}
| long long int
| %ldIt because that glibc defines uint64_t as "unsigned long int" when word size (means sizeof(long)) is 64bit (see include/bits/types.h), but nolibc directly use the 64bit "unsigned long long" (see tools/include/nolibc/stdint.h), which is simpler, seems kernel uses it too (include/uapi/asm-generic/int-ll64.h).
use a simple conversion to solve it.
--- tools/testing/selftests/nolibc/nolibc-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +1 -1
@@ -174,7 +174,7 @@ static int expect_eq(uint64_t expr, int llen, uint64_t val) {int ret = !(expr == val); - llen +=printf (" = %lld " , expr); + llen +=printf (" = %lld " , (long long )expr);pad_spc (llen,64 , ret ?"[FAIL]\n" :" [OK]\n" );return ret; }
Signature · 1 line
2.40.1
PATCH v2 45/53 selftests/nolibc: not include limits.h for nolibc
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
When compile nolibc-test.c with 2.31 glibc, we got such error:
In file included from /usr/riscv64-linux-gnu/include/sys/cdefs.h:452,
from /usr/riscv64-linux-gnu/include/features.h:461,
from /usr/riscv64-linux-gnu/include/bits/libc-header-start.h:33,
from /usr/riscv64-linux-gnu/include/limits.h:26,
from /usr/lib/gcc-cross/riscv64-linux-gnu/9/include/limits.h:194,
from /usr/lib/gcc-cross/riscv64-linux-gnu/9/include/syslimits.h:7,
from /usr/lib/gcc-cross/riscv64-linux-gnu/9/include/limits.h:34,
from /labs/linux-lab/src/linux-stable/tools/testing/selftests/nolibc/nolibc-test.c:6:
/usr/riscv64-linux-gnu/include/bits/wordsize.h:28:3: error: #error "rv32i-based targets are not supported"
28 | # error "rv32i-based targets are not supported"Glibc (>= 2.33) commit 5b6113d62efa ("RISC-V: Support the 32-bit ABI implementation") fixed up above error.
As suggested by Thomas, defining INT_MIN/INT_MAX for nolibc can remove the including of limits.h, and therefore no above error. of course, the other libcs still require limits.h, move it to the right place.
The LONG_MIN/LONG_MAX are also defined too.
--- tools/include/nolibc/stdint.h | 14 ++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 4 +--- 2 files changed, 15 insertions(+), 3 deletions(-)
tools/include/nolibc/stdint.h +14 -0
@@ -96,4 +96,18 @@ typedef uint64_t uintmax_t;#define UINT_FAST32_MAX SIZE_MAX#define UINT_FAST64_MAX UINT64_MAX +#ifndef INT_MIN +#define INT_MIN (-__INT_MAX__ - 1) +#endif +#ifndef INT_MAX +#define INT_MAX __INT_MAX__ +#endif + +#ifndef LONG_MIN +#define LONG_MIN (-__LONG_MAX__ - 1) +#endif +#ifndef LONG_MAX +#define LONG_MAX __LONG_MAX__ +#endif +#endif /* _NOLIBC_STDINT_H */
tools/testing/selftests/nolibc/nolibc-test.c +1 -3
@@ -2,9 +2,6 @@#define _GNU_SOURCE -/* platform-specific include files coming from the compiler */ -#include <limits.h> -/* libc-specific include files * The program may be built in 3 ways: * $(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined
@@ -39,6 +36,7 @@#include <stddef.h> #include <stdint.h> #include <unistd.h> +#include <limits.h> #endif #endif
Signature · 1 line
2.40.1
PATCH v2 46/53 selftests/nolibc: use INT_MAX instead of __INT_MAX__
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
nolibc now has INT_MAX in stdint.h, so, don't mix INT_MAX and __INT_MAX__, unify them to INT_MAX.
--- tools/testing/selftests/nolibc/nolibc-test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +2 -2
@@ -936,7 +936,7 @@ static const struct test test_names[] = {int main (int argc,char **argv,char **envp) {int min =0 ; -int max = __INT_MAX__; +int max =INT_MAX ;int ret =0 ;int err;int idx;
@@ -984,7 +984,7 @@ int main(int argc, char **argv, char **envp)* here, which defaults to the full range. */ do { - min = 0; max = __INT_MAX__; + min = 0; max = INT_MAX; value = colon; if (value && *value) { colon = strchr(value, ':');
Signature · 1 line
2.40.1
PATCH v2 47/53 tools/nolibc: arm: add missing my_syscall6
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
This is required by the coming removal of the oldselect and newselect support.
pselect6/pselect6_time64 will be used unconditionally, they have 6 arguments.
--- tools/include/nolibc/arch-arm.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
tools/include/nolibc/arch-arm.h +23 -0
@@ -198,6 +198,29 @@ struct sys_stat_struct { _arg1; \ }) +#define my_syscall6 (num, arg1, arg2, arg3, arg4, arg5, arg6) \ +({ \ + register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \ + register long _arg1 __asm__ ("r0") = (long)(arg1); \ + register long _arg2 __asm__ ("r1") = (long)(arg2); \ + register long _arg3 __asm__ ("r2") = (long)(arg3); \ + register long _arg4 __asm__ ("r3") = (long)(arg4); \ + register long _arg5 __asm__ ("r4") = (long)(arg5); \ + register long _arg6 __asm__ ("r5") = (long)(arg6); \ + \ + __asm__ volatile ( \ + _NOLIBC_THUMB_SET_R7 \ + "svc #0\n" \ + _NOLIBC_THUMB_RESTORE_R7 \ + : "=r"(_arg1), "=r" (_num) \ + : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ + "r"(_arg6), "r"(_num) \ + : "memory", "cc", "lr" \ + ); \ + _arg1; \ +}) + + char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak));
Signature · 1 line
2.40.1
PATCH v2 48/53 tools/nolibc: open: fix up compile warning for arm
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
In function ‘open’:
nolibc/sysroot/arm/include/sys.h:919:23: warning: ‘mode_t’ {aka ‘short unsigned int’} is promoted to ‘int’ when passed through ‘...’
919 | mode = va_arg(args, mode_t);
| ^
nolibc/sysroot/arm/include/sys.h:919:23: note: (so you should pass ‘int’ not ‘mode_t’ {aka ‘short unsigned int’} to ‘va_arg’) nolibc/sysroot/arm/include/sys.h:919:23: note: if this code is reached, the program will abort
--- tools/include/nolibc/sys.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/sys.h +1 -1
@@ -862,7 +862,7 @@ int open(const char *path, int flags, ...)va_list args;va_start (args, flags); - mode =va_arg (args, mode_t); + mode =va_arg (args, int);va_end (args); }
Signature · 1 line
2.40.1
PATCH v2 49/53 selftests/nolibc: support two errnos with EXPECT_SYSER2()
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
Some functions may be implemented with different syscalls in different platforms, these syscalls may set different errnos for the same arguments, let's support such cases.
--- tools/testing/selftests/nolibc/nolibc-test.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +10 -4
@@ -300,18 +300,24 @@ static int expect_sysne(int expr, int llen, int val) } +#define EXPECT_SYSER2 (cond, expr, expret, experr1, experr2) \ + do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syserr2(expr, expret, experr1, experr2, llen); } while (0) +#define EXPECT_SYSER (cond, expr, expret, experr) \ - do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syserr(expr, expret, experr, llen); } while (0) + EXPECT_SYSER2(cond, expr, expret, experr, 0) -static int expect_syserr (int expr,int expret,int experr,int llen) +static int expect_syserr2 (int expr,int expret,int experr1,int experr2,int llen) {int ret =0 ;int _errno = errno; llen +=printf (" = %d %s " , expr,errorname (_errno)); -if (expr != expret || _errno != experr) { +if (expr != expret || (_errno != experr1 && _errno != experr2)) { ret =1 ; - llen +=printf (" != (%d %s) " , expret,errorname (experr)); +if (experr2 ==0 ) + llen +=printf (" != (%d %s) " , expret,errorname (experr1)); +else + llen +=printf (" != (%d %s %s) " , expret,errorname (experr1),errorname (experr2)); llen +=pad_spc (llen,64 ,"[FAIL]\n" ); }else { llen +=pad_spc (llen,64 ," [OK]\n" );
Signature · 1 line
2.40.1
PATCH v2 50/53 selftests/nolibc: remove gettimeofday_bad1/2 completely
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
In the clock_gettime / clock_gettime64 syscalls based gettimeofday(), there is no way to let kernel space 'fixup' the invalid data pointer of 'struct timeval' and 'struct timezone' for us for we need to read timespec from kernel space and then convert to timeval in user-space ourselves and also we need to simply ignore and reset timezone in user-space.
Without this removal, the invalid (void *)1 address will trigger a sigsegv (signum = 11) signal and stop the whole test.
--- tools/testing/selftests/nolibc/nolibc-test.c | 4 ---- 1 file changed, 4 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +0 -4
@@ -593,10 +593,6 @@ int run_syscall(int min, int max)CASE_TEST (fork);EXPECT_SYSZR (1 ,test_fork ());break ;CASE_TEST (getdents64_root);EXPECT_SYSNE (1 ,test_getdents64 ("/" ),-1 );break ;CASE_TEST (getdents64_null);EXPECT_SYSER (1 ,test_getdents64 ("/dev/null" ),-1 ,ENOTDIR );break ; -#ifdef NOLIBC -CASE_TEST (gettimeofday_bad1);EXPECT_SYSER (1 ,gettimeofday ((void *)1 ,NULL ),-1 ,EFAULT );break ; -CASE_TEST (gettimeofday_bad2);EXPECT_SYSER (1 ,gettimeofday (NULL , (void *)1 ),-1 ,EFAULT );break ; -#endif CASE_TEST (getpagesize);EXPECT_SYSZR (1 ,test_getpagesize ());break ;CASE_TEST (ioctl_tiocinq);EXPECT_SYSZR (1 ,ioctl (0 ,TIOCINQ , &tmp));break ;CASE_TEST (ioctl_tiocinq);EXPECT_SYSZR (1 ,ioctl (0 ,TIOCINQ , &tmp));break ;
Signature · 1 line
2.40.1
PATCH v2 51/53 selftests/nolibc: add new gettimeofday test cases
In reply to Paul E. McKenneyFrom: Zhangjin Wu <[email protected]>
These 2 test cases are added to cover the normal using scenes of gettimeofday().
They have been used to trigger and fix up such issue with nolibc:
nolibc-test.c:(.text.gettimeofday+0x54): undefined reference to `__aeabi_ldivmod'
This issue happens while there is no "unsigned int" conversion in the coming new clock_gettime / clock_gettime64 syscall path of gettimeofday():
tv->tv_usec = ts.tv_nsec / 1000;
--- tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +4 -0
@@ -539,6 +539,8 @@ static int test_stat_timestamps(void)*/ int run_syscall (int min,int max) { +struct timeval tv; +struct timezone tz;struct stat stat_buf;int euid0;int proc;
@@ -593,6 +595,8 @@ int run_syscall(int min, int max)CASE_TEST (fork);EXPECT_SYSZR (1 ,test_fork ());break ;CASE_TEST (getdents64_root);EXPECT_SYSNE (1 ,test_getdents64 ("/" ),-1 );break ;CASE_TEST (getdents64_null);EXPECT_SYSER (1 ,test_getdents64 ("/dev/null" ),-1 ,ENOTDIR );break ; +CASE_TEST (gettimeofday_tv);EXPECT_SYSZR (1 ,gettimeofday (&tv,NULL ));break ; +CASE_TEST (gettimeofday_tv_tz);EXPECT_SYSZR (1 ,gettimeofday (&tv, &tz));break ;CASE_TEST (getpagesize);EXPECT_SYSZR (1 ,test_getpagesize ());break ;CASE_TEST (ioctl_tiocinq);EXPECT_SYSZR (1 ,ioctl (0 ,TIOCINQ , &tmp));break ;CASE_TEST (ioctl_tiocinq);EXPECT_SYSZR (1 ,ioctl (0 ,TIOCINQ , &tmp));break ;
Signature · 1 line
2.40.1
PATCH v2 52/53 selftests/nolibc: also count skipped and failed tests in output
In reply to Paul E. McKenneyFrom: Willy Tarreau <[email protected]>
Right now skipped and failed test counts are not reported, and a few times already we missed skipped ones that ought not to. Let's now count each category and continue to invite the user to check the report file when skipped+fail > 0. E.g:
$ make run-user
(...)
CC nolibc-test
136 test(s) passed, 2 skipped, 0 failed. See all results in .../run.outNote that it's important to be careful about the trailing \r on the qemu output (thanks Zhangjin for noticing).
--- tools/testing/selftests/nolibc/Makefile | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
tools/testing/selftests/nolibc/Makefile +12 -3
@@ -130,7 +130,10 @@ libc-test: nolibc-test.c # qemu user-land test run-user: nolibc-test $(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || : - $(Q)grep -w FAIL "$(CURDIR)/run.out" && echo "See all results in $(CURDIR)/run.out" || echo "$$(grep -c ^[0-9].*OK $(CURDIR)/run.out) test(s) passed." + $(Q)awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{f++} /\[SKIPPED\][\r]*$$/{s++} \ + END{ printf("%d test(s) passed, %d skipped, %d failed.", p, s, f); \ + if (s+f > 0) printf(" See all results in %s\n", ARGV[1]); else print; }' \ + $(CURDIR)/run.out initramfs: nolibc-test $(QUIET_MKDIR)mkdir -p initramfs
@@ -146,12 +149,18 @@ kernel: initramfs # run the tests after building the kernel run: kernel $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" - $(Q)grep -w FAIL "$(CURDIR)/run.out" && echo "See all results in $(CURDIR)/run.out" || echo "$$(grep -c ^[0-9].*OK $(CURDIR)/run.out) test(s) passed." + $(Q)awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{f++} /\[SKIPPED\][\r]*$$/{s++} \ + END{ printf("%d test(s) passed, %d skipped, %d failed.", p, s, f); \ + if (s+f > 0) printf(" See all results in %s\n", ARGV[1]); else print; }' \ + $(CURDIR)/run.out # re-run the tests from an existing kernel rerun: $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" - $(Q)grep -w FAIL "$(CURDIR)/run.out" && echo "See all results in $(CURDIR)/run.out" || echo "$$(grep -c ^[0-9].*OK $(CURDIR)/run.out) test(s) passed." + $(Q)awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{f++} /\[SKIPPED\][\r]*$$/{s++} \ + END{ printf("%d test(s) passed, %d skipped, %d failed.", p, s, f); \ + if (s+f > 0) printf(" See all results in %s\n", ARGV[1]); else print; }' \ + $(CURDIR)/run.out clean: $(call QUIET_CLEAN, sysroot)
Signature · 1 line
2.40.1
PATCH v2 53/53 selftests/nolibc: make sure gcc always use little endian on MIPS
In reply to Paul E. McKenneyFrom: Willy Tarreau <[email protected]>
The test on MIPS stopped working after I upgraded some of my toolchains to use the ones from kernel.org because the mips toolchain defaults to big endian, even though it supports both endians. Let's just add an explicit -EL to make sure it always succeeds like the kernel does.
--- tools/testing/selftests/nolibc/Makefile | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/nolibc/Makefile +1 -0
@@ -77,6 +77,7 @@ Q=@ endif CFLAGS_s390 = -m64 +CFLAGS_mips = -EL CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all)) CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \ $(call cc-option,-fno-stack-protector) \
Signature · 1 line
2.40.1