Skip to content

Commit 0a93e9f

Browse files
committed
Revert "[asan][windows] Eliminate the static asan runtime on windows (#93770)"
This reverts commit 8fa66c6.
1 parent 3af717d commit 0a93e9f

File tree

56 files changed

+1069
-1291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1069
-1291
lines changed

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -909,16 +909,10 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
909909
DiagnoseErrors);
910910
}
911911

912-
SharedRuntime = Args.hasFlag(
913-
options::OPT_shared_libsan, options::OPT_static_libsan,
914-
TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia() ||
915-
TC.getTriple().isOSDarwin() || TC.getTriple().isOSWindows());
916-
if (!SharedRuntime && TC.getTriple().isOSWindows()) {
917-
Arg *A =
918-
Args.getLastArg(options::OPT_shared_libsan, options::OPT_static_libsan);
919-
D.Diag(clang::diag::err_drv_unsupported_opt_for_target)
920-
<< A->getSpelling() << TC.getTriple().str();
921-
}
912+
SharedRuntime =
913+
Args.hasFlag(options::OPT_shared_libsan, options::OPT_static_libsan,
914+
TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia() ||
915+
TC.getTriple().isOSDarwin());
922916

923917
ImplicitCfiRuntime = TC.getTriple().isAndroid();
924918

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
201201
if (TC.getSanitizerArgs(Args).needsAsanRt()) {
202202
CmdArgs.push_back(Args.MakeArgString("-debug"));
203203
CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
204-
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dynamic"));
205-
auto defines = Args.getAllArgValues(options::OPT_D);
206-
if (Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd) ||
207-
find(begin(defines), end(defines), "_DLL") != end(defines)) {
204+
if (TC.getSanitizerArgs(Args).needsSharedRt() ||
205+
Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd)) {
206+
for (const auto &Lib : {"asan_dynamic", "asan_dynamic_runtime_thunk"})
207+
CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
208208
// Make sure the dynamic runtime thunk is not optimized out at link time
209209
// to ensure proper SEH handling.
210210
CmdArgs.push_back(Args.MakeArgString(
@@ -213,15 +213,19 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
213213
: "-include:__asan_seh_interceptor"));
214214
// Make sure the linker consider all object files from the dynamic runtime
215215
// thunk.
216-
CmdArgs.push_back(Args.MakeArgString(
217-
std::string("-wholearchive:") +
216+
CmdArgs.push_back(Args.MakeArgString(std::string("-wholearchive:") +
218217
TC.getCompilerRT(Args, "asan_dynamic_runtime_thunk")));
218+
} else if (DLL) {
219+
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dll_thunk"));
219220
} else {
220-
// Make sure the linker consider all object files from the static runtime
221-
// thunk.
222-
CmdArgs.push_back(Args.MakeArgString(
223-
std::string("-wholearchive:") +
224-
TC.getCompilerRT(Args, "asan_static_runtime_thunk")));
221+
for (const auto &Lib : {"asan", "asan_cxx"}) {
222+
CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
223+
// Make sure the linker consider all object files from the static lib.
224+
// This is necessary because instrumented dlls need access to all the
225+
// interface exported by the static lib in the main executable.
226+
CmdArgs.push_back(Args.MakeArgString(std::string("-wholearchive:") +
227+
TC.getCompilerRT(Args, Lib)));
228+
}
225229
}
226230
}
227231

clang/test/Driver/cl-link.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
// ASAN: link.exe
1414
// ASAN: "-debug"
1515
// ASAN: "-incremental:no"
16-
// ASAN: "{{[^"]*}}clang_rt.asan_dynamic.lib"
17-
// ASAN: "-wholearchive:{{.*}}clang_rt.asan_static_runtime_thunk.lib"
16+
// ASAN: "{{[^"]*}}clang_rt.asan.lib"
17+
// ASAN: "-wholearchive:{{.*}}clang_rt.asan.lib"
18+
// ASAN: "{{[^"]*}}clang_rt.asan_cxx.lib"
19+
// ASAN: "-wholearchive:{{.*}}clang_rt.asan_cxx.lib"
1820
// ASAN: "{{.*}}cl-link{{.*}}.obj"
1921

2022
// RUN: %clang_cl -m32 -arch:IA32 --target=i386-pc-win32 /MD /Tc%s -fuse-ld=link -### -fsanitize=address 2>&1 | FileCheck --check-prefix=ASAN-MD %s
2123
// ASAN-MD: link.exe
2224
// ASAN-MD: "-debug"
2325
// ASAN-MD: "-incremental:no"
2426
// ASAN-MD: "{{.*}}clang_rt.asan_dynamic.lib"
27+
// ASAN-MD: "{{[^"]*}}clang_rt.asan_dynamic_runtime_thunk.lib"
2528
// ASAN-MD: "-include:___asan_seh_interceptor"
2629
// ASAN-MD: "-wholearchive:{{.*}}clang_rt.asan_dynamic_runtime_thunk.lib"
2730
// ASAN-MD: "{{.*}}cl-link{{.*}}.obj"
@@ -37,8 +40,7 @@
3740
// ASAN-DLL: "-dll"
3841
// ASAN-DLL: "-debug"
3942
// ASAN-DLL: "-incremental:no"
40-
// ASAN-DLL: "{{.*}}clang_rt.asan_dynamic.lib"
41-
// ASAN-DLL: "-wholearchive:{{.*}}clang_rt.asan_static_runtime_thunk.lib"
43+
// ASAN-DLL: "{{.*}}clang_rt.asan_dll_thunk.lib"
4244
// ASAN-DLL: "{{.*}}cl-link{{.*}}.obj"
4345

4446
// RUN: %clang_cl /Zi /Tc%s -fuse-ld=link -### 2>&1 | FileCheck --check-prefix=DEBUG %s

compiler-rt/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,8 @@ if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
378378
endif()
379379

380380
if(MSVC)
381-
382-
# asan on windows only supports the release dll version of the runtimes, in the interest of
383-
# only having one asan dll to support/test. Having asan statically linked
384-
# with the runtime might be possible, but it multiplies the number of scenerios to test.
385-
# the program USING sanitizers can use whatever version of the runtime it wants to.
386-
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
381+
# FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214.
382+
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
387383

388384
# Remove any /M[DT][d] flags, and strip any definitions of _DEBUG.
389385
# Since we're using CMAKE_MSVC_RUNTIME_LIBRARY (CMP0091 set to NEW),

compiler-rt/lib/asan/CMakeLists.txt

Lines changed: 77 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ set(ASAN_SOURCES
3232
asan_win.cpp
3333
)
3434

35-
if(WIN32)
36-
set(ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES
37-
asan_globals_win.cpp
38-
asan_win_common_runtime_thunk.cpp
39-
asan_win_dynamic_runtime_thunk.cpp
40-
)
41-
set(ASAN_STATIC_RUNTIME_THUNK_SOURCES
42-
asan_globals_win.cpp
43-
asan_malloc_win_thunk.cpp
44-
asan_win_common_runtime_thunk.cpp
45-
asan_win_static_runtime_thunk.cpp
46-
)
47-
endif()
48-
4935
if (NOT WIN32 AND NOT APPLE)
5036
list(APPEND ASAN_SOURCES
5137
asan_interceptors_vfork.S
@@ -150,7 +136,7 @@ append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
150136
add_compiler_rt_object_libraries(RTAsan_dynamic
151137
OS ${SANITIZER_COMMON_SUPPORTED_OS}
152138
ARCHS ${ASAN_SUPPORTED_ARCH}
153-
SOURCES ${ASAN_SOURCES}
139+
SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
154140
ADDITIONAL_HEADERS ${ASAN_HEADERS}
155141
CFLAGS ${ASAN_DYNAMIC_CFLAGS}
156142
DEFS ${ASAN_DYNAMIC_DEFINITIONS})
@@ -235,52 +221,46 @@ else()
235221
RTSanitizerCommonSymbolizerInternal
236222
RTLSanCommon
237223
RTUbsan)
238-
if (NOT WIN32)
239-
add_compiler_rt_runtime(clang_rt.asan
240-
STATIC
241-
ARCHS ${ASAN_SUPPORTED_ARCH}
242-
OBJECT_LIBS RTAsan_preinit
243-
RTAsan
244-
${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
245-
CFLAGS ${ASAN_CFLAGS}
246-
DEFS ${ASAN_COMMON_DEFINITIONS}
247-
PARENT_TARGET asan)
248224

249-
add_compiler_rt_runtime(clang_rt.asan_cxx
250-
STATIC
251-
ARCHS ${ASAN_SUPPORTED_ARCH}
252-
OBJECT_LIBS RTAsan_cxx
253-
RTUbsan_cxx
254-
CFLAGS ${ASAN_CFLAGS}
255-
DEFS ${ASAN_COMMON_DEFINITIONS}
256-
PARENT_TARGET asan)
225+
add_compiler_rt_runtime(clang_rt.asan
226+
STATIC
227+
ARCHS ${ASAN_SUPPORTED_ARCH}
228+
OBJECT_LIBS RTAsan_preinit
229+
RTAsan
230+
${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
231+
CFLAGS ${ASAN_CFLAGS}
232+
DEFS ${ASAN_COMMON_DEFINITIONS}
233+
PARENT_TARGET asan)
257234

258-
add_compiler_rt_runtime(clang_rt.asan_static
259-
STATIC
260-
ARCHS ${ASAN_SUPPORTED_ARCH}
261-
OBJECT_LIBS RTAsan_static
262-
CFLAGS ${ASAN_CFLAGS}
263-
DEFS ${ASAN_COMMON_DEFINITIONS}
264-
PARENT_TARGET asan)
235+
add_compiler_rt_runtime(clang_rt.asan_cxx
236+
STATIC
237+
ARCHS ${ASAN_SUPPORTED_ARCH}
238+
OBJECT_LIBS RTAsan_cxx
239+
RTUbsan_cxx
240+
CFLAGS ${ASAN_CFLAGS}
241+
DEFS ${ASAN_COMMON_DEFINITIONS}
242+
PARENT_TARGET asan)
265243

266-
add_compiler_rt_runtime(clang_rt.asan-preinit
267-
STATIC
268-
ARCHS ${ASAN_SUPPORTED_ARCH}
269-
OBJECT_LIBS RTAsan_preinit
270-
CFLAGS ${ASAN_CFLAGS}
271-
DEFS ${ASAN_COMMON_DEFINITIONS}
272-
PARENT_TARGET asan)
273-
endif()
244+
add_compiler_rt_runtime(clang_rt.asan_static
245+
STATIC
246+
ARCHS ${ASAN_SUPPORTED_ARCH}
247+
OBJECT_LIBS RTAsan_static
248+
CFLAGS ${ASAN_CFLAGS}
249+
DEFS ${ASAN_COMMON_DEFINITIONS}
250+
PARENT_TARGET asan)
251+
252+
add_compiler_rt_runtime(clang_rt.asan-preinit
253+
STATIC
254+
ARCHS ${ASAN_SUPPORTED_ARCH}
255+
OBJECT_LIBS RTAsan_preinit
256+
CFLAGS ${ASAN_CFLAGS}
257+
DEFS ${ASAN_COMMON_DEFINITIONS}
258+
PARENT_TARGET asan)
274259

275260
foreach(arch ${ASAN_SUPPORTED_ARCH})
276261
if (COMPILER_RT_HAS_VERSION_SCRIPT)
277-
if(WIN32)
278-
set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch})
279-
else()
280-
set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch})
281-
endif()
282262
add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
283-
LIBS ${SANITIZER_RT_VERSION_LIST_LIBS}
263+
LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
284264
EXTRA asan.syms.extra)
285265
set(VERSION_SCRIPT_FLAG
286266
-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
@@ -298,11 +278,25 @@ else()
298278
endif()
299279

300280
set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
281+
if (WIN32)
282+
add_compiler_rt_object_libraries(AsanWeakInterception
283+
${SANITIZER_COMMON_SUPPORTED_OS}
284+
ARCHS ${arch}
285+
SOURCES
286+
asan_win_weak_interception.cpp
287+
CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DYNAMIC
288+
DEFS ${ASAN_COMMON_DEFINITIONS})
289+
set(ASAN_DYNAMIC_WEAK_INTERCEPTION
290+
AsanWeakInterception
291+
UbsanWeakInterception
292+
SancovWeakInterception
293+
SanitizerCommonWeakInterception)
294+
endif()
295+
301296
add_compiler_rt_runtime(clang_rt.asan
302297
SHARED
303298
ARCHS ${arch}
304299
OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
305-
RTAsan_cxx
306300
RTAsan_dynamic
307301
# The only purpose of RTAsan_dynamic_version_script_dummy is to
308302
# carry a dependency of the shared runtime on the version script.
@@ -330,48 +324,49 @@ else()
330324
endif()
331325

332326
if (WIN32)
327+
add_compiler_rt_object_libraries(AsanDllThunk
328+
${SANITIZER_COMMON_SUPPORTED_OS}
329+
ARCHS ${arch}
330+
SOURCES asan_globals_win.cpp
331+
asan_win_dll_thunk.cpp
332+
CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DLL_THUNK
333+
DEFS ${ASAN_COMMON_DEFINITIONS})
334+
335+
add_compiler_rt_runtime(clang_rt.asan_dll_thunk
336+
STATIC
337+
ARCHS ${arch}
338+
OBJECT_LIBS AsanDllThunk
339+
UbsanDllThunk
340+
SancovDllThunk
341+
SanitizerCommonDllThunk
342+
SOURCES $<TARGET_OBJECTS:RTInterception.${arch}>
343+
PARENT_TARGET asan)
344+
333345
set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
346+
if(MSVC)
347+
list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
348+
elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
349+
list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
350+
endif()
334351

335352
add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
336353
${SANITIZER_COMMON_SUPPORTED_OS}
337354
ARCHS ${arch}
338-
SOURCES ${ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES}
355+
SOURCES asan_globals_win.cpp
356+
asan_win_dynamic_runtime_thunk.cpp
339357
CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
340358
DEFS ${ASAN_COMMON_DEFINITIONS})
341359

342360
add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
343361
STATIC
344362
ARCHS ${arch}
345363
OBJECT_LIBS AsanDynamicRuntimeThunk
346-
UbsanRuntimeThunk
347-
SancovRuntimeThunk
348-
SanitizerRuntimeThunk
364+
UbsanDynamicRuntimeThunk
365+
SancovDynamicRuntimeThunk
366+
SanitizerCommonDynamicRuntimeThunk
349367
CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
350368
DEFS ${ASAN_COMMON_DEFINITIONS}
351369
PARENT_TARGET asan)
352-
353-
# mingw does not support static linkage of the CRT
354-
if(NOT MINGW)
355-
set(STATIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_STATIC_RUNTIME_THUNK")
356-
357-
add_compiler_rt_object_libraries(AsanStaticRuntimeThunk
358-
${SANITIZER_COMMON_SUPPORTED_OS}
359-
ARCHS ${arch}
360-
SOURCES ${ASAN_STATIC_RUNTIME_THUNK_SOURCES}
361-
CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
362-
DEFS ${ASAN_DYNAMIC_DEFINITIONS})
363-
364-
add_compiler_rt_runtime(clang_rt.asan_static_runtime_thunk
365-
STATIC
366-
ARCHS ${arch}
367-
OBJECT_LIBS AsanStaticRuntimeThunk
368-
UbsanRuntimeThunk
369-
SancovRuntimeThunk
370-
SanitizerRuntimeThunk
371-
CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
372-
DEFS ${ASAN_DYNAMIC_DEFINITIONS}
373-
PARENT_TARGET asan)
374-
endif()
375370
endif()
376371
endforeach()
377372
endif()

0 commit comments

Comments
 (0)