kleisauke / wasm-vips

libvips for the browser and Node.js, compiled to WebAssembly with Emscripten.
https://kleisauke.github.io/wasm-vips/
MIT License
463 stars 25 forks source link

`WasmTrapJSTypeError` while loading an SVG when building with LTO #48

Open kleisauke opened 1 year ago

kleisauke commented 1 year ago

This test program:

import Vips from './lib/vips-node.mjs';

const vips = await Vips({
  dynamicLibraries: ['vips-resvg.wasm']
});

const svg =
  '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" id="svg-root" />';
const im = vips.Image.newFromBuffer(svg);
im.writeToFile('x.png');

Fails with:

$ node test.mjs
wasm://wasm/00561afe:1

TypeError: type incompatibility when transforming from/to JS
    at wasm://wasm/00561afe:wasm-function[616]:0x7a3aa
    at gh (lib/vips-node.mjs:314:433)
    at wasm://wasm/00561afe:wasm-function[1113]:0xeafb9
    at wasm://wasm/00561afe:wasm-function[201]:0xcf95
    at wasm://wasm/00561afe:wasm-function[1304]:0x119fa2
    at wasm://wasm/0169d79e:wasm-function[6279]:0x474c27
    at wasm://wasm/0169d79e:wasm-function[311]:0x1ca8a
    at wasm://wasm/0169d79e:wasm-function[1465]:0xfbd6f
    at wasm://wasm/0169d79e:wasm-function[6208]:0x46ab34
    at sh (lib/vips-node.mjs:315:262)

Node.js v18.16.0
Readable stack trace ``` 1) SVG input Convert SVG with embedded images to PNG, respecting dimensions, autoconvert to PNG: Uncaught TypeError: type incompatibility when transforming from/to JS at rosvgtree::parse::parse::h46e668755e5e4dd7 (wasm://wasm/030b2c16:wasm-function[1098]:0xe5bf3) at invoke_vii (build/Release/sharp-emscripten-wasm32.node.js:7803:27) at _$LT$usvg_tree..Tree$u20$as$u20$usvg_parser..TreeParsing$GT$::from_str::h2ad984f34c8b8916 (wasm://wasm/030b2c16:wasm-function[1699]:0x161843) at _$LT$usvg_tree..Tree$u20$as$u20$usvg_parser..TreeParsing$GT$::from_data::hb74bbe40b8ecb9f0 (wasm://wasm/030b2c16:wasm-function[596]:0x60ca1) at invoke_viiii (build/Release/sharp-emscripten-wasm32.node.js:7936:27) at resvg_parse_tree_from_file (wasm://wasm/030b2c16:wasm-function[594]:0x60283) at vips_foreign_load_svg_file_header (wasm://wasm/030b2c16:wasm-function[5339]:0x2b4596) at vips_foreign_load_build (wasm://wasm/030b2c16:wasm-function[4480]:0x21e4af) at vips_object_build (wasm://wasm/030b2c16:wasm-function[4667]:0x2313ac) at vips_cache_operation_buildp (wasm://wasm/030b2c16:wasm-function[4227]:0x1ea98a) ```

When wasm-vips was build with LTO enabled, i.e:

$ npm run build -- --enable-lto

It looks like this error orginates from V8: https://github.com/nodejs/node/blob/v18.16.0/deps/v8/src/common/message-template.h#L600

Building without LTO or removing id="svg-root" from this particular SVG seems to fix this.

This was found while trying to update wasm-vips in https://github.com/lovell/sharp/pull/3522.

kleisauke commented 1 year ago

On a somewhat tangent, LTO also causes some other test failures within wasm-vips' test suite.

Details ``` 1) foreign svgload: TypeError: type incompatibility when transforming from/to JS at wasm://wasm/00561afe:wasm-function[616]:0x7a3aa at gh (lib/vips-node.mjs:314:433) at wasm://wasm/00561afe:wasm-function[1113]:0xeafb9 at wasm://wasm/00561afe:wasm-function[201]:0xcf95 at rh (lib/vips-node.mjs:315:366) at wasm://wasm/00561afe:wasm-function[1291]:0x11847c at wasm://wasm/0169d79e:wasm-function[6279]:0x474c27 at wasm://wasm/0169d79e:wasm-function[311]:0x1ca8a at wasm://wasm/0169d79e:wasm-function[1465]:0xfbd6f at wasm://wasm/0169d79e:wasm-function[6208]:0x46ab34 at sh (lib/vips-node.mjs:315:262) at wasm://wasm/0169d79e:wasm-function[4909]:0x31284d at lh (lib/vips-node.mjs:313:432) at wasm://wasm/0169d79e:wasm-function[2456]:0x1a41b9 at gh (lib/vips-node.mjs:314:433) at wasm://wasm/0169d79e:wasm-function[911]:0x93e6e at Function.Image$svgload (eval at Ee (lib/vips-node.mjs:156:242), :8:10) at a. [as svgload] (lib/vips-node.mjs:146:259) at Object.svgload (test/unit/test_foreign.js:19:33) at fileLoader (test/unit/test_foreign.js:78:33) at Context. (test/unit/test_foreign.js:907:5) at process.processImmediate (node:internal/timers:476:21) 2) histogram histIsmonotonic: vips::Error: unable to call hist_ismonotonic source: bad seek to 2435 : no property named `precision' at Yc (lib/vips-node.mjs:122:45) at lh (lib/vips-node.mjs:313:432) at wasm://wasm/0169d79e:wasm-function[6208]:0x46c065 at sh (lib/vips-node.mjs:315:262) at wasm://wasm/0169d79e:wasm-function[4020]:0x25fefa at wasm://wasm/0169d79e:wasm-function[931]:0x94659 at Ld.Image$histIsmonotonic [as histIsmonotonic] (eval at Ee (lib/vips-node.mjs:156:242), :9:10) at Context. (test/unit/test_histogram.js:35:15) at process.processImmediate (node:internal/timers:476:21) 3) histogram case: vips::Error: property at Yc (lib/vips-node.mjs:122:45) at lh (lib/vips-node.mjs:313:432) at wasm://wasm/0169d79e:wasm-function[6208]:0x46a9bf at sh (lib/vips-node.mjs:315:262) at wasm://wasm/0169d79e:wasm-function[4545]:0x2dee69 at lh (lib/vips-node.mjs:313:432) at wasm://wasm/0169d79e:wasm-function[2645]:0x1b72d4 at Ld.Image$case [as case] (eval at Ee (lib/vips-node.mjs:156:242), :10:10) at Context. (test/unit/test_histogram.js:130:25) at process.processImmediate (node:internal/timers:476:21) 4) morphology countlines: vips::Error: unable to call countlines : no property named `precision' at Yc (lib/vips-node.mjs:122:45) at lh (lib/vips-node.mjs:313:432) at wasm://wasm/0169d79e:wasm-function[6208]:0x46c065 at sh (lib/vips-node.mjs:315:262) at wasm://wasm/0169d79e:wasm-function[4425]:0x2d4957 at Og (lib/vips-node.mjs:317:367) at wasm://wasm/0169d79e:wasm-function[2411]:0x1a0d38 at Ld.Image$countlines [as countlines] (eval at Ee (lib/vips-node.mjs:156:242), :10:10) at Context. (test/unit/test_morphology.js:11:23) at process.processImmediate (node:internal/timers:476:21) 5) morphology rank: vips::Error: property at Yc (lib/vips-node.mjs:122:45) at lh (lib/vips-node.mjs:313:432) at wasm://wasm/0169d79e:wasm-function[6208]:0x46a9bf at sh (lib/vips-node.mjs:315:262) at wasm://wasm/0169d79e:wasm-function[1237]:0xc643b at wasm://wasm/0169d79e:wasm-function[2366]:0x19d9e6 at Ld.Image$rank [as rank] (eval at Ee (lib/vips-node.mjs:156:242), :12:10) at Context. (test/unit/test_morphology.js:69:20) at process.processImmediate (node:internal/timers:476:21) 6) resample mapim: vips::Error: property at Yc (lib/vips-node.mjs:122:45) at lh (lib/vips-node.mjs:313:432) at wasm://wasm/0169d79e:wasm-function[6208]:0x46a9bf at sh (lib/vips-node.mjs:315:262) at wasm://wasm/0169d79e:wasm-function[3823]:0x22f06a at rh (lib/vips-node.mjs:315:366) at wasm://wasm/0169d79e:wasm-function[6417]:0x483721 at lh (lib/vips-node.mjs:313:432) at wasm://wasm/0169d79e:wasm-function[2623]:0x1b4407 at Ld.Image$mapim (eval at Ee (lib/vips-node.mjs:156:242), :10:10) at a. [as mapim] (lib/vips-node.mjs:146:259) at toPolar (test/unit/test_resample.js:51:18) at Context. (test/unit/test_resample.js:310:15) at process.processImmediate (node:internal/timers:476:21) ```

Disabling the support for dynamic modules at runtime or via the --disable-modules build option seems to fix that. https://github.com/kleisauke/wasm-vips/blob/1a0160a367afb1f5137b27cb745dae2d01781e60/test/unit/node-helper.js#L12-L13 https://github.com/kleisauke/wasm-vips/blob/1a0160a367afb1f5137b27cb745dae2d01781e60/build.sh#L79-L82

kleisauke commented 9 months ago

LTO also causes some other test failures within wasm-vips' test suite.

These test failures are no longer reproducible, first good commit is e1cc2ddaecff889d1d9f2b8fb12e53fab51ccfad according to git bisect. Unfortunately, the main issue still persists on git master (tested with commit b5764895a98ea29e44f30c79b0f8e35603459b4b).

kleisauke commented 9 months ago

Upstream issue: https://github.com/emscripten-core/emscripten/issues/20414.