Skip to content

Commit

Permalink
Fix a panic using tables with the wrong type
Browse files Browse the repository at this point in the history
This commit fixes an accidental issue introduced in bytecodealliance#8018 where using an
element segment which had been dropped with an `externref` table would
cause a panic. The panic happened due to an assertion that tables are
being used with the right type of item and that was being mismatched.
The underlying issue was that dropped element segments are modeled as an
empty element segment but the empty element segment was using the
"functions" encoding as opposed to the "expressions" encoding. This
meant that code later assumed that due to the use of functions the table
must be a table-of-functions, but this was not correct for
externref-based tables.

The fix in this commit is to instead model the encoding as an
"expressions" list which means that the table type is dispatched on to
call the appropriate initializer.

There is no memory safety issue with this mistake as the assertion was
specifically targetted at preventing memory safety. This does, however,
enable any WebAssembly module to panic a host.

Closes bytecodealliance#8281
  • Loading branch information
alexcrichton committed Apr 2, 2024
1 parent 7279e46 commit 734aa7b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/runtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,12 @@ impl Instance {
// disconnected from the lifetime of `self`.
let module = self.module().clone();

let empty = TableSegmentElements::Functions(Box::new([]));
// NB: fall back to an expressions-based list of elements which doesn't
// have static type information (as opposed to `Functions`) since we
// don't know just yet what type the table has. The type will be be
// inferred in the next step within `table_init_segment`.
let empty = TableSegmentElements::Expressions(Box::new([]));

let elements = match module.passive_elements_map.get(&elem_index) {
Some(index) if !self.dropped_elements.contains(elem_index) => {
&module.passive_elements[*index]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(module
(table $2 0 0 externref)
(func (export "table")
(i32.const 0)
(i32.const 0)
(i32.const 0)
(table.init 0 0)
)
(elem $2 declare externref)
)
(assert_return (invoke "table"))

0 comments on commit 734aa7b

Please sign in to comment.