Skip to content

Commit 1e33c74

Browse files
alvinhochuntstellar
authored andcommitted
[clang][MinGW] Add asan DLL lib before other libs and objects
As stated in #61685, by passing LLD the import lib of the asan DLL first, the asan DLL will be listed as the first entry in the Import Directory Table, making it be loaded first before other user DLLs. This allows asan to be initialized as early as possible to increase its instrumentation coverage to include other DLLs not built with asan. This also avoids some false asan reports on `realloc` for memory allocated during initialization of user DLLs being loaded earlier than asan, because after this change they will be loaded later than asan. Differential Revision: https://reviews.llvm.org/D146908 (cherry picked from commit 81358e9)
1 parent e59e0b9 commit 1e33c74

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

clang/lib/Driver/ToolChains/MinGW.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
200200
Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
201201
Args.AddLastArg(CmdArgs, options::OPT_Z_Flag);
202202

203+
// Add asan_dynamic as the first import lib before other libs. This allows
204+
// asan to be initialized as early as possible to increase its instrumentation
205+
// coverage to include other user DLLs which has not been built with asan.
206+
if (Sanitize.needsAsanRt() && !Args.hasArg(options::OPT_nostdlib) &&
207+
!Args.hasArg(options::OPT_nodefaultlibs)) {
208+
// MinGW always links against a shared MSVCRT.
209+
CmdArgs.push_back(
210+
TC.getCompilerRTArgString(Args, "asan_dynamic", ToolChain::FT_Shared));
211+
}
212+
203213
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
204214
if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_mdll)) {
205215
CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("dllcrt2.o")));

clang/test/Driver/mingw-sanitizers.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
// RUN: %clang -target i686-windows-gnu %s -### -fsanitize=address 2>&1 | FileCheck --check-prefix=ASAN-I686 %s
2-
// ASAN-I686: "{{.*}}libclang_rt.asan_dynamic-i386.dll.a"
3-
// ASAN-I686: "{{[^"]*}}libclang_rt.asan_dynamic_runtime_thunk-i386.a"
4-
// ASAN-I686: "--require-defined" "___asan_seh_interceptor"
5-
// ASAN-I686: "--whole-archive" "{{.*}}libclang_rt.asan_dynamic_runtime_thunk-i386.a" "--no-whole-archive"
6-
7-
// RUN: %clang -target x86_64-windows-gnu %s -### -fsanitize=address 2>&1 | FileCheck --check-prefix=ASAN-X86_64 %s
8-
// ASAN-X86_64: "{{.*}}libclang_rt.asan_dynamic-x86_64.dll.a"
1+
// RUN: touch %t.a
2+
// RUN: %clang -target i686-windows-gnu %s -### -fsanitize=address -lcomponent %/t.a 2>&1 | FileCheck --check-prefixes=ASAN-ALL,ASAN-I686 -DINPUT=%/t.a %s
3+
// RUN: %clang -target x86_64-windows-gnu %s -### -fsanitize=address -lcomponent %/t.a 2>&1 | FileCheck --check-prefixes=ASAN-ALL,ASAN-X86_64 -DINPUT=%/t.a %s
4+
//
5+
// ASAN-ALL-NOT:"-l{{[^"]+"]}}"
6+
// ASAN-ALL-NOT:"[[INPUT]]"
7+
// ASAN-I686: "{{[^"]*}}libclang_rt.asan_dynamic-i386.dll.a"
8+
// ASAN-X86_64: "{{[^"]*}}libclang_rt.asan_dynamic-x86_64.dll.a"
9+
// ASAN-ALL: "-lcomponent"
10+
// ASAN-ALL: "[[INPUT]]"
11+
// ASAN-I686: "{{[^"]*}}libclang_rt.asan_dynamic-i386.dll.a"
12+
// ASAN-I686: "{{[^"]*}}libclang_rt.asan_dynamic_runtime_thunk-i386.a"
13+
// ASAN-I686: "--require-defined" "___asan_seh_interceptor"
14+
// ASAN-I686: "--whole-archive" "{{[^"]*}}libclang_rt.asan_dynamic_runtime_thunk-i386.a" "--no-whole-archive"
15+
// ASAN-X86_64: "{{[^"]*}}libclang_rt.asan_dynamic-x86_64.dll.a"
916
// ASAN-X86_64: "{{[^"]*}}libclang_rt.asan_dynamic_runtime_thunk-x86_64.a"
1017
// ASAN-X86_64: "--require-defined" "__asan_seh_interceptor"
11-
// ASAN-X86_64: "--whole-archive" "{{.*}}libclang_rt.asan_dynamic_runtime_thunk-x86_64.a" "--no-whole-archive"
18+
// ASAN-X86_64: "--whole-archive" "{{[^"]*}}libclang_rt.asan_dynamic_runtime_thunk-x86_64.a" "--no-whole-archive"
1219

1320
// RUN: %clang -target x86_64-windows-gnu %s -### -fsanitize=vptr

0 commit comments

Comments
 (0)