From 8cee2e8539ae8dea441470401378979965a652b2 Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Mon, 4 Mar 2019 22:58:11 +0000 Subject: [PATCH] [sanitizer] Intercept bzero. Summary: Intercept bzero and enable existing __bzero interceptor in Linux. bzero is deprecated but still used occasionally. Reviewers: vitalybuka Subscribers: srhines, kubamracek, #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D58850 llvm-svn: 355347 --- .../sanitizer_common_interceptors.inc | 12 +++++++++++- .../sanitizer_platform_interceptors.h | 3 ++- compiler-rt/test/asan/TestCases/Linux/bzero.cc | 15 +++++++++++++++ compiler-rt/test/msan/Linux/bzero.cc | 16 ++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 compiler-rt/test/asan/TestCases/Linux/bzero.cc create mode 100644 compiler-rt/test/msan/Linux/bzero.cc diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index abe487e830c3..4ea274f7840c 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -5529,12 +5529,21 @@ INTERCEPTOR(void *, __bzero, void *block, uptr size) { void *ctx; COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size); } - #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero); #else #define INIT___BZERO #endif // SANITIZER_INTERCEPT___BZERO +#if SANITIZER_INTERCEPT_BZERO +INTERCEPTOR(void *, bzero, void *block, uptr size) { + void *ctx; + COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size); +} +#define INIT_BZERO COMMON_INTERCEPT_FUNCTION(bzero); +#else +#define INIT_BZERO +#endif // SANITIZER_INTERCEPT_BZERO + #if SANITIZER_INTERCEPT_FTIME INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) { void *ctx; @@ -9693,6 +9702,7 @@ static void InitializeCommonInterceptors() { INIT_CAPGET; INIT_AEABI_MEM; INIT___BZERO; + INIT_BZERO; INIT_FTIME; INIT_XDR; INIT_TSEARCH; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index 906740c8bfc8..a4116f8a0aa7 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -411,7 +411,8 @@ #else #define SANITIZER_INTERCEPT_AEABI_MEM 0 #endif -#define SANITIZER_INTERCEPT___BZERO SI_MAC +#define SANITIZER_INTERCEPT___BZERO SI_MAC || SI_LINUX_NOT_ANDROID +#define SANITIZER_INTERCEPT_BZERO SI_LINUX_NOT_ANDROID #define SANITIZER_INTERCEPT_FTIME \ (!SI_FREEBSD && !SI_NETBSD && !SI_OPENBSD && SI_POSIX) #define SANITIZER_INTERCEPT_XDR SI_LINUX_NOT_ANDROID || SI_SOLARIS diff --git a/compiler-rt/test/asan/TestCases/Linux/bzero.cc b/compiler-rt/test/asan/TestCases/Linux/bzero.cc new file mode 100644 index 000000000000..430edb744692 --- /dev/null +++ b/compiler-rt/test/asan/TestCases/Linux/bzero.cc @@ -0,0 +1,15 @@ +// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s + +// REQUIRES: !android + +#include +#include + +int main(int argc, char *argv[]) { + char buf[100]; + // *& to suppress bzero-to-memset optimization. + (*&bzero)(buf, sizeof(buf) + 1); + // CHECK: AddressSanitizer: stack-buffer-overflow + // CHECK-NEXT: WRITE of size 101 at + return 0; +} diff --git a/compiler-rt/test/msan/Linux/bzero.cc b/compiler-rt/test/msan/Linux/bzero.cc new file mode 100644 index 000000000000..cb319a6cfa1e --- /dev/null +++ b/compiler-rt/test/msan/Linux/bzero.cc @@ -0,0 +1,16 @@ +// RUN: %clangxx_msan -O0 %s -o %t && %run %t + +// REQUIRES: !android + +#include +#include +#include + +int main(int argc, char *argv[]) { + char buf[100]; + assert(0 == __msan_test_shadow(buf, sizeof(buf))); + // *& to suppress bzero-to-memset optimization. + (*&bzero)(buf, 50); + assert(50 == __msan_test_shadow(buf, sizeof(buf))); + return 0; +}