[PATCH] D26650: [asan] Don't assert that a target is within 2GB on 32-bit Windows

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 15 10:39:06 PST 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL286997: [asan] Don't assert that a target is within 2GB on 32-bit Windows (authored by rnk).

Changed prior to commit:
  https://reviews.llvm.org/D26650?vs=77917&id=78027#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D26650

Files:
  compiler-rt/trunk/lib/interception/interception_win.cc
  compiler-rt/trunk/lib/interception/tests/CMakeLists.txt
  compiler-rt/trunk/lib/interception/tests/interception_win_test.cc


Index: compiler-rt/trunk/lib/interception/interception_win.cc
===================================================================
--- compiler-rt/trunk/lib/interception/interception_win.cc
+++ compiler-rt/trunk/lib/interception/interception_win.cc
@@ -148,10 +148,16 @@
 }
 
 static bool DistanceIsWithin2Gig(uptr from, uptr target) {
+#if SANITIZER_WINDOWS64
   if (from < target)
     return target - from <= (uptr)0x7FFFFFFFU;
   else
     return from - target <= (uptr)0x80000000U;
+#else
+  // In a 32-bit address space, the address calculation will wrap, so this check
+  // is unnecessary.
+  return true;
+#endif
 }
 
 static uptr GetMmapGranularity() {
Index: compiler-rt/trunk/lib/interception/tests/interception_win_test.cc
===================================================================
--- compiler-rt/trunk/lib/interception/tests/interception_win_test.cc
+++ compiler-rt/trunk/lib/interception/tests/interception_win_test.cc
@@ -204,19 +204,38 @@
 
 // A buffer holding the dynamically generated code under test.
 u8* ActiveCode;
-size_t ActiveCodeLength = 4096;
+const size_t ActiveCodeLength = 4096;
+
+int InterceptorFunction(int x);
+
+/// Allocate code memory more than 2GB away from Base.
+u8 *AllocateCode2GBAway(u8 *Base) {
+  // Find a 64K aligned location after Base plus 2GB.
+  size_t TwoGB = 0x80000000;
+  size_t AllocGranularity = 0x10000;
+  Base = (u8 *)((((uptr)Base + TwoGB + AllocGranularity)) & ~(AllocGranularity - 1));
+
+  // Check if that location is free, and if not, loop over regions until we find
+  // one that is.
+  MEMORY_BASIC_INFORMATION mbi = {};
+  while (sizeof(mbi) == VirtualQuery(Base, &mbi, sizeof(mbi))) {
+    if (mbi.State & MEM_FREE) break;
+    Base += mbi.RegionSize;
+  }
+
+  // Allocate one RWX page at the free location.
+  return (u8 *)::VirtualAlloc(Base, ActiveCodeLength, MEM_COMMIT | MEM_RESERVE,
+                              PAGE_EXECUTE_READWRITE);
+}
 
 template<class T>
 static void LoadActiveCode(
     const T &code,
     uptr *entry_point,
     FunctionPrefixKind prefix_kind = FunctionPrefixNone) {
   if (ActiveCode == nullptr) {
-    ActiveCode =
-        (u8*)::VirtualAlloc(nullptr, ActiveCodeLength,
-                            MEM_COMMIT | MEM_RESERVE,
-                            PAGE_EXECUTE_READWRITE);
-    ASSERT_NE(ActiveCode, nullptr);
+    ActiveCode = AllocateCode2GBAway((u8*)&InterceptorFunction);
+    ASSERT_NE(ActiveCode, nullptr) << "failed to allocate RWX memory 2GB away";
   }
 
   size_t position = 0;
Index: compiler-rt/trunk/lib/interception/tests/CMakeLists.txt
===================================================================
--- compiler-rt/trunk/lib/interception/tests/CMakeLists.txt
+++ compiler-rt/trunk/lib/interception/tests/CMakeLists.txt
@@ -29,6 +29,7 @@
 endif()
 if(MSVC)
   list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gcodeview)
+  list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON -Wl,-largeaddressaware)
 endif()
 list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON -g)
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26650.78027.patch
Type: text/x-patch
Size: 3001 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161115/9984e204/attachment.bin>


More information about the llvm-commits mailing list