llvm-project
31 строка · 1.0 Кб
1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2
3// UNSUPPORTED: ios
4
5#include "test.h"
6#include <sys/mman.h>
7
8// Test for previously unbounded memory consumption for large mallocs.
9// Code allocates a large memory block (that is handled by LargeMmapAllocator),
10// and forces allocation of meta shadow for the block. Then freed the block.
11// But meta shadow was not unmapped. Then code occupies the virtual memory
12// range of the block with something else (that does not need meta shadow).
13// And repeats. As the result meta shadow growed infinitely.
14// This program used to consume >2GB. Now it consumes <50MB.
15
16int main() {
17for (int i = 0; i < 1000; i++) {
18const int kSize = 1 << 20;
19const int kPageSize = 4 << 10;
20volatile int *p = new int[kSize];
21for (int j = 0; j < kSize; j += kPageSize / sizeof(*p))
22__atomic_store_n(&p[i], 1, __ATOMIC_RELEASE);
23delete[] p;
24mmap(0, kSize * sizeof(*p) + kPageSize, PROT_NONE, MAP_PRIVATE | MAP_ANON,
25-1, 0);
26}
27fprintf(stderr, "DONE\n");
28return 0;
29}
30
31// CHECK: DONE
32