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
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ if(ENABLE_GCOV)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgcov")
endif()

if(DEFINED SANITIZER)
if(NOT ("${COMPILER_FAMILY}" STREQUAL "gcc" AND "${COMPILER_VERSION}" VERSION_GREATER "4.8"))
message(SEND_ERROR "Cannot use sanitizer without gcc >= 4.8")
endif()
message(STATUS "Running cmake with sanitizer=${SANITIZER}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -g -fsanitize=${SANITIZER} -fno-omit-frame-pointer")
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1 -g -fsanitize=${SANITIZER} -fno-omit-frame-pointer")
endif()

# Users don't have to configure CMAKE_INSTALL_PREFIX unless they want to customize
# the destination.
set(CMAKE_INSTALL_PREFIX ${DSN_ROOT} CACHE STRING "" FORCE)
Expand Down
17 changes: 16 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ 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 problem: address, leak, thread, undefined etc."
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 +64,9 @@ function usage_build()
}
function run_build()
{
#Note: No memory 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 +80,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 +137,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 +210,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 "Use sanitizer to build: sanitizer=$SANITIZER"
CMAKE_OPTIONS="$CMAKE_OPTIONS -DSANITIZER=$SANITIZER"
else
echo "No use sanitizer to build"
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
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