[compiler-rt] 516d757 - [msan][asan] Add runtime flag intercept_strcmp

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 18 13:46:09 PDT 2020


Author: Vitaly Buka
Date: 2020-09-18T13:45:55-07:00
New Revision: 516d7574320554022e56bbdfcddb269f87a1ba0f

URL: https://github.com/llvm/llvm-project/commit/516d7574320554022e56bbdfcddb269f87a1ba0f
DIFF: https://github.com/llvm/llvm-project/commit/516d7574320554022e56bbdfcddb269f87a1ba0f.diff

LOG: [msan][asan] Add runtime flag intercept_strcmp

Can be used to disable interceptor to workaround issues of
non-instrumented code.

Reviewed By: morehouse, eugenis

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

Added: 
    compiler-rt/test/asan/TestCases/strcmp.c
    compiler-rt/test/msan/strcmp.c

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/lib/sanitizer_common/sanitizer_flags.inc

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 6fd9ddb02a52..80035349b659 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -445,8 +445,10 @@ INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
     c2 = (unsigned char)s2[i];
     if (c1 != c2 || c1 == '\0') break;
   }
-  COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
-  COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
+  if (common_flags()->intercept_strcmp) {
+    COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
+    COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
+  }
   int result = CharCmpX(c1, c2);
   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
                              s2, result);

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_flags.inc b/compiler-rt/lib/sanitizer_common/sanitizer_flags.inc
index 065258a5a6e1..b83ac03408d8 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_flags.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_flags.inc
@@ -195,6 +195,9 @@ COMMON_FLAG(bool, intercept_strtok, true,
 COMMON_FLAG(bool, intercept_strpbrk, true,
             "If set, uses custom wrappers for strpbrk function "
             "to find more errors.")
+COMMON_FLAG(
+    bool, intercept_strcmp, true,
+    "If set, uses custom wrappers for strcmp functions to find more errors.")
 COMMON_FLAG(bool, intercept_strlen, true,
             "If set, uses custom wrappers for strlen and strnlen functions "
             "to find more errors.")

diff  --git a/compiler-rt/test/asan/TestCases/strcmp.c b/compiler-rt/test/asan/TestCases/strcmp.c
new file mode 100644
index 000000000000..417bd491ebe0
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/strcmp.c
@@ -0,0 +1,19 @@
+// RUN: %clang_asan %s -o %t
+// RUN: %env_asan_opts=intercept_strcmp=false %run %t 2>&1
+// RUN: %env_asan_opts=intercept_strcmp=true not %run %t 2>&1 | FileCheck %s
+// RUN:                                      not %run %t 2>&1 | FileCheck %s
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char **argv) {
+  char s1[] = "abcd";
+  char s2[] = "1234";
+  assert(strcmp(s1, s2) > 0);
+  assert(strcmp(s1 - 1, s2));
+
+  // CHECK: {{.*ERROR: AddressSanitizer: stack-buffer-underflow on address}}
+  // CHECK: READ of size 1
+  return 0;
+}

diff  --git a/compiler-rt/test/msan/strcmp.c b/compiler-rt/test/msan/strcmp.c
new file mode 100644
index 000000000000..42776af970a6
--- /dev/null
+++ b/compiler-rt/test/msan/strcmp.c
@@ -0,0 +1,20 @@
+// RUN: %clang_msan %s -o %t
+// RUN: MSAN_OPTIONS=intercept_strcmp=false %run %t 2>&1
+// RUN: MSAN_OPTIONS=intercept_strcmp=true not %run %t 2>&1 | FileCheck %s
+// RUN:                                    not %run %t 2>&1 | FileCheck %s
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char **argv) {
+  char undef;
+  char s1[] = "abcd";
+  char s2[] = "1234";
+  assert(strcmp(s1, s2) > 0);
+  s2[0] = undef;
+  assert(strcmp(s1, s2));
+
+  // CHECK: {{.*WARNING: MemorySanitizer: use-of-uninitialized-value}}
+  return 0;
+}


        


More information about the llvm-commits mailing list