[PATCH] D51879: [ASan] [MinGW] Avoid including headers for functions that will be redefined

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 10 12:27:38 PDT 2018


mstorsjo created this revision.
mstorsjo added reviewers: rnk, timurrrr.
Herald added subscribers: Sanitizers, kubamracek.

With mingw-w64 headers, including windows.h ends up including stdlib.h and malloc.h implicitly, with the following path:

compiler-rt/lib/asan/asan_malloc_win.cc
x86_64-w64-mingw32/include/windows.h
x86_64-w64-mingw32/include/windef.h
x86_64-w64-mingw32/include/minwindef.h
x86_64-w64-mingw32/include/winnt.h
lib/clang/8.0.0/include/x86intrin.h
lib/clang/8.0.0/include/immintrin.h
lib/clang/8.0.0/include/xmmintrin.h
lib/clang/8.0.0/include/mm_malloc.h
x86_64-w64-mingw32/include/c++/v1/stdlib.h
x86_64-w64-mingw32/include/stdlib.h

These headers include declarations of functions like e.g. free(), without the dllexport attribute, but other included headers also include inline functions that reference free().

If these functions have been declared before (either with or without dllimport), we can still redeclare them with dllexport, this only yields a warning. However, if they have been declared without dllimport and referenced by an inline function in the headers, it has already been emitted to IR and we can't add the dllexport attribute afterwards.

By undefining __STDC_HOSTED__, we avoid xmmintrin.h including mm_malloc.h.


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D51879

Files:
  lib/asan/asan_malloc_win.cc


Index: lib/asan/asan_malloc_win.cc
===================================================================
--- lib/asan/asan_malloc_win.cc
+++ lib/asan/asan_malloc_win.cc
@@ -12,6 +12,16 @@
 // Windows-specific malloc interception.
 //===----------------------------------------------------------------------===//
 
+#ifdef __MINGW32__
+// mingw-w64 winnt.h pulls in x86intrin.h, which can pull in mm_malloc.h
+// if __STDC_HOSTED__ is defined. mm_malloc.h includes stdlib.h and malloc.h,
+// which include conflicting declarations of these functions. Having earlier
+// declarations with or without dllimport produces warnings, but if an
+// inline function in a header referenced one of the functions, we no longer
+// can redeclare them with dllexport below.
+#undef __STDC_HOSTED__
+#endif
+
 #include "sanitizer_common/sanitizer_platform.h"
 #if SANITIZER_WINDOWS
 #define WIN32_LEAN_AND_MEAN


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51879.164719.patch
Type: text/x-patch
Size: 894 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180910/0288eb82/attachment.bin>


More information about the llvm-commits mailing list