1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/discardable_memory.h"
#include "base/memory/discardable_memory_allocator.h"
#include "base/memory/madv_free_discardable_memory_allocator_posix.h"
#include "base/memory/madv_free_discardable_memory_posix.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/run_until.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
#include "components/discardable_memory/service/discardable_shared_memory_manager.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/renderer/discardable_memory_utils.h"
#include "content/renderer/render_thread_impl.h"
#include "content/shell/browser/shell.h"
#include "ui/gfx/buffer_format_util.h"
#include "url/gurl.h"
namespace content {
namespace {
class RenderThreadImplDiscardableMemoryBrowserTest : public ContentBrowserTest {
public:
RenderThreadImplDiscardableMemoryBrowserTest()
: discardable_memory_allocator_(nullptr) {}
// Overridden from BrowserTestBase:
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(switches::kSingleProcess);
}
void SetUpOnMainThread() override {
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
PostTaskToInProcessRendererAndWait(base::BindOnce(
&RenderThreadImplDiscardableMemoryBrowserTest::SetUpOnRenderThread,
base::Unretained(this)));
}
std::unique_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory(
size_t size) {
std::unique_ptr<base::DiscardableMemory> rv;
PostTaskToInProcessRendererAndWait(base::BindLambdaForTesting([&] {
rv =
discardable_memory_allocator()->AllocateLockedDiscardableMemory(size);
}));
return rv;
}
std::unique_ptr<base::DiscardableMemory>
AllocateLockedDiscardableMemoryWithRetryOrDie(
size_t size,
base::OnceClosure on_no_memory) {
std::unique_ptr<base::DiscardableMemory> rv;
PostTaskToInProcessRendererAndWait(base::BindLambdaForTesting([&] {
rv = discardable_memory_allocator()
->AllocateLockedDiscardableMemoryWithRetryOrDie(
size, std::move(on_no_memory));
}));
return rv;
}
base::DiscardableMemoryAllocator* discardable_memory_allocator() {
return discardable_memory_allocator_;
}
private:
void SetUpOnRenderThread() {
discardable_memory_allocator_ =
RenderThreadImpl::current()->GetDiscardableMemoryAllocatorForTest();
}
raw_ptr<base::DiscardableMemoryAllocator> discardable_memory_allocator_;
};
// TODO(crbug.com/362224383): This test was flaky on Windows ASan bots.
#if BUILDFLAG(IS_WIN) && defined(ADDRESS_SANITIZER)
#define MAYBE_LockDiscardableMemory DISABLED_LockDiscardableMemory
#else
#define MAYBE_LockDiscardableMemory LockDiscardableMemory
#endif
IN_PROC_BROWSER_TEST_F(RenderThreadImplDiscardableMemoryBrowserTest,
MAYBE_LockDiscardableMemory) {
const size_t kSize = 1024 * 1024; // 1MiB.
std::unique_ptr<base::DiscardableMemory> memory =
AllocateLockedDiscardableMemory(kSize);
ASSERT_TRUE(memory);
void* addr = memory->data();
ASSERT_NE(nullptr, addr);
memory->Unlock();
// Simulate memory being discarded as if under memory pressure.
memory->DiscardForTesting();
// Should fail as memory should have been purged.
EXPECT_FALSE(memory->Lock());
}
// Ensure that address space mapped by allocating discardable memory is unmapped
// after discarding under memory pressure, by creating and discarding a large
// amount of discardable memory.
//
// Disable the test for the Android asan build.
// See http://crbug.com/667837 for detail.
#if !(BUILDFLAG(IS_ANDROID) && defined(ADDRESS_SANITIZER))
IN_PROC_BROWSER_TEST_F(RenderThreadImplDiscardableMemoryBrowserTest,
// TODO(crbug.com/40681859): Re-enable this test
DISABLED_DiscardableMemoryAddressSpace) {
const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB.
const size_t kNumberOfInstances = 1024 + 1; // >4GiB total.
base::DiscardableMemoryBacking impl = base::GetDiscardableMemoryBacking();
// TODO(gordonguan): When MADV_FREE DiscardableMemory is discarded, the
// backing memory is freed, but remains mapped in memory. It is only
// unmapped when the object is destroyed, or on the next Lock() after
// discard. Therefore, an abundance of discarded but mapped discardable
// memory instances may cause an out-of-memory condition.
if (impl != base::DiscardableMemoryBacking::kSharedMemory)
return;
std::vector<std::unique_ptr<base::DiscardableMemory>> instances;
for (size_t i = 0; i < kNumberOfInstances; ++i) {
std::unique_ptr<base::DiscardableMemory> memory =
AllocateLockedDiscardableMemory(kLargeSize);
ASSERT_TRUE(memory);
void* addr = memory->data();
ASSERT_NE(nullptr, addr);
memory->Unlock();
instances.push_back(std::move(memory));
}
}
#endif
// TODO(crbug.com/378037524): This test was flaky on Windows ASan bots.
#if BUILDFLAG(IS_WIN) && defined(ADDRESS_SANITIZER)
#define MAYBE_ReleaseFreeDiscardableMemory_Explicitly \
DISABLED_ReleaseFreeDiscardableMemory_Explicitly
#else
#define MAYBE_ReleaseFreeDiscardableMemory_Explicitly \
ReleaseFreeDiscardableMemory_Explicitly
#endif
IN_PROC_BROWSER_TEST_F(RenderThreadImplDiscardableMemoryBrowserTest,
MAYBE_ReleaseFreeDiscardableMemory_Explicitly) {
const size_t kSize = 1024 * 1024; // 1MiB.
base::DiscardableMemoryBacking impl = base::GetDiscardableMemoryBacking();
std::unique_ptr<base::DiscardableMemory> memory =
AllocateLockedDiscardableMemory(kSize);
EXPECT_TRUE(memory);
EXPECT_GE(discardable_memory_allocator()->GetBytesAllocated(), kSize);
memory.reset();
EXPECT_EQ(discardable_memory_allocator()->GetBytesAllocated(), 0U);
if (impl != base::DiscardableMemoryBacking::kSharedMemory) {
LOG(INFO) << "Not using shared-memory backing. Skipping test.";
return;
}
EXPECT_GE(discardable_memory::DiscardableSharedMemoryManager::Get()
->GetBytesAllocated(),
kSize);
static_cast<discardable_memory::ClientDiscardableSharedMemoryManager*>(
discardable_memory_allocator())
->ReleaseFreeMemory();
// ReleaseFreeMemory() should result in the allocated bytes dropping to zero
// within a shorter time than the RunLoop timeout.
EXPECT_TRUE(base::test::RunUntil([]() {
return discardable_memory::DiscardableSharedMemoryManager::Get()
->GetBytesAllocated() == 0;
}));
}
// TODO(crbug.com/362120461): This test was flaky on Windows ASan bots.
#if BUILDFLAG(IS_WIN) && defined(ADDRESS_SANITIZER)
#define MAYBE_ReleaseFreeDiscardableMemory_ByCriticalPressure \
DISABLED_ReleaseFreeDiscardableMemory_ByCriticalPressure
#else
#define MAYBE_ReleaseFreeDiscardableMemory_ByCriticalPressure \
ReleaseFreeDiscardableMemory_ByCriticalPressure
#endif
IN_PROC_BROWSER_TEST_F(RenderThreadImplDiscardableMemoryBrowserTest,
MAYBE_ReleaseFreeDiscardableMemory_ByCriticalPressure) {
const size_t kSize = 1024 * 1024; // 1MiB.
base::DiscardableMemoryBacking impl = base::GetDiscardableMemoryBacking();
std::unique_ptr<base::DiscardableMemory> memory =
AllocateLockedDiscardableMemory(kSize);
EXPECT_TRUE(memory);
EXPECT_GE(discardable_memory_allocator()->GetBytesAllocated(), kSize);
memory.reset();
EXPECT_EQ(discardable_memory_allocator()->GetBytesAllocated(), 0U);
if (impl != base::DiscardableMemoryBacking::kSharedMemory) {
LOG(INFO) << "Not using shared-memory backing. Skipping test.";
return;
}
EXPECT_GE(discardable_memory::DiscardableSharedMemoryManager::Get()
->GetBytesAllocated(),
kSize);
// Call RenderThreadImpl::ReleaseFreeMemory through a fake memory pressure
// notification. The pressure notification will be handled on the test
// main thread, so it is sufficient to RunAllTasksUntilIdle(), after which
// the manager should report that the memory has been freed.
base::MemoryPressureListener::SimulatePressureNotification(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
RunAllTasksUntilIdle();
EXPECT_EQ(0u, discardable_memory::DiscardableSharedMemoryManager::Get()
->GetBytesAllocated());
}
// TODO(crbug.com/364379688): This test is flaky on Windows ASan bots.
#if BUILDFLAG(IS_WIN) && defined(ADDRESS_SANITIZER)
#define MAYBE_CheckReleaseMemory DISABLED_CheckReleaseMemory
#else
#define MAYBE_CheckReleaseMemory CheckReleaseMemory
#endif
IN_PROC_BROWSER_TEST_F(RenderThreadImplDiscardableMemoryBrowserTest,
MAYBE_CheckReleaseMemory) {
std::vector<std::unique_ptr<base::DiscardableMemory>> all_memory;
auto* allocator =
static_cast<discardable_memory::ClientDiscardableSharedMemoryManager*>(
discardable_memory_allocator());
constexpr size_t kMaxRegions = 10;
constexpr size_t kRegionSize = 4 * 1024 * 1024;
allocator->SetBytesAllocatedLimitForTesting(kMaxRegions * kRegionSize);
// Allocate the maximum amount of memory.
for (size_t i = 0; i < kMaxRegions; i++) {
auto region = AllocateLockedDiscardableMemoryWithRetryOrDie(
kRegionSize, base::DoNothing());
all_memory.push_back(std::move(region));
}
auto region = AllocateLockedDiscardableMemoryWithRetryOrDie(
kRegionSize, base::BindLambdaForTesting([&]() { all_memory.clear(); }));
// Checks that the memory reclaim callback was called, and that the allocation
// then succeeded. Allocation success is checked because the test has not
// crashed.
EXPECT_TRUE(all_memory.empty());
allocator->SetBytesAllocatedLimitForTesting(0);
}
} // namespace
} // namespace content
|