-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_hardcoded_optimization.py
More file actions
97 lines (80 loc) · 2.84 KB
/
Copy pathtest_hardcoded_optimization.py
File metadata and controls
97 lines (80 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
"""Test to understand the hardcoded optimization behavior."""
from textwrap import dedent
import subprocess
import tempfile
import os
def get_venom_ir(yul_code: str) -> str:
"""Get Venom IR output for Yul code."""
with tempfile.NamedTemporaryFile(mode='w', suffix='.yul', delete=False) as f:
f.write(yul_code)
f.flush()
yul_file = f.name
try:
cmd = [
'python',
'vyper/cli/yul.py',
'--venom',
yul_file
]
env = os.environ.copy()
env['PYTHONPATH'] = '/Users/harkal/projects/charles_cooper/repos/vyper:.'
result = subprocess.run(
cmd,
capture_output=True,
text=True,
env=env
)
if result.returncode != 0:
raise RuntimeError(f"Yul compilation failed: {result.stderr}")
return result.stdout
finally:
os.unlink(yul_file)
def main():
# Pattern that should trigger hardcoding
yul_hardcoded = dedent("""
object "Test" {
code {
function abi_decode_tuple_t_uint256t_uint256(headStart, dataEnd) -> value0, value1 {
value0 := calldataload(headStart)
value1 := calldataload(add(headStart, 32))
}
let x, y := abi_decode_tuple_t_uint256t_uint256(4, calldatasize())
mstore(0, x)
mstore(32, y)
}
}
""")
# Pattern that should NOT trigger hardcoding (three params)
yul_three_params = dedent("""
object "Test" {
code {
function abi_decode_tuple_t_uint256t_uint256t_uint256(headStart, dataEnd) -> value0, value1, value2 {
value0 := calldataload(headStart)
value1 := calldataload(add(headStart, 32))
value2 := calldataload(add(headStart, 64))
}
let x, y, z := abi_decode_tuple_t_uint256t_uint256t_uint256(4, calldatasize())
mstore(0, x)
mstore(32, y)
mstore(64, z)
}
}
""")
print("=== VENOM IR for 2-param abi_decode (potentially hardcoded) ===")
ir2 = get_venom_ir(yul_hardcoded)
print(ir2)
print("\n=== VENOM IR for 3-param abi_decode (not hardcoded) ===")
ir3 = get_venom_ir(yul_three_params)
print(ir3)
# Check for function invocation vs inline
if "invoke" in ir2 and "abi_decode" in ir2:
print("\n✓ 2-param version uses function invocation (NOT inlined)")
else:
print("\n✗ 2-param version appears to be inlined")
if "invoke" in ir3 and "abi_decode" in ir3:
print("✓ 3-param version uses function invocation")
else:
print("✗ 3-param version appears to be inlined")
if __name__ == "__main__":
main()