-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathLifterTest.swift
3098 lines (2541 loc) · 95.5 KB
/
LifterTest.swift
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import XCTest
@testable import Fuzzilli
class LifterTests: XCTestCase {
func testDeterministicLifting() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
for _ in 0..<10 {
b.buildPrefix()
b.build(n: 100, by: .generating)
let program = b.finalize()
let code1 = fuzzer.lifter.lift(program)
let code2 = fuzzer.lifter.lift(program)
XCTAssertEqual(code1, code2)
}
}
func testFuzzILLifter() {
// Mostly this just ensures that the FuzzILLifter supports all operations
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let lifter = FuzzILLifter()
for _ in 0..<100 {
b.buildPrefix()
b.build(n: 100, by: .generating)
let program = b.finalize()
_ = lifter.lift(program)
}
}
func testLiftingWithLineNumbers() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
for _ in 0..<10 {
b.buildPrefix()
b.build(n: 100, by: .generating)
let program = b.finalize()
let codeWithLinenumbers = fuzzer.lifter.lift(program, withOptions: .includeLineNumbers)
for (i, line) in codeWithLinenumbers.split(separator: "\n").enumerated() {
XCTAssert(line.trimmingCharacters(in: .whitespaces).starts(with: "\(i+1)"))
}
}
}
func testExpressionInlining1() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let Object = b.createNamedVariable(forBuiltin: "Object")
let obj = b.construct(Object)
let o = b.createNamedVariable(forBuiltin: "SomeObj")
let foo = b.getProperty("foo", of: o)
let bar = b.getProperty("bar", of: foo)
let i = b.loadInt(42)
let r = b.callMethod("baz", on: bar, withArgs: [i, i])
b.setProperty("r", of: obj, to: r)
let Math = b.createNamedVariable(forBuiltin: "Math")
let lhs = b.callMethod("random", on: Math)
let rhs = b.loadFloat(13.37)
let s = b.binary(lhs, rhs, with: .Add)
b.setProperty("s", of: obj, to: s)
b.getProperty("s", of: obj)
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
const v1 = new Object();
v1.r = SomeObj.foo.bar.baz(42, 42);
v1.s = Math.random() + 13.37;
v1.s;
"""
XCTAssertEqual(actual, expected)
}
func testExpressionInlining2() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let Object = b.createNamedVariable(forBuiltin: "Object")
let obj = b.construct(Object)
let i = b.loadInt(42)
let o = b.createNamedVariable(forBuiltin: "SomeObj")
let foo = b.getProperty("foo", of: o)
let bar = b.getProperty("bar", of: foo)
let baz = b.getProperty("baz", of: bar)
let r = b.callFunction(baz, withArgs: [i, i])
b.setProperty("r", of: obj, to: r)
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
const v1 = new Object();
const t1 = SomeObj.foo.bar.baz;
v1.r = t1(42, 42);
"""
XCTAssertEqual(actual, expected)
}
func testExpressionInlining3() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
// Test that effectful operations aren't reordered in any sematic-changing way.
let Object = b.createNamedVariable(forBuiltin: "Object")
let obj = b.construct(Object)
let effectful = b.createNamedVariable(forBuiltin: "effectful")
var lhs = b.callMethod("func1", on: effectful)
var rhs = b.callMethod("func2", on: effectful)
var res = b.binary(lhs, rhs, with: .Add)
b.setProperty("res1", of: obj, to: res)
lhs = b.callMethod("func3", on: effectful)
b.callMethod("func4", on: effectful)
rhs = b.loadFloat(13.37)
res = b.binary(lhs, rhs, with: .Add)
b.setProperty("res2", of: obj, to: res)
b.callMethod("func5", on: effectful)
let val1 = b.callMethod("func6", on: effectful)
b.callMethod("func7", on: effectful)
let val2 = b.loadFloat(13.37)
res = b.unary(.Minus, val1)
b.setProperty("res3", of: obj, to: res)
b.setProperty("res4", of: obj, to: val2)
let arg = b.callMethod("func8", on: effectful)
let res1 = b.callMethod("func9", on: effectful)
let res2 = b.callMethod("func10", on: effectful, withArgs: [arg])
b.setProperty("res5", of: obj, to: res1)
b.setProperty("res6", of: obj, to: res2)
b.callMethod("func11", on: effectful)
let tmp1 = b.callMethod("func12", on: effectful)
let tmp2 = b.callMethod("func13", on: effectful)
lhs = b.callMethod("func14", on: effectful)
rhs = b.callMethod("func15", on: effectful)
rhs = b.binary(lhs, rhs, with: .Mul)
lhs = b.binary(tmp2, rhs, with: .Add)
res = b.binary(lhs, tmp1, with: .Div)
b.setProperty("res7", of: obj, to: res)
var x = b.callMethod("func16", on: effectful)
var y = b.callMethod("func17", on: effectful)
var z = b.callMethod("func18", on: effectful)
res = b.callMethod("func19", on: effectful, withArgs: [x, y, z])
b.setProperty("res8", of: obj, to: res)
x = b.callMethod("func20", on: effectful)
y = b.callMethod("func21", on: effectful)
z = b.callMethod("func22", on: effectful)
res = b.callMethod("func23", on: effectful, withArgs: [y, z, x])
b.setProperty("res9", of: obj, to: res)
x = b.callMethod("func24", on: effectful)
y = b.callMethod("func25", on: effectful)
z = b.callMethod("func26", on: effectful)
res = b.callMethod("func27", on: effectful, withArgs: [x, z, y])
b.setProperty("res10", of: obj, to: res)
x = b.callMethod("func28", on: effectful)
y = b.callMethod("func29", on: effectful)
z = b.callMethod("func30", on: effectful)
res = b.callMethod("func31", on: effectful, withArgs: [z, x, y])
b.setProperty("res11", of: obj, to: res)
x = b.callMethod("func32", on: effectful)
y = b.callMethod("func33", on: effectful)
z = b.callMethod("func34", on: effectful)
let tmp = b.callMethod("func35", on: effectful, withArgs: [y])
res = b.callMethod("func36", on: effectful, withArgs: [x, z, tmp])
b.setProperty("res12", of: obj, to: res)
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
const v1 = new Object();
v1.res1 = effectful.func1() + effectful.func2();
const v6 = effectful.func3();
effectful.func4();
v1.res2 = v6 + 13.37;
effectful.func5();
const v11 = effectful.func6();
effectful.func7();
v1.res3 = -v11;
v1.res4 = 13.37;
const v15 = effectful.func8();
const v16 = effectful.func9();
const v17 = effectful.func10(v15);
v1.res5 = v16;
v1.res6 = v17;
effectful.func11();
const v19 = effectful.func12();
v1.res7 = (effectful.func13() + (effectful.func14() * effectful.func15())) / v19;
v1.res8 = effectful.func19(effectful.func16(), effectful.func17(), effectful.func18());
const v30 = effectful.func20();
v1.res9 = effectful.func23(effectful.func21(), effectful.func22(), v30);
const v34 = effectful.func24();
const v35 = effectful.func25();
v1.res10 = effectful.func27(v34, effectful.func26(), v35);
const v38 = effectful.func28();
const v39 = effectful.func29();
v1.res11 = effectful.func31(effectful.func30(), v38, v39);
const v42 = effectful.func32();
const v43 = effectful.func33();
const v44 = effectful.func34();
v1.res12 = effectful.func36(v42, v44, effectful.func35(v43));
"""
XCTAssertEqual(actual, expected)
}
func testExpressionInlining4() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let someValue = b.createNamedVariable(forBuiltin: "someValue")
let computeThreshold = b.createNamedVariable(forBuiltin: "computeThreshold")
let threshold = b.callFunction(computeThreshold)
let cond = b.compare(someValue, with: threshold, using: .lessThan)
// The comparison and the function call can be inlined into the header of the if-statement.
b.buildIf(cond) {
let doSomething = b.createNamedVariable(forBuiltin: "doSomething")
b.callFunction(doSomething)
}
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
if (someValue < computeThreshold()) {
doSomething();
}
"""
XCTAssertEqual(actual, expected)
}
func testExpressionInlining5() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let v0 = b.loadInt(0)
let f = b.createNamedVariable(forBuiltin: "computeNumIterations")
let numIterations = b.callFunction(f)
// The function call should not be inlined into the loop header as that would change the programs behavior.
b.buildWhileLoop({ b.compare(v0, with: numIterations, using: .lessThan) }) {
b.unary(.PostInc, v0)
}
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
let v0 = 0;
const v2 = computeNumIterations();
while (v0 < v2) {
v0++;
}
"""
XCTAssertEqual(actual, expected)
}
func testExpressionInlining6() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
// Test that (potentially) effectful operations are only executed once.
let Object = b.createNamedVariable(forBuiltin: "Object")
let o = b.construct(Object)
let v0 = b.loadInt(1337)
let f1 = b.createNamedVariable(forBuiltin: "func1")
let r1 = b.callFunction(f1, withArgs: [v0])
let f2 = b.createNamedVariable(forBuiltin: "func2")
let r2 = b.callFunction(f2, withArgs: [r1])
b.setProperty("x", of: o, to: r2)
b.setProperty("y", of: o, to: r2)
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
const v1 = new Object();
const v6 = func2(func1(1337));
v1.x = v6;
v1.y = v6;
"""
XCTAssertEqual(actual, expected)
}
func testExpressionInlining7() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
// The identifier for NaN should be inlined into its uses...
let n1 = b.loadFloat(Double.nan)
b.createArray(with: [n1, n1, n1])
// ... but when it's reassigned, the identifier needs to be stored to a local variable.
let n2 = b.loadFloat(Double.nan)
b.reassign(n2, to: b.loadFloat(13.37))
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
[NaN,NaN,NaN];
let v2 = NaN;
v2 = 13.37;
"""
XCTAssertEqual(actual, expected)
}
func testExpressionInlining8() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
b.buildPlainFunction(with: .parameters(n: 1)) { args in
let o = args[0]
let x = b.getProperty("x", of: o)
let y = b.getProperty("y", of: o)
let z = b.getProperty("z", of: o)
// Cannot inline the property load of .x as that would change the
// evaluation order at runtime (.x would now be loaded after .y).
let r = b.ternary(y, x, z)
b.doReturn(r)
}
// Note that this example also demonstrates that we currently allow expressions
// to be inlined into lazily-evaluated expressions, which may cause them to not
// be executed at runtime (in the example here, the a1.z load may never execute).
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
function f0(a1) {
const v2 = a1.x;
return a1.y ? v2 : a1.z;
}
"""
XCTAssertEqual(actual, expected)
}
func testExpressionInlining9() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
b.buildPlainFunction(with: .parameters(n: 1)) { args in
let p = args[0]
let two = b.loadInt(2)
let Math = b.createNamedVariable(forBuiltin: "Math")
let x = b.getProperty("x", of: p)
let xSquared = b.binary(x, two, with: .Exp)
let y = b.getProperty("y", of: p)
let ySquared = b.binary(y, two, with: .Exp)
let sum = b.binary(xSquared, ySquared, with: .Add)
let result = b.callMethod("sqrt", on: Math, withArgs: [sum])
b.doReturn(result)
}
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
function f0(a1) {
return Math.sqrt((a1.x ** 2) + (a1.y ** 2));
}
"""
XCTAssertEqual(actual, expected)
}
func testExpressionUninlining() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
// This test ensures that expression inlining works corretly even when
// expressions are explicitly "un-inlined", for example by a SetElement
// operation where we force the object to be a variable.
let i1 = b.loadInt(0)
let i2 = b.loadInt(10)
b.buildDoWhileLoop(do: {
// The SetElement will "un-inline" i2, but for the do-while loop we'll still need the inlined expression (`10`).
b.setElement(0, of: i2, to: i1)
b.setElement(1, of: i2, to: i1)
b.unary(.PostInc, i1)
}, while: { b.compare(i1, with: i2, using: .lessThan) })
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
let v0 = 0;
do {
const t2 = 10;
t2[0] = v0;
const t4 = 10;
t4[1] = v0;
v0++;
} while (v0 < 10)
"""
XCTAssertEqual(actual, expected)
}
func testIdentifierLifting() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
// This tests checks that identifiers and other pure expressions
// are completely omitted from the lifted code if they are not used.
// This is important in some cases, for example if a non-existant
// named variable is (only) accessed via `typeof`. In that case, the
// `typeof` will make the access valid (no exception is raised), and so
// the construct can currently only be minimized if an (unused) named
// variable access also does not raise an exception.
b.loadInt(42)
b.loadString("foobar")
b.createNamedVariable("nonexistant", declarationMode: .none)
let v = b.createNamedVariable("alsoNonexistantButSafeToAccessViaTypeOf", declarationMode: .none)
b.typeof(v)
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
typeof alsoNonexistantButSafeToAccessViaTypeOf;
"""
XCTAssertEqual(actual, expected)
}
func testObjectLiteralLifting() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let v1 = b.loadInt(42)
let v2 = b.loadFloat(13.37)
let v3 = b.loadString("foobar")
let null = b.loadNull()
let v4 = b.binary(v3, v1, with: .Add)
let otherObject = b.createNamedVariable(forBuiltin: "SomeObject")
let toPrimitive = b.getProperty("toPrimitive", of: b.createNamedVariable(forBuiltin: "Symbol"))
b.buildObjectLiteral { obj in
obj.addProperty("p1", as: v1)
obj.addProperty("__proto__", as: null)
obj.addElement(0, as: v2)
obj.addElement(-1, as: v2)
obj.addComputedProperty(v3, as: v3)
obj.addProperty("p2", as: v2)
obj.addComputedProperty(v4, as: v1)
obj.setPrototype(to: null)
obj.addMethod("m", with: .parameters(n: 2)) { args in
let r = b.binary(args[1], args[2], with: .Sub)
b.doReturn(r)
}
obj.addComputedMethod(toPrimitive, with: .parameters(n: 0)) { args in
b.doReturn(v1)
}
obj.addGetter(for: "prop") { this in
let r = b.getProperty("p", of: this)
b.doReturn(r)
}
obj.addSetter(for: "prop") { this, v in
b.setProperty("p", of: this, to: v)
}
obj.copyProperties(from: otherObject)
}
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
const v4 = "foobar" + 42;
const v7 = Symbol.toPrimitive;
const v17 = {
p1: 42,
__proto__: null,
0: 13.37,
[-1]: 13.37,
["foobar"]: "foobar",
p2: 13.37,
[v4]: 42,
__proto__: null,
m(a9, a10) {
return a9 - a10;
},
[v7]() {
return 42;
},
get prop() {
return this.p;
},
set prop(a16) {
this.p = a16;
},
...SomeObject,
};
"""
XCTAssertEqual(actual, expected)
}
func testObjectLiteralIsDistinguishableFromBlockStatement() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let i1 = b.loadInt(1)
let i2 = b.loadInt(2)
b.buildObjectLiteral { obj in
obj.addProperty("foo", as: i1)
obj.addProperty("bar", as: i2)
}
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
// These must not lift to something like "{ foo: 1, bar: 2 };" as that would be invalid:
// the parser cannot distinguish it from a block statement.
let expected = """
const v2 = { foo: 1, bar: 2 };
"""
XCTAssertEqual(actual, expected)
}
func testObjectLiteralInlining() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let print = b.createNamedVariable(forBuiltin: "print")
// We inline empty object literals.
let o1 = b.buildObjectLiteral { obj in }
b.callFunction(print, withArgs: [o1])
// We inline "simple" object literals"
let i = b.loadInt(42)
let f = b.loadFloat(13.37)
let foo = b.loadString("foo")
let o2 = b.buildObjectLiteral { obj in
obj.addProperty("foo", as: i)
obj.addProperty("bar", as: f)
obj.addElement(42, as: foo)
}
b.callFunction(print, withArgs: [o2])
// We don't inline object literals as soon as they have any methods.
let o3 = b.buildObjectLiteral { obj in
obj.addMethod("baz", with: .parameters(n: 0)) { _ in }
}
b.callFunction(print, withArgs: [o3])
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
print({});
print({ foo: 42, bar: 13.37, 42: "foo" });
const v9 = {
baz() {
},
};
print(v9);
"""
XCTAssertEqual(actual, expected)
}
func testClassDefinitionLifting() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let i = b.loadInt(42)
let two = b.loadInt(2)
let baz = b.loadString("baz")
let baz42 = b.binary(baz, i, with: .Add)
let C = b.buildClassDefinition() { cls in
cls.addInstanceProperty("foo")
cls.addInstanceProperty("bar", value: baz)
cls.addInstanceElement(0, value: i)
cls.addInstanceElement(1)
cls.addInstanceElement(-1)
cls.addInstanceComputedProperty(baz42)
cls.addInstanceComputedProperty(two, value: baz42)
cls.addConstructor(with: .parameters(n: 1)) { params in
let this = params[0]
b.setProperty("foo", of: this, to: params[1])
}
cls.addInstanceMethod("m", with: .parameters(n: 0)) { params in
let this = params[0]
let foo = b.getProperty("foo", of: this)
b.doReturn(foo)
}
cls.addInstanceGetter(for: "baz") { this in
b.doReturn(b.loadInt(1337))
}
cls.addInstanceSetter(for: "baz") { this, v in
}
cls.addStaticProperty("foo")
cls.addStaticInitializer { this in
b.setProperty("foo", of: this, to: i)
}
cls.addStaticProperty("bar", value: baz)
cls.addStaticElement(0, value: i)
cls.addStaticElement(1)
cls.addStaticElement(-1)
cls.addStaticComputedProperty(baz42)
cls.addStaticComputedProperty(two, value: baz42)
cls.addStaticMethod("m", with: .parameters(n: 0)) { params in
let this = params[0]
let foo = b.getProperty("foo", of: this)
b.doReturn(foo)
}
cls.addStaticGetter(for: "baz") { this in
b.doReturn(b.loadInt(1337))
}
cls.addStaticSetter(for: "baz") { this, v in
}
cls.addPrivateInstanceProperty("ifoo")
cls.addPrivateInstanceProperty("ibar", value: baz)
cls.addPrivateInstanceMethod("im", with: .parameters(n: 0)) { args in
let this = args[0]
let foo = b.getPrivateProperty("ifoo", of: this)
b.setPrivateProperty("ibar", of: this, to: foo)
b.doReturn(foo)
}
cls.addPrivateInstanceMethod("in", with: .parameters(n: 1)) { args in
let this = args[0]
b.callPrivateMethod("im", on: this)
b.updatePrivateProperty("ibar", of: this, with: args[1], using: .Add)
}
cls.addPrivateStaticProperty("sfoo")
cls.addPrivateStaticProperty("sbar", value: baz)
cls.addPrivateStaticMethod("sm", with: .parameters(n: 0)) { args in
let this = args[0]
let foo = b.getPrivateProperty("sfoo", of: this)
b.setPrivateProperty("sbar", of: this, to: foo)
b.doReturn(foo)
}
cls.addPrivateStaticMethod("sn", with: .parameters(n: 1)) { args in
let this = args[0]
b.callPrivateMethod("sm", on: this)
b.updatePrivateProperty("sbar", of: this, with: args[1], using: .Add)
}
}
b.construct(C, withArgs: [b.loadInt(42)])
b.reassign(C, to: b.createNamedVariable(forBuiltin: "Uint8Array"))
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
const v3 = "baz" + 42;
class C4 {
foo;
bar = "baz";
0 = 42;
1;
[-1];
[v3];
[2] = v3;
constructor(a6) {
this.foo = a6;
}
m() {
return this.foo;
}
get baz() {
return 1337;
}
set baz(a12) {
}
static foo;
static {
this.foo = 42;
}
static bar = "baz";
static 0 = 42;
static 1;
static [-1];
static [v3];
static [2] = v3;
static m() {
return this.foo;
}
static get baz() {
return 1337;
}
static set baz(a19) {
}
#ifoo;
#ibar = "baz";
#im() {
const v21 = this.#ifoo;
this.#ibar = v21;
return v21;
}
#in(a23) {
this.#im();
this.#ibar += a23;
}
static #sfoo;
static #sbar = "baz";
static #sm() {
const v26 = this.#sfoo;
this.#sbar = v26;
return v26;
}
static #sn(a28) {
this.#sm();
this.#sbar += a28;
}
}
new C4(42);
C4 = Uint8Array;
"""
XCTAssertEqual(actual, expected)
}
func testArrayLiteralLifting() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
var initialValues = [Variable]()
initialValues.append(b.loadInt(1))
initialValues.append(b.loadInt(2))
initialValues.append(b.loadUndefined())
initialValues.append(b.loadInt(4))
initialValues.append(b.loadUndefined())
initialValues.append(b.loadInt(6))
let v = b.loadString("foobar")
b.reassign(v, to: b.loadUndefined())
initialValues.append(v)
let va = b.createArray(with: initialValues)
b.createArray(with: [b.loadInt(301), b.loadUndefined()])
b.createArray(with: [b.loadUndefined()])
b.createArray(with: [va, b.loadUndefined()], spreading: [true,false])
b.createArray(with: [b.loadUndefined()], spreading: [false])
b.createIntArray(with: [1, 2, 3, 4])
b.createFloatArray(with: [1.1, 2.2, 3.3, 4.4])
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
let v6 = "foobar";
const v8 = [1,2,,4,,6,v6 = undefined];
[301,,];
[,];
[...v8,,];
[,];
[1,2,3,4];
[1.1,2.2,3.3,4.4];
"""
XCTAssertEqual(actual, expected)
}
func testBinaryOperationLifting() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let Math = b.createNamedVariable(forBuiltin: "Math")
let v = b.callMethod("random", on: Math)
let two_v = b.binary(v, v, with: .Add)
let three_v = b.binary(two_v, v, with: .Add)
let twelve_v = b.binary(b.loadInt(4), three_v, with: .Mul)
let six_v = b.binary(twelve_v, b.loadInt(2), with: .Div)
let print = b.createNamedVariable(forBuiltin: "print")
b.callFunction(print, withArgs: [six_v])
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
// TODO: Lifting could be improved to remove some brackets.
let expected = """
const v1 = Math.random();
print((4 * ((v1 + v1) + v1)) / 2);
"""
XCTAssertEqual(actual, expected)
}
func testRegExpInlining() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let v0 = b.loadRegExp("a", RegExpFlags())
b.compare(v0, with: v0, using: .equal);
let v1 = b.loadRegExp("b", RegExpFlags())
b.compare(v0, with: v1, using: .equal);
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
const v0 = /a/;
v0 == v0;
v0 == /b/;
"""
XCTAssertEqual(actual, expected)
}
func testNestedCodeStrings() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let code1 = b.buildCodeString() {
let code2 = b.buildCodeString() {
let code3 = b.buildCodeString() {
let code4 = b.buildCodeString() {
let print = b.createNamedVariable(forBuiltin: "print")
let msg = b.loadString("Hello")
b.callFunction(print, withArgs: [msg])
}
let code5 = b.buildCodeString() {
let print = b.createNamedVariable(forBuiltin: "print")
let msg = b.loadString("World")
b.callFunction(print, withArgs: [msg])
}
let eval = b.createNamedVariable(forBuiltin: "eval")
b.callFunction(eval, withArgs: [code4])
b.callFunction(eval, withArgs: [code5])
}
let eval = b.createNamedVariable(forBuiltin: "eval")
b.callFunction(eval, withArgs: [code3])
}
let eval = b.createNamedVariable(forBuiltin: "eval")
b.callFunction(eval, withArgs: [code2])
}
let eval = b.createNamedVariable(forBuiltin: "eval")
b.callFunction(eval, withArgs: [code1])
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
const v0 = `
const v1 = \\`
const v2 = \\\\\\`
const v3 = \\\\\\\\\\\\\\`
print("Hello");
\\\\\\\\\\\\\\`;
const v7 = \\\\\\\\\\\\\\`
print("World");
\\\\\\\\\\\\\\`;
eval(v3);
eval(v7);
\\\\\\`;
eval(v2);
\\`;
eval(v1);
`;
eval(v0);
"""
XCTAssertEqual(actual, expected)
}
func testFunctionLifting() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let f = b.buildPlainFunction(with: .parameters(n: 1)) { args in
b.doReturn(args[0])
}
b.callFunction(f, withArgs: [b.loadFloat(13.37)])
let f2 = b.buildArrowFunction(with: .parameters(n: 0)) { args in
b.doReturn(b.loadString("foobar"))
}
b.reassign(f, to: f2)
b.callFunction(f)
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
function f0(a1) {
return a1;
}
f0(13.37);
const v4 = () => {
return "foobar";
};
f0 = v4;
f0();
"""
XCTAssertEqual(actual, expected)
}
func testStrictFunctionLifting() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let sf = b.buildPlainFunction(with: .parameters(n: 3)) { args in
b.directive("use strict")
b.buildIfElse(args[0], ifBody: {
let v = b.binary(args[1], args[2], with: .Mul)
b.doReturn(v)
}, elseBody: {
let v = b.binary(args[1], args[2], with: .Add)
b.doReturn(v)
})
}
b.callFunction(sf, withArgs: [b.loadBool(true), b.loadInt(1), b.loadInt(2)])
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """
function f0(a1, a2, a3) {
'use strict';
if (a1) {
return a2 * a3;
} else {
return a2 + a3;
}
}
f0(true, 1, 2);
"""
XCTAssertEqual(actual, expected)
}
func testNamedFunctionLifting() {
let fuzzer = makeMockFuzzer()
let b = fuzzer.makeBuilder()
let foo = b.buildPlainFunction(with: .parameters(n: 0), named: "foo") { args in }
let bar = b.buildGeneratorFunction(with: .parameters(n: 0), named: "bar") { args in }
let baz = b.buildAsyncFunction(with: .parameters(n: 0), named: "baz") { args in }
let bla = b.buildAsyncGeneratorFunction(with: .parameters(n: 0), named: "bla") { args in }
b.callFunction(foo)
b.callFunction(bar)
b.callFunction(baz)
b.callFunction(bla)
let program = b.finalize()
let actual = fuzzer.lifter.lift(program)
let expected = """