Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/core/postscript/wasm_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ class PsWasmCompiler {
} while (n !== 0);
}

// `i32.const` immediates are signed LEB128 (Wasm spec), so they must be
// emitted with sign extension — the unsigned encoder mis-encodes any value
// whose final 7-bit group has bit 0x40 set (e.g. 64 → 0x40 → decoded as −64).
_emitSLEB128(n) {
for (;;) {
const b = n & 0x7f;
n >>= 7; // arithmetic shift keeps the sign bit
if ((n === 0 && (b & 0x40) === 0) || (n === -1 && (b & 0x40) !== 0)) {
this._code.push(b);
return;
}
this._code.push(b | 0x80);
}
}

_emitF64Const(value) {
this._code.push(OP.f64_const);
PsWasmCompiler.#f64View.setFloat64(0, value, true /* little-endian */);
Expand Down Expand Up @@ -532,11 +547,11 @@ class PsWasmCompiler {
const shift = first.value;
if (shift > 0) {
code.push(OP.i32_const);
this._emitULEB128(shift);
this._emitSLEB128(shift);
code.push(OP.i32_shl);
} else if (shift < 0) {
code.push(OP.i32_const);
this._emitULEB128(-shift);
this._emitSLEB128(-shift);
code.push(OP.i32_shr_s);
}
code.push(OP.f64_convert_i32_s);
Expand Down Expand Up @@ -870,7 +885,7 @@ class PsWasmCompiler {
const min = this._range[i * 2];
const max = this._range[i * 2 + 1];
code.push(OP.i32_const);
this._emitULEB128(i * 8);
this._emitSLEB128(i * 8);
if (!this._compileNode(outputs[i])) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.pdf
*.error

!postscript_type4_many_outputs.pdf
!boundingBox_invalid.pdf
!pdkids.pdf
!tracemonkey.pdf
Expand Down
Binary file added test/pdfs/postscript_type4_many_outputs.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[
{
"id": "postscript-type4-many-outputs",
"file": "pdfs/postscript_type4_many_outputs.pdf",
"md5": "90c1ebd35acc6c7d01f4fdb0ae0bef82",
"rounds": 1,
"type": "eq"
},
{
"id": "bug1755201",
"file": "pdfs/bug1755201.pdf",
Expand Down
20 changes: 20 additions & 0 deletions test/unit/postscript_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,26 @@ describe("PostScript Type 4 lexer, parser, and Wasm compiler", function () {
);
expect(r2).toBeCloseTo(0.5, 9);
});

it("compiles functions with 9+ outputs (signed i32.const store offset)", async function () {
// Regression: each output's f64.store address is emitted as
// `i32.const (i * 8)`. i32.const immediates are *signed* LEB128, so the
// 9th output offset (64) must not be written with the unsigned encoder,
// which yields the byte 0x40 that Wasm decodes as -64 → out-of-bounds
// store → runtime trap. compileAndRun throws if the Wasm function traps.
for (const nOut of [9, 10, 16, 20]) {
const range = [];
for (let i = 0; i < nOut; i++) {
range.push(0, 1000);
}
const src = "{" + " dup".repeat(nOut - 1) + " }";
const out = compileAndRun(src, [0, 1], range, [0.5]);
expect(out.length).toBe(nOut);
for (const value of out) {
expect(value).toBeCloseTo(0.5, 10);
}
}
});
});

// PSStackToTree
Expand Down