This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode: 0x38
Introduced: Frontier (EVM genesis)
CODESIZE pushes the byte length of the currently executing code onto the stack.
Specification
Stack Input:
Stack Output:
Gas Cost: 2 (GasQuickStep)
Operation:
Behavior
Returns the size of the currently executing bytecode, including deployed code and constructor code.
Examples
Basic Usage
function getCodeSize() public pure returns (uint256 size) {
assembly {
size := codesize()
}
}
Code Copying
function copyOwnCode() public pure returns (bytes memory code) {
assembly {
let size := codesize()
code := mload(0x40) // Free memory pointer
mstore(code, size)
codecopy(add(code, 0x20), 0, size)
mstore(0x40, add(add(code, 0x20), size))
}
}
Gas Cost
Cost: 2 gas (GasQuickStep)
Common Usage
Constructor Detection
During contract construction, CODESIZE includes constructor code. After deployment, only runtime code.
Implementation
export function codesize(frame: FrameType): EvmError | null {
const gasErr = consumeGas(frame, 2n);
if (gasErr) return gasErr;
const pushErr = pushStack(frame, BigInt(frame.bytecode.length));
if (pushErr) return pushErr;
frame.pc += 1;
return null;
}
References