Skip to content

Commit fa32500

Browse files
committed
Add sanitizer support for clang/mac
1 parent 3dcc63e commit fa32500

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,18 @@ test-py: ## Clean and Make unit tests
9191
python -m pytest -v csp/tests --junitxml=junit.xml $(TEST_ARGS)
9292

9393
test-py-sanitizer: ## Clean and Make unit tests with sanitizers enabled
94-
ASAN_OPTIONS=detect_leaks=0,detect_stack_use_after_return=true,use_odr_indicator=1,strict_init_order=true,strict_string_checks=true LD_PRELOAD=$$(gcc -print-file-name=libasan.so) \
95-
python -m pytest -v csp/tests --junitxml=junit.xml $(TEST_ARGS)
94+
@if [ "$$(uname -s)" = "Darwin" ]; then \
95+
ASAN_OPTIONS=detect_leaks=0,detect_stack_use_after_return=true,use_odr_indicator=1,strict_init_order=true,strict_string_checks=true \
96+
DYLD_INSERT_LIBRARIES=$$($(CXX) -print-file-name=libclang_rt.asan_osx_dynamic.dylib) \
97+
python -m pytest -v csp/tests --junitxml=junit.xml $(TEST_ARGS); \
98+
elif [ "$$(uname -s)" = "Linux" ]; then \
99+
ASAN_OPTIONS=detect_leaks=0,detect_stack_use_after_return=true,use_odr_indicator=1,strict_init_order=true,strict_string_checks=true \
100+
LD_PRELOAD=$$($(CXX) -print-file-name=libasan.so) \
101+
python -m pytest -v csp/tests --junitxml=junit.xml $(TEST_ARGS); \
102+
else \
103+
echo "Unsupported platform: $$(uname -s)"; \
104+
exit 1; \
105+
fi
96106

97107
test-cpp: ## Make C++ unit tests
98108
ifneq ($(OS),Windows_NT)

cpp/cmake/modules/Findcsp_autogen.cmake

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,25 @@ function(csp_autogen MODULE_NAME DEST_FILENAME HEADER_NAME_OUTVAR SOURCE_NAME_OU
2727
endif()
2828

2929
if(CSP_ENABLE_ASAN)
30-
execute_process(
31-
COMMAND gcc -print-file-name=libasan.so
32-
OUTPUT_VARIABLE ASAN_LIB_PATH
33-
OUTPUT_STRIP_TRAILING_WHITESPACE
34-
)
35-
# Turn off leak checks as we are using PyMalloc when we run autogen
36-
set(ASAN_PRELOAD_CMD "ASAN_OPTIONS=detect_leaks=0" "LD_PRELOAD=${ASAN_LIB_PATH}")
30+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
31+
# Clang - use DYLD_INSERT_LIBRARIES
32+
execute_process(
33+
COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libclang_rt.asan_osx_dynamic.dylib
34+
OUTPUT_VARIABLE ASAN_LIB_PATH
35+
OUTPUT_STRIP_TRAILING_WHITESPACE
36+
)
37+
set(PRELOAD_CMD "DYLD_INSERT_LIBRARIES=${ASAN_LIB_PATH}")
38+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
39+
# GCC - use LD_PRELOAD
40+
execute_process(
41+
COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libasan.so
42+
OUTPUT_VARIABLE ASAN_LIB_PATH
43+
OUTPUT_STRIP_TRAILING_WHITESPACE
44+
)
45+
set(PRELOAD_CMD "LD_PRELOAD=${ASAN_LIB_PATH}")
46+
endif()
47+
# Turn off leak checks as we are using PyMalloc when we run autogen
48+
set(ASAN_PRELOAD_CMD "ASAN_OPTIONS=detect_leaks=0" ${PRELOAD_CMD})
3749
else()
3850
set(ASAN_PRELOAD_CMD "")
3951
endif()

0 commit comments

Comments
 (0)