-
Notifications
You must be signed in to change notification settings - Fork 598
fix: AVM witgen track gas for nested calls and external halts #10731
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8de5b36
fix: AVM witgen track gas for nested calls and external halts
543e0ce
cleanup
ede026e
debugs
af7c38f
comment
ab8058f
fix
8bce306
fix
e0e66e6
fmt
34d0c79
cleanup
46fcc0f
fmt
81c9d97
Merge branch 'master' into db/gas-halts
dbanks12 c32d97c
fix
c11a5f5
merge
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -428,6 +428,8 @@ AvmError Execution::execute_enqueued_call(AvmTraceBuilder& trace_builder, | |
| // These hints help us to set up first call ctx | ||
| uint32_t clk = trace_builder.get_clk(); | ||
| auto context_id = static_cast<uint8_t>(clk); | ||
| uint32_t l2_gas_allocated_to_enqueued_call = trace_builder.get_l2_gas_left(); | ||
| uint32_t da_gas_allocated_to_enqueued_call = trace_builder.get_da_gas_left(); | ||
| trace_builder.current_ext_call_ctx = AvmTraceBuilder::ExtCallCtx{ | ||
| .context_id = context_id, | ||
| .parent_id = 0, | ||
|
|
@@ -436,10 +438,13 @@ AvmError Execution::execute_enqueued_call(AvmTraceBuilder& trace_builder, | |
| .nested_returndata = {}, | ||
| .last_pc = 0, | ||
| .success_offset = 0, | ||
| .l2_gas = 0, | ||
| .da_gas = 0, | ||
| .start_l2_gas_left = l2_gas_allocated_to_enqueued_call, | ||
| .start_da_gas_left = da_gas_allocated_to_enqueued_call, | ||
| .l2_gas_left = l2_gas_allocated_to_enqueued_call, | ||
| .da_gas_left = da_gas_allocated_to_enqueued_call, | ||
| .internal_return_ptr_stack = {}, | ||
| }; | ||
| trace_builder.allocate_gas_for_call(l2_gas_allocated_to_enqueued_call, da_gas_allocated_to_enqueued_call); | ||
| // Find the bytecode based on contract address of the public call request | ||
| std::vector<uint8_t> bytecode = | ||
| trace_builder.get_bytecode(trace_builder.current_ext_call_ctx.contract_address, check_bytecode_membership); | ||
|
|
@@ -451,11 +456,13 @@ AvmError Execution::execute_enqueued_call(AvmTraceBuilder& trace_builder, | |
| std::stack<uint32_t> debug_counter_stack; | ||
| uint32_t counter = 0; | ||
| trace_builder.set_call_ptr(context_id); | ||
| while (is_ok(error) && (pc = trace_builder.get_pc()) < bytecode.size()) { | ||
| while ((pc = trace_builder.get_pc()) < bytecode.size()) { | ||
|
Comment on lines
-454
to
+459
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we break on error now |
||
| auto [inst, parse_error] = Deserialization::parse(bytecode, pc); | ||
| error = parse_error; | ||
|
|
||
| // FIXME: properly handle case when an instruction fails parsing | ||
| // especially first instruction in bytecode | ||
| if (!is_ok(error)) { | ||
| error = parse_error; | ||
| break; | ||
| } | ||
|
|
||
|
|
@@ -848,9 +855,10 @@ AvmError Execution::execute_enqueued_call(AvmTraceBuilder& trace_builder, | |
| std::get<uint16_t>(inst.operands.at(3)), | ||
| std::get<uint16_t>(inst.operands.at(4)), | ||
| std::get<uint16_t>(inst.operands.at(5))); | ||
| // TODO: what if an error is encountered on return or call which have already modified stack? | ||
| // We hack it in here the logic to change contract address that we are processing | ||
| bytecode = trace_builder.get_bytecode(trace_builder.current_ext_call_ctx.contract_address, | ||
| check_bytecode_membership); | ||
| /*check_membership=*/false); | ||
|
Comment on lines
-853
to
+861
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't need to check membership when we're returning to parent's byecode |
||
| debug_counter_stack.push(counter); | ||
| counter = 0; | ||
| break; | ||
|
|
@@ -864,7 +872,7 @@ AvmError Execution::execute_enqueued_call(AvmTraceBuilder& trace_builder, | |
| std::get<uint16_t>(inst.operands.at(5))); | ||
| // We hack it in here the logic to change contract address that we are processing | ||
| bytecode = trace_builder.get_bytecode(trace_builder.current_ext_call_ctx.contract_address, | ||
| check_bytecode_membership); | ||
| /*check_membership=*/false); | ||
| debug_counter_stack.push(counter); | ||
| counter = 0; | ||
| break; | ||
|
|
@@ -873,53 +881,58 @@ AvmError Execution::execute_enqueued_call(AvmTraceBuilder& trace_builder, | |
| auto ret = trace_builder.op_return(std::get<uint8_t>(inst.operands.at(0)), | ||
| std::get<uint16_t>(inst.operands.at(1)), | ||
| std::get<uint16_t>(inst.operands.at(2))); | ||
| // We hack it in here the logic to change contract address that we are processing | ||
| // did the return opcode hit an exceptional halt? | ||
| error = ret.error; | ||
| if (ret.is_top_level) { | ||
| error = ret.error; | ||
| returndata.insert(returndata.end(), ret.return_data.begin(), ret.return_data.end()); | ||
|
|
||
| } else { | ||
| } else if (is_ok(error)) { | ||
| // switch back to caller's bytecode | ||
| bytecode = trace_builder.get_bytecode(trace_builder.current_ext_call_ctx.contract_address, | ||
| check_bytecode_membership); | ||
| /*check_membership=*/false); | ||
| counter = debug_counter_stack.top(); | ||
| debug_counter_stack.pop(); | ||
| } | ||
| // on error/exceptional-halt, jumping back to parent code is handled at bottom of execution loop | ||
| break; | ||
| } | ||
| case OpCode::REVERT_8: { | ||
| info("HIT REVERT_8 ", "[PC=" + std::to_string(pc) + "] " + inst.to_string()); | ||
| auto ret = trace_builder.op_revert(std::get<uint8_t>(inst.operands.at(0)), | ||
| std::get<uint8_t>(inst.operands.at(1)), | ||
| std::get<uint8_t>(inst.operands.at(2))); | ||
| // error is only set here if the revert opcode hit an exceptional halt | ||
| // revert itself does not trigger "error" | ||
| error = ret.error; | ||
| if (ret.is_top_level) { | ||
| error = ret.error; | ||
| returndata.insert(returndata.end(), ret.return_data.begin(), ret.return_data.end()); | ||
| } else { | ||
| // change to the current ext call ctx | ||
| } else if (is_ok(error)) { | ||
| // switch back to caller's bytecode | ||
| bytecode = trace_builder.get_bytecode(trace_builder.current_ext_call_ctx.contract_address, | ||
| check_bytecode_membership); | ||
| /*check_membership=*/false); | ||
| counter = debug_counter_stack.top(); | ||
| debug_counter_stack.pop(); | ||
| } | ||
|
|
||
| // on error/exceptional-halt, jumping back to parent code is handled at bottom of execution loop | ||
| break; | ||
| } | ||
| case OpCode::REVERT_16: { | ||
| info("HIT REVERT_16 ", "[PC=" + std::to_string(pc) + "] " + inst.to_string()); | ||
| auto ret = trace_builder.op_revert(std::get<uint8_t>(inst.operands.at(0)), | ||
| std::get<uint16_t>(inst.operands.at(1)), | ||
| std::get<uint16_t>(inst.operands.at(2))); | ||
| // error is only set here if the revert opcode hit an exceptional halt | ||
| // revert itself does not trigger "error" | ||
| error = ret.error; | ||
| if (ret.is_top_level) { | ||
| error = ret.error; | ||
| returndata.insert(returndata.end(), ret.return_data.begin(), ret.return_data.end()); | ||
| } else { | ||
| // change to the current ext call ctx | ||
| } else if (is_ok(error)) { | ||
| // switch back to caller's bytecode | ||
| bytecode = trace_builder.get_bytecode(trace_builder.current_ext_call_ctx.contract_address, | ||
| check_bytecode_membership); | ||
| /*check_membership=*/false); | ||
| counter = debug_counter_stack.top(); | ||
| debug_counter_stack.pop(); | ||
| } | ||
|
|
||
| // on error/exceptional-halt, jumping back to parent code is handled at bottom of execution loop | ||
| break; | ||
| } | ||
|
|
||
|
|
@@ -987,18 +1000,36 @@ AvmError Execution::execute_enqueued_call(AvmTraceBuilder& trace_builder, | |
| "."); | ||
| break; | ||
| } | ||
| } | ||
| if (!is_ok(error)) { | ||
| auto const error_ic = counter - 1; // Need adjustement as counter increment occurs in loop body | ||
| std::string reason_prefix = exceptionally_halted(error) ? "exceptional halt" : "REVERT opcode"; | ||
| info("AVM enqueued call halted due to ", | ||
| reason_prefix, | ||
| ". Error: ", | ||
| to_name(error), | ||
| " at PC: ", | ||
| pc, | ||
| " IC: ", | ||
| error_ic); | ||
|
|
||
| if (!is_ok(error)) { | ||
| const bool is_top_level = trace_builder.current_ext_call_ctx.context_id == 0; | ||
|
|
||
| auto const error_ic = counter - 1; // Need adjustement as counter increment occurs in loop body | ||
| std::string call_type = is_top_level ? "enqueued" : "nested"; | ||
| info("AVM ", | ||
| call_type, | ||
| " call exceptionally halted. Error: ", | ||
| to_name(error), | ||
| " at PC: ", | ||
| pc, | ||
| " IC: ", | ||
| error_ic); | ||
|
|
||
| trace_builder.handle_exceptional_halt(); | ||
|
|
||
| if (is_top_level) { | ||
| break; | ||
| } | ||
| // otherwise, handle exceptional halt and proceed with execution in caller/parent | ||
| // We hack it in here the logic to change contract address that we are processing | ||
| bytecode = trace_builder.get_bytecode(trace_builder.current_ext_call_ctx.contract_address, | ||
| /*check_membership=*/false); | ||
| counter = debug_counter_stack.top(); | ||
| debug_counter_stack.pop(); | ||
|
|
||
| // reset error as we've now returned to caller | ||
| error = AvmError::NO_ERROR; | ||
| } | ||
| } | ||
| return error; | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 concept of "fake rows" is now reused for rows that halt (for any reason). Rows that halt have a jump in gas as they return to the parent's gas with the nested call's used gas consumed.