[MSAN RT] Use __sanitizer::mem_is_zero in __msan_test_shadow
authorGui Andrade <guiand@google.com>
Mon, 10 Aug 2020 19:03:40 +0000 (19:03 +0000)
committerGui Andrade <guiand@google.com>
Mon, 10 Aug 2020 19:22:27 +0000 (19:22 +0000)
The former function is particularly optimized for exactly the
use case we're interested in: an all-zero buffer.

This reduces the overhead of calling this function some 80% or
more. This is particularly for instrumenting code heavy with
string processing functions, like grep. An invocation of grep
with the pattern '[aeiou]k[aeiou]' has its runtime reduced by
~75% with this patch

Differential Revision: https://reviews.llvm.org/D84961

compiler-rt/lib/msan/msan.cpp

index 9afc7b0..a1ad5c4 100644 (file)
@@ -527,6 +527,9 @@ void __msan_dump_shadow(const void *x, uptr size) {
 sptr __msan_test_shadow(const void *x, uptr size) {
   if (!MEM_IS_APP(x)) return -1;
   unsigned char *s = (unsigned char *)MEM_TO_SHADOW((uptr)x);
+  if (__sanitizer::mem_is_zero((const char *)s, size))
+    return -1;
+  // Slow path: loop through again to find the location.
   for (uptr i = 0; i < size; ++i)
     if (s[i])
       return i;