[compiler-rt] 0bccdf8 - [NFC][scudo] Add releasePagesToOS test

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Sat May 22 22:49:21 PDT 2021


Author: Vitaly Buka
Date: 2021-05-22T22:42:59-07:00
New Revision: 0bccdf82f7057da9bf59f94338e6809284d068ec

URL: https://github.com/llvm/llvm-project/commit/0bccdf82f7057da9bf59f94338e6809284d068ec
DIFF: https://github.com/llvm/llvm-project/commit/0bccdf82f7057da9bf59f94338e6809284d068ec.diff

LOG: [NFC][scudo] Add releasePagesToOS test

Added: 
    compiler-rt/lib/scudo/standalone/tests/common_test.cpp

Modified: 
    compiler-rt/lib/scudo/standalone/tests/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/tests/CMakeLists.txt b/compiler-rt/lib/scudo/standalone/tests/CMakeLists.txt
index b84290f75d79..23854ee02261 100644
--- a/compiler-rt/lib/scudo/standalone/tests/CMakeLists.txt
+++ b/compiler-rt/lib/scudo/standalone/tests/CMakeLists.txt
@@ -81,6 +81,7 @@ set(SCUDO_UNIT_TEST_SOURCES
   checksum_test.cpp
   chunk_test.cpp
   combined_test.cpp
+  common_test.cpp
   flags_test.cpp
   list_test.cpp
   map_test.cpp

diff  --git a/compiler-rt/lib/scudo/standalone/tests/common_test.cpp b/compiler-rt/lib/scudo/standalone/tests/common_test.cpp
new file mode 100644
index 000000000000..7b35b2494eda
--- /dev/null
+++ b/compiler-rt/lib/scudo/standalone/tests/common_test.cpp
@@ -0,0 +1,58 @@
+//===-- common_test.cpp ---------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "internal_defs.h"
+#include "tests/scudo_unit_test.h"
+
+#include "common.h"
+#include <algorithm>
+#include <fstream>
+
+namespace scudo {
+
+static uptr getResidentMemorySize() {
+  uptr Size;
+  uptr Resident;
+  std::ifstream IFS("/proc/self/statm");
+  IFS >> Size;
+  IFS >> Resident;
+  return Resident * getPageSizeCached();
+}
+
+// Fuchsia needs getResidentMemorySize implementation.
+TEST(ScudoCommonTest, SKIP_ON_FUCHSIA(ResidentMemorySize)) {
+  uptr OnStart = getResidentMemorySize();
+  EXPECT_GT(OnStart, 0);
+
+  const uptr Size = 1ull << 30;
+  const uptr Threshold = Size >> 3;
+
+  MapPlatformData Data;
+  uptr *P = reinterpret_cast<uptr *>(
+      map(nullptr, Size, "ResidentMemorySize", 0, &Data));
+  const uptr N = Size / sizeof(*P);
+  ASSERT_NE(nullptr, P);
+  EXPECT_EQ(std::count(P, P + N, 0), N);
+  EXPECT_LT(getResidentMemorySize() - OnStart, Threshold);
+
+  memset(P, 1, Size);
+  EXPECT_EQ(std::count(P, P + N, 0), 0);
+  EXPECT_LT(getResidentMemorySize() - Size, Threshold);
+
+  releasePagesToOS((uptr)P, 0, Size, &Data);
+  EXPECT_EQ(std::count(P, P + N, 0), N);
+  // FIXME: does not work with QEMU-user.
+  // EXPECT_LT(getResidentMemorySize() - OnStart, Threshold) << OnStart << " "
+  // <<  getResidentMemorySize();
+
+  memset(P, 1, Size);
+  EXPECT_EQ(std::count(P, P + N, 0), 0);
+  EXPECT_LT(getResidentMemorySize() - Size, Threshold);
+}
+
+} // namespace scudo
\ No newline at end of file


        


More information about the llvm-commits mailing list