1 //===-- common_test.cpp -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "internal_defs.h"
10 #include "tests/scudo_unit_test.h"
19 static uptr
getResidentMemorySize() {
21 UNREACHABLE("Not implemented!");
24 std::ifstream
IFS("/proc/self/statm");
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;
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
);
45 EXPECT_GT(getResidentMemorySize(), OnStart
+ Size
- Threshold
);
47 MemMap
.releasePagesToOS(MemMap
.getBase(), Size
);
48 EXPECT_LT(getResidentMemorySize(), OnStart
+ Threshold
);
51 EXPECT_GT(getResidentMemorySize(), OnStart
+ Size
- Threshold
);
53 MemMap
.unmap(MemMap
.getBase(), Size
);
56 TEST(ScudoCommonTest
, Zeros
) {
57 const uptr Size
= 1ull << 20;
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
);
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
);