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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Alex Beregszaszi
* @date 2017
* Unit tests for the LLL compiler.
*/
#include <test/Options.h>
#include <libdevcore/FixedHash.h>
#include <liblll/Compiler.h>
#include <boost/test/unit_test.hpp>
#include <string>
#include <memory>
using namespace std;
namespace dev
{
namespace lll
{
namespace test
{
namespace
{
bool successCompile(string const& _sourceCode)
{
vector<string> errors;
bytes bytecode = lll::compileLLL(_sourceCode, dev::test::Options::get().evmVersion(), false, &errors);
if (!errors.empty())
return false;
if (bytecode.empty())
return false;
return true;
}
}
BOOST_AUTO_TEST_SUITE(LLLCompiler)
BOOST_AUTO_TEST_CASE(smoke_test)
{
char const* sourceCode = "1";
BOOST_CHECK(successCompile(sourceCode));
}
BOOST_AUTO_TEST_CASE(switch_valid)
{
char const* sourceCode = R"(
(switch (origin))
)";
BOOST_CHECK(successCompile(sourceCode));
sourceCode = R"(
(switch
1 (panic)
2 (panic))
)";
BOOST_CHECK(successCompile(sourceCode));
sourceCode = R"(
(switch
1 (panic)
2 (panic)
(panic))
)";
BOOST_CHECK(successCompile(sourceCode));
sourceCode = R"(
(switch
1 (origin)
2 (origin)
(origin))
)";
BOOST_CHECK(successCompile(sourceCode));
}
BOOST_AUTO_TEST_CASE(switch_invalid_arg_count)
{
char const* sourceCode = R"(
(switch)
)";
BOOST_CHECK(!successCompile(sourceCode));
}
BOOST_AUTO_TEST_CASE(switch_inconsistent_return_count)
{
// cannot return stack items if the default case is not present
char const* sourceCode = R"(
(switch
1 (origin)
2 (origin)
)";
BOOST_CHECK(!successCompile(sourceCode));
// return count mismatch
sourceCode = R"(
(switch
1 (origin)
2 (origin)
(panic))
)";
BOOST_CHECK(!successCompile(sourceCode));
// return count mismatch
sourceCode = R"(
(switch
1 (panic)
2 (panic)
(origin))
)";
BOOST_CHECK(!successCompile(sourceCode));
}
BOOST_AUTO_TEST_CASE(disallowed_asm_instructions)
{
for (unsigned i = 1; i <= 32; i++)
BOOST_CHECK(!successCompile("(asm PUSH" + boost::lexical_cast<string>(i) + ")"));
}
BOOST_AUTO_TEST_CASE(disallowed_functional_asm_instructions)
{
for (unsigned i = 1; i <= 32; i++)
BOOST_CHECK(!successCompile("(PUSH" + boost::lexical_cast<string>(i) + ")"));
for (unsigned i = 1; i <= 16; i++)
BOOST_CHECK(!successCompile("(DUP" + boost::lexical_cast<string>(i) + ")"));
for (unsigned i = 1; i <= 16; i++)
BOOST_CHECK(!successCompile("(SWAP" + boost::lexical_cast<string>(i) + ")"));
BOOST_CHECK(!successCompile("(JUMPDEST)"));
}
BOOST_AUTO_TEST_CASE(valid_opcodes_functional)
{
vector<string> opcodes_bytecode {
"00",
"6000600001",
"6000600002",
"6000600003",
"6000600004",
"6000600005",
"6000600006",
"6000600007",
"60006000600008",
"60006000600009",
"600060000a",
"600060000b",
"6000600010",
"6000600011",
"6000600012",
"6000600013",
"6000600014",
"600015",
"6000600016",
"6000600017",
"6000600018",
"600019",
"600060001a",
"6000600020",
"30",
"600031",
"32",
"33",
"34",
"600035",
"36",
"60006000600037",
"38",
"60006000600039",
"3a",
"60003b",
"60006000600060003c",
"3d",
"6000600060003e",
"600040",
"41",
"42",
"43",
"44",
"45",
"600050",
"600051",
"6000600052",
"6000600053",
"600054",
"6000600055",
"600056",
"6000600057",
"58",
"59",
"5a",
"60ff",
"61ffff",
"62ffffff",
"63ffffffff",
"64ffffffffff",
"65ffffffffffff",
"66ffffffffffffff",
"67ffffffffffffffff",
"68ffffffffffffffffff",
"69ffffffffffffffffffff",
"6affffffffffffffffffffff",
"6bffffffffffffffffffffffff",
"6cffffffffffffffffffffffffff",
"6dffffffffffffffffffffffffffff",
"6effffffffffffffffffffffffffffff",
"6fffffffffffffffffffffffffffffffff",
"70ffffffffffffffffffffffffffffffffff",
"71ffffffffffffffffffffffffffffffffffff",
"72ffffffffffffffffffffffffffffffffffffff",
"73ffffffffffffffffffffffffffffffffffffffff",
"74ffffffffffffffffffffffffffffffffffffffffff",
"75ffffffffffffffffffffffffffffffffffffffffffff",
"76ffffffffffffffffffffffffffffffffffffffffffffff",
"77ffffffffffffffffffffffffffffffffffffffffffffffff",
"78ffffffffffffffffffffffffffffffffffffffffffffffffff",
"79ffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7affffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7bffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7cffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"60006000a0",
"600060006000a1",
"6000600060006000a2",
"60006000600060006000a3",
"600060006000600060006000a4",
"600060006000f0",
"6000600060006000600060006000f1",
"6000600060006000600060006000f2",
"60006000f3",
"600060006000600060006000f4",
"600060006000600060006000fa",
"60006000fd",
"fe",
"6000ff"
};
vector<string> opcodes_lll {
"{ (STOP) }",
"{ (ADD 0 0) }",
"{ (MUL 0 0) }",
"{ (SUB 0 0) }",
"{ (DIV 0 0) }",
"{ (SDIV 0 0) }",
"{ (MOD 0 0) }",
"{ (SMOD 0 0) }",
"{ (ADDMOD 0 0 0) }",
"{ (MULMOD 0 0 0) }",
"{ (EXP 0 0) }",
"{ (SIGNEXTEND 0 0) }",
"{ (LT 0 0) }",
"{ (GT 0 0) }",
"{ (SLT 0 0) }",
"{ (SGT 0 0) }",
"{ (EQ 0 0) }",
"{ (ISZERO 0) }",
"{ (AND 0 0) }",
"{ (OR 0 0) }",
"{ (XOR 0 0) }",
"{ (NOT 0) }",
"{ (BYTE 0 0) }",
"{ (KECCAK256 0 0) }",
"{ (ADDRESS) }",
"{ (BALANCE 0) }",
"{ (ORIGIN) }",
"{ (CALLER) }",
"{ (CALLVALUE) }",
"{ (CALLDATALOAD 0) }",
"{ (CALLDATASIZE) }",
"{ (CALLDATACOPY 0 0 0) }",
"{ (CODESIZE) }",
"{ (CODECOPY 0 0 0) }",
"{ (GASPRICE) }",
"{ (EXTCODESIZE 0) }",
"{ (EXTCODECOPY 0 0 0 0) }",
"{ (RETURNDATASIZE) }",
"{ (RETURNDATACOPY 0 0 0) }",
"{ (BLOCKHASH 0) }",
"{ (COINBASE) }",
"{ (TIMESTAMP) }",
"{ (NUMBER) }",
"{ (DIFFICULTY) }",
"{ (GASLIMIT) }",
"{ (POP 0) }",
"{ (MLOAD 0) }",
"{ (MSTORE 0 0) }",
"{ (MSTORE8 0 0) }",
"{ (SLOAD 0) }",
"{ (SSTORE 0 0) }",
"{ (JUMP 0) }",
"{ (JUMPI 0 0) }",
"{ (PC) }",
"{ (MSIZE) }",
"{ (GAS) }",
"{ 0xff }",
"{ 0xffff }",
"{ 0xffffff }",
"{ 0xffffffff }",
"{ 0xffffffffff }",
"{ 0xffffffffffff }",
"{ 0xffffffffffffff }",
"{ 0xffffffffffffffff }",
"{ 0xffffffffffffffffff }",
"{ 0xffffffffffffffffffff }",
"{ 0xffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff }",
"{ 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff }",
"{ (LOG0 0 0) }",
"{ (LOG1 0 0 0) }",
"{ (LOG2 0 0 0 0) }",
"{ (LOG3 0 0 0 0 0) }",
"{ (LOG4 0 0 0 0 0 0) }",
"{ (CREATE 0 0 0) }",
"{ (CALL 0 0 0 0 0 0 0) }",
"{ (CALLCODE 0 0 0 0 0 0 0) }",
"{ (RETURN 0 0) }",
"{ (DELEGATECALL 0 0 0 0 0 0) }",
"{ (STATICCALL 0 0 0 0 0 0) }",
"{ (REVERT 0 0) }",
"{ (INVALID) }",
"{ (SELFDESTRUCT 0) }"
};
for (size_t i = 0; i < opcodes_bytecode.size(); i++) {
vector<string> errors;
bytes code = lll::compileLLL(opcodes_lll[i], dev::test::Options::get().evmVersion(), false, &errors);
BOOST_REQUIRE_MESSAGE(errors.empty(), opcodes_lll[i]);
BOOST_CHECK_EQUAL(toHex(code), opcodes_bytecode[i]);
}
}
BOOST_AUTO_TEST_CASE(valid_opcodes_asm)
{
vector<string> opcodes_bytecode {
"00",
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"0a",
"0b",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"1a",
"20",
"30",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
"38",
"39",
"3a",
"3b",
"3c",
"3d",
"3e",
"40",
"41",
"42",
"43",
"44",
"45",
"50",
"51",
"52",
"53",
"54",
"55",
"56",
"57",
"58",
"59",
"5a",
"5b",
"60ff",
"61ffff",
"62ffffff",
"63ffffffff",
"64ffffffffff",
"65ffffffffffff",
"66ffffffffffffff",
"67ffffffffffffffff",
"68ffffffffffffffffff",
"69ffffffffffffffffffff",
"6affffffffffffffffffffff",
"6bffffffffffffffffffffffff",
"6cffffffffffffffffffffffffff",
"6dffffffffffffffffffffffffffff",
"6effffffffffffffffffffffffffffff",
"6fffffffffffffffffffffffffffffffff",
"70ffffffffffffffffffffffffffffffffff",
"71ffffffffffffffffffffffffffffffffffff",
"72ffffffffffffffffffffffffffffffffffffff",
"73ffffffffffffffffffffffffffffffffffffffff",
"74ffffffffffffffffffffffffffffffffffffffffff",
"75ffffffffffffffffffffffffffffffffffffffffffff",
"76ffffffffffffffffffffffffffffffffffffffffffffff",
"77ffffffffffffffffffffffffffffffffffffffffffffffff",
"78ffffffffffffffffffffffffffffffffffffffffffffffffff",
"79ffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7affffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7bffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7cffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"80",
"81",
"82",
"83",
"84",
"85",
"86",
"87",
"88",
"89",
"8a",
"8b",
"8c",
"8d",
"8e",
"8f",
"90",
"91",
"92",
"93",
"94",
"95",
"96",
"97",
"98",
"99",
"9a",
"9b",
"9c",
"9d",
"9e",
"9f",
"a0",
"a1",
"a2",
"a3",
"a4",
"f0",
"f1",
"f2",
"f3",
"f4",
"fa",
"fd",
"fe",
"ff"
};
vector<string> opcodes_lll {
"{ (asm STOP) }",
"{ (asm ADD) }",
"{ (asm MUL) }",
"{ (asm SUB) }",
"{ (asm DIV) }",
"{ (asm SDIV ) }",
"{ (asm MOD) }",
"{ (asm SMOD) }",
"{ (asm ADDMOD) }",
"{ (asm MULMOD) }",
"{ (asm EXP) }",
"{ (asm SIGNEXTEND) }",
"{ (asm LT) }",
"{ (asm GT) }",
"{ (asm SLT) }",
"{ (asm SGT) }",
"{ (asm EQ) }",
"{ (asm ISZERO) }",
"{ (asm AND) }",
"{ (asm OR) }",
"{ (asm XOR) }",
"{ (asm NOT) }",
"{ (asm BYTE) }",
"{ (asm KECCAK256) }",
"{ (asm ADDRESS) }",
"{ (asm BALANCE) }",
"{ (asm ORIGIN) }",
"{ (asm CALLER) }",
"{ (asm CALLVALUE) }",
"{ (asm CALLDATALOAD) }",
"{ (asm CALLDATASIZE) }",
"{ (asm CALLDATACOPY) }",
"{ (asm CODESIZE) }",
"{ (asm CODECOPY) }",
"{ (asm GASPRICE) }",
"{ (asm EXTCODESIZE)}",
"{ (asm EXTCODECOPY) }",
"{ (asm RETURNDATASIZE) }",
"{ (asm RETURNDATACOPY) }",
"{ (asm BLOCKHASH) }",
"{ (asm COINBASE) }",
"{ (asm TIMESTAMP) }",
"{ (asm NUMBER) }",
"{ (asm DIFFICULTY) }",
"{ (asm GASLIMIT) }",
"{ (asm POP) }",
"{ (asm MLOAD) }",
"{ (asm MSTORE) }",
"{ (asm MSTORE8) }",
"{ (asm SLOAD) }",
"{ (asm SSTORE) }",
"{ (asm JUMP ) }",
"{ (asm JUMPI ) }",
"{ (asm PC) }",
"{ (asm MSIZE) }",
"{ (asm GAS) }",
"{ (asm JUMPDEST) }",
"{ (asm 0xff) }",
"{ (asm 0xffff) }",
"{ (asm 0xffffff) }",
"{ (asm 0xffffffff) }",
"{ (asm 0xffffffffff) }",
"{ (asm 0xffffffffffff) }",
"{ (asm 0xffffffffffffff) }",
"{ (asm 0xffffffffffffffff) }",
"{ (asm 0xffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) }",
"{ (asm DUP1) }",
"{ (asm DUP2) }",
"{ (asm DUP3) }",
"{ (asm DUP4) }",
"{ (asm DUP5) }",
"{ (asm DUP6) }",
"{ (asm DUP7) }",
"{ (asm DUP8) }",
"{ (asm DUP9) }",
"{ (asm DUP10) }",
"{ (asm DUP11) }",
"{ (asm DUP12) }",
"{ (asm DUP13) }",
"{ (asm DUP14) }",
"{ (asm DUP15) }",
"{ (asm DUP16) }",
"{ (asm SWAP1) }",
"{ (asm SWAP2) }",
"{ (asm SWAP3) }",
"{ (asm SWAP4) }",
"{ (asm SWAP5) }",
"{ (asm SWAP6) }",
"{ (asm SWAP7) }",
"{ (asm SWAP8) }",
"{ (asm SWAP9) }",
"{ (asm SWAP10) }",
"{ (asm SWAP11) }",
"{ (asm SWAP12) }",
"{ (asm SWAP13) }",
"{ (asm SWAP14) }",
"{ (asm SWAP15) }",
"{ (asm SWAP16) }",
"{ (asm LOG0) }",
"{ (asm LOG1) }",
"{ (asm LOG2) }",
"{ (asm LOG3) }",
"{ (asm LOG4) }",
"{ (asm CREATE) }",
"{ (asm CALL) }",
"{ (asm CALLCODE) }",
"{ (asm RETURN) }",
"{ (asm DELEGATECALL) }",
"{ (asm STATICCALL) }",
"{ (asm REVERT) }",
"{ (asm INVALID) }",
"{ (asm SELFDESTRUCT) }"
};
for (size_t i = 0; i < opcodes_bytecode.size(); i++) {
vector<string> errors;
bytes code = lll::compileLLL(opcodes_lll[i], dev::test::Options::get().evmVersion(), false, &errors);
BOOST_REQUIRE_MESSAGE(errors.empty(), opcodes_lll[i]);
BOOST_CHECK_EQUAL(toHex(code), opcodes_bytecode[i]);
}
}
BOOST_AUTO_TEST_SUITE_END()
}
}
} // end namespaces
|