-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec_suite.go
More file actions
1221 lines (1043 loc) · 41.1 KB
/
Copy pathexec_suite.go
File metadata and controls
1221 lines (1043 loc) · 41.1 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
/*
Copyright The containerd Authors.
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
http://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.
*/
package shimtest
import (
"bytes"
"fmt"
"hash/crc32"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
taskAPI "github.com/containerd/containerd/api/runtime/task/v3"
tasktypes "github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/ttrpc"
typeurl "github.com/containerd/typeurl/v2"
"github.com/opencontainers/runtime-spec/specs-go"
)
// ExecSuite contains tests gated on the "exec" feature: running
// processes inside a container, stdio round-trip, the in-VM clock,
// exec exit-code propagation, and file-IO benchmarks driven via
// hashverify.
type ExecSuite struct {
cfg Config
}
// NewExecSuite constructs an ExecSuite from the given options.
func NewExecSuite(cfg Config) *ExecSuite {
return &ExecSuite{cfg: cfg}
}
// Run runs every test in the suite as a subtest of t.
func (s *ExecSuite) Run(t *testing.T) {
t.Helper()
registerShimLeakCheck(t, s.cfg.ShimBinary)
t.Run("Exec", s.testExec)
t.Run("StdioRoundTrip", s.testStdioRoundTrip)
t.Run("LargeStdioRoundTrip", s.testLargeStdioRoundTrip)
t.Run("Clock", s.testClock)
t.Run("ExitCodes", s.testExitCodes)
t.Run("LargeFileRead", s.testLargeFileRead)
t.Run("BindMountRead", s.testBindMountRead)
t.Run("FastExitOutput", s.testFastExitOutput)
t.Run("ExecOutputDrainAfterExit", s.testExecOutputDrainAfterExit)
t.Run("ExecDiscardIO", s.testExecDiscardIO)
t.Run("ExecCommandNotFound", s.testExecCommandNotFound)
}
// TestExec runs `echo execworks` inside a container and verifies the
// stdout makes it back through the shim.
func (s *ExecSuite) testExec(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/forever"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "exec")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := taskAPI.NewTTRPCTaskClient(client)
drainFifo(t, ctx, stdoutPath)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("start failed:", err)
}
execID := "exec1"
execStdout, execStderr := createIOFifos(t, t.TempDir())
var execBuf bytes.Buffer
var execMu sync.Mutex
drainFifoInto(t, ctx, execStdout, &execBuf, &execMu)
drainFifo(t, ctx, execStderr)
procSpec, err := typeurl.MarshalAnyToProto(&specs.Process{
Args: []string{"/bin/echo", "execworks"},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin"},
})
if err != nil {
t.Fatal("failed to marshal exec spec:", err)
}
if _, err := tc.Exec(ctx, &taskAPI.ExecProcessRequest{
ID: containerID,
ExecID: execID,
Spec: procSpec,
Stdout: execStdout,
Stderr: execStderr,
}); err != nil {
t.Fatal("exec failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec start failed:", err)
}
waitResp, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID, ExecID: execID})
if err != nil {
t.Fatal("exec wait failed:", err)
}
t.Log("exec exit status:", waitResp.ExitStatus)
time.Sleep(200 * time.Millisecond)
execMu.Lock()
got := execBuf.String()
execMu.Unlock()
t.Log("exec stdout:", strings.TrimSpace(got))
if !strings.Contains(got, "execworks") {
t.Fatalf("exec output %q does not contain 'execworks'", got)
}
if _, err := tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec delete failed:", err)
}
tc.Kill(ctx, &taskAPI.KillRequest{ID: containerID, Signal: uint32(syscall.SIGKILL), All: true})
tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID})
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID})
shutdownTask(ctx, tc, containerID)
}
// TestStdioRoundTrip writes a token to stdin of an exec'd `cat` and
// verifies the same token is read back from stdout.
func (s *ExecSuite) testStdioRoundTrip(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/forever"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "exec")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := taskAPI.NewTTRPCTaskClient(client)
drainFifo(t, ctx, stdoutPath)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("start failed:", err)
}
execID := "stdio-rt"
execDir := t.TempDir()
execStdin, execStdout, execStderr := createStdioFifos(t, execDir)
var execBuf bytes.Buffer
var execMu sync.Mutex
drainFifoInto(t, ctx, execStdout, &execBuf, &execMu)
drainFifo(t, ctx, execStderr)
stdinFifo, err := openPipeWriter(ctx, execStdin)
if err != nil {
t.Fatal("open stdin pipe:", err)
}
defer stdinFifo.Close()
procSpec, err := typeurl.MarshalAnyToProto(&specs.Process{
Args: []string{"/bin/cat"},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin"},
})
if err != nil {
t.Fatal("marshal exec spec:", err)
}
if _, err := tc.Exec(ctx, &taskAPI.ExecProcessRequest{
ID: containerID,
ExecID: execID,
Spec: procSpec,
Stdin: execStdin,
Stdout: execStdout,
Stderr: execStderr,
}); err != nil {
t.Fatal("exec failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec start failed:", err)
}
token := randomSuffix() + "\n"
if _, err := stdinFifo.Write([]byte(token)); err != nil {
t.Fatal("write to stdin:", err)
}
deadline := time.After(30 * time.Second)
for {
execMu.Lock()
got := execBuf.String()
execMu.Unlock()
if strings.Contains(got, strings.TrimSpace(token)) {
t.Log("stdio round trip succeeded, got:", strings.TrimSpace(got))
break
}
select {
case <-deadline:
t.Fatalf("timed out waiting for stdio round trip, got: %q", got)
case <-time.After(10 * time.Millisecond):
}
}
stdinFifo.Close()
tc.Kill(ctx, &taskAPI.KillRequest{ID: containerID, ExecID: execID, Signal: uint32(syscall.SIGKILL)})
tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID, ExecID: execID})
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID, ExecID: execID})
tc.Kill(ctx, &taskAPI.KillRequest{ID: containerID, Signal: uint32(syscall.SIGKILL), All: true})
tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID})
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID})
shutdownTask(ctx, tc, containerID)
}
// TestClock verifies the in-container clock matches the host's
// (within a tolerance) by running `date +%s` and bracketing it with
// host timestamps.
func (s *ExecSuite) testClock(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/forever"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "exec")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := taskAPI.NewTTRPCTaskClient(client)
drainFifo(t, ctx, stdoutPath)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("start failed:", err)
}
execID := "clock1"
execStdout, execStderr := createIOFifos(t, t.TempDir())
var execBuf bytes.Buffer
var execMu sync.Mutex
drainFifoInto(t, ctx, execStdout, &execBuf, &execMu)
drainFifo(t, ctx, execStderr)
procSpec, err := typeurl.MarshalAnyToProto(&specs.Process{
Args: []string{"/bin/date", "+%s"},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin:/sbin:/usr/sbin"},
})
if err != nil {
t.Fatal("failed to marshal exec spec:", err)
}
before := time.Now()
if _, err := tc.Exec(ctx, &taskAPI.ExecProcessRequest{
ID: containerID,
ExecID: execID,
Spec: procSpec,
Stdout: execStdout,
Stderr: execStderr,
}); err != nil {
t.Fatal("exec failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec start failed:", err)
}
waitResp, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID, ExecID: execID})
if err != nil {
t.Fatal("exec wait failed:", err)
}
after := time.Now()
if waitResp.ExitStatus != 0 {
t.Fatalf("clock exec exited with status %d", waitResp.ExitStatus)
}
time.Sleep(200 * time.Millisecond)
execMu.Lock()
got := strings.TrimSpace(execBuf.String())
execMu.Unlock()
t.Log("clock exec output:", got)
vmEpoch, err := strconv.ParseInt(got, 10, 64)
if err != nil {
t.Fatalf("failed to parse VM timestamp %q: %v", got, err)
}
vmTime := time.Unix(vmEpoch, 0)
t.Logf("host before: %s", before.UTC())
t.Logf("VM time: %s (epoch %d)", vmTime.UTC(), vmEpoch)
t.Logf("host after: %s", after.UTC())
tolerance := 2 * time.Second
if vmTime.Before(before.Add(-tolerance)) || vmTime.After(after.Add(tolerance)) {
t.Fatalf("VM clock is not synchronized: VM=%s, host range=[%s, %s], drift=%s",
vmTime.UTC(), before.UTC(), after.UTC(),
fmt.Sprintf("%+.1fs", vmTime.Sub(before).Seconds()))
}
if _, err := tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec delete failed:", err)
}
tc.Kill(ctx, &taskAPI.KillRequest{ID: containerID, Signal: uint32(syscall.SIGKILL), All: true})
tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID})
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID})
shutdownTask(ctx, tc, containerID)
}
// TestExitCodes runs /bin/exit N inside a container via exec for a
// range of values and verifies each is propagated back via Wait.
func (s *ExecSuite) testExitCodes(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/forever"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "exec")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := taskAPI.NewTTRPCTaskClient(client)
drainFifo(t, ctx, stdoutPath)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("start failed:", err)
}
for i, code := range []int{0, 1, 2, 42, 127, 255} {
t.Run(fmt.Sprintf("Exit%d", code), func(t *testing.T) {
execID := fmt.Sprintf("exit-%d", i)
execStdout, execStderr := createIOFifos(t, t.TempDir())
drainFifo(t, ctx, execStdout)
drainFifo(t, ctx, execStderr)
procSpec, err := typeurl.MarshalAnyToProto(&specs.Process{
Args: []string{"/bin/exit", strconv.Itoa(code)},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin"},
})
if err != nil {
t.Fatal("marshal exec spec:", err)
}
if _, err := tc.Exec(ctx, &taskAPI.ExecProcessRequest{
ID: containerID,
ExecID: execID,
Spec: procSpec,
Stdout: execStdout,
Stderr: execStderr,
}); err != nil {
t.Fatal("exec failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec start failed:", err)
}
waitResp, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID, ExecID: execID})
if err != nil {
t.Fatal("exec wait failed:", err)
}
if waitResp.ExitStatus != uint32(code) {
t.Fatalf("expected exit status %d, got %d", code, waitResp.ExitStatus)
}
if _, err := tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec delete failed:", err)
}
})
}
tc.Kill(ctx, &taskAPI.KillRequest{ID: containerID, Signal: uint32(syscall.SIGKILL), All: true})
tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID})
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID})
shutdownTask(ctx, tc, containerID)
}
// TestLargeFileRead reads /data/bigfile (a 64 MiB fixture from the
// secondary read-only erofs layer) inside the container, verifies
// its crc32-Castagnoli, and reports throughput.
func (s *ExecSuite) testLargeFileRead(t *testing.T) {
s.runHashverify(t, bigFileContainerPath, bigFileHashHex(), nil)
}
// TestBindMountRead streams the same 64 MiB fixture into a host
// tempfile, bind-mounts it into the container, and runs hashverify
// against the bind path.
func (s *ExecSuite) testBindMountRead(t *testing.T) {
hostFile := filepath.Join(t.TempDir(), "bigfile")
f, err := os.Create(hostFile)
if err != nil {
t.Fatal("create host bigfile:", err)
}
if _, err := io.Copy(f, newBigFileReader()); err != nil {
f.Close()
t.Fatal("write host bigfile:", err)
}
if err := f.Close(); err != nil {
t.Fatal("close host bigfile:", err)
}
const containerPath = "/tmp/bigfile"
mount := specs.Mount{
Type: "bind",
Source: hostFile,
Destination: containerPath,
Options: []string{"rbind"},
}
s.runHashverify(t, containerPath, bigFileHashHex(), []specs.Mount{mount})
}
// runHashverify spins up a container with the given extra mounts,
// execs /bin/hashverify against the path, parses
// "ok bytes=N ns=M cpu_bound=K" from stdout, and reports throughput
// via t.Log.
func (s *ExecSuite) runHashverify(t *testing.T, path, hashHex string, extraMounts []specs.Mount) {
t.Helper()
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
cid := containerID(t)
var opts []func(*specs.Spec)
if len(extraMounts) > 0 {
opts = append(opts, withExtraMounts(extraMounts...))
}
createOCISpec(t, bundleDir, []string{"/bin/forever"}, s.cfg, opts...)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "exec")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, cid, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := taskAPI.NewTTRPCTaskClient(client)
drainFifo(t, ctx, stdoutPath)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, cid, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: cid}); err != nil {
t.Fatal("start failed:", err)
}
execID := "hashv"
execStdout, execStderr := createIOFifos(t, t.TempDir())
var execBuf bytes.Buffer
var execMu sync.Mutex
drainFifoInto(t, ctx, execStdout, &execBuf, &execMu)
drainFifo(t, ctx, execStderr)
procSpec, err := typeurl.MarshalAnyToProto(&specs.Process{
Args: []string{"/bin/hashverify", path, hashHex},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin"},
})
if err != nil {
t.Fatal("marshal exec spec:", err)
}
if _, err := tc.Exec(ctx, &taskAPI.ExecProcessRequest{
ID: cid,
ExecID: execID,
Spec: procSpec,
Stdout: execStdout,
Stderr: execStderr,
}); err != nil {
t.Fatal("exec failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: cid, ExecID: execID}); err != nil {
t.Fatal("exec start failed:", err)
}
waitResp, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: cid, ExecID: execID})
if err != nil {
t.Fatal("exec wait failed:", err)
}
if waitResp.ExitStatus != 0 {
t.Fatalf("hashverify exit status %d", waitResp.ExitStatus)
}
time.Sleep(200 * time.Millisecond)
execMu.Lock()
out := strings.TrimSpace(execBuf.String())
execMu.Unlock()
var nBytes, elapsedNS, cpuBound int64
parsed, err := fmt.Sscanf(out, "ok bytes=%d ns=%d cpu_bound=%d", &nBytes, &elapsedNS, &cpuBound)
if err != nil || parsed != 3 {
t.Fatalf("could not parse hashverify output %q: %v (parsed %d)", out, err, parsed)
}
mibPerSec := float64(nBytes) / (float64(elapsedNS) / 1e9) / (1 << 20)
t.Logf("read %d bytes in %.2f ms = %.2f MiB/s (cpu_bound=%d)",
nBytes, float64(elapsedNS)/1e6, mibPerSec, cpuBound)
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: cid, ExecID: execID})
tc.Kill(ctx, &taskAPI.KillRequest{ID: cid, Signal: uint32(syscall.SIGKILL), All: true})
tc.Wait(ctx, &taskAPI.WaitRequest{ID: cid})
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: cid})
shutdownTask(ctx, tc, cid)
}
// testLargeStdioRoundTrip pipes 20 MiB through stdin → /bin/cat →
// stdout and verifies the full byte count and CRC-32 checksum on the
// way out. It uses the same deterministic 0x00..0xff tile payload as
// the FastExit tests.
//
// This test asserts that a conforming shim delivers all bytes written
// to exec stdin to the process and relays all bytes written by the
// process to stdout back to the host without truncation under sustained
// load. Two API contracts are verified:
//
// - All stdin bytes must be forwarded to the process before the stdin
// connection is closed. A shim that drops buffered bytes on the
// stdin close path will produce truncated output from cat.
//
// - All stdout bytes produced by the process must be delivered to the
// host before the stdout connection is closed. A shim that drops
// buffered bytes when the process exits will produce truncated
// output.
//
// Stdin is closed via the CloseIO RPC, not by simply closing the
// client-side FIFO write end. This matches how containerd itself signals
// EOF: the shim holds its own write-end reference on the stdin FIFO (to
// unblock its internal O_RDONLY open) and only releases it when it
// receives a CloseIO request. Closing the test's write end alone leaves
// the shim's reference open, so cat never sees EOF. The CloseIO RPC
// instructs the shim to drop its reference, delivering EOF to the
// process — exactly the protocol used by `ctr exec`.
//
// A third API contract is also verified: the shim must close the exec's
// stdout connection once the process exits and its output has been
// flushed, without waiting for the caller to issue Delete. The test
// waits for stdout EOF before calling Delete; a shim that defers the
// stdout close until Delete will time out here.
//
// Both failure modes are detected by the byte-count and CRC-32
// assertions at the end of the test.
func (s *ExecSuite) testLargeStdioRoundTrip(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/forever"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "exec")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := taskAPI.NewTTRPCTaskClient(client)
drainFifo(t, ctx, stdoutPath)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("start failed:", err)
}
execID := "large-rt"
execDir := t.TempDir()
execStdin, execStdout, execStderr := createStdioFifos(t, execDir)
stdoutReader, err := openPipeReader(ctx, execStdout)
if err != nil {
t.Fatal("open stdout reader:", err)
}
t.Cleanup(func() { stdoutReader.Close() })
h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
var byteCount int64
outDone := make(chan struct{})
go func() {
defer close(outDone)
byteCount, _ = io.Copy(h, stdoutReader)
}()
drainFifo(t, ctx, execStderr)
stdinFifo, err := openPipeWriter(ctx, execStdin)
if err != nil {
t.Fatal("open stdin pipe:", err)
}
procSpec, err := typeurl.MarshalAnyToProto(&specs.Process{
Args: []string{"/bin/cat"},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin"},
})
if err != nil {
t.Fatal("marshal exec spec:", err)
}
if _, err := tc.Exec(ctx, &taskAPI.ExecProcessRequest{
ID: containerID,
ExecID: execID,
Spec: procSpec,
Stdin: execStdin,
Stdout: execStdout,
Stderr: execStderr,
}); err != nil {
t.Fatal("exec failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec start failed:", err)
}
// Write the tiled payload to stdin in a background goroutine.
// The write and the stdout drain run concurrently so that neither
// the stdin FIFO nor cat's stdout pipe can fill and deadlock.
writeDone := make(chan error, 1)
go func() {
_, err := io.Copy(stdinFifo, io.LimitReader(&infiniteTileReader{}, int64(largeStdioPayloadSize)))
stdinFifo.Close()
writeDone <- err
}()
// Wait for all stdin to be written.
select {
case err := <-writeDone:
if err != nil {
t.Fatal("write to stdin failed:", err)
}
case <-time.After(90 * time.Second):
t.Fatal("timed out writing stdin payload")
}
// Signal EOF to the in-container process via the CloseIO RPC.
//
// Closing the client-side FIFO write end alone is not sufficient:
// the shim holds its own write-end reference on the stdin FIFO
// (opened in openStdin to unblock the shim's internal O_RDONLY
// open) and releases it only when it receives a CloseIO request.
// This matches the protocol used by containerd and `ctr exec`:
// the client calls CloseIO after it is done writing stdin, which
// causes the shim to close its write reference and deliver EOF to
// the process.
if _, err := tc.CloseIO(ctx, &taskAPI.CloseIORequest{
ID: containerID,
ExecID: execID,
Stdin: true,
}); err != nil {
t.Fatal("CloseIO failed:", err)
}
// Wait for cat to exit (it exits once stdin reaches EOF).
waitResp, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID, ExecID: execID})
if err != nil {
t.Fatal("exec wait failed:", err)
}
if waitResp.ExitStatus != 0 {
t.Fatalf("cat exited with status %d", waitResp.ExitStatus)
}
// Wait for the stdout reader to reach EOF before calling Delete.
//
// A conforming shim must close the exec's stdout connection once
// the process exits and its output has been flushed — it must not
// defer closing stdout until Delete is called. Waiting here before
// Delete tests that contract: if the shim closes stdout promptly on
// process exit, outDone fires quickly; if the shim holds stdout
// open until Delete, this will time out.
select {
case <-outDone:
case <-time.After(30 * time.Second):
t.Fatal("timed out waiting for stdout drain after process exit")
}
if _, err := tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec delete failed:", err)
}
if byteCount != int64(largeStdioPayloadSize) {
t.Fatalf("got %d bytes, want %d (truncated by %d bytes)",
byteCount, largeStdioPayloadSize, int64(largeStdioPayloadSize)-byteCount)
}
gotCRC := h.Sum32()
if wantCRC := tiledPayloadCRC32(largeStdioPayloadSize); gotCRC != wantCRC {
t.Fatalf("CRC mismatch: got %08x, want %08x (data corrupted)", gotCRC, wantCRC)
}
t.Logf("ok — %d bytes, CRC %08x", byteCount, gotCRC)
tc.Kill(ctx, &taskAPI.KillRequest{ID: containerID, Signal: uint32(syscall.SIGKILL), All: true})
tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID})
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID})
tc.Shutdown(ctx, &taskAPI.ShutdownRequest{ID: containerID})
}
// burstPayloadSize is the number of bytes written by /bin/burstexit in
// the FastExitOutput and FastExitInit tests. It must be large enough
// that the kernel socket buffers cannot absorb the entire stream
// before the process exits, ensuring that the shim's host-side copy
// goroutine is still mid-read from the vsock when ioShutdown closes
// the connection.
//
// 8 MiB comfortably exceeds the default Linux socket receive buffer
// (rmem_default ≈ 208 KiB) and the 4 KiB bufPool chunk used by
// io.CopyBuffer.
const burstPayloadSize = 8 * 1024 * 1024
// largeStdioPayloadSize is the number of bytes piped through the
// LargeStdioRoundTrip test. Larger than burstPayloadSize to stress the
// shim's in-flight buffering more aggressively.
const largeStdioPayloadSize = 20 * 1024 * 1024
// tiledPayload builds a repeating 0x00..0xff tiled payload of the given
// size. This matches the byte stream produced by /bin/burstexit and is
// shared between the exec and stress suite tests.
func tiledPayload(size int) []byte {
payload := make([]byte, size)
for i := range payload {
payload[i] = byte(i % 256)
}
return payload
}
// tiledPayloadCRC32 returns the Castagnoli CRC-32 of a tiled payload of
// the given size without allocating the full payload. Used when only the
// expected checksum is needed (e.g. verifying /bin/burstexit output).
func tiledPayloadCRC32(size int) uint32 {
var tile [256]byte
for i := range tile {
tile[i] = byte(i)
}
h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
for remaining := size; remaining > 0; {
n := remaining
if n > len(tile) {
n = len(tile)
}
h.Write(tile[:n])
remaining -= n
}
return h.Sum32()
}
// burstExpectedCRC32 returns the Castagnoli CRC-32 of the deterministic
// tiled payload of exactly burstPayloadSize bytes.
func burstExpectedCRC32() uint32 {
return tiledPayloadCRC32(burstPayloadSize)
}
// infiniteTileReader emits an infinite repeating 0x00..0xff tile stream.
// Wrap with io.LimitReader to produce a payload of exactly n bytes without
// allocating the full buffer.
type infiniteTileReader struct{ off int64 }
func (r *infiniteTileReader) Read(p []byte) (int, error) {
for i := range p {
p[i] = byte((r.off + int64(i)) % 256)
}
r.off += int64(len(p))
return len(p), nil
}
// testFastExitOutput execs /bin/burstexit (writes 8 MiB then exits)
// inside a long-running container, then immediately shuts down the
// shim without waiting for the exec to finish or calling Delete.
//
// The race: the exec process writes 8 MiB quickly. The in-VM stdio
// copy goroutine drains the process's stdout pipe into the vsock
// stream. The host copy goroutine reads from the vsock into the FIFO.
// When Shutdown runs, service.shutdown → c.shutdown → ioShutdown
// closes the vsock connection. If the host goroutine is still
// mid-Read at that moment, it sees "use of closed network connection"
// and returns early, dropping whatever bytes were still buffered in
// the kernel socket receive queue.
//
// A correct shim waits for ioDone (all copy goroutines finished
// draining) before closing the connections.
func (s *ExecSuite) testFastExitOutput(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/forever"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "exec")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := taskAPI.NewTTRPCTaskClient(client)
drainFifo(t, ctx, stdoutPath)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("start failed:", err)
}
execID := "burst-0"
execDir := t.TempDir()
execStdout, execStderr := createIOFifos(t, execDir)
var execBuf bytes.Buffer
var execMu sync.Mutex
// drainFifoIntoDone closes done when the FIFO write end is closed,
// which happens after ioShutdown returns (the copy goroutine calls
// wc.Close after exiting). We block on this before reading execBuf.
drainDone := drainFifoIntoDone(t, ctx, execStdout, &execBuf, &execMu)
drainFifo(t, ctx, execStderr)
procSpec, err := typeurl.MarshalAnyToProto(&specs.Process{
Args: []string{"/bin/burstexit", strconv.Itoa(burstPayloadSize), "0"},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin"},
})
if err != nil {
t.Fatal("marshal exec spec:", err)
}
if _, err := tc.Exec(ctx, &taskAPI.ExecProcessRequest{
ID: containerID,
ExecID: execID,
Spec: procSpec,
Stdout: execStdout,
Stderr: execStderr,
}); err != nil {
t.Fatal("exec failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID, ExecID: execID}); err != nil {
t.Fatal("exec start failed:", err)
}
// Shut down the shim immediately — without waiting for the exec to
// finish or calling Delete. This triggers service.shutdown →
// c.shutdown → ioShutdown for the exec stream while burstexit is
// still running (or has just exited with bytes still in the vsock
// receive buffer). The in-VM delete+drain sequence (waitTimeout on
// the IO wg) never runs, so the host copy goroutine races against
// the connection close.
//
// We do NOT kill the init first: Kill(All:true) sends SIGKILL to
// vminitd itself, which triggers the VM's own shutdown sequence and
// tears down the vsock transport before the host can drain it.
// Calling Shutdown directly keeps the VM alive long enough for
// service.shutdown → c.shutdown → ioShutdown to drain the streams
// before s.sb.Stop kills the VM.
tc.Shutdown(ctx, &taskAPI.ShutdownRequest{ID: containerID})
// Wait for the FIFO reader to reach EOF. ioShutdown (via the copy
// goroutine's wc.Close) closes the FIFO write end when the goroutine
// exits. On a buggy shim the goroutine exits early on the close
// error and drainDone fires before all bytes arrive.
select {
case <-drainDone:
case <-time.After(30 * time.Second):
t.Fatal("timed out waiting for stdout drain after shutdown")
}
execMu.Lock()
got := execBuf.Bytes()
execMu.Unlock()
wantCRC := burstExpectedCRC32()
if len(got) != burstPayloadSize {
t.Fatalf("got %d bytes, want %d (truncated by %d bytes)",
len(got), burstPayloadSize, burstPayloadSize-len(got))
}
gotCRC := crc32.Checksum(got, crc32.MakeTable(crc32.Castagnoli))
if gotCRC != wantCRC {
t.Fatalf("CRC mismatch: got %08x, want %08x (data corrupted)", gotCRC, wantCRC)
}
t.Logf("ok — %d bytes, CRC %08x", len(got), gotCRC)
}
// testExecOutputDrainAfterExit verifies that a conforming shim promptly
// closes the exec process's stdout connection when the process exits,
// allowing Shutdown to complete without waiting for the 30 s ioShutdown
// fallback.
//
// The init container is started with null (empty) stdout/stderr so that
// init's stdio shutdown completes instantly and does not consume the
// shared 30 s shutdown deadline. This isolates the exec's stdio close
// as the sole determinant of total Shutdown duration.
//
// A non-conforming shim that fails to propagate the write-end close of
// the exec's stdout back to the host will leave the host copy goroutine
// blocked until the 30 s fallback fires; Shutdown will exceed the 5 s
// assertion and the test will fail on the duration check.
func (s *ExecSuite) testExecOutputDrainAfterExit(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/forever"}, s.cfg)
// Start the init container with null stdout/stderr so its stdio
// shutdown completes instantly. If we used real FIFOs, /bin/forever
// (which never writes) would hold stdout open, consuming the shared
// 30 s shutdown deadline before exec's ioShutdown even starts.
ns := uniqueTestNamespace(t, "exec")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := taskAPI.NewTTRPCTaskClient(client)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, "", "", rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("start failed:", err)
}
execID := "drain-0"
execDir := t.TempDir()
execStdout, execStderr := createIOFifos(t, execDir)
var execBuf bytes.Buffer
var execMu sync.Mutex
drainDone := drainFifoIntoDone(t, ctx, execStdout, &execBuf, &execMu)
drainFifo(t, ctx, execStderr)
procSpec, err := typeurl.MarshalAnyToProto(&specs.Process{
Args: []string{"/bin/burstexit", strconv.Itoa(burstPayloadSize), "0"},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin"},
})
if err != nil {
t.Fatal("marshal exec spec:", err)
}