Skip to content

Commit

Permalink
[wasm-reduce] Do not crash on non-func element segments (#6778)
Browse files Browse the repository at this point in the history
Generalize the code for simplifying element segments to handle more than
just null and funcref elements.
  • Loading branch information
tlively authored Jul 26, 2024
1 parent 9cc1cb1 commit f938154
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
15 changes: 5 additions & 10 deletions src/tools/wasm-reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,18 +854,13 @@ struct Reducer
reduceByZeroing(
segment.get(),
first,
[&](Expression* entry) {
if (entry->is<RefNull>()) {
// we don't need to replace a ref.null
[&](Expression* elem) {
if (elem->is<RefNull>()) {
// We don't need to replace a ref.null.
return true;
} else if (first->is<RefNull>()) {
return false;
} else {
// Both are ref.func
auto* f = first->cast<RefFunc>();
auto* e = entry->cast<RefFunc>();
return f->func == e->func;
}
// Is the element equal to our first "zero" element?
return ExpressionAnalyzer::equal(first, elem);
},
1,
shrank);
Expand Down
11 changes: 10 additions & 1 deletion test/reduce/memory_table.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
(type $i (func (result i32)))
(memory $0 256 256)
(table 481 481 funcref)
(table 354 354 i31ref)
(elem (i32.const 0) $f0 $f0 $f1 $f2 $f0 $f3 $f0)
(elem (table 1) (i32.const 0) i31ref
(item (ref.i31 (i32.const 0))) (item (ref.i31 (i32.const 42))) (item (ref.i31 (i32.const 99))))
(data (i32.const 0) "p\0bflkj")
(data (i32.const 10960) "1234hello")
(export "f1" (func $f1))
(export "f2" (func $f2))
(export "f4" (func $f4))
(export "f5" (func $f5))
(func $f0 (result i32)
(i32.const 1234)
)
Expand All @@ -27,5 +31,10 @@
(call_indirect (type $i) (i32.const 0))
)
)
(func $f5 (result i32)
(i32.add
(i31.get_s (table.get 1 (i32.const 0)))
(i31.get_u (table.get 1 (i32.const 1)))
)
)
)

14 changes: 14 additions & 0 deletions test/reduce/memory_table.wast.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
(type $0 (func (result i32)))
(type $1 (func))
(memory $0 256 256)
(table $0 354 354 i31ref)
(elem $0 (table $0) (i32.const 0) i31ref (item (ref.i31
(i32.const 0)
)) (item (ref.i31
(i32.const 42)
)))
(export "f1" (func $0))
(export "f2" (func $1))
(export "f4" (func $2))
(export "f5" (func $3))
(func $0
)
(func $1 (result i32)
Expand All @@ -22,5 +29,12 @@
(i32.const 1234)
)
)
(func $3 (result i32)
(i31.get_u
(table.get $0
(i32.const 1)
)
)
)
)

0 comments on commit f938154

Please sign in to comment.