-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate exports #357
Validate exports #357
Conversation
Codecov Report
@@ Coverage Diff @@
## master #357 +/- ##
========================================
Coverage 98.84% 98.84%
========================================
Files 38 38
Lines 11468 11571 +103
========================================
+ Hits 11335 11437 +102
- Misses 133 134 +1 |
38a45eb
to
05a3574
Compare
throw validation_error{"invalid index of an exported function"}; | ||
break; | ||
case ExternalKind::Table: | ||
if (export_.index != 0 || !module.has_table()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to using module
class helpers
module.imported_globals_mutability.size() + module.globalsec.size(); | ||
|
||
// Validate exports. | ||
std::unordered_set<std::string_view> export_names; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this much faster than vector
or what is the reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With set it is straight-forward to check if element exists.
lib/fizzy/parser.cpp
Outdated
default: | ||
assert(false); | ||
} | ||
if (const auto [_, inserted] = export_names.emplace(export_.name); !inserted) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I so much dislike this syntax of C++17 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!std::get<1>(export_names.emplace(export_.name))
isn't nicer.
But this entire thing is in a for loop, so why not just keep these as two statements (like the rest of codebase)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export_names.emplace(export_.name).second
will work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed it to @chfast's suggestion
EXPECT_EQ(module.exportsec[3].name, "xyz"); | ||
EXPECT_EQ(module.exportsec[3].kind, ExternalKind::Global); | ||
EXPECT_EQ(module.exportsec[3].index, 0x45); | ||
EXPECT_EQ(module.exportsec[3].index, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too bad the old test was invalid, but nicer to see what we're testing.
test/unittests/validation_test.cpp
Outdated
@@ -231,3 +231,68 @@ TEST(validation, call_indirect_no_table) | |||
const auto wasm = from_hex("0061736d0100000001050160017f00030201000a0901070020001101000b"); | |||
EXPECT_THROW_MESSAGE(parse(wasm), validation_error, "call_indirect without defined table"); | |||
} | |||
|
|||
TEST(validation, export_invald_index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invald -> invalid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
(export "m" (memory 1)) | ||
*/ | ||
const auto wasm_mem = from_hex("0061736d010000000503010001070501016d0201"); | ||
EXPECT_THROW_MESSAGE(parse(wasm_mem), validation_error, "invalid index of an exported memory"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have different message whether memory/table section is present or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine - 0 is invalid index in some context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change looks good in general, please fix the typo before merging. I'm not too strong on the if
statement. Whether to have different message based on table/memory section being present, that is probably a useful question.
Cases with exporting table/memory 0 when none is defined are missing from the spectests, added to the "to upstream" list. |
No description provided.