srctree

Robin Linden parent ea2b5d58 7f08dae9
wasm: Give the module parsing errors a more specific name

inlinesplit
wasm/wasm.cpp added: 21, removed: 18, total 3
@@ -220,21 +220,21 @@ std::optional<ValueType> ValueType::parse(std::istream &is) {
}
}
 
tl::expected<Module, ParseError> Module::parse_from(std::istream &is) {
tl::expected<Module, ModuleParseError> Module::parse_from(std::istream &is) {
std::string buf;
 
// https://webassembly.github.io/spec/core/binary/modules.html#binary-magic
buf.resize(kMagicSize);
is.read(buf.data(), buf.size());
if (!is || buf != "\0asm"sv) {
return tl::unexpected{ParseError::InvalidMagic};
return tl::unexpected{ModuleParseError::InvalidMagic};
}
 
// https://webassembly.github.io/spec/core/binary/modules.html#binary-version
buf.resize(kVersionSize);
is.read(buf.data(), buf.size());
if (!is || buf != "\1\0\0\0"sv) {
return tl::unexpected{ParseError::UnsupportedVersion};
return tl::unexpected{ModuleParseError::UnsupportedVersion};
}
 
Module module;
@@ -249,20 +249,20 @@ tl::expected<Module, ParseError> Module::parse_from(std::istream &is) {
}
 
if (!(id >= static_cast<int>(SectionId::Custom) && id <= static_cast<int>(SectionId::DataCount))) {
return tl::unexpected{ParseError::InvalidSectionId};
return tl::unexpected{ModuleParseError::InvalidSectionId};
}
 
// TODO(robinlinden): Propagate error from leb128-parsing.
auto size = Leb128<std::uint32_t>::decode_from(is);
if (!size) {
return tl::unexpected{ParseError::Unknown};
return tl::unexpected{ModuleParseError::Unknown};
}
 
std::vector<std::uint8_t> content;
content.resize(*size);
is.read(reinterpret_cast<char *>(content.data()), *size);
if (!is) {
return tl::unexpected{ParseError::UnexpectedEof};
return tl::unexpected{ModuleParseError::UnexpectedEof};
}
 
module.sections.push_back(Section{static_cast<SectionId>(id), std::move(content)});
 
wasm/wasm.h added: 21, removed: 18, total 3
@@ -129,7 +129,7 @@ struct CodeSection {
[[nodiscard]] bool operator==(CodeSection const &) const = default;
};
 
enum class ParseError {
enum class ModuleParseError {
Unknown,
UnexpectedEof,
InvalidMagic,
@@ -139,8 +139,8 @@ enum class ParseError {
 
// https://webassembly.github.io/spec/core/syntax/modules.html
struct Module {
static tl::expected<Module, ParseError> parse_from(std::istream &&is) { return parse_from(is); }
static tl::expected<Module, ParseError> parse_from(std::istream &);
static tl::expected<Module, ModuleParseError> parse_from(std::istream &&is) { return parse_from(is); }
static tl::expected<Module, ModuleParseError> parse_from(std::istream &);
 
std::vector<Section> sections{};
 
 
wasm/wasm_test.cpp added: 21, removed: 18, total 3
@@ -372,12 +372,12 @@ void code_section_tests() {
int main() {
etest::test("invalid magic", [] {
auto wasm_bytes = std::stringstream{"hello"};
expect_eq(wasm::Module::parse_from(wasm_bytes), tl::unexpected{wasm::ParseError::InvalidMagic});
expect_eq(wasm::Module::parse_from(wasm_bytes), tl::unexpected{wasm::ModuleParseError::InvalidMagic});
});
 
etest::test("unsupported version", [] {
auto wasm_bytes = std::stringstream{"\0asm\2\0\0\0"s};
expect_eq(wasm::Module::parse_from(wasm_bytes), tl::unexpected{wasm::ParseError::UnsupportedVersion});
expect_eq(wasm::Module::parse_from(wasm_bytes), tl::unexpected{wasm::ModuleParseError::UnsupportedVersion});
});
 
// https://webassembly.github.io/spec/core/syntax/modules.html
@@ -389,22 +389,25 @@ int main() {
 
etest::test("invalid section id", [] {
auto wasm_bytes = std::stringstream{"\0asm\1\0\0\0\x0d"s};
expect_eq(wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ParseError::InvalidSectionId});
expect_eq(wasm::Module::parse_from(std::move(wasm_bytes)),
tl::unexpected{wasm::ModuleParseError::InvalidSectionId});
});
 
etest::test("missing size", [] {
auto wasm_bytes = std::stringstream{"\0asm\1\0\0\0\0"s};
expect_eq(wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ParseError::Unknown});
expect_eq(wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ModuleParseError::Unknown});
});
 
etest::test("missing content", [] {
auto wasm_bytes = std::stringstream{"\0asm\1\0\0\0\0\4"s};
expect_eq(wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ParseError::UnexpectedEof});
expect_eq(
wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ModuleParseError::UnexpectedEof});
});
 
etest::test("not enough content", [] {
auto wasm_bytes = std::stringstream{"\0asm\1\0\0\0\0\4\0\0\0"s};
expect_eq(wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ParseError::UnexpectedEof});
expect_eq(
wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ModuleParseError::UnexpectedEof});
});
 
etest::test("one valid section", [] {