Verified contractExactMIT
OpcodeScanner
Address0x19e8ea3043e10d854caeec7b2c2360c3153a356d
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.37
- Same code
Only this contract
Details
- Compiler
- solc 0.8.37+commit.f401782d
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- osaka
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,984,262
- Creation transaction
- 0x28fb13d462…b78e48caa0
4 files
- src/runtime/OpcodeScanner.solmain5.87 KB
- src/common/Stewarded.sol1.78 KB
- src/common/Versioned.sol565 B
- src/interfaces/IOpcodeScanner.sol751 B
src/runtime/OpcodeScanner.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.37;
// ▄▄▄▄▄ ▄ ▄ ▄ ▄
// █▄▄▄ ▀▄ ▄▀ █▄▄▄█ EVH · Ethereum Virtual Hook
// █▄▄▄▄ ▀ █ █ plugin hypervisor for Uniswap V4
//
// ethereumvirtualhook.xyz · x.com/VirtualHookETH
import {Stewarded} from "../common/Stewarded.sol";
import {IOpcodeScanner} from "../interfaces/IOpcodeScanner.sol";
/// @title OpcodeScanner
/// @notice Static admission gate. A plugin's runtime bytecode is disassembled
/// on-chain and swept for opcodes that would let it escape its scope.
///
/// Banned by default and why:
/// DELEGATECALL 0xF4 · CALLCODE 0xF2 — foreign code in the plugin's
/// own storage context, defeating namespacing
/// CREATE 0xF0 · CREATE2 0xF5 — deploy unscanned code, then call it
/// SELFDESTRUCT 0xFF — vanish from a lineup mid-dispatch
///
/// CALL and STATICCALL are allowed: plugins legitimately read oracles.
/// Who a plugin may affect is bounded by permits, bonds and strikes, not
/// by pretending a static sweep can decide it.
///
/// @dev The sweep skips PUSH immediates, so a 0xF4 inside `PUSH32` data is
/// not a false positive, and it stops before solc's CBOR metadata
/// trailer, which is unreachable data. Necessary, not sufficient.
contract OpcodeScanner is Stewarded, IOpcodeScanner {
uint256 public constant MAX_CODE_SIZE = 24_576;
uint8 internal constant OP_PUSH1 = 0x60;
uint8 internal constant OP_PUSH32 = 0x7F;
mapping(uint8 => bool) public override isBanned;
uint8[] internal _banned;
/// @notice Council escape hatch for code a linear sweep misreads (data
/// segments after the executable region). Keyed by codehash, so a
/// whitelisting can never be transplanted onto different code.
mapping(bytes32 => bool) public isWhitelistedCodehash;
event OpcodeBanned(uint8 indexed opcode, bool banned);
event CodehashWhitelisted(bytes32 indexed codehash, bool whitelisted, string rationale);
constructor(address steward_) Stewarded(steward_) {
_setBanned(0xF0, true);
_setBanned(0xF2, true);
_setBanned(0xF4, true);
_setBanned(0xF5, true);
_setBanned(0xFF, true);
}
// ------------------------------------------------------------------ admin
function setBanned(uint8 opcode, bool banned) external onlySteward {
_setBanned(opcode, banned);
}
function whitelistCodehash(bytes32 codehash, bool whitelisted, string calldata rationale) external onlySteward {
isWhitelistedCodehash[codehash] = whitelisted;
emit CodehashWhitelisted(codehash, whitelisted, rationale);
}
function _setBanned(uint8 opcode, bool banned) internal {
if (isBanned[opcode] == banned) return;
isBanned[opcode] = banned;
if (banned) {
_banned.push(opcode);
} else {
uint256 n = _banned.length;
for (uint256 i; i < n; ++i) {
if (_banned[i] == opcode) {
_banned[i] = _banned[n - 1];
_banned.pop();
break;
}
}
}
emit OpcodeBanned(opcode, banned);
}
// ------------------------------------------------------------------ sweep
function bannedOpcodes() external view override returns (uint8[] memory) {
return _banned;
}
/// @notice Length of `code` with solc's `<cbor><uint16 length>` trailer cut.
function executableLength(bytes memory code) public pure returns (uint256) {
uint256 size = code.length;
if (size < 4) return size;
uint256 trailer = (uint256(uint8(code[size - 2])) << 8) | uint256(uint8(code[size - 1]));
if (trailer == 0 || trailer + 2 >= size) return size;
return size - (trailer + 2);
}
function inspect(address target) public view override returns (bool clean, uint8 opcode, uint256 offset) {
bytes memory code = target.code;
if (code.length == 0 || code.length > MAX_CODE_SIZE) return (false, 0, 0);
if (isWhitelistedCodehash[target.codehash]) return (true, 0, 0);
return _sweep(code);
}
function inspectBytecode(bytes calldata code) external view returns (bool clean, uint8 opcode, uint256 offset) {
if (code.length == 0) return (false, 0, 0);
return _sweep(code);
}
function requireClean(address target) external view override {
uint256 size = target.code.length;
if (size == 0) revert NoBytecode();
if (size > MAX_CODE_SIZE) revert BytecodeTooLarge(size);
(bool clean, uint8 opcode, uint256 offset) = inspect(target);
if (!clean) revert BannedOpcode(opcode, offset);
}
/// @notice Executable instruction count (PUSH data excluded), a rough
/// complexity signal for the plugin page.
function instructionCount(address target) external view returns (uint256 n) {
bytes memory code = target.code;
uint256 size = executableLength(code);
uint256 i;
while (i < size) {
uint8 op = uint8(code[i]);
unchecked {
++n;
i += (op >= OP_PUSH1 && op <= OP_PUSH32) ? uint256(op - OP_PUSH1) + 2 : 1;
}
}
}
function _sweep(bytes memory code) internal view returns (bool, uint8, uint256) {
uint256 size = executableLength(code);
uint256 i;
while (i < size) {
uint8 op = uint8(code[i]);
if (isBanned[op]) return (false, op, i);
unchecked {
i += (op >= OP_PUSH1 && op <= OP_PUSH32) ? uint256(op - OP_PUSH1) + 2 : 1;
}
}
return (true, 0, 0);
}
}
src/common/Stewarded.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.37;
// ▄▄▄▄▄ ▄ ▄ ▄ ▄
// █▄▄▄ ▀▄ ▄▀ █▄▄▄█ EVH · Ethereum Virtual Hook
// █▄▄▄▄ ▀ █ █ plugin hypervisor for Uniswap V4
//
// ethereumvirtualhook.xyz · x.com/VirtualHookETH
import {Versioned} from "./Versioned.sol";
/// @title Stewarded
/// @notice Two-step stewardship. Every EVH system contract carries one steward,
/// so the whole system can be handed to the Chronolock in a single sweep.
/// @dev A nomination never moves control on its own; the nominee has to pull
/// it. That makes a typo'd address a no-op instead of a lost protocol.
abstract contract Stewarded is Versioned {
address public steward;
address public nominee;
event StewardNominated(address indexed from, address indexed to);
event StewardshipTransferred(address indexed from, address indexed to);
error CallerNotSteward();
error CallerNotNominee();
error NullAddress();
modifier onlySteward() {
if (msg.sender != steward) revert CallerNotSteward();
_;
}
constructor(address steward_) {
if (steward_ == address(0)) revert NullAddress();
steward = steward_;
emit StewardshipTransferred(address(0), steward_);
}
function nominateSteward(address to) external onlySteward {
if (to == address(0)) revert NullAddress();
nominee = to;
emit StewardNominated(steward, to);
}
function acceptStewardship() external {
if (msg.sender != nominee) revert CallerNotNominee();
address previous = steward;
steward = msg.sender;
nominee = address(0);
emit StewardshipTransferred(previous, msg.sender);
}
}
src/common/Versioned.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.37;
// ▄▄▄▄▄ ▄ ▄ ▄ ▄
// █▄▄▄ ▀▄ ▄▀ █▄▄▄█ EVH · Ethereum Virtual Hook
// █▄▄▄▄ ▀ █ █ plugin hypervisor for Uniswap V4
//
// ethereumvirtualhook.xyz · x.com/VirtualHookETH
/// @title Versioned
/// @notice One release string for the whole system. Every EVH contract inherits
/// it, so any deployed address answers `VERSION()` with the same value.
abstract contract Versioned {
string public constant VERSION = "1.0";
}
src/interfaces/IOpcodeScanner.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.37;
// ▄▄▄▄▄ ▄ ▄ ▄ ▄
// █▄▄▄ ▀▄ ▄▀ █▄▄▄█ EVH · Ethereum Virtual Hook
// █▄▄▄▄ ▀ █ █ plugin hypervisor for Uniswap V4
//
// ethereumvirtualhook.xyz · x.com/VirtualHookETH
interface IOpcodeScanner {
error BannedOpcode(uint8 opcode, uint256 offset);
error NoBytecode();
error BytecodeTooLarge(uint256 size);
function inspect(address target) external view returns (bool clean, uint8 opcode, uint256 offset);
function requireClean(address target) external view;
function isBanned(uint8 opcode) external view returns (bool);
function bannedOpcodes() external view returns (uint8[] memory);
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "osaka",
"remappings": [
"v4-core/=lib/v4-core/",
"v4-periphery/=lib/v4-periphery/",
"@uniswap/v4-core/=lib/v4-core/",
"forge-std/=lib/forge-std/src/",
"solmate/=lib/v4-core/lib/solmate/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
"ds-test/=lib/v4-core/lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
"forge-gas-snapshot/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
"halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
"hardhat/=lib/v4-core/node_modules/hardhat/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"permit2/=lib/v4-periphery/lib/permit2/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.