| 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 | |
| 16 | int main() { |
| 17 | for (int i = 0; i < 1000; i++) { |
| 18 | const int kSize = 1 << 20; |
| 19 | const int kPageSize = 4 << 10; |
| 20 | volatile int *p = new int[kSize]; |
| 21 | for (int j = 0; j < kSize; j += kPageSize / sizeof(*p)) |
| 22 | __atomic_store_n(&p[i], 1, __ATOMIC_RELEASE); |
| 23 | delete[] p; |
| 24 | mmap(addr: 0, len: kSize * sizeof(*p) + kPageSize, PROT_NONE, MAP_PRIVATE | MAP_ANON, |
| 25 | fd: -1, offset: 0); |
| 26 | } |
| 27 | fprintf(stderr, format: "DONE\n" ); |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | // CHECK: DONE |
| 32 | |