Commit e43c4d25 authored by George Hotz's avatar George Hotz

running a block in unicorn-go

parent 7fbb36b8
...@@ -16,7 +16,7 @@ jobs: ...@@ -16,7 +16,7 @@ jobs:
- name: Install unicorn - name: Install unicorn
run: | run: |
curl -LO https://github.com/unicorn-engine/unicorn/archive/1.0.3.tar.gz curl -LO https://github.com/unicorn-engine/unicorn/archive/1.0.3.tar.gz
tar xvf 1.0.3.tar.gz tar xf 1.0.3.tar.gz
cd unicorn-1.0.3 cd unicorn-1.0.3
UNICORN_ARCHS=mips make -j8 UNICORN_ARCHS=mips make -j8
sudo UNICORN_ARCHS=mips make install sudo UNICORN_ARCHS=mips make install
......
...@@ -10,23 +10,24 @@ import ( ...@@ -10,23 +10,24 @@ import (
"time" "time"
) )
func LoadMappedFile(fn string, ram map[uint32](uint32)) { func LoadMappedFile(fn string, ram map[uint32](uint32), base uint32) {
dat, _ := ioutil.ReadFile(fn) dat, _ := ioutil.ReadFile(fn)
for i := 0; i < len(dat); i += 4 { for i := 0; i < len(dat); i += 4 {
ram[uint32(i)] = binary.BigEndian.Uint32(dat[i : i+4]) ram[base+uint32(i)] = binary.BigEndian.Uint32(dat[i : i+4])
} }
} }
func RunMinigeth(fn string, steps int, debug int) { func RunMinigeth(fn string, steps int, debug int) {
ram := make(map[uint32](uint32)) ram := make(map[uint32](uint32))
LoadMappedFile(fn, ram) LoadMappedFile(fn, ram, 0)
LoadMappedFile(fmt.Sprintf("/tmp/eth/%d", 13284469), ram, 0x30000000)
RunWithRam(ram, steps, debug) RunWithRam(ram, steps, debug)
} }
func runTest(fn string, steps int, debug int) (uint32, uint64) { func runTest(fn string, steps int, debug int) (uint32, uint64) {
ram := make(map[uint32](uint32)) ram := make(map[uint32](uint32))
ram[0xC000007C] = 0xDEAD0000 ram[0xC000007C] = 0xDEAD0000
LoadMappedFile(fn, ram) LoadMappedFile(fn, ram, 0)
start := time.Now() start := time.Now()
remainingGas, err := RunWithRam(ram, steps, debug) remainingGas, err := RunWithRam(ram, steps, debug)
......
package main package main
import ( import (
"encoding/binary"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"time" "time"
"github.com/ethereum/go-ethereum/common"
uc "github.com/unicorn-engine/unicorn/bindings/go/unicorn" uc "github.com/unicorn-engine/unicorn/bindings/go/unicorn"
) )
...@@ -23,6 +25,7 @@ func RegRead(u *uc.Unicorn, reg int) { ...@@ -23,6 +25,7 @@ func RegRead(u *uc.Unicorn, reg int) {
} }
// reimplement simple.py in go
func RunUnicorn(fn string) { func RunUnicorn(fn string) {
mu, err := uc.NewUnicorn(uc.ARCH_MIPS, uc.MODE_32|uc.MODE_BIG_ENDIAN) mu, err := uc.NewUnicorn(uc.ARCH_MIPS, uc.MODE_32|uc.MODE_BIG_ENDIAN)
check(err) check(err)
...@@ -33,7 +36,16 @@ func RunUnicorn(fn string) { ...@@ -33,7 +36,16 @@ func RunUnicorn(fn string) {
} }
syscall_no, _ := mu.RegRead(uc.MIPS_REG_V0) syscall_no, _ := mu.RegRead(uc.MIPS_REG_V0)
v0 := uint64(0) v0 := uint64(0)
if syscall_no == 4004 { if syscall_no == 4020 {
oracle_hash, _ := mu.MemRead(0x30001000, 0x20)
hash := common.BytesToHash(oracle_hash)
key := fmt.Sprintf("/tmp/eth/%s", hash)
value, _ := ioutil.ReadFile(key)
tmp := []byte{0, 0, 0, 0}
binary.BigEndian.PutUint32(tmp, uint32(len(value)))
mu.MemWrite(0x31000000, tmp)
mu.MemWrite(0x31000004, value)
} else if syscall_no == 4004 {
buf, _ := mu.RegRead(uc.MIPS_REG_A1) buf, _ := mu.RegRead(uc.MIPS_REG_A1)
count, _ := mu.RegRead(uc.MIPS_REG_A2) count, _ := mu.RegRead(uc.MIPS_REG_A2)
bytes, _ := mu.MemRead(buf, count) bytes, _ := mu.MemRead(buf, count)
...@@ -52,7 +64,7 @@ func RunUnicorn(fn string) { ...@@ -52,7 +64,7 @@ func RunUnicorn(fn string) {
} else if syscall_no == 4120 { } else if syscall_no == 4120 {
v0 = 1 v0 = 1
} else { } else {
fmt.Println("syscall", syscall_no) //fmt.Println("syscall", syscall_no)
} }
mu.RegWrite(uc.MIPS_REG_V0, v0) mu.RegWrite(uc.MIPS_REG_V0, v0)
mu.RegWrite(uc.MIPS_REG_A3, 0) mu.RegWrite(uc.MIPS_REG_A3, 0)
...@@ -60,7 +72,7 @@ func RunUnicorn(fn string) { ...@@ -60,7 +72,7 @@ func RunUnicorn(fn string) {
ministart := time.Now() ministart := time.Now()
mu.HookAdd(uc.HOOK_CODE, func(mu uc.Unicorn, addr uint64, size uint32) { mu.HookAdd(uc.HOOK_CODE, func(mu uc.Unicorn, addr uint64, size uint32) {
if steps%100000 == 0 { if steps%1000000 == 0 {
steps_per_sec := float64(steps) * 1e9 / float64(time.Now().Sub(ministart).Nanoseconds()) steps_per_sec := float64(steps) * 1e9 / float64(time.Now().Sub(ministart).Nanoseconds())
fmt.Printf("%6d Code: 0x%x, 0x%x steps per s %f\n", steps, addr, size, steps_per_sec) fmt.Printf("%6d Code: 0x%x, 0x%x steps per s %f\n", steps, addr, size, steps_per_sec)
} }
...@@ -69,9 +81,14 @@ func RunUnicorn(fn string) { ...@@ -69,9 +81,14 @@ func RunUnicorn(fn string) {
check(mu.MemMap(0, 0x80000000)) check(mu.MemMap(0, 0x80000000))
// program
dat, _ := ioutil.ReadFile(fn) dat, _ := ioutil.ReadFile(fn)
mu.MemWrite(0, dat) mu.MemWrite(0, dat)
// inputs
inputs, _ := ioutil.ReadFile(fmt.Sprintf("/tmp/eth/%d", 13284469))
mu.MemWrite(0x30000000, inputs)
mu.Start(0, 0xdead0000) mu.Start(0, 0xdead0000)
} }
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