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
15int main() {
16 int fd = open(file: "/dev/zero", O_RDWR);
17 if (fd == -1) perror(s: "open(/dev/zero)"), exit(status: 1);
18 for (size_t mmap_size = 64ull << 30; mmap_size >= 4 << 10; mmap_size /= 2) {
19 size_t allocated = 0;
20 while (mmap(addr: 0, len: mmap_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
21 fd: fd, offset: 0) != MAP_FAILED) {
22 allocated += mmap_size;
23 }
24 fprintf(stderr, format: "allocated %zu with size %zu\n", allocated, mmap_size);
25 }
26 fprintf(stderr, format: "DONE\n");
27 // If tsan runtime will try to allocate something during exit handling,
28 // the allocation will fail because there is no VA whatsoever.
29 // It's observed to fail with the following error in some cases:
30 // failed to allocate 0x1000 (4096) bytes of DTLS_NextBlock.
31 // So terminate the process immediately.
32 _exit(status: 0);
33}
34
35// CHECK: DONE
36

source code of compiler-rt/test/tsan/mmap_lots.cpp