Skip to content

Commit

Permalink
Merge pull request #18117 from vbotbuildovich/backport-pr-18113-v24.1…
Browse files Browse the repository at this point in the history
….x-806

[v24.1.x] wasm/parser: better global support
  • Loading branch information
rockwotj authored Apr 27, 2024
2 parents 8b166a1 + 4dc38b3 commit 28e242a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 100 deletions.
95 changes: 62 additions & 33 deletions src/v/wasm/parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ val_type parse_val_type(iobuf_parser_base* parser) {
}
}

val_type parse_ref_type(iobuf_parser_base* parser) {
auto reftype = parse_val_type(parser);
if (reftype != val_type::externref && reftype != val_type::funcref) {
throw parse_exception(
fmt::format("invalid reftype: {}", uint8_t(reftype)));
}
return reftype;
}

ss::sstring parse_name(iobuf_parser_base* parser) {
auto str_len = leb128::decode<uint32_t>(parser);
if (str_len > max_name_length) {
Expand Down Expand Up @@ -191,8 +200,8 @@ parse_signature_types(iobuf_parser_base* parser, size_t max) {

function_signature parse_signature(iobuf_parser_base* parser) {
auto magic = parser->consume_type<uint8_t>();
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
if (magic != 0x60) {
constexpr uint8_t signature_start_magic_byte = 0x60;
if (magic != signature_start_magic_byte) {
throw parse_exception(
fmt::format("function type magic mismatch: {}", magic));
}
Expand All @@ -215,11 +224,7 @@ declaration::limits parse_limits(iobuf_parser_base* parser) {
}

declaration::table parse_table_type(iobuf_parser_base* parser) {
auto reftype = parse_val_type(parser);
if (reftype != val_type::externref && reftype != val_type::funcref) {
throw parse_exception(
fmt::format("invalid tabletype type: {}", uint8_t(reftype)));
}
auto reftype = parse_ref_type(parser);
auto limits = parse_limits(parser);
return {.reftype = reftype, .limits = limits};
}
Expand All @@ -235,31 +240,42 @@ declaration::global parse_global_type(iobuf_parser_base* parser) {
}

void skip_global_constexpr(iobuf_parser_base* parser) {
auto opcode = parser->consume_type<uint8_t>();
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers)
enum class op : uint8_t {
global_get = 0x23,
i32_const = 0x41,
i64_const = 0x42,
f32_const = 0x43,
f64_const = 0x44,
ref_null = 0xD0,
};
auto opcode = parser->consume_type<op>();
switch (opcode) {
case 0x041:
case op::global_get:
case op::i32_const:
std::ignore = leb128::decode<uint32_t>(parser);
break;
case 0x042:
case op::i64_const:
std::ignore = leb128::decode<uint64_t>(parser);
break;
case 0x043:
case op::f32_const:
std::ignore = parser->consume_type<float>();
break;
case 0x044:
case op::f64_const:
std::ignore = parser->consume_type<double>();
break;
case op::ref_null:
std::ignore = parse_ref_type(parser);
break;
default:
throw parse_exception(
fmt::format("unimplemented global opcode: {}", opcode));
throw parse_exception(fmt::format(
"unimplemented global opcode: {}", static_cast<uint8_t>(opcode)));
}
auto end = parser->consume_type<uint8_t>();
if (end != 0x0B) {
constexpr uint8_t end_expression_marker = 0x0B;
if (end != end_expression_marker) {
throw parse_exception(
fmt::format("expected end of global initalizer, got: {}", end));
}
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers)
}

class module_extractor {
Expand Down Expand Up @@ -326,46 +342,59 @@ class module_extractor {
}
// The size of this section
auto size = leb128::decode<uint32_t>(_parser);
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers)
switch (id) {
case 0x00: // Custom section
enum class section : uint8_t {
custom = 0x00,
type = 0x01,
import = 0x02,
function = 0x03,
table = 0x04,
memory = 0x05,
global = 0x06,
exprt = 0x07, // export
start = 0x08,
element = 0x09,
data_count = 0x0C,
code = 0x0A,
data = 0x0B,
};
switch (static_cast<section>(id)) {
case section::custom:
// Skip over custom sections
_parser->skip(size);
break;
case 0x01: // type section
case section::type:
parse_signature_section();
break;
case 0x02: // import section
case section::import:
parse_import_section();
break;
case 0x03: // function section
case section::function:
parse_function_decl_section();
break;
case 0x04: // table section
case section::table:
parse_table_section();
break;
case 0x05: // memory section
case section::memory:
parse_memories_section();
break;
case 0x06: // global section
case section::global:
parse_globals_section();
break;
case 0x07: // export section
case section::exprt:
parse_export_section();
break;
case 0x08: // start section
case 0x09: // element section
case 0x0C: // data count section
case 0x0A: // code section
case 0x0B: // data section
case section::start:
case section::element:
case section::data_count:
case section::code:
case section::data:
// Since we don't need the information from these sections at the
// moment, we can skip them.
_parser->skip(size);
break;
default:
throw parse_exception(fmt::format("unknown section id: {}", id));
}
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers)
}

void parse_signature_section() {
Expand Down
84 changes: 17 additions & 67 deletions src/v/wasm/parser/tests/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -384,73 +384,23 @@ INSTANTIATE_TEST_SUITE_P(
(global (;7;) i32 (i32.const 234008))
(global (;8;) i32 (i32.const 233968))
(global (;9;) i32 (i32.const 233868))
(global (;10;) i32 (i32.const 233920))
(global (;11;) i32 (i32.const 237064))
(global (;12;) i32 (i32.const 237072))
(global (;13;) i32 (i32.const 237048))
(global (;14;) i32 (i32.const 237056))
(global (;15;) i32 (i32.const 237236))
(global (;16;) i32 (i32.const 237164))
(global (;17;) i32 (i32.const 237228))
(global (;18;) i32 (i32.const 237172))
(global (;19;) i32 (i32.const 220524))
(global (;20;) i32 (i32.const 238992))
(global (;21;) i32 (i32.const 236496))
(global (;22;) i32 (i32.const 236508))
(global (;23;) i32 (i32.const 236452))
(global (;24;) i32 (i32.const 236416))
(global (;25;) i32 (i32.const 237160))
(global (;26;) i32 (i32.const 236352))
(global (;27;) i32 (i32.const 236280))
(global (;28;) i32 (i32.const 235592))
(global (;29;) i32 (i32.const 235528))
(global (;30;) i32 (i32.const 235396))
(global (;31;) i32 (i32.const 235256))
(global (;32;) i32 (i32.const 236212))
(global (;33;) i32 (i32.const 236144))
(global (;34;) i32 (i32.const 236076))
(global (;35;) i32 (i32.const 236008))
(global (;36;) i32 (i32.const 235920))
(global (;37;) i32 (i32.const 235832))
(global (;38;) i32 (i32.const 235744))
(global (;39;) i32 (i32.const 235648))
(global (;40;) i32 (i32.const 235148))
(global (;41;) i32 (i32.const 235032))
(global (;42;) i32 (i32.const 234912))
(global (;43;) i32 (i32.const 234784))
(global (;44;) i32 (i32.const 234516))
(global (;45;) i32 (i32.const 234356))
(global (;46;) i32 (i32.const 234436))
(global (;47;) i32 (i32.const 234276))
(global (;48;) i32 (i32.const 234188))
(global (;49;) i32 (i32.const 234080))
(global (;50;) i32 (i32.const 234752))
(global (;51;) i32 (i32.const 234720))
(global (;52;) i32 (i32.const 450))
(global (;53;) i32 (i32.const 233820))
(global (;54;) i32 (i32.const 237120))
(global (;55;) i32 (i32.const 237112))
(global (;56;) i32 (i32.const 237040))
(global (;57;) i32 (i32.const 237032))
(global (;58;) i32 (i32.const 237024))
(global (;59;) i32 (i32.const 237016))
(global (;60;) i32 (i32.const 237104))
(global (;61;) i32 (i32.const 237096))
(global (;62;) i32 (i32.const 237088))
(global (;63;) i32 (i32.const 237080))
(global (;64;) i32 (i32.const 237008))
(global (;65;) i32 (i32.const 237000))
(global (;66;) i32 (i32.const 236992))
(global (;67;) i32 (i32.const 236984))
(global (;68;) i32 (i32.const 237220))
(global (;69;) i32 (i32.const 237204))
(global (;70;) i32 (i32.const 237212))
(global (;71;) i32 (i32.const 237196))
(global (;72;) i32 (i32.const 237188))
(global (;73;) i32 (i32.const 237180))
(global (;74;) i32 (i32.const 236976))
(global (;75;) i32 (i32.const 236968))
(global (;76;) i32 (i32.const 234048))
(global $a i32 (i32.const -2))
(global (;3;) f32 (f32.const -3))
(global (;4;) f64 (f64.const -4))
(global $b i64 (i64.const -5))
(global $x (mut i32) (i32.const -12))
(global (;7;) (mut f32) (f32.const -13))
(global (;8;) (mut f64) (f64.const -14))
(global $y (mut i64) (i64.const -15))
(global $z1 i32 (global.get 0))
(global $z2 i64 (global.get 1))
(global $r externref (ref.null extern))
(global $mr (mut externref) (ref.null extern))
(global funcref (ref.null func))
)
)WAT",
.exports = {},
Expand Down

0 comments on commit 28e242a

Please sign in to comment.