From cf130cc2ff5d6d2d1894a5563db5e8c85b0d4b1a Mon Sep 17 00:00:00 2001 From: Kris Kwiatkowski Date: Thu, 17 Jun 2021 08:27:29 +0100 Subject: [PATCH] Build libcxx and libcxxabi with Memory Sanitizer --- .cmake/libstd-memcheck.mk | 14 ++++++++++++++ .github/workflows/main.yml | 9 +++++++-- CMakeLists.txt | 27 ++++++++++++++------------- test/ct.cpp | 23 +++++++++++++++++++++++ 4 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 .cmake/libstd-memcheck.mk create mode 100644 test/ct.cpp diff --git a/.cmake/libstd-memcheck.mk b/.cmake/libstd-memcheck.mk new file mode 100644 index 00000000..62e1048c --- /dev/null +++ b/.cmake/libstd-memcheck.mk @@ -0,0 +1,14 @@ +include(ExternalProject) +string (REPLACE " " "$" LLVM_PROJECT_TARGETS "libcxx libcxxabi") +set(PREFIX ${CMAKE_CURRENT_BINARY_DIR}/3rd/llvm-project) +ExternalProject_Add( + llvm-project + GIT_REPOSITORY https://github.com/llvm/llvm-project.git + GIT_TAG llvmorg-12.0.0 + GIT_SHALLOW TRUE + CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=${LLVM_PROJECT_TARGETS} -DLLVM_USE_SANITIZER=MemoryWithOrigins -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ../llvm-project/llvm + BUILD_COMMAND make cxx cxxabi + INSTALL_COMMAND DESTDIR=${PREFIX} make install-cxx-headers install-cxx install-cxxabi + COMMENT "Building memcheck instrumented libc++ and libc++abi" + PREFIX ${PREFIX} +) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d2c3ca56..2df860ff 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,7 +21,8 @@ jobs: clang-release-build, gcc-debug-build, clang-debug-build, - clang-debug-asan-build, + clang-release-asan-build, + clang-release-msan-build, ] include: @@ -41,10 +42,14 @@ jobs: cc: /usr/bin/clang cxx: /usr/bin/clang++ flags: -DCMAKE_BUILD_TYPE=Debug - - name: clang-debug-asan-build + - name: clang-release-asan-build cc: clang cxx: clang++ flags: -DCMAKE_BUILD_TYPE=Release -DADDRSAN=1 + - name: clang-release-msan-build + cc: clang + cxx: clang++ + flags: -DCMAKE_BUILD_TYPE=Release -DMEMSAN=1 steps: - uses: actions/checkout@v1 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index 70429e67..796f4e6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ FetchContent_Declare( SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/cpu_features GIT_REPOSITORY https://github.com/google/cpu_features.git GIT_TAG bc2846e78faeb26b8a46c17df369d4e5f1f9e2bb + GIT_SHALLOW TRUE ) FetchContent_Populate(cpu_features) @@ -32,9 +33,22 @@ FetchContent_Declare( SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gbench GIT_REPOSITORY https://github.com/kriskwiatkowski/benchmark.git GIT_TAG 49862ab56b6b7c3afd87b80bd5d787ed78ce3b96 + GIT_SHALLOW TRUE ) FetchContent_Populate(gbench) +if(MEMSAN) + # PQC_MEMSAN enables usage of some internals from clang + if (NOT CMAKE_C_COMPILER_ID MATCHES "Clang") + message(FATAL_ERROR "Must use clang if compiled with memory sanitizer.") + endif() + if(ADDRSAN) + message(FATAL_ERROR "Can't use MSAN and ASAN") + endif() + include(.cmake/libstd-memcheck.mk) + #set(C_CXX_FLAGS "${C_CXX_FLAGS} -DPQC_MEMSAN=1 -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer") +endif() + add_subdirectory(3rd/gtest) set(BUILD_PIC ON CACHE BOOL "") add_subdirectory(3rd/cpu_features) @@ -117,19 +131,6 @@ if(ADDRSAN) set(LDFLAGS "${LDFLAGS} -fsanitize=undefined,address,leak") endif() -# Build with memory sanitizer -if(MEMSAN) - # PQC_MEMSAN enables usage of some internals from clang - if (NOT CMAKE_C_COMPILER_ID MATCHES "Clang") - message(FATAL_ERROR "Must use clang if compiled with memory sanitizer.") - endif() - if(ADDRSAN) - message(FATAL_ERROR "Can't use MSAN and ASAN") - endif() - - set(C_CXX_FLAGS "${C_CXX_FLAGS} -DPQC_MEMSAN=1 -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer") -endif() - include(.cmake/common.mk) # Control Debug/Release mode diff --git a/test/ct.cpp b/test/ct.cpp new file mode 100644 index 00000000..73d79f20 --- /dev/null +++ b/test/ct.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +// #ifdef VALGRIND +// #include +// #include +// #define POISON(p,sz) VALGRIND_MAKE_MEM_UNDEFINED(p,sz) +// #endif + +#ifdef PQC_MEMSAN +#include +#define POISON(p,sz) __msan_poison(p,sz) +#endif + + +TEST(ConstantTime, Poisonner_Basic) { + unsigned char x[8] = {0}; +//gi POISON(x, 4); + if(x[5]) x[6] = x[5]; + //UNPOISON(x, 4); +}