diff options
| author | Kuba Mracek <mracek@apple.com> | 2016-12-11 08:42:42 +0000 |
|---|---|---|
| committer | Kuba Mracek <mracek@apple.com> | 2016-12-11 08:42:42 +0000 |
| commit | b93f78128f5e1a282b159dc9e8cea48297698742 (patch) | |
| tree | 8e405ab75ca862c2d94c82635ad8e06a97b6f0c9 | |
| parent | 7a230f422508ff974982b4d50175395060daf2e3 (diff) | |
| download | bcm5719-llvm-b93f78128f5e1a282b159dc9e8cea48297698742.tar.gz bcm5719-llvm-b93f78128f5e1a282b159dc9e8cea48297698742.zip | |
[sanitizer] Handle malloc_destroy_zone() on Darwin
We currently have a interceptor for malloc_create_zone, which returns a new zone that redirects all the zone requests to our sanitizer zone. However, calling malloc_destroy_zone on that zone will cause libmalloc to print out some warning messages, because the zone is not registered in the list of zones. This patch handles this and adds a testcase for that.
Differential Revision: https://reviews.llvm.org/D27083
llvm-svn: 289375
| -rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_malloc_mac.inc | 13 | ||||
| -rw-r--r-- | compiler-rt/test/asan/TestCases/Darwin/malloc_destroy_zone.cc | 21 |
2 files changed, 34 insertions, 0 deletions
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_malloc_mac.inc b/compiler-rt/lib/sanitizer_common/sanitizer_malloc_mac.inc index 149857c168c..caf753ad2f5 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_malloc_mac.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_malloc_mac.inc @@ -46,9 +46,22 @@ INTERCEPTOR(malloc_zone_t *, malloc_create_zone, // This matches the behavior of malloc_create_zone() on OSX 10.7 and higher. mprotect(new_zone, allocated_size, PROT_READ); } + // We're explicitly *NOT* registering the zone. return new_zone; } +INTERCEPTOR(void, malloc_destroy_zone, malloc_zone_t *zone) { + COMMON_MALLOC_ENTER(); + // We don't need to do anything here. We're not registering new zones, so we + // don't to unregister. Just un-mprotect and free() the zone. + if (GetMacosVersion() >= MACOS_VERSION_LION) { + uptr page_size = GetPageSizeCached(); + uptr allocated_size = RoundUpTo(sizeof(sanitizer_zone), page_size); + mprotect(zone, allocated_size, PROT_READ | PROT_WRITE); + } + COMMON_MALLOC_FREE(zone); +} + INTERCEPTOR(malloc_zone_t *, malloc_default_zone, void) { COMMON_MALLOC_ENTER(); return &sanitizer_zone; diff --git a/compiler-rt/test/asan/TestCases/Darwin/malloc_destroy_zone.cc b/compiler-rt/test/asan/TestCases/Darwin/malloc_destroy_zone.cc new file mode 100644 index 00000000000..9144bd689f7 --- /dev/null +++ b/compiler-rt/test/asan/TestCases/Darwin/malloc_destroy_zone.cc @@ -0,0 +1,21 @@ +// RUN: %clangxx_asan %s -o %t && %run %t 2>&1 | FileCheck %s + +#include <malloc/malloc.h> +#include <stdlib.h> +#include <stdio.h> + +int main() { + fprintf(stderr, "start\n"); + malloc_zone_t *zone = malloc_create_zone(0, 0); + fprintf(stderr, "zone = %p\n", zone); + malloc_set_zone_name(zone, "myzone"); + fprintf(stderr, "name changed\n"); + malloc_destroy_zone(zone); + fprintf(stderr, "done\n"); + return 0; +} + +// CHECK: start +// CHECK-NEXT: zone = 0x{{.*}} +// CHECK-NEXT: name changed +// CHECK-NEXT: done |

