Skip to content

Commit

Permalink
wasm/parser: remove magic number warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
  • Loading branch information
rockwotj committed Apr 26, 2024
1 parent 36326c5 commit 216a07a
Showing 1 changed file with 50 additions and 30 deletions.
80 changes: 50 additions & 30 deletions src/v/wasm/parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,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 Down Expand Up @@ -240,35 +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 0x23: // global.get
case 0x41: // i32.const
case op::global_get:
case op::i32_const:
std::ignore = leb128::decode<uint32_t>(parser);
break;
case 0x42: // i64.const
case op::i64_const:
std::ignore = leb128::decode<uint64_t>(parser);
break;
case 0x43: // f32.const
case op::f32_const:
std::ignore = parser->consume_type<float>();
break;
case 0x44: // f64.const
case op::f64_const:
std::ignore = parser->consume_type<double>();
break;
case 0xD0: // ref.null
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 @@ -335,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

0 comments on commit 216a07a

Please sign in to comment.