From 255e8b5e54e93fc77302a546dbb7a932412d1bde Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sun, 24 Sep 2017 10:05:21 +0200 Subject: pkg/ipc: windows port --- pkg/ipc/ipc_fuchsia.go | 108 ------------------------------------------------- pkg/ipc/ipc_simple.go | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 108 deletions(-) delete mode 100644 pkg/ipc/ipc_fuchsia.go create mode 100644 pkg/ipc/ipc_simple.go diff --git a/pkg/ipc/ipc_fuchsia.go b/pkg/ipc/ipc_fuchsia.go deleted file mode 100644 index 7f576b1c2..000000000 --- a/pkg/ipc/ipc_fuchsia.go +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright 2017 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -// +build fuchsia - -package ipc - -import ( - "bytes" - "encoding/binary" - "fmt" - "io/ioutil" - "os" - "os/exec" - "path/filepath" - "strings" - "time" - - "github.com/google/syzkaller/pkg/osutil" - "github.com/google/syzkaller/prog" -) - -type Env struct { - bin []string - pid int - config Config - - StatExecs uint64 - StatRestarts uint64 -} - -func MakeEnv(bin string, pid int, config Config) (*Env, error) { - if config.Timeout < 7*time.Second { - config.Timeout = 7 * time.Second - } - env := &Env{ - bin: strings.Split(bin, " "), - pid: pid, - config: config, - } - if len(env.bin) == 0 { - return nil, fmt.Errorf("binary is empty string") - } - if false { - env.bin[0] = osutil.Abs(env.bin[0]) - base := filepath.Base(env.bin[0]) - pidStr := fmt.Sprint(pid) - if len(base)+len(pidStr) >= 16 { - // TASK_COMM_LEN is currently set to 16 - base = base[:15-len(pidStr)] - } - binCopy := filepath.Join(filepath.Dir(env.bin[0]), base+pidStr) - if err := os.Link(env.bin[0], binCopy); err == nil { - env.bin[0] = binCopy - } - } - return env, nil -} - -func (env *Env) Close() error { - return nil -} - -func (env *Env) Exec(opts *ExecOpts, p *prog.Prog) (output []byte, info []CallInfo, failed, hanged bool, err0 error) { - dir, err := ioutil.TempDir("./", "syzkaller-testdir") - if err != nil { - err0 = fmt.Errorf("failed to create temp dir: %v", err) - return - } - defer os.RemoveAll(dir) - - data := make([]byte, prog.ExecBufferSize) - if err := p.SerializeForExec(data, env.pid); err != nil { - err0 = err - return - } - inbuf := new(bytes.Buffer) - binary.Write(inbuf, binary.LittleEndian, uint64(env.config.Flags)) - binary.Write(inbuf, binary.LittleEndian, uint64(opts.Flags)) - binary.Write(inbuf, binary.LittleEndian, uint64(env.pid)) - inbuf.Write(data) - - cmd := exec.Command(env.bin[0], env.bin[1:]...) - cmd.Env = []string{} - cmd.Dir = dir - cmd.Stdin = inbuf - if env.config.Flags&FlagDebug != 0 { - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stdout - } - if err := cmd.Start(); err != nil { - err0 = err - return - } - done := make(chan error) - go func() { - done <- cmd.Wait() - }() - t := time.NewTimer(env.config.Timeout) - select { - case <-done: - t.Stop() - case <-t.C: - cmd.Process.Kill() - <-done - } - return -} diff --git a/pkg/ipc/ipc_simple.go b/pkg/ipc/ipc_simple.go new file mode 100644 index 000000000..9e5c52b8c --- /dev/null +++ b/pkg/ipc/ipc_simple.go @@ -0,0 +1,108 @@ +// Copyright 2017 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +// +build fuchsia windows + +package ipc + +import ( + "bytes" + "encoding/binary" + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "strings" + "time" + + "github.com/google/syzkaller/pkg/osutil" + "github.com/google/syzkaller/prog" +) + +type Env struct { + bin []string + pid int + config Config + + StatExecs uint64 + StatRestarts uint64 +} + +func MakeEnv(bin string, pid int, config Config) (*Env, error) { + if config.Timeout < 7*time.Second { + config.Timeout = 7 * time.Second + } + env := &Env{ + bin: strings.Split(bin, " "), + pid: pid, + config: config, + } + if len(env.bin) == 0 { + return nil, fmt.Errorf("binary is empty string") + } + if false { + env.bin[0] = osutil.Abs(env.bin[0]) + base := filepath.Base(env.bin[0]) + pidStr := fmt.Sprint(pid) + if len(base)+len(pidStr) >= 16 { + // TASK_COMM_LEN is currently set to 16 + base = base[:15-len(pidStr)] + } + binCopy := filepath.Join(filepath.Dir(env.bin[0]), base+pidStr) + if err := os.Link(env.bin[0], binCopy); err == nil { + env.bin[0] = binCopy + } + } + return env, nil +} + +func (env *Env) Close() error { + return nil +} + +func (env *Env) Exec(opts *ExecOpts, p *prog.Prog) (output []byte, info []CallInfo, failed, hanged bool, err0 error) { + dir, err := ioutil.TempDir("./", "syzkaller-testdir") + if err != nil { + err0 = fmt.Errorf("failed to create temp dir: %v", err) + return + } + defer os.RemoveAll(dir) + + data := make([]byte, prog.ExecBufferSize) + if err := p.SerializeForExec(data, env.pid); err != nil { + err0 = err + return + } + inbuf := new(bytes.Buffer) + binary.Write(inbuf, binary.LittleEndian, uint64(env.config.Flags)) + binary.Write(inbuf, binary.LittleEndian, uint64(opts.Flags)) + binary.Write(inbuf, binary.LittleEndian, uint64(env.pid)) + inbuf.Write(data) + + cmd := exec.Command(env.bin[0], env.bin[1:]...) + cmd.Env = []string{} + cmd.Dir = dir + cmd.Stdin = inbuf + if env.config.Flags&FlagDebug != 0 { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stdout + } + if err := cmd.Start(); err != nil { + err0 = err + return + } + done := make(chan error) + go func() { + done <- cmd.Wait() + }() + t := time.NewTimer(env.config.Timeout) + select { + case <-done: + t.Stop() + case <-t.C: + cmd.Process.Kill() + <-done + } + return +} -- cgit mrf-deployment