[compiler-rt] r302001 - [sanitizer] Intercept mcheck and mprobe on Linux

Maxim Ostapenko via llvm-commits llvm-commits at lists.llvm.org
Wed May 3 00:09:10 PDT 2017


Author: chefmax
Date: Wed May  3 02:09:10 2017
New Revision: 302001

URL: http://llvm.org/viewvc/llvm-project?rev=302001&view=rev
Log:
[sanitizer] Intercept mcheck and mprobe on Linux

This patch addresses https://github.com/google/sanitizers/issues/804.
Users can use mcheck and mprobe functions to verify heap state so we should intercept them to avoid breakage of valid code.

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

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/mprobe.cc
Modified:
    compiler-rt/trunk/lib/lsan/lsan_interceptors.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h

Modified: compiler-rt/trunk/lib/lsan/lsan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_interceptors.cc?rev=302001&r1=302000&r2=302001&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_interceptors.cc Wed May  3 02:09:10 2017
@@ -185,6 +185,20 @@ INTERCEPTOR(void, cfree, void *p) ALIAS(
 #define LSAN_MAYBE_INTERCEPT_CFREE
 #endif // SANITIZER_INTERCEPT_CFREE
 
+#if SANITIZER_INTERCEPT_MCHECK_MPROBE
+INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
+  return 0;
+}
+
+INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
+  return 0;
+}
+
+INTERCEPTOR(int, mprobe, void *ptr) {
+  return 0;
+}
+#endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
+
 #define OPERATOR_NEW_BODY                              \
   ENSURE_LSAN_INITED;                                  \
   GET_STACK_TRACE_MALLOC;                              \

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=302001&r1=302000&r2=302001&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Wed May  3 02:09:10 2017
@@ -6142,6 +6142,20 @@ INTERCEPTOR(int, getloadavg, double *loa
 #define INIT_GETLOADAVG
 #endif
 
+#if SANITIZER_INTERCEPT_MCHECK_MPROBE
+INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
+  return 0;
+}
+
+INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
+  return 0;
+}
+
+INTERCEPTOR(int, mprobe, void *ptr) {
+  return 0;
+}
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h?rev=302001&r1=302000&r2=302001&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Wed May  3 02:09:10 2017
@@ -339,5 +339,6 @@
 #define SANITIZER_INTERCEPT_CFREE (!SI_FREEBSD && !SI_MAC)
 #define SANITIZER_INTERCEPT_ALIGNED_ALLOC (!SI_MAC)
 #define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC)
+#define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
 
 #endif  // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/mprobe.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/mprobe.cc?rev=302001&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/mprobe.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/mprobe.cc Wed May  3 02:09:10 2017
@@ -0,0 +1,42 @@
+// RUN: %clangxx %s -o %t && %run %t 2>&1 | FileCheck %s
+// UNSUPPORTED: android
+
+#include <stdio.h>
+#include <stdlib.h>
+#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 2)
+#include <mcheck.h>
+#else
+#define MCHECK_OK 0
+extern "C" int mcheck(void (*abortfunc)(int mstatus));
+extern "C" int mcheck_pedantic(void (*abortfunc)(int mstatus));
+extern "C" int mprobe(void *ptr);
+#endif
+
+void check_heap() {
+  void *p = malloc(1000);
+  int res = mprobe(p);
+  if (res == MCHECK_OK)
+    printf("Success!\n");
+  free(p);
+}
+
+int main(int argc, char *argv[]) {
+  void *p;
+  if (mcheck(NULL) != 0) {
+    fprintf(stderr, "mcheck() failed\n");
+    exit(EXIT_FAILURE);
+  }
+
+  check_heap();
+  // CHECK: Success!
+
+  if (mcheck_pedantic(NULL) != 0) {
+    fprintf(stderr, "mcheck_pedantic() failed\n");
+    exit(EXIT_FAILURE);
+  }
+
+  check_heap();
+  // CHECK: Success!
+
+  return 0;
+}




More information about the llvm-commits mailing list