Skip to content

Commit ea7ac26

Browse files
authored
Merge commit from fork
1 parent d2a7b25 commit ea7ac26

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

core/iwasm/interpreter/wasm_loader.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9572,6 +9572,16 @@ preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
95729572

95739573
/* preserve locals before blocks to ensure that "tee/set_local" inside
95749574
blocks will not influence the value of these locals */
9575+
uint32 frame_offset_cell =
9576+
(uint32)(loader_ctx->frame_offset - loader_ctx->frame_offset_bottom);
9577+
uint32 frame_ref_cell =
9578+
(uint32)(loader_ctx->frame_ref - loader_ctx->frame_ref_bottom);
9579+
if (frame_offset_cell < loader_ctx->stack_cell_num
9580+
|| frame_ref_cell < loader_ctx->stack_cell_num) {
9581+
set_error_buf(error_buf, error_buf_size, "stack cell num error");
9582+
return false;
9583+
}
9584+
95759585
while (i < loader_ctx->stack_cell_num) {
95769586
int16 cur_offset = loader_ctx->frame_offset_bottom[i];
95779587
uint8 cur_type = loader_ctx->frame_ref_bottom[i];
@@ -11928,13 +11938,19 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1192811938
break;
1192911939
}
1193011940

11941+
uint8 *frame_ref_before_pop = loader_ctx->frame_ref;
1193111942
POP_TYPE(
1193211943
wasm_type->types[wasm_type->param_count - i - 1]);
1193311944
#if WASM_ENABLE_FAST_INTERP != 0
1193411945
/* decrease the frame_offset pointer accordingly to keep
11935-
* consistent with frame_ref stack */
11936-
cell_num = wasm_value_type_cell_num(
11937-
wasm_type->types[wasm_type->param_count - i - 1]);
11946+
* consistent with frame_ref stack. Use the actual
11947+
* popped cell count instead of
11948+
* wasm_value_type_cell_num() because when the stack top
11949+
* is VALUE_TYPE_ANY, wasm_loader_pop_frame_ref always
11950+
* pops exactly 1 cell regardless of the expected type
11951+
*/
11952+
cell_num = (uint32)(frame_ref_before_pop
11953+
- loader_ctx->frame_ref);
1193811954
loader_ctx->frame_offset -= cell_num;
1193911955

1194011956
if (loader_ctx->frame_offset
Binary file not shown.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
(module
2+
(global $g0 (mut i32) (i32.const 0))
3+
(global $g1 (mut i32) (i32.const 0))
4+
(global $g2 (mut i32) (i32.const 0))
5+
(global $g3 (mut i32) (i32.const 0))
6+
(global $g4 (mut i32) (i32.const 0))
7+
(global $g5 (mut i32) (i32.const 0))
8+
(global $g6 (mut i32) (i32.const 0))
9+
(global $g7 (mut i32) (i32.const 0))
10+
11+
(export "test" (func $0))
12+
(func $0
13+
(local i32)
14+
15+
global.get $g0
16+
global.get $g1
17+
global.get $g2
18+
global.get $g3
19+
global.get $g4
20+
global.get $g5
21+
global.get $g6
22+
global.get $g7
23+
global.get $g0
24+
global.get $g1
25+
global.get $g2
26+
global.get $g3
27+
global.get $g4
28+
global.get $g5
29+
global.get $g6
30+
global.get $g7
31+
global.get $g0
32+
global.get $g1
33+
global.get $g2
34+
global.get $g3
35+
global.get $g4
36+
global.get $g4
37+
global.get $g4
38+
global.get $g4
39+
global.get $g4
40+
global.get $g4
41+
global.get $g4
42+
global.get $g4
43+
global.get $g4
44+
global.get $g0
45+
46+
;; has consumed 30 elements, left 2 elements on stack
47+
block
48+
block
49+
f64.const 3.14
50+
;; RESET current block stack and mark polymorphic
51+
unreachable
52+
;; PUSH ANY
53+
select
54+
55+
loop (param i64) (result i32)
56+
;; NOW, unmatched stacks. Enlarge frame_ref stack. Keep frame_offset stack unchanged.
57+
global.get $g0
58+
i32.eqz
59+
;; OUT-OF-BOUNDS
60+
if
61+
unreachable
62+
end
63+
i32.wrap_i64
64+
end
65+
local.set 0
66+
end
67+
end
68+
unreachable
69+
)
70+
)

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+
980000
1762+
],
1763+
"runtime": "iwasm-default",
1764+
"file": "frame_offset_overflow.wasm",
1765+
"mode": "fast-interp",
1766+
"options": "-f test",
1767+
"argument": "",
1768+
"expected return": {
1769+
"ret code": 1,
1770+
"stdout content": "Exception: unreachable",
1771+
"description": "no 'frame offset overflow'"
1772+
}
17571773
}
17581774
]
17591775
}

0 commit comments

Comments
 (0)