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 // REQUIRES: x86_64-target-arch
6 //
7 // Tests that calling mmap() during during dfsan initialization works.
8 
9 #include <sanitizer/dfsan_interface.h>
10 #include <sys/mman.h>
11 #include <unistd.h>
12 
13 #ifdef CALLOC
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.
calloc(size_t Num,size_t Size)18 void *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 
main()32 int main() { return 0; }
33 
34 #endif // CALLOC
35