Commit d16685bd authored by protolambda's avatar protolambda

cmd: improve exit handling

parent d2241c6d
......@@ -162,7 +162,13 @@ func (p *ProcessPreimageOracle) Close() error {
}
_ = p.cmd.Process.Signal(os.Interrupt)
p.cmd.WaitDelay = time.Second * 10
return p.cmd.Wait()
err := p.cmd.Wait()
if err, ok := err.(*exec.ExitError); ok {
if err.Success() {
return nil
}
}
return err
}
type StepFn func(proof bool) (*mipsevm.StepWitness, error)
......@@ -248,6 +254,10 @@ func Run(ctx *cli.Context) error {
startStep := state.Step
for !state.Exited {
if err := ctx.Context.Err(); err != nil {
return err
}
step := state.Step
name := meta.LookupSymbol(state.PC)
......
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/urfave/cli/v2"
......@@ -18,9 +22,26 @@ func main() {
cmd.LoadELFCommand,
cmd.RunCommand,
}
err := app.Run(os.Args)
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
for {
<-c
cancel()
fmt.Println("\r\nExiting...")
}
}()
err := app.RunContext(ctx, os.Args)
if err != nil {
if errors.Is(err, ctx.Err()) {
_, _ = fmt.Fprintf(os.Stderr, "command interrupted")
os.Exit(130)
} else {
_, _ = fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
}
}
}
......@@ -41,7 +41,7 @@ func (m *InstrumentedState) handleSyscall() error {
a1 := m.state.Registers[5]
a2 := m.state.Registers[6]
fmt.Printf("syscall: %d\n", syscallNum)
//fmt.Printf("syscall: %d\n", syscallNum)
switch syscallNum {
case 4090: // mmap
sz := a1
......@@ -50,11 +50,11 @@ func (m *InstrumentedState) handleSyscall() error {
}
if a0 == 0 {
v0 = m.state.Heap
fmt.Printf("mmap heap 0x%x size 0x%x\n", v0, sz)
//fmt.Printf("mmap heap 0x%x size 0x%x\n", v0, sz)
m.state.Heap += sz
} else {
v0 = a0
fmt.Printf("mmap hint 0x%x size 0x%x\n", v0, sz)
//fmt.Printf("mmap hint 0x%x size 0x%x\n", v0, sz)
}
// Go does this thing where it first gets memory with PROT_NONE,
// and then mmaps with a hint with prot=3 (PROT_READ|WRITE).
......@@ -84,7 +84,7 @@ func (m *InstrumentedState) handleSyscall() error {
m.trackMemAccess(effAddr)
mem := m.state.Memory.GetMemory(effAddr)
dat, datLen := m.readPreimage(m.state.PreimageKey, m.state.PreimageOffset)
fmt.Printf("reading pre-image data: addr: %08x, offset: %d, datLen: %d, data: %x, key: %s count: %d\n", a1, m.state.PreimageOffset, datLen, dat[:datLen], m.state.PreimageKey, a2)
//fmt.Printf("reading pre-image data: addr: %08x, offset: %d, datLen: %d, data: %x, key: %s count: %d\n", a1, m.state.PreimageOffset, datLen, dat[:datLen], m.state.PreimageKey, a2)
alignment := a1 & 3
space := 4 - alignment
if space < datLen {
......@@ -99,7 +99,7 @@ func (m *InstrumentedState) handleSyscall() error {
m.state.Memory.SetMemory(effAddr, binary.BigEndian.Uint32(outMem[:]))
m.state.PreimageOffset += datLen
v0 = datLen
fmt.Printf("read %d pre-image bytes, new offset: %d, eff addr: %08x mem: %08x\n", datLen, m.state.PreimageOffset, effAddr, outMem)
//fmt.Printf("read %d pre-image bytes, new offset: %d, eff addr: %08x mem: %08x\n", datLen, m.state.PreimageOffset, effAddr, outMem)
case fdHintRead: // hint response
// don't actually read into memory, just say we read it all, we ignore the result anyway
v0 = a2
......@@ -147,7 +147,7 @@ func (m *InstrumentedState) handleSyscall() error {
copy(key[32-a2:], tmp[:])
m.state.PreimageKey = key
m.state.PreimageOffset = 0
fmt.Printf("updating pre-image key: %s\n", m.state.PreimageKey)
//fmt.Printf("updating pre-image key: %s\n", m.state.PreimageKey)
v0 = a2
default:
v0 = 0xFFffFFff
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment