Repository navigation
Expand file tree
/
Copy pathexec_test.go
More file actions
1091 lines (1049 loc) · 39 KB
/
Copy pathexec_test.go
File metadata and controls
1091 lines (1049 loc) · 39 KB
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
package glua
import (
"context"
"errors"
"fmt"
"math"
"reflect"
"runtime"
"slices"
"strings"
"sync"
"testing"
)
func TestFloatLoop(t *testing.T) {
res, err := runProto(t, progFloatLoop(1000))
if err != nil {
t.Fatal(err)
}
want := 0.0
for i := 1; i <= 1000; i++ {
want += float64(i) * 1.5
}
if f, ok := res[0].Float(); !ok || res[0].Kind() != KindFloat || f != want {
t.Fatalf("got %v, want %v", res[0], want)
}
}
func TestFib(t *testing.T) {
res, err := runProto(t, progFib(20))
if err != nil {
t.Fatal(err)
}
if i, _ := res[0].Int(); i != 6765 {
t.Fatalf("fib(20) = %v", res[0])
}
}
func TestFieldRead(t *testing.T) {
res, err := runProto(t, progFieldRead(5000))
if err != nil {
t.Fatal(err)
}
if i, _ := res[0].Int(); i != 5000 {
t.Fatalf("got %v", res[0])
}
}
// The expected counts and last values come from C Lua 5.4.8.
func TestForLoopCorners(t *testing.T) {
nan := Float(math.NaN())
tests := []struct {
init, limit, step Value
n int64
last Value
}{
{Int(1), Int(10), Int(1), 10, Int(10)},
{Int(1), Int(10), Int(3), 4, Int(10)},
{Int(10), Int(1), Int(-1), 10, Int(1)},
{Int(10), Int(1), Int(-3), 4, Int(1)},
{Int(1), Int(0), Int(1), 0, Value{}},
{Int(0), Int(1), Int(-1), 0, Value{}},
{Int(9223372036854775806), Int(9223372036854775807), Int(1), 2, Int(9223372036854775807)},
{Int(-9223372036854775808), Int(-9223372036854775806), Int(1), 3, Int(-9223372036854775806)},
{Int(-9223372036854775806), Int(-9223372036854775808), Int(-1), 3, Int(-9223372036854775808)},
{Int(9223372036854775807), Int(9223372036854775807), Int(-9223372036854775808), 1, Int(9223372036854775807)},
{Int(0), Int(-9223372036854775808), Int(-9223372036854775808), 2, Int(-9223372036854775808)},
{Int(-9223372036854775808), Int(9223372036854775807), Int(9223372036854775807), 3, Int(9223372036854775806)},
{Int(9223372036854775807), Int(-9223372036854775808), Int(-9223372036854775808), 2, Int(-1)},
{Int(1), Float(3.7), Int(1), 3, Int(3)},
{Int(1), Float(-3.7), Int(-1), 5, Int(-3)},
{Int(1), Float(1e+100), Int(9223372036854775807), 1, Int(1)},
{Float(1), Int(3), Float(0.5), 5, Float(3)},
{Int(3), Float(1), Float(-0.75), 3, Float(1.5)},
{Float(0.1), Float(0.35), Float(0.1), 3, Float(0.30000000000000004)},
{Int(1), nan, Int(1), 0, Value{}},
{Float(1), nan, Int(1), 1, Float(1)},
}
for _, tt := range tests {
res, err := runProto(t, progForCount(tt.init, tt.limit, tt.step))
if err != nil {
t.Errorf("for %v, %v, %v: %v", tt.init, tt.limit, tt.step, err)
continue
}
if n, _ := res[0].Int(); n != tt.n || !same(&res[1], &tt.last) {
t.Errorf("for %v, %v, %v: %v iterations, last %v (%v); want %d, %v (%v)",
tt.init, tt.limit, tt.step, res[0], res[1], res[1].Kind(), tt.n, tt.last, tt.last.Kind())
}
}
if _, err := runProto(t, progForCount(Int(1), Int(2), Int(0))); err == nil || !strings.Contains(err.Error(), "'for' step is zero") {
t.Errorf("zero step: %v", err)
}
}
// The step limit is exact: a loop with n back-edges runs under Steps = n and fails under n-1.
func TestStepLimitExact(t *testing.T) {
for _, n := range []int64{1, 100, 1023, 1024, 1025, 5000} {
p := progEmptyLoop(n + 1) // n+1 iterations take n back-edges
for _, limit := range []int64{n, n - 1} {
ex := newExecutor()
ex.maxSteps = limit
_, err := runClosure(context.Background(), ex, closureOf(p))
if ok := limit >= n; ok != (err == nil) {
t.Errorf("%d back-edges under Steps=%d: %v", n, limit, err)
}
if err != nil && !errors.Is(err, ErrSteps) {
t.Errorf("want ErrSteps, got %v", err)
}
}
}
}
// Nested entries share the budget of the outermost one.
func TestNestedEntriesShareBudget(t *testing.T) {
inner := closureOf(progEmptyLoop(151)) // 150 back-edges
host := &goFunc{f: func(c *Call) error {
v, err := c.Invoke(luaFnValue(inner))
if err != nil {
return err
}
return c.ReturnValues(v)
}}
outer := closureOf(progCallHost(), goFnValue(host))
for _, tt := range []struct {
limit int64
ok bool
}{{150, true}, {149, false}} {
ex := newExecutor()
ex.maxSteps = tt.limit
_, err := runClosure(context.Background(), ex, outer, Int(0))
if (err == nil) != tt.ok || (err != nil && !errors.Is(err, ErrSteps)) {
t.Errorf("Steps=%d: %v", tt.limit, err)
}
}
}
func TestCancelledContextNeverRuns(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
ran := false
host := &goFunc{f: func(c *Call) error { ran = true; return nil }}
_, err := runClosure(ctx, newExecutor(), closureOf(progCallHost(), goFnValue(host)), Int(1))
if !errors.Is(err, context.Canceled) || ran {
t.Fatalf("err %v, ran %v", err, ran)
}
}
func TestCancelDuringLoop(t *testing.T) {
ctx, cancel := context.WithCancelCause(context.Background())
stop := errors.New("stop")
ticks := 0
host := &goFunc{f: func(c *Call) error {
ticks++
cancel(stop)
return nil
}}
// call the host function, which cancels, then spin: the next tick expiry must notice
a := newAsm("cancel", 0, 6)
a.upval(false, 0)
a.abc(opGetUpval, 0, 0, 0)
a.abc(opCall, 0, 1, 1)
a.loadI(1, 1)
a.abx(opLoadK, 2, a.konst(Int(1<<40)))
a.loadI(3, 1)
a.forPrep(1, "exit")
a.label("body")
a.forLoop(1, "body")
a.label("exit")
a.abc(opReturn0, 0, 0, 0)
ex := newExecutor()
ex.maxSteps = math.MaxInt64
_, err := runClosure(ctx, ex, closureOf(a.done(), goFnValue(host)))
if !errors.Is(err, stop) || ticks != 1 {
t.Fatalf("err %v", err)
}
}
// A host function calls back into Lua, which calls another host function. The outer one
// must still see its own arguments afterwards.
func TestNestedHostCalls(t *testing.T) {
innerHost := &goFunc{f: func(c *Call) error {
x, _ := c.Value(0).Int()
return c.ReturnValues(Int(x * 10))
}}
lua := closureOf(progCallHost(), goFnValue(innerHost))
outerHost := &goFunc{f: func(c *Call) error {
before := c.Value(0)
v, err := c.Invoke(luaFnValue(lua), Int(7))
if err != nil {
return err
}
if !c.Value(0).Equal(before) {
return errors.New("outer arguments were overwritten")
}
x, _ := before.Int()
y, _ := v.Int()
return c.ReturnValues(Int(x + y))
}}
res, err := runClosure(context.Background(), newExecutor(), closureOf(progCallHost(), goFnValue(outerHost)), Int(5))
if err != nil {
t.Fatal(err)
}
if i, _ := res[0].Int(); i != 75 {
t.Fatalf("got %v, want 75", res[0])
}
}
func TestHostErrorKeepsGoError(t *testing.T) {
sentinel := errors.New("not found")
host := &goFunc{f: func(c *Call) error { return sentinel }}
_, err := runClosure(context.Background(), newExecutor(), closureOf(progCallHost(), goFnValue(host)), Int(1))
var le *Error
if !errors.Is(err, sentinel) || !errors.As(err, &le) || le.Value.String() != "not found" {
t.Fatalf("got %v", err)
}
}
// Errorf and ArgError write their messages as luaL_error and luaL_argerror do: the position
// of the Lua code that called the host function, none for a call from Go, before the whole
// message when the error is wrapped, and the function as the call names it. The Go error
// stays behind the message, also when error(e, 0) raises it again, and the Go side shows the
// position once, Terminate's included.
func TestHostErrorsWithPosition(t *testing.T) {
missing := errors.New("not found")
lookup := func(c *Call) error { return c.Errorf("user %d: %w", c.Arg[int](0), missing) }
rt := MustNew(
Global("lookup", GoFunc(lookup)),
Global("wrapped", GoFunc(func(c *Call) error { return fmt.Errorf("lookup: %w", lookup(c)) })),
Global("deny", GoFunc(func(c *Call) error { return Terminate(lookup(c)) })),
Global("check", GoFunc(func(c *Call) error { return c.ArgError(c.Len()-1, "positive expected") })),
Global("invoke", GoFunc(func(c *Call) error {
if _, err := c.Invoke(c.Value(0)); err != nil {
return c.Errorf("in callback: %w", err)
}
return nil
})))
s, err := rt.Load(context.Background(), "t.lua", `
local obj = { check = check }
function f()
return select(2, pcall(function() lookup(7) end)), select(2, pcall(lookup, 7)),
select(2, pcall(function() wrapped(7) end)),
select(2, pcall(function() check(1) end)), select(2, pcall(function() obj:check(1) end)),
select(2, pcall(check, 1)), select(2, pcall(function() obj:check() end)),
select(2, pcall(function() invoke(function() error("inner") end) end))
end
function g() local _, e = pcall(function() lookup(7) end) error(e, 0) end
function h() lookup(7) end
function d() pcall(deny, 7) end`)
if err != nil {
t.Fatal(err)
}
res, err := s.CallN(context.Background(), nil, s.Globals().Get(String("f")))
want := []string{"t.lua:4: user 7: not found", "user 7: not found", "t.lua:5: lookup: user 7: not found",
"t.lua:6: bad argument #1 to 'check' (positive expected)", "t.lua:6: bad argument #1 to 'check' (positive expected)",
"bad argument #1 to 'check' (positive expected)", "t.lua:7: calling 'check' on bad self (positive expected)",
"t.lua:8: in callback: t.lua:8: inner"}
if err != nil || len(res) != len(want) {
t.Fatalf("got %v, %v", res, err)
}
for i, w := range want {
if res[i].String() != w {
t.Errorf("message %d = %q, want %q", i+1, res[i], w)
}
}
for name, want := range map[string]string{"g": "t.lua:10: user 7: not found", "h": "t.lua:11: user 7: not found",
"d": "t.lua:12: user 7: not found"} {
if _, err := s.Call(context.Background(), s.Globals().Get(String(name))); !errors.Is(err, missing) || err.Error() != want {
t.Errorf("%s: got %v, want %q", name, err, want)
}
}
// a terminal error stays terminal, wrapped
limited := MustNew(Limits{Steps: 10_000}, Global("invoke", GoFunc(func(c *Call) error {
_, err := c.Invoke(c.Value(0))
return c.Errorf("in callback: %w", err)
})))
ls, err := limited.Load(context.Background(), "l.lua", "function f() pcall(invoke, function() while true do end end) end")
if err != nil {
t.Fatal(err)
}
if _, err := ls.Call(context.Background(), ls.Globals().Get(String("f"))); !errors.Is(err, ErrSteps) {
t.Errorf("a wrapped step limit: %v", err)
}
}
func TestHostPanicPoisonsExecutor(t *testing.T) {
host := &goFunc{f: func(c *Call) error { panic("boom") }}
ex := newExecutor()
func() {
defer func() {
if r := recover(); r != "boom" {
t.Fatalf("recovered %v", r)
}
}()
runClosure(context.Background(), ex, closureOf(progCallHost(), goFnValue(host)), Int(1))
}()
if !ex.poisoned || ex.running {
t.Fatal("executor not poisoned after a panic")
}
var pool sync.Pool
release(&pool, ex)
if pool.Get() != nil {
t.Fatal("a poisoned executor was pooled")
}
}
func TestConcurrentEntryPanics(t *testing.T) {
ex := newExecutor()
host := &goFunc{f: func(c *Call) error {
defer func() {
if r := recover(); r == nil || !strings.Contains(r.(string), "concurrent use") {
t.Errorf("recovered %v", r)
}
}()
ex.call(context.Background(), closureOf(progAdd1()), 10, 0)
return nil
}}
runClosure(context.Background(), ex, closureOf(progCallHost(), goFnValue(host)), Int(1))
}
// An error three frames deep leaves pointers in registers. Reset must clear all of them.
func TestResetAfterDeepError(t *testing.T) {
// f3 calls nil; f2 calls f3; f1 calls f2
f3 := newAsm("f3", 1, 3)
f3.abc(opLoadNil, 1, 0, 0)
f3.abc(opCall, 1, 1, 1)
f3.abc(opReturn0, 0, 0, 0)
p3 := f3.done()
mk := func(name string, next *proto) *proto {
a := newAsm(name, 1, 3)
a.abx(opClosure, 1, a.proto(next))
a.abc(opMove, 2, 0, 0)
a.abc(opCall, 1, 2, 1)
a.abc(opReturn0, 0, 0, 0)
return a.done()
}
p1 := mk("f1", mk("f2", p3))
ex := newExecutor()
big := String(strings.Repeat("x", 100))
copy(ex.stack[1:], []Value{big})
err := ex.call(context.Background(), closureOf(p1), 0, 1)
if err == nil || !strings.Contains(err.Error(), "attempt to call a nil value") {
t.Fatalf("got %v", err)
}
if ex.st.hi < 5 {
t.Fatalf("high-water mark %d does not cover the frames", ex.st.hi)
}
ex.reset()
for i, v := range ex.stack {
if v.p != nil {
t.Fatalf("register %d still holds %v after reset", i, v)
}
}
}
type request struct {
Method string
Size int64
Admin bool
Path string
}
func TestRule(t *testing.T) {
e := newEngine()
rule := newRule[request, bool](e, closureOf(progRule()))
ctx := context.Background()
for _, tt := range []struct {
r request
want bool
}{
{request{Method: "GET", Size: 10, Admin: true}, true},
{request{Method: "GET", Size: 10, Admin: false}, false},
{request{Method: "POST", Size: 10, Admin: true}, false},
{request{Method: "GET", Size: 5000, Admin: true}, false},
{request{Method: strings.Clone("GET"), Size: 1, Admin: true}, true}, // not the constant's pointer
} {
got, err := rule.Eval(ctx, &tt.r)
if err != nil || got != tt.want {
t.Errorf("%+v: got %v, %v", tt.r, got, err)
}
}
}
// Eval allocates nothing when the struct is built per call, and the slot is empty afterwards.
func TestRuleEvalNoAlloc(t *testing.T) {
if raceEnabled {
t.Skip("sync.Pool drops items in race builds")
}
e := newEngine()
rule := newRule[request, bool](e, closureOf(progRule()))
ctx := context.Background()
method := strings.Clone("GET")
allocs := testing.AllocsPerRun(1000, func() {
r := request{Method: method, Size: 10, Admin: true}
if ok, err := rule.Eval(ctx, &r); !ok || err != nil {
t.Fatal(ok, err)
}
})
if allocs != 0 && !stackStress { // lua_stackstress allocates a stack at every check
t.Fatalf("Eval allocates %v times per call", allocs)
}
ex := e.pool.Get().(*executor)
if slot := (*request)(ex.slots[rule.id*maxSlotArgs]); *slot != (request{}) {
t.Fatalf("slot still holds %+v", *slot)
}
}
func TestTypedFuncs(t *testing.T) {
e := newEngine()
ctx := context.Background()
add1 := bindFunc1[int64, int64](e, closureOf(progAdd1()))
if r, err := add1(ctx, 41); r != 42 || err != nil {
t.Fatal(r, err)
}
id := bindFunc1[string, string](e, closureOf(progIdentity()))
s := strings.Repeat("s", 46)
if r, err := id(ctx, s); r != s || err != nil {
t.Fatal(r, err)
}
byPtr := bindFunc1[*request, bool](e, closureOf(progRule()))
req := &request{Method: "GET", Size: 1, Admin: true}
if r, err := byPtr(ctx, req); !r || err != nil {
t.Fatal(r, err)
}
if r, err := byPtr(ctx, nil); err == nil || !strings.Contains(err.Error(), "attempt to index a nil value") {
t.Fatal(r, err)
}
byVal := bindFunc1[request, bool](e, closureOf(progRule()))
if r, err := byVal(ctx, *req); !r || err != nil {
t.Fatal(r, err)
}
if raceEnabled || stackStress {
return // sync.Pool drops items in race builds; lua_stackstress allocates stacks
}
for name, f := range map[string]func(){
"add1": func() { add1(ctx, 1) },
"identity": func() { id(ctx, s) },
"pointer": func() { byPtr(ctx, req) },
"value": func() { byVal(ctx, request{Method: "GET", Size: 1, Admin: true}) },
} {
if a := testing.AllocsPerRun(1000, f); a != 0 {
t.Errorf("%s allocates %v times per call", name, a)
}
}
}
// An integer can carry the bits of a userdata or a table. GETFIELD must treat it like
// any other non-table.
func TestGetFieldRejectsForgedValues(t *testing.T) {
id := bindType(reflect.TypeFor[request]())
a := newAsm("forged", 1, 2)
a.field(opGetField, 1, 0, int(a.kstr("method")))
a.abc(opReturn1, 1, 0, 0)
p := a.done()
for _, v := range []Value{
Int(int64(kindUser<<56 | id)),
Int(int64(kindTable << 56)),
Float(math.Float64frombits(kindUser<<56 | id)),
{},
} {
p.hints[0] = 0 // a trained hint would make no difference, but start clean
_, err := runProto(t, p, v)
if err == nil || !strings.Contains(err.Error(), "attempt to index a") {
t.Errorf("%v: got %v", v, err)
}
}
}
// One site, two table shapes: the hint gives up after four misses and the inline probe
// takes over. Results stay correct throughout, also from two goroutines at once.
func TestMegamorphicSite(t *testing.T) {
a := newAsm("getx", 1, 2)
a.field(opGetField, 1, 0, int(a.kstr("x")))
a.abc(opReturn1, 1, 0, 0)
p := a.done()
// four shapes with x in four different slots, so that the site misses at every change
shapes := make([]*Table, 0, 4)
used := map[int]bool{}
for n := 0; len(shapes) < 4; n++ {
t := NewTable(0, 8)
for j := range n % 7 {
t.Set(String(strings.Repeat("k", j+1+n/7)), Int(int64(j)))
}
t.Set(String("x"), Int(int64(100+len(shapes))))
if _, slot := t.getSlot(String("x")); !used[slot] {
used[slot] = true
shapes = append(shapes, t)
}
}
var wg sync.WaitGroup
for g := range 2 {
wg.Go(func() {
ex := newExecutor()
for n := range 200 {
i := (n + g) % len(shapes)
res, err := runClosure(context.Background(), ex, closureOf(p), tableValue(shapes[i]))
if err != nil {
t.Error(err)
return
}
if v, _ := res[0].Int(); v != int64(100+i) {
t.Errorf("shape %d: got %v", i, res[0])
return
}
}
})
}
wg.Wait()
if p.hints[0] != megamorphic {
t.Errorf("hint is %x, want megamorphic", p.hints[0])
}
}
func TestStringKeyBehindOtherPointer(t *testing.T) {
tbl := NewTable(0, 4)
tbl.Set(String(strings.Clone("x")), Int(9)) // not the canonical constant
a := newAsm("getx", 1, 2)
a.field(opGetField, 1, 0, int(a.kstr("x")))
a.abc(opReturn1, 1, 0, 0)
res, err := runProto(t, a.done(), tableValue(tbl))
if err != nil {
t.Fatal(err)
}
if v, _ := res[0].Int(); v != 9 {
t.Fatalf("got %v", res[0])
}
}
func TestCallDepthLimit(t *testing.T) {
// local function f() return f() + 1 end
a := newAsm("rec", 0, 2)
a.upval(true, 0)
a.abc(opGetUpval, 0, 0, 0)
a.abc(opCall, 0, 1, 2)
a.abc(opAddK, 0, 0, int(a.kint(1)))
a.abc(opReturn1, 0, 0, 0)
m := newAsm("main", 0, 2)
m.abx(opClosure, 0, m.proto(a.done()))
m.abc(opMove, 1, 0, 0)
m.abc(opCall, 1, 1, 2)
m.abc(opReturn1, 1, 0, 0)
_, err := runProto(t, m.done())
if err == nil || !strings.Contains(err.Error(), "stack overflow") {
t.Fatalf("got %v", err)
}
}
func TestStackGrowth(t *testing.T) {
// deep but legal recursion moves the stack many times
res, err := runProto(t, progFib(18))
if err != nil {
t.Fatal(err)
}
if i, _ := res[0].Int(); i != 2584 {
t.Fatal(res[0])
}
ex := newExecutor()
ex.maxCallDepth = 100000
// f(n) = n == 0 and 0 or f(n-1)
a := newAsm("down", 1, 3)
a.upval(true, 0)
a.cjump(opLtKJmp, 0, int(a.kint(1)), "rec")
a.abc(opReturn1, 0, 0, 0)
a.label("rec")
a.abc(opGetUpval, 1, 0, 0)
a.abc(opSubK, 2, 0, int(a.kint(1)))
a.abc(opCall, 1, 2, 2)
a.abc(opReturn1, 1, 0, 0)
m := newAsm("main", 0, 3)
m.abx(opClosure, 0, m.proto(a.done()))
m.abc(opMove, 1, 0, 0)
m.abx(opLoadK, 2, m.konst(Int(5000)))
m.abc(opCall, 1, 2, 2)
m.abc(opReturn1, 1, 0, 0)
res, err = runClosure(context.Background(), ex, closureOf(m.done()))
if err != nil {
t.Fatal(err)
}
if i, _ := res[0].Int(); i != 0 || len(ex.stack) <= initStack {
t.Fatalf("got %v, stack %d", res[0], len(ex.stack))
}
}
// A string that doubles on every iteration runs out of the entry's allocation budget long
// before the step limit, and fails with ErrAlloc instead of exhausting memory.
func TestSingleAllocationCap(t *testing.T) {
_, err := runSource("t", `local s = "x" for i = 1, 100 do s = s .. s end return #s`)
if !errors.Is(err, ErrAlloc) {
t.Fatalf("got %v, want ErrAlloc", err)
}
}
// After a terminal error the closers still run, on a grace budget: a closer that loops
// forever is stopped, and the terminal error stands.
func TestCloseAfterTerminalError(t *testing.T) {
src := `
done = false
local a <close> = setmetatable({}, {__close = function() done = true end})
local b <close> = setmetatable({}, {__close = function() while true do end end})
while true do end`
p, _, err := compileChunk("t", src)
if err != nil {
t.Fatal(err)
}
g := testGlobals()
ex := newExecutor()
ex.maxSteps = 10_000
_, err = runClosure(context.Background(), ex, &closure{p: p, env: g})
if !errors.Is(err, ErrSteps) {
t.Fatalf("got %v, want ErrSteps", err)
}
// b's closer used up the grace budget, so a's closer is abandoned
if g.Get(String("done")).Truthy() {
t.Error("a's closer ran after b's breached the grace budget")
}
src2 := `
done = false
local a <close> = setmetatable({}, {__close = function() done = true end})
while true do end`
p, _, _ = compileChunk("t", src2)
g = testGlobals()
ex = newExecutor()
ex.maxSteps = 10_000
runClosure(context.Background(), ex, &closure{p: p, env: g})
if !g.Get(String("done")).Truthy() {
t.Error("a closer did not run after a terminal error")
}
}
// A yield cannot cross a Go function: Lua code called back from Go gets C Lua's error.
func TestYieldAcrossGoCall(t *testing.T) {
p, _, err := compileChunk("t", `
local co = coroutine.create(function()
return each(function() coroutine.yield(1) end)
end)
return coroutine.resume(co)`)
if err != nil {
t.Fatal(err)
}
g := testGlobals()
g.Set(String("each"), goFnValue(&goFunc{name: "each", f: func(c *Call) error {
_, err := c.Invoke(c.Value(0))
return err
}}))
res, err := runClosure(context.Background(), newExecutor(), &closure{p: p, env: g})
if err != nil {
t.Fatal(err)
}
if res[0].Truthy() || !strings.Contains(res[1].String(), "attempt to yield across a C-call boundary") {
t.Fatalf("got %v %v", res[0], res[1])
}
}
// A coroutine that returned leaves its stack to the next one the entry makes, and a new
// coroutine's stack is already big enough for its body's frame, so a loop of short
// coroutines allocates no stacks after the first.
func TestCoroutineStacksReused(t *testing.T) {
if stackStress {
t.Skip("lua_stackstress allocates a stack at every check")
}
s := mustLoad(t, MustNew(LibSafe|LibCoroutine), `local f = function() return 1 end
function work(n) for i = 1, n do coroutine.wrap(f)() end end`)
work, _ := s.Func1[int64, struct{}]("work")
ctx := context.Background()
if _, err := work(ctx, 10); err != nil {
t.Fatal(err)
}
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
if _, err := work(ctx, 1000); err != nil {
t.Fatal(err)
}
runtime.ReadMemStats(&after)
if per := (after.TotalAlloc - before.TotalAlloc) / 1000; per > 2<<10 {
t.Errorf("%d bytes allocated for each coroutine", per)
}
}
// A kept stack holds nothing of the coroutine that left it, and reset lets it go.
func TestCoroutineStacksKeptClean(t *testing.T) {
var kept int
check := GoFunc(func(c *Call) error {
for _, k := range c.ex.coStacks {
if len(k.stack) != coStackSize || slices.ContainsFunc(k.stack, func(v Value) bool { return !v.IsNil() }) {
return c.Errorf("a kept stack of %d slots holds values", len(k.stack))
}
}
kept = len(c.ex.coStacks)
return nil
})
rt := MustNew(LibSafe|LibCoroutine, Global("check", check))
_, err := runInEnv(t, rt, `
local function wide(...) local a, b, c, d, e, f = {}, "x", 1, {}, "y", 2 return select("#", ...) end
local function deep(n) if n == 0 then return 0 end return 1 + deep(n - 1) end
coroutine.wrap(deep)(2000)
coroutine.wrap(wide)(1, 2, 3)
check()`)
if err != nil {
t.Fatal(err)
}
if kept != 1 && !stackStress {
t.Errorf("%d stacks kept, want the one that did not grow", kept)
}
ex := rt.newExecutor()
ex.coStacks = append(ex.coStacks, coStack{make([]Value, coStackSize), nil})
ex.reset()
if len(ex.coStacks) != 0 || ex.coStacks[:1][0].stack != nil {
t.Error("reset kept the stacks")
}
}
// A Go function in tail position returns its results from the caller's frame at once, or,
// for pcall, pairs with __pairs and a yield, through the RETURN after the TAILCALL.
func TestTailCallGo(t *testing.T) {
for _, tc := range []struct{ src, want string }{
{"local function f() return select('#') end return f()", "0"},
{"local function f(...) return select(2, ...) end return f(1, 2, 3)", "2\t3"},
{"local function f() return string.byte('abc', 1, 3) end local a, b = f() return a, b", "97\t98"},
{"local function f() return string.byte('abc', 1, 3) end local t = {f(), f()} return #t", "4"},
{"local function f() return tostring(nil) end return (f()) .. '!'", "nil!"},
{"local function f(x) return setmetatable(x, {__index = {k = 1}}) end return f({}).k", "1"},
{"local function f() return pcall(error, 'e') end return f()", "false\te"},
{"local function f() return pcall(function(...) return ... end, 1, 2) end return f()", "true\t1\t2"},
{"local function f(t) return pairs(t) end local n = 0 for _, v in f({3, 4}) do n = n + v end return n", "7"},
{"local function f(t) return pairs(t) end local p = setmetatable({}, {__pairs = function() return next, {7}, nil end})\n" +
"local n = 0 for _, v in f(p) do n = n + v end return n", "7"},
{"local function f(x) return coroutine.yield(x) end\n" +
"local co = coroutine.wrap(function(x) local a, b = f(x) return a + b end)\n" +
"local y = co(5) return y, co(y, 1)", "5\t6"},
{"local function f() return error('boom') end local ok, e = pcall(f) return ok, e", "false\ttest:1: boom"}, // the position of f, as C Lua gives
{"return math.max(3, 4)", "4"},
} {
for _, checked := range []bool{false, true} {
res, err := runInEnv(t, MustNew(LibSafe|LibCoroutine, withLoop(checked)), tc.src)
if err != nil {
t.Fatalf("%s: %v", tc.src, err)
}
if got := joinValues(res); got != tc.want {
t.Errorf("%s (checked %v): got %q, want %q", tc.src, checked, got, tc.want)
}
}
}
}
// The values a yield passes stay on the coroutine's stack until its next resume, and the
// resumer reads them there: after the stack grew, through nested resumes, and after a yield
// from inside pcall. The executor is back on the resumer's thread after an error too.
func TestYieldValuesInPlace(t *testing.T) {
src := `
local function deep(n) if n == 0 then return coroutine.yield("a", n, "c") end return deep(n - 1) end
local co = coroutine.create(function(n) local x, y = deep(n); return x .. y end)
local ok, a, b, c = coroutine.resume(co, 3000) -- the stack grows before the yield
assert(ok and a == "a" and b == 0 and c == "c", tostring(a))
ok, a = coroutine.resume(co, "p", "q")
assert(ok and a == "pq", tostring(a))
local inner = coroutine.wrap(function() for i = 1, 3 do coroutine.yield(i, i * i) end end)
local outer = coroutine.wrap(function()
for i = 1, 3 do
local v, sq = inner() -- a nested resume, then a yield of what it gave
coroutine.yield(v + sq)
end
end)
assert(outer() == 2 and outer() == 6 and outer() == 12)
local pc = coroutine.wrap(function() return pcall(coroutine.yield, "in pcall") end)
assert(pc() == "in pcall")
assert(select("#", pc("r1", "r2")) == 3) -- true, then the resume values
local bad = coroutine.wrap(function() error("boom") end)
local ok2, err = pcall(bad)
assert(not ok2 and err:find("boom"), err)
local co2, main = coroutine.running()
assert(main and coroutine.status(co2) == "running")
return coroutine.wrap(function() coroutine.yield("after") end)()`
for _, checked := range []bool{true, false} {
res, err := runInEnv(t, MustNew(LibSafe|LibCoroutine, withLoop(checked)), src)
if err != nil || len(res) != 1 || res[0].String() != "after" {
t.Errorf("checked=%v: %v %v", checked, res, err)
}
}
}
// A thread that runs keeps no copy of its counters and frames: they are the executor's. So
// an entry that resumed a coroutine leaves nothing behind in the main thread's record, and
// the pooled executor holds no closure of the script.
func TestRunningThreadKeepsNoState(t *testing.T) {
p, _, err := compileChunk("t", `
local co = coroutine.create(function() coroutine.yield(1) end)
coroutine.resume(co)
return coroutine.resume(co)`)
if err != nil {
t.Fatal(err)
}
ex := newExecutor()
ex.strMeta = nil
g := testGlobals()
g.Set(String("coroutine"), tableValue(coroutineLib()))
if _, err := runClosure(context.Background(), ex, &closure{p: p, env: g}); err != nil {
t.Fatal(err)
}
if m := ex.mainCo; m.st.cl != nil || m.pframes != nil || m.st.hi != 0 {
t.Errorf("the main thread's record after the entry: %+v", m.st)
}
}
// A resume that fails before the body runs, for want of room for its values, is raised in
// the resumer and leaves the coroutine suspended where it was, so it can be resumed again
// and closed.
func TestResumeWithoutRoomLeavesCoroutine(t *testing.T) {
if stackStress {
t.Skip("moving a stack of a million slots at every check is slow")
}
src := `
local function deep(n) if n == 0 then return (coroutine.yield("deep")) end return (deep(n - 1)) end
local co = coroutine.create(deep)
assert(select(2, coroutine.resume(co, 5000)) == "deep") -- its stack holds 5000 frames
local big = {} -- values that fit the resumer's stack but not the coroutine's
for i = 1, 1045000 do big[i] = i end
local ok, err = pcall(coroutine.resume, co, table.unpack(big)) -- line 7
assert(not ok and tostring(err):find("stack overflow"), tostring(err))
assert(coroutine.status(co) == "suspended", coroutine.status(co))
local ok2, v = coroutine.resume(co, "again")
assert(ok2 and v == "again", tostring(v))
assert(coroutine.status(co) == "dead")
return err`
rt := MustNew(LibSafe|LibCoroutine, Limits{Steps: NoLimit, AllocBytes: NoLimit})
res, err := runInEnv(t, rt, src)
if err != nil || len(res) != 1 || !strings.HasPrefix(res[0].String(), "test:7: stack overflow") {
t.Errorf("%v %v", res, err) // the position is the resume's, not the coroutine's
}
}
// A panic in a coroutine's host function, recovered at the entry of a nested Script call,
// ends that entry and discards its executor; the entry that called it goes on, on its own
// thread, with its stack and the main thread as they were.
func TestNestedEntryPanicLeavesCallerThread(t *testing.T) {
var inner *Script
bang := GoFunc(func(c *Call) error { panic("bang") })
nested := GoFunc(func(c *Call) error {
f, _ := inner.Func0[int64]("go")
_, err := f(c.Context())
var pe *PanicError
if !errors.As(err, &pe) {
return c.Errorf("nested: %v", err)
}
return c.Return(int64(7))
})
rt := MustNew(LibSafe|LibCoroutine, RecoverPanics(), Global("bang", bang), Global("nested", nested))
inner = mustLoad(t, rt, `function go() coroutine.resume(coroutine.create(function() bang() end)); return 1 end`)
outer := mustLoad(t, rt, `function work()
local t = {}
for i = 1, 100 do t[i] = i end
local v = nested()
local s = 0
for i = 1, 100 do s = s + t[i] end
local _, main = coroutine.running()
return v + s, main
end`)
work, _ := outer.Func0[Value]("work")
for i := range 3 {
res, err := work(context.Background())
if err != nil || res.String() != "5057" {
t.Fatalf("round %d: %v %v", i, res, err)
}
}
}
// A coroutine closed after dying in error leaves its stack to the next one, as a body that
// returned does: a loop of wrapped bodies that fail allocates no stacks after the first.
func TestCoroutineStacksReusedAfterError(t *testing.T) {
if stackStress {
t.Skip("lua_stackstress allocates a stack at every check")
}
s := mustLoad(t, MustNew(LibSafe|LibCoroutine), `local f = function() error("x") end
function work(n) for i = 1, n do pcall(coroutine.wrap(f)) end end`)
work, _ := s.Func1[int64, struct{}]("work")
ctx := context.Background()
if _, err := work(ctx, 10); err != nil {
t.Fatal(err)
}
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
if _, err := work(ctx, 1000); err != nil {
t.Fatal(err)
}
runtime.ReadMemStats(&after)
if per := (after.TotalAlloc - before.TotalAlloc) / 1000; per > 4<<10 {
t.Errorf("%d bytes allocated for each coroutine", per)
}
}
// Values that the coroutine's stack cannot hold are refused before the switch, with the
// resumer's position, exactly where its first frame would overflow the stack's limit.
func TestResumeAtStackLimit(t *testing.T) {
if stackStress {
t.Skip("a stack of a million slots moves slowly")
}
src := `
local co = coroutine.create(function(...) return select("#", ...) end)
local big = {}
for i = 1, 1048063 do big[i] = i end -- one more slot than the limit allows a first frame
local ok, err = pcall(coroutine.resume, co, table.unpack(big)) -- line 5
assert(not ok, "resumed")
assert(coroutine.status(co) == "suspended", coroutine.status(co))
assert(select(2, coroutine.resume(co, 1, 2)) == 2)
return err`
rt := MustNew(LibSafe|LibCoroutine, Limits{Steps: NoLimit, AllocBytes: NoLimit})
res, err := runInEnv(t, rt, src)
if err != nil || len(res) != 1 || !strings.HasPrefix(res[0].String(), "test:5: stack overflow") {
t.Errorf("%v %v", res, err)
}
}
// The closers of a coroutine run on its own stack, placed by its frames, not above the
// arguments of the resume or close that runs them, which lie on another thread's stack: a
// close from deep in the main thread does not grow the coroutine's stack to that depth.
func TestCloseFromDeepResumer(t *testing.T) {
src := `
local co = coroutine.create(function()
local x <close> = setmetatable({}, {__close = function() end})
coroutine.yield()
end)
coroutine.resume(co)
local function deep(n, ...)
if n == 0 then return coroutine.close(co) end
return (deep(n - 1, ...))
end
local wide = {}
for i = 1, 150 do wide[i] = i end
assert(deep(2000, table.unpack(wide)))
local never = coroutine.create(function() end)
assert(coroutine.close(never))
return coroutine.status(co), coroutine.status(never)`
rt := MustNew(LibSafe|LibCoroutine, Limits{AllocBytes: 16 << 20})
res, err := runInEnv(t, rt, src)
if err != nil || len(res) != 2 || res[0].String() != "dead" || res[1].String() != "dead" {
t.Errorf("%v %v", res, err)
}
}
// A host function that recovers a panic which unwound the VM through it ends its entry
// with the executor's counters, and the thread it runs on, as the panic left them: the
// entry completes, and the executor is never used again. The next entry of the VM runs
// coroutines on a fresh one.
func TestHostRecoveredPanicDiscardsExecutor(t *testing.T) {
bang := GoFunc(func(c *Call) error { panic("bang") })
swallow := GoFunc(func(c *Call) error {
defer func() { recover() }()
_, err := c.Invoke(c.Value(0))
return err
})
rt := MustNew(LibSafe|LibCoroutine, withLoop(true), Global("bang", bang), Global("swallow", swallow))
s := mustLoad(t, rt, `
function hit() swallow(function() coroutine.resume(coroutine.create(function() bang() end)) end) end
function gen()
local s = 0
for v in coroutine.wrap(function() for i = 1, 100 do coroutine.yield(i) end end) do s = s + v end
return s
end`)
ctx := context.Background()
vm, err := s.NewVM(ctx)
if err != nil {
t.Fatal(err)
}
hit, _ := vm.Func0[struct{}]("hit")
gen, _ := vm.Func0[int64]("gen")
for i := range 3 {
hit(ctx) // whatever the damaged entry returns
if got, err := gen(ctx); err != nil || got != 5050 {
t.Fatalf("round %d after a recovered panic: %d, %v", i, got, err)
}
}
}
// A closer is placed above the arguments of the Go call it runs under, not above those of
// a call that returned, whose record may hold a position on another thread's stack.
func TestCloserPlacementIgnoresReturnedCalls(t *testing.T) {
var sizes []int
g := GoFunc(func(c *Call) error { return nil })
a := GoFunc(func(c *Call) error { _, err := c.Invoke(c.Value(0)); return err })
mark := GoFunc(func(c *Call) error { sizes = append(sizes, len(c.ex.stack)); return nil })
rt := MustNew(LibSafe|LibCoroutine, Global("g", g), Global("a", a), Global("mark", mark))
_, err := runInEnv(t, rt, `
local co = coroutine.wrap(function()
local function deep(n) if n == 0 then g() coroutine.yield() return end return (deep(n - 1)) end
deep(600)
end)
co() -- g ran 600 frames up the coroutine's stack; its record at that depth stays
mark()
a(function() pcall(function() local x <close> = setmetatable({}, {__close = function() end}) error("e") end) end)
mark()`)
if err != nil {
t.Fatal(err)
}
if sizes[1] != sizes[0] {
t.Errorf("the main stack grew from %d to %d slots for a closer placed by a returned call", sizes[0], sizes[1])
}
}
// A wrapped coroutine that fails leaves its error value nowhere: the call record that
// resume wrote it into lets it go when the host call returns.
func TestWrapFailureKeepsNothing(t *testing.T) {