[PATCH] D149025: [sanitizer][asan][win] Only unmap unneeded shadow memory on x86_64

Alvin Wong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 23 09:25:54 PDT 2023


alvinhochun created this revision.
alvinhochun added reviewers: vitalybuka, mstorsjo.
Herald added subscribers: Enna1, pengfei.
Herald added a project: All.
alvinhochun requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

D21942 <https://reviews.llvm.org/D21942> / 1128db8fe1c13800ebc77206efc50d0a219b8750 added support for
committing shadow memory on demand on Win 64-bit. The reason it is not
enabled on 32-bit wasn't clear but the page table overhead on Windows 7
may be a contributing factor.

In `AsanMapUnmapCallback::OnUnmap`, `FlushUnneededASanShadowMemory` is
called to release shadow memory. It calls `ReleaseMemoryPagesToOS`,
which had been a no-op on Windows, until D95892 <https://reviews.llvm.org/D95892> /
81b1d3da094c54ffd75e05c8d4683792edf17f4c <https://reviews.llvm.org/rG81b1d3da094c54ffd75e05c8d4683792edf17f4c> in which it was changed to
unmap full pages that the memory region covers. This was done on both
32-bit and 64-bit.

AddressSanitizerInterface.GetHeapSizeTest appears to fail on i686
targets as a side effect of this. This test allocates and frees a huge
chunk of memory which causes shadow memory to be unmapped immediately.
When the test allocates the chunk of memory a second time, asan tries to
reuse the same shadow memory region, but because the shadow memory has
now been unmapped, it causes an access violation and crashes the test.

x86_64 is not affected, because the code that handles commiting shadow
memory on demand also handles this situation, allowing the test to work
without crashing.

Therefore, this patch changes `FlushUnneededASanShadowMemory` on Windows
to only release/unmap the shadow memory on x86_64 to stop this from
happening on i686.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149025

Files:
  compiler-rt/lib/asan/asan_win.cpp


Index: compiler-rt/lib/asan/asan_win.cpp
===================================================================
--- compiler-rt/lib/asan/asan_win.cpp
+++ compiler-rt/lib/asan/asan_win.cpp
@@ -194,9 +194,12 @@
 }
 
 void FlushUnneededASanShadowMemory(uptr p, uptr size) {
+  // Only asan on 64-bit Windows supports committing shadow memory on demand.
+#if SANITIZER_WINDOWS64
   // Since asan's mapping is compacting, the shadow chunk may be
   // not page-aligned, so we only flush the page-aligned portion.
   ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size));
+#endif
 }
 
 // ---------------------- TSD ---------------- {{{


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149025.516181.patch
Type: text/x-patch
Size: 636 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230423/e0d0859a/attachment.bin>


More information about the llvm-commits mailing list