v8/test/unittests/api/access-check-unittest.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

225 lines
8.6 KiB
C++
Raw Normal View History

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "include/v8.h"
Reland "[no-wasm] Exclude src/wasm from compilation" This is a reland of 80f5dfda0147d6b078ae6c9d0eb947bd012bf72d. A condition in pipeline.cc was inverted, which lead to a CSA verifier error. Original change's description: > [no-wasm] Exclude src/wasm from compilation > > This is the biggest chunk, including > - all of src/wasm, > - torque file for wasm objects, > - torque file for wasm builtins, > - wasm builtins, > - wasm runtime functions, > - int64 lowering, > - simd scala lowering, > - WasmGraphBuilder (TF graph construction for wasm), > - wasm frame types, > - wasm interrupts, > - the JSWasmCall opcode, > - wasm backing store allocation. > > Those components are all recursively entangled, so I found no way to > split this change up further. > > Some includes that were recursively included by wasm headers needed to > be added explicitly now. > > backing-store-unittest.cc is renamed to wasm-backing-store-unittest.cc > because it only tests wasm backing stores. This file is excluded from > no-wasm builds then. > > R=jkummerow@chromium.org, jgruber@chromium.org, mlippautz@chromium.org, petermarshall@chromium.org > > Bug: v8:11238 > Change-Id: I7558f2d12d2dd6c65128c4de7b79173668c80b2b > Cq-Include-Trybots: luci.v8.try:v8_linux64_no_wasm_compile_rel > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2742955 > Commit-Queue: Clemens Backes <clemensb@chromium.org> > Reviewed-by: Peter Marshall <petermarshall@chromium.org> > Reviewed-by: Toon Verwaest <verwaest@chromium.org> > Reviewed-by: Michael Lippautz <mlippautz@chromium.org> > Reviewed-by: Jakob Kummerow <jkummerow@chromium.org> > Reviewed-by: Jakob Gruber <jgruber@chromium.org> > Cr-Commit-Position: refs/heads/master@{#73344} TBR=jgruber@chromium.org Bug: v8:11238 Change-Id: I20bd2847a59c68738b5a336cd42582b7b1499585 Cq-Include-Trybots: luci.v8.try:v8_linux64_no_wasm_compile_rel Cq-Include-Trybots: luci.v8.try:v8_linux_verify_csa_rel_ng Cq-Include-Trybots: luci.v8.try:v8_linux64_verify_csa_rel_ng Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2752867 Reviewed-by: Clemens Backes <clemensb@chromium.org> Reviewed-by: Jakob Gruber <jgruber@chromium.org> Commit-Queue: Clemens Backes <clemensb@chromium.org> Cr-Commit-Position: refs/heads/master@{#73348}
2021-03-11 13:42:01 +00:00
#include "src/debug/debug.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
using AccessCheckTest = TestWithIsolate;
namespace {
bool AccessCheck(Local<Context> accessing_context,
Local<Object> accessed_object, Local<Value> data) {
return false;
}
MaybeLocal<Value> CompileRun(Isolate* isolate, const char* source) {
Local<String> source_string =
[api] Create v8::String::NewFromLiteral that returns Local<String> String::NewFromLiteral is a templated function that takes a char[N] argument that can be used as an alternative to String::NewFromUtf8 and returns a Local<String> rather than a MaybeLocal<String> reducing the number of ToLocalChecked() or other checks. Since the string length is known at compile time, it can statically assert that the length is less than String::kMaxLength, which means that it can never fail at runtime. This also converts all found uses of NewFromUtf8 taking a string literal or a variable initialized from a string literal to use the new API. In some cases the types of stored string literals are changed from const char* to const char[] to ensure the size is retained. This API does introduce a small difference compared to NewFromUtf8. For a case like "abc\0def", NewFromUtf8 (using length -1 to infer length) would treat this as a 3 character string, whereas the new API will treat it as a 7 character string. As a drive-by fix, this also fixes all redundant uses of v8::NewStringType::kNormal when passed to any of the String::New* functions. Change-Id: Id96a44bc068d9c4eaa634aea688e024675a0e5b3 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2089935 Commit-Queue: Dan Elphick <delphick@chromium.org> Reviewed-by: Mathias Bynens <mathias@chromium.org> Reviewed-by: Mythri Alle <mythria@chromium.org> Reviewed-by: Clemens Backes <clemensb@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#66622}
2020-03-09 10:41:45 +00:00
String::NewFromUtf8(isolate, source).ToLocalChecked();
Local<Context> context = isolate->GetCurrentContext();
Local<Script> script =
Script::Compile(context, source_string).ToLocalChecked();
return script->Run(context);
}
v8::Local<v8::String> v8_str(const char* x) {
[api] Create v8::String::NewFromLiteral that returns Local<String> String::NewFromLiteral is a templated function that takes a char[N] argument that can be used as an alternative to String::NewFromUtf8 and returns a Local<String> rather than a MaybeLocal<String> reducing the number of ToLocalChecked() or other checks. Since the string length is known at compile time, it can statically assert that the length is less than String::kMaxLength, which means that it can never fail at runtime. This also converts all found uses of NewFromUtf8 taking a string literal or a variable initialized from a string literal to use the new API. In some cases the types of stored string literals are changed from const char* to const char[] to ensure the size is retained. This API does introduce a small difference compared to NewFromUtf8. For a case like "abc\0def", NewFromUtf8 (using length -1 to infer length) would treat this as a 3 character string, whereas the new API will treat it as a 7 character string. As a drive-by fix, this also fixes all redundant uses of v8::NewStringType::kNormal when passed to any of the String::New* functions. Change-Id: Id96a44bc068d9c4eaa634aea688e024675a0e5b3 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2089935 Commit-Queue: Dan Elphick <delphick@chromium.org> Reviewed-by: Mathias Bynens <mathias@chromium.org> Reviewed-by: Mythri Alle <mythria@chromium.org> Reviewed-by: Clemens Backes <clemensb@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#66622}
2020-03-09 10:41:45 +00:00
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x).ToLocalChecked();
}
} // namespace
TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) {
isolate()->SetFailedAccessCheckCallbackFunction(
[](v8::Local<v8::Object> host, v8::AccessType type,
v8::Local<v8::Value> data) {});
Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate());
global_template->SetAccessCheckCallback(AccessCheck);
Local<FunctionTemplate> getter_template = FunctionTemplate::New(
isolate(), [](const FunctionCallbackInfo<Value>& info) { FAIL(); });
getter_template->SetAcceptAnyReceiver(false);
Local<FunctionTemplate> setter_template = FunctionTemplate::New(
isolate(), [](const FunctionCallbackInfo<v8::Value>& info) { FAIL(); });
setter_template->SetAcceptAnyReceiver(false);
global_template->SetAccessorProperty(v8_str("property"), getter_template,
setter_template);
Local<Context> target_context =
Context::New(isolate(), nullptr, global_template);
Local<Context> accessing_context =
Context::New(isolate(), nullptr, global_template);
accessing_context->Global()
->Set(accessing_context, v8_str("other"), target_context->Global())
.FromJust();
Context::Scope context_scope(accessing_context);
Local<Value> result =
CompileRun(isolate(),
"Object.getOwnPropertyDescriptor(this, 'property')"
" .get.call(other);")
.ToLocalChecked();
EXPECT_TRUE(result->IsUndefined());
CompileRun(isolate(),
"Object.getOwnPropertyDescriptor(this, 'property')"
" .set.call(other, 42);");
}
class AccessRegressionTest : public AccessCheckTest {
protected:
i::Handle<i::JSFunction> RetrieveFunctionFrom(Local<Context> context,
const char* script) {
Context::Scope context_scope(context);
Local<Value> getter = CompileRun(isolate(), script).ToLocalChecked();
EXPECT_TRUE(getter->IsFunction());
i::Handle<i::JSReceiver> r =
Utils::OpenHandle(*Local<Function>::Cast(getter));
EXPECT_TRUE(r->IsJSFunction());
return i::Handle<i::JSFunction>::cast(r);
}
};
TEST_F(AccessRegressionTest,
InstantiatedLazyAccessorPairsHaveCorrectNativeContext) {
// The setup creates two contexts and sets an object created
// in context 1 on the global of context 2.
// The object has an accessor pair {property}. Accessing the
// property descriptor of {property} causes instantiation of the
// accessor pair. The test checks that the access pair has the
// correct native context.
Local<FunctionTemplate> getter_template = FunctionTemplate::New(
isolate(), [](const FunctionCallbackInfo<Value>&) { FAIL(); });
Local<FunctionTemplate> setter_template = FunctionTemplate::New(
isolate(), [](const FunctionCallbackInfo<v8::Value>&) { FAIL(); });
Local<ObjectTemplate> object_template = ObjectTemplate::New(isolate());
object_template->SetAccessorProperty(v8_str("property"), getter_template,
setter_template);
Local<Context> context1 = Context::New(isolate(), nullptr);
Local<Context> context2 = Context::New(isolate(), nullptr);
Local<Object> object =
object_template->NewInstance(context1).ToLocalChecked();
context2->Global()
->Set(context2, v8_str("object_from_context1"), object)
.Check();
i::Handle<i::JSFunction> getter = RetrieveFunctionFrom(
context2,
"Object.getOwnPropertyDescriptor(object_from_context1, 'property').get");
ASSERT_EQ(getter->native_context(), *Utils::OpenHandle(*context1));
}
// Regression test for https://crbug.com/986063.
TEST_F(AccessRegressionTest,
InstantiatedLazyAccessorPairsHaveCorrectNativeContextDebug) {
// The setup creates two contexts and installs an object "object"
// on the global this for each context.
// The object consists of:
// - an accessor pair "property".
// - a normal function "breakfn".
//
// The test sets a break point on {object.breakfn} in the first context.
// This forces instantation of the JSFunction for the {object.property}
// accessor pair. The test verifies afterwards that the respective
// JSFunction of the getter have the correct native context.
Local<FunctionTemplate> getter_template = FunctionTemplate::New(
isolate(), [](const FunctionCallbackInfo<Value>&) { FAIL(); });
Local<FunctionTemplate> setter_template = FunctionTemplate::New(
isolate(), [](const FunctionCallbackInfo<v8::Value>&) { FAIL(); });
Local<FunctionTemplate> break_template = FunctionTemplate::New(
isolate(), [](const FunctionCallbackInfo<v8::Value>&) { FAIL(); });
Local<Context> context1 = Context::New(isolate(), nullptr);
Local<Context> context2 = Context::New(isolate(), nullptr);
Local<ObjectTemplate> object_template = ObjectTemplate::New(isolate());
object_template->Set(isolate(), "breakfn", break_template);
object_template->SetAccessorProperty(v8_str("property"), getter_template,
setter_template);
Local<Object> object1 =
object_template->NewInstance(context1).ToLocalChecked();
EXPECT_TRUE(
context1->Global()->Set(context1, v8_str("object"), object1).IsJust());
Local<Object> object2 =
object_template->NewInstance(context2).ToLocalChecked();
EXPECT_TRUE(
context2->Global()->Set(context2, v8_str("object"), object2).IsJust());
// Force instantiation of the JSFunction for the getter and setter
// of {object.property} by setting a break point on {object.breakfn}
{
Context::Scope context_scope(context1);
i::Isolate* iso = reinterpret_cast<i::Isolate*>(isolate());
i::Handle<i::JSFunction> break_fn =
RetrieveFunctionFrom(context1, "object.breakfn");
int id;
iso->debug()->SetBreakpointForFunction(i::handle(break_fn->shared(), iso),
iso->factory()->empty_string(), &id);
}
i::Handle<i::JSFunction> getter_c1 = RetrieveFunctionFrom(
context1, "Object.getOwnPropertyDescriptor(object, 'property').get");
i::Handle<i::JSFunction> getter_c2 = RetrieveFunctionFrom(
context2, "Object.getOwnPropertyDescriptor(object, 'property').get");
ASSERT_EQ(getter_c1->native_context(), *Utils::OpenHandle(*context1));
ASSERT_EQ(getter_c2->native_context(), *Utils::OpenHandle(*context2));
}
namespace {
bool failed_access_check_callback_called;
class AccessCheckTestConsoleDelegate : public debug::ConsoleDelegate {
public:
void Log(const debug::ConsoleCallArguments& args,
const debug::ConsoleContext& context) {
FAIL();
}
};
} // namespace
// Ensure that {console.log} does an access check for its arguments.
TEST_F(AccessCheckTest, ConsoleLog) {
isolate()->SetFailedAccessCheckCallbackFunction(
[](v8::Local<v8::Object> host, v8::AccessType type,
v8::Local<v8::Value> data) {
failed_access_check_callback_called = true;
});
AccessCheckTestConsoleDelegate console{};
debug::SetConsoleDelegate(isolate(), &console);
Local<ObjectTemplate> object_template = ObjectTemplate::New(isolate());
object_template->SetAccessCheckCallback(AccessCheck);
Local<Context> context1 = Context::New(isolate(), nullptr);
Local<Context> context2 = Context::New(isolate(), nullptr);
Local<Object> object1 =
object_template->NewInstance(context1).ToLocalChecked();
EXPECT_TRUE(context2->Global()
->Set(context2, v8_str("object_from_context1"), object1)
.IsJust());
Context::Scope context_scope(context2);
failed_access_check_callback_called = false;
CompileRun(isolate(), "console.log(object_from_context1);").ToLocalChecked();
ASSERT_TRUE(failed_access_check_callback_called);
}
} // namespace v8