Skip to content

Commit

Permalink
feat: make postject faster (#86)
Browse files Browse the repository at this point in the history
* feat: make postject faster

This change replaces the [`vecFromJSArray()`](https://emscripten.org/docs/api_reference/val.h.html?highlight=vecfromjsarray#_CPPv4N10emscripten10emscripten3val14vecFromJSArrayERK3val)
call in `vec_from_val()` with a
call to [`convertJSArrayToNumberVector()`](https://emscripten.org/docs/api_reference/val.h.html?highlight=convertjsarraytonumbervector#_CPPv4N10emscripten10emscripten3val28convertJSArrayToNumberVectorERK3val),
which reduces the time consumption of Postject from ~30s to ~6s on a
Mach-O Node.js binary when run on my x86_64 macOS.

Fixes: #85
Refs: emscripten-core/emscripten#11119
Signed-off-by: Darshan Sen <raisinten@gmail.com>

* fix: increase timeout to pass tests on Windows

Refs: https://app.circleci.com/pipelines/github/RaisinTen/postject/7/workflows/4bfbf11d-4796-459d-a339-a28098670f37/jobs/77/tests
Signed-off-by: Darshan Sen <raisinten@gmail.com>

---------

Signed-off-by: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
RaisinTen authored May 18, 2023
1 parent db69d03 commit 7533f2c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/postject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ enum class ExecutableFormat { kELF, kMachO, kPE, kUnknown };
enum class InjectResult { kAlreadyExists, kError, kSuccess };

std::vector<uint8_t> vec_from_val(const emscripten::val& value) {
// TODO(dsanders11) - vecFromJSArray incurs a copy, so memory usage is higher
// than it needs to be. Explore ways to access the memory
// directly and avoid the copy.
return emscripten::vecFromJSArray<uint8_t>(value);
// We are using `convertJSArrayToNumberVector()` instead of `vecFromJSArray()`
// because it is faster. It is okay if we use it without additional type
// checking because this function is only called on Node.js Buffer instances
// which is expected to contain elements that are safe to pass to the JS
// function, `Number()`.
return emscripten::convertJSArrayToNumberVector<uint8_t>(value);
}

ExecutableFormat get_executable_format(const emscripten::val& executable) {
Expand Down
8 changes: 4 additions & 4 deletions test/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe("postject CLI", () => {
expect(status).to.equal(0);
expect(stdout).to.have.string(resourceContents);
}
}).timeout(3_00_000);
}).timeout(15_000);

it("should display an error message when filename doesn't exist", async () => {
{
Expand All @@ -137,7 +137,7 @@ describe("postject CLI", () => {
expect(stdout).to.not.have.string("Injection done!");
expect(status).to.equal(1);
}
}).timeout(3_00_000);
}).timeout(15_000);

it("should display an error message when the file is not a supported executable type", async () => {
const bogusFile = path.join(tempDir, "bogus.exe");
Expand All @@ -160,7 +160,7 @@ describe("postject CLI", () => {
);
expect(stdout).to.not.have.string("Injection done!");
expect(status).to.equal(1);
}).timeout(3_00_000);
}).timeout(15_000);
});

describe("postject API", () => {
Expand Down Expand Up @@ -234,7 +234,7 @@ describe("postject API", () => {
expect(status).to.equal(0);
expect(stdout).to.have.string(resourceContents);
}
}).timeout(3_00_000);
}).timeout(15_000);
});

describe("api.js should not contain __filename and __dirname", () => {
Expand Down

0 comments on commit 7533f2c

Please sign in to comment.