This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathfunctions.cpp
344 lines (337 loc) · 13.1 KB
/
functions.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include "util.h"
#include "lllparser.h"
#include "bignum.h"
#include "optimize.h"
#include "rewriteutils.h"
#include "preprocess.h"
#include "functions.h"
strvec oldSignatureToTypes(std::string sig) {
strvec o;
for (unsigned i = 0; i < sig.size(); i++) {
if (sig[i] == 'i')
o.push_back("int256");
else if (sig[i] == 's')
o.push_back("bytes");
else if (sig[i] == 'a')
o.push_back("int256[]");
else
err("Bad signature: "+sig, Metadata());
}
return o;
}
std::vector<std::string> getArgNames(std::vector<Node> args) {
std::vector<std::string> o;
for (unsigned i = 0; i < args.size(); i++) {
if (args[i].val == ":")
o.push_back(args[i].args[0].val);
else
o.push_back(args[i].val);
}
return o;
}
std::vector<std::string> getArgTypes(std::vector<Node> args) {
std::vector<std::string> o;
for (unsigned i = 0; i < args.size(); i++) {
if (args[i].val == ":" && args[i].args[1].val == "str")
o.push_back("bytes");
else if (args[i].val == ":" && args[i].args[1].val == "arr")
o.push_back("int256[]");
else if (args[i].val == ":" && args[i].args[1].val == "access")
o.push_back(args[i].args[1].args[0].val + "[]");
else if (args[i].val == ":" && args[i].args[1].val == "int")
o.push_back("int256");
else if (args[i].val == ":" && (args[i].args[1].val == "s" ||
args[i].args[1].val == "a"))
err("Invalid datatype! Remember to change s -> str and a -> arr for latest version", args[0].metadata);
else if (args[i].val == ":")
o.push_back(args[i].args[1].val);
else
o.push_back("int256");
}
return o;
}
// Convert a list of arguments into a node wrapping another
// node that can use _datastart and _datasz
Node packArguments(std::vector<Node> args, strvec argTypeNames,
unsigned functionPrefix, Node inner, Metadata m,
bool usePrefix) {
// Arguments
std::vector<Node> funArgs;
// Is a variable an array?
std::vector<int> argTypes;
// Fill up above three argument lists
int argCount = 0;
bool haveVarg = false;
for (unsigned i = 0; i < args.size(); i++) {
Metadata m = args[i].metadata;
if (args[i].val == "=") {
// do nothing
}
else {
// Determine the correct argument type
std::string argType;
if (argCount >= (signed)argTypeNames.size())
err("Too many args. Note that the signature of the function "
"that you are using may no longer be valid. "
"If you want to be sure, run "
"`serpent mk_signature <file>` on the contract you are "
"including to determine the correct signature.", m);
argType = argTypeNames[argCount];
funArgs.push_back(args[i]);
// Array
if (isArrayType(argType)) {
argTypes.push_back(ARRAY);
haveVarg = true;
}
// Long string
else if (argType == "bytes" || argType == "string") {
argTypes.push_back(BYTES);
haveVarg = true;
}
// Integer (also usable for short strings)
else if (argType == "int256" or argType == "bytes20" or argType == "address")
argTypes.push_back(STATIC);
else {
argTypes.push_back(STATIC);
}
argCount++;
}
}
int static_arg_size = (4 * usePrefix) + (funArgs.size()) * 32;
int prefixOffset = 4 * usePrefix;
Node pattern;
// If we don't have any variable arguments, then we can take a fast track:
// simply encode everything sequentially as
// <4 prefix bytes> <arg 1> <arg 2> ...
if (!haveVarg) {
// For convenience, we create an allocated memory slice that is
// slightly too long, and then only use the part from position 28.
// This lets us do (mstore <start of allocated memory> <prefix>)
// to store the prefix bytes
pattern = asn("with",
tkn("_datastart"),
asn("add",
asn("alloc", tkn(utd(static_arg_size + 32))),
tkn("28")),
asn("seq"));
if (usePrefix)
pattern.args[2].args.push_back(
asn("mstore",
asn("sub", tkn("_datastart"), tkn("28")),
tkn(utd(functionPrefix))));
for (unsigned i = 0; i < funArgs.size(); i++) {
pattern.args[2].args.push_back(
asn("mstore",
asn("add", tkn("_datastart", m), tkn(utd(i * 32 + prefixOffset), m)),
funArgs[i],
m));
}
pattern.args[2].args.push_back(
asn("with",
tkn("_datasz", m),
tkn(utd(static_arg_size), m),
inner,
m));
return pattern;
}
// More general case, where we do have some variable arguments
// Start by allocating memory for the "head", the slice of data
// that contains only the static arguments. We also allocate some
// extra memory in the head in order to store the start positions
// and sizes of all dynamic variables
pattern = asn("with",
tkn("_offset", m),
tkn(utd(static_arg_size - prefixOffset), m),
asn("with",
tkn("_head", m),
asn("add",
asn("alloc", tkn(utd(static_arg_size * 3 + 32), m), m),
tkn("28")),
asn("seq")));
// Add prefix bytes
if (usePrefix)
pattern.args[2].args[2].args.push_back(
asn("mstore",
asn("sub", tkn("_head"), tkn("28")),
tkn(utd(functionPrefix))));
// Add head bytes per argument
for (unsigned i = 0; i < funArgs.size(); i++) {
Node headNode;
// Static-sized variables
if (argTypes[i] == STATIC) {
pattern.args[2].args[2].args.push_back(
asn("mstore",
asn("add", tkn("_head", m), tkn(utd(i * 32 + prefixOffset), m)),
funArgs[i],
m));
}
// Dynamic-sized
else {
// Compute the total number of bytes needed to represent a variable,
// including the length prefix
Node szNode = asn("mload", asn("sub", tkn("_n"), tkn("32")));
Node varSizeNode;
if (argTypes[i] == BYTES)
varSizeNode = asn("add", tkn("32"), asn("ceil32", szNode));
else
varSizeNode = asn("add", tkn("32"), asn("mul", tkn("32"), szNode));
pattern.args[2].args[2].args.push_back(
asn("with",
tkn("_n"),
funArgs[i],
// Compute variable size
asn("with",
tkn("_sz"),
varSizeNode,
asn("seq",
// Store offset in head
asn("mstore",
asn("add", tkn("_head"), tkn(utd(i * 32 + prefixOffset))),
tkn("_offset")),
// Store the _real_ memory start location variable value (ie.
// the start of the 32 byte length prefix)
asn("mstore",
asn("add",
tkn("_head"),
tkn(utd(static_arg_size + i * 32))),
asn("sub", tkn("_n"), tkn("32"))),
// Store variable size
asn("mstore",
asn("add",
tkn("_head"),
tkn(utd(static_arg_size * 2 + i * 32))),
tkn("_sz")),
// Update offset
asn("set",
tkn("_offset"),
asn("add", tkn("_offset"), tkn("_sz")))))));
}
}
// Now that we know the required size of the data array, we can create it
Node subpattern = asn("with",
tkn("_datastart"),
asn("alloc", asn("add", tkn("4"), tkn("_offset"))),
asn("seq"));
// And copy the head into it
subpattern.args[2].args.push_back(
asn("mcopy",
tkn("_datastart"),
tkn("_head"),
tkn(utd(static_arg_size))));
// We use the "offset" variable here as a counter of where we are in the
// data array
subpattern.args[2].args.push_back(
asn("set",
tkn("_offset"),
asn("add", tkn("_datastart"), tkn(utd(static_arg_size)))));
// Store the variable-szed arguments
for (unsigned i = 0; i < funArgs.size(); i++) {
Node headNode;
if (argTypes[i] != STATIC) {
subpattern.args[2].args.push_back(
asn("with",
tkn("_sz"),
// Grab the size that we stored in the head
asn("mload",
asn("add",
tkn("_head"),
tkn(utd(static_arg_size * 2 + i * 32)))),
asn("seq",
asn("mcopy",
tkn("_offset"),
// Grab the start position that we stored
// in the head
asn("mload",
asn("add",
tkn("_head"),
tkn(utd(static_arg_size + i * 32)))),
tkn("_sz")),
asn("set",
tkn("_offset"),
asn("add", tkn("_offset"), tkn("_sz"))))));
}
}
subpattern.args[2].args.push_back(
asn("with",
tkn("_datasz"),
asn("sub", tkn("_offset"), tkn("_datastart")),
inner));
pattern.args[2].args[2].args.push_back(subpattern);
return pattern;
}
// Create a node for argument unpacking
Node unpackArguments(std::vector<Node> vars, Metadata m) {
std::vector<std::string> varNames;
std::vector<int> varTypes;
bool haveVarg = false;
// Fill in variable and long variable names, as well as which
// long variables are arrays and which are strings
for (unsigned i = 0; i < vars.size(); i++) {
if (vars[i].val == ":") {
if (vars[i].args.size() != 2)
err("Malformed def!", m);
varNames.push_back(vars[i].args[0].val);
std::string tag = vars[i].args[1].val;
haveVarg = true;
if (tag == "str" || tag == "bytes" || tag == "string")
varTypes.push_back(BYTES);
else if (tag == "access")
varTypes.push_back(ARRAY);
else if (isArrayType(tag))
varTypes.push_back(ARRAY);
else {
varTypes.push_back(STATIC);
}
}
else {
varNames.push_back(vars[i].val);
varTypes.push_back(STATIC);
}
}
std::vector<Node> sub;
if (!varNames.size()) {
// do nothing if we have no arguments
}
else if (!haveVarg) {
for (unsigned i = 0; i < varNames.size(); i++) {
sub.push_back(
asn("set",
tkn(varNames[i]),
asn("calldataload", tkn(utd(4 + 32 * i)))));
}
}
else {
sub.push_back(asn("with",
tkn("_inputdata"),
asn("alloc", asn("calldatasize")),
asn("seq")));
sub[0].args[2].args.push_back(asn("calldatacopy",
tkn("_inputdata"),
tkn("4"),
asn("calldatasize")));
// Copy over short variables
for (unsigned i = 0; i < varNames.size(); i++) {
if (varTypes[i] == STATIC) {
sub[0].args[2].args.push_back(
asn("set",
tkn(varNames[i]),
asn("calldataload", tkn(utd(4 + 32 * i)))));
}
else {
sub[0].args[2].args.push_back(
asn("set",
tkn(varNames[i]),
asn("add",
asn("add",
tkn("_inputdata"),
tkn("32")),
asn("calldataload", tkn(utd(4 + 32 * i))))));
}
}
}
return asn("seq", sub, m);
}