Skip to content

Commit bdf7dfc

Browse files
cemonemwucke13
authored andcommitted
fix: allow stack to stay ununified when block types are asserted for br,br_table,return
Signed-off-by: Cem Onem <cem.oenem@dlr.de>
1 parent 4b7eeaf commit bdf7dfc

File tree

5 files changed

+127
-68
lines changed

5 files changed

+127
-68
lines changed

src/core/reader/types/element.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ impl ElemType {
132132
// },
133133
// };
134134

135-
valid_stack.assert_val_types(&[super::ValType::NumType(super::NumType::I32)])?;
135+
valid_stack
136+
.assert_val_types(&[super::ValType::NumType(super::NumType::I32)], true)?;
136137

137138
ElemMode::Active(ActiveElem {
138139
table_idx,

src/validation/code.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,11 @@ fn validate_intrablock_jump_and_generate_sidetable_entry(
168168
label_idx: usize,
169169
stack: &mut ValidationStack,
170170
sidetable: &mut Sidetable,
171+
unify_to_expected_types: bool,
171172
) -> Result<()> {
172173
let ctrl_stack_len = stack.ctrl_stack.len();
173174

174-
stack.assert_val_types_of_label_jump_types_on_top(label_idx)?;
175+
stack.assert_val_types_of_label_jump_types_on_top(label_idx, unify_to_expected_types)?;
175176

176177
let targeted_ctrl_block_entry = stack
177178
.ctrl_stack
@@ -228,15 +229,15 @@ fn read_instructions(
228229
let label_info = LabelInfo::Block {
229230
stps_to_backpatch: Vec::new(),
230231
};
231-
stack.assert_push_ctrl(label_info, block_ty)?;
232+
stack.assert_push_ctrl(label_info, block_ty, true)?;
232233
}
233234
LOOP => {
234235
let block_ty = BlockType::read(wasm)?.as_func_type(fn_types)?;
235236
let label_info = LabelInfo::Loop {
236237
ip: wasm.pc,
237238
stp: sidetable.len(),
238239
};
239-
stack.assert_push_ctrl(label_info, block_ty)?;
240+
stack.assert_push_ctrl(label_info, block_ty, true)?;
240241
}
241242
IF => {
242243
let block_ty = BlockType::read(wasm)?.as_func_type(fn_types)?;
@@ -255,10 +256,10 @@ fn read_instructions(
255256
stp: stp_here,
256257
stps_to_backpatch: Vec::new(),
257258
};
258-
stack.assert_push_ctrl(label_info, block_ty)?;
259+
stack.assert_push_ctrl(label_info, block_ty, true)?;
259260
}
260261
ELSE => {
261-
let (mut label_info, block_ty) = stack.assert_pop_ctrl()?;
262+
let (mut label_info, block_ty) = stack.assert_pop_ctrl(true)?;
262263
if let LabelInfo::If {
263264
stp,
264265
stps_to_backpatch,
@@ -291,23 +292,23 @@ fn read_instructions(
291292
stack.push_valtype(*valtype);
292293
}
293294

294-
stack.assert_push_ctrl(label_info, block_ty)?;
295+
stack.assert_push_ctrl(label_info, block_ty, true)?;
295296
} else {
296297
return Err(Error::ElseWithoutMatchingIf);
297298
}
298299
}
299300
BR => {
300301
let label_idx = wasm.read_var_u32()? as LabelIdx;
301302
validate_intrablock_jump_and_generate_sidetable_entry(
302-
wasm, label_idx, stack, sidetable,
303+
wasm, label_idx, stack, sidetable, false,
303304
)?;
304305
stack.make_unspecified()?;
305306
}
306307
BR_IF => {
307308
let label_idx = wasm.read_var_u32()? as LabelIdx;
308309
stack.assert_pop_val_type(ValType::NumType(NumType::I32))?;
309310
validate_intrablock_jump_and_generate_sidetable_entry(
310-
wasm, label_idx, stack, sidetable,
311+
wasm, label_idx, stack, sidetable, true,
311312
)?;
312313
}
313314
BR_TABLE => {
@@ -316,7 +317,7 @@ fn read_instructions(
316317
stack.assert_pop_val_type(ValType::NumType(NumType::I32))?;
317318
for label_idx in &label_vec {
318319
validate_intrablock_jump_and_generate_sidetable_entry(
319-
wasm, *label_idx, stack, sidetable,
320+
wasm, *label_idx, stack, sidetable, false,
320321
)?;
321322
}
322323

@@ -325,6 +326,7 @@ fn read_instructions(
325326
max_label_idx,
326327
stack,
327328
sidetable,
329+
false,
328330
)?;
329331

330332
// The label arity of the branches must be explicitly checked against each other further
@@ -354,7 +356,7 @@ fn read_instructions(
354356
stack.make_unspecified()?;
355357
}
356358
END => {
357-
let (label_info, block_ty) = stack.assert_pop_ctrl()?;
359+
let (label_info, block_ty) = stack.assert_pop_ctrl(true)?;
358360
let stp_here = sidetable.len();
359361

360362
match label_info {
@@ -408,7 +410,7 @@ fn read_instructions(
408410
RETURN => {
409411
let label_idx = stack.ctrl_stack.len() - 1; // return behaves the same as br <most_outer>
410412
validate_intrablock_jump_and_generate_sidetable_entry(
411-
wasm, label_idx, stack, sidetable,
413+
wasm, label_idx, stack, sidetable, false,
412414
)?;
413415
stack.make_unspecified()?;
414416
}
@@ -492,7 +494,7 @@ fn read_instructions(
492494
LOCAL_TEE => {
493495
let local_idx = wasm.read_var_u32()? as LocalIdx;
494496
let local_ty = locals.get(local_idx).ok_or(Error::InvalidLocalIdx)?;
495-
stack.assert_val_types_on_top(&[*local_ty])?;
497+
stack.assert_val_types_on_top(&[*local_ty], true)?;
496498
}
497499
// global.get [] -> [t]
498500
GLOBAL_GET => {

src/validation/data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(super) fn validate_data_section(
3838
read_constant_expression(wasm, &mut valid_stack, imported_global_types, None)?
3939
};
4040

41-
valid_stack.assert_val_types(&[ValType::NumType(NumType::I32)])?;
41+
valid_stack.assert_val_types(&[ValType::NumType(NumType::I32)], true)?;
4242

4343
let byte_vec = wasm.read_vec(|el| el.read_u8())?;
4444

@@ -77,7 +77,7 @@ pub(super) fn validate_data_section(
7777
read_constant_expression(wasm, &mut valid_stack, imported_global_types, None)?
7878
};
7979

80-
valid_stack.assert_val_types(&[ValType::NumType(NumType::I32)])?;
80+
valid_stack.assert_val_types(&[ValType::NumType(NumType::I32)], true)?;
8181

8282
let byte_vec = wasm.read_vec(|el| el.read_u8())?;
8383

src/validation/globals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(super) fn validate_global_section(
2525
let stack = &mut ValidationStack::new();
2626
let init_expr = read_constant_expression(wasm, stack, imported_global_types, None)?;
2727

28-
stack.assert_val_types(&[ty.ty])?;
28+
stack.assert_val_types(&[ty.ty], true)?;
2929

3030
Ok(Global { ty, init_expr })
3131
})

0 commit comments

Comments
 (0)