Skip to content

Commit 6d78f7f

Browse files
committed
loader: fix block/loop ref params type checking
1 parent a6a9f1f commit 6d78f7f

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

core/iwasm/interpreter/wasm_loader.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12050,9 +12050,25 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1205012050
WASMFuncType *wasm_type = block_type.u.type;
1205112051

1205212052
BranchBlock *cur_block = loader_ctx->frame_csp - 1;
12053+
#if WASM_ENABLE_GC != 0
12054+
WASMRefType *ref_type;
12055+
uint32 j = 0;
12056+
#endif
1205312057
#if WASM_ENABLE_FAST_INTERP != 0
1205412058
uint32 cell_num;
1205512059
available_params = block_type.u.type->param_count;
12060+
#endif
12061+
#if WASM_ENABLE_GC != 0
12062+
/* find the index of the last param
12063+
* in wasm_type->ref_type_maps as j */
12064+
for (i = 0; i < block_type.u.type->param_count; i++) {
12065+
if (wasm_is_type_multi_byte_type(wasm_type->types[i])) {
12066+
j += 1;
12067+
}
12068+
}
12069+
if (j > 0) {
12070+
j -= 1;
12071+
}
1205612072
#endif
1205712073
for (i = 0; i < block_type.u.type->param_count; i++) {
1205812074

@@ -12066,6 +12082,16 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1206612082
#endif
1206712083
break;
1206812084
}
12085+
#if WASM_ENABLE_GC != 0
12086+
if (wasm_is_type_multi_byte_type(wasm_type->types[wasm_type->param_count - i - 1])) {
12087+
bh_assert(wasm_type->ref_type_maps[j].index == wasm_type->param_count - i - 1);
12088+
ref_type = wasm_type->ref_type_maps[j].ref_type;
12089+
bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
12090+
ref_type,
12091+
wasm_reftype_struct_size(ref_type));
12092+
j--;
12093+
}
12094+
#endif
1206912095

1207012096
POP_TYPE(
1207112097
wasm_type->types[wasm_type->param_count - i - 1]);
157 Bytes
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
;; 定义不同的引用类型
2+
(type $struct_a (struct (field (mut i32))))
3+
(type $struct_b (struct (field (mut i64))))
4+
(type $struct_c (struct (field (mut i32)) (field (mut i32))))
5+
6+
(func $main
7+
;; 准备参数:i32, ref_a, i32, ref_b
8+
(i32.const 10)
9+
(struct.new $struct_a (i32.const 100))
10+
(i32.const 20)
11+
(struct.new $struct_b (i64.const 200))
12+
13+
;; 带交错参数的block:i32, ref_a, i32, ref_b -> ref_c
14+
(block (param i32 (ref $struct_a) i32 (ref $struct_b)) (result (ref $struct_c))
15+
;; 清理栈中的参数
16+
drop ;; 丢弃 ref_b
17+
drop ;; 丢弃 i32
18+
drop ;; 丢弃 ref_a
19+
drop ;; 丢弃 i32
20+
;; 返回新的第三种类型引用
21+
22+
(struct.new $struct_c (i32.const 300) (i32.const 400))
23+
)
24+
25+
;; 丢弃返回值
26+
drop
27+
)
28+
29+
(memory 1)
30+
(export "memory" (memory 0))
31+
(export "_start" (func $main))

tests/regression/ba-issues/running_config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,22 @@
17541754
"stdout content": "",
17551755
"description": "no sanitizer 'heap-buffer-overflow'"
17561756
}
1757+
},
1758+
{
1759+
"deprecated": false,
1760+
"ids": [
1761+
4646
1762+
],
1763+
"runtime": "iwasm-default-gc-enabled",
1764+
"file": "test.wasm",
1765+
"mode": "classic-interp",
1766+
"options": "-f _start",
1767+
"argument": "",
1768+
"expected return": {
1769+
"ret code": 0,
1770+
"stdout content": "",
1771+
"description": "load successfully"
1772+
}
17571773
}
17581774
]
17591775
}

0 commit comments

Comments
 (0)