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
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@302001
91177308-0d34-0410-b5e6-96231b
Change-Id: I310e0e06d0ad4450acf779630acfb71cde9ea1d2
Signed-off-by: Slava Barinov <v.barinov@samsung.com>
--- /dev/null
+/* { dg-do run } */
+/* { dg-options "-fsanitize=address" } */
+
+#include <stdio.h>
+#include <stdlib.h>
+#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 2)
+#include <mcheck.h>
+#else
+#define MCHECK_OK 0
+extern int mcheck(void (*abortfunc)(int mstatus));
+extern int mcheck_pedantic(void (*abortfunc)(int mstatus));
+extern 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();
+ /* { dg-output "Success!\n" } */
+
+ if (mcheck_pedantic(NULL) != 0) {
+ fprintf(stderr, "mcheck_pedantic() failed\n");
+ exit(EXIT_FAILURE);
+ }
+
+ check_heap();
+ /* { dg-output "Success!\n" } */
+
+ return 0;
+}
return REAL(munmap)(addr, length);
}
+#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
+
namespace __lsan {
void InitializeInterceptors() {
INTERCEPT_FUNCTION(mmap);
INTERCEPT_FUNCTION(mmap64);
INTERCEPT_FUNCTION(munmap);
+#if SANITIZER_INTERCEPT_MCHECK_MPROBE
+ INTERCEPT_FUNCTION(mcheck);
+ INTERCEPT_FUNCTION(mcheck_pedantic);
+ INTERCEPT_FUNCTION(mprobe);
+#endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
Report("LeakSanitizer: failed to create thread key.\n");
// FIXME: add other *stat interceptor
+#if SANITIZER_INTERCEPT_MCHECK_MPROBE
+INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
+ return 0;
+}
+#define INIT_MCHECK COMMON_INTERCEPT_FUNCTION(mcheck)
+
+INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
+ return 0;
+}
+#define INIT_MCHECK_PEDANTIC COMMON_INTERCEPT_FUNCTION(mcheck_pedantic)
+
+INTERCEPTOR(int, mprobe, void *ptr) {
+ return 0;
+}
+#define INIT_MPROBE COMMON_INTERCEPT_FUNCTION(mprobe)
+#else
+#define INIT_MCHECK
+#define INIT_MCHECK_PEDANTIC
+#define INIT_MPROBE
+#endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
+
static void InitializeCommonInterceptors() {
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
INIT___LXSTAT;
INIT___LXSTAT64;
// FIXME: add other *stat interceptors.
+ INIT___PRINTF_CHK;
+ INIT_MCHECK;
+ INIT_MCHECK_PEDANTIC;
+ INIT_MPROBE;
}
#define SANITIZER_INTERCEPT___XSTAT64 SI_LINUX_NOT_ANDROID
#define SANITIZER_INTERCEPT___LXSTAT SANITIZER_INTERCEPT___XSTAT
#define SANITIZER_INTERCEPT___LXSTAT64 SI_LINUX_NOT_ANDROID
+#define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H