Build libcxx and libcxxabi with Memory Sanitizer

This commit is contained in:
Henry Case 2021-06-17 08:27:29 +01:00
parent 24881fade8
commit 7be2562de5
4 changed files with 58 additions and 15 deletions

14
.cmake/libstd-memcheck.mk Normal file
View File

@ -0,0 +1,14 @@
include(ExternalProject)
string (REPLACE " " "$<SEMICOLON>" 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}
)

View File

@ -21,7 +21,8 @@ jobs:
clang-release-build, clang-release-build,
gcc-debug-build, gcc-debug-build,
clang-debug-build, clang-debug-build,
clang-debug-asan-build, clang-release-asan-build,
clang-release-msan-build,
] ]
include: include:
@ -41,10 +42,14 @@ jobs:
cc: /usr/bin/clang cc: /usr/bin/clang
cxx: /usr/bin/clang++ cxx: /usr/bin/clang++
flags: -DCMAKE_BUILD_TYPE=Debug flags: -DCMAKE_BUILD_TYPE=Debug
- name: clang-debug-asan-build - name: clang-release-asan-build
cc: clang cc: clang
cxx: clang++ cxx: clang++
flags: -DCMAKE_BUILD_TYPE=Release -DADDRSAN=1 flags: -DCMAKE_BUILD_TYPE=Release -DADDRSAN=1
- name: clang-release-msan-build
cc: clang
cxx: clang++
flags: -DCMAKE_BUILD_TYPE=Release -DMEMSAN=1
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
with: with:

View File

@ -24,6 +24,7 @@ FetchContent_Declare(
SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/cpu_features SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/cpu_features
GIT_REPOSITORY https://github.com/google/cpu_features.git GIT_REPOSITORY https://github.com/google/cpu_features.git
GIT_TAG bc2846e78faeb26b8a46c17df369d4e5f1f9e2bb GIT_TAG bc2846e78faeb26b8a46c17df369d4e5f1f9e2bb
GIT_SHALLOW TRUE
) )
FetchContent_Populate(cpu_features) FetchContent_Populate(cpu_features)
@ -32,9 +33,22 @@ FetchContent_Declare(
SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gbench SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gbench
GIT_REPOSITORY https://github.com/kriskwiatkowski/benchmark.git GIT_REPOSITORY https://github.com/kriskwiatkowski/benchmark.git
GIT_TAG 49862ab56b6b7c3afd87b80bd5d787ed78ce3b96 GIT_TAG 49862ab56b6b7c3afd87b80bd5d787ed78ce3b96
GIT_SHALLOW TRUE
) )
FetchContent_Populate(gbench) 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) add_subdirectory(3rd/gtest)
set(BUILD_PIC ON CACHE BOOL "") set(BUILD_PIC ON CACHE BOOL "")
add_subdirectory(3rd/cpu_features) add_subdirectory(3rd/cpu_features)
@ -117,19 +131,6 @@ if(ADDRSAN)
set(LDFLAGS "${LDFLAGS} -fsanitize=undefined,address,leak") set(LDFLAGS "${LDFLAGS} -fsanitize=undefined,address,leak")
endif() 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) include(.cmake/common.mk)
# Control Debug/Release mode # Control Debug/Release mode

23
test/ct.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <algorithm>
#include <vector>
#include <gtest/gtest.h>
#include <pqc/pqc.h>
// #ifdef VALGRIND
// #include <valgrind/valgrind.h>
// #include <valgrind/memcheck.h>
// #define POISON(p,sz) VALGRIND_MAKE_MEM_UNDEFINED(p,sz)
// #endif
#ifdef PQC_MEMSAN
#include <sanitizer/msan_interface.h>
#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);
}