Skip to content

Commit 7533f2c

Browse files
authored
feat: make postject faster (#86)
* 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 <[email protected]> * 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 <[email protected]> --------- Signed-off-by: Darshan Sen <[email protected]>
1 parent db69d03 commit 7533f2c

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/postject.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ enum class ExecutableFormat { kELF, kMachO, kPE, kUnknown };
1414
enum class InjectResult { kAlreadyExists, kError, kSuccess };
1515

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

2325
ExecutableFormat get_executable_format(const emscripten::val& executable) {

test/cli.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ describe("postject CLI", () => {
115115
expect(status).to.equal(0);
116116
expect(stdout).to.have.string(resourceContents);
117117
}
118-
}).timeout(3_00_000);
118+
}).timeout(15_000);
119119

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

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

166166
describe("postject API", () => {
@@ -234,7 +234,7 @@ describe("postject API", () => {
234234
expect(status).to.equal(0);
235235
expect(stdout).to.have.string(resourceContents);
236236
}
237-
}).timeout(3_00_000);
237+
}).timeout(15_000);
238238
});
239239

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

0 commit comments

Comments
 (0)