From e1cf8bdd9b95ea9c19a0a45d691df40573cbb4e4 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Thu, 30 Mar 2023 14:06:13 +0530 Subject: [PATCH 1/2] sea: fix memory leak detected by asan Signed-off-by: Darshan Sen --- src/node_sea.cc | 5 +++-- test/parallel/test-single-executable-application.js | 5 ----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/node_sea.cc b/src/node_sea.cc index b65620d1ff9684..8f1bb030047ffd 100644 --- a/src/node_sea.cc +++ b/src/node_sea.cc @@ -17,6 +17,7 @@ #include #include #include +#include #if !defined(DISABLE_SINGLE_EXECUTABLE_APPLICATION) @@ -60,7 +61,7 @@ std::tuple FixupArgsForSEA(int argc, char** argv) { // Repeats argv[0] at position 1 on argv as a replacement for the missing // entry point file path. if (IsSingleExecutable()) { - char** new_argv = new char*[argc + 2]; + static std::vector new_argv(argc + 2); int new_argc = 0; new_argv[new_argc++] = argv[0]; new_argv[new_argc++] = argv[0]; @@ -72,7 +73,7 @@ std::tuple FixupArgsForSEA(int argc, char** argv) { new_argv[new_argc] = nullptr; argc = new_argc; - argv = new_argv; + argv = new_argv.data(); } return {argc, argv}; diff --git a/test/parallel/test-single-executable-application.js b/test/parallel/test-single-executable-application.js index 902093dc6e412d..9ceb61883e1664 100644 --- a/test/parallel/test-single-executable-application.js +++ b/test/parallel/test-single-executable-application.js @@ -16,11 +16,6 @@ if (!process.config.variables.single_executable_application) if (!['darwin', 'win32', 'linux'].includes(process.platform)) common.skip(`Unsupported platform ${process.platform}.`); -if (process.platform === 'linux' && process.config.variables.asan) { - // Source of the memory leak - https://github.com/nodejs/node/blob/da0bc6db98cef98686122ea1e2cd2dbd2f52d123/src/node_sea.cc#L94. - common.skip('Running the resultant binary fails because of a memory leak ASAN error.'); -} - if (process.platform === 'linux' && process.config.variables.is_debug === 1) common.skip('Running the resultant binary fails with `Couldn\'t read target executable"`.'); From d12133c6c35da0bf5ef2b1625f51771dfa1b79c9 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Thu, 30 Mar 2023 20:23:58 +0530 Subject: [PATCH 2/2] sea: refactor code to be easier to read Refs: https://github.com/nodejs/node/pull/47309#discussion_r1153242084 Signed-off-by: Darshan Sen --- src/node_sea.cc | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/node_sea.cc b/src/node_sea.cc index 8f1bb030047ffd..838ddcb0ff5528 100644 --- a/src/node_sea.cc +++ b/src/node_sea.cc @@ -61,18 +61,12 @@ std::tuple FixupArgsForSEA(int argc, char** argv) { // Repeats argv[0] at position 1 on argv as a replacement for the missing // entry point file path. if (IsSingleExecutable()) { - static std::vector new_argv(argc + 2); - int new_argc = 0; - new_argv[new_argc++] = argv[0]; - new_argv[new_argc++] = argv[0]; - - for (int i = 1; i < argc; ++i) { - new_argv[new_argc++] = argv[i]; - } - - new_argv[new_argc] = nullptr; - - argc = new_argc; + static std::vector new_argv; + new_argv.reserve(argc + 2); + new_argv.emplace_back(argv[0]); + new_argv.insert(new_argv.end(), argv, argv + argc); + new_argv.emplace_back(nullptr); + argc = new_argv.size() - 1; argv = new_argv.data(); }