Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Enable ASAN, TSAN, USAN and LSAN
  • Loading branch information
romainthomas committed Sep 28, 2017
1 parent 051a452 commit 7f6aeb0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
52 changes: 52 additions & 0 deletions CMakeLists.txt
Expand Up @@ -60,6 +60,12 @@ option(LIEF_ELF "Build LIEF with ELF module" ON)
option(LIEF_PE "Build LIEF with PE module" ON)
option(LIEF_MACHO "Build LIEF with MachO module" ON)

# Sanitizer
option(LIEF_ASAN "Enable Address sanitizer" OFF)
option(LIEF_LSAN "Enable Leak sanitizer" OFF)
option(LIEF_TSAN "Enable Thread sanitizer" OFF)
option(LIEF_USAN "Enable undefined sanitizer" OFF)


# CCACHE
# ======
Expand Down Expand Up @@ -389,6 +395,52 @@ add_dependencies(LIB_LIEF_SHARED lief_mbed_tls)
set_property(TARGET LIB_LIEF_STATIC LIB_LIEF_SHARED PROPERTY CXX_STANDARD 11)
set_property(TARGET LIB_LIEF_STATIC LIB_LIEF_SHARED PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET LIB_LIEF_STATIC LIB_LIEF_SHARED PROPERTY POSITION_INDEPENDENT_CODE ON)
set(SANITIZER_FLAGS -fno-omit-frame-pointer -g -O1)

if (LIEF_ASAN)
message(STATUS "Address sanitizer enabled")
list(APPEND SANITIZER_FLAGS -fsanitize=address)

set_property(TARGET LIB_LIEF_STATIC LIB_LIEF_SHARED APPEND PROPERTY LINK_FLAGS -fsanitize=address)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
endif()


if (LIEF_LSAN)
message(STATUS "Leak sanitizer enabled")
list(APPEND SANITIZER_FLAGS -fsanitize=leak)

set_property(TARGET LIB_LIEF_STATIC LIB_LIEF_SHARED APPEND PROPERTY LINK_FLAGS -fsanitize=leak)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=leak")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=leak")
endif()


if (LIEF_TSAN)
message(STATUS "Thread sanitizer enabled")
list(APPEND SANITIZER_FLAGS -fsanitize=thread)

set_property(TARGET LIB_LIEF_STATIC LIB_LIEF_SHARED APPEND PROPERTY LINK_FLAGS -fsanitize=thread)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=thread")
endif()


if (LIEF_USAN)
message(STATUS "Undefined sanitizer enabled")
list(APPEND SANITIZER_FLAGS -fsanitize=undefined)

set_property(TARGET LIB_LIEF_STATIC LIB_LIEF_SHARED APPEND PROPERTY LINK_FLAGS -fsanitize=undefined)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=undefined")
endif()

if (LIEF_ASAN OR LIEF_LSAN OR LIEF_TSAN OR LIEF_USAN)
#message(STATUS "${SANITIZER_FLAGS}")
target_compile_options(LIB_LIEF_STATIC PRIVATE ${SANITIZER_FLAGS})
target_compile_options(LIB_LIEF_SHARED PRIVATE ${SANITIZER_FLAGS})
endif()


CHECK_CXX_COMPILER_FLAG("-Weverything" HAS_WARN_EVERY)
Expand Down
33 changes: 30 additions & 3 deletions api/python/CMakeLists.txt
Expand Up @@ -137,13 +137,40 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(pyLIEF PRIVATE ${PYLIEF_COMPILE_FLAGS})
endif()

#set_target_properties(pyLIEF PROPERTIES PREFIX "" OUTPUT_NAME "lief")

if (LIEF_ASAN)
set_property(TARGET pyLIEF APPEND PROPERTY LINK_FLAGS -fsanitize=address)
set(PYLIEF_DEPS_LIBRARIES ${PYLIEF_DEPS_LIBRARIES} asan)
endif()


if (LIEF_LSAN)
set_property(TARGET pyLIEF APPEND PROPERTY LINK_FLAGS -fsanitize=leak)
set(PYLIEF_DEPS_LIBRARIES ${PYLIEF_DEPS_LIBRARIES} lsan)
endif()


if (LIEF_TSAN)
set_property(TARGET pyLIEF APPEND PROPERTY LINK_FLAGS -fsanitize=thread)
set(PYLIEF_DEPS_LIBRARIES ${PYLIEF_DEPS_LIBRARIES} tsan)
endif()


if (LIEF_USAN)
#set_property(TARGET pyLIEF APPEND PROPERTY LINK_FLAGS -fsanitize=undefined)
set(PYLIEF_DEPS_LIBRARIES ${PYLIEF_DEPS_LIBRARIES} ubsan)
endif()

#if (LIEF_ASAN OR LIEF_LSAN OR LIEF_TSAN OR LIEF_USAN)
# target_compile_options(pyLIEF PRIVATE ${SANITIZER_FLAGS})
#endif()

set_target_properties(pyLIEF PROPERTIES PREFIX "" OUTPUT_NAME "_pylief")
add_dependencies(pyLIEF lief_pybind11)

if(APPLE)
set_target_properties(pyLIEF PROPERTIES MACOSX_RPATH ".")
set_target_properties(pyLIEF PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
set_target_properties(pyLIEF APPEND PROPERTIES MACOSX_RPATH ".")
set_target_properties(pyLIEF APPEND PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
endif()

set_target_properties(pyLIEF PROPERTIES PREFIX "")
Expand Down

0 comments on commit 7f6aeb0

Please sign in to comment.