diff --git a/rocbuild.cmake b/rocbuild.cmake index 1391782..6d1cd7d 100644 --- a/rocbuild.cmake +++ b/rocbuild.cmake @@ -50,7 +50,7 @@ endmacro() # Transitively list all link libraries of a target (recursive call) # Modified from https://github.com/libigl/libigl/blob/main/cmake/igl/igl_copy_dll.cmake, GPL-3.0 / MPL-2.0 -function(rocbuild_get_dependencies_recursive OUTPUT_VARIABLE TARGET) +function(rocbuild_get_target_dependencies_impl OUTPUT_VARIABLE TARGET) get_target_property(_aliased ${TARGET} ALIASED_TARGET) if(_aliased) set(TARGET ${_aliased}) @@ -74,7 +74,7 @@ function(rocbuild_get_dependencies_recursive OUTPUT_VARIABLE TARGET) if(NOT (DEPENDENCY IN_LIST VISITED_TARGETS)) list(APPEND VISITED_TARGETS ${DEPENDENCY}) - rocbuild_get_dependencies_recursive(VISITED_TARGETS ${DEPENDENCY}) + rocbuild_get_target_dependencies_impl(VISITED_TARGETS ${DEPENDENCY}) endif() endif() endforeach() @@ -82,9 +82,9 @@ function(rocbuild_get_dependencies_recursive OUTPUT_VARIABLE TARGET) endfunction() # Transitively list all link libraries of a target -function(rocbuild_get_dependencies OUTPUT_VARIABLE TARGET) +function(rocbuild_get_target_dependencies OUTPUT_VARIABLE TARGET) set(DISCOVERED_TARGETS "") - rocbuild_get_dependencies_recursive(DISCOVERED_TARGETS ${TARGET}) + rocbuild_get_target_dependencies_impl(DISCOVERED_TARGETS ${TARGET}) set(${OUTPUT_VARIABLE} ${DISCOVERED_TARGETS} PARENT_SCOPE) endfunction() @@ -132,7 +132,7 @@ function(rocbuild_copy_dlls target) ) # Retrieve all target dependencies - rocbuild_get_dependencies(TARGET_DEPENDENCIES ${target}) + rocbuild_get_target_dependencies(TARGET_DEPENDENCIES ${target}) set(DEPENDENCY_FILES "") foreach(DEPENDENCY IN LISTS TARGET_DEPENDENCIES) @@ -305,14 +305,37 @@ function(rocbuild_print_args) endfunction() -function(rocbuild_enable_asan TARGET) +function(rocbuild_target_enable_asan TARGET) + # Retrieve all target dependencies + rocbuild_get_target_dependencies(TARGETS_TO_PROCESS ${TARGET}) + if(MSVC) - target_compile_options(${TARGET} PUBLIC /fsanitize=address) - target_link_options(${TARGET} PUBLIC /ignore:4300) # /INCREMENTAL + set(ASAN_COMPILE_OPTIONS /fsanitize=address) + set(ASAN_LINK_OPTIONS /ignore:4300) # /INCREMENTAL else() - target_compile_options(${TARGET} PUBLIC -fsanitize=address -fno-omit-frame-pointer -g) - target_link_options(${TARGET} PUBLIC -fsanitize=address) - endif() + set(ASAN_COMPILE_OPTIONS -fsanitize=address -fno-omit-frame-pointer -g) + set(ASAN_LINK_OPTIONS -fsanitize=address) + endif() + + # Add TARGET itself to the list of targets to process + list(APPEND TARGETS_TO_PROCESS ${TARGET}) + + foreach(DEPENDENCY IN LISTS TARGETS_TO_PROCESS) + # Skip imported targets + get_target_property(IMPORTED ${DEPENDENCY} IMPORTED) + if(IMPORTED) + continue() + endif() + + get_target_property(TYPE ${DEPENDENCY} TYPE) + if(TYPE STREQUAL "INTERFACE_LIBRARY") + target_compile_options(${DEPENDENCY} INTERFACE ${ASAN_COMPILE_OPTIONS}) + target_link_options(${DEPENDENCY} INTERFACE ${ASAN_LINK_OPTIONS}) + else() + target_compile_options(${DEPENDENCY} PUBLIC ${ASAN_COMPILE_OPTIONS}) + target_link_options(${DEPENDENCY} PUBLIC ${ASAN_LINK_OPTIONS}) + endif() + endforeach() endfunction() diff --git a/tests/asan/CMakeLists.txt b/tests/asan/CMakeLists.txt index 51a8b19..a2f687d 100644 --- a/tests/asan/CMakeLists.txt +++ b/tests/asan/CMakeLists.txt @@ -1,8 +1,12 @@ cmake_minimum_required(VERSION 3.10) -project(asan) +project(test_asan) include(../../rocbuild.cmake) -add_executable(test basic-global-overflow.cpp) +add_executable(basic-global-overflow basic-global-overflow.cpp) +rocbuild_target_enable_asan(basic-global-overflow) -rocbuild_enable_asan(test) +add_library(matrix INTERFACE matrix.hpp) +add_executable(test_matrix test_matrix.cpp) +target_link_libraries(test_matrix PRIVATE matrix) +rocbuild_target_enable_asan(matrix) \ No newline at end of file diff --git a/tests/asan/matrix.hpp b/tests/asan/matrix.hpp new file mode 100644 index 0000000..c4602d9 --- /dev/null +++ b/tests/asan/matrix.hpp @@ -0,0 +1,40 @@ +#pragma once + +class Matrix +{ +public: + Matrix(int rows, int cols) : rows(rows), cols(cols) + { + data = new int[rows * cols]; + } + + ~Matrix() + { + delete[] data; + } + + int& operator()(int i, int j) + { + return data[i * cols + j]; + } + + int operator()(int i, int j) const + { + return data[i * cols + j]; + } + + int getRows() const + { + return rows; + } + + int getCols() const + { + return cols; + } + +private: + int rows; + int cols; + int* data; +}; \ No newline at end of file diff --git a/tests/asan/test_matrix.cpp b/tests/asan/test_matrix.cpp new file mode 100644 index 0000000..2008c4d --- /dev/null +++ b/tests/asan/test_matrix.cpp @@ -0,0 +1,14 @@ +#include "matrix.hpp" +#include + +int main() +{ + Matrix m(2, 2); + m(0, 0) = 1; + + Matrix m2 = m; + + printf("bye\n"); + + return 0; +} \ No newline at end of file