Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
fix: overflow in message_call_gas (#996)
Browse files Browse the repository at this point in the history
* fix: overflow in message_call_gas

* fmt
  • Loading branch information
enitrat authored Sep 30, 2024
1 parent 109ab1e commit 9baf1a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 13 additions & 7 deletions crates/evm/src/gas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,33 @@ pub fn max_message_call_gas(gas: u64) -> u64 {
/// * `memory_cost`: The amount needed to extend the memory in the current frame.
/// * `extra_gas`: The amount of gas needed for transferring value + creating a new account inside a
/// message call.
/// * `call_stipend`: The amount of stipend provided to a message call to execute code while
/// transferring value(native token).
///
/// # Returns
///
/// * `message_call_gas`: `MessageCallGas`
/// * `Result<MessageCallGas, EVMError>`: The calculated MessageCallGas or an error if overflow
/// occurs.
pub fn calculate_message_call_gas(
value: u256, gas: u64, gas_left: u64, memory_cost: u64, extra_gas: u64
) -> MessageCallGas {
) -> Result<MessageCallGas, EVMError> {
let call_stipend = if value == 0 {
0
} else {
CALL_STIPEND
};
let gas = if gas_left < extra_gas + memory_cost {

// Check for overflow when adding extra_gas and memory_cost
let total_extra_cost = extra_gas.checked_add(memory_cost).ok_or(EVMError::OutOfGas)?;
let gas = if gas_left < total_extra_cost {
gas
} else {
min(gas, max_message_call_gas(gas_left - memory_cost - extra_gas))
let remaining_gas = gas_left - total_extra_cost; // Safe because of the check above
min(gas, max_message_call_gas(remaining_gas))
};

return MessageCallGas { cost: gas + extra_gas, stipend: gas + call_stipend };
let cost = gas.checked_add(extra_gas).ok_or(EVMError::OutOfGas)?;
let stipend = gas.checked_add(call_stipend).ok_or(EVMError::OutOfGas)?;

Result::Ok(MessageCallGas { cost, stipend })
}


Expand Down
8 changes: 4 additions & 4 deletions crates/evm/src/instructions/system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub impl SystemOperations of SystemOperationsTrait {
self.gas_left(),
memory_expansion.expansion_cost,
access_gas_cost + transfer_gas_cost + create_gas_cost
);
)?;
self.charge_gas(message_call_gas.cost + memory_expansion.expansion_cost)?;
// Only the transfer gas is left to charge.

Expand Down Expand Up @@ -140,7 +140,7 @@ pub impl SystemOperations of SystemOperationsTrait {
self.gas_left(),
memory_expansion.expansion_cost,
access_gas_cost + transfer_gas_cost
);
)?;
self.charge_gas(message_call_gas.cost + memory_expansion.expansion_cost)?;

// If sender_balance < value, return early, pushing
Expand Down Expand Up @@ -212,7 +212,7 @@ pub impl SystemOperations of SystemOperationsTrait {

let message_call_gas = gas::calculate_message_call_gas(
0, gas, self.gas_left(), memory_expansion.expansion_cost, access_gas_cost
);
)?;
self.charge_gas(message_call_gas.cost + memory_expansion.expansion_cost)?;

self
Expand Down Expand Up @@ -265,7 +265,7 @@ pub impl SystemOperations of SystemOperationsTrait {

let message_call_gas = gas::calculate_message_call_gas(
0, gas, self.gas_left(), memory_expansion.expansion_cost, access_gas_cost
);
)?;
let gas_to_charge = message_call_gas
.cost
.checked_add(memory_expansion.expansion_cost)
Expand Down

0 comments on commit 9baf1a9

Please sign in to comment.