[PATCH] DFSan's set label function should avoid writing to the shadow memory when the write would not change the value in memory.

Sam Kerner skerner at chromium.org
Wed Aug 13 18:48:46 PDT 2014


Hi pcc,

When writing a label to shadow memory, don't write if the value is already set to the value being written.  This dramatically reduces real memory consumption in programs with sparse use of labels.

http://reviews.llvm.org/D4894

Files:
  lib/dfsan/dfsan.cc

Index: lib/dfsan/dfsan.cc
===================================================================
--- lib/dfsan/dfsan.cc
+++ lib/dfsan/dfsan.cc
@@ -169,8 +169,20 @@
 
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
 void __dfsan_set_label(dfsan_label label, void *addr, uptr size) {
-  for (dfsan_label *labelp = shadow_for(addr); size != 0; --size, ++labelp)
+  for (dfsan_label *labelp = shadow_for(addr); size != 0; --size, ++labelp) {
+    // Don't write the label if it is already the value we need it to be.
+    // In a program where most addresses are not labeled, it is common that
+    // a page of shadow memory is entirely zeroed.  The Linux copy-on-write
+    // implementation will share all of the zeroed pages, making a copy of a
+    // page when any value is written.  The un-sharing will happen even if
+    // the value written does not change the value in memory.  Avoiding the
+    // write when both |label| and |*labelp| are zero dramatically reduces
+    // the amount of real memory used by large programs.
+    if (label == *labelp)
+      continue;
+
     *labelp = label;
+  }
 }
 
 SANITIZER_INTERFACE_ATTRIBUTE
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4894.12476.patch
Type: text/x-patch
Size: 1132 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140814/457608df/attachment.bin>


More information about the llvm-commits mailing list