Skip to content

Commit

Permalink
src/disasm.ts: Add UnknownProxy heuristic, track fixed SLOAD slots
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Feb 4, 2024
1 parent 30e179d commit 3ad283d
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/disasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { hexToBytes, bytesToHex } from "./utils.js";

import { opcodes, pushWidth, isPush, isLog, isHalt, isCompare } from "./opcodes.js";

import { slotResolvers, SequenceWalletProxyResolver, FixedProxyResolver } from "./proxies.js";
import { slotResolvers, SequenceWalletProxyResolver, FixedProxyResolver, UnknownProxyResolver } from "./proxies.js";


function valueToOffset(value: Uint8Array): number {
Expand Down Expand Up @@ -149,7 +149,7 @@ export class Program {
fallback?: number; // instruction offset for fallback function

eventCandidates: Array<string>; // PUSH32 found before a LOG instruction
proxySlots: Array<string>; // PUSH32 found that match known proxy slots
fixedSlots: Array<string>; // PUSH32 followed by SLOAD
proxies: Array<ProxyResolver>;

init?: Program; // Program embedded as init code
Expand All @@ -159,7 +159,7 @@ export class Program {
this.selectors = {};
this.notPayable = {};
this.eventCandidates = [];
this.proxySlots = [];
this.fixedSlots = [];
this.proxies = [];
this.init = init;
}
Expand Down Expand Up @@ -265,6 +265,13 @@ export function disasm(bytecode: string, config?: {onlyJumpTable: boolean}): Pro
continue
}

if (inst === opcodes.SLOAD &&
isPush(code.at(-2))
) {
const slot = bytesToHex(code.valueAt(-2));
p.fixedSlots.push(slot);
}

// Possible minimal proxy pattern? EIP-1167
if (inst === opcodes.DELEGATECALL &&
code.at(-2) === opcodes.GAS) {
Expand All @@ -281,6 +288,15 @@ export function disasm(bytecode: string, config?: {onlyJumpTable: boolean}): Pro
) {
// SequenceWallet-style proxy (keyed on address)
p.proxies.push(new SequenceWalletProxyResolver());

} else if (
code.at(-3) === opcodes.DUP5 &&
p.fixedSlots.length > 0
) {
// Might be a custom TransparentProxy? Not handling it yet
p.proxies.push(new UnknownProxyResolver({
slot: p.fixedSlots[0] || undefined,
}));
}
}

Expand Down

0 comments on commit 3ad283d

Please sign in to comment.