Skip to content

Commit

Permalink
cannon: Cut patch flag from load-elf command altogether
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaxter committed Sep 19, 2024
1 parent 1760d3b commit 273bf9e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ cannon-prestate: op-program cannon ## Generates prestate using cannon and op-pro
.PHONY: cannon-prestate

cannon-prestate-mt: op-program cannon ## Generates prestate using cannon and op-program in the multithreaded cannon format
./cannon/bin/cannon load-elf --type cannon-mt --patch stack --path op-program/bin/op-program-client.elf --out op-program/bin/prestate-mt.bin.gz --meta op-program/bin/meta-mt.json
./cannon/bin/cannon load-elf --type cannon-mt --path op-program/bin/op-program-client.elf --out op-program/bin/prestate-mt.bin.gz --meta op-program/bin/meta-mt.json
./cannon/bin/cannon run --proof-at '=0' --stop-at '=1' --input op-program/bin/prestate-mt.bin.gz --meta op-program/bin/meta-mt.json --proof-fmt 'op-program/bin/%d-mt.json' --output ""
mv op-program/bin/0-mt.json op-program/bin/prestate-proof-mt.json
.PHONY: cannon-prestate
Expand Down
60 changes: 24 additions & 36 deletions cannon/cmd/load_elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@ var (
TakesFile: true,
Required: true,
}
LoadELFPatchFlag = &cli.StringSliceFlag{
Name: "patch",
Usage: "Type of patching to do",
Value: cli.NewStringSlice("go", "stack"),
Required: false,
}
LoadELFOutFlag = &cli.PathFlag{
Name: "out",
Usage: "Output path to write JSON state to. State is dumped to stdout if set to -. Not written if empty.",
Usage: "Output path to write state to. State is dumped to stdout if set to '-'. Not written if empty. Use file extension '.bin', '.bin.gz', or '.json' for binary, compressed binary, or JSON formats.",
Value: "state.json",
Required: false,
}
Expand Down Expand Up @@ -67,51 +61,46 @@ func vmTypeFromString(ctx *cli.Context) (VMType, error) {
}

func LoadELF(ctx *cli.Context) error {
elfPath := ctx.Path(LoadELFPathFlag.Name)
elfProgram, err := elf.Open(elfPath)
if err != nil {
return fmt.Errorf("failed to open ELF file %q: %w", elfPath, err)
}
if elfProgram.Machine != elf.EM_MIPS {
return fmt.Errorf("ELF is not big-endian MIPS R3000, but got %q", elfProgram.Machine.String())
}

var createInitialState func(f *elf.File) (mipsevm.FPVMState, error)

allowGoGCPatch := false
var patcher = program.PatchStack
if vmType, err := vmTypeFromString(ctx); err != nil {
return err
} else if vmType == cannonVMType {
allowGoGCPatch = true
createInitialState = func(f *elf.File) (mipsevm.FPVMState, error) {
return program.LoadELF(f, singlethreaded.CreateInitialState)
}
patcher = func(state mipsevm.FPVMState) error {
err := program.PatchGoGC(elfProgram, state)
if err != nil {
return err
}
return program.PatchStack(state)
}
} else if vmType == mtVMType {
createInitialState = func(f *elf.File) (mipsevm.FPVMState, error) {
return program.LoadELF(f, multithreaded.CreateInitialState)
}
} else {
return fmt.Errorf("invalid VM type: %q", vmType)
}
elfPath := ctx.Path(LoadELFPathFlag.Name)
elfProgram, err := elf.Open(elfPath)
if err != nil {
return fmt.Errorf("failed to open ELF file %q: %w", elfPath, err)
}
if elfProgram.Machine != elf.EM_MIPS {
return fmt.Errorf("ELF is not big-endian MIPS R3000, but got %q", elfProgram.Machine.String())
}

state, err := createInitialState(elfProgram)
if err != nil {
return fmt.Errorf("failed to load ELF data into VM state: %w", err)
}
for _, typ := range ctx.StringSlice(LoadELFPatchFlag.Name) {
switch typ {
case "stack":
err = program.PatchStack(state)
case "go":
if allowGoGCPatch {
err = program.PatchGoGC(elfProgram, state)
} else {
err = fmt.Errorf("patch type %q not allowed for vm type %q", typ, ctx.String(LoadELFVMTypeFlag.Name))
}
default:
return fmt.Errorf("unrecognized form of patching: %q", typ)
}
if err != nil {
return fmt.Errorf("failed to apply patch %s: %w", typ, err)
}
err = patcher(state)
if err != nil {
return fmt.Errorf("failed to patch state: %w", err)
}
meta, err := program.MakeMetadata(elfProgram)
if err != nil {
Expand All @@ -131,13 +120,12 @@ func LoadELF(ctx *cli.Context) error {

var LoadELFCommand = &cli.Command{
Name: "load-elf",
Usage: "Load ELF file into Cannon JSON state",
Description: "Load ELF file into Cannon JSON state, optionally patch out functions",
Usage: "Load ELF file into Cannon state",
Description: "Load ELF file into Cannon state",
Action: LoadELF,
Flags: []cli.Flag{
LoadELFVMTypeFlag,
LoadELFPathFlag,
LoadELFPatchFlag,
LoadELFOutFlag,
LoadELFMetaFlag,
},
Expand Down

0 comments on commit 273bf9e

Please sign in to comment.