From a87c2fbee961fb49b7a1950cfeb799923bb905d5 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Fri, 30 Sep 2022 09:22:45 +0530 Subject: [PATCH] fix: OOM abort error while injecting data (#45) * fix: OOM abort error while injecting data After building Postject with -sASSERTIONS enabled, the error looked like this: ``` Aborted(Cannot enlarge memory arrays to size 82239488 bytes (OOM). Either (1) compile with -sINITIAL_MEMORY=X with X higher than the current value 16777216, (2) compile with -sALLOW_MEMORY_GROWTH which allows increasing the size at runtime, or (3) if you want malloc to return NULL (0) instead of this abort, compile with -sABORTING_MALLOC=0) ``` so I followed option 2 in this change. Fixes: https://github.com/postmanlabs/postject/issues/42 Signed-off-by: Darshan Sen * fixup! test: bump test timeout 40000ms is still too for macOS. Refs: https://app.circleci.com/pipelines/github/postmanlabs/postject/139/workflows/aee3c215-ddc7-4451-aad5-70bfc58dd8c3/jobs/1051/tests Signed-off-by: Darshan Sen * fixup! test: bump test timeout 50000ms is still too low for macOS. Refs: https://app.circleci.com/pipelines/github/postmanlabs/postject/140/workflows/d653f352-5415-4a83-95b8-c3a52393dc76/jobs/1062/tests Signed-off-by: Darshan Sen * fixup! set INITIAL_MEMORY and MAXIMUM_MEMORY Resolves a code review comment. Signed-off-by: Darshan Sen Signed-off-by: Darshan Sen --- CMakeLists.txt | 2 +- test/cli.mjs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e2ea1c..21bf599 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ endif() add_subdirectory(vendor/lief) add_executable(postject src/postject.cpp) -set_target_properties(postject PROPERTIES LINK_FLAGS "-sMODULARIZE=1 --bind") +set_target_properties(postject PROPERTIES LINK_FLAGS "-sMODULARIZE=1 -sALLOW_MEMORY_GROWTH -sINITIAL_MEMORY=268435456 -sMAXIMUM_MEMORY=4294967296 --bind") if(MSVC) set_property(TARGET postject PROPERTY LINK_FLAGS /NODEFAULTLIB:MSVCRT) diff --git a/test/cli.mjs b/test/cli.mjs index 3c77ce4..9203e01 100644 --- a/test/cli.mjs +++ b/test/cli.mjs @@ -88,3 +88,49 @@ describe("postject CLI", () => { } }).timeout(30000); }); + +describe("Inject data into Node.js", () => { + let filename; + let tempDir; + let resourceContents; + let resourceFilename; + + beforeEach(async () => { + tempDir = temporaryDirectory(); + await fs.ensureDir(tempDir); + + filename = path.join(tempDir, path.basename(process.execPath)); + + await fs.copy(process.execPath, filename); + + resourceContents = crypto.randomBytes(64).toString("hex"); + resourceFilename = path.join(tempDir, "resource.bin"); + await fs.writeFile(resourceFilename, resourceContents); + }); + + afterEach(() => { + rimraf.sync(tempDir); + }); + + it("should inject a resource successfully", async () => { + { + const { status, stdout, stderr } = spawnSync( + "node", + ["./dist/main.js", filename, "foobar", resourceFilename], + { encoding: "utf-8" } + ); + // TODO(dsanders11) - Enable this once we squelch LIEF warnings + // expect(stderr).to.be.empty; + expect(stdout).to.be.empty; + expect(status).to.equal(0); + } + + // After injection + { + const { status } = spawnSync(filename, ["-e", "process.exit()"], { + encoding: "utf-8", + }); + expect(status).to.equal(0); + } + }).timeout(60000); +});