Skip to content

Commit a05321e

Browse files
legendecasv8-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[execution] Lookup interceptor for RestrictedGlobalProperty
When the script context looks up if a global property is restricted, it should also query the global interceptor. To avoid calling the interceptor for every declaration unconditionally, an interceptor has to be defined with `PropertyHandlerFlags::kHasDontDeleteProperty` to intercept restricted global property queries. Refs: nodejs/node#63715 Refs: nodejs/node#52634 Change-Id: I623ff285c4e8773d8ee7f681cbad68ba24bd3f40 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7898818 Reviewed-by: Leszek Swirski <[email protected]> Reviewed-by: Igor Sheludko <[email protected]> Commit-Queue: Igor Sheludko <[email protected]> Cr-Commit-Position: refs/heads/main@{#108280}
1 parent 1267199 commit a05321e

9 files changed

Lines changed: 88 additions & 11 deletions

File tree

‎include/v8-template.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,13 @@ enum class PropertyHandlerFlags {
729729
*/
730730
kHasNoSideEffect = 1 << 2,
731731

732+
/**
733+
* The interceptor may return non-configurable (PropertyAttribute::DontDelete)
734+
* properties. When set on a global object's interceptor, it will be consulted
735+
* during HasRestrictedGlobalProperty checks for lexical declarations.
736+
*/
737+
kHasDontDeleteProperty = 1 << 3,
738+
732739
/**
733740
* This flag is used to distinguish which callbacks were provided -
734741
* GenericNamedPropertyXXXCallback (old signature) or

‎src/api/api.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,8 @@ i::DirectHandle<i::InterceptorInfo> CreateInterceptorInfo(
16231623
!(flags & PropertyHandlerFlags::kOnlyInterceptStrings));
16241624
obj->set_non_masking(flags & PropertyHandlerFlags::kNonMasking);
16251625
obj->set_has_no_side_effect(flags & PropertyHandlerFlags::kHasNoSideEffect);
1626+
obj->set_has_dont_delete_property(
1627+
flags & PropertyHandlerFlags::kHasDontDeleteProperty);
16261628

16271629
if (data.IsEmpty()) {
16281630
data = v8::Undefined(reinterpret_cast<v8::Isolate*>(i_isolate));

‎src/execution/execution.cc‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,14 @@ MaybeDirectHandle<Context> NewScriptContext(
235235
}
236236

237237
if (IsLexicalVariableMode(mode)) {
238-
LookupIterator lookup_it(isolate, global_object, name, global_object,
239-
LookupIterator::OWN_SKIP_INTERCEPTOR);
240-
Maybe<PropertyAttributes> maybe =
241-
JSReceiver::GetPropertyAttributes(&lookup_it);
242-
// Can't fail since the we looking up own properties on the global object
243-
// skipping interceptors.
244-
CHECK(!maybe.IsNothing());
245-
if ((maybe.FromJust() & DONT_DELETE) != 0) {
246-
// https://tc39.es/ecma262/#sec-globaldeclarationinstantiation 5.a:
238+
Maybe<bool> has_restricted = JSGlobalObject::HasRestrictedGlobalProperty(
239+
isolate, global_object, name);
240+
if (has_restricted.IsNothing()) return MaybeDirectHandle<Context>();
241+
if (has_restricted.FromJust()) {
242+
// https://tc39.es/ecma262/#sec-globaldeclarationinstantiation 3.a:
247243
// If envRec.HasVarDeclaration(name) is true, throw a SyntaxError
248244
// exception.
249-
// https://tc39.es/ecma262/#sec-globaldeclarationinstantiation 5.d:
245+
// https://tc39.es/ecma262/#sec-globaldeclarationinstantiation 3.d:
250246
// If hasRestrictedGlobal is true, throw a SyntaxError exception.
251247
MessageLocation location(script, 0, 1);
252248
isolate->ThrowAt(isolate->factory()->NewSyntaxError(

‎src/objects/api-callbacks-inl.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ BOOL_ACCESSORS(InterceptorInfo, flags, has_no_side_effect,
234234
// TODO(ishell): remove once all the Api changes are done.
235235
BOOL_ACCESSORS(InterceptorInfo, flags, has_new_callbacks_signature,
236236
HasNewCallbacksSignatureBit::kShift)
237+
BOOL_ACCESSORS(InterceptorInfo, flags, has_dont_delete_property,
238+
HasDontDeletePropertyBit::kShift)
237239

238240
void InterceptorInfo::RemoveCallbackRedirectionForSerialization(
239241
IsolateForSandbox isolate) {

‎src/objects/api-callbacks.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,25 @@ V8_OBJECT class InterceptorInfo : public HeapObject {
244244
// TODO(ishell): remove support for old signatures once they go through
245245
// Api deprecation process.
246246
DECL_BOOLEAN_ACCESSORS(has_new_callbacks_signature)
247+
DECL_BOOLEAN_ACCESSORS(has_dont_delete_property)
247248

248249
using CanInterceptSymbolsBit = base::BitField<bool, 0, 1, uint32_t>;
249250
using NonMaskingBit = CanInterceptSymbolsBit::Next<bool, 1>;
250251
using NamedBit = NonMaskingBit::Next<bool, 1>;
251252
using HasNoSideEffectBit = NamedBit::Next<bool, 1>;
252253
using HasNewCallbacksSignatureBit = HasNoSideEffectBit::Next<bool, 1>;
254+
using HasDontDeletePropertyBit = HasNewCallbacksSignatureBit::Next<bool, 1>;
253255
enum Flag : uint32_t {
254256
kNone = 0,
255257
kCanInterceptSymbols = CanInterceptSymbolsBit::kMask,
256258
kNonMasking = NonMaskingBit::kMask,
257259
kNamed = NamedBit::kMask,
258260
kHasNoSideEffect = HasNoSideEffectBit::kMask,
259261
kHasNewCallbacksSignature = HasNewCallbacksSignatureBit::kMask,
262+
kHasDontDeleteProperty = HasDontDeletePropertyBit::kMask,
260263
};
261264
using Flags = base::Flags<Flag>;
262-
static constexpr int kFlagCount = 5;
265+
static constexpr int kFlagCount = 6;
263266

264267
DECL_PRINTER(InterceptorInfo)
265268
DECL_VERIFIER(InterceptorInfo)

‎src/objects/api-callbacks.tq‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bitfield struct InterceptorInfoFlags extends uint31 {
99
named: bool: 1 bit;
1010
has_no_side_effect: bool: 1 bit;
1111
has_new_callbacks_signature: bool: 1 bit;
12+
has_dont_delete_property: bool: 1 bit;
1213
}
1314

1415
@cppObjectLayoutDefinition

‎src/objects/js-objects.cc‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5828,6 +5828,24 @@ void JSGlobalObject::InvalidatePropertyCell(DirectHandle<JSGlobalObject> global,
58285828
value);
58295829
}
58305830

5831+
// static
5832+
Maybe<bool> JSGlobalObject::HasRestrictedGlobalProperty(
5833+
Isolate* isolate, DirectHandle<JSGlobalObject> global,
5834+
DirectHandle<Name> name) {
5835+
LookupIterator::Configuration config = LookupIterator::OWN_SKIP_INTERCEPTOR;
5836+
if (global->HasNamedInterceptor() &&
5837+
global->GetNamedInterceptor()->has_dont_delete_property()) {
5838+
config = LookupIterator::OWN;
5839+
}
5840+
LookupIterator it(isolate, global, name, global, config);
5841+
Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it);
5842+
if (maybe.IsNothing()) return Nothing<bool>();
5843+
// Global var and function bindings (except those that are introduced by
5844+
// non-strict direct eval) are non-configurable and are therefore restricted
5845+
// global properties.
5846+
return Just((maybe.FromJust() & DONT_DELETE) != 0);
5847+
}
5848+
58315849
// static
58325850
MaybeDirectHandle<JSDate> JSDate::New(Isolate* isolate,
58335851
DirectHandle<JSFunction> constructor,

‎src/objects/js-objects.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,11 @@ V8_OBJECT class JSGlobalObject : public JSSpecialObject {
12611261
static void InvalidatePropertyCell(DirectHandle<JSGlobalObject> object,
12621262
DirectHandle<Name> name);
12631263

1264+
// https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty
1265+
static Maybe<bool> HasRestrictedGlobalProperty(
1266+
Isolate* isolate, DirectHandle<JSGlobalObject> global,
1267+
DirectHandle<Name> name);
1268+
12641269
inline bool IsDetached();
12651270
inline Tagged<NativeContext> native_context();
12661271

‎test/cctest/test-api-interceptors.cc‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,49 @@ THREADED_TEST(InterceptorFunctionRedeclareWithQueryCallback) {
827827
v8::Script::Compile(ctx, code).ToLocalChecked()->Run(ctx).ToLocalChecked();
828828
}
829829

830+
// A lexical declaration must throw when the interceptor reports the property
831+
// as non-configurable (DontDelete), per HasRestrictedGlobalProperty checks in
832+
// https://tc39.es/ecma262/#sec-globaldeclarationinstantiation.
833+
THREADED_TEST(LexicalDeclThrowsForRestrictedGlobalViaInterceptor) {
834+
v8::HandleScope scope(CcTest::isolate());
835+
LocalContext env;
836+
v8::Local<v8::FunctionTemplate> templ =
837+
v8::FunctionTemplate::New(CcTest::isolate());
838+
839+
v8::Local<ObjectTemplate> object_template = templ->InstanceTemplate();
840+
object_template->SetHandler(v8::NamedPropertyHandlerConfiguration(
841+
nullptr, nullptr, QueryCallbackSetDontDelete, nullptr, nullptr,
842+
v8::Local<v8::Value>(),
843+
v8::PropertyHandlerFlags::kHasDontDeleteProperty));
844+
v8::Local<v8::Context> ctx =
845+
v8::Context::New(CcTest::isolate(), nullptr, object_template);
846+
847+
v8::TryCatch try_catch(CcTest::isolate());
848+
v8::Local<v8::String> code = v8_str("let x;");
849+
CHECK(v8::Script::Compile(ctx, code).ToLocalChecked()->Run(ctx).IsEmpty());
850+
CHECK(try_catch.HasCaught());
851+
}
852+
853+
// Without kHasDontDeleteProperty, the interceptor is not consulted for
854+
// HasRestrictedGlobalProperty and lexical declarations succeed.
855+
THREADED_TEST(LexicalDeclSucceedsWithoutRestrictedGlobalFlag) {
856+
v8::HandleScope scope(CcTest::isolate());
857+
LocalContext env;
858+
v8::Local<v8::FunctionTemplate> templ =
859+
v8::FunctionTemplate::New(CcTest::isolate());
860+
861+
v8::Local<ObjectTemplate> object_template = templ->InstanceTemplate();
862+
object_template->SetHandler(v8::NamedPropertyHandlerConfiguration(
863+
nullptr, nullptr, QueryCallbackSetDontDelete));
864+
v8::Local<v8::Context> ctx =
865+
v8::Context::New(CcTest::isolate(), nullptr, object_template);
866+
867+
v8::TryCatch try_catch(CcTest::isolate());
868+
v8::Local<v8::String> code = v8_str("let x;");
869+
CHECK(!v8::Script::Compile(ctx, code).ToLocalChecked()->Run(ctx).IsEmpty());
870+
CHECK(!try_catch.HasCaught());
871+
}
872+
830873
// Regression test for chromium bug 656648.
831874
// Do not crash on non-masking, intercepting setter callbacks.
832875
THREADED_TEST(NonMaskingInterceptor) {

0 commit comments

Comments
 (0)