diff options
| -rw-r--r-- | .github/workflows/testsuite.yaml | 19 | ||||
| -rw-r--r-- | tests/Makefile | 25 | ||||
| -rw-r--r-- | tests/common.h | 62 | ||||
| -rw-r--r-- | tests/test_memcpy_dynamic_read.c | 16 | ||||
| -rw-r--r-- | tests/test_memcpy_dynamic_write.c | 16 | ||||
| -rw-r--r-- | tests/test_memcpy_static_read.c | 16 | ||||
| -rw-r--r-- | tests/test_memcpy_static_write.c | 16 |
7 files changed, 170 insertions, 0 deletions
diff --git a/.github/workflows/testsuite.yaml b/.github/workflows/testsuite.yaml new file mode 100644 index 0000000..115b145 --- /dev/null +++ b/.github/workflows/testsuite.yaml | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | name: Test suite | ||
| 2 | on: | ||
| 3 | pull_request: | ||
| 4 | push: | ||
| 5 | schedule: | ||
| 6 | - cron: '0 16 * * 5' | ||
| 7 | |||
| 8 | jobs: | ||
| 9 | ci: | ||
| 10 | runs-on: ubuntu-latest | ||
| 11 | steps: | ||
| 12 | - name: Checking out the code | ||
| 13 | uses: actions/checkout@v3 | ||
| 14 | - name: Downloading musl-based toolchain | ||
| 15 | run: wget https://musl.cc/x86_64-linux-musl-native.tgz | ||
| 16 | - name: Extracting musl-based toolchain | ||
| 17 | run: tar xzf ./x86_64-linux-musl-native.tgz | ||
| 18 | - name: Running the testsuite | ||
| 19 | run: make -C tests | grep -zqv FAIL | ||
diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..c48d0b0 --- /dev/null +++ b/tests/Makefile | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | CC=../x86_64-linux-musl-native/bin/gcc | ||
| 2 | CFLAGS=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2 | ||
| 3 | |||
| 4 | TARGETS=test_memcpy_static_write \ | ||
| 5 | test_memcpy_dynamic_write \ | ||
| 6 | test_memcpy_static_read \ | ||
| 7 | test_memcpy_dynamic_read | ||
| 8 | |||
| 9 | .SILENT: | ||
| 10 | |||
| 11 | all: $(TARGETS) run | ||
| 12 | |||
| 13 | $(TARGETS): %: %.c | ||
| 14 | $(CC) $(CFLAGS) -o $@ $< | ||
| 15 | |||
| 16 | run: $(TARGETS) | ||
| 17 | $(foreach EXE, $(TARGETS), \ | ||
| 18 | ./$(EXE) 1 2 3 4 5 6 7 8 9 0 >/dev/null && echo "$(EXE) OK" || echo "$(EXE) FAIL" ; \ | ||
| 19 | ) | ||
| 20 | |||
| 21 | clean: | ||
| 22 | $(foreach EXE, $(TARGETS), \ | ||
| 23 | rm -f ./$(EXE) \ | ||
| 24 | ) | ||
| 25 | |||
diff --git a/tests/common.h b/tests/common.h new file mode 100644 index 0000000..4bd5a4e --- /dev/null +++ b/tests/common.h | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | /* Copyright (C) 2004-2020 Free Software Foundation, Inc. | ||
| 2 | This snippet is taken from debug/tst-chk1 in the GNU C Library. | ||
| 3 | |||
| 4 | The GNU C Library is free software; you can redistribute it and/or | ||
| 5 | modify it under the terms of the GNU Lesser General Public | ||
| 6 | License as published by the Free Software Foundation; either | ||
| 7 | version 2.1 of the License, or (at your option) any later version. | ||
| 8 | |||
| 9 | The GNU C Library is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | Lesser General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU Lesser General Public | ||
| 15 | License along with the GNU C Library; if not, see | ||
| 16 | <https://www.gnu.org/licenses/>. */ | ||
| 17 | |||
| 18 | #include <setjmp.h> | ||
| 19 | #include <unistd.h> | ||
| 20 | #include <signal.h> | ||
| 21 | #include <stdio.h> | ||
| 22 | |||
| 23 | volatile int chk_fail_ok; | ||
| 24 | volatile int ret; | ||
| 25 | jmp_buf chk_fail_buf; | ||
| 26 | |||
| 27 | static void | ||
| 28 | handler (int sig) | ||
| 29 | { | ||
| 30 | if (chk_fail_ok) | ||
| 31 | { | ||
| 32 | chk_fail_ok = 0; | ||
| 33 | longjmp (chk_fail_buf, 1); | ||
| 34 | } | ||
| 35 | else | ||
| 36 | _exit (127); | ||
| 37 | } | ||
| 38 | |||
| 39 | void | ||
| 40 | __attribute__((constructor)) | ||
| 41 | set_fortify_handler (void) | ||
| 42 | { | ||
| 43 | struct sigaction sa; | ||
| 44 | |||
| 45 | sa.sa_handler = handler; | ||
| 46 | sa.sa_flags = 0; | ||
| 47 | sigemptyset (&sa.sa_mask); | ||
| 48 | |||
| 49 | sigaction (SIGILL, &sa, NULL); | ||
| 50 | } | ||
| 51 | |||
| 52 | #define FAIL() \ | ||
| 53 | do { fprintf (stderr, "Failure on line %d\n", __LINE__); ret = 1; } while (0) | ||
| 54 | #define CHK_FAIL_START \ | ||
| 55 | chk_fail_ok = 1; \ | ||
| 56 | if (! setjmp (chk_fail_buf)) \ | ||
| 57 | { | ||
| 58 | #define CHK_FAIL_END \ | ||
| 59 | chk_fail_ok = 0; \ | ||
| 60 | FAIL (); \ | ||
| 61 | } | ||
| 62 | |||
diff --git a/tests/test_memcpy_dynamic_read.c b/tests/test_memcpy_dynamic_read.c new file mode 100644 index 0000000..65cb77a --- /dev/null +++ b/tests/test_memcpy_dynamic_read.c | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[12] = {0}; | ||
| 7 | memcpy(buffer, "1234567890", sizeof(buffer) - 1); | ||
| 8 | puts(buffer); | ||
| 9 | |||
| 10 | CHK_FAIL_START | ||
| 11 | memcpy(buffer, "123456", argc); | ||
| 12 | CHK_FAIL_END | ||
| 13 | |||
| 14 | puts(buffer); | ||
| 15 | return ret; | ||
| 16 | } | ||
diff --git a/tests/test_memcpy_dynamic_write.c b/tests/test_memcpy_dynamic_write.c new file mode 100644 index 0000000..60a594f --- /dev/null +++ b/tests/test_memcpy_dynamic_write.c | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[8] = {0}; | ||
| 7 | memcpy(buffer, "1234567890", sizeof(buffer) - 1); | ||
| 8 | puts(buffer); | ||
| 9 | |||
| 10 | CHK_FAIL_START | ||
| 11 | memcpy(buffer, "1234567890", argc); | ||
| 12 | CHK_FAIL_END | ||
| 13 | |||
| 14 | puts(buffer); | ||
| 15 | return ret; | ||
| 16 | } | ||
diff --git a/tests/test_memcpy_static_read.c b/tests/test_memcpy_static_read.c new file mode 100644 index 0000000..fed67a8 --- /dev/null +++ b/tests/test_memcpy_static_read.c | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[8] = {0}; | ||
| 7 | memcpy(buffer, "123456", 4); | ||
| 8 | puts(buffer); | ||
| 9 | |||
| 10 | CHK_FAIL_START | ||
| 11 | memcpy(buffer, "123456", sizeof(buffer)); | ||
| 12 | CHK_FAIL_END | ||
| 13 | |||
| 14 | puts(buffer); | ||
| 15 | return ret; | ||
| 16 | } | ||
diff --git a/tests/test_memcpy_static_write.c b/tests/test_memcpy_static_write.c new file mode 100644 index 0000000..1a378f8 --- /dev/null +++ b/tests/test_memcpy_static_write.c | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[8] = {0}; | ||
| 7 | memcpy(buffer, "1234567890", sizeof(buffer) - 1); | ||
| 8 | puts(buffer); | ||
| 9 | |||
| 10 | CHK_FAIL_START | ||
| 11 | memcpy(buffer, "1234567890", sizeof(buffer) + 1); | ||
| 12 | CHK_FAIL_END | ||
| 13 | |||
| 14 | puts(buffer); | ||
| 15 | return ret; | ||
| 16 | } | ||
