From 4e0e68da9221bfef4d6cfb3e19f573be77f27e83 Mon Sep 17 00:00:00 2001 From: Kris Kwiatkowski Date: Wed, 16 Jun 2021 07:48:47 +0100 Subject: [PATCH] adds address and memory sanitizer --- CMakeLists.txt | 55 ++++++++++++++++++++++++--------------------- src/capi/pqapi.c | 2 +- src/capi/schemes.h | 2 +- src/common/Makefile | 22 ------------------ src/common/utils.h | 5 ++--- 5 files changed, 34 insertions(+), 52 deletions(-) delete mode 100644 src/common/Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d35d74f..70429e67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,9 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") endif() if(CMAKE_C_COMPILER_ID MATCHES "Clang") - set(CLANG 1) + # Additional flags only useful when compiling with clang + set(C_CXX_FLAGS + "-Wconditional-uninitialized -Wno-missing-variable-declarations -Wno-unused-command-line-argument") endif() if (MACOSX) @@ -98,8 +100,7 @@ endif() # Global configuration -set(C_CXX_FLAGS - "-Wno-ignored-qualifiers \ +set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-ignored-qualifiers \ -Wall \ -Werror \ -Wextra \ @@ -107,29 +108,26 @@ set(C_CXX_FLAGS -Wshadow \ -Wno-variadic-macros \ -Wundef \ - -Wunused-result") - -if(CLANG) - set(C_CXX_FLAGS - "-Wconditional-uninitialized \ - -Wmissing-variable-declarations") -endif() + -Wunused-result \ + -Wno-unused-command-line-argument") # Build with address sanitizer if(ADDRSAN) - set(CLANG 1) set(C_CXX_FLAGS "${C_CXX_FLAGS} -fsanitize=undefined,address,leak -fno-omit-frame-pointer") 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(CLANG 1) - set(C_CXX_FLAGS "${C_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer") + 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) @@ -150,13 +148,21 @@ include_directories( set_property(GLOBAL PROPERTY obj_libs "") - # Set CPU architecture -set(CMAKE_C_FLAGS "${C_CXX_FLAGS} -D${ARCH}") -set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -D${ARCH}") +set(C_CXX_FLAGS "${C_CXX_FLAGS} -D${ARCH}") + +# Build for haswell if on x86_64 +if(${ARCH} STREQUAL "ARCH_x86_64") + string(APPEND C_CXX_FLAGS " -march=haswell") +endif() + +set(PQC_CMAKE_C_FLAGS "${C_CXX_FLAGS}") +set(PQC_CMAKE_CXX_FLAGS "${C_CXX_FLAGS}") +set(CMAKE_C_FLAGS ${PQC_CMAKE_C_FLAGS}) +set(CMAKE_CXX_FLAGS ${PQC_CMAKE_CXX_FLAGS}) if(PQC_WEAK_RANDOMBYTES) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPQC_WEAK_RANDOMBYTES") + string(APPEND PQC_CMAKE_C_FLAGS " -DPQC_WEAK_RANDOMBYTES") endif() # Define sources of the components @@ -223,10 +229,8 @@ add_subdirectory(src/kem/mceliece/mceliece6688128f/clean) add_subdirectory(src/kem/mceliece/mceliece6960119f/clean) add_subdirectory(src/kem/mceliece/mceliece8192128f/clean) # Hardware optimized targets -if(${ARCH} STREQUAL "ARCH_x86_64") -set(CMAKE_C_FLAGS - "${CMAKE_C_FLAGS} -march=haswell") +if(${ARCH} STREQUAL "ARCH_x86_64") set(SRC_COMMON_AVX2 src/common/keccak4x/KeccakP-1600-times4-SIMD256.c ) @@ -279,8 +283,6 @@ add_subdirectory(src/kem/hqc/hqc-rmrs-192/avx2) add_subdirectory(src/kem/hqc/hqc-rmrs-256/avx2) endif() - - # The rest of the library set(SRC_COMMON_GENERIC src/common/aes.c @@ -326,10 +328,14 @@ target_link_libraries( ${OBJ_LIBS} ) +SET(UT_SRC test/ut.cpp) +if(MEMSAN) +SET(UT_SRC ${UT_SRC} test/ct.cpp) +endif() + add_executable( ut - - test/ut.cpp + ${UT_SRC} ) target_link_libraries( @@ -340,7 +346,6 @@ target_link_libraries( target_include_directories( ut PRIVATE - ${CMAKE_SOURCE_DIR}) if(NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug") diff --git a/src/capi/pqapi.c b/src/capi/pqapi.c index fef84c56..11189a1b 100644 --- a/src/capi/pqapi.c +++ b/src/capi/pqapi.c @@ -71,7 +71,7 @@ const pqc_sig_ctx_t sigs[] = { // Contains capabilities on x86 CPU on which implementation is running X86Features CPU_CAPS; -const X86Features * const get_cpu_caps(void) { +const X86Features * get_cpu_caps(void) { return &CPU_CAPS; } diff --git a/src/capi/schemes.h b/src/capi/schemes.h index 6190c4f0..de778768 100644 --- a/src/capi/schemes.h +++ b/src/capi/schemes.h @@ -117,4 +117,4 @@ #include "kem/hqc/hqc-rmrs-256/avx2/api.h" #include "kem/sike/includes/sike/sike.h" -#endif \ No newline at end of file +#endif diff --git a/src/common/Makefile b/src/common/Makefile deleted file mode 100644 index 7cf72008..00000000 --- a/src/common/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -# This Makefile can be used with GNU Make or BSD Make - -LIB=libcommon.a -HEADERS= fips202.h aes.h sha2.h randombytes.h sp800-185.h nistseedexpander.h cpucycles.h speed_print.h -OBJECTS= fips202.o aes.o sha2.o randombytes.o sp800-185.o nistseedexpander.o cpucycles.o speed_print.o - -CFLAGS=-O3 -march=native -mtune=native -flto -mavx2 -maes -mbmi2 -Wall -Wextra -Wpedantic -Wvla -Wredundant-decls -Wmissing-prototypes -std=gnu99 $(EXTRAFLAGS) - -all: $(LIB) - -%.o: %.s $(HEADERS) - $(AS) -o $@ $< - -%.o: %.c $(HEADERS) - $(CC) $(CFLAGS) -c -o $@ $< - -$(LIB): $(OBJECTS) - $(AR) -r $@ $(OBJECTS) - -clean: - $(RM) $(OBJECTS) - $(RM) $(LIB) diff --git a/src/common/utils.h b/src/common/utils.h index 4fb793b1..45142dab 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -32,7 +32,6 @@ (((uint16_t)(x)[0])<<8 | \ ((uint16_t)(x)[1])<<0) \ +const X86Features * get_cpu_caps(void); -const X86Features * const get_cpu_caps(void); - -#endif \ No newline at end of file +#endif