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

precompiles: Improve output buffer handling #951

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions test/state/precompiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,14 @@ evmc::Result call_precompile(evmc_revision rev, const evmc_message& msg) noexcep
if (gas_left < 0)
return evmc::Result{EVMC_OUT_OF_GAS};

// Buffer for the precompile's output.
// Big enough to handle all "expmod" tests, but in case does not match the size requirement
// from analysis, the result will likely be incorrect.
// TODO: Replace with std::pmr::monotonic_buffer_resource?
uint8_t output_buf[4096];
assert(std::size(output_buf) >= max_output_size);

// Allocate buffer for the precompile's output and pass its ownership to evmc::Result.
// TODO: This can be done more elegantly by providing constructor evmc::Result(std::unique_ptr).
const auto output_data = new (std::nothrow) uint8_t[max_output_size];
const auto [status_code, output_size] =
execute(msg.input_data, msg.input_size, output_buf, std::size(output_buf));

evmc::Result result{
status_code, status_code == EVMC_SUCCESS ? gas_left : 0, 0, output_buf, output_size};

return result;
execute(msg.input_data, msg.input_size, output_data, max_output_size);
const evmc_result result{status_code, status_code == EVMC_SUCCESS ? gas_left : 0, 0,
output_data, output_size,
[](const evmc_result* res) noexcept { delete[] res->output_data; }};
return evmc::Result{result};
}
} // namespace evmone::state