Skip to content

Commit

Permalink
[wasm-reduce] Do not crash on non-func element segments
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 committed Jul 19, 2024
1 parent cf0d21a commit 2c055b3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/tools/wasm-reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,18 +854,14 @@ 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;
}
// Replace the element if it is different from our first "zero"
// element.
return !ExpressionAnalyzer::equal(first, elem);
},
1,
shrank);
Expand Down
10 changes: 9 additions & 1 deletion test/reduce/memory_table.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
(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 1))))
(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 +30,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)))
)
)
)

4 changes: 4 additions & 0 deletions test/reduce/memory_table.wast.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(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 +23,8 @@
(i32.const 1234)
)
)
(func $3 (result i32)
(i32.const 1)
)
)

0 comments on commit 2c055b3

Please sign in to comment.