-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (120 loc) · 4.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package mipsevm
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
uc "github.com/CryptoKass/unicorn/bindings/go/unicorn"
)
func WriteCheckpoint(ram map[uint32](uint32), fn string, step int) {
trieroot := RamToTrie(ram)
dat := TrieToJson(trieroot, step)
fmt.Printf("writing %s len %d with root %s\n", fn, len(dat), trieroot)
ioutil.WriteFile(fn, dat, 0644)
}
func mipsevm() {
var target int
var programPath string
var evm bool
var basedir string
var outputGolden bool
var blockNumber int
var root string
defaultBasedir := os.Getenv("BASEDIR")
if len(defaultBasedir) == 0 {
defaultBasedir = "/tmp/cannon"
}
flag.StringVar(&basedir, "basedir", defaultBasedir, "Directory to read inputs, write outputs, and cache preimage oracle data.")
flag.IntVar(&blockNumber, "blockNumber", -1, "For state transition programs (e.g. rollups), used to create a separate subdirectory in the basedir for each block inputs/outputs and snapshots.")
flag.IntVar(&target, "target", -1, "Target number of instructions to execute in the trace. If < 0 will execute until termination")
flag.StringVar(&programPath, "program", "mipigo/minigeth.bin", "Path to binary file containing the program to run")
flag.BoolVar(&evm, "evm", false, "If the program should be executed by a MIPS emulator running inside the EVM. This is much much slower than using the Unicorn emulator but exactly replicates the fault proving environment.")
flag.BoolVar(&outputGolden, "outputGolden", false, "Do not read any inputs and instead produce a snapshot of the state prior to execution. Written to <basedir>/golden.json")
flag.Parse()
if blockNumber >= 0 {
root = fmt.Sprintf("%s/%d_%d", basedir, 0, blockNumber)
} else {
root = basedir
}
regfault := -1
regfault_str, regfault_valid := os.LookupEnv("REGFAULT")
if regfault_valid {
regfault, _ = strconv.Atoi(regfault_str)
}
// step 1, generate the checkpoints every million steps using unicorn
ram := make(map[uint32](uint32))
lastStep := 1
if evm {
// TODO: fix this
log.Fatal("EVM execution currently not supported")
/*ZeroRegisters(ram)
LoadMappedFile(programPath, ram, 0)
WriteCheckpoint(ram, "/tmp/cannon/golden.json", -1)
LoadMappedFile(fmt.Sprintf("%s/input", root), ram, 0x30000000)
RunWithRam(ram, target-1, 0, root, nil)
lastStep += target - 1
fn := fmt.Sprintf("%s/checkpoint_%d.json", root, lastStep)
WriteCheckpoint(ram, fn, lastStep)*/
} else {
mu := GetHookedUnicorn(root, ram, func(step int, mu uc.Unicorn, ram map[uint32](uint32)) {
// it seems this runs before the actual step happens
// this can be raised to 10,000,000 if the files are too large
//if (target == -1 && step%10000000 == 0) || step == target {
// first run checkpointing is disabled for now since is isn't used
if step == regfault {
fmt.Printf("regfault at step %d\n", step)
mu.RegWrite(uc.MIPS_REG_V0, 0xbabababa)
}
if step == target {
SyncRegs(mu, ram)
fn := fmt.Sprintf("%s/checkpoint_%d.json", root, step)
WriteCheckpoint(ram, fn, step)
if step == target {
// done
mu.RegWrite(uc.MIPS_REG_PC, 0x5ead0004)
}
}
lastStep = step + 1
})
ZeroRegisters(ram)
// not ready for golden yet
LoadMappedFileUnicorn(mu, programPath, ram, 0)
if outputGolden {
WriteCheckpoint(ram, fmt.Sprintf("%s/golden.json", root), -1)
fmt.Println("Writing golden snapshot and exiting early without execution")
os.Exit(0)
}
LoadMappedFileUnicorn(mu, fmt.Sprintf("%s/input", root), ram, 0x30000000)
mu.Start(0, 0x5ead0004)
SyncRegs(mu, ram)
}
if target == -1 {
if ram[0x30000800] != 0x1337f00d {
log.Fatal("failed to output state root, exiting")
}
output_filename := fmt.Sprintf("%s/output", root)
outputs, err := ioutil.ReadFile(output_filename)
check(err)
real := append([]byte{0x13, 0x37, 0xf0, 0x0d}, outputs...)
output := []byte{}
for i := 0; i < 0x44; i += 4 {
t := make([]byte, 4)
binary.BigEndian.PutUint32(t, ram[uint32(0x30000800+i)])
output = append(output, t...)
}
if bytes.Compare(real, output) != 0 {
fmt.Println("MISMATCH OUTPUT, OVERWRITING!!!")
ioutil.WriteFile(output_filename, output[4:], 0644)
} else {
fmt.Println("output match")
}
WriteCheckpoint(ram, fmt.Sprintf("%s/checkpoint_final.json", root), lastStep)
}
// step 2 (optional), validate each 1 million chunk in EVM
// step 3 (super optional) validate each 1 million chunk on chain
//RunWithRam(ram, steps, debug, nil)
}