From 4b83c8fbed7b9cea831be880ec8aa1098b465f25 Mon Sep 17 00:00:00 2001 From: Marco Vanotti Date: Tue, 19 Nov 2019 17:04:13 -0800 Subject: pkg/repro: refactor vm initialization into new fn This commit moves the instance initialization inside Run() to a subroutine to decrease cyclomatic complexity in the Run function. --- pkg/repro/repro.go | 57 +++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 26 deletions(-) (limited to 'pkg/repro') diff --git a/pkg/repro/repro.go b/pkg/repro/repro.go index 2faefe306..3c1c6fba1 100644 --- a/pkg/repro/repro.go +++ b/pkg/repro/repro.go @@ -60,6 +60,34 @@ type instance struct { executorBin string } +func initInstance(cfg *mgrconfig.Config, vmPool *vm.Pool, vmIndex int) (*instance, error) { + vmInst, err := vmPool.Create(vmIndex) + if err != nil { + return nil, fmt.Errorf("failed to create VM: %v", err) + + } + execprogBin, err := vmInst.Copy(cfg.SyzExecprogBin) + if err != nil { + vmInst.Close() + return nil, fmt.Errorf("failed to copy to VM: %v", err) + } + executorCmd := targets.Get(cfg.TargetOS, cfg.TargetArch).SyzExecutorCmd + if executorCmd == "" { + executorCmd, err = vmInst.Copy(cfg.SyzExecutorBin) + if err != nil { + vmInst.Close() + return nil, fmt.Errorf("failed to copy to VM: %v", err) + } + } + return &instance{ + Instance: vmInst, + index: vmIndex, + execprogBin: execprogBin, + executorBin: executorCmd, + }, nil + +} + func Run(crashLog []byte, cfg *mgrconfig.Config, reporter report.Reporter, vmPool *vm.Pool, vmIndexes []int) (*Result, *Stats, error) { if len(vmIndexes) == 0 { @@ -125,35 +153,12 @@ func Run(crashLog []byte, cfg *mgrconfig.Config, reporter report.Reporter, vmPoo continue default: } - vmInst, err := vmPool.Create(vmIndex) + var err error + inst, err = initInstance(cfg, vmPool, vmIndex) if err != nil { - ctx.reproLog(0, "failed to create VM: %v", err) + ctx.reproLog(0, "failed to init instance: %v", err) time.Sleep(10 * time.Second) continue - - } - execprogBin, err := vmInst.Copy(cfg.SyzExecprogBin) - if err != nil { - ctx.reproLog(0, "failed to copy to VM: %v", err) - vmInst.Close() - time.Sleep(10 * time.Second) - continue - } - executorCmd := targets.Get(cfg.TargetOS, cfg.TargetArch).SyzExecutorCmd - if executorCmd == "" { - executorCmd, err = vmInst.Copy(cfg.SyzExecutorBin) - if err != nil { - ctx.reproLog(0, "failed to copy to VM: %v", err) - vmInst.Close() - time.Sleep(10 * time.Second) - continue - } - } - inst = &instance{ - Instance: vmInst, - index: vmIndex, - execprogBin: execprogBin, - executorBin: executorCmd, } break } -- cgit mrf-deployment