diff --git a/bin/dsn.cmake b/bin/dsn.cmake index 5a3e8e0372..0bad3dde7c 100644 --- a/bin/dsn.cmake +++ b/bin/dsn.cmake @@ -173,6 +173,8 @@ endfunction() function(dsn_setup_compiler_flags) if(CMAKE_BUILD_TYPE STREQUAL "Debug") add_definitions(-DDSN_BUILD_TYPE=Debug) + #for sanitizer + add_definitions(-g) else() add_definitions(-g) add_definitions(-O2) @@ -212,6 +214,18 @@ function(dsn_setup_compiler_flags) message(STATUS "use ccache to speed up compilation") endif(CCACHE_FOUND) + # add sanitizer check + if(DEFINED SANITIZER) + if(NOT (("${COMPILER_FAMILY}" STREQUAL "clang") OR + ("${COMPILER_FAMILY}" STREQUAL "gcc" AND "${COMPILER_VERSION}" VERSION_GREATER "4.8"))) + message(SEND_ERROR "Cannot use sanitizer without clang or gcc >= 4.8") + endif() + + message(STATUS "Running cmake with sanitizer=${SANITIZER}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${SANITIZER}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${SANITIZER}") + endif() + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free" CACHE diff --git a/run.sh b/run.sh index 26d7777c45..9e95f372d5 100755 --- a/run.sh +++ b/run.sh @@ -55,6 +55,8 @@ function usage_build() echo " to enable valgrind memcheck, default no" echo " --skip_thirdparty whether to skip building thirdparties, default no" echo " --check whether to perform code check before building" + echo " --sanitizer build with sanitizer to check potential problems, + type: address|leak|thread|undefined" if [ "$ONLY_BUILD" == "NO" ]; then echo " -m|--test_module specify modules to test, split by ','," echo " e.g., \"dsn.core.tests,dsn.tests\"," @@ -63,6 +65,9 @@ function usage_build() } function run_build() { + #NOTE(jiashuo1): No memory check mode, because MemorySanitizer is only available in Clang for Linux x86_64 targets + SANITIZERS=("address" "leak" "thread" "undefined") + C_COMPILER="gcc" CXX_COMPILER="g++" BUILD_TYPE="release" @@ -76,6 +81,7 @@ function run_build() DISABLE_GPERF=NO SKIP_THIRDPARTY=NO CHECK=NO + SANITIZER="" TEST_MODULE="" while [[ $# > 0 ]]; do key="$1" @@ -132,6 +138,16 @@ function run_build() --check) CHECK=YES ;; + --sanitizer) + IS_SANITIZERS=`echo ${SANITIZERS[@]} | grep -w $2` + if [[ -z ${IS_SANITIZERS} ]]; then + echo "ERROR: unknown sanitizer type \"$2\"" + usage_build + exit 1 + fi + SANITIZER="$2" + shift + ;; -m|--test_module) if [ "$ONLY_BUILD" == "YES" ]; then echo "ERROR: unknown option \"$key\"" @@ -195,7 +211,7 @@ function run_build() fi C_COMPILER="$C_COMPILER" CXX_COMPILER="$CXX_COMPILER" BUILD_TYPE="$BUILD_TYPE" \ ONLY_BUILD="$ONLY_BUILD" CLEAR="$CLEAR" JOB_NUM="$JOB_NUM" \ - BOOST_DIR="$BOOST_DIR" ENABLE_GCOV="$ENABLE_GCOV" \ + BOOST_DIR="$BOOST_DIR" ENABLE_GCOV="$ENABLE_GCOV" SANITIZER="$SANITIZER" \ RUN_VERBOSE="$RUN_VERBOSE" TEST_MODULE="$TEST_MODULE" NO_TEST="$NO_TEST" \ DISABLE_GPERF="$DISABLE_GPERF" $scripts_dir/build.sh } diff --git a/scripts/linux/build.sh b/scripts/linux/build.sh index d70ad48417..f36a0097f1 100755 --- a/scripts/linux/build.sh +++ b/scripts/linux/build.sh @@ -89,6 +89,14 @@ else echo "DISABLE_GPERF=NO" fi +if [ ! -z "$SANITIZER" ] +then + echo "SANITIZER=$SANITIZER" + CMAKE_OPTIONS="$CMAKE_OPTIONS -DSANITIZER=$SANITIZER" +else + echo "Build without sanitizer" +fi + # You can specify customized boost by defining BOOST_DIR. # Install boost like this: # wget http://downloads.sourceforge.net/project/boost/boost/1.54.0/boost_1_54_0.zip?r=&ts=1442891144&use_mirror=jaist