-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vm: enhance cached data handling for empty functions and add validation tests #56442
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1564,9 +1564,13 @@ Local<Object> ContextifyContext::CompileFunctionAndCacheResult( | |
return Object::New(env->isolate()); | ||
|
||
std::unique_ptr<ScriptCompiler::CachedData> new_cached_data; | ||
if (produce_cached_data) { | ||
new_cached_data.reset(ScriptCompiler::CreateCodeCacheForFunction(fn)); | ||
if (produce_cached_data && !fn.IsEmpty()) { | ||
TryCatchScope try_cache(env); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no JavaScript execution in this scope and this |
||
if (options != ScriptCompiler::kConsumeCodeCache) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this guard is the correct solution. When |
||
new_cached_data.reset(ScriptCompiler::CreateCodeCacheForFunction(fn)); | ||
} | ||
} | ||
|
||
if (StoreCodeCacheResult(env, | ||
result, | ||
options, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
|
||
// Test that creating cached data for an empty function doesn't crash | ||
{ | ||
const script = new vm.Script(''); | ||
// Get cached data from empty script | ||
const cachedData = script.createCachedData(); | ||
|
||
// Previously this would trigger a fatal V8 error: | ||
// FATAL ERROR: v8::ScriptCompiler::CreateCodeCacheForFunction Expected SharedFunctionInfo with wrapped source code | ||
vm.compileFunction('', undefined, { | ||
cachedData, | ||
produceCachedData: true | ||
}); | ||
} | ||
|
||
// Test normal case with actual function content still works | ||
{ | ||
const script = new vm.Script('function test() { return 42; }'); | ||
const cachedData = script.createCachedData(); | ||
|
||
const fn = vm.compileFunction('return 42', undefined, { | ||
cachedData, | ||
produceCachedData: true | ||
}); | ||
|
||
const expected = 42; | ||
assert.strictEqual(fn(), expected); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
fn
here is always not empty.