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 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "internal_defs.h" |
10 | #include "tests/scudo_unit_test.h" |
11 | |
12 | #include "common.h" |
13 | #include "mem_map.h" |
14 | #include <algorithm> |
15 | #include <fstream> |
16 | |
17 | namespace scudo { |
18 | |
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(); |
28 | } |
29 | |
30 | // Fuchsia needs getResidentMemorySize implementation. |
31 | TEST(ScudoCommonTest, SKIP_ON_FUCHSIA(ResidentMemorySize)) { |
32 | uptr OnStart = getResidentMemorySize(); |
33 | EXPECT_GT(OnStart, 0UL); |
34 | |
35 | const uptr Size = 1ull << 30; |
36 | const uptr Threshold = Size >> 3; |
37 | |
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); |
43 | |
44 | memset(P, 1, Size); |
45 | EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold); |
46 | |
47 | MemMap.releasePagesToOS(MemMap.getBase(), Size); |
48 | EXPECT_LT(getResidentMemorySize(), OnStart + Threshold); |
49 | |
50 | memset(P, 1, Size); |
51 | EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold); |
52 | |
53 | MemMap.unmap(MemMap.getBase(), Size); |
54 | } |
55 | |
56 | TEST(ScudoCommonTest, Zeros) { |
57 | const uptr Size = 1ull << 20; |
58 | |
59 | MemMapT MemMap; |
60 | ASSERT_TRUE(MemMap.map(/*Addr=*/0U, Size, Name: "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); |
65 | |
66 | memset(s: P, c: 1, n: Size); |
67 | EXPECT_EQ(std::count(P, P + N, 0), 0); |
68 | |
69 | MemMap.releasePagesToOS(From: MemMap.getBase(), Size); |
70 | EXPECT_EQ(std::count(P, P + N, 0), N); |
71 | |
72 | MemMap.unmap(Addr: MemMap.getBase(), Size); |
73 | } |
74 | |
75 | } // namespace scudo |
76 | |