jvoisin / fortify-headers

Standalone portable header-based implementation of FORTIFY_SOURCE=3
BSD Zero Clause License
19 stars 3 forks source link

testsuite fails on Alpine Linux #69

Open ncopa opened 1 month ago

ncopa commented 1 month ago

Running tests fails miserable on Alpine Linux:

ncopa-desktop:~/src/fortify-headers (master)$ make -C tests
make: Entering directory '/var/ncopa/src/fortify-headers/tests'
make: ../x86_64-linux-musl-native/bin/gcc: No such file or directory
make: *** [Makefile:178: test_FD_CLR_SETSIZE] Error 127
make: Leaving directory '/var/ncopa/src/fortify-headers/tests'

The Makefile assumes you have a ../x86_64-linux-musl-native/bin/gcc compiler available and fails if you don't.

Also If I use the system gcc by setting CC=gcc:

ncopa-desktop:~/src/fortify-headers (master)$ make -C tests CC=gcc
make: Entering directory '/var/ncopa/src/fortify-headers/tests'                                                                                                                     
<command-line>: warning: "_FORTIFY_SOURCE" redefined                                                                                                                                
<built-in>: note: this is the location of the previous definition                                                                                                                   
In file included from ../include/unistd.h:26,                                                                                                                                       
                 from common.h:19,                                                                                                                                                  
                 from test_FD_CLR_SETSIZE.c:1:                                                                                                                                      
../include/unistd.h:48:13: error: 'confstr' undeclared here (not in a function)           
   48 | _FORTIFY_FN(confstr) size_t confstr(int __n, char * _FORTIFY_POS0 __s, size_t __l)                                                                                          
      |             ^~~~~~~
../include/fortify-headers.h:68:40: note: in definition of macro '_FORTIFY_ORIG'
   68 | #define _FORTIFY_ORIG(p,fn) __typeof__(fn) __orig_##fn __asm__(_FORTIFY_STR(p) #fn)
      |                                        ^~
../include/fortify-headers.h:70:25: note: in expansion of macro '_FORTIFY_FNB'
   70 | #define _FORTIFY_FN(fn) _FORTIFY_FNB(fn); _FORTIFY_INLINE
      |                         ^~~~~~~~~~~~
../include/unistd.h:48:1: note: in expansion of macro '_FORTIFY_FN'
   48 | _FORTIFY_FN(confstr) size_t confstr(int __n, char * _FORTIFY_POS0 __s, size_t __l) 
      | ^~~~~~~~~~~
../include/unistd.h:48:1: warning: 'access' attribute only applies to function types [-Wattributes]
../include/unistd.h:48:22: error: unknown type name 'size_t'
   48 | _FORTIFY_FN(confstr) size_t confstr(int __n, char * _FORTIFY_POS0 __s, size_t __l) 
      |                      ^~~~~~
../include/unistd.h:27:1: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
   26 | #include "fortify-headers.h"
  +++ |+#include <stddef.h>
   27 | 
...
...
...
test_FD_CLR_SETSIZE.c:10:3: note: in expansion of macro 'CHK_FAIL_END'
   10 |   CHK_FAIL_END
      |   ^~~~~~~~~~~~
common.h:54:17: note: 'stderr' is defined in header '<stdio.h>'; this is probably fixable by adding '#include <stdio.h>'
   54 |   do { fprintf (stderr, "Failure in %s:%d\n", __FILE__, __LINE__); ret = 1; } while (0)
      |                 ^~~~~~
common.h:61:7: note: in expansion of macro 'FAIL'
   61 |       FAIL ();                                  \
      |       ^~~~
test_FD_CLR_SETSIZE.c:10:3: note: in expansion of macro 'CHK_FAIL_END'
   10 |   CHK_FAIL_END
      |   ^~~~~~~~~~~~
test_FD_CLR_SETSIZE.c:12:3: error: implicit declaration of function 'puts' [-Wimplicit-function-declaration]
   12 |   puts((const char*)&rfds);
      |   ^~~~
test_FD_CLR_SETSIZE.c:12:3: note: include '<stdio.h>' or provide a declaration of 'puts'
make: *** [Makefile:178: test_FD_CLR_SETSIZE] Error 1
make: Leaving directory '/var/ncopa/src/fortify-headers/tests'

Part of the problem is that the Alpine Linux gcc enables fortify-headers by default. There are warnings like:

<command-line>: warning: "_FORTIFY_SOURCE" redefined                                                                                                                                
<built-in>: note: this is the location of the previous definition

This can be worked around by setting -U_FORTIFY_SOURCE:

diff --git a/tests/Makefile b/tests/Makefile
index 333f088..79ac154 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,4 +1,4 @@
-CFLAGS+=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2 -DFORTIFY_PEDANTIC_CHECKS -Wno-format -Werror=pointer-arith
+CFLAGS+=-I../include/ -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -static -O2 -DFORTIFY_PEDANTIC_CHECKS -Wno-format -Werror=pointer-arith

 COMPTIME_TARGETS= \
        test_memcpy_overwrite_under  \

I see that for clang there are -nostdinc to avoid it mess up with system headers. We can do something similar for gcc on Alpine:

diff --git a/tests/Makefile b/tests/Makefile
index 333f088..dbd6458 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,4 +1,4 @@
-CFLAGS+=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2 -DFORTIFY_PEDANTIC_CHECKS -Wno-format -Werror=pointer-arith
+CFLAGS+=-I../include/ -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -static -O2 -DFORTIFY_PEDANTIC_CHECKS -Wno-format -Werror=pointer-arith -nostdinc -isystem /usr/include

 COMPTIME_TARGETS= \
        test_memcpy_overwrite_under  \

And with that the tests builds and runs and passes!

I think it would make sense to move out the gcc vs clang, and hardcoded CC, and system header include CFLAGS out of the Makefile and make them easier to override, and set the proper overrides in .github/workflow/