From 436a4dc33b2376531c4a754f188555391ce1ea8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Thu, 2 Jan 2025 21:59:26 +0100 Subject: [PATCH 1/4] vm: enhance cached data handling for empty functions --- src/node_contextify.cc | 11 +++++-- .../test-vm-cached-data-validation.js | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-vm-cached-data-validation.js diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 3aa7986bf17e26..d814e573ae6c73 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1564,9 +1564,16 @@ Local ContextifyContext::CompileFunctionAndCacheResult( return Object::New(env->isolate()); std::unique_ptr new_cached_data; - if (produce_cached_data) { - new_cached_data.reset(ScriptCompiler::CreateCodeCacheForFunction(fn)); + if (produce_cached_data && !fn.IsEmpty()) { + TryCatchScope try_cache(env); + Local resource_name = fn->GetScriptOrigin().ResourceName(); + if (!resource_name.IsEmpty() && + resource_name->ToString(parsing_context) + .ToLocalChecked()->Length() > 0) { + new_cached_data.reset(ScriptCompiler::CreateCodeCacheForFunction(fn)); + } } + if (StoreCodeCacheResult(env, result, options, diff --git a/test/parallel/test-vm-cached-data-validation.js b/test/parallel/test-vm-cached-data-validation.js new file mode 100644 index 00000000000000..bbfa87cd7a84a2 --- /dev/null +++ b/test/parallel/test-vm-cached-data-validation.js @@ -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); +} From f0c08bbd77f1d1f8139e064976d23bb42c30b65c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Thu, 2 Jan 2025 22:02:45 +0100 Subject: [PATCH 2/4] fix c++ fomat --- src/node_contextify.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index d814e573ae6c73..35e897766c9d94 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1568,8 +1568,8 @@ Local ContextifyContext::CompileFunctionAndCacheResult( TryCatchScope try_cache(env); Local resource_name = fn->GetScriptOrigin().ResourceName(); if (!resource_name.IsEmpty() && - resource_name->ToString(parsing_context) - .ToLocalChecked()->Length() > 0) { + resource_name->ToString(parsing_context).ToLocalChecked()->Length() > + 0) { new_cached_data.reset(ScriptCompiler::CreateCodeCacheForFunction(fn)); } } From 46ad86b3be574dc126b5923f31b65ce5e7e130ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Thu, 2 Jan 2025 22:06:29 +0100 Subject: [PATCH 3/4] fix c++ fomat --- src/node_contextify.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 35e897766c9d94..557a4137f03d3b 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1569,7 +1569,7 @@ Local ContextifyContext::CompileFunctionAndCacheResult( Local resource_name = fn->GetScriptOrigin().ResourceName(); if (!resource_name.IsEmpty() && resource_name->ToString(parsing_context).ToLocalChecked()->Length() > - 0) { + 0) { new_cached_data.reset(ScriptCompiler::CreateCodeCacheForFunction(fn)); } } From e1b97b883d48f23854d133729d2098dcb6b6aab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Fri, 3 Jan 2025 00:59:50 +0100 Subject: [PATCH 4/4] fix tests --- src/node_contextify.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 557a4137f03d3b..3cf3f5c37cee26 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1566,10 +1566,7 @@ Local ContextifyContext::CompileFunctionAndCacheResult( std::unique_ptr new_cached_data; if (produce_cached_data && !fn.IsEmpty()) { TryCatchScope try_cache(env); - Local resource_name = fn->GetScriptOrigin().ResourceName(); - if (!resource_name.IsEmpty() && - resource_name->ToString(parsing_context).ToLocalChecked()->Length() > - 0) { + if (options != ScriptCompiler::kConsumeCodeCache) { new_cached_data.reset(ScriptCompiler::CreateCodeCacheForFunction(fn)); } }