-
Notifications
You must be signed in to change notification settings - Fork 592
feat(avm): add calldata & returndata to context #13008
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <span> | ||
| #include <vector> | ||
|
|
||
| #include "barretenberg/vm2/common/aztec_types.hpp" | ||
| #include "barretenberg/vm2/common/field.hpp" | ||
| #include "barretenberg/vm2/common/memory_types.hpp" | ||
| #include "barretenberg/vm2/simulation/bytecode_manager.hpp" | ||
| #include "barretenberg/vm2/simulation/events/context_events.hpp" | ||
| #include "barretenberg/vm2/simulation/events/event_emitter.hpp" | ||
|
|
@@ -26,7 +28,6 @@ class ContextInterface { | |
| virtual void set_pc(uint32_t new_pc) = 0; | ||
| virtual uint32_t get_next_pc() const = 0; | ||
| virtual void set_next_pc(uint32_t new_next_pc) = 0; | ||
| virtual void set_nested_returndata(std::vector<FF> return_data) = 0; | ||
| virtual bool halted() const = 0; | ||
| virtual void halt() = 0; | ||
|
|
||
|
|
@@ -35,11 +36,24 @@ class ContextInterface { | |
| // Environment. | ||
| virtual const AztecAddress& get_address() const = 0; | ||
| virtual const AztecAddress& get_msg_sender() const = 0; | ||
| virtual std::span<const FF> get_calldata() const = 0; | ||
| virtual bool get_is_static() const = 0; | ||
|
|
||
| // Input / Output | ||
| virtual std::vector<FF> get_calldata(uint32_t cd_offset, uint32_t cd_size) const = 0; | ||
| virtual std::vector<FF> get_returndata(uint32_t rd_offset, uint32_t rd_size) = 0; | ||
| virtual ContextInterface& get_child_context() = 0; | ||
| virtual void set_child_context(std::unique_ptr<ContextInterface> child_ctx) = 0; | ||
|
|
||
| virtual MemoryAddress get_last_rd_offset() const = 0; | ||
| virtual void set_last_rd_offset(MemoryAddress rd_offset) = 0; | ||
|
|
||
| virtual MemoryAddress get_last_rd_size() const = 0; | ||
| virtual void set_last_rd_size(MemoryAddress rd_size) = 0; | ||
|
|
||
| virtual bool get_last_success() const = 0; | ||
| virtual void set_last_success(bool success) = 0; | ||
|
|
||
| // Events | ||
| virtual void emit_context_snapshot() = 0; | ||
| virtual ContextEvent get_current_context() = 0; | ||
IlyasRidhuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
|
|
@@ -49,19 +63,15 @@ class BaseContext : public ContextInterface { | |
| BaseContext(uint32_t context_id, | ||
| AztecAddress address, | ||
| AztecAddress msg_sender, | ||
| std::span<const FF> calldata, | ||
| bool is_static, | ||
| std::unique_ptr<BytecodeManagerInterface> bytecode, | ||
| std::unique_ptr<MemoryInterface> memory, | ||
| EventEmitterInterface<ContextStackEvent>& ctx_stack_events) | ||
|
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. Migrated to |
||
| std::unique_ptr<MemoryInterface> memory) | ||
| : address(address) | ||
| , msg_sender(msg_sender) | ||
| , calldata(calldata.begin(), calldata.end()) | ||
| , is_static(is_static) | ||
| , context_id(context_id) | ||
| , bytecode(std::move(bytecode)) | ||
| , memory(std::move(memory)) | ||
| , ctx_stack_events(ctx_stack_events) | ||
| {} | ||
|
|
||
| // Having getters and setters make it easier to mock the context. | ||
|
|
@@ -72,7 +82,6 @@ class BaseContext : public ContextInterface { | |
| void set_pc(uint32_t new_pc) override { pc = new_pc; } | ||
| uint32_t get_next_pc() const override { return next_pc; } | ||
| void set_next_pc(uint32_t new_next_pc) override { next_pc = new_next_pc; } | ||
| void set_nested_returndata(std::vector<FF> return_data) override { nested_returndata = std::move(return_data); } | ||
| bool halted() const override { return has_halted; } | ||
| void halt() override { has_halted = true; } | ||
|
|
||
|
|
@@ -81,31 +90,40 @@ class BaseContext : public ContextInterface { | |
| // Environment. | ||
| const AztecAddress& get_address() const override { return address; } | ||
| const AztecAddress& get_msg_sender() const override { return msg_sender; } | ||
| std::span<const FF> get_calldata() const override { return calldata; } | ||
| bool get_is_static() const override { return is_static; } | ||
|
|
||
| // Event Emitting | ||
| void emit_context_snapshot() override | ||
| ContextInterface& get_child_context() override { return *child_context; } | ||
| void set_child_context(std::unique_ptr<ContextInterface> child_ctx) override | ||
| { | ||
| ctx_stack_events.emit({ .id = context_id, | ||
| .next_pc = next_pc, | ||
| .msg_sender = msg_sender, | ||
| .contract_addr = address, | ||
| .is_static = is_static }); | ||
| }; | ||
| child_context = std::move(child_ctx); | ||
| } | ||
|
|
||
| ContextEvent get_current_context() override | ||
| MemoryAddress get_last_rd_offset() const override { return last_child_rd_offset; } | ||
| void set_last_rd_offset(MemoryAddress rd_offset) override { last_child_rd_offset = rd_offset; } | ||
|
|
||
| MemoryAddress get_last_rd_size() const override { return last_child_rd_size; } | ||
| void set_last_rd_size(MemoryAddress rd_size) override { last_child_rd_size = rd_size; } | ||
|
|
||
| bool get_last_success() const override { return last_child_success; } | ||
| void set_last_success(bool success) override { last_child_success = success; } | ||
|
|
||
| // Input / Output | ||
| std::vector<FF> get_returndata(uint32_t rd_offset, uint32_t rd_size) override | ||
| { | ||
| return { | ||
| .id = context_id, .pc = pc, .msg_sender = msg_sender, .contract_addr = address, .is_static = is_static | ||
| }; | ||
| MemoryInterface& child_memory = get_child_context().get_memory(); | ||
| auto get_returndata_size = child_memory.get(last_child_rd_size); | ||
| uint32_t returndata_size = static_cast<uint32_t>(get_returndata_size.value); | ||
| [[maybe_unused]] uint32_t write_size = std::min(rd_offset + rd_size, returndata_size); | ||
| std::vector<FF> | ||
IlyasRidhuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| retrieved_returndata = {}; // child_memory.set_slice(get_last_rd_offset() + rd_offset, write_size) | ||
| retrieved_returndata.resize(rd_size); | ||
| return retrieved_returndata; | ||
| }; | ||
|
|
||
| private: | ||
| // Environment. | ||
| AztecAddress address; | ||
| AztecAddress msg_sender; | ||
| std::vector<FF> calldata; | ||
| bool is_static; | ||
|
|
||
| uint32_t context_id; | ||
|
|
@@ -114,12 +132,14 @@ class BaseContext : public ContextInterface { | |
| uint32_t pc = 0; | ||
| uint32_t next_pc = 0; | ||
| bool has_halted = false; | ||
| std::vector<FF> nested_returndata; | ||
| std::unique_ptr<BytecodeManagerInterface> bytecode; | ||
| std::unique_ptr<MemoryInterface> memory; | ||
|
|
||
| // Emiiters | ||
| EventEmitterInterface<ContextStackEvent>& ctx_stack_events; | ||
| // Output | ||
| std::unique_ptr<ContextInterface> child_context = nullptr; | ||
| MemoryAddress last_child_rd_offset = 0; | ||
| MemoryAddress last_child_rd_size = 0; | ||
| bool last_child_success = false; | ||
| }; | ||
|
|
||
| // TODO(ilyas): flesh these out in the cpp file, these are just temporary | ||
|
|
@@ -128,20 +148,47 @@ class EnqueuedCallContext : public BaseContext { | |
| EnqueuedCallContext(uint32_t context_id, | ||
| AztecAddress address, | ||
| AztecAddress msg_sender, | ||
| std::span<const FF> calldata, | ||
| bool is_static, | ||
| std::unique_ptr<BytecodeManagerInterface> bytecode, | ||
| std::unique_ptr<MemoryInterface> memory, | ||
| EventEmitterInterface<ContextStackEvent>& ctx_stack_events) | ||
| : BaseContext(context_id, | ||
| address, | ||
| msg_sender, | ||
| calldata, | ||
| is_static, | ||
| std::move(bytecode), | ||
| std::move(memory), | ||
| ctx_stack_events) | ||
| std::span<const FF> calldata) | ||
| : BaseContext(context_id, address, msg_sender, is_static, std::move(bytecode), std::move(memory)) | ||
| , calldata(calldata.begin(), calldata.end()) | ||
| {} | ||
|
|
||
| // Event Emitting | ||
| ContextEvent get_current_context() override | ||
| { | ||
| return { .id = get_context_id(), | ||
| .pc = get_pc(), | ||
| .msg_sender = get_msg_sender(), | ||
| .contract_addr = get_address(), | ||
| .is_static = get_is_static(), | ||
| .parent_cd_addr = 0, | ||
| .parent_cd_size_addr = 0, | ||
| .last_child_rd_addr = get_last_rd_offset(), | ||
| .last_child_rd_size_addr = get_last_rd_size(), | ||
| .last_child_success = get_last_success() }; | ||
| }; | ||
|
|
||
| // Input / Output | ||
| std::vector<FF> get_calldata(uint32_t cd_offset, uint32_t cd_size) const override | ||
| { | ||
| // TODO(ilyas): Do we assert to assert cd_size < calldata.size(), otherwise it could trigger a massive write of | ||
| // zeroes. OTOH: this should be caught by an OUT_OF_GAS exception | ||
| std::vector<FF> padded_calldata(cd_size, 0); // Vector of size cd_size filled with zeroes; | ||
|
|
||
| // We first take a slice of the data, the most we can slice is the actual size of the data | ||
| size_t slice_size = std::min(static_cast<size_t>(cd_offset + cd_size), calldata.size()); | ||
|
|
||
| for (size_t i = cd_offset; i < slice_size; i++) { | ||
| padded_calldata[i] = calldata[i]; | ||
| } | ||
| return padded_calldata; | ||
| }; | ||
|
|
||
| private: | ||
| std::vector<FF> calldata; | ||
| }; | ||
|
|
||
| // Parameters for a nested call need to be changed | ||
|
|
@@ -150,20 +197,52 @@ class NestedContext : public BaseContext { | |
| NestedContext(uint32_t context_id, | ||
| AztecAddress address, | ||
| AztecAddress msg_sender, | ||
| std::span<const FF> calldata, | ||
| bool is_static, | ||
| std::unique_ptr<BytecodeManagerInterface> bytecode, | ||
| std::unique_ptr<MemoryInterface> memory, | ||
| EventEmitterInterface<ContextStackEvent>& ctx_stack_events) | ||
| : BaseContext(context_id, | ||
| address, | ||
| msg_sender, | ||
| calldata, | ||
| is_static, | ||
| std::move(bytecode), | ||
| std::move(memory), | ||
| ctx_stack_events) | ||
| ContextInterface& parent_context, | ||
| MemoryAddress cd_offset_address, /* This is a direct mem address */ | ||
| MemoryAddress cd_size_address /* This is a direct mem address */ | ||
| ) | ||
| : BaseContext(context_id, address, msg_sender, is_static, std::move(bytecode), std::move(memory)) | ||
| , parent_cd_offset(cd_offset_address) | ||
| , parent_cd_size(cd_size_address) | ||
| , parent_context(parent_context) | ||
| {} | ||
|
|
||
| // Event Emitting | ||
| ContextEvent get_current_context() override | ||
| { | ||
| return { .id = get_context_id(), | ||
| .pc = get_pc(), | ||
| .msg_sender = get_msg_sender(), | ||
| .contract_addr = get_address(), | ||
| .is_static = get_is_static(), | ||
| .parent_cd_addr = parent_cd_offset, | ||
| .parent_cd_size_addr = parent_cd_size }; | ||
IlyasRidhuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| // Input / Output | ||
| std::vector<FF> get_calldata(uint32_t cd_offset, uint32_t cd_size) const override | ||
| { | ||
| ValueRefAndTag get_calldata_size = parent_context.get_memory().get(parent_cd_size); | ||
| // TODO(ilyas): error if tag != U32 | ||
| auto calldata_size = static_cast<uint32_t>(get_calldata_size.value); | ||
| uint32_t read_size = std::min(cd_offset + cd_size, calldata_size); | ||
|
|
||
| auto retrieved_calldata = parent_context.get_memory().get_slice(parent_cd_offset + cd_offset, read_size).first; | ||
|
|
||
| // Pad the calldata | ||
| retrieved_calldata.resize(cd_size, 0); | ||
| return retrieved_calldata; | ||
| }; | ||
|
|
||
| private: | ||
| // These are direct addresses to look up into the parent context during calldata copying | ||
| MemoryAddress parent_cd_offset; | ||
| MemoryAddress parent_cd_size; | ||
|
|
||
| ContextInterface& parent_context; | ||
| }; | ||
|
|
||
| } // namespace bb::avm2::simulation | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.