Skip to content

Commit 78b4e7c

Browse files
authored
[WebAssembly] validate table.grow correctly (#80437)
This PR add support in wasm asm type checker to implement checker of `table.grow` Fixes: #79966.
1 parent b8d92e1 commit 78b4e7c

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst,
288288
return true;
289289
if (popType(ErrorLoc, wasm::ValType::I32))
290290
return true;
291+
} else if (Name == "table.size") {
292+
if (getTable(Operands[1]->getStartLoc(), Inst, Type))
293+
return true;
294+
Stack.push_back(wasm::ValType::I32);
295+
} else if (Name == "table.grow") {
296+
if (getTable(Operands[1]->getStartLoc(), Inst, Type))
297+
return true;
298+
if (popType(ErrorLoc, wasm::ValType::I32))
299+
return true;
300+
Stack.push_back(wasm::ValType::I32);
291301
} else if (Name == "table.fill") {
292302
if (getTable(Operands[1]->getStartLoc(), Inst, Type))
293303
return true;

llvm/test/MC/WebAssembly/tables.s

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,13 @@ table_set:
7171

7272
# CHECK: table_grow:
7373
# CHECK-NEXT: .functype table_grow (i32) -> (i32)
74-
# CHECK-NEXT: i32.const 0
75-
# CHECK-NEXT: table.get foo
7674
# CHECK-NEXT: local.get 0
7775
# CHECK: table.grow foo
7876
# CHECK-NEXT: local.get 0
7977
# CHECK-NEXT: i32.add
8078
# CHECK-NEXT: end_function
8179
table_grow:
8280
.functype table_grow (i32) -> (i32)
83-
i32.const 0
84-
table.get foo
8581
local.get 0
8682

8783
# ENC: table.grow foo # encoding: [0xfc,0x0f,0x80'A',0x80'A',0x80'A',0x80'A',A]
@@ -149,16 +145,13 @@ table_fill:
149145
# BIN-NEXT: Offset: 0x2D
150146
# BIN-NEXT: - Type: R_WASM_TABLE_NUMBER_LEB
151147
# BIN-NEXT: Index: 0
152-
# BIN-NEXT: Offset: 0x38
153-
# BIN-NEXT: - Type: R_WASM_TABLE_NUMBER_LEB
154-
# BIN-NEXT: Index: 0
155-
# BIN-NEXT: Offset: 0x41
148+
# BIN-NEXT: Offset: 0x39
156149
# BIN-NEXT: - Type: R_WASM_TABLE_NUMBER_LEB
157150
# BIN-NEXT: Index: 2
158-
# BIN-NEXT: Offset: 0x51
151+
# BIN-NEXT: Offset: 0x49
159152
# BIN-NEXT: - Type: R_WASM_TABLE_NUMBER_LEB
160153
# BIN-NEXT: Index: 2
161-
# BIN-NEXT: Offset: 0x5A
154+
# BIN-NEXT: Offset: 0x52
162155
# BIN-NEXT: Functions:
163156
# BIN-NEXT: - Index: 0
164157
# BIN-NEXT: Locals: []
@@ -171,7 +164,7 @@ table_fill:
171164
# BIN-NEXT: Body: 200020012680808080000B
172165
# BIN-NEXT: - Index: 3
173166
# BIN-NEXT: Locals: []
174-
# BIN-NEXT: Body: 41002580808080002000FC0F808080800020006A0B
167+
# BIN-NEXT: Body: 2000FC0F808080800020006A0B
175168
# BIN-NEXT: - Index: 4
176169
# BIN-NEXT: Locals: []
177170
# BIN-NEXT: Body: 200041002582808080002001FC1182808080000B

llvm/test/MC/WebAssembly/type-checker-errors.s

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,26 @@ table_fill_type_mismatch_3:
215215
table.fill valid_table
216216
end_function
217217

218+
table_grow_non_exist_table:
219+
.functype table_grow_non_exist_table () -> (i32)
220+
i32.const 0
221+
# CHECK: [[@LINE+1]]:14: error: symbol invalid_table missing .tabletype
222+
table.grow invalid_table
223+
end_function
224+
225+
table_grow_wrong_parameter:
226+
.functype table_grow_non_exist_table () -> (i32)
227+
# CHECK: [[@LINE+1]]:3: error: empty stack while popping i32
228+
table.grow valid_table
229+
end_function
230+
231+
table_grow_wrong_result:
232+
.functype table_grow_non_exist_table () -> (f32)
233+
i32.const 0
234+
table.grow valid_table
235+
# CHECK: [[@LINE+1]]:3: error: popped i32, expected f32
236+
end_function
237+
218238
drop_empty_stack_while_popping:
219239
.functype drop_empty_stack_while_popping () -> ()
220240
# CHECK: :[[@LINE+1]]:3: error: empty stack while popping value

0 commit comments

Comments
 (0)