[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).
This commit is contained in:
calixteman 2021-04-23 12:04:14 +02:00 committed by GitHub
parent 57a1ea840f
commit 762cfd2d1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 23 deletions

File diff suppressed because one or more lines are too long

View File

@ -71,20 +71,26 @@ class Sandbox {
} }
let success = false; let success = false;
let buf = 0;
try { try {
const sandboxData = JSON.stringify(data); const sandboxData = JSON.stringify(data);
// "pdfjsScripting.initSandbox..." MUST be the last line to be evaluated // "pdfjsScripting.initSandbox..." MUST be the last line to be evaluated
// since the returned value is used for the communication. // since the returned value is used for the communication.
code.push(`pdfjsScripting.initSandbox({ data: ${sandboxData} })`); code.push(`pdfjsScripting.initSandbox({ data: ${sandboxData} })`);
buf = this._module.stringToNewUTF8(code.join("\n"));
success = !!this._module.ccall( success = !!this._module.ccall(
"init", "init",
"number", "number",
["string", "number"], ["number", "number"],
[code.join("\n"), this._alertOnError] [buf, this._alertOnError]
); );
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} finally {
if (buf) {
this._module.ccall("free", "number", ["number"], [buf]);
}
} }
if (success) { if (success) {