1// Check that malloc_default_zone and malloc_zone_from_ptr return the
2// sanitizer-installed malloc zone even when MallocStackLogging (MSL) is
3// requested. This prevents crashes in certain situations. Note that the
4// sanitizers and MSL cannot be used together. If both are enabled, MSL
5// functionality is essentially deactivated since it only hooks the default
6// allocator which is replaced by a custom sanitizer allocator.
7//
8// MSL=lite creates its own special malloc zone, copies the passed zone name,
9// and leaks it.
10// RUN: echo "leak:create_and_insert_msl_lite_zone" >> lsan.supp
11//
12// RUN: %clangxx -g %s -o %t
13// RUN: %run %t | FileCheck %s
14// RUN: %env MallocStackLogging=lite LSAN_OPTIONS=suppressions=lsan.supp %run %t | FileCheck %s
15// RUN: %env MallocStackLogging=full %run %t | FileCheck %s
16//
17// UBSan does not install a malloc zone.
18// XFAIL: ubsan
19//
20// Currently fails on darwin/lsan
21// XFAIL: darwin && lsan
22
23#include <malloc/malloc.h>
24#include <stdlib.h>
25#include <stdio.h>
26
27int main(void) {
28 malloc_zone_t *default_zone = malloc_default_zone();
29 printf("default zone name: %s\n", malloc_get_zone_name(default_zone));
30// CHECK: default zone name: {{a|l|t}}san
31
32 void *ptr1 = malloc(size: 10);
33 void *ptr2 = malloc_zone_malloc(default_zone, 10);
34
35 malloc_zone_t* zone1 = malloc_zone_from_ptr(ptr1);
36 malloc_zone_t* zone2 = malloc_zone_from_ptr(ptr2);
37
38 printf("zone1: %d\n", zone1 == default_zone);
39 printf("zone2: %d\n", zone2 == default_zone);
40// CHECK: zone1: 1
41// CHECK: zone2: 1
42
43 free(ptr: ptr1);
44 malloc_zone_free(zone2, ptr2);
45
46 return 0;
47}
48

source code of compiler-rt/test/sanitizer_common/TestCases/Darwin/malloc_zone.cpp