Skip to content

Commit ca09d81

Browse files
authored
fix: allow negative gas refund propagation (#333)
1 parent 3791d8f commit ca09d81

4 files changed

Lines changed: 9 additions & 10 deletions

File tree

src/compiler/evm_frontend/evm_imported.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ const uint8_t *evmHandleCreateInternal(zen::runtime::EVMInstance *Instance,
794794
if (GasUsed != 0) {
795795
Instance->chargeGas(GasUsed);
796796
}
797+
// Track subcall refund (may be negative)
797798
Instance->addGasRefund(Result.gas_refund);
798799

799800
std::vector<uint8_t> ReturnData(Result.output_data,
@@ -956,6 +957,7 @@ static uint64_t evmHandleCallInternal(zen::runtime::EVMInstance *Instance,
956957
Instance->chargeGas(GasUsed);
957958
}
958959

960+
// Track subcall refund (may be negative)
959961
Instance->addGasRefund(Result.gas_refund);
960962

961963
// Copy return data to memory if output area is specified.

src/evm/interpreter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct EVMFrame {
3030
evmc_message Msg = {};
3131
evmc::Host *Host = nullptr;
3232
evmc_tx_context MTx = {};
33-
uint64_t GasRefundSnapshot = 0;
33+
int64_t GasRefundSnapshot = 0;
3434

3535
size_t Sp = 0;
3636
uint64_t Pc = 0;

src/evm/opcode_handlers.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,11 +1461,8 @@ void CallHandler::doExecute() {
14611461
return;
14621462
}
14631463

1464-
if (Result.gas_refund > 0) {
1465-
// Track subcall refund at Instance level
1466-
Context->getInstance()->addGasRefund(Result.gas_refund);
1467-
}
1468-
1464+
// Track subcall refund at Instance level (may be negative)
1465+
Context->getInstance()->addGasRefund(Result.gas_refund);
14691466
Context->setStatus(EVMC_SUCCESS);
14701467
}
14711468

src/runtime/evm_instance.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ class EVMInstance final : public RuntimeObject<EVMInstance> {
5959
void chargeGas(uint64_t GasCost);
6060
void addGas(uint64_t GasAmount);
6161

62-
void addGasRefund(uint64_t Amount) { GasRefund += Amount; }
63-
void setGasRefund(uint64_t Amount) { GasRefund = Amount; }
64-
uint64_t getGasRefund() const { return GasRefund; }
62+
void addGasRefund(int64_t Amount) { GasRefund += Amount; }
63+
void setGasRefund(int64_t Amount) { GasRefund = Amount; }
64+
int64_t getGasRefund() const { return GasRefund; }
6565
void restoreGasRefundSnapshot() {
6666
if (!GasRefundStack.empty()) {
6767
GasRefund = GasRefundStack.back();
@@ -288,7 +288,7 @@ class EVMInstance final : public RuntimeObject<EVMInstance> {
288288
newEVMInstance(Isolation &Iso, const EVMModule &Mod, uint64_t GasLimit = 0);
289289

290290
const EVMModule *Mod = nullptr;
291-
uint64_t GasRefund = 0;
291+
int64_t GasRefund = 0;
292292
// memory
293293
uint8_t *MemoryBase = nullptr;
294294
uint64_t MemorySize = 0;

0 commit comments

Comments
 (0)