From a2ede79f1502a17eacdf8fba012819fbfc7c8b2d Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Thu, 25 Oct 2018 22:15:44 +0000 Subject: [sanitizer] Fix mallopt interceptor. On error, mallopt is supposed to return 0, not -1. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@345323 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/asan/asan_malloc_linux.cc | 2 +- lib/hwasan/hwasan_interceptors.cc | 2 +- lib/lsan/lsan_interceptors.cc | 2 +- lib/msan/msan_interceptors.cc | 2 +- lib/scudo/scudo_malloc.cpp | 2 +- test/sanitizer_common/TestCases/Linux/mallopt.cc | 9 +++++++++ 6 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 test/sanitizer_common/TestCases/Linux/mallopt.cc diff --git a/lib/asan/asan_malloc_linux.cc b/lib/asan/asan_malloc_linux.cc index 76bdff999..0a534fe21 100644 --- a/lib/asan/asan_malloc_linux.cc +++ b/lib/asan/asan_malloc_linux.cc @@ -209,7 +209,7 @@ INTERCEPTOR(struct fake_mallinfo, mallinfo, void) { } INTERCEPTOR(int, mallopt, int cmd, int value) { - return -1; + return 0; } #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO diff --git a/lib/hwasan/hwasan_interceptors.cc b/lib/hwasan/hwasan_interceptors.cc index 9a0770f56..fed4003de 100644 --- a/lib/hwasan/hwasan_interceptors.cc +++ b/lib/hwasan/hwasan_interceptors.cc @@ -186,7 +186,7 @@ struct __sanitizer_struct_mallinfo __sanitizer_mallinfo() { } int __sanitizer_mallopt(int cmd, int value) { - return -1; + return 0; } void __sanitizer_malloc_stats(void) { diff --git a/lib/lsan/lsan_interceptors.cc b/lib/lsan/lsan_interceptors.cc index fde52e496..a9bd2ba42 100644 --- a/lib/lsan/lsan_interceptors.cc +++ b/lib/lsan/lsan_interceptors.cc @@ -153,7 +153,7 @@ INTERCEPTOR(struct fake_mallinfo, mallinfo, void) { #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo) INTERCEPTOR(int, mallopt, int cmd, int value) { - return -1; + return 0; } #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt) #else diff --git a/lib/msan/msan_interceptors.cc b/lib/msan/msan_interceptors.cc index f338d422e..9f346a55a 100644 --- a/lib/msan/msan_interceptors.cc +++ b/lib/msan/msan_interceptors.cc @@ -265,7 +265,7 @@ INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) { #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD INTERCEPTOR(int, mallopt, int cmd, int value) { - return -1; + return 0; } #define MSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt) #else diff --git a/lib/scudo/scudo_malloc.cpp b/lib/scudo/scudo_malloc.cpp index 91a77b365..eef776809 100644 --- a/lib/scudo/scudo_malloc.cpp +++ b/lib/scudo/scudo_malloc.cpp @@ -79,7 +79,7 @@ INTERCEPTOR_ATTRIBUTE size_t malloc_usable_size(void *ptr) { #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO INTERCEPTOR_ATTRIBUTE int mallopt(int cmd, int value) { - return -1; + return 0; } #endif } // extern "C" diff --git a/test/sanitizer_common/TestCases/Linux/mallopt.cc b/test/sanitizer_common/TestCases/Linux/mallopt.cc new file mode 100644 index 000000000..0aa9f9844 --- /dev/null +++ b/test/sanitizer_common/TestCases/Linux/mallopt.cc @@ -0,0 +1,9 @@ +// Check that mallopt does not return invalid values (ex. -1). +// RUN: %clangxx -O2 %s -o %t && %run %t +#include +#include + +int main() { + int res = mallopt(M_ARENA_MAX, 0); + assert(res == 0 || res == 1); +} -- cgit v1.2.3