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

[wasm-reduce] Do not crash on non-func element segments #6778

Merged
merged 4 commits into from
Jul 26, 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
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)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the suggestion above I think one of these two operations will get reduced but not the other.

)
)
)

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)
)
)
)
)

Loading