Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / scudo / standalone / tests / common_test.cpp
blobfff7c662a41bcdf4cc7aa3fa3f29f44b0d83f0e7
1 //===-- common_test.cpp -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "internal_defs.h"
10 #include "tests/scudo_unit_test.h"
12 #include "common.h"
13 #include "mem_map.h"
14 #include <algorithm>
15 #include <fstream>
17 namespace scudo {
19 static uptr getResidentMemorySize() {
20 if (!SCUDO_LINUX)
21 UNREACHABLE("Not implemented!");
22 uptr Size;
23 uptr Resident;
24 std::ifstream IFS("/proc/self/statm");
25 IFS >> Size;
26 IFS >> Resident;
27 return Resident * getPageSizeCached();
30 // Fuchsia needs getResidentMemorySize implementation.
31 TEST(ScudoCommonTest, SKIP_ON_FUCHSIA(ResidentMemorySize)) {
32 uptr OnStart = getResidentMemorySize();
33 EXPECT_GT(OnStart, 0UL);
35 const uptr Size = 1ull << 30;
36 const uptr Threshold = Size >> 3;
38 MemMapT MemMap;
39 ASSERT_TRUE(MemMap.map(/*Addr=*/0U, Size, "ResidentMemorySize"));
40 ASSERT_NE(MemMap.getBase(), 0U);
41 void *P = reinterpret_cast<void *>(MemMap.getBase());
42 EXPECT_LT(getResidentMemorySize(), OnStart + Threshold);
44 memset(P, 1, Size);
45 EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold);
47 MemMap.releasePagesToOS(MemMap.getBase(), Size);
48 EXPECT_LT(getResidentMemorySize(), OnStart + Threshold);
50 memset(P, 1, Size);
51 EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold);
53 MemMap.unmap(MemMap.getBase(), Size);
56 TEST(ScudoCommonTest, Zeros) {
57 const uptr Size = 1ull << 20;
59 MemMapT MemMap;
60 ASSERT_TRUE(MemMap.map(/*Addr=*/0U, Size, "Zeros"));
61 ASSERT_NE(MemMap.getBase(), 0U);
62 uptr *P = reinterpret_cast<uptr *>(MemMap.getBase());
63 const ptrdiff_t N = Size / sizeof(uptr);
64 EXPECT_EQ(std::count(P, P + N, 0), N);
66 memset(P, 1, Size);
67 EXPECT_EQ(std::count(P, P + N, 0), 0);
69 MemMap.releasePagesToOS(MemMap.getBase(), Size);
70 EXPECT_EQ(std::count(P, P + N, 0), N);
72 MemMap.unmap(MemMap.getBase(), Size);
75 } // namespace scudo