aminya / project_options

A general-purpose CMake library that provides functions that improve the CMake experience following the best practices.

Home Page:https://aminya.github.io/project_options/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v0.24.0, Visual Studio 2022 linker doesn't understand sanitizers?

hihig2001 opened this issue · comments

#135

I think it is related with this.
After I cleaned up my out folder, build reports this kind of error

  Cleaning... 0 files.
  [1/14] Building CXX object fuzz_test\CMakeFiles\fuzz_tester.dir\fuzz_tester.cpp.obj
E:\work\Knuckles\Game\out\build\windows-msvc-debug-hard-mode\cl : Command line warning D9002: ignoring unknown option '/fsanitize=fuzzer'
  [2/14] Linking CXX executable fuzz_test\fuzz_tester.exe
  FAILED: fuzz_test/fuzz_tester.exe 
  cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=fuzz_test\CMakeFiles\fuzz_tester.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests  -- C:\PROGRA~2\MICROS~4\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\link.exe /nologo fuzz_test\CMakeFiles\fuzz_tester.dir\fuzz_tester.cpp.obj  /out:fuzz_test\fuzz_tester.exe /implib:fuzz_test\fuzz_tester.lib /pdb:fuzz_test\fuzz_tester.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console  /fsanitize=address /fsanitize=fuzzer -LIBPATH:C:\PROGRA~2\MICROS~4\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\ATLMFC\lib\x64   -LIBPATH:C:\PROGRA~2\MICROS~4\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\lib\x64   -LIBPATH:C:\PROGRA~2\MICROS~4\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\lib\x86\store\REFERE~1   -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100190~1.0\ucrt\x64   -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100190~1.0\um\x64   -LIBPATH:"C:\Program Files (x86)\Windows Kits\10\References\x64" vcpkg_installed\x64-windows\debug\lib\fmtd.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D E:\work\Knuckles\Game\out\build\windows-msvc-debug-hard-mode\fuzz_test && C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Bypass -file C:/dev/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary E:/work/Knuckles/Game/out/build/windows-msvc-debug-hard-mode/fuzz_test/fuzz_tester.exe -installedDir E:/work/Knuckles/Game/out/build/windows-msvc-debug-hard-mode/vcpkg_installed/x64-windows/debug/bin -OutVariable out""
  LINK Pass 1: command "C:\PROGRA~2\MICROS~4\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\link.exe /nologo fuzz_test\CMakeFiles\fuzz_tester.dir\fuzz_tester.cpp.obj /out:fuzz_test\fuzz_tester.exe /implib:fuzz_test\fuzz_tester.lib /pdb:fuzz_test\fuzz_tester.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console /fsanitize=address /fsanitize=fuzzer -LIBPATH:C:\PROGRA~2\MICROS~4\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\ATLMFC\lib\x64 -LIBPATH:C:\PROGRA~2\MICROS~4\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\lib\x64 -LIBPATH:C:\PROGRA~2\MICROS~4\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\lib\x86\store\REFERE~1 -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100190~1.0\ucrt\x64 -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100190~1.0\um\x64 -LIBPATH:C:\Program Files (x86)\Windows Kits\10\References\x64 vcpkg_installed\x64-windows\debug\lib\fmtd.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:fuzz_test\CMakeFiles\fuzz_tester.dir/intermediate.manifest fuzz_test\CMakeFiles\fuzz_tester.dir/manifest.res" failed (exit code 1120) with the following output:
E:\work\Knuckles\Game\out\build\windows-msvc-debug-hard-mode\LINK : warning LNK4044: unrecognized option '/fsanitize=address'; ignored
E:\work\Knuckles\Game\out\build\windows-msvc-debug-hard-mode\LINK : warning LNK4044: unrecognized option '/fsanitize=fuzzer'; ignored
E:\work\Knuckles\Game\out\build\windows-msvc-debug-hard-mode\LINK : warning LNK4300: ignoring '/INCREMENTAL' because input module contains ASAN metadata
E:\work\Knuckles\Game\out\build\windows-msvc-debug-hard-mode\LINK : warning LNK4044: unrecognized option '/fsanitize=address'; ignored
E:\work\Knuckles\Game\out\build\windows-msvc-debug-hard-mode\LINK : warning LNK4044: unrecognized option '/fsanitize=fuzzer'; ignored
     Creating library fuzz_test\fuzz_tester.lib and object fuzz_test\fuzz_tester.exp
E:\work\Knuckles\Game\out\build\windows-msvc-debug-hard-mode\LIBCMTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
E:\work\Knuckles\Game\out\build\windows-msvc-debug-hard-mode\fuzz_test\fuzz_tester.exe : fatal error LNK1120: 1 unresolved externals
  [3/14] Building CXX object knuckles\test\constexpr\CMakeFiles\my_lib_relaxed_constexpr_tests.dir\constexpr_tests.cpp.obj
  [4/14] Building CXX object knuckles\test\CMakeFiles\my_lib_tests.dir\tests.cpp.obj
  [5/14] Building CXX object console_app\test\CMakeFiles\my_exe_helpers_tests.dir\tests.cpp.obj
  [6/14] Building CXX object knuckles\test\constexpr\CMakeFiles\my_lib_constexpr_tests.dir\constexpr_tests.cpp.obj
  [7/14] Building CXX object console_app\CMakeFiles\knuckles_exe.dir\src\main.cpp.obj
  [8/14] Building CXX object knuckles\CMakeFiles\knuckles_lib.dir\src\lib.cpp.obj

I don't know what is my environment has wrong though...
it's all good if I downgrade 0.21.1

Visual Studio only has the address sanitizers and doesn't support the fuzzer. As far as I remember, we skipped the fuzzer on Windows. Are you adding it yourself?

https://docs.microsoft.com/lv-lv/cpp/build/reference/fsanitize?view=msvc-170

libfuzzer has supported since VS2022 17.0.
also clang-cl supports its if i directly link some of libraries except debug build.
So I added this lines of code to my fuzz test cmake.
This was correctly built until 0.21.1.

if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
    if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
        #https://stackoverflow.com/questions/66531482/application-crashes-when-using-address-sanitizer-with-msvc
        set_property(TARGET fuzz_tester PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
        target_compile_options(fuzz_tester PRIVATE /fsanitize=address /fsanitize=fuzzer)
        target_link_options(fuzz_tester PRIVATE /fsanitize=address /fsanitize=fuzzer)
    elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
        if (CMAKE_BUILD_TYPE MATCHES "Debug")
            message(WARNING "This is not supported!")
        else()
            #https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake
            #http://ekse.github.io/fuzzing/2019/08/07/libfuzzer-windows-cmake.html
            #https://patricia.no/2020/12/01/trying-asan-on-windows-in-cmake-in-clion.html
            set_property(TARGET fuzz_tester PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
            target_compile_definitions(fuzz_tester PRIVATE WASM_FUZZING)
            target_link_directories(fuzz_tester PUBLIC "$ENV{VSINSTALLDIR}VC/Tools/Llvm/x64/lib/clang/${CMAKE_CXX_COMPILER_VERSION}/lib/windows")
            target_link_libraries(fuzz_tester PUBLIC
                clang_rt.asan-preinit-x86_64
                clang_rt.asan-x86_64
                clang_rt.asan_cxx-x86_64
                clang_rt.fuzzer-x86_64
            )
            target_compile_options(fuzz_tester PRIVATE -fsanitize=fuzzer,undefined,address)
            target_link_options(fuzz_tester PRIVATE -coverage -fsanitize=fuzzer,undefined,address)
        endif()
    endif()
else()
    if (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
        target_compile_options(fuzz_tester PRIVATE -fsanitize=fuzzer,undefined,address)
        target_link_options(fuzz_tester PRIVATE -coverage -fsanitize=fuzzer,undefined,address)
    else()
        message(WARNING "This is not supported!")
    endif()
endif()

hi is there are something i can help you to understand the problem better other than above comment?

Your code is not correct. You are passing unsupported link options to the linker (e.g. /fsanitize=fuzzer). This is not caused by project_options, it is because of your custom code.

Oh I see I added unnecessary linker options. After I removed unnecessary linker line It worked great! Thank you!