-
Notifications
You must be signed in to change notification settings - Fork 821
Fix validation of typed select with empty result types (#2708) #2810
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
Changes from 3 commits
ba20b0e
435c6ad
73d0f3b
e6d3387
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -589,8 +589,21 @@ Result Validator::OnReturnCallRefExpr(ReturnCallRefExpr* expr) { | |
| } | ||
|
|
||
| Result Validator::OnSelectExpr(SelectExpr* expr) { | ||
| result_ |= validator_.OnSelect(expr->loc, expr->result_type.size(), | ||
| expr->result_type.data()); | ||
| Index result_count; | ||
| Type* result_types = nullptr; | ||
|
|
||
| if (expr->result_type.empty()) { | ||
| validator_.PrintError(expr->loc, "invalid arity in select instruction: 0."); | ||
| result_ |= Result::Error; | ||
| result_count = 0; | ||
| } else if (expr->IsUntyped()) { | ||
| result_count = 0; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @zherczeg I have simplified this by initializing result_count to 0. |
||
| } else { | ||
| result_count = expr->result_type.size(); | ||
| result_types = expr->result_type.data(); | ||
| } | ||
|
|
||
| result_ |= validator_.OnSelect(expr->loc, result_count, result_types); | ||
| return Result::Ok; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -977,7 +977,12 @@ Result WatWriter::ExprVisitorDelegate::OnReturnCallRefExpr( | |
|
|
||
| Result WatWriter::ExprVisitorDelegate::OnSelectExpr(SelectExpr* expr) { | ||
| writer_->WritePutsSpace(Opcode::Select_Opcode.GetName()); | ||
| if (!expr->result_type.empty()) { | ||
| if (expr->IsUntyped()) { | ||
| // no result annotation | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually don't prefer empty statements.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Updated it to avoid the empty branch. |
||
| } else if (expr->result_type.empty()) { | ||
| writer_->WriteOpenSpace("result"); | ||
| writer_->WriteCloseSpace(); | ||
| } else { | ||
| writer_->WriteTypes(expr->result_type, "result"); | ||
| } | ||
| writer_->WriteNewline(NO_FORCE_NEWLINE); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ;;; TOOL: run-gen-wasm-bad | ||
| ;;; ERROR: 1 | ||
| ;;; RUN: %(wasm-interp)s --enable-all %(temp_file)s.wasm --run-export=run | ||
| ;;; ERROR3: 1 | ||
| magic | ||
| version | ||
| section(TYPE) { count[1] function params[0] results[1] i32 } | ||
| section(FUNCTION) { count[1] type[0] } | ||
| section(EXPORT) { count[1] str("run") func_kind 0 } | ||
| section(CODE) { | ||
| count[1] | ||
| func { | ||
| locals[0] | ||
| i32.const 42 | ||
| i32.const 100 | ||
| i32.const 1 | ||
| select_t[0x1c] | ||
| result_count[0x00] | ||
| end | ||
| } | ||
| } | ||
| (;; STDERR ;;; | ||
| 0000029: error: invalid arity in select instruction: 0. | ||
| 0000029: error: invalid arity in select instruction: 0. | ||
| 0000029: error: invalid arity in select instruction: 0. | ||
| ;;; STDERR ;;) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| ;;; TOOL: wat2wasm | ||
| ;;; ERROR: 1 | ||
| (module | ||
| (func | ||
| i32.const 42 | ||
| i32.const 100 | ||
| i32.const 1 | ||
| select (result) | ||
| drop)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should not this be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zherczeg You're right. Only the i32 condition should be consumed here. I updated the recovery logic and changed the test to drop drop. |
||
| (;; STDERR ;;; | ||
| out/test/parse/expr/bad-select-empty-result.txt:8:5: error: invalid arity in select instruction: 0. | ||
| select (result) | ||
| ^^^^^^ | ||
| ;;; STDERR ;;) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need {}?
expr_ptr->result_type = Type::Void;should workThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zherczeg since SelectExpr already defaults to {Type::Void}, I simplified it to only assign when result_count != 0.