Skip to content

Commit

Permalink
Make EVM push frame function only callable from system contract
Browse files Browse the repository at this point in the history
  • Loading branch information
IAvecilla committed Aug 23, 2024
1 parent f16dc4d commit e31bfa8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
9 changes: 5 additions & 4 deletions system-contracts/contracts/EvmGasManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ pragma solidity ^0.8.0;
import "./EvmConstants.sol";

import {ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT} from "./Constants.sol";
import {ISystemContract} from "./interfaces/ISystemContract.sol";

// We consider all the contracts (including system ones) as warm.
uint160 constant PRECOMPILES_END = 0xffff;

// Denotes that passGas has been consumed
uint256 constant INF_PASS_GAS = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;

contract EvmGasManager {
contract EvmGasManager is ISystemContract {
// We need strust to use `storage` pointers
struct WarmAccountInfo {
bool isWarm;
Expand Down Expand Up @@ -85,7 +86,7 @@ contract EvmGasManager {
/*
returns true if the account was already warm
*/
function warmAccount(address account) external payable onlySystemEvm returns (bool wasWarm) {
function warmAccount(address account) external payable returns (bool wasWarm) {
if (uint160(account) < PRECOMPILES_END) return true;

wasWarm = tloadWarmAccount(account);
Expand All @@ -96,7 +97,7 @@ contract EvmGasManager {
return tloadWarmSlot(msg.sender, _slot).warm;
}

function warmSlot(uint256 _slot, uint256 _currentValue) external payable onlySystemEvm returns (bool, uint256) {
function warmSlot(uint256 _slot, uint256 _currentValue) external payable returns (bool, uint256) {
SlotInfo memory info = tloadWarmSlot(msg.sender, _slot);

if (info.warm) {
Expand All @@ -123,7 +124,7 @@ contract EvmGasManager {
*/

function pushEVMFrame(uint256 _passGas, bool _isStatic) external {
function pushEVMFrame(uint256 _passGas, bool _isStatic) onlySystemCall external {
EVMStackFrameInfo memory frame = EVMStackFrameInfo({passGas: _passGas, isStatic: _isStatic});

evmStackFrames.push(frame);
Expand Down
21 changes: 20 additions & 1 deletion system-contracts/contracts/EvmInterpreterFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,27 @@ function _pushEVMFrame(_passGas, _isStatic) {
mstore(4, _passGas)
mstore(36, _isStatic)

let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 68, 0, 0)
let farCallAbi := getFarCallABI(
0,
0,
0,
68,
gas(),
// Only rollup is supported for now
0,
0,
0,
1
)

let to := EVM_GAS_MANAGER_CONTRACT()
printString("PRE VERBATIM EVM FRAME")
let success := verbatim_6i_1o("system_call", to, farCallAbi, 0, 0, 0, 0)
printString("POST VERBATIM EVM FRAME")

// let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 68, 0, 0)
if iszero(success) {
printString("FAILED PUSH EVM FRAME")
// This error should never happen
revert(0, 0)
}
Expand Down
42 changes: 40 additions & 2 deletions system-contracts/contracts/EvmInterpreterPreprocessed.yul
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,27 @@ object "EVMInterpreter" {
mstore(4, _passGas)
mstore(36, _isStatic)

let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 68, 0, 0)
let farCallAbi := getFarCallABI(
0,
0,
0,
68,
gas(),
// Only rollup is supported for now
0,
0,
0,
1
)

let to := EVM_GAS_MANAGER_CONTRACT()
printString("PRE VERBATIM EVM FRAME")
let success := verbatim_6i_1o("system_call", to, farCallAbi, 0, 0, 0, 0)
printString("POST VERBATIM EVM FRAME")

// let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 68, 0, 0)
if iszero(success) {
printString("FAILED PUSH EVM FRAME")
// This error should never happen
revert(0, 0)
}
Expand Down Expand Up @@ -3897,8 +3916,27 @@ object "EVMInterpreter" {
mstore(4, _passGas)
mstore(36, _isStatic)

let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 68, 0, 0)
let farCallAbi := getFarCallABI(
0,
0,
0,
68,
gas(),
// Only rollup is supported for now
0,
0,
0,
1
)

let to := EVM_GAS_MANAGER_CONTRACT()
printString("PRE VERBATIM EVM FRAME")
let success := verbatim_6i_1o("system_call", to, farCallAbi, 0, 0, 0, 0)
printString("POST VERBATIM EVM FRAME")

// let success := call(gas(), EVM_GAS_MANAGER_CONTRACT(), 0, 0, 68, 0, 0)
if iszero(success) {
printString("FAILED PUSH EVM FRAME")
// This error should never happen
revert(0, 0)
}
Expand Down

0 comments on commit e31bfa8

Please sign in to comment.