summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvoisin2023-09-20 18:05:27 +0200
committerjvoisin2023-09-20 18:43:56 +0200
commitfd4332dbcd5227fde96e7bc128418d834b5b910f (patch)
tree4b2fce2510a5a2ee189147b76ef937a1c568a090
parentd2594298b89d0fb8989cae3ebc8900e77b6aa478 (diff)
Add tests for compile-time errors
-rw-r--r--.github/workflows/testsuite.yaml9
-rw-r--r--include/fortify-headers.h6
-rw-r--r--include/string.h3
-rw-r--r--tests/Makefile34
4 files changed, 32 insertions, 20 deletions
diff --git a/.github/workflows/testsuite.yaml b/.github/workflows/testsuite.yaml
index c2ca309..fb655e3 100644
--- a/.github/workflows/testsuite.yaml
+++ b/.github/workflows/testsuite.yaml
@@ -35,7 +35,7 @@ jobs:
35 run: make -C tests gcc 35 run: make -C tests gcc
36 - name: Running the testsuite 36 - name: Running the testsuite
37 shell: bash 37 shell: bash
38 run: make -C tests run | grep -zqv FAIL 38 run: make -C tests run
39 39
40 clang: 40 clang:
41 runs-on: ubuntu-latest 41 runs-on: ubuntu-latest
@@ -64,6 +64,9 @@ jobs:
64 - name: Build 64 - name: Build
65 shell: bash 65 shell: bash
66 run: make -C tests clang 66 run: make -C tests clang
67 - name: Running the testsuite 67 - name: Running the compile-time testsuite
68 shell: bash
69 run: make -C tests clang
70 - name: Running the run-time testsuite
68 shell: bash 71 shell: bash
69 run: make -C tests run | grep -zqv FAIL 72 run: make -C tests run
diff --git a/include/fortify-headers.h b/include/fortify-headers.h
index 3eaed7c..bc7bf00 100644
--- a/include/fortify-headers.h
+++ b/include/fortify-headers.h
@@ -106,9 +106,9 @@
106#define __diagnose_as_builtin(...) 106#define __diagnose_as_builtin(...)
107#endif 107#endif
108 108
109#if __has_attribute (__diagnose_if) 109#if __has_attribute (diagnose_if)
110#define __warning_if(cond, msg) __attribute__ ((__diagnose_if (cond, msg, "warning"))) 110#define __warning_if(cond, msg) __attribute__ ((diagnose_if (cond, msg, "warning")))
111#define __error_if(cond, msg) __attribute__ ((__diagnose_if (cond, msg, "error"))) 111#define __error_if(cond, msg) __attribute__ ((diagnose_if (cond, msg, "error")))
112#else 112#else
113#define __warning_if(cond, msg) 113#define __warning_if(cond, msg)
114#define __error_if(cond, msg) 114#define __error_if(cond, msg)
diff --git a/include/string.h b/include/string.h
index be13cb4..010b7f2 100644
--- a/include/string.h
+++ b/include/string.h
@@ -43,6 +43,7 @@ __access(read_only, 2, 3)
43__diagnose_as_builtin(__builtin_memcpy, 1, 2, 3) 43__diagnose_as_builtin(__builtin_memcpy, 1, 2, 3)
44_FORTIFY_FN(memcpy) void *memcpy(void * _FORTIFY_POS0 __od, 44_FORTIFY_FN(memcpy) void *memcpy(void * _FORTIFY_POS0 __od,
45 const void * _FORTIFY_POS0 __os, size_t __n) 45 const void * _FORTIFY_POS0 __os, size_t __n)
46__error_if((__bos(__od, 0) < __n), "'memcpy' called with `n` bigger than the size of `d`.")
46{ 47{
47 size_t __bd = __bos(__od, 0); 48 size_t __bd = __bos(__od, 0);
48 size_t __bs = __bos(__os, 0); 49 size_t __bs = __bos(__os, 0);
@@ -72,8 +73,8 @@ _FORTIFY_FN(memmove) void *memmove(void * _FORTIFY_POS0 __d,
72 73
73__access(write_only, 1, 3) 74__access(write_only, 1, 3)
74__diagnose_as_builtin(__builtin_memset, 1, 2, 3) 75__diagnose_as_builtin(__builtin_memset, 1, 2, 3)
75__warning_if(__c != 0 && __n == 0, "'memset' will set `0` bytes; did you invert the arguments?")
76_FORTIFY_FN(memset) void *memset(void * _FORTIFY_POS0 __d, int __c, size_t __n) 76_FORTIFY_FN(memset) void *memset(void * _FORTIFY_POS0 __d, int __c, size_t __n)
77__warning_if(__c != 0 && __n == 0, "'memset' will set `0` bytes; did you invert the arguments?")
77{ 78{
78 size_t __b = __bos(__d, 0); 79 size_t __b = __bos(__d, 0);
79 80
diff --git a/tests/Makefile b/tests/Makefile
index b14fd6b..4e5ee00 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,6 +1,11 @@
1CFLAGS=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2 1CFLAGS=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2
2 2
3TARGETS= \ 3COMPTIME_TARGETS= \
4 test_memcpy_overwrite_under \
5 test_memcpy_static_write \
6
7
8RUNTIME_TARGETS= \
4 test_FD_CLR_SETSIZE \ 9 test_FD_CLR_SETSIZE \
5 test_FD_CLR_negative \ 10 test_FD_CLR_negative \
6 test_FD_SET_SETSIZE \ 11 test_FD_SET_SETSIZE \
@@ -42,9 +47,7 @@ TARGETS= \
42 test_memcpy_dynamic_read \ 47 test_memcpy_dynamic_read \
43 test_memcpy_dynamic_write \ 48 test_memcpy_dynamic_write \
44 test_memcpy_overwrite_over \ 49 test_memcpy_overwrite_over \
45 test_memcpy_overwrite_under \
46 test_memcpy_static_read \ 50 test_memcpy_static_read \
47 test_memcpy_static_write \
48 test_memmove_dynamic_read \ 51 test_memmove_dynamic_read \
49 test_memmove_dynamic_write \ 52 test_memmove_dynamic_write \
50 test_memmove_static_read \ 53 test_memmove_static_read \
@@ -124,37 +127,42 @@ TARGETS= \
124.SILENT: 127.SILENT:
125 128
126gcc: CC=../x86_64-linux-musl-native/bin/gcc 129gcc: CC=../x86_64-linux-musl-native/bin/gcc
127gcc: $(TARGETS) 130gcc: $(RUNTIME_TARGETS)
128 131
129clang: CC=clang 132clang: CC=clang
130clang: GCOV=gcov
131clang: CFLAGS+=-I/usr/include/x86_64-linux-musl 133clang: CFLAGS+=-I/usr/include/x86_64-linux-musl
132clang: CFLAGS+=-I../x86_64-linux-musl-native/include/ 134clang: CFLAGS+=-I../x86_64-linux-musl-native/include/
133clang: CFLAGS+=-Ix86_64-linux-musl-native/include/ 135clang: CFLAGS+=-Ix86_64-linux-musl-native/include/
134clang: CFLAGS+=-nostdinc 136clang: CFLAGS+=-nostdinc
135clang: $(TARGETS) 137clang: comptime $(RUNTIME_TARGETS)
136
137all: gcc
138 138
139coverage: CFLAGS += -fprofile-arcs -ftest-coverage 139coverage: CFLAGS += -fprofile-arcs -ftest-coverage
140coverage: CC=../x86_64-linux-musl-native/bin/gcc 140coverage: CC=../x86_64-linux-musl-native/bin/gcc
141coverage: GCOV=../x86_64-linux-musl-native/bin/gcov 141coverage: GCOV=../x86_64-linux-musl-native/bin/gcov
142coverage: $(TARGETS) run 142coverage: $(RUNTIME_TARGETS) run
143 $(GCOV) *.c 143 $(GCOV) *.c
144 lcov --capture --directory . --output-file coverage.info 144 lcov --capture --directory . --output-file coverage.info
145 lcov --remove ./coverage.info "*/tests/*" --output-file cleaned-coverage.info 145 lcov --remove ./coverage.info "*/tests/*" --output-file cleaned-coverage.info
146 genhtml cleaned-coverage.info --output-directory coverage 146 genhtml cleaned-coverage.info --output-directory coverage
147 147
148$(TARGETS): %: %.c 148all: gcc
149
150
151$(RUNTIME_TARGETS): %: %.c
149 $(CC) $(CFLAGS) -o $@ $< 152 $(CC) $(CFLAGS) -o $@ $<
150 153
151run: $(TARGETS) 154run: $(RUNTIME_TARGETS)
152 $(foreach EXE, $(TARGETS), \ 155 $(foreach EXE, $(RUNTIME_TARGETS), \
153 timeout 1s ./$(EXE) 1234567890 2 3 4 5 6 7 8 9 0 >/dev/null && echo "$(EXE) OK" || echo "$(EXE) FAIL"; \ 156 timeout 1s ./$(EXE) 1234567890 2 3 4 5 6 7 8 9 0 >/dev/null && echo "$(EXE) OK" || echo "$(EXE) FAIL"; \
154 ) 157 )
155 158
159comptime: # only works on clang, as gcc doesn't have the diagnose_if attribute
160 $(foreach EXE, $(COMPTIME_TARGETS), \
161 ($(CC) $(CFLAGS) -o ./$(EXE) ./$(EXE).c 1>/dev/null 2>/dev/null && echo "$(EXE) FAIL" || echo "$(EXE) OK") || true ;\
162 )
163
156clean: 164clean:
157 $(foreach EXE, $(TARGETS), \ 165 $(foreach EXE, $(RUNTIME_TARGETS) $(COMPTIME_TARGETS), \
158 rm -f ./$(EXE) \ 166 rm -f ./$(EXE) \
159 ) 167 )
160 rm -rf ./*.gcno ./*.gcda ./*.gcov ./coverage.info ./coverage 168 rm -rf ./*.gcno ./*.gcda ./*.gcov ./coverage.info ./coverage