[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
This commit is contained in:
Vitaly Buka 2020-09-18 03:00:50 -07:00
parent 1c4c21489f
commit 516d757432
4 changed files with 46 additions and 2 deletions

View File

@ -445,8 +445,10 @@ INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
c2 = (unsigned char)s2[i]; c2 = (unsigned char)s2[i];
if (c1 != c2 || c1 == '\0') break; if (c1 != c2 || c1 == '\0') break;
} }
COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1); if (common_flags()->intercept_strcmp) {
COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1); COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
}
int result = CharCmpX(c1, c2); int result = CharCmpX(c1, c2);
CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1, CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
s2, result); s2, result);

View File

@ -195,6 +195,9 @@ COMMON_FLAG(bool, intercept_strtok, true,
COMMON_FLAG(bool, intercept_strpbrk, true, COMMON_FLAG(bool, intercept_strpbrk, true,
"If set, uses custom wrappers for strpbrk function " "If set, uses custom wrappers for strpbrk function "
"to find more errors.") "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, COMMON_FLAG(bool, intercept_strlen, true,
"If set, uses custom wrappers for strlen and strnlen functions " "If set, uses custom wrappers for strlen and strnlen functions "
"to find more errors.") "to find more errors.")

View File

@ -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;
}

View File

@ -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;
}