[compiler-rt] r224856 - [ASan/Win] Add basic support for MemoryRangeIsAvailable and DumpProcessMap to make it easier to debug startup shadow mapping failures

Timur Iskhodzhanov timurrrr at google.com
Fri Dec 26 06:28:32 PST 2014


Author: timurrrr
Date: Fri Dec 26 08:28:32 2014
New Revision: 224856

URL: http://llvm.org/viewvc/llvm-project?rev=224856&view=rev
Log:
[ASan/Win] Add basic support for MemoryRangeIsAvailable and DumpProcessMap to make it easier to debug startup shadow mapping failures

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/shadow_mapping_failure.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=224856&r1=224855&r2=224856&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc Fri Dec 26 08:28:32 2014
@@ -20,6 +20,7 @@
 #include <windows.h>
 #include <dbghelp.h>
 #include <io.h>
+#include <psapi.h>
 #include <stdlib.h>
 
 #include "sanitizer_common.h"
@@ -132,8 +133,10 @@ void FlushUnneededShadowMemory(uptr addr
 }
 
 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
-  // FIXME: shall we do anything here on Windows?
-  return true;
+  MEMORY_BASIC_INFORMATION mbi;
+  CHECK(VirtualQuery((void *)range_start, &mbi, sizeof(mbi)));
+  return mbi.Protect & PAGE_NOACCESS &&
+         (uptr)mbi.BaseAddress + mbi.RegionSize >= range_end;
 }
 
 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
@@ -188,7 +191,43 @@ u32 GetUid() {
 }
 
 void DumpProcessMap() {
-  UNIMPLEMENTED();
+  Report("Dumping process modules:\n");
+  HANDLE cur_process = GetCurrentProcess();
+
+  // Query the list of modules.  Start by assuming there are no more than 256
+  // modules and retry if that's not sufficient.
+  HMODULE *modules = 0;
+  uptr modules_buffer_size = sizeof(HMODULE) * 256;
+  DWORD bytes_required;
+  while (!modules) {
+    modules = (HMODULE *)MmapOrDie(modules_buffer_size, __FUNCTION__);
+    CHECK(EnumProcessModules(cur_process, modules, modules_buffer_size,
+                             &bytes_required));
+    if (bytes_required > modules_buffer_size) {
+      // Either there turned out to be more than 256 modules, or new modules
+      // could have loaded since the last try.  Retry.
+      UnmapOrDie(modules, modules_buffer_size);
+      modules = 0;
+      modules_buffer_size = bytes_required;
+    }
+  }
+
+  for (size_t i = 0; i < bytes_required / sizeof(HMODULE); ++i) {
+    char module_name[MAX_PATH];
+    bool got_module_name = GetModuleFileNameEx(
+        cur_process, modules[i], module_name, sizeof(module_name));
+    MODULEINFO mi;
+    if (GetModuleInformation(cur_process, modules[i], &mi, sizeof(mi))) {
+      Printf("\t%p-%p %s\n", mi.lpBaseOfDll,
+             (void *)((uptr)mi.lpBaseOfDll + mi.SizeOfImage),
+             got_module_name ? module_name : "[no name]");
+    } else if (got_module_name) {
+      Printf("\t???-??? %s\n", module_name);
+    } else {
+      Printf("\t???\n");
+    }
+  }
+  UnmapOrDie(modules, modules_buffer_size);
 }
 
 void DisableCoreDumperIfNecessary() {

Modified: compiler-rt/trunk/test/asan/TestCases/Windows/shadow_mapping_failure.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/shadow_mapping_failure.cc?rev=224856&r1=224855&r2=224856&view=diff
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/shadow_mapping_failure.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/shadow_mapping_failure.cc Fri Dec 26 08:28:32 2014
@@ -9,6 +9,9 @@ int main() {
   printf("Hello, world!\n");
   scanf("%s", bigchunk);
 // CHECK-NOT: Hello, world!
-// CHECK: ERROR: AddressSanitizer failed to allocate
-// CHECK: ReserveShadowMemoryRange failed
+// CHECK: Shadow memory range interleaves with an existing memory mapping.
+// CHECK: Dumping process modules:
+// CHECK-DAG: 0x{{[0-9a-f]*}}-0x{{[0-9a-f]*}} {{.*}}shadow_mapping_failure
+// CHECK-DAG: 0x{{[0-9a-f]*}}-0x{{[0-9a-f]*}} {{.*}}kernel32.dll
+// CHECK-DAG: 0x{{[0-9a-f]*}}-0x{{[0-9a-f]*}} {{.*}}ntdll.dll
 }





More information about the llvm-commits mailing list