Skip to content

Commit 41b09bb

Browse files
authored
[ASAN][sanitizers][win] Allow windows-asan to be built with /MDd and intercept functions from the debug runtimes. (llvm#77840)
It turns out this works _mostly_ fine, even when mixing debug versions of asan with programs built with the release runtime. Using /MT (or /MTd) with a dynamically linked asan has never really worked that well, and I am planning on opening a PR that will completely remove the static-asan configuration for windows and make programs linked with the static CRT/runtime work with the DLL version of asan. This is better than the current situation because the static linked version of asan doesn't work well on windows if there are multiple DLLs in the process using it. The check for building asan with only /MD or /MT has been removed. It was in AsanDoesNotSupportStaticLinkage, but was checking for debug CRTs, not static linkage. The kind of static linkage this function is supposed to check for (on linux for example) doesn't really exist on windows. Note: There is one outstanding issue with this approach, if you mix a /MDd DLLs and /MD dlls in the same process then the "real" function called by asan interceptors will be the same for calls from both contexts, potentially screwing up things like errno. This only happens if you mix /MD and /MDd in the same process, because otherwise asan won't find functions from both runtimes to intercept. We are working on a fix for this, and it mainly hits with the CRT functions exported from both ucrtbase and ntdll. This change is being upstreamed from Microsoft's fork.
1 parent f3d534c commit 41b09bb

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

compiler-rt/lib/asan/asan_win.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,7 @@ void PlatformTSDDtor(void *tsd) { AsanThread::TSDDtor(tsd); }
266266
// }}}
267267

268268
// ---------------------- Various stuff ---------------- {{{
269-
void *AsanDoesNotSupportStaticLinkage() {
270-
#if defined(_DEBUG)
271-
#error Please build the runtime with a non-debug CRT: /MD or /MT
272-
#endif
273-
return 0;
274-
}
269+
void *AsanDoesNotSupportStaticLinkage() { return 0; }
275270

276271
uptr FindDynamicShadowStart() {
277272
return MapDynamicShadow(MemToShadowSize(kHighMemEnd), ASAN_SHADOW_SCALE,

compiler-rt/lib/interception/interception_win.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -945,19 +945,26 @@ bool OverrideFunction(
945945

946946
static void **InterestingDLLsAvailable() {
947947
static const char *InterestingDLLs[] = {
948-
"kernel32.dll",
949-
"msvcr100.dll", // VS2010
950-
"msvcr110.dll", // VS2012
951-
"msvcr120.dll", // VS2013
952-
"vcruntime140.dll", // VS2015
953-
"ucrtbase.dll", // Universal CRT
954-
#if (defined(__MINGW32__) && defined(__i386__))
955-
"libc++.dll", // libc++
956-
"libunwind.dll", // libunwind
957-
#endif
958-
// NTDLL should go last as it exports some functions that we should
959-
// override in the CRT [presumably only used internally].
960-
"ntdll.dll", NULL};
948+
"kernel32.dll",
949+
"msvcr100d.dll", // VS2010
950+
"msvcr110d.dll", // VS2012
951+
"msvcr120d.dll", // VS2013
952+
"vcruntime140d.dll", // VS2015
953+
"ucrtbased.dll", // Universal CRT
954+
"msvcr100.dll", // VS2010
955+
"msvcr110.dll", // VS2012
956+
"msvcr120.dll", // VS2013
957+
"vcruntime140.dll", // VS2015
958+
"ucrtbase.dll", // Universal CRT
959+
# if (defined(__MINGW32__) && defined(__i386__))
960+
"libc++.dll", // libc++
961+
"libunwind.dll", // libunwind
962+
# endif
963+
// NTDLL should go last as it exports some functions that we should
964+
// override in the CRT [presumably only used internally].
965+
"ntdll.dll",
966+
NULL
967+
};
961968
static void *result[ARRAY_SIZE(InterestingDLLs)] = { 0 };
962969
if (!result[0]) {
963970
for (size_t i = 0, j = 0; InterestingDLLs[i]; ++i) {

0 commit comments

Comments
 (0)