Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Sep 11, 2024
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
8 changes: 7 additions & 1 deletion src/linking/linkModule.ml
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,13 @@ let link (em1 : extended_module) libname (em2 : extended_module) =
)
end else ();

let old_table_size = read_table_size em1.module_ in
let max x y = if x >= y then x else y in (* use `Int.max` when bumping to 4.13 *)

(* Rust requires a table offset of at least 1 as elem[0] is considered invalid.
There are debug checks panicking if the element index is zero.
On the other hand, elem[0] can be used by the Motoko backend code (em1),
as correct Rust-generated Wasm code does not call elem[0]. *)
let old_table_size = max (read_table_size em1.module_) 1l in
let lib_table_start = align_i32 dylink.table_alignment old_table_size in

let uses_memory64 = uses_memory64 em1.module_ in
Expand Down
2 changes: 1 addition & 1 deletion test/ld/ok/representative.linked.wat.ok
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
i32.mul)
(func $link_start (type 0)
call $__wasm_call_ctors)
(table (;0;) 0 0 funcref)
(table (;0;) 1 1 funcref)
(memory (;0;) i64 2)
(global (;0;) i64 (i64.const 65536))
(start $link_start))