[llvm-commits] [compiler-rt] r151715 - /compiler-rt/trunk/lib/asan/asan_malloc_win.cc

Timur Iskhodzhanov timurrrr at google.com
Wed Feb 29 03:43:03 PST 2012


Author: timurrrr
Date: Wed Feb 29 05:43:03 2012
New Revision: 151715

URL: http://llvm.org/viewvc/llvm-project?rev=151715&view=rev
Log:
[ASan] Replace CRT .dll malloc with our implementation at asan_init() time

Modified:
    compiler-rt/trunk/lib/asan/asan_malloc_win.cc

Modified: compiler-rt/trunk/lib/asan/asan_malloc_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_malloc_win.cc?rev=151715&r1=151714&r2=151715&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_malloc_win.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_malloc_win.cc Wed Feb 29 05:43:03 2012
@@ -18,12 +18,7 @@
 #include "asan_internal.h"
 #include "asan_stack.h"
 
-namespace __asan {
-void ReplaceSystemMalloc() {
-  // FIXME: investigate whether any action is needed.
-  // Currenlty, we fail to intercept malloc() called from intercepted _strdup().
-}
-}  // namespace __asan
+#include "interception/interception.h"
 
 // ---------------------- Replacement functions ---------------- {{{1
 using namespace __asan;  // NOLINT
@@ -55,4 +50,37 @@
 }
 }  // extern "C"
 
+using __interception::GetRealFunctionAddress;
+
+// We don't want to include "windows.h" in this file to avoid extra attributes
+// set on malloc/free etc (e.g. dllimport), so declare a few things manually:
+extern "C" int __stdcall VirtualProtect(void* addr, size_t size,
+                                        DWORD prot, DWORD *old_prot);
+const int PAGE_EXECUTE_READWRITE = 0x40;
+
+namespace __asan {
+void ReplaceSystemMalloc() {
+#ifdef _WIN64
+# error ReplaceSystemMalloc was not tested on x64
+#endif
+  char *crt_malloc;
+  if (GetRealFunctionAddress("malloc", (void**)&crt_malloc)) {
+    // Replace malloc in the CRT dll with a jump to our malloc.
+    DWORD old_prot, unused;
+    CHECK(VirtualProtect(crt_malloc, 16, PAGE_EXECUTE_READWRITE, &old_prot));
+    REAL(memset)(crt_malloc, 0xCC /* int 3 */, 16);  // just in case.
+
+    uintptr_t jmp_offset = (intptr_t)malloc - (intptr_t)crt_malloc - 5;
+    crt_malloc[0] = 0xE9;  // jmp, should be followed by an offset.
+    REAL(memcpy)(crt_malloc + 1, &jmp_offset, sizeof(jmp_offset));
+
+    CHECK(VirtualProtect(crt_malloc, 16, old_prot, &unused));
+
+    // FYI: FlushInstructionCache is needed on Itanium etc but not on x86/x64.
+  }
+
+  // FIXME: investigate whether anything else is needed.
+}
+}  // namespace __asan
+
 #endif  // _WIN32





More information about the llvm-commits mailing list