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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/renderer/gc_callback.h"
#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/features/feature.h"
#include "extensions/renderer/bindings/test_js_runner.h"
#include "extensions/renderer/scoped_web_frame.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/script_context_set.h"
#include "extensions/renderer/test_extensions_renderer_client.h"
#include "gin/function_template.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/web/web_frame.h"
#include "v8/include/v8.h"
namespace extensions {
namespace {
void SetToTrue(bool* value) {
if (*value)
ADD_FAILURE() << "Value is already true";
*value = true;
}
enum CallbackType { NATIVE, NATIVE_WITH_NO_FALLBACK, JS, JS_WITH_NO_FALLBACK };
class GCCallbackTest : public testing::TestWithParam<CallbackType> {
public:
GCCallbackTest() : script_context_set_(&active_extensions_) {}
GCCallbackTest(const GCCallbackTest&) = delete;
GCCallbackTest& operator=(const GCCallbackTest&) = delete;
protected:
ScriptContextSet& script_context_set() { return script_context_set_; }
v8::Isolate* Isolate() {
return web_frame_.frame()->GetAgentGroupScheduler()->Isolate();
}
v8::Local<v8::Context> v8_context() {
return v8::Local<v8::Context>::New(Isolate(), v8_context_);
}
ScriptContext* RegisterScriptContext() {
// No world ID.
return script_context_set_.Register(
web_frame_.frame(), v8::Local<v8::Context>::New(Isolate(), v8_context_),
/*world_id=*/0, /*is_webview=*/false);
}
void RequestGarbageCollection() {
Isolate()->RequestGarbageCollectionForTesting(
v8::Isolate::kFullGarbageCollection);
}
// Returns a (self-owning) GCCallback for a soon-to-be-collected object.
// The GCCallback will delete itself, or memory tests will complain.
GCCallback* GetGCCallback(ScriptContext* script_context,
bool* callback_invoked,
bool* fallback_invoked) {
// Nest another HandleScope so that |object| and |unreachable_function|'s
// handles will be garbage collected.
v8::Isolate* isolate = script_context->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> object = v8::Object::New(isolate);
base::OnceClosure fallback;
if (has_fallback())
fallback = base::BindOnce(SetToTrue, fallback_invoked);
if (GetParam() == JS) {
v8::Local<v8::FunctionTemplate> unreachable_function =
gin::CreateFunctionTemplate(
isolate, base::BindRepeating(SetToTrue, callback_invoked));
v8::Local<v8::Context> v8_context = isolate->GetCurrentContext();
return new GCCallback(
script_context, object,
unreachable_function->GetFunction(v8_context).ToLocalChecked(),
std::move(fallback));
}
return new GCCallback(script_context, object,
base::BindOnce(SetToTrue, callback_invoked),
std::move(fallback));
}
bool has_fallback() const {
switch (GetParam()) {
case JS:
case NATIVE:
return true;
case JS_WITH_NO_FALLBACK:
case NATIVE_WITH_NO_FALLBACK:
return false;
}
NOTREACHED();
return false;
}
private:
void SetUp() override {
v8::Isolate* isolate = Isolate();
v8::HandleScope handle_scope(isolate);
// We need a context that has been initialized by blink; grab the main world
// context from the web frame.
v8::Local<v8::Context> local_v8_context =
web_frame_.frame()->MainWorldScriptContext();
DCHECK(!local_v8_context.IsEmpty());
v8_context_.Reset(isolate, local_v8_context);
test_js_runner_ =
std::make_unique<TestJSRunner::Scope>(std::make_unique<TestJSRunner>());
}
void TearDown() override {
v8_context_.Reset();
test_js_runner_.reset();
RequestGarbageCollection();
}
base::test::TaskEnvironment task_environment_;
ScopedWebFrame web_frame_; // (this will construct the v8::Isolate)
// ExtensionsRendererClient is a dependency of ScriptContextSet.
TestExtensionsRendererClient extensions_renderer_client_;
ExtensionIdSet active_extensions_;
ScriptContextSet script_context_set_;
v8::Global<v8::Context> v8_context_;
std::unique_ptr<TestJSRunner::Scope> test_js_runner_;
};
TEST_P(GCCallbackTest, GCBeforeContextInvalidated) {
v8::Isolate* isolate = Isolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(v8_context());
ScriptContext* script_context = RegisterScriptContext();
bool callback_invoked = false;
bool fallback_invoked = false;
GetGCCallback(script_context, &callback_invoked, &fallback_invoked);
// Trigger a GC. Only the callback should be invoked.
RequestGarbageCollection();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback_invoked);
EXPECT_FALSE(fallback_invoked);
// Invalidate the context. The fallback should not be invoked because the
// callback was already invoked.
script_context_set().Remove(script_context);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fallback_invoked);
}
TEST_P(GCCallbackTest, ContextInvalidatedBeforeGC) {
v8::Isolate* isolate = Isolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(v8_context());
ScriptContext* script_context = RegisterScriptContext();
bool callback_invoked = false;
bool fallback_invoked = false;
GetGCCallback(script_context, &callback_invoked, &fallback_invoked);
// Invalidate the context. Only the fallback should be invoked.
script_context_set().Remove(script_context);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(callback_invoked);
// The fallback should have been invoked, if it wasn't null.
EXPECT_EQ(has_fallback(), fallback_invoked);
// Trigger a GC. The callback should not be invoked because the fallback was
// already invoked.
RequestGarbageCollection();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(callback_invoked);
}
// Test the scenario of an object being garbage collected while the
// ScriptContext is valid, but the ScriptContext being invalidated before the
// callback has a chance to run.
TEST_P(GCCallbackTest,
ContextInvalidatedBetweenGarbageCollectionAndCallbackRunning) {
v8::Isolate* isolate = Isolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(v8_context());
ScriptContext* script_context = RegisterScriptContext();
bool callback_invoked = false;
bool fallback_invoked = false;
GetGCCallback(script_context, &callback_invoked, &fallback_invoked);
RequestGarbageCollection(); // Object GC'd; callback queued.
script_context_set().Remove(script_context); // Script context invalidated.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(callback_invoked);
// The fallback should have been invoked, if it wasn't null.
EXPECT_EQ(has_fallback(), fallback_invoked);
}
INSTANTIATE_TEST_SUITE_P(NativeCallback,
GCCallbackTest,
::testing::Values(NATIVE));
INSTANTIATE_TEST_SUITE_P(JSCallback, GCCallbackTest, ::testing::Values(JS));
INSTANTIATE_TEST_SUITE_P(NativeCallbackWithNoFallback,
GCCallbackTest,
::testing::Values(NATIVE_WITH_NO_FALLBACK));
INSTANTIATE_TEST_SUITE_P(JSCallbackWithNoFallback,
GCCallbackTest,
::testing::Values(JS_WITH_NO_FALLBACK));
} // namespace
} // namespace extensions
|