Skip to content

Commit

Permalink
feat: Apply ASAN compile/link options on dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
zchrissirhcz committed Dec 12, 2024
1 parent 78206b9 commit 76fc76d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
45 changes: 34 additions & 11 deletions rocbuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -74,17 +74,17 @@ 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()
set(${OUTPUT_VARIABLE} ${VISITED_TARGETS} PARENT_SCOPE)
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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()


Expand Down
10 changes: 7 additions & 3 deletions tests/asan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
40 changes: 40 additions & 0 deletions tests/asan/matrix.hpp
Original file line number Diff line number Diff line change
@@ -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;
};
14 changes: 14 additions & 0 deletions tests/asan/test_matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "matrix.hpp"
#include <stdio.h>

int main()
{
Matrix m(2, 2);
m(0, 0) = 1;

Matrix m2 = m;

printf("bye\n");

return 0;
}

0 comments on commit 76fc76d

Please sign in to comment.