Skip to content
Merged
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
56 changes: 20 additions & 36 deletions src/compiler/evm_frontend/evm_imported.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,10 +900,10 @@ static uint64_t evmHandleCallInternal(zen::runtime::EVMInstance *Instance,
}
}

uint64_t CallGas = Gas;
if (HasValueArgs) {
uint64_t GasCost = 0;
std::optional<bool> AccountState;
GasCost = HasValue ? zen::evm::CALL_VALUE_COST : 0;
uint64_t GasCost = HasValue ? zen::evm::CALL_VALUE_COST : 0;
if (CallKind == EVMC_CALL) {
if (HasValue || Instance->getRevision() < EVMC_SPURIOUS_DRAGON) {
AccountState = Module->Host->account_exists(TargetAddr);
Expand All @@ -913,54 +913,38 @@ static uint64_t evmHandleCallInternal(zen::runtime::EVMInstance *Instance,
}
}

if (Instance->getGas() < GasCost) {
zen::runtime::EVMInstance::triggerInstanceExceptionOnJIT(
Instance, zen::common::ErrorCode::GasLimitExceeded);
}
Instance->chargeGas(GasCost);
}

uint64_t GasLeft = Instance->getGas();
if (Rev >= EVMC_TANGERINE_WHISTLE) {
const uint64_t GasCap = GasLeft - GasLeft / 64;
CallGas = std::min(CallGas, GasCap);
} else if (CallGas > GasLeft) {
zen::runtime::EVMInstance::triggerInstanceExceptionOnJIT(
Instance, zen::common::ErrorCode::GasLimitExceeded);
}

if (HasValueArgs) {
bool HasEnoughBalance = true;
if (HasValue) {
Instance->addGas(zen::evm::CALL_GAS_STIPEND);
CallGas += zen::evm::CALL_GAS_STIPEND;

const auto CallerBalance =
Module->Host->get_balance(CurrentMsg->recipient);
const intx::uint256 CallerValue =
intx::be::load<intx::uint256>(CallerBalance);
HasEnoughBalance = CallerValue >= intx::uint256(Value);
if (!HasEnoughBalance) {
GasCost -= zen::evm::CALL_GAS_STIPEND;
}

if (CallKind == EVMC_CALL && HasEnoughBalance) {
if (!AccountState.has_value()) {
AccountState = Module->Host->account_exists(TargetAddr);
}
if (!AccountState.value()) {
GasCost -= zen::evm::CALL_GAS_STIPEND;
}
if (!HasEnoughBalance) {
Instance->setReturnData({});
return 0;
}
}

Instance->chargeGas(GasCost);

if (HasValue && !HasEnoughBalance) {
Instance->setReturnData({});
return 0;
}
}

uint8_t *MemoryBase = Instance->getMemoryBase();
uint64_t CallGas = Gas;
uint64_t GasLeft = Instance->getGas();
if (Rev >= EVMC_TANGERINE_WHISTLE) {
const uint64_t GasCap = GasLeft - GasLeft / 64;
if (CallGas > GasCap) {
CallGas = GasCap;
}
} else if (CallGas > GasLeft) {
Instance->chargeGas(GasLeft + 1);
}
if (HasValueArgs && HasValue) {
CallGas += zen::evm::CALL_GAS_STIPEND;
}

if (CurrentMsg->depth >= zen::evm::MAXSTACK) {
Instance->setReturnData({});
Expand Down
16 changes: 16 additions & 0 deletions src/runtime/evm_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,20 @@ void EVMInstance::chargeGas(uint64_t GasCost) {
Msg->gas = static_cast<int64_t>(NewGas);
}

void EVMInstance::addGas(uint64_t GasAmount) {
evmc_message *Msg = getCurrentMessage();
ZEN_ASSERT(Msg && "Active message required for gas accounting");
uint64_t GasLeft = getGas();
if (GasLeft > UINT64_MAX - GasAmount) {
#if defined(ZEN_ENABLE_JIT) && defined(ZEN_ENABLE_CPU_EXCEPTION)
triggerInstanceExceptionOnJIT(this, common::ErrorCode::GasLimitExceeded);
#else
throw common::getError(common::ErrorCode::GasLimitExceeded);
#endif
}
uint64_t NewGas = GasLeft + GasAmount;
setGas(NewGas);
Msg->gas = static_cast<int64_t>(NewGas);
}

} // namespace zen::runtime
1 change: 1 addition & 0 deletions src/runtime/evm_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class EVMInstance final : public RuntimeObject<EVMInstance> {
bool expandMemoryChecked(uint64_t OffsetA, uint64_t SizeA, uint64_t OffsetB,
uint64_t SizeB);
void chargeGas(uint64_t GasCost);
void addGas(uint64_t GasAmount);

void addGasRefund(uint64_t Amount) { GasRefund += Amount; }
void setGasRefund(uint64_t Amount) { GasRefund = Amount; }
Expand Down
3 changes: 3 additions & 0 deletions tests/evmone_unittests/EVMOneMultipassUnitTestsRunList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,6 @@ multi_vm/evm.delegatecall_oog_depth_limit/external_vm
multi_vm/evm.call_failing_with_value/external_vm
multi_vm/evm.call_with_value_depth_limit/external_v
multi_vm/evm.call_new_account_creation_cost/external_vm
multi_vm/evm.call_with_value/external_vm
multi_vm/evm.callcode_new_account_create/external_vm
multi_vm/evm.call_value/external_vm