From 27f689959decd391b047c8034d481267d500549e Mon Sep 17 00:00:00 2001 From: Taras Madan Date: Thu, 15 May 2025 15:01:02 +0200 Subject: vm: func Run accepts context It allows to use context as a single termination signal source. --- pkg/build/netbsd.go | 5 ++++- pkg/instance/execprog.go | 5 ++++- pkg/manager/diff.go | 6 ++++-- syz-manager/manager.go | 6 ++++-- syz-manager/snapshot.go | 4 +++- vm/adb/adb.go | 6 +++--- vm/bhyve/bhyve.go | 7 +++---- vm/cuttlefish/cuttlefish.go | 5 +++-- vm/gce/gce.go | 5 ++--- vm/gvisor/gvisor.go | 7 +++---- vm/isolated/isolated.go | 6 +++--- vm/proxyapp/proxyappclient.go | 12 ++---------- vm/proxyapp/proxyappclient_test.go | 38 +++++++++++++++----------------------- vm/qemu/qemu.go | 6 +++--- vm/starnix/starnix.go | 6 +++--- vm/vm.go | 16 +++------------- vm/vm_test.go | 5 +++-- vm/vmimpl/vmimpl.go | 12 +++++------- vm/vmm/vmm.go | 7 +++---- vm/vmware/vmware.go | 6 +++--- 20 files changed, 76 insertions(+), 94 deletions(-) diff --git a/pkg/build/netbsd.go b/pkg/build/netbsd.go index 31ae6de51..21564e1aa 100644 --- a/pkg/build/netbsd.go +++ b/pkg/build/netbsd.go @@ -4,6 +4,7 @@ package build import ( + "context" "encoding/json" "fmt" "os" @@ -155,7 +156,9 @@ func (ctx netbsd) copyKernelToDisk(targetArch, vmType, outputDir, kernel string) } commands = append(commands, "mknod /dev/vhci c 355 0") commands = append(commands, "sync") // Run sync so that the copied image is stored properly. - _, rep, err := inst.Run(time.Minute, reporter, strings.Join(commands, ";")) + ctxTimeout, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + _, rep, err := inst.Run(ctxTimeout, reporter, strings.Join(commands, ";")) if err != nil { return fmt.Errorf("error syncing the instance %w", err) } diff --git a/pkg/instance/execprog.go b/pkg/instance/execprog.go index 4fd656fc5..83461d07e 100644 --- a/pkg/instance/execprog.go +++ b/pkg/instance/execprog.go @@ -4,6 +4,7 @@ package instance import ( + "context" "fmt" "os" "time" @@ -123,7 +124,9 @@ func (inst *ExecProgInstance) runCommand(command string, duration time.Duration, if inst.BeforeContextLen != 0 { opts = append(opts, vm.OutputSize(inst.BeforeContextLen)) } - output, rep, err := inst.VMInstance.Run(duration, inst.reporter, command, opts...) + ctxTimeout, cancel := context.WithTimeout(context.Background(), duration) + defer cancel() + output, rep, err := inst.VMInstance.Run(ctxTimeout, inst.reporter, command, opts...) if err != nil { return nil, fmt.Errorf("failed to run command in VM: %w", err) } diff --git a/pkg/manager/diff.go b/pkg/manager/diff.go index dd9c7eebc..61fc91731 100644 --- a/pkg/manager/diff.go +++ b/pkg/manager/diff.go @@ -535,8 +535,10 @@ func (kc *kernelContext) runInstance(ctx context.Context, inst *vm.Instance, return nil, fmt.Errorf("failed to parse manager's address") } cmd := fmt.Sprintf("%v runner %v %v %v", executorBin, inst.Index(), host, port) - _, rep, err := inst.Run(kc.cfg.Timeouts.VMRunningTime, kc.reporter, cmd, - vm.ExitTimeout, vm.StopContext(ctx), vm.InjectExecuting(injectExec), + ctxTimeout, cancel := context.WithTimeout(ctx, kc.cfg.Timeouts.VMRunningTime) + defer cancel() + _, rep, err := inst.Run(ctxTimeout, kc.reporter, cmd, vm.ExitTimeout, + vm.InjectExecuting(injectExec), vm.EarlyFinishCb(func() { // Depending on the crash type and kernel config, fuzzing may continue // running for several seconds even after kernel has printed a crash report. diff --git a/syz-manager/manager.go b/syz-manager/manager.go index 20b815422..f951db46c 100644 --- a/syz-manager/manager.go +++ b/syz-manager/manager.go @@ -652,8 +652,10 @@ func (mgr *Manager) runInstanceInner(ctx context.Context, inst *vm.Instance, inj return nil, nil, fmt.Errorf("failed to parse manager's address") } cmd := fmt.Sprintf("%v runner %v %v %v", executorBin, inst.Index(), host, port) - _, rep, err := inst.Run(mgr.cfg.Timeouts.VMRunningTime, mgr.reporter, cmd, - vm.ExitTimeout, vm.StopContext(ctx), vm.InjectExecuting(injectExec), + ctxTimeout, cancel := context.WithTimeout(ctx, mgr.cfg.Timeouts.VMRunningTime) + defer cancel() + _, rep, err := inst.Run(ctxTimeout, mgr.reporter, cmd, + vm.ExitTimeout, vm.InjectExecuting(injectExec), finishCb, ) if err != nil { diff --git a/syz-manager/snapshot.go b/syz-manager/snapshot.go index 21e6ed62b..f2a01961e 100644 --- a/syz-manager/snapshot.go +++ b/syz-manager/snapshot.go @@ -41,7 +41,9 @@ func (mgr *Manager) snapshotLoop(ctx context.Context, inst *vm.Instance) error { // All network connections (including ssh) will break once we start restoring snapshots. // So we start a background process and log to /dev/kmsg. cmd := fmt.Sprintf("nohup %v exec snapshot 1>/dev/null 2>/dev/kmsg