Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake code to run ASAN for Windows tests #463

Merged
merged 17 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(IS_DEBUG_BUILD ON)
endif()

option(HIDAPI_ENABLE_ASAN "Build HIDAPI with ASAN address sanitizer instrumentation" ON)
Youw marked this conversation as resolved.
Show resolved Hide resolved

if(HIDAPI_ENABLE_ASAN)
add_compile_options(-fsanitize=address)
Youw marked this conversation as resolved.
Show resolved Hide resolved
endif()

if(WIN32)
# so far only Windows has tests
option(HIDAPI_WITH_TESTS "Build HIDAPI (unit-)tests" ${IS_DEBUG_BUILD})
Expand Down
5 changes: 5 additions & 0 deletions windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ set(SOURCES
hidapi_hidsdi.h
)

if(HIDAPI_ENABLE_ASAN)
# Option /MTd needed to embedd ASAN library
add_compile_options(/MTd)
endif()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW: in my other project(s) I didn't it. for ASAN

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static linking is not needed in general, you could als add the ASAN DLL, that comes in various flavors with VisualStudio, to your DLL search path.
But it's placed in a directory with a name like this:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29333\bin\Hostx64\x86\clang_rt.asan_dbg_dynamic-i386.dll
I'm not able to determine this path using CMake. It depends on many things like VisualStudio installation path, Visual Studio version and the build type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is rather easy to get this path:

  • it is MSVC-specitic (so unser if(MSVC));
  • as per documentation - on MSVC the support is limited to x86 and x64 on Windows 10, so only one of two possible suffixes: i386 or x86_64;
  • the location is documented and it matches the path to CMAKE_C_COMPILER directory;

But in any case - I don't believe we need to handle that in our CMake. ASAN runs are designed to be executed only in development builds (e.g. no "production" scenarios).
The runtime is always available from MSVC development sonsole, so I believe that is just enough.


if(BUILD_SHARED_LIBS)
list(APPEND SOURCES hidapi.rc)
endif()
Expand Down
3 changes: 3 additions & 0 deletions windows/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ foreach(TEST_CASE ${HID_DESCRIPTOR_RECONSTRUCT_TEST_CASES})
COMMAND hid_report_reconstructor_test "${TEST_PP_DATA}" "${TEST_EXPECTED_DESCRIPTOR}"
WORKING_DIRECTORY "$<TARGET_FILE_DIR:hidapi_winapi>"
)
if(HIDAPI_ENABLE_ASAN)
set_property(TEST "WinHidReportReconstructTest_${TEST_CASE}" PROPERTY ENVIRONMENT "ASAN_SAVE_DUMPS=AsanDump_${TEST_CASE}.dmp")
endif()
endforeach()