swift icon indicating copy to clipboard operation
swift copied to clipboard

Question about “locals exceed maximum”

Open SDGGiesbrecht opened this issue 4 years ago • 6 comments

carton test fails with the following:

error: failed to run `[...]/.build/wasm32-unknown-wasi/debug/[Package]Tests.wasm`
│   1: module instantiation failed (engine: jit, compiler: cranelift)
╰─> 2: Validation error: locals exceed maximum (at offset 1442120)

I don’t understand what that means or how to go about finding the underlying problem.

SDGGiesbrecht avatar Jan 09 '21 00:01 SDGGiesbrecht

Thanks! This is one of the errors we've been chasing for some time, but couldn't find an isolated reproducible test case. Do you have one to share here by any chance? Either an isolated file compilable with swiftc or a SwiftPM project would be much appreciated!

MaxDesiatov avatar Jan 09 '21 09:01 MaxDesiatov

Yes, I managed to narrow the problem down to a particular long dictionary literal. Let me clean‐up some of the mess left over from my experiments, and then I’ll post a link.

SDGGiesbrecht avatar Jan 09 '21 21:01 SDGGiesbrecht

I don't think it's related to carton directly, so I'm moving it to the toolchain repo in the meantime...

MaxDesiatov avatar Jan 09 '21 21:01 MaxDesiatov

This was the offending literal.

git clone https://github.com/SDGGiesbrecht/SDGWeb
cd SDGWeb
git checkout 8bd5066bebc69e8d686a879628363689108d8048

With the empty literal variant applied when compiling for the web platform, everything works. But if you comment out the #if to enable the 2125‐entry literal variant used by the other platforms, then this error occurs.

Note that the error doesn’t surface until you actually attempt to launch the tests (carton test). The compilation phase on its own throws no errors (swift build --triple wasm32-unknown-wasi), even though that is almost certainly where the real problem is.

SDGGiesbrecht avatar Jan 09 '21 21:01 SDGGiesbrecht

But I still don’t know what the error message means; I only know how to dodge it.

SDGGiesbrecht avatar Jan 09 '21 22:01 SDGGiesbrecht

WebAssembly hosts have certain limits placed on modules they load, and binaries not satisfying these limits don't pass validation, which hosts are required to do before actual execution. The Swift toolchain and LLVM unfortunately don't run validation during the build, so this error doesn't surface until you actually load the binary in the host (i.e. the browser). This isn't an error in your code, this is a bug in how LLVM or swiftc generate Wasm binary for such literals.

Thanks for isolating this, we'll try to create a test case for it and submit a patch upstream.

CC @kateinoigakukun

MaxDesiatov avatar Jan 09 '21 22:01 MaxDesiatov