Skip to content
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 local count in local.set/get/tee #363

Merged
merged 7 commits into from
Jun 24, 2020
Merged

Validate local count in local.set/get/tee #363

merged 7 commits into from
Jun 24, 2020

Conversation

axic
Copy link
Member

@axic axic commented May 29, 2020

No description provided.

lib/fizzy/parser_expr.cpp Outdated Show resolved Hide resolved
uint64_t local_count = 0;
for (const auto& l : locals_vec)
{
local_count += l.count;
if (local_count > std::numeric_limits<uint32_t>::max())
throw parser_error{"too many local variables"};
}

auto [code, pos2] = parse_expr(pos1, end, static_cast<uint32_t>(local_count), func_idx, module);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First I tried to pass on locals_vec but realised it would need to do this counting again. We can refactor this when type checking is introduced, because then we'll need the types of locals.

@@ -564,6 +566,13 @@ parser_result<Code> parse_expr(
{
uint32_t imm;
std::tie(imm, pos) = leb128u_decode<uint32_t>(pos, end);

if (instr == Instr::local_get || instr == Instr::local_set)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add local_tee here. However it seems that doesn't change the number of successful validation tests, which suggests they are not covered upstream.

@axic axic force-pushed the validate-locals branch 3 times, most recently from 2320ef5 to 4d72375 Compare June 2, 2020 21:57
@codecov
Copy link

codecov bot commented Jun 2, 2020

Codecov Report

Merging #363 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff            @@
##           master     #363    +/-   ##
========================================
  Coverage   99.32%   99.32%            
========================================
  Files          42       42            
  Lines       12692    12829   +137     
========================================
+ Hits        12606    12743   +137     
  Misses         86       86            

lib/fizzy/parser_expr.cpp Outdated Show resolved Hide resolved
lib/fizzy/parser_expr.cpp Outdated Show resolved Hide resolved
@axic axic requested review from chfast and gumb0 and removed request for chfast June 2, 2020 22:06
@axic axic marked this pull request as ready for review June 2, 2020 22:08
lib/fizzy/parser.hpp Outdated Show resolved Hide resolved
lib/fizzy/parser_expr.cpp Show resolved Hide resolved
lib/fizzy/parser_expr.cpp Outdated Show resolved Hide resolved
lib/fizzy/parser_expr.cpp Outdated Show resolved Hide resolved
lib/fizzy/parser_expr.cpp Outdated Show resolved Hide resolved
@axic axic force-pushed the validate-locals branch 4 times, most recently from 327178a to 62a933e Compare June 3, 2020 09:31
@axic axic requested a review from chfast June 3, 2020 09:32
@@ -148,6 +150,12 @@ parser_result<Code> parse_expr(
// The function's implicit block.
control_stack.emplace(Instr::block, function_arity, 0);

// This is needed for aiding validation.
assert((uint64_t{local_count} + module.typesec[func_type_idx].inputs.size()) <=
Copy link
Member Author

@axic axic Jun 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should make this a proper parser error... or is this more like a missing case in the spec?!

Copy link
Collaborator

@chfast chfast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also validation tests are missing.

lib/fizzy/parser_expr.cpp Outdated Show resolved Hide resolved
@axic axic force-pushed the validate-locals branch 2 times, most recently from d04fa37 to cc6693e Compare June 3, 2020 11:26
const uint8_t* input, const uint8_t* end, FuncIdx func_idx, const Module& module);
/// @param input The beginning of the expr binary input.
/// @param end The end of the binary input.
/// @param local_count The number of locals defined.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are going to need types for type validation, maybe makes sense to pass const Locals& right away?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was discussed in the an earlier comment. I started out with std::vector<Locals> but then local counting and validation of it has to be moved into parse_expr, which I think isn't the nicest.

I'd defer this work once we implement type checking.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create Code object in Module before parse_expr?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create Code object in Module before parse_expr?

I would advice against this, because it would make code confusingly both input and output parameter

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not mean that parse_expr should return Code. Maybe it should return Expr. But is also bad to ask parse_expr to move the information about locals (here local_count, maybe type information later) from one place to another. That's unneeded and trivial responsibility.

lib/fizzy/parser.hpp Outdated Show resolved Hide resolved
lib/fizzy/parser_expr.cpp Outdated Show resolved Hide resolved
// This is needed for aiding validation.
assert((uint64_t{local_count} + module.typesec[func_type_idx].inputs.size()) <=
std::numeric_limits<uint32_t>::max());
const uint32_t max_local_index =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it's not max index, maybe total_local_arg_count or local_and_arg_count

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't it max index? The immediate it is compared against is called "local index".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's max index + 1

@axic axic force-pushed the validate-locals branch 2 times, most recently from b79d3dd to 0488136 Compare June 8, 2020 09:05
@gumb0
Copy link
Collaborator

gumb0 commented Jun 8, 2020

Looks ok, needs a test fro validation failure.

@axic axic force-pushed the validate-locals branch 2 times, most recently from 7ae75ca to ddf45be Compare June 8, 2020 17:20
@axic
Copy link
Member Author

axic commented Jun 8, 2020

@gumb0 added tests.

// The function's implicit block.
control_stack.emplace(Instr::block, function_arity, 0);

// This is needed for aiding validation.
assert(
(uint64_t{local_count} + func_type.inputs.size()) <= std::numeric_limits<uint32_t>::max());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this case is also missing from the spectests.

@gumb0 gumb0 force-pushed the validate-locals branch from ddf45be to 59fdbd0 Compare June 22, 2020 12:21
@axic axic force-pushed the validate-locals branch 2 times, most recently from 99a24b8 to 2813c84 Compare June 24, 2020 16:58
@axic axic force-pushed the validate-locals branch from 2813c84 to 88dbbcd Compare June 24, 2020 16:59
@axic axic merged commit e1625a7 into master Jun 24, 2020
@axic axic deleted the validate-locals branch June 24, 2020 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants