@@ -32,6 +32,20 @@ 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+
3549if (NOT WIN32 AND NOT APPLE )
3650 list (APPEND ASAN_SOURCES
3751 asan_interceptors_vfork.S
@@ -83,7 +97,13 @@ SET(ASAN_HEADERS
8397 )
8498
8599include_directories (..)
86-
100+ if (MSVC )
101+ # asan on windows only supports the release dll version of the runtimes, in the interest of
102+ # only having one asan dll to support/test. Having asan statically linked
103+ # with the runtime might be possible, but it multiplies the number of scenerios to test.
104+ # the program USING sanitizers can use whatever version of the runtime it wants to.
105+ set (CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
106+ endif ()
87107set (ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS} )
88108
89109append_list_if(MSVC /Zl ASAN_CFLAGS)
@@ -117,7 +137,11 @@ append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
117137set (ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS} )
118138append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
119139 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
120- append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
140+
141+ # LLVM turns /OPT:ICF back on when LLVM_ENABLE_PDBs is set
142+ # we _REALLY_ need to turn it back off for ASAN, because the way
143+ # asan emulates weak functions from DLLs requires NOICF
144+ append_list_if(MSVC "/DEBUG;/OPT:NOICF" ASAN_DYNAMIC_LINK_FLAGS)
121145
122146set (ASAN_DYNAMIC_LIBS
123147 ${COMPILER_RT_UNWINDER_LINK_LIBS}
@@ -136,7 +160,7 @@ append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
136160add_compiler_rt_object_libraries(RTAsan_dynamic
137161 OS ${SANITIZER_COMMON_SUPPORTED_OS}
138162 ARCHS ${ASAN_SUPPORTED_ARCH}
139- SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
163+ SOURCES ${ASAN_SOURCES}
140164 ADDITIONAL_HEADERS ${ASAN_HEADERS}
141165 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
142166 DEFS ${ASAN_DYNAMIC_DEFINITIONS} )
@@ -221,46 +245,52 @@ else()
221245 RTSanitizerCommonSymbolizerInternal
222246 RTLSanCommon
223247 RTUbsan)
248+ if (NOT WIN32 )
249+ add_compiler_rt_runtime(clang_rt.asan
250+ STATIC
251+ ARCHS ${ASAN_SUPPORTED_ARCH}
252+ OBJECT_LIBS RTAsan_preinit
253+ RTAsan
254+ ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
255+ CFLAGS ${ASAN_CFLAGS}
256+ DEFS ${ASAN_COMMON_DEFINITIONS}
257+ PARENT_TARGET asan)
224258
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)
234-
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)
259+ add_compiler_rt_runtime(clang_rt.asan_cxx
260+ STATIC
261+ ARCHS ${ASAN_SUPPORTED_ARCH}
262+ OBJECT_LIBS RTAsan_cxx
263+ RTUbsan_cxx
264+ CFLAGS ${ASAN_CFLAGS}
265+ DEFS ${ASAN_COMMON_DEFINITIONS}
266+ PARENT_TARGET asan)
243267
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)
268+ add_compiler_rt_runtime(clang_rt.asan_static
269+ STATIC
270+ ARCHS ${ASAN_SUPPORTED_ARCH}
271+ OBJECT_LIBS RTAsan_static
272+ CFLAGS ${ASAN_CFLAGS}
273+ DEFS ${ASAN_COMMON_DEFINITIONS}
274+ PARENT_TARGET asan)
251275
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)
276+ add_compiler_rt_runtime(clang_rt.asan-preinit
277+ STATIC
278+ ARCHS ${ASAN_SUPPORTED_ARCH}
279+ OBJECT_LIBS RTAsan_preinit
280+ CFLAGS ${ASAN_CFLAGS}
281+ DEFS ${ASAN_COMMON_DEFINITIONS}
282+ PARENT_TARGET asan)
283+ endif ()
259284
260285 foreach (arch ${ASAN_SUPPORTED_ARCH} )
261286 if (COMPILER_RT_HAS_VERSION_SCRIPT)
287+ if (WIN32 )
288+ set (SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch} )
289+ else ()
290+ set (SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch} )
291+ endif ()
262292 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
263- LIBS clang_rt.asan- ${arch} clang_rt.asan_cxx- ${arch }
293+ LIBS ${SANITIZER_RT_VERSION_LIST_LIBS }
264294 EXTRA asan.syms.extra)
265295 set (VERSION_SCRIPT_FLAG
266296 -Wl,--version -script,${CMAKE_CURRENT_BINARY_DIR} /clang_rt.asan-dynamic-${arch} .vers)
@@ -278,25 +308,11 @@ else()
278308 endif ()
279309
280310 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-
296311 add_compiler_rt_runtime(clang_rt.asan
297312 SHARED
298313 ARCHS ${arch}
299314 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
315+ RTAsan_cxx
300316 RTAsan_dynamic
301317 # The only purpose of RTAsan_dynamic_version_script_dummy is to
302318 # carry a dependency of the shared runtime on the version script.
@@ -324,49 +340,48 @@ else()
324340 endif ()
325341
326342 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-
345343 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 ()
351344
352345 add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
353346 ${SANITIZER_COMMON_SUPPORTED_OS}
354347 ARCHS ${arch}
355- SOURCES asan_globals_win.cpp
356- asan_win_dynamic_runtime_thunk.cpp
348+ SOURCES ${ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES}
357349 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
358350 DEFS ${ASAN_COMMON_DEFINITIONS} )
359351
360352 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
361353 STATIC
362354 ARCHS ${arch}
363355 OBJECT_LIBS AsanDynamicRuntimeThunk
364- UbsanDynamicRuntimeThunk
365- SancovDynamicRuntimeThunk
366- SanitizerCommonDynamicRuntimeThunk
356+ UbsanRuntimeThunk
357+ SancovRuntimeThunk
358+ SanitizerRuntimeThunk
367359 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
368360 DEFS ${ASAN_COMMON_DEFINITIONS}
369361 PARENT_TARGET asan)
362+
363+ # mingw does not support static linkage of the CRT
364+ if (NOT MINGW)
365+ set (STATIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_STATIC_RUNTIME_THUNK" )
366+
367+ add_compiler_rt_object_libraries(AsanStaticRuntimeThunk
368+ ${SANITIZER_COMMON_SUPPORTED_OS}
369+ ARCHS ${arch}
370+ SOURCES ${ASAN_STATIC_RUNTIME_THUNK_SOURCES}
371+ CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
372+ DEFS ${ASAN_DYNAMIC_DEFINITIONS} )
373+
374+ add_compiler_rt_runtime(clang_rt.asan_static_runtime_thunk
375+ STATIC
376+ ARCHS ${arch}
377+ OBJECT_LIBS AsanStaticRuntimeThunk
378+ UbsanRuntimeThunk
379+ SancovRuntimeThunk
380+ SanitizerRuntimeThunk
381+ CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
382+ DEFS ${ASAN_DYNAMIC_DEFINITIONS}
383+ PARENT_TARGET asan)
384+ endif ()
370385 endif ()
371386 endforeach ()
372387endif ()
0 commit comments