Commit 0c9a0072 authored by Stefano Sanfilippo's avatar Stefano Sanfilippo Committed by Commit Bot

[compiler,api] Pass non-strings to the modifying callback when unconditional codegen is on.

In the current state, when unconditional compilation is on, strings are evaluated and other objects are passed through unchanged. After this, non-strings are passed to the modifying callback which could unwrap and eval them. eval(string) is not affected.

If a non-modifying callback is set, it still takes the precedence, and the non-string object is returned as it would be currently (line 1933).

Change-Id: I835b976b3420635baba245c08f8563a9e5b3b246
Bug: chromium:1024786
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1917147
Commit-Queue: Stefano Sanfilippo <ssanfilippo@chromium.org>
Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarMichael Stanton <mvstanton@chromium.org>
Reviewed-by: 's avatarDaniel Vogelheim <vogelheim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67570}
parent 6a7b21c5
...@@ -1898,7 +1898,6 @@ bool CodeGenerationFromStringsAllowed(Isolate* isolate, Handle<Context> context, ...@@ -1898,7 +1898,6 @@ bool CodeGenerationFromStringsAllowed(Isolate* isolate, Handle<Context> context,
// (via v8::Isolate::SetModifyCodeGenerationFromStringsCallback) // (via v8::Isolate::SetModifyCodeGenerationFromStringsCallback)
bool ModifyCodeGenerationFromStrings(Isolate* isolate, Handle<Context> context, bool ModifyCodeGenerationFromStrings(Isolate* isolate, Handle<Context> context,
Handle<i::Object>* source) { Handle<i::Object>* source) {
DCHECK(context->allow_code_gen_from_strings().IsFalse(isolate));
DCHECK(isolate->modify_code_gen_callback()); DCHECK(isolate->modify_code_gen_callback());
DCHECK(source); DCHECK(source);
...@@ -1939,10 +1938,8 @@ std::pair<MaybeHandle<String>, bool> Compiler::ValidateDynamicCompilationSource( ...@@ -1939,10 +1938,8 @@ std::pair<MaybeHandle<String>, bool> Compiler::ValidateDynamicCompilationSource(
// allow_code_gen_from_strings can be many things, so we'll always check // allow_code_gen_from_strings can be many things, so we'll always check
// against the 'false' literal, so that e.g. undefined and 'true' are treated // against the 'false' literal, so that e.g. undefined and 'true' are treated
// the same. // the same.
if (!context->allow_code_gen_from_strings().IsFalse(isolate)) { if (!context->allow_code_gen_from_strings().IsFalse(isolate) &&
if (!original_source->IsString()) { original_source->IsString()) {
return {MaybeHandle<String>(), true};
}
return {Handle<String>::cast(original_source), false}; return {Handle<String>::cast(original_source), false};
} }
......
...@@ -19351,6 +19351,52 @@ TEST(ModifyCodeGenFromStrings) { ...@@ -19351,6 +19351,52 @@ TEST(ModifyCodeGenFromStrings) {
try_catch.Reset(); try_catch.Reset();
} }
v8::ModifyCodeGenerationFromStringsResult RejectStringsIncrementNumbers(
Local<Context> context, Local<Value> source) {
if (source->IsString()) {
return {false, v8::MaybeLocal<String>()};
}
Local<v8::Number> number;
if (!source->ToNumber(context).ToLocal(&number)) {
return {true, v8::MaybeLocal<String>()};
}
Local<v8::String> incremented =
String::NewFromUtf8(context->GetIsolate(),
std::to_string(number->Value() + 1).c_str(),
v8::NewStringType::kNormal)
.ToLocalChecked();
return {true, incremented};
}
TEST(AllowFromStringsOrModifyCodegen) {
LocalContext context;
v8::HandleScope scope(context->GetIsolate());
context->GetIsolate()->SetModifyCodeGenerationFromStringsCallback(
&RejectStringsIncrementNumbers);
context->AllowCodeGenerationFromStrings(false);
TryCatch try_catch(CcTest::isolate());
Local<Value> result = CompileRun("eval('40+2')");
CHECK(result.IsEmpty());
CHECK(try_catch.HasCaught());
try_catch.Reset();
result = CompileRun("eval(42)");
CHECK_EQ(43, result->Int32Value(context.local()).FromJust());
context->AllowCodeGenerationFromStrings(true);
result = CompileRun("eval('40+2')");
CHECK_EQ(42, result->Int32Value(context.local()).FromJust());
result = CompileRun("eval(42)");
CHECK_EQ(43, result->Int32Value(context.local()).FromJust());
}
TEST(SetErrorMessageForCodeGenFromStrings) { TEST(SetErrorMessageForCodeGenFromStrings) {
LocalContext context; LocalContext context;
v8::HandleScope scope(context->GetIsolate()); v8::HandleScope scope(context->GetIsolate());
......
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