Commit 9ff7156f authored by Ulan Degenbaev's avatar Ulan Degenbaev Committed by Commit Bot

[test] Fix UAF in cctest/test-memory-measurement/RandomizedTimeout

The test creates a mock platform. The bug was that the lifetime of the
mock platform was shoter than the lifetime of the isolate. Even though
the mock platform restores the old platfrom, a background thread may
still have a pointer to the mock platform leading to UAF.

Bug: v8:10690
Tbr: dinfuehr@chromium.rg
Change-Id: Ic14bf408e5e3e9e7d07e01af545bb88c21462300
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2290850Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#68777}
parent d4078c64
...@@ -134,15 +134,11 @@ namespace { ...@@ -134,15 +134,11 @@ namespace {
class MockPlatform : public TestPlatform { class MockPlatform : public TestPlatform {
public: public:
MockPlatform() MockPlatform() : TestPlatform(), mock_task_runner_(new MockTaskRunner()) {
: old_platform_(i::V8::GetCurrentPlatform()),
mock_task_runner_(new MockTaskRunner()) {
// Now that it's completely constructed, make this the current platform. // Now that it's completely constructed, make this the current platform.
i::V8::SetPlatformForTesting(this); i::V8::SetPlatformForTesting(this);
} }
~MockPlatform() override { i::V8::SetPlatformForTesting(old_platform_); }
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner( std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
v8::Isolate*) override { v8::Isolate*) override {
return mock_task_runner_; return mock_task_runner_;
...@@ -169,6 +165,10 @@ class MockPlatform : public TestPlatform { ...@@ -169,6 +165,10 @@ class MockPlatform : public TestPlatform {
UNREACHABLE(); UNREACHABLE();
} }
bool NonNestableTasksEnabled() const override { return true; }
bool NonNestableDelayedTasksEnabled() const override { return true; }
bool IdleTasksEnabled() override { return false; } bool IdleTasksEnabled() override { return false; }
double Delay() { return delay_; } double Delay() { return delay_; }
...@@ -184,7 +184,6 @@ class MockPlatform : public TestPlatform { ...@@ -184,7 +184,6 @@ class MockPlatform : public TestPlatform {
double delay_ = -1; double delay_ = -1;
std::unique_ptr<Task> task_; std::unique_ptr<Task> task_;
}; };
v8::Platform* old_platform_;
std::shared_ptr<MockTaskRunner> mock_task_runner_; std::shared_ptr<MockTaskRunner> mock_task_runner_;
}; };
...@@ -203,16 +202,21 @@ class MockMeasureMemoryDelegate : public v8::MeasureMemoryDelegate { ...@@ -203,16 +202,21 @@ class MockMeasureMemoryDelegate : public v8::MeasureMemoryDelegate {
} // namespace } // namespace
TEST(RandomizedTimeout) { TEST(RandomizedTimeout) {
CcTest::InitializeVM();
MockPlatform platform; MockPlatform platform;
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
// We have to create the isolate manually here. Using CcTest::isolate() would
// lead to the situation when the isolate outlives MockPlatform which may lead
// to UAF on the background thread.
v8::Isolate* isolate = v8::Isolate::New(create_params);
std::vector<double> delays; std::vector<double> delays;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
CcTest::isolate()->MeasureMemory( isolate->MeasureMemory(std::make_unique<MockMeasureMemoryDelegate>());
std::make_unique<MockMeasureMemoryDelegate>());
delays.push_back(platform.Delay()); delays.push_back(platform.Delay());
platform.PerformTask(); platform.PerformTask();
} }
std::sort(delays.begin(), delays.end()); std::sort(delays.begin(), delays.end());
isolate->Dispose();
CHECK_LT(delays[0], delays.back()); CHECK_LT(delays[0], delays.back());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment