-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJULIA Examples.txt
190 lines (138 loc) · 5.9 KB
/
JULIA Examples.txt
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
calldata of struct (fixed size)
struct Thing {
uint256 u;
uint256[2] u_arr0;
uint256[2] u_arr1;
}
function test(uint256 u0, Thing thing, uint256 u1)
test(1, [13, [1,2], [3,4]], 2)
0e982672
0x000 0000000000000000000000000000000000000000000000000000000000000001 // u0
0x020 000000000000000000000000000000000000000000000000000000000000000d // thing.u
0x040 0000000000000000000000000000000000000000000000000000000000000001 // thing.u_arr0[0]
0x060 0000000000000000000000000000000000000000000000000000000000000002 // thing.u_arr0[1]
0x080 0000000000000000000000000000000000000000000000000000000000000003 // thing.u_arr1[0]
0x0a0 0000000000000000000000000000000000000000000000000000000000000004 // thing.u_arr1[1]
0x0c0 0000000000000000000000000000000000000000000000000000000000000002 // u1
calldata of struct (dynamic)
struct Thing {
uint256 u;
uint256[] u_arr0;
uint256[] u_arr1;
}
function test(uint256 u0, Thing thing, uint256 u1)
test(1, [13, [1,2], [3,4]], 2)
116523a7
0x000 0000000000000000000000000000000000000000000000000000000000000001 // u0
0x020 0000000000000000000000000000000000000000000000000000000000000060 // thing pos
0x040 0000000000000000000000000000000000000000000000000000000000000002 // u1
0x060 000000000000000000000000000000000000000000000000000000000000000d // thing.u
0x080 0000000000000000000000000000000000000000000000000000000000000060 // thing.u_arr0 offset from pos
0x0a0 00000000000000000000000000000000000000000000000000000000000000c0 // thing.u_arr1 offset from pos
0x0c0 0000000000000000000000000000000000000000000000000000000000000002 // thing.u_arr0 len
0x0e0 0000000000000000000000000000000000000000000000000000000000000001 // thing.u_arr0[0]
0x100 0000000000000000000000000000000000000000000000000000000000000002 // thing.u_arr0[1]
0x120 0000000000000000000000000000000000000000000000000000000000000002 // thing.u_arr1 len
0x140 0000000000000000000000000000000000000000000000000000000000000003 // thing.u_arr1[0]
0x160 0000000000000000000000000000000000000000000000000000000000000004 // thing.u_arr1[1]
dynamic array
contract Test {
uint256[] arr;
constructor () {
arr.push(1);
arr.push(2);
arr.push(3);
}
function getArr() public view returns (uint256[]) {
assembly {
mstore(0, arr_slot) // arr elem storage slot
let arr_pos := keccak256(0, 0x20) // at keccak256(arr_slot)
let arr_len := sload(arr_slot) // len at arr_slot
let ptr := mload(0x40) // get free mem ptr for copying arr to return
let ret := ptr // copy ptr so we can move ptr
mstore(ptr, 0x20) // first store sizeof arr elem
ptr := add(ptr, 0x20) // move ptr a word
mstore(ptr, arr_len) // then store arr len
for { let i := 0 } lt(i, arr_len) { i := add(i, 1) } {
ptr := add(ptr, 0x20) // move ptr a word
mstore(ptr, sload(add(arr_pos, i))) // then store each of the elem
}
let size := mul(add(2, arr_len), 0x20) // size = 2 + len words
return(ret, size) // return arr
}
}
}
Test.getArr() => [1,2,3]
mapping value
contract Test {
mapping(uint256=>uint256) map;
constructor () {
map[13] = 42;
}
function getMap(uint256 _key) public view returns (uint256 _val) {
assembly {
mstore(0x00, _key) // key for map
mstore(0x20, map_slot) // concat with map slot
let val_pos := keccak256(0, 0x40) // keccak256'd
_val := sload(val_pos) // return value from storage
}
}
}
Test.getMap(13) => 42
mapping to dynamic array
contract Test {
mapping(uint256=>uint256[]) map;
constructor () {
map[13].push(1);
map[13].push(2);
map[13].push(3);
}
function getArr(uint256 _key) public view returns (uint256[]) {
assembly {
mstore(0x00, _key) // key for map
mstore(0x20, map_slot) // concat with map slot
let arr_len_pos := keccak256(0, 0x40) // keccak256'd
let arr_len := sload(arr_len_pos) // len at arr_len_pos
mstore(0, arr_len_pos) // arr elems storage slot
let arr_pos := keccak256(0, 0x20) // at keccak256(arr_len_pos)
let ptr := mload(0x40) // get free mem ptr for copying arr to return
let ret := ptr // copy ptr so we can move ptr
mstore(ptr, 0x20) // first store sizeof arr elem
ptr := add(ptr, 0x20) // move ptr a word
mstore(ptr, arr_len) // then store arr len
for { let i := 0 } lt(i, arr_len) { i := add(i, 1) } {
ptr := add(ptr, 0x20) // move ptr a word
mstore(ptr, sload(add(arr_pos, i))) // then store each of the elem
}
let size := mul(add(2, arr_len), 0x20) // size = 2 + len words
return(ret, size) // return arr
}
}
}
Test.getArr(13) => [1,2,3]
useful debugging snippets
// log calldata
assembly {
let ptr := mload(0x40)
let size := calldatasize()
calldatacopy(ptr, 0, size)
log0(ptr, size)
}
// log call signature
assembly {
mstore(0, calldataload(0))
log0(0, 0x04)
}
// log msg.sender
assembly {
mstore(0, caller())
log0(0x0c, 0x14)
}
tips & reminders
if function is being called (not hitting fallback)
but no code executes, double check in data size
when copying dynamic struct from calldata to mem
for new call, struct position needs to be updated
function sig for fallback use tuples
function sig for calls inside use structs
struct storage items are passed as storage slot