xref: /llvm-project/compiler-rt/test/tsan/mmap_lots.cpp (revision 0984b8de0b0d5d178a8e6e5de1eb89f29493a89e)
1 // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 
3 // Test that mmap does not return unexpected addresses
4 // (the check is in the interceptor).
5 
6 #include <fcntl.h>
7 #include <stddef.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/mman.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 
15 int main() {
16   int fd = open("/dev/zero", O_RDWR);
17   if (fd == -1) perror("open(/dev/zero)"), exit(1);
18   for (size_t mmap_size = 64ull << 30; mmap_size >= 4 << 10; mmap_size /= 2) {
19     size_t allocated = 0;
20     while (mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
21                 fd, 0) != MAP_FAILED) {
22       allocated += mmap_size;
23     }
24     fprintf(stderr, "allocated %zu with size %zu\n", allocated, mmap_size);
25   }
26   fprintf(stderr, "DONE\n");
27 }
28 
29 // CHECK: DONE
30