From 7f09d983f3a63c3b28d5cb526e78c83249e87529 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 5 Aug 2024 18:48:12 +0200 Subject: [PATCH] sea: don't set code cache flags when snapshot is used When both useCodeCache and useSnapshot are set, we generate the snapshot and skip the generation of the code cache since the snapshot already includes the code cache. But we previously still persist the code cache setting in the flags that got serialized into the SEA, so the resulting executable would still try to read the code cache even if it's not added to the SEA, leading to a flaky crash caused by OOB on some platforms. This patch fixes the crash by ignoring the code cache setting when generating the flag if both snapshot and code cache is configured. PR-URL: https://github.com/nodejs/node/pull/54120 Fixes: https://github.com/nodejs/node/issues/50740 Reviewed-By: Chengzhong Wu Reviewed-By: Richard Lau --- src/node_sea.cc | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/node_sea.cc b/src/node_sea.cc index bef7fe7c22739c..1cb09365dfca54 100644 --- a/src/node_sea.cc +++ b/src/node_sea.cc @@ -373,7 +373,14 @@ std::optional ParseSingleExecutableConfig( return std::nullopt; } if (use_code_cache.value()) { - result.flags |= SeaFlags::kUseCodeCache; + if (use_snapshot.value()) { + // TODO(joyeecheung): code cache in snapshot should be configured by + // separate snapshot configurations. + FPrintF(stderr, + "\"useCodeCache\" is redundant when \"useSnapshot\" is true\n"); + } else { + result.flags |= SeaFlags::kUseCodeCache; + } } auto assets_opt = parser.GetTopLevelStringDict("assets"); @@ -529,19 +536,14 @@ ExitCode GenerateSingleExecutableBlob( std::optional optional_sv_code_cache; std::string code_cache; if (static_cast(config.flags & SeaFlags::kUseCodeCache)) { - if (builds_snapshot_from_main) { - FPrintF(stderr, - "\"useCodeCache\" is redundant when \"useSnapshot\" is true\n"); - } else { - std::optional optional_code_cache = - GenerateCodeCache(config.main_path, main_script); - if (!optional_code_cache.has_value()) { - FPrintF(stderr, "Cannot generate V8 code cache\n"); - return ExitCode::kGenericUserError; - } - code_cache = optional_code_cache.value(); - optional_sv_code_cache = code_cache; + std::optional optional_code_cache = + GenerateCodeCache(config.main_path, main_script); + if (!optional_code_cache.has_value()) { + FPrintF(stderr, "Cannot generate V8 code cache\n"); + return ExitCode::kGenericUserError; } + code_cache = optional_code_cache.value(); + optional_sv_code_cache = code_cache; } std::unordered_map assets;