Skip to content

[WebAssembly] validate table.grow correctly #80437

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

Merged
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
10 changes: 10 additions & 0 deletions llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst,
return true;
if (popType(ErrorLoc, wasm::ValType::I32))
return true;
} else if (Name == "table.size") {
if (getTable(Operands[1]->getStartLoc(), Inst, Type))
return true;
Stack.push_back(wasm::ValType::I32);
} else if (Name == "table.grow") {
if (getTable(Operands[1]->getStartLoc(), Inst, Type))
return true;
if (popType(ErrorLoc, wasm::ValType::I32))
return true;
Stack.push_back(wasm::ValType::I32);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wow, this is pretty broken.

} else if (Name == "table.fill") {
if (getTable(Operands[1]->getStartLoc(), Inst, Type))
return true;
Expand Down
15 changes: 4 additions & 11 deletions llvm/test/MC/WebAssembly/tables.s
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,13 @@ table_set:

# CHECK: table_grow:
# CHECK-NEXT: .functype table_grow (i32) -> (i32)
# CHECK-NEXT: i32.const 0
# CHECK-NEXT: table.get foo
# CHECK-NEXT: local.get 0
# CHECK: table.grow foo
# CHECK-NEXT: local.get 0
# CHECK-NEXT: i32.add
# CHECK-NEXT: end_function
table_grow:
.functype table_grow (i32) -> (i32)
i32.const 0
table.get foo
local.get 0

# ENC: table.grow foo # encoding: [0xfc,0x0f,0x80'A',0x80'A',0x80'A',0x80'A',A]
Expand Down Expand Up @@ -149,16 +145,13 @@ table_fill:
# BIN-NEXT: Offset: 0x2D
# BIN-NEXT: - Type: R_WASM_TABLE_NUMBER_LEB
# BIN-NEXT: Index: 0
# BIN-NEXT: Offset: 0x38
# BIN-NEXT: - Type: R_WASM_TABLE_NUMBER_LEB
# BIN-NEXT: Index: 0
# BIN-NEXT: Offset: 0x41
# BIN-NEXT: Offset: 0x39
# BIN-NEXT: - Type: R_WASM_TABLE_NUMBER_LEB
# BIN-NEXT: Index: 2
# BIN-NEXT: Offset: 0x51
# BIN-NEXT: Offset: 0x49
# BIN-NEXT: - Type: R_WASM_TABLE_NUMBER_LEB
# BIN-NEXT: Index: 2
# BIN-NEXT: Offset: 0x5A
# BIN-NEXT: Offset: 0x52
# BIN-NEXT: Functions:
# BIN-NEXT: - Index: 0
# BIN-NEXT: Locals: []
Expand All @@ -171,7 +164,7 @@ table_fill:
# BIN-NEXT: Body: 200020012680808080000B
# BIN-NEXT: - Index: 3
# BIN-NEXT: Locals: []
# BIN-NEXT: Body: 41002580808080002000FC0F808080800020006A0B
# BIN-NEXT: Body: 2000FC0F808080800020006A0B
# BIN-NEXT: - Index: 4
# BIN-NEXT: Locals: []
# BIN-NEXT: Body: 200041002582808080002001FC1182808080000B
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/MC/WebAssembly/type-checker-errors.s
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,26 @@ table_fill_type_mismatch_3:
table.fill valid_table
end_function

table_grow_non_exist_table:
.functype table_grow_non_exist_table () -> (i32)
i32.const 0
# CHECK: [[@LINE+1]]:14: error: symbol invalid_table missing .tabletype
table.grow invalid_table
end_function

table_grow_wrong_parameter:
.functype table_grow_non_exist_table () -> (i32)
# CHECK: [[@LINE+1]]:3: error: empty stack while popping i32
table.grow valid_table
end_function

table_grow_wrong_result:
.functype table_grow_non_exist_table () -> (f32)
i32.const 0
table.grow valid_table
# CHECK: [[@LINE+1]]:3: error: popped i32, expected f32
end_function

drop_empty_stack_while_popping:
.functype drop_empty_stack_while_popping () -> ()
# CHECK: :[[@LINE+1]]:3: error: empty stack while popping value
Expand Down