1// RUN: %clang_dfsan -fno-sanitize=dataflow -DCALLOC -c %s -o %t-calloc.o
2// RUN: %clang_dfsan %s %t-calloc.o -o %t
3// RUN: %run %t
4//
5// Tests that calling mmap() during during dfsan initialization works.
6
7#include <sanitizer/dfsan_interface.h>
8#include <sys/mman.h>
9#include <unistd.h>
10
11#ifdef CALLOC
12
13extern void exit(int) __attribute__((noreturn));
14
15// dfsan_init() installs interceptors via dlysm(), which calls calloc().
16// Calling mmap() from here should work even if interceptors haven't been fully
17// set up yet.
18void *calloc(size_t Num, size_t Size) {
19 size_t PageSize = getpagesize();
20 Size = Size * Num;
21 Size = (Size + PageSize - 1) & ~(PageSize - 1); // Round up to PageSize.
22 void *Ret = mmap(NULL, Size, PROT_READ | PROT_WRITE,
23 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
24 // Use assert may cause link errors that require -Wl,-z,notext.
25 // Do not know the root cause yet.
26 if (Ret == MAP_FAILED) exit(-1);
27 return Ret;
28}
29
30#else
31
32int main() { return 0; }
33
34#endif // CALLOC
35

source code of compiler-rt/test/dfsan/mmap_at_init.c