Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

build: add sanitizer support #359

Merged
merged 15 commits into from
Dec 18, 2019
14 changes: 14 additions & 0 deletions bin/dsn.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ endfunction()
function(dsn_setup_compiler_flags)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DDSN_BUILD_TYPE=Debug)
#for sanitizer
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
add_definitions(-g)
else()
add_definitions(-g)
add_definitions(-O2)
Expand Down Expand Up @@ -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}")
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
18 changes: 17 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type> 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\","
Expand All @@ -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
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
SANITIZERS=("address" "leak" "thread" "undefined")

C_COMPILER="gcc"
CXX_COMPILER="g++"
BUILD_TYPE="release"
Expand All @@ -76,6 +81,7 @@ function run_build()
DISABLE_GPERF=NO
SKIP_THIRDPARTY=NO
CHECK=NO
SANITIZER=""
TEST_MODULE=""
while [[ $# > 0 ]]; do
key="$1"
Expand Down Expand Up @@ -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\""
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions scripts/linux/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down