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
48 changes: 26 additions & 22 deletions src/compiler/evm_frontend/evm_imported.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,13 +878,39 @@ static uint64_t evmHandleCallInternal(zen::runtime::EVMInstance *Instance,
return 0;
}

// Calculate required memory sizes for input and output
// Only expand memory if we actually need to access it
bool needArgsMemory = ArgsSize > 0;
bool needRetMemory = RetSize > 0;
if (needArgsMemory && needRetMemory) {
if (!Instance->expandMemoryChecked(ArgsOffset, ArgsSize, RetOffset,
RetSize)) {
Instance->setReturnData({});
return 0;
}
} else if (needArgsMemory) {
if (!Instance->expandMemoryChecked(ArgsOffset, ArgsSize)) {
Instance->setReturnData({});
return 0;
}
} else if (needRetMemory) {
if (!Instance->expandMemoryChecked(RetOffset, RetSize)) {
Instance->setReturnData({});
return 0;
}
}

bool HasEnoughBalance = true;
if (TransfersValue) {
const auto CallerBalance = Module->Host->get_balance(CurrentMsg->recipient);
const intx::uint256 CallerValue =
intx::be::load<intx::uint256>(CallerBalance);
HasEnoughBalance = CallerValue >= intx::uint256(Value);
uint64_t ValueCost = zen::evm::CALL_VALUE_COST;
if (Instance->getGas() < ValueCost) {
zen::runtime::EVMInstance::triggerInstanceExceptionOnJIT(
Instance, zen::common::ErrorCode::GasLimitExceeded);
}
if (!HasEnoughBalance) {
ValueCost -= zen::evm::CALL_GAS_STIPEND;
}
Expand All @@ -905,28 +931,6 @@ static uint64_t evmHandleCallInternal(zen::runtime::EVMInstance *Instance,
return 0;
}

// Calculate required memory sizes for input and output
// Only expand memory if we actually need to access it
bool needArgsMemory = ArgsSize > 0;
bool needRetMemory = RetSize > 0;
if (needArgsMemory && needRetMemory) {
if (!Instance->expandMemoryChecked(ArgsOffset, ArgsSize, RetOffset,
RetSize)) {
Instance->setReturnData({});
return 0;
}
} else if (needArgsMemory) {
if (!Instance->expandMemoryChecked(ArgsOffset, ArgsSize)) {
Instance->setReturnData({});
return 0;
}
} else if (needRetMemory) {
if (!Instance->expandMemoryChecked(RetOffset, RetSize)) {
Instance->setReturnData({});
return 0;
}
}

uint8_t *MemoryBase = Instance->getMemoryBase();
uint64_t CallGas = Gas;
uint64_t GasLeft = Instance->getGas();
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/evm_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ bool EVMInstance::expandMemoryChecked(uint64_t OffsetA, uint64_t SizeA,
expandMemory(RequiredSize);
return true;
}

void EVMInstance::chargeGas(uint64_t GasCost) {
evmc_message *Msg = getCurrentMessage();
ZEN_ASSERT(Msg && "Active message required for gas accounting");

uint64_t GasLeft = getGas();
if (GasLeft < GasCost) {
#if defined(ZEN_ENABLE_JIT) && defined(ZEN_ENABLE_CPU_EXCEPTION)
Expand Down
2 changes: 2 additions & 0 deletions tests/evmone_unittests/EVMOneMultipassUnitTestsRunList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,5 @@ multi_vm/evm.undefined_instruction_analysis_overflow/external_vm
multi_vm/evm.undefined_instruction_block_cost_negative/external_vm
multi_vm/evm.call_oog_after_depth_check/external_vm
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