xref: /llvm-project/compiler-rt/test/dfsan/mmap_at_init.c (revision 372c275800140f35a697f12a2e83d94d5603eaf5)
187a6325fSJianzhou Zhao // RUN: %clang_dfsan -fno-sanitize=dataflow -DCALLOC -c %s -o %t-calloc.o
287a6325fSJianzhou Zhao // RUN: %clang_dfsan %s %t-calloc.o -o %t
387a6325fSJianzhou Zhao // RUN: %run %t
487a6325fSJianzhou Zhao //
587a6325fSJianzhou Zhao // Tests that calling mmap() during during dfsan initialization works.
687a6325fSJianzhou Zhao 
7*372c2758SVitaly Buka // `internal_symbolizer` can not use `realloc` on memory from the test `calloc`.
8*372c2758SVitaly Buka // UNSUPPORTED: internal_symbolizer
9*372c2758SVitaly Buka 
1087a6325fSJianzhou Zhao #include <sanitizer/dfsan_interface.h>
1187a6325fSJianzhou Zhao #include <sys/mman.h>
1287a6325fSJianzhou Zhao #include <unistd.h>
1387a6325fSJianzhou Zhao 
1487a6325fSJianzhou Zhao #ifdef CALLOC
1587a6325fSJianzhou Zhao 
167d644e12SAaron Ballman extern void exit(int) __attribute__((noreturn));
177d644e12SAaron Ballman 
1887a6325fSJianzhou Zhao // dfsan_init() installs interceptors via dlysm(), which calls calloc().
1987a6325fSJianzhou Zhao // Calling mmap() from here should work even if interceptors haven't been fully
2087a6325fSJianzhou Zhao // set up yet.
calloc(size_t Num,size_t Size)2187a6325fSJianzhou Zhao void *calloc(size_t Num, size_t Size) {
2287a6325fSJianzhou Zhao   size_t PageSize = getpagesize();
2387a6325fSJianzhou Zhao   Size = Size * Num;
2487a6325fSJianzhou Zhao   Size = (Size + PageSize - 1) & ~(PageSize - 1); // Round up to PageSize.
2587a6325fSJianzhou Zhao   void *Ret = mmap(NULL, Size, PROT_READ | PROT_WRITE,
2687a6325fSJianzhou Zhao                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2787a6325fSJianzhou Zhao   // Use assert may cause link errors that require -Wl,-z,notext.
2887a6325fSJianzhou Zhao   // Do not know the root cause yet.
2987a6325fSJianzhou Zhao   if (Ret == MAP_FAILED) exit(-1);
3087a6325fSJianzhou Zhao   return Ret;
3187a6325fSJianzhou Zhao }
3287a6325fSJianzhou Zhao 
3387a6325fSJianzhou Zhao #else
3487a6325fSJianzhou Zhao 
main()3587a6325fSJianzhou Zhao int main() { return 0; }
3687a6325fSJianzhou Zhao 
3787a6325fSJianzhou Zhao #endif // CALLOC
38