xref: /llvm-project/compiler-rt/test/dfsan/mmap_at_init.c (revision 372c275800140f35a697f12a2e83d94d5603eaf5)
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 // `internal_symbolizer` can not use `realloc` on memory from the test `calloc`.
8 // UNSUPPORTED: internal_symbolizer
9 
10 #include <sanitizer/dfsan_interface.h>
11 #include <sys/mman.h>
12 #include <unistd.h>
13 
14 #ifdef CALLOC
15 
16 extern void exit(int) __attribute__((noreturn));
17 
18 // dfsan_init() installs interceptors via dlysm(), which calls calloc().
19 // Calling mmap() from here should work even if interceptors haven't been fully
20 // set up yet.
calloc(size_t Num,size_t Size)21 void *calloc(size_t Num, size_t Size) {
22   size_t PageSize = getpagesize();
23   Size = Size * Num;
24   Size = (Size + PageSize - 1) & ~(PageSize - 1); // Round up to PageSize.
25   void *Ret = mmap(NULL, Size, PROT_READ | PROT_WRITE,
26                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
27   // Use assert may cause link errors that require -Wl,-z,notext.
28   // Do not know the root cause yet.
29   if (Ret == MAP_FAILED) exit(-1);
30   return Ret;
31 }
32 
33 #else
34 
main()35 int main() { return 0; }
36 
37 #endif // CALLOC
38