-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
Copy pathtest_yjit.rb
5457 lines (4407 loc) · 92 KB
/
test_yjit.rb
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
# To run the tests in this file only, with YJIT enabled:
# make btest BTESTS=bootstraptest/test_yjit.rb RUN_OPTS="--yjit-call-threshold=1"
# This used to trigger a "try to mark T_NONE"
# due to an uninitialized local in foo.
assert_normal_exit %{
def foo(...)
_local_that_should_nil_on_call = GC.start
end
def test_bug21021
puts [], [], [], [], [], []
foo []
end
GC.stress = true
test_bug21021
}
assert_equal 'nil', %{
def foo(...)
_a = _b = _c = binding.local_variable_get(:_c)
_c
end
# [Bug #21021]
def test_local_fill_in_forwardable
puts [], [], [], [], []
foo []
end
test_local_fill_in_forwardable.inspect
}
# regression test for popping before side exit
assert_equal "ok", %q{
def foo(a, *) = a
def call(args, &)
foo(1) # spill at where the block arg will be
foo(*args, &)
end
call([1, 2])
begin
call([])
rescue ArgumentError
:ok
end
}
# regression test for send processing before side exit
assert_equal "ok", %q{
def foo(a, *) = :foo
def call(args)
send(:foo, *args)
end
call([1, 2])
begin
call([])
rescue ArgumentError
:ok
end
}
# test discarding extra yield arguments
assert_equal "22131300500015901015", %q{
def splat_kw(ary) = yield *ary, a: 1
def splat(ary) = yield *ary
def kw = yield 1, 2, a: 3
def kw_only = yield a: 0
def simple = yield 0, 1
def none = yield
def calls
[
splat([1, 1, 2]) { |x, y| x + y },
splat([1, 1, 2]) { |y, opt = raise| opt + y},
splat_kw([0, 1]) { |a:| a },
kw { |a:| a },
kw { |one| one },
kw { |one, a:| a },
kw_only { |a:| a },
kw_only { |a: 1| a },
simple { 5.itself },
simple { |a| a },
simple { |opt = raise| opt },
simple { |*rest| rest },
simple { |opt_kw: 5| opt_kw },
none { |a: 9| a },
# autosplat ineractions
[0, 1, 2].yield_self { |a, b| [a, b] },
[0, 1, 2].yield_self { |a, opt = raise| [a, opt] },
[1].yield_self { |a, opt = 4| a + opt },
]
end
calls.join
}
# test autosplat with empty splat
assert_equal "ok", %q{
def m(pos, splat) = yield pos, *splat
m([:ok], []) {|v0,| v0 }
}
# regression test for send stack shifting
assert_normal_exit %q{
def foo(a, b)
a.singleton_methods(b)
end
def call_foo
[1, 1, 1, 1, 1, 1, send(:foo, 1, 1)]
end
call_foo
}
# regression test for keyword splat with yield
assert_equal 'nil', %q{
def splat_kw(kwargs) = yield(**kwargs)
splat_kw({}) { _1 }.inspect
}
# regression test for arity check with splat
assert_equal '[:ae, :ae]', %q{
def req_one(a_, b_ = 1) = raise
def test(args)
req_one *args
rescue ArgumentError
:ae
end
[test(Array.new 5), test([])]
} unless rjit_enabled? # Not yet working on RJIT
# regression test for arity check with splat and send
assert_equal '[:ae, :ae]', %q{
def two_reqs(a, b_, _ = 1) = a.gsub(a, a)
def test(name, args)
send(name, *args)
rescue ArgumentError
:ae
end
[test(:two_reqs, ["g", nil, nil, nil]), test(:two_reqs, ["g"])]
}
# regression test for GC marking stubs in invalidated code
assert_normal_exit %q{
skip true unless GC.respond_to?(:compact)
garbage = Array.new(10_000) { [] } # create garbage to cause iseq movement
eval(<<~RUBY)
def foo(n, garbage)
if n == 2
# 1.times.each to create a cfunc frame to preserve the JIT frame
# which will return to a stub housed in an invalidated block
return 1.times.each do
Object.define_method(:foo) {}
garbage.clear
GC.verify_compaction_references(toward: :empty, expand_heap: true)
end
end
foo(n + 1, garbage)
end
RUBY
foo(1, garbage)
}
# regression test for callee block handler overlapping with arguments
assert_equal '3', %q{
def foo(_req, *args) = args.last
def call_foo = foo(0, 1, 2, 3, &->{})
call_foo
}
# call leaf builtin with a block argument
assert_equal '0', "0.abs(&nil)"
# regression test for invokeblock iseq guard
assert_equal 'ok', %q{
skip :ok unless GC.respond_to?(:compact)
def foo = yield
10.times do |i|
ret = eval("foo { #{i} }")
raise "failed at #{i}" unless ret == i
GC.compact
end
:ok
} unless rjit_enabled? # Not yet working on RJIT
# regression test for overly generous guard elision
assert_equal '[0, :sum, 0, :sum]', %q{
# In faulty versions, the following happens:
# 1. YJIT puts object on the temp stack with type knowledge
# (CArray or CString) about RBASIC_CLASS(object).
# 2. In iter=0, due to the type knowledge, YJIT generates
# a call to sum() without any guard on RBASIC_CLASS(object).
# 3. In iter=1, a singleton class is added to the object,
# changing RBASIC_CLASS(object), falsifying the type knowledge.
# 4. Because the code from (1) has no class guard, it is incorrectly
# reused and the wrong method is invoked.
# Putting a literal is important for gaining type knowledge.
def carray(iter)
array = []
array.sum(iter.times { def array.sum(_) = :sum })
end
def cstring(iter)
string = "".dup
string.sum(iter.times { def string.sum(_) = :sum })
end
[carray(0), carray(1), cstring(0), cstring(1)]
}
# regression test for return type of Integer#/
# It can return a T_BIGNUM when inputs are T_FIXNUM.
assert_equal 0x3fffffffffffffff.to_s, %q{
def call(fixnum_min)
(fixnum_min / -1) - 1
end
call(-(2**62))
}
# regression test for return type of String#<<
assert_equal 'Sub', %q{
def call(sub) = (sub << sub).itself
class Sub < String; end
call(Sub.new('o')).class
}
# String#dup with FL_EXIVAR
assert_equal '["str", "ivar"]', %q{
def str_dup(str) = str.dup
str = "str"
str.instance_variable_set(:@ivar, "ivar")
str = str_dup(str)
[str, str.instance_variable_get(:@ivar)]
}
# test splat filling required and feeding rest
assert_equal '[0, 1, 2, [3, 4]]', %q{
public def lead_rest(a, b, *rest)
[self, a, b, rest]
end
def call(args) = 0.lead_rest(*args)
call([1, 2, 3, 4])
}
# test missing opts are nil initialized
assert_equal '[[0, 1, nil, 3], [0, 1, nil, 3], [0, 1, nil, 3, []], [0, 1, nil, 3, []]]', %q{
public def lead_opts(a, b=binding.local_variable_get(:c), c=3)
[self, a, b, c]
end
public def opts_rest(a=raise, b=binding.local_variable_get(:c), c=3, *rest)
[self, a, b, c, rest]
end
def call(args)
[
0.lead_opts(1),
0.lead_opts(*args),
0.opts_rest(1),
0.opts_rest(*args),
]
end
call([1])
}
# test filled optionals with unspecified keyword param
assert_equal 'ok', %q{
def opt_rest_opt_kw(_=1, *, k: :ok) = k
def call = opt_rest_opt_kw(0)
call
}
# test splat empty array with rest param
assert_equal '[0, 1, 2, []]', %q{
public def foo(a=1, b=2, *rest)
[self, a, b, rest]
end
def call(args) = 0.foo(*args)
call([])
}
# Regression test for yielding with autosplat to block with
# optional parameters. https://github.com/Shopify/yjit/issues/313
assert_equal '[:a, :b, :a, :b]', %q{
def yielder(arg) = yield(arg) + yield(arg)
yielder([:a, :b]) do |c = :c, d = :d|
[c, d]
end
}
# Regression test for GC mishap while doing shape transition
assert_equal '[:ok]', %q{
# [Bug #19601]
class RegressionTest
def initialize
@a = @b = @fourth_ivar_does_shape_transition = nil
end
def extender
@first_extended_ivar = [:ok]
end
end
GC.stress = true
# Used to crash due to GC run in rb_ensure_iv_list_size()
# not marking the newly allocated [:ok].
RegressionTest.new.extender.itself
} unless rjit_enabled? # Skip on RJIT since this uncovers a crash
assert_equal 'true', %q{
# regression test for tracking type of locals for too long
def local_setting_cmp(five)
victim = 5
five.define_singleton_method(:respond_to?) do |_, _|
victim = nil
end
# +1 makes YJIT track that victim is a number and
# defined? calls respond_to? from above indirectly
unless (victim + 1) && defined?(five.something)
# Would return wrong result if we still think `five` is a number
victim.nil?
end
end
local_setting_cmp(Object.new)
local_setting_cmp(Object.new)
}
assert_equal '18374962167983112447', %q{
# regression test for incorrectly discarding 32 bits of a pointer when it
# comes to default values.
def large_literal_default(n: 0xff00_fabcafe0_00ff)
n
end
def call_graph_root
large_literal_default
end
call_graph_root
call_graph_root
}
assert_normal_exit %q{
# regression test for a leak caught by an assert on --yjit-call-threshold=2
Foo = 1
eval("def foo = [#{(['Foo,']*256).join}]")
foo
foo
Object.send(:remove_const, :Foo)
}
assert_normal_exit %q{
# Test to ensure send on overridden c functions
# doesn't corrupt the stack
class Bar
def bar(x)
x
end
end
class Foo
def bar
Bar.new
end
end
foo = Foo.new
# before this change, this line would error
# because "s" would still be on the stack
# String.to_s is the overridden method here
p foo.bar.bar("s".__send__(:to_s))
}
assert_equal '[nil, nil, nil, nil, nil, nil]', %q{
[NilClass, TrueClass, FalseClass, Integer, Float, Symbol].each do |klass|
klass.class_eval("def foo = @foo")
end
[nil, true, false, 0xFABCAFE, 0.42, :cake].map do |instance|
instance.foo
instance.foo
end
}
assert_equal '[nil, nil, nil, nil, nil, nil]', %q{
# Tests defined? on non-heap objects
[NilClass, TrueClass, FalseClass, Integer, Float, Symbol].each do |klass|
klass.class_eval("def foo = defined?(@foo)")
end
[nil, true, false, 0xFABCAFE, 0.42, :cake].map do |instance|
instance.foo
instance.foo
end
}
assert_equal '[nil, "instance-variable", nil, "instance-variable"]', %q{
# defined? on object that changes shape between calls
class Foo
def foo
defined?(@foo)
end
def add
@foo = 1
end
def remove
self.remove_instance_variable(:@foo)
end
end
obj = Foo.new
[obj.foo, (obj.add; obj.foo), (obj.remove; obj.foo), (obj.add; obj.foo)]
}
assert_equal '["instance-variable", 5]', %q{
# defined? on object too complex for shape information
class Foo
def initialize
100.times { |i| instance_variable_set("@foo#{i}", i) }
end
def foo
[defined?(@foo5), @foo5]
end
end
Foo.new.foo
}
# getinstancevariable with shape too complex
assert_normal_exit %q{
class Foo
def initialize
@a = 1
end
def getter
@foobar
end
end
# Initialize ivars in changing order, making the Foo
# class have shape too complex
100.times do |x|
foo = Foo.new
foo.instance_variable_set(:"@a#{x}", 1)
foo.instance_variable_set(:"@foobar", 777)
# The getter method eventually sees shape too complex
r = foo.getter
if r != 777
raise "error"
end
end
}
assert_equal '0', %q{
# This is a regression test for incomplete invalidation from
# opt_setinlinecache. This test might be brittle, so
# feel free to remove it in the future if it's too annoying.
# This test assumes --yjit-call-threshold=2.
module M
Foo = 1
def foo
Foo
end
def pin_self_type_then_foo
_ = @foo
foo
end
def only_ints
1 + self
foo
end
end
class Integer
include M
end
class Sub
include M
end
foo_method = M.instance_method(:foo)
dbg = ->(message) do
return # comment this out to get printouts
$stderr.puts RubyVM::YJIT.disasm(foo_method)
$stderr.puts message
end
2.times { 42.only_ints }
dbg["There should be two versions of getinlineache"]
module M
remove_const(:Foo)
end
dbg["There should be no getinlinecaches"]
2.times do
42.only_ints
rescue NameError => err
_ = "caught name error #{err}"
end
dbg["There should be one version of getinlineache"]
2.times do
Sub.new.pin_self_type_then_foo
rescue NameError
_ = 'second specialization'
end
dbg["There should be two versions of getinlineache"]
module M
Foo = 1
end
dbg["There should still be two versions of getinlineache"]
42.only_ints
dbg["There should be no getinlinecaches"]
# Find name of the first VM instruction in M#foo.
insns = RubyVM::InstructionSequence.of(foo_method).to_a
if defined?(RubyVM::YJIT.blocks_for) && (insns.last.find { Array === _1 }&.first == :opt_getinlinecache)
RubyVM::YJIT.blocks_for(RubyVM::InstructionSequence.of(foo_method))
.filter { _1.iseq_start_index == 0 }.count
else
0 # skip the test
end
}
# Check that frozen objects are respected
assert_equal 'great', %q{
class Foo
attr_accessor :bar
def initialize
@bar = 1
freeze
end
end
foo = Foo.new
5.times do
begin
foo.bar = 2
rescue FrozenError
end
end
foo.bar == 1 ? "great" : "NG"
}
# Check that global variable set works
assert_equal 'string', %q{
def foo
$foo = "string"
end
foo
}
# Check that exceptions work when setting global variables
assert_equal 'rescued', %q{
def set_var
$var = 100
rescue
:rescued
end
set_var
trace_var(:$var) { raise }
set_var
}
# Check that global variables work
assert_equal 'string', %q{
$foo = "string"
def foo
$foo
end
foo
}
# Check that exceptions work when getting global variable
assert_equal 'rescued', %q{
Warning[:deprecated] = true
module Warning
def warn(message)
raise
end
end
def get_var
$=
rescue
:rescued
end
$VERBOSE = true
get_var
get_var
}
# Check that global tracepoints work
assert_equal 'true', %q{
def foo
1
end
foo
foo
foo
called = false
tp = TracePoint.new(:return) { |event|
if event.method_id == :foo
called = true
end
}
tp.enable
foo
tp.disable
called
}
# Check that local tracepoints work
assert_equal 'true', %q{
def foo
1
end
foo
foo
foo
called = false
tp = TracePoint.new(:return) { |_| called = true }
tp.enable(target: method(:foo))
foo
tp.disable
called
}
# Make sure that optional param methods return the correct value
assert_equal '1', %q{
def m(ary = [])
yield(ary)
end
# Warm the JIT with a 0 param call
2.times { m { } }
m(1) { |v| v }
}
# Test for topn
assert_equal 'array', %q{
def threequals(a)
case a
when Array
"array"
when Hash
"hash"
else
"unknown"
end
end
threequals([])
threequals([])
threequals([])
}
# Test for opt_mod
assert_equal '2', %q{
def mod(a, b)
a % b
end
mod(7, 5)
mod(7, 5)
}
# Test for opt_mult
assert_equal '12', %q{
def mult(a, b)
a * b
end
mult(6, 2)
mult(6, 2)
}
# Test for opt_div
assert_equal '3', %q{
def div(a, b)
a / b
end
div(6, 2)
div(6, 2)
}
# BOP redefined methods work when JIT compiled
assert_equal 'false', %q{
def less_than x
x < 10
end
class Integer
def < x
false
end
end
less_than 2
less_than 2
less_than 2
}
# BOP redefinition works on Integer#<
assert_equal 'false', %q{
def less_than x
x < 10
end
less_than 2
less_than 2
class Integer
def < x
false
end
end
less_than 2
}
# BOP redefinition works on Integer#<=
assert_equal 'false', %q{
def le(x, y) = x <= y
le(2, 2)
class Integer
def <=(_) = false
end
le(2, 2)
}
# BOP redefinition works on Integer#>
assert_equal 'false', %q{
def gt(x, y) = x > y
gt(3, 2)
class Integer
def >(_) = false
end
gt(3, 2)
}
# BOP redefinition works on Integer#>=
assert_equal 'false', %q{
def ge(x, y) = x >= y
ge(2, 2)
class Integer
def >=(_) = false
end
ge(2, 2)
}
# Putobject, less-than operator, fixnums
assert_equal '2', %q{
def check_index(index)
if 0x40000000 < index
raise "wat? #{index}"
end
index
end
check_index 2
check_index 2
}
# foo leaves a temp on the stack before the call
assert_equal '6', %q{
def bar
return 5
end
def foo
return 1 + bar
end
foo()
retval = foo()
}
# Method with one arguments
# foo leaves a temp on the stack before the call
assert_equal '7', %q{
def bar(a)
return a + 1
end
def foo
return 1 + bar(5)
end
foo()
retval = foo()
}
# Method with two arguments
# foo leaves a temp on the stack before the call
assert_equal '0', %q{
def bar(a, b)
return a - b
end
def foo
return 1 + bar(1, 2)
end
foo()
retval = foo()
}
# Passing argument types to callees
assert_equal '8.5', %q{
def foo(x, y)
x + y
end
def bar
foo(7, 1.5)
end
bar
bar
}
# Recursive Ruby-to-Ruby calls
assert_equal '21', %q{
def fib(n)
if n < 2
return n
end
return fib(n-1) + fib(n-2)
end
r = fib(8)
}
# Ruby-to-Ruby call and C call
assert_normal_exit %q{
def bar
puts('hi!')
end
def foo
bar
end
foo()
foo()
}
# Method aliasing
assert_equal '42', %q{
class Foo
def method_a
42
end
alias method_b method_a
def method_a
:somethingelse
end
end
@obj = Foo.new
def test
@obj.method_b
end
test
test
}
# Method aliasing with method from parent class
assert_equal '777', %q{
class A
def method_a
777
end
end
class B < A
alias method_b method_a
end
@obj = B.new
def test
@obj.method_b
end
test
test
}
# The hash method is a C function and uses the self argument
assert_equal 'true', %q{
def lehashself
hash
end
a = lehashself
b = lehashself
a == b
}
# Method redefinition (code invalidation) test
assert_equal '1', %q{
def ret1
return 1
end
klass = Class.new do
def alias_then_hash(klass, method_to_redefine)