Commit d16685bd authored by protolambda's avatar protolambda

cmd: improve exit handling

parent d2241c6d
...@@ -162,7 +162,13 @@ func (p *ProcessPreimageOracle) Close() error { ...@@ -162,7 +162,13 @@ func (p *ProcessPreimageOracle) Close() error {
} }
_ = p.cmd.Process.Signal(os.Interrupt) _ = p.cmd.Process.Signal(os.Interrupt)
p.cmd.WaitDelay = time.Second * 10 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) type StepFn func(proof bool) (*mipsevm.StepWitness, error)
...@@ -248,6 +254,10 @@ func Run(ctx *cli.Context) error { ...@@ -248,6 +254,10 @@ func Run(ctx *cli.Context) error {
startStep := state.Step startStep := state.Step
for !state.Exited { for !state.Exited {
if err := ctx.Context.Err(); err != nil {
return err
}
step := state.Step step := state.Step
name := meta.LookupSymbol(state.PC) name := meta.LookupSymbol(state.PC)
......
package main package main
import ( import (
"context"
"errors"
"fmt" "fmt"
"os" "os"
"os/signal"
"syscall"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
...@@ -18,9 +22,26 @@ func main() { ...@@ -18,9 +22,26 @@ func main() {
cmd.LoadELFCommand, cmd.LoadELFCommand,
cmd.RunCommand, 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 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) _, _ = fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1) os.Exit(1)
} }
}
} }
...@@ -41,7 +41,7 @@ func (m *InstrumentedState) handleSyscall() error { ...@@ -41,7 +41,7 @@ func (m *InstrumentedState) handleSyscall() error {
a1 := m.state.Registers[5] a1 := m.state.Registers[5]
a2 := m.state.Registers[6] a2 := m.state.Registers[6]
fmt.Printf("syscall: %d\n", syscallNum) //fmt.Printf("syscall: %d\n", syscallNum)
switch syscallNum { switch syscallNum {
case 4090: // mmap case 4090: // mmap
sz := a1 sz := a1
...@@ -50,11 +50,11 @@ func (m *InstrumentedState) handleSyscall() error { ...@@ -50,11 +50,11 @@ func (m *InstrumentedState) handleSyscall() error {
} }
if a0 == 0 { if a0 == 0 {
v0 = m.state.Heap 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 m.state.Heap += sz
} else { } else {
v0 = a0 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, // Go does this thing where it first gets memory with PROT_NONE,
// and then mmaps with a hint with prot=3 (PROT_READ|WRITE). // and then mmaps with a hint with prot=3 (PROT_READ|WRITE).
...@@ -84,7 +84,7 @@ func (m *InstrumentedState) handleSyscall() error { ...@@ -84,7 +84,7 @@ func (m *InstrumentedState) handleSyscall() error {
m.trackMemAccess(effAddr) m.trackMemAccess(effAddr)
mem := m.state.Memory.GetMemory(effAddr) mem := m.state.Memory.GetMemory(effAddr)
dat, datLen := m.readPreimage(m.state.PreimageKey, m.state.PreimageOffset) 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 alignment := a1 & 3
space := 4 - alignment space := 4 - alignment
if space < datLen { if space < datLen {
...@@ -99,7 +99,7 @@ func (m *InstrumentedState) handleSyscall() error { ...@@ -99,7 +99,7 @@ func (m *InstrumentedState) handleSyscall() error {
m.state.Memory.SetMemory(effAddr, binary.BigEndian.Uint32(outMem[:])) m.state.Memory.SetMemory(effAddr, binary.BigEndian.Uint32(outMem[:]))
m.state.PreimageOffset += datLen m.state.PreimageOffset += datLen
v0 = 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 case fdHintRead: // hint response
// don't actually read into memory, just say we read it all, we ignore the result anyway // don't actually read into memory, just say we read it all, we ignore the result anyway
v0 = a2 v0 = a2
...@@ -147,7 +147,7 @@ func (m *InstrumentedState) handleSyscall() error { ...@@ -147,7 +147,7 @@ func (m *InstrumentedState) handleSyscall() error {
copy(key[32-a2:], tmp[:]) copy(key[32-a2:], tmp[:])
m.state.PreimageKey = key m.state.PreimageKey = key
m.state.PreimageOffset = 0 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 v0 = a2
default: default:
v0 = 0xFFffFFff 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