提交 e1b5833b 编写于 作者: K Krzysztof Binias 提交者: Tao Luo

[PROPOSAL] Add support for dynamic code analysis (Sanitizers) (#18303)

* Add support for dynamic code analysis (Sanitizers)
test=develop

* Move options to one option

test=develop

* Missing check

test=develop
上级 233746d8
......@@ -73,6 +73,7 @@ option(WITH_HIGH_LEVEL_API_TEST "Test fluid python high-level api interface"
option(PY_VERSION "Compile PaddlePaddle with python3 support" ${PY_VERSION})
option(WITH_FAST_MATH "Make use of fast math library, might affect the precision to some extent" ON)
option(WITH_DGC "Use DGC(Deep Gradient Compression) or not" ON)
option(SANITIZER_TYPE "Choose the type of sanitizer, options are: Address, Leak, Memory, Thread, Undefined" OFF)
# PY_VERSION
if(NOT PY_VERSION)
......@@ -121,6 +122,12 @@ endif()
if (REPLACE_ENFORCE_GLOG)
add_definitions("-DREPLACE_ENFORCE_GLOG")
endif()
if (SANITIZER_TYPE AND NOT "${SANITIZER_TYPE}" MATCHES "^(Address|Leak|Memory|Thread|Undefined)$")
message("Choose the correct type of sanitizer")
return()
endif()
########################################################################################
include(external/mklml) # download mklml package
......
......@@ -37,6 +37,12 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
function(safe_set_flag is_c src_list flag_name)
string(REPLACE "-" "_" safe_name ${flag_name})
string(REPLACE "=" "_" safe_name ${safe_name})
if(${flag_name} MATCHES "fsanitize")
set(CMAKE_REQUIRED_FLAGS_RETAINED ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS ${flag_name})
endif()
if(is_c)
CHECK_C_COMPILER_FLAG(${flag_name} C_COMPILER_SUPPORT_FLAG_${safe_name})
set(safe_name C_COMPILER_SUPPORT_FLAG_${safe_name})
......@@ -47,6 +53,10 @@ function(safe_set_flag is_c src_list flag_name)
if(${safe_name})
set(${src_list} "${${src_list}} ${flag_name}" PARENT_SCOPE)
endif()
if(${flag_name} MATCHES "fsanitize")
set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_RETAINED})
endif()
endfunction()
# helper macro to set cflag
......@@ -108,6 +118,20 @@ if(BARRIER_FOUND)
endif(BARRIER_FOUND)
SET(CMAKE_EXTRA_INCLUDE_FILES "")
# Only one sanitizer is allowed in compile time
string(TOLOWER "${SANITIZER_TYPE}" sanitizer_type)
if(sanitizer_type STREQUAL "address")
set(fsanitize "-fsanitize=address")
elseif(sanitizer_type STREQUAL "leak")
set(fsanitize "-fsanitize=leak")
elseif(sanitizer_type STREQUAL "memory")
set(fsanitize "-fsanitize=memory")
elseif(sanitizer_type STREQUAL "thread")
set(fsanitize "-fsanitize=thread")
elseif(sanitizer_type STREQUAL "undefined")
set(fsanitize "-fsanitize=undefined")
endif()
# Common flags. the compiler flag used for C/C++ sources whenever release or debug
# Do not care if this flag is support for gcc.
......@@ -132,6 +156,7 @@ set(COMMON_FLAGS
-Wno-error=int-in-bool-context # Warning in Eigen gcc 7.2
-Wimplicit-fallthrough=0 # Warning in tinyformat.h
-Wno-error=maybe-uninitialized # Warning in boost gcc 7.2
${fsanitize}
)
set(GPU_COMMON_FLAGS
......@@ -173,7 +198,6 @@ endif(UNIX AND NOT APPLE)
foreach(flag ${COMMON_FLAGS})
safe_set_cflag(CMAKE_C_FLAGS ${flag})
safe_set_cxxflag(CMAKE_CXX_FLAGS ${flag})
endforeach()
foreach(flag ${GPU_COMMON_FLAGS})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册