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
21#include <malloc/malloc.h>
22#include <stdlib.h>
23#include <stdio.h>
24
25int main(void) {
26 malloc_zone_t *default_zone = malloc_default_zone();
27 printf("default zone name: %s\n", malloc_get_zone_name(default_zone));
28// CHECK: default zone name: {{a|l|t}}san
29
30 void *ptr1 = malloc(size: 10);
31 void *ptr2 = malloc_zone_malloc(default_zone, 10);
32
33 malloc_zone_t* zone1 = malloc_zone_from_ptr(ptr1);
34 malloc_zone_t* zone2 = malloc_zone_from_ptr(ptr2);
35
36 printf("zone1: %d\n", zone1 == default_zone);
37 printf("zone2: %d\n", zone2 == default_zone);
38// CHECK: zone1: 1
39// CHECK: zone2: 1
40
41 free(ptr: ptr1);
42 malloc_zone_free(zone2, ptr2);
43
44 return 0;
45}
46

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