Skip to content

Commit a87c2fb

Browse files
authored
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: #42 Signed-off-by: Darshan Sen <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * fixup! set INITIAL_MEMORY and MAXIMUM_MEMORY Resolves a code review comment. Signed-off-by: Darshan Sen <[email protected]> Signed-off-by: Darshan Sen <[email protected]>
1 parent 0035c07 commit a87c2fb

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ endif()
2020
add_subdirectory(vendor/lief)
2121

2222
add_executable(postject src/postject.cpp)
23-
set_target_properties(postject PROPERTIES LINK_FLAGS "-sMODULARIZE=1 --bind")
23+
set_target_properties(postject PROPERTIES LINK_FLAGS "-sMODULARIZE=1 -sALLOW_MEMORY_GROWTH -sINITIAL_MEMORY=268435456 -sMAXIMUM_MEMORY=4294967296 --bind")
2424

2525
if(MSVC)
2626
set_property(TARGET postject PROPERTY LINK_FLAGS /NODEFAULTLIB:MSVCRT)

test/cli.mjs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,49 @@ describe("postject CLI", () => {
8888
}
8989
}).timeout(30000);
9090
});
91+
92+
describe("Inject data into Node.js", () => {
93+
let filename;
94+
let tempDir;
95+
let resourceContents;
96+
let resourceFilename;
97+
98+
beforeEach(async () => {
99+
tempDir = temporaryDirectory();
100+
await fs.ensureDir(tempDir);
101+
102+
filename = path.join(tempDir, path.basename(process.execPath));
103+
104+
await fs.copy(process.execPath, filename);
105+
106+
resourceContents = crypto.randomBytes(64).toString("hex");
107+
resourceFilename = path.join(tempDir, "resource.bin");
108+
await fs.writeFile(resourceFilename, resourceContents);
109+
});
110+
111+
afterEach(() => {
112+
rimraf.sync(tempDir);
113+
});
114+
115+
it("should inject a resource successfully", async () => {
116+
{
117+
const { status, stdout, stderr } = spawnSync(
118+
"node",
119+
["./dist/main.js", filename, "foobar", resourceFilename],
120+
{ encoding: "utf-8" }
121+
);
122+
// TODO(dsanders11) - Enable this once we squelch LIEF warnings
123+
// expect(stderr).to.be.empty;
124+
expect(stdout).to.be.empty;
125+
expect(status).to.equal(0);
126+
}
127+
128+
// After injection
129+
{
130+
const { status } = spawnSync(filename, ["-e", "process.exit()"], {
131+
encoding: "utf-8",
132+
});
133+
expect(status).to.equal(0);
134+
}
135+
}).timeout(60000);
136+
});

0 commit comments

Comments
 (0)