[PATCH] D102469: [sanitizer] Reduce redzone size for small size global objects

Zhiwei Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 14 00:06:35 PDT 2021


condy created this revision.
condy added a reviewer: condy.
Herald added a subscriber: hiraditya.
condy requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

currently 1 byte global object has a ridiculous 63 bytes redzone. This patch reduce the redzone size to 32 if the size of global object is less than half of 32 (the minimal size of redzone). So that a 12 bytes object has a 20 bytes redzone, a 20 bytes object has a 44 bytes redzone.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102469

Files:
  llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp


Index: llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -2552,13 +2552,20 @@
   constexpr uint64_t kMaxRZ = 1 << 18;
   const uint64_t MinRZ = getMinRedzoneSizeForGlobal();
 
-  // Calculate RZ, where MinRZ <= RZ <= MaxRZ, and RZ ~ 1/4 * SizeInBytes.
-  uint64_t RZ =
-      std::max(MinRZ, std::min(kMaxRZ, (SizeInBytes / MinRZ / 4) * MinRZ));
+  uint64_t RZ = 0;
+  // Reduce redzone size for small size objects, e.g. int, char[1]. MinRZ is at
+  // least 32 bytes, optimize when SizeInBytes is less than half of MinRZ.
+  if (SizeInBytes < MinRZ / 2) {
+    RZ = MinRZ - SizeInBytes;
+  } else {
+    // Calculate RZ, where MinRZ <= RZ <= MaxRZ, and RZ ~ 1/4 * SizeInBytes.
+    RZ = std::max(MinRZ, std::min(kMaxRZ, (SizeInBytes / MinRZ / 4) * MinRZ));
+
+    // Round up to multiple of MinRZ.
+    if (SizeInBytes % MinRZ)
+      RZ += MinRZ - (SizeInBytes % MinRZ);
+  }
 
-  // Round up to multiple of MinRZ.
-  if (SizeInBytes % MinRZ)
-    RZ += MinRZ - (SizeInBytes % MinRZ);
   assert((RZ + SizeInBytes) % MinRZ == 0);
 
   return RZ;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102469.345356.patch
Type: text/x-patch
Size: 1247 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210514/d2a43e87/attachment.bin>


More information about the llvm-commits mailing list