diff --git a/Dockerfile.dev b/Dockerfile.dev index f2caf5b..ec1ccef 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -100,6 +100,9 @@ RUN cd test && \ for test in stacksmash; do \ $arch-w64-mingw32-clang $test.c -o $arch/$test-asan.exe -fsanitize=address -g -gcodeview -Wl,-pdb,$arch/$test-asan.pdb || exit 1; \ done; \ + for test in ubsan; do \ + $arch-w64-mingw32-clang $test.c -o $arch/$test.exe -fsanitize=undefined || exit 1; \ + done; \ done # Build libssp diff --git a/run-tests.sh b/run-tests.sh index 898e4e3..24430a9 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -20,6 +20,7 @@ TESTS_CPP="hello-cpp hello-exception tlstest-main exception-locale" TESTS_CPP_DLL="tlstest-lib" TESTS_SSP="stacksmash" TESTS_ASAN="stacksmash" +TESTS_UBSAN="ubsan" for arch in $ARCHS; do mkdir -p $arch for test in $TESTS_C; do @@ -54,6 +55,16 @@ for arch in $ARCHS; do esac $arch-w64-mingw32-clang $test.c -o $arch/$test-asan.exe -fsanitize=address -g -gcodeview -Wl,-pdb,$arch/$test-asan.pdb done + for test in $TESTS_UBSAN; do + case $arch in + # Ubsan might not require anything too x86 specific, but we don't + # build any of the sanitizer libs for anything else than x86. + i686|x86_64) ;; + *) continue ;; + esac + $arch-w64-mingw32-clang $test.c -o $arch/$test.exe -fsanitize=undefined + TESTS_EXTRA="$TESTS_EXTRA $test" + done DLL="$TESTS_C_DLL $TESTS_CPP_DLL" case $arch in i686|x86_64) diff --git a/test/ubsan.c b/test/ubsan.c new file mode 100644 index 0000000..4ad008a --- /dev/null +++ b/test/ubsan.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018 Martin Storsjo + * + * This file is part of llvm-mingw. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +int32_t var = INT32_MAX; + +int main(int argc, char *argv[]) { + char buf[10]; + if (argc > 1) { + fprintf(stderr, "triggering undefined behaviour\n"); + fflush(stderr); + var++; + } else { + var--; + fprintf(stderr, "%s: A test tool for undefined behaviour sanitizer. \n" + "Run this with a command line argument to trigger " + "undefined behaviour.\n", argv[0]); + } + return 0; +}