Skip to content

Commit e7a052a

Browse files
authored
Merge pull request #427 from wasmx/validate-br-table-label-types
Validate br_table label types
2 parents ee1f21d + c5e2a45 commit e7a052a

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

Diff for: circle.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ jobs:
270270
- benchmark:
271271
min_time: "0.01"
272272
- spectest:
273-
expected_passed: 5282
274-
expected_failed: 150
273+
expected_passed: 5283
274+
expected_failed: 149
275275
expected_skipped: 6381
276276

277277
sanitizers-macos:
@@ -288,8 +288,8 @@ jobs:
288288
- benchmark:
289289
min_time: "0.01"
290290
- spectest:
291-
expected_passed: 5282
292-
expected_failed: 150
291+
expected_passed: 5283
292+
expected_failed: 149
293293
expected_skipped: 6381
294294

295295
benchmark:
@@ -400,8 +400,8 @@ jobs:
400400
expected_failed: 9
401401
expected_skipped: 7323
402402
- spectest:
403-
expected_passed: 5282
404-
expected_failed: 150
403+
expected_passed: 5283
404+
expected_failed: 149
405405
expected_skipped: 6381
406406
- collect_coverage_data
407407

Diff for: lib/fizzy/parser_expr.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,16 @@ void validate_result_count(const ControlFrame& frame, const Stack<ValType>& oper
131131
throw validation_error{"missing result"};
132132
}
133133

134-
inline uint8_t get_branch_arity(const ControlFrame& frame) noexcept
134+
inline std::optional<ValType> get_branch_frame_type(const ControlFrame& frame) noexcept
135135
{
136-
const uint8_t arity = frame.type.has_value() ? 1 : 0;
137-
138136
// For loops arity is considered always 0, because br executed in loop jumps to the top,
139137
// resetting frame stack to 0, so it should not keep top stack value even if loop has a result.
140-
return frame.instruction == Instr::loop ? 0 : arity;
138+
return frame.instruction == Instr::loop ? std::nullopt : frame.type;
139+
}
140+
141+
inline uint8_t get_branch_arity(const ControlFrame& frame) noexcept
142+
{
143+
return get_branch_frame_type(frame).has_value() ? 1 : 0;
141144
}
142145

143146
inline void validate_branch_stack_height(const ControlFrame& current_frame,
@@ -579,7 +582,7 @@ parser_result<Code> parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f
579582
throw validation_error{"invalid label index"};
580583

581584
auto& default_branch_frame = control_stack[default_label_idx];
582-
const auto default_branch_arity = get_branch_arity(default_branch_frame);
585+
const auto default_branch_type = get_branch_frame_type(default_branch_frame);
583586

584587
validate_branch_stack_height(frame, default_branch_frame, operand_stack);
585588

@@ -589,7 +592,7 @@ parser_result<Code> parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f
589592
{
590593
auto& branch_frame = control_stack[idx];
591594

592-
if (get_branch_arity(branch_frame) != default_branch_arity)
595+
if (get_branch_frame_type(branch_frame) != default_branch_type)
593596
throw validation_error{"br_table labels have inconsistent types"};
594597

595598
branch_frame.br_immediate_offsets.push_back(code.immediates.size());

Diff for: test/unittests/validation_test.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ TEST(validation, br_table_default_invalid_label_index)
384384
EXPECT_THROW_MESSAGE(parse(wasm), validation_error, "invalid label index");
385385
}
386386

387-
TEST(validation, br_table_invalid_arity)
387+
TEST(validation, br_table_invalid_type)
388388
{
389389
/* wat2wasm --no-check
390390
(func (param $x i32) (result i32)
@@ -450,6 +450,22 @@ TEST(validation, br_table_invalid_arity)
450450
"0061736d0100000001060160017f017f030201000a12011000027f037f410120000e0100010b0b0b");
451451

452452
EXPECT_THROW_MESSAGE(parse(wasm4), validation_error, "br_table labels have inconsistent types");
453+
454+
/* wat2wasm --no-check
455+
(func (param $x i32)
456+
(block $a (result i32)
457+
(block $b (result i64)
458+
i32.const 1
459+
local.get $x
460+
br_table $a $b
461+
)
462+
)
463+
drop
464+
)
465+
*/
466+
const auto wasm5 = from_hex(
467+
"0061736d0100000001050160017f00030201000a13011100027f027e410120000e0101000b0b1a0b");
468+
EXPECT_THROW_MESSAGE(parse(wasm5), validation_error, "br_table labels have inconsistent types");
453469
}
454470

455471
TEST(validation, call_unknown_function)

0 commit comments

Comments
 (0)