Skip to content
Closed
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions src/evm/evm.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ constexpr auto MAX_SIZE_OF_INITCODE = 0xC000;
/// code.
static constexpr auto EMPTY_CODE_HASH =
0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470_bytes32;

/// Store the gas a finished frame hands back to its caller, applying the EVM's
/// terminal gas rule: an exceptional halt consumes all of the frame's gas, and
/// only a success or a revert returns any.
///
/// This must be the last write to `Result.gas_left`. The instance's own gas
/// counter is not authoritative once a frame has halted - the interpreter
/// zeroes the frame's gas rather than the instance's, and the JIT trap path
/// never touches either - so assigning `Instance::getGas()` unconditionally
/// hands the caller gas the EVM has already burned. evmone applies the same
/// rule in `make_execution_result`.
inline void setFrameGasLeft(evmc::Result &Result, uint64_t InstanceGas) {
const bool KeepsGas =
Result.status_code == EVMC_SUCCESS || Result.status_code == EVMC_REVERT;
Result.gas_left = KeepsGas ? static_cast<int64_t>(InstanceGas) : 0;
}

} // namespace zen::evm

#endif // ZEN_EVM_GAS_EVM_H
2 changes: 1 addition & 1 deletion src/runtime/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ void Runtime::callEVMMainOnPhysStack(EVMInstance &Inst, evmc_message &Msg,
} else {
callEVMInInterpMode(Inst, MsgWithCode, Result);
}
Result.gas_left = Inst.getGas();
zen::evm::setFrameGasLeft(Result, Inst.getGas());
}

void Runtime::callEVMMain(EVMInstance &Inst, evmc_message &Msg,
Expand Down
5 changes: 3 additions & 2 deletions src/vm/dt_evmc_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "common/enums.h"
#include "common/errors.h"
#include "compiler/evm_frontend/evm_analyzer.h"
#include "evm/evm.h"
#include "evm/interpreter.h"
#include "evm/opcode_handlers.h"
#include "jit_profile.h"
Expand Down Expand Up @@ -691,7 +692,7 @@ evmc_result runInterpreterOnResolvedInstance(DTVM *VM, EVMModule *Mod,

evmc::Result Result =
std::move(const_cast<evmc::Result &>(Ctx.getExeResult()));
Result.gas_left = TheInst->getGas();
zen::evm::setFrameGasLeft(Result, TheInst->getGas());

return Result.release_raw();
}
Expand Down Expand Up @@ -985,7 +986,7 @@ evmc_result execute(evmc_vm *EVMInstance, const evmc_host_interface *Host,
VM->RT->callEVMInJITMode(*TheInst, MsgWithCode, Result);
#endif // ZEN_ENABLE_VIRTUAL_STACK

Result.gas_left = TheInst->getGas();
zen::evm::setFrameGasLeft(Result, TheInst->getGas());
return Result.release_raw();
}
#else
Expand Down
Loading