dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
505 stars 97 forks source link

Fix: Start Wasm table for the RTS at offset >= 1 #4685

Closed luc-blaeser closed 2 weeks ago

luc-blaeser commented 3 weeks ago

Rust requires a table offset of at least 1 as the element index 0 is considered invalid and causes a debug null check to panic when called. On the other hand, elem[0] can be used by the Motoko backend code, as correct Rust-generated Wasm code does not call elem[0].

This bug is independent of wasm32 and wasm64.

This issue has been observed in https://github.com/dfinity/motoko/actions/runs/10703077671/job/29672766216?pr=4683 and happened only on Linux and only under nix-build (not nix-shell).

github-actions[bot] commented 3 weeks ago

Comparing from 5b36b8b799f7083ee0a77b47928fff42e4a16093 to 6080b3f0087bacf21f46f39683993d5d416a9b0b: In terms of gas, no changes are observed in 5 tests. In terms of size, no changes are observed in 5 tests.

luc-blaeser commented 3 weeks ago

Why didn't the bytecode-verifier in the replica catch this? Do we have Motoko-compiled canisters out there with this bug baked-in?

It is actually only a requirement by Rust, not Wasm. It only affects debug Rust compilation AFAIK. I believe people probably do not release debug Motoko code. So, probably it is okay.

luc-blaeser commented 2 weeks ago

Thank you for the review, Claudio and Gabor!