diff --git a/src/env-inl.h b/src/env-inl.h index 761a7bfc995..bbeb47b72ce 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -841,6 +841,14 @@ void Environment::set_process_exit_handler( #undef VY #undef VP +#define V(Name, label, _, __) \ + inline v8::Local \ + IsolateData::Name##_permission_string() const { \ + return Name##_permission_string##_.Get(isolate_); \ + } + PERMISSIONS(V) +#undef V + #define VM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate) #define V(PropertyName, TypeName) \ inline v8::Local IsolateData::PropertyName() const { \ @@ -870,6 +878,14 @@ void Environment::set_process_exit_handler( #undef VY #undef VP +#define V(Name, label, _, __) \ + inline v8::Local \ + Environment::Name##_permission_string() const { \ + return isolate_data()->Name##_permission_string(); \ + } + PERMISSIONS(V) +#undef V + #define V(PropertyName, TypeName) \ inline v8::Local Environment::PropertyName() const { \ return isolate_data()->PropertyName(); \ diff --git a/src/env.cc b/src/env.cc index 87340112fbe..7b275e09b72 100644 --- a/src/env.cc +++ b/src/env.cc @@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) { #undef VS #undef VP +#define V(Name, label, _, __) \ + info.primitive_values.push_back( \ + creator->AddData(Name##_permission_string##_.Get(isolate))); + PERMISSIONS(V) +#undef V + info.primitive_values.reserve(info.primitive_values.size() + AsyncWrap::PROVIDERS_LENGTH); for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) { @@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) { #undef VS #undef VP +#define V(Name, label, _, __) \ + do { \ + MaybeLocal maybe_field = \ + isolate_->GetDataFromSnapshotOnce( \ + info->primitive_values[i++]); \ + Local field; \ + if (!maybe_field.ToLocal(&field)) { \ + fprintf(stderr, \ + "Failed to deserialize " #Name "_permission_string\n"); \ + } \ + Name##_permission_string##_.Set(isolate_, field); \ + } while (0); + PERMISSIONS(V) +#undef V + for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) { MaybeLocal maybe_field = isolate_->GetDataFromSnapshotOnce(info->primitive_values[i++]); @@ -520,6 +541,17 @@ void IsolateData::CreateProperties() { PER_ISOLATE_STRING_PROPERTIES(V) #undef V +#define V(Name, label, _, __) \ + Name##_permission_string##_.Set( \ + isolate_, \ + String::NewFromOneByte(isolate_, \ + reinterpret_cast(#Name), \ + NewStringType::kInternalized, \ + sizeof(#Name) - 1) \ + .ToLocalChecked()); + PERMISSIONS(V) +#undef V + // Create all the provider strings that will be passed to JS. Place them in // an array so the array index matches the PROVIDER id offset. This way the // strings can be retrieved quickly. @@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const { PER_ISOLATE_STRING_PROPERTIES(V) #undef V +#define V(Name, label, _, __) \ + tracker->TrackField(#Name "_permission_string", Name##_permission_string()); + PERMISSIONS(V) +#undef V + tracker->TrackField("async_wrap_providers", async_wrap_providers_); if (node_allocator_ != nullptr) { diff --git a/src/env.h b/src/env.h index c2caf979023..c2bf9fdd497 100644 --- a/src/env.h +++ b/src/env.h @@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer { #undef VS #undef VP +#define V(Name, label, _, __) \ + inline v8::Local Name##_permission_string() const; + PERMISSIONS(V) +#undef V + #define VM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate) #define V(PropertyName, TypeName) \ inline v8::Local PropertyName() const; \ @@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer { #undef VS #undef VY #undef VP + +#define V(Name, label, _, __) \ + v8::Eternal Name##_permission_string##_; + PERMISSIONS(V) +#undef V + // Keep a list of all Persistent strings used for AsyncWrap Provider types. std::array, AsyncWrap::PROVIDERS_LENGTH> async_wrap_providers_; @@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer { #undef VY #undef VP +#define V(Name, label, _, __) \ + inline v8::Local Name##_permission_string() const; + PERMISSIONS(V) +#undef V + #define V(PropertyName, TypeName) \ inline v8::Local PropertyName() const; \ inline void set_ ## PropertyName(v8::Local value); diff --git a/src/permission/permission.cc b/src/permission/permission.cc index 919edd0821f..9815073c987 100644 --- a/src/permission/permission.cc +++ b/src/permission/permission.cc @@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo& args) { } // namespace #define V(Name, label, _, __) \ - if (perm == PermissionScope::k##Name) return #Name; -const char* Permission::PermissionToString(const PermissionScope perm) { + if (perm == PermissionScope::k##Name) return env->Name##_permission_string(); +v8::Local Permission::PermissionToString( + Environment* env, const PermissionScope perm) { PERMISSIONS(V) - return nullptr; + UNREACHABLE(); } #undef V @@ -192,12 +193,9 @@ MaybeLocal CreateAccessDeniedError(Environment* env, Local err = ERR_ACCESS_DENIED( env->isolate(), "Access to this API has been restricted. %s", suggestion); - Local perm_string; Local resource_string; - std::string_view perm_str = Permission::PermissionToString(perm); - if (!ToV8Value(env->context(), perm_str, env->isolate()) - .ToLocal(&perm_string) || - !ToV8Value(env->context(), res, env->isolate()) + Local perm_string = Permission::PermissionToString(env, perm); + if (!ToV8Value(env->context(), res, env->isolate()) .ToLocal(&resource_string) || err->Set(env->context(), env->permission_string(), perm_string) .IsNothing() || @@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env, v8::Local context = env->context(); v8::Local msg = v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0); - const char* perm_str = PermissionToString(permission); msg->Set(context, env->permission_string(), - v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked()) + PermissionToString(env, permission)) .Check(); msg->Set(context, env->resource_string(), - v8::String::NewFromUtf8(isolate, - res.data(), - v8::NewStringType::kNormal, - static_cast(res.size())) - .ToLocalChecked()) + ToV8Value(context, res).ToLocalChecked()) .Check(); ch->Publish(env, msg); publishing_ = false; @@ -333,18 +326,13 @@ void Permission::Drop(Environment* env, v8::Local context = env->context(); v8::Local msg = v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0); - const char* perm_str = PermissionToString(scope); msg->Set(context, env->permission_string(), - v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked()) + PermissionToString(env, scope)) .Check(); msg->Set(context, env->resource_string(), - v8::String::NewFromUtf8(isolate, - param.data(), - v8::NewStringType::kNormal, - static_cast(param.size())) - .ToLocalChecked()) + ToV8Value(context, param).ToLocalChecked()) .Check(); msg->Set(context, FIXED_ONE_BYTE_STRING(isolate, "drop"), diff --git a/src/permission/permission.h b/src/permission/permission.h index 5597e5ce244..674d99c1409 100644 --- a/src/permission/permission.h +++ b/src/permission/permission.h @@ -113,7 +113,8 @@ class Permission { FORCE_INLINE bool warning_only() const { return warning_only_; } static PermissionScope StringToPermission(const std::string& perm); - static const char* PermissionToString(PermissionScope perm); + static v8::Local PermissionToString(Environment* env, + PermissionScope perm); static void ThrowAccessDenied(Environment* env, PermissionScope perm, const std::string_view& res);