[build] Add LeakSanitizer option to CMake (#158686)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/158686
Approved by: https://github.com/eellison
This commit is contained in:
Benjamin Glass
2025-09-09 15:09:58 +00:00
committed by PyTorch MergeBot
parent af60398c3a
commit bdbe931d58
5 changed files with 22 additions and 3 deletions

View File

@ -234,6 +234,7 @@ cmake_dependent_option(INSTALL_TEST "Install test binaries if BUILD_TEST is on"
option(USE_CPP_CODE_COVERAGE "Compile C/C++ with code coverage flags" OFF) option(USE_CPP_CODE_COVERAGE "Compile C/C++ with code coverage flags" OFF)
option(USE_COLORIZE_OUTPUT "Colorize output during compilation" ON) option(USE_COLORIZE_OUTPUT "Colorize output during compilation" ON)
option(USE_ASAN "Use Address+Undefined Sanitizers" OFF) option(USE_ASAN "Use Address+Undefined Sanitizers" OFF)
option(USE_LSAN "Use Leak Sanitizer" OFF)
option(USE_TSAN "Use Thread Sanitizer" OFF) option(USE_TSAN "Use Thread Sanitizer" OFF)
option(USE_CUDA "Use CUDA" ON) option(USE_CUDA "Use CUDA" ON)
option(USE_XPU "Use XPU" ON) option(USE_XPU "Use XPU" ON)

View File

@ -1830,6 +1830,12 @@ if(BUILD_TEST)
target_link_libraries(${test_name}_${CPU_CAPABILITY} Sanitizer::undefined) target_link_libraries(${test_name}_${CPU_CAPABILITY} Sanitizer::undefined)
endif() endif()
endif() endif()
if(USE_LSAN AND TARGET Sanitizer::leak)
target_link_libraries(${test_name}_${CPU_CAPABILITY} Sanitizer::leak)
endif()
if(USE_TSAN AND TARGET Sanitizer::thread)
target_link_libraries(${test_name}_${CPU_CAPABILITY} Sanitizer::thread)
endif()
else() else()
add_executable(${test_name}_${CPU_CAPABILITY} "${test_src}") add_executable(${test_name}_${CPU_CAPABILITY} "${test_src}")
target_link_libraries(${test_name}_${CPU_CAPABILITY} torch_library sleef gtest_main) target_link_libraries(${test_name}_${CPU_CAPABILITY} torch_library sleef gtest_main)

View File

@ -108,24 +108,32 @@ if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO AND NOT INTERN_BUILD_MOBILE)
enable_ubsan() enable_ubsan()
endif() endif()
if(USE_ASAN OR USE_TSAN) if(USE_ASAN OR USE_LSAN OR USE_TSAN)
find_package(Sanitizer REQUIRED) find_package(Sanitizer REQUIRED)
if(USE_ASAN) if(USE_ASAN)
if(TARGET Sanitizer::address) if(TARGET Sanitizer::address)
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::address) list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::address)
else() else()
message(WARNING "Not ASAN found. Suppress this warning with -DUSE_ASAN=OFF.") message(WARNING "ASAN not found. Suppress this warning with -DUSE_ASAN=OFF.")
caffe2_update_option(USE_ASAN OFF) caffe2_update_option(USE_ASAN OFF)
endif() endif()
if(TARGET Sanitizer::undefined) if(TARGET Sanitizer::undefined)
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::undefined) list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::undefined)
endif() endif()
endif() endif()
if(USE_LSAN)
if(TARGET Sanitizer::leak)
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::leak)
else()
message(WARNING "LSAN not found. Suppress this warning with -DUSE_LSAN=OFF.")
caffe2_update_option(USE_LSAN OFF)
endif()
endif()
if(USE_TSAN) if(USE_TSAN)
if(TARGET Sanitizer::thread) if(TARGET Sanitizer::thread)
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::thread) list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::thread)
else() else()
message(WARNING "Not TSAN found. Suppress this warning with -DUSE_TSAN=OFF.") message(WARNING "TSAN not found. Suppress this warning with -DUSE_TSAN=OFF.")
caffe2_update_option(USE_TSAN OFF) caffe2_update_option(USE_TSAN OFF)
endif() endif()
endif() endif()

View File

@ -66,6 +66,7 @@ function(caffe2_print_configuration_summary)
message(STATUS " LAPACK : ${LAPACK_INFO}") message(STATUS " LAPACK : ${LAPACK_INFO}")
endif() endif()
message(STATUS " USE_ASAN : ${USE_ASAN}") message(STATUS " USE_ASAN : ${USE_ASAN}")
message(STATUS " USE_LSAN : ${USE_LSAN}")
message(STATUS " USE_TSAN : ${USE_TSAN}") message(STATUS " USE_TSAN : ${USE_TSAN}")
message(STATUS " USE_CPP_CODE_COVERAGE : ${USE_CPP_CODE_COVERAGE}") message(STATUS " USE_CPP_CODE_COVERAGE : ${USE_CPP_CODE_COVERAGE}")
message(STATUS " USE_CUDA : ${USE_CUDA}") message(STATUS " USE_CUDA : ${USE_CUDA}")

View File

@ -95,6 +95,9 @@ endif()
if(USE_ASAN AND TARGET Sanitizer::undefined) if(USE_ASAN AND TARGET Sanitizer::undefined)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES Sanitizer::undefined) list(APPEND TORCH_PYTHON_LINK_LIBRARIES Sanitizer::undefined)
endif() endif()
if(USE_LSAN AND TARGET Sanitizer::leak)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES Sanitizer::leak)
endif()
if(USE_TSAN AND TARGET Sanitizer::thread) if(USE_TSAN AND TARGET Sanitizer::thread)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES Sanitizer::thread) list(APPEND TORCH_PYTHON_LINK_LIBRARIES Sanitizer::thread)
endif() endif()