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

使用clang-cl并开启asan时,cxxflags里缺少-fsanitize=address,并且clang-cl不会主动链接clang_rt.asan-x86_64 #5728

Closed
Arteiimis opened this issue Oct 17, 2024 · 6 comments
Labels
Milestone

Comments

@Arteiimis
Copy link

Xmake 版本

v2.9.5

操作系统版本和架构

Windows10 22H2

描述问题

使用clang-cl并开启asan时,cxxflags里缺少-fsanitize=address,并且clang-cl不会主动链接clang_rt.asan-x86_64

经过测试,llvm自带的clang_rt.asan-x86_64路径: \path\to\llvm\version\lib\clang\19\lib\windows并不在系统路径中, 直接add_links("clang_rt.asan-x86_64")并不奏效, 并且只链接clang_rt.asan-x86_64, asan功能不完善.

期待的结果

正确处理cxxflags, 并且添加链接

工程配置

target("play_ground")
	set_kind("binary")
	set_rundir("$(projectdir)")
	
	set_policy("build.sanitizer.address", true)
	
	add_languages("c++23")
	add_files("src/*.cpp")
	add_includedirs("src")
	add_cxxflags("-std=c++23")
	
	-- add_linkdirs("D:\\scoop\\apps\\llvm\\19.1.2\\lib\\clang\\19\\lib\\windows")
	-- add_links("clang_rt.asan_dynamic-x86_64", "clang_rt.asan_dynamic_runtime_thunk-x86_64")
	-- add_links("clang_rt.asan-x86_64")

	add_packages(requirements)

	set_toolchains("clang-cl")
	-- set_toolset("ld", "lld-link")
target_end()

附加信息和错误日志

checking for flags (-DNDEBUG) ... ok
[ 60%]: cache compiling.release src\main.cpp
D:\scoop\apps\llvm\current\bin\clang-cl.exe -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.play_ground.pdb -O2 -std:c++20 -std:c++latest -MD -Isrc /EHsc /utf-8 -Xclang -std=c++23 -DNDEBUG -Fobuild\.objs\play_ground\windows\x64\release\src\main.cpp.obj src\main.cpp
checking for flags (-fcolor-diagnostics) ... ok
checking for flags (-fansi-escape-codes) ... ok
[ 60%]: cache compiling.release src\thread_pool.cpp
D:\scoop\apps\llvm\current\bin\clang-cl.exe -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.play_ground.pdb -O2 -std:c++20 -std:c++latest -MD -Isrc /EHsc /utf-8 -Xclang -std=c++23 -DNDEBUG -Fobuild\.objs\play_ground\windows\x64\release\src\thread_pool.cpp.obj src\thread_pool.cpp
[ 80%]: linking.release play_ground.exe
"D:\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.41.34120\\bin\\HostX64\\x64\\link.exe" -nologo -dynamicbase -nxcompat -machine:x64 /opt:ref /opt:icf -debug -pdb:build\windows\x64\release\play_ground.pdb -out:build\windows\x64\release\play_ground.exe build\.objs\play_ground\windows\x64\release\src\main.cpp.obj build\.objs\play_ground\windows\x64\release\src\thread_pool.cpp.obj
checking for clang-cl.exe ... D:\scoop\apps\llvm\current\bin\clang-cl.exe
checking for the c compiler (cc) ... clang-cl.exe
checking for flags (cl_external_includedir) ... ok
create ok!
compile_commands.json updated!
[100%]: build ok, spent 1.563s
@Arteiimis Arteiimis added the bug label Oct 17, 2024
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Title: When using clang-cl and turning on asan, -fsanitize=address is missing in cxxflags, and clang-cl will not actively link clang_rt.asan-x86_64

Xmake version

v2.9.5

Operating system version and architecture

Windows 10 22H2

Describe the problem

When using clang-cl and turning on asan, -fsanitize=address is missing in cxxflags, and clang-cl will not actively link clang_rt.asan-x86_64

After testing, the clang_rt.asan-x86_64 path that comes with llvm: \path\to\llvm\version\lib\clang\19\lib\windows is not in the system path, directly add_links("clang_rt.asan -x86_64") does not work, and only links clang_rt.asan-x86_64, and the asan function is incomplete.

Expected results

Correctly handle cxxflags and add links

Project configuration

target("play_ground")
set_kind("binary")
set_rundir("$(projectdir)")
	
set_policy("build.sanitizer.address", true)
	
add_languages("c++23")
add_files("src/*.cpp")
add_includedirs("src")
add_cxxflags("-std=c++23")
	
-- add_linkdirs("D:\\scoop\\apps\\llvm\\19.1.2\\lib\\clang\\19\\lib\\windows")
-- add_links("clang_rt.asan_dynamic-x86_64", "clang_rt.asan_dynamic_runtime_thunk-x86_64")
-- add_links("clang_rt.asan-x86_64")

add_packages(requirements)

set_toolchains("clang-cl")
-- set_toolset("ld", "lld-link")
target_end()

Additional information and error logs

checking for flags (-DNDEBUG) ... ok
[60%]: cache compiling.release src\main.cpp
D:\scoop\apps\llvm\current\bin\clang-cl.exe -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.play_ground.pdb -O2 -std:c++20 - std:c++latest -MD -Isrc /EHsc /utf-8 -Xclang -std=c++23 -DNDEBUG -Fobuild\.objs\play_ground\windows\x64\release\src\main.cpp.obj src\ main.cpp
checking for flags (-fcolor-diagnostics) ... ok
checking for flags (-fansi-escape-codes) ... ok
[60%]: cache compiling.release src\thread_pool.cpp
D:\scoop\apps\llvm\current\bin\clang-cl.exe -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.play_ground.pdb -O2 -std:c++20 - std:c++latest -MD -Isrc /EHsc /utf-8 -Xclang -std=c++23 -DNDEBUG -Fobuild\.objs\play_ground\windows\x64\release\src\thread_pool.cpp.obj src\ thread_pool.cpp
[80%]: linking.release play_ground.exe
"D:\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.41.34120\\bin\\HostX64\\x64\\link.exe" -nologo -dynamicbase -nxcompat - machine:x64 /opt:ref /opt:icf -debug -pdb:build\windows\x64\release\play_ground.pdb -out:build\windows\x64\release\play_ground.exe build\.objs\play_ground\windows\ x64\release\src\main.cpp.obj build\.objs\play_ground\windows\x64\release\src\thread_pool.cpp.obj
checking for clang-cl.exe ... D:\scoop\apps\llvm\current\bin\clang-cl.exe
checking for the c compiler (cc) ... clang-cl.exe
checking for flags (cl_external_includedir) ... ok
Create OK!
compile_commands.json updated!
[100%]: build ok, spent 1.563s

@star-hengxing
Copy link
Contributor

clang 相关的 toolchain 大概都需要处理
https://stackoverflow.com/questions/76702561/clang-with-address-sanitizer-on-windows-fails-at-link-time

@waruqi
Copy link
Member

waruqi commented Oct 18, 2024

clang 原本就支持,clang-cl 还没处理,只支持了 cl 。。可以提个 pr 过来

if flagname and target:has_tool(sourcekind, "cl", "clang", "clangxx", "gcc", "gxx") then

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


clang originally supported it, clang-cl has not been processed yet, and only cl is supported. . Can you submit a PR?

if flagname and target:has_tool(sourcekind, "cl", "clang", "clangxx", "gcc", "gxx") then

@waruqi
Copy link
Member

waruqi commented Oct 21, 2024

试下这个 patch #5742

clang-cl不会主动链接clang_rt.asan-x86_64

这边测试没这个问题,而且 clang-cl 是编译器 又不是链接器

PS C:\Users\wangrunqing\Downloads\test91> xmake f --toolchain=clang-cl -c
checking for platform ... windows
checking for architecture ... x64
checking for Microsoft Visual Studio (x64) version ... 2022
checking for LLVM Clang C/C++ Compiler (x64) version ... 18.1.6
PS C:\Users\wangrunqing\Downloads\test91> xmake -rv
checking for C:\Program Files\LLVM\bin\clang-cl.exe ... ok
checking for flags (-FS -Fd) ... ok
checking for flags (-O2) ... ok
checking for flags (-DNDEBUG) ... ok
[ 50%]: cache compiling.release src\main.cpp
"C:\\Program Files\\LLVM\\bin\\clang-cl.exe" -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.toytrain.pdb -O2 -std:c++latest /EHsc -fsanitize=address -DNDEBUG -Fobuild\.objs\toytrain\windows\x64\release\src\main.cpp.obj src\main.cpp
checking for flags (-fcolor-diagnostics) ... ok
checking for flags (-fansi-escape-codes) ... ok
[ 75%]: linking.release toytrain.exe
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\VC\\Tools\\MSVC\\14.41.33923\\bin\\HostX64\\x64\\link.exe" -nologo -dynamicbase -nxcompat -machine:x64 /opt:ref /opt:icf -debug -pdb:build\windows\x64\release\toytrain.pdb -fsanitize=address -out:build\windows\x64\release\toytrain.exe build\.objs\toytrain\windows\x64\release\src\main.cpp.obj
checking for clang-cl.exe ... C:\Program Files\LLVM\bin\clang-cl.exe
checking for the c compiler (cc) ... clang-cl.exe
[100%]: build ok, spent 2.547s
PS C:\Users\wangrunqing\Downloads\test91> xmake r
hello world!

@waruqi waruqi added this to the v2.9.6 milestone Oct 21, 2024
@Arteiimis
Copy link
Author

试下这个 patch #5742

clang-cl不会主动链接clang_rt.asan-x86_64

这边测试没这个问题,而且 clang-cl 是编译器 又不是链接器

PS C:\Users\wangrunqing\Downloads\test91> xmake f --toolchain=clang-cl -c
checking for platform ... windows
checking for architecture ... x64
checking for Microsoft Visual Studio (x64) version ... 2022
checking for LLVM Clang C/C++ Compiler (x64) version ... 18.1.6
PS C:\Users\wangrunqing\Downloads\test91> xmake -rv
checking for C:\Program Files\LLVM\bin\clang-cl.exe ... ok
checking for flags (-FS -Fd) ... ok
checking for flags (-O2) ... ok
checking for flags (-DNDEBUG) ... ok
[ 50%]: cache compiling.release src\main.cpp
"C:\\Program Files\\LLVM\\bin\\clang-cl.exe" -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.toytrain.pdb -O2 -std:c++latest /EHsc -fsanitize=address -DNDEBUG -Fobuild\.objs\toytrain\windows\x64\release\src\main.cpp.obj src\main.cpp
checking for flags (-fcolor-diagnostics) ... ok
checking for flags (-fansi-escape-codes) ... ok
[ 75%]: linking.release toytrain.exe
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\VC\\Tools\\MSVC\\14.41.33923\\bin\\HostX64\\x64\\link.exe" -nologo -dynamicbase -nxcompat -machine:x64 /opt:ref /opt:icf -debug -pdb:build\windows\x64\release\toytrain.pdb -fsanitize=address -out:build\windows\x64\release\toytrain.exe build\.objs\toytrain\windows\x64\release\src\main.cpp.obj
checking for clang-cl.exe ... C:\Program Files\LLVM\bin\clang-cl.exe
checking for the c compiler (cc) ... clang-cl.exe
[100%]: build ok, spent 2.547s
PS C:\Users\wangrunqing\Downloads\test91> xmake r
hello world!

没问题了,感谢!

@waruqi waruqi closed this as completed Oct 21, 2024
waruqi added a commit that referenced this issue Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants