Skip to content

Fix references to required libraries when building LLVM with ASAN and MultiThreaded[Debug] on Windows #139657

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

Merged
merged 2 commits into from
Jun 20, 2025

Conversation

dgg5503
Copy link
Contributor

@dgg5503 dgg5503 commented May 13, 2025

After #81677, statically linking ASAN under Windows is no longer supported. Therefore, when using Clang built past 53a81d4 to build LLVM / Clang with -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded[Debug] -DLLVM_USE_SANITIZER=Address, a different set of dependent libraries must be linked. This is mentioned in the description of #81677 and also in https://devblogs.microsoft.com/cppblog/msvc-address-sanitizer-one-dll-for-all-runtime-configurations/.

@llvmbot llvmbot added the cmake Build system in general and CMake in particular label May 13, 2025
@dgg5503 dgg5503 requested review from barcharcraz and vitalybuka May 13, 2025 14:16
Copy link
Collaborator

@vitalybuka vitalybuka left a comment

Choose a reason for hiding this comment

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

LGTM, but please wait for @barcharcraz

@dgg5503
Copy link
Contributor Author

dgg5503 commented May 16, 2025

Ping @barcharcraz

3 similar comments
@dgg5503
Copy link
Contributor Author

dgg5503 commented May 21, 2025

Ping @barcharcraz

@dgg5503
Copy link
Contributor Author

dgg5503 commented May 27, 2025

Ping @barcharcraz

@dgg5503
Copy link
Contributor Author

dgg5503 commented Jun 2, 2025

Ping @barcharcraz

@dgg5503
Copy link
Contributor Author

dgg5503 commented Jun 9, 2025

Ping @barcharcraz -- @vitalybuka , would it be OK to merge this in if I do not get a response this week from @barcharcraz ?

@barcharcraz
Copy link
Contributor

I have seen your pings and will look today :)

@barcharcraz
Copy link
Contributor

lgtm

but I question if we really need to be messing with the linker flags here in the first place. This is exactly what the clang driver does if you link through it with /fsanitize=address or what link.exe does when it sees the asan directive.

@dgg5503
Copy link
Contributor Author

dgg5503 commented Jun 13, 2025

lgtm

but I question if we really need to be messing with the linker flags here in the first place. This is exactly what the clang driver does if you link through it with /fsanitize=address or what link.exe does when it sees the asan directive.

Thanks for the review @barcharcraz !

I will verify this some time next week.

@dgg5503
Copy link
Contributor Author

dgg5503 commented Jun 16, 2025

Hi @barcharcraz ,

I re-reviewed these changes and can confirm they are still necessary in the case of building LLVM / Clang with -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded[Debug] -DLLVM_USE_SANITIZER=Address -- at least when building using Clang-CL and lld-link from the latest Clang / LLVM binary package (20.1.7).

but I question if we really need to be messing with the linker flags here in the first place. This is exactly what the clang driver does if you link through it with /fsanitize=address or what link.exe does when it sees the asan directive.

It indeed does look like the appropriate libraries will be added automatically as per:

if (TC.getSanitizerArgs(Args).needsAsanRt()) {
CmdArgs.push_back(Args.MakeArgString("-debug"));
CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dynamic"));
auto defines = Args.getAllArgValues(options::OPT_D);
if (Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd) ||
llvm::is_contained(defines, "_DLL")) {
// Make sure the dynamic runtime thunk is not optimized out at link time
// to ensure proper SEH handling.
CmdArgs.push_back(Args.MakeArgString(
TC.getArch() == llvm::Triple::x86
? "-include:___asan_seh_interceptor"
: "-include:__asan_seh_interceptor"));
// Make sure the linker consider all object files from the dynamic runtime
// thunk.
CmdArgs.push_back(Args.MakeArgString(
std::string("-wholearchive:") +
TC.getCompilerRT(Args, "asan_dynamic_runtime_thunk")));
} else {
// Make sure the linker consider all object files from the static runtime
// thunk.
CmdArgs.push_back(Args.MakeArgString(
std::string("-wholearchive:") +
TC.getCompilerRT(Args, "asan_static_runtime_thunk")));
}
}

However, at least from the CMake options I'm using and my build environment, it appears that linking is invoked directly via the linker rather than linking via the compiler driver invoking the linker. Therefore, unless there's a CMake flag or some other bits of configuration I may be overlooking (please let me know if that is the case), it seems like this change is required.

I will wait a few more days before merging to give you time to reply in-case there's extra discussion that may be worth having.

Thanks again for reviewing, sorry if my pings were a bit annoying 😅...

@dgg5503 dgg5503 merged commit 71e20c6 into llvm:main Jun 20, 2025
7 checks passed
@dgg5503 dgg5503 deleted the dgg5503/main/windows-asan-dll branch June 20, 2025 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants