Skip to content

Commit 762cfd2

Browse files
authored
[JS] Use heap allocation when initializing quickjs sandbox (#13286)
- In case of large string the sandbox initialization failed because of an OOM * so allocate a new string in the heap * and free it after use. - it requires a quickjs update since we need to export some symbols (stringToNewUTF8 and free).
1 parent 57a1ea8 commit 762cfd2

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

external/quickjs/quickjs-eval.js

+22-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pdf.sandbox.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,26 @@ class Sandbox {
7171
}
7272

7373
let success = false;
74+
let buf = 0;
7475
try {
7576
const sandboxData = JSON.stringify(data);
7677
// "pdfjsScripting.initSandbox..." MUST be the last line to be evaluated
7778
// since the returned value is used for the communication.
7879
code.push(`pdfjsScripting.initSandbox({ data: ${sandboxData} })`);
80+
buf = this._module.stringToNewUTF8(code.join("\n"));
7981

8082
success = !!this._module.ccall(
8183
"init",
8284
"number",
83-
["string", "number"],
84-
[code.join("\n"), this._alertOnError]
85+
["number", "number"],
86+
[buf, this._alertOnError]
8587
);
8688
} catch (error) {
8789
console.error(error);
90+
} finally {
91+
if (buf) {
92+
this._module.ccall("free", "number", ["number"], [buf]);
93+
}
8894
}
8995

9096
if (success) {

0 commit comments

Comments
 (0)