Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Copy link
Member

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.

TryCatchScope try_cache(env);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no JavaScript execution in this scope and this TryCacheScope is redundant.

if (options != ScriptCompiler::kConsumeCodeCache) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this guard is the correct solution. When produceCachedData is true, a new cached data should always be returned regardless of whether the old one is consumed or not.

new_cached_data.reset(ScriptCompiler::CreateCodeCacheForFunction(fn));
}
}

if (StoreCodeCacheResult(env,
result,
options,
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-vm-cached-data-validation.js
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);
}
Loading