From c26e8132fca65da752223bf6cd59cf8509dd89e4 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Mon, 7 Aug 2023 07:57:56 +0200 Subject: [PATCH] fix ubsan for `test_libnimbus_lc` (#5258) Pass `-fsanitize=undefined` flag when compiling `test_libnimbus_lc` when building in ubsan mode. --- .github/workflows/ci.yml | 1 + Makefile | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 119571c4e..813217fd7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -106,6 +106,7 @@ jobs: # Stack usage test and UBSAN on recent enough gcc: if [[ '${{ runner.os }}' == 'Linux' && '${{ matrix.target.cpu }}' == 'amd64' ]]; then if [[ '${{ github.sha }}' =~ ^7 ]]; then + export WITH_UBSAN=1 >> $GITHUB_ENV export NIMFLAGS="${NIMFLAGS} -d:limitStackUsage --passC:-fsanitize=undefined --passL:-fsanitize=undefined" else export NIMFLAGS="${NIMFLAGS} -d:limitStackUsage" diff --git a/Makefile b/Makefile index cb76b5303..5c6e3e869 100644 --- a/Makefile +++ b/Makefile @@ -756,18 +756,29 @@ libnimbus_lc.a: | build deps echo -e $(BUILD_END_MSG) "build/$@" # `-Wno-maybe-uninitialized` in Linux: https://github.com/nim-lang/Nim/issues/22246 +# `-fsanitize=undefined` in Windows: https://github.com/msys2/MINGW-packages/issues/3163 test_libnimbus_lc: libnimbus_lc.a + echo -e $(BUILD_MSG) "build/$@" && \ set -x && \ + EXTRA_FLAGS=() && \ case "$$(uname)" in \ Darwin) \ - clang -D__DIR__="\"beacon_chain/libnimbus_lc\"" --std=c17 -Weverything -Werror -Wno-declaration-after-statement -Wno-nullability-extension -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o build/test_libnimbus_lc beacon_chain/libnimbus_lc/test_libnimbus_lc.c build/libnimbus_lc.a -framework Security; \ + if (( $${WITH_UBSAN:-0} )); then \ + EXTRA_FLAGS+=('-fsanitize=undefined'); \ + fi; \ + clang -D__DIR__="\"beacon_chain/libnimbus_lc\"" --std=c17 -Weverything -Werror -Wno-declaration-after-statement -Wno-nullability-extension -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o build/test_libnimbus_lc beacon_chain/libnimbus_lc/test_libnimbus_lc.c build/libnimbus_lc.a -framework Security "$${EXTRA_FLAGS[@]}"; \ ;; \ MINGW64_*) \ - gcc -D__DIR__="\"beacon_chain/libnimbus_lc\"" --std=c17 -Wall -Wextra -pedantic -Werror -pedantic-errors -flto -o build/test_libnimbus_lc -D_CRT_SECURE_NO_WARNINGS beacon_chain/libnimbus_lc/test_libnimbus_lc.c build/libnimbus_lc.a; \ + if (( $${WITH_UBSAN:-0} )); then \ + echo "MINGW cannot find -lubsan." && exit 1; \ + fi; \ + gcc -D__DIR__="\"beacon_chain/libnimbus_lc\"" --std=c17 -Wall -Wextra -pedantic -Werror -pedantic-errors -flto -o build/test_libnimbus_lc -D_CRT_SECURE_NO_WARNINGS beacon_chain/libnimbus_lc/test_libnimbus_lc.c build/libnimbus_lc.a "$${EXTRA_FLAGS[@]}"; \ ;; \ *) \ - gcc -D__DIR__="\"beacon_chain/libnimbus_lc\"" --std=c17 -Wall -Wextra -pedantic -Werror -pedantic-errors -Wno-maybe-uninitialized -flto -o build/test_libnimbus_lc beacon_chain/libnimbus_lc/test_libnimbus_lc.c build/libnimbus_lc.a; \ + if (( $${WITH_UBSAN:-0} )); then \ + EXTRA_FLAGS+=('-fsanitize=undefined'); \ + fi; \ + gcc -D__DIR__="\"beacon_chain/libnimbus_lc\"" --std=c17 -Wall -Wextra -pedantic -Werror -pedantic-errors -Wno-maybe-uninitialized -flto -o build/test_libnimbus_lc beacon_chain/libnimbus_lc/test_libnimbus_lc.c build/libnimbus_lc.a "$${EXTRA_FLAGS[@]}"; \ ;; \ esac && \ echo -e $(BUILD_END_MSG) "build/$@"