Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
eaa6641c
Commit
eaa6641c
authored
Oct 31, 2021
by
George Hotz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor out GetHookedUnicorn
parent
c031a4d7
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
92 deletions
+45
-92
compare_unievm_test.go
mipsevm/compare_unievm_test.go
+5
-1
full.go
mipsevm/full.go
+21
-0
main.go
mipsevm/main.go
+9
-81
mips_run_test.go
mipsevm/mips_run_test.go
+1
-1
unicorn.go
mipsevm/unicorn.go
+9
-9
No files found.
mipsevm/compare_unievm_test.go
View file @
eaa6641c
...
...
@@ -49,11 +49,15 @@ func TestCompareUnicornEvm(t *testing.T) {
})
uniram
:=
make
(
map
[
uint32
](
uint32
))
go
RunUnicorn
(
fn
,
uniram
,
steps
,
true
,
false
,
func
(
step
int
,
mu
uc
.
Unicorn
,
ram
map
[
uint32
](
uint32
))
{
go
RunUnicorn
(
fn
,
uniram
,
true
,
false
,
func
(
step
int
,
mu
uc
.
Unicorn
,
ram
map
[
uint32
](
uint32
))
{
SyncRegs
(
mu
,
ram
)
cuni
<-
RegSerialize
(
ram
)
done
.
Lock
()
done
.
Unlock
()
// halt at steps
if
step
==
steps
{
mu
.
RegWrite
(
uc
.
MIPS_REG_PC
,
0x5ead0004
)
}
})
mismatch
:=
false
...
...
mipsevm/full.go
View file @
eaa6641c
package
main
import
(
"encoding/binary"
"fmt"
"io/ioutil"
"log"
...
...
@@ -78,6 +79,26 @@ func RunWithInputAndGas(interpreter *vm.EVMInterpreter, statedb *StateDB, input
return
dat
,
(
gas
-
contract
.
Gas
),
err
}
func
ZeroRegisters
(
ram
map
[
uint32
](
uint32
))
{
for
i
:=
uint32
(
0xC0000000
);
i
<
0xC0000000
+
36
*
4
;
i
+=
4
{
WriteRam
(
ram
,
i
,
0
)
}
}
func
LoadData
(
dat
[]
byte
,
ram
map
[
uint32
](
uint32
),
base
uint32
)
{
for
i
:=
0
;
i
<
len
(
dat
);
i
+=
4
{
value
:=
binary
.
BigEndian
.
Uint32
(
dat
[
i
:
i
+
4
])
if
value
!=
0
{
ram
[
base
+
uint32
(
i
)]
=
value
}
}
}
func
LoadMappedFile
(
fn
string
,
ram
map
[
uint32
](
uint32
),
base
uint32
)
{
dat
,
_
:=
ioutil
.
ReadFile
(
fn
)
LoadData
(
dat
,
ram
,
base
)
}
func
RunFull
()
{
interpreter
,
statedb
:=
GetInterpreter
(
0
,
true
)
DeployChain
(
interpreter
,
statedb
)
...
...
mipsevm/main.go
View file @
eaa6641c
package
main
import
(
"encoding/binary"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
)
import
"fmt"
func
LoadData
(
dat
[]
byte
,
ram
map
[
uint32
](
uint32
),
base
uint32
)
{
for
i
:=
0
;
i
<
len
(
dat
);
i
+=
4
{
value
:=
binary
.
BigEndian
.
Uint32
(
dat
[
i
:
i
+
4
])
if
value
!=
0
{
ram
[
base
+
uint32
(
i
)]
=
value
}
}
}
func
LoadMappedFile
(
fn
string
,
ram
map
[
uint32
](
uint32
),
base
uint32
)
{
dat
,
_
:=
ioutil
.
ReadFile
(
fn
)
LoadData
(
dat
,
ram
,
base
)
}
func
ZeroRegisters
(
ram
map
[
uint32
](
uint32
))
{
for
i
:=
uint32
(
0xC0000000
);
i
<
0xC0000000
+
36
*
4
;
i
+=
4
{
WriteRam
(
ram
,
i
,
0
)
}
}
func
RunMinigeth
(
fn
string
,
steps
int
,
debug
int
)
{
func
main
()
{
// step 1, generate the checkpoints every million steps using unicorn
ram
:=
make
(
map
[
uint32
](
uint32
))
LoadMappedFile
(
fn
,
ram
,
0
)
LoadMappedFile
(
fmt
.
Sprintf
(
"/tmp/eth/%d"
,
13284469
),
ram
,
0xB0000000
)
RunWithRam
(
ram
,
steps
,
debug
,
nil
)
}
LoadMappedFile
(
"../mipigo/minigeth"
,
ram
,
0
)
LoadMappedFile
(
fmt
.
Sprintf
(
"/tmp/eth/%d/input"
,
13284469
),
ram
,
0xB0000000
)
ZeroRegisters
(
ram
)
func
runTest
(
fn
string
,
steps
int
,
debug
int
)
(
uint32
,
uint64
)
{
ram
:=
make
(
map
[
uint32
](
uint32
))
ram
[
0xC000007C
]
=
0x5EAD0000
LoadMappedFile
(
fn
,
ram
,
0
)
// step 2 (optional), validate each 1 million chunk in EVM
start
:=
time
.
Now
()
remainingGas
,
err
:=
RunWithRam
(
ram
,
steps
,
debug
,
nil
)
elapsed
:=
time
.
Now
()
.
Sub
(
start
)
// step 3 (super optional) validate each 1 million chunk on chain
fmt
.
Println
(
err
,
remainingGas
,
elapsed
,
ram
[
0xbffffff4
],
ram
[
0xbffffff8
],
fmt
.
Sprintf
(
"%x"
,
ram
[
0xc0000080
]),
fn
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
return
ram
[
0xbffffff4
]
&
ram
[
0xbffffff8
],
remainingGas
}
//RunWithRam(ram, steps, debug, nil)
func
main
()
{
steps
,
_
:=
strconv
.
Atoi
(
os
.
Getenv
(
"STEPS"
))
if
steps
==
0
{
steps
=
1000000
}
if
len
(
os
.
Args
)
>
1
{
if
strings
.
HasPrefix
(
os
.
Args
[
1
],
"../mipigo/"
)
{
debug
,
_
:=
strconv
.
Atoi
(
os
.
Getenv
(
"DEBUG"
))
RunMinigeth
(
os
.
Args
[
1
],
steps
,
debug
)
}
else
if
os
.
Args
[
1
]
==
"unicorn"
{
uniram
:=
make
(
map
[
uint32
](
uint32
))
RunUnicorn
(
os
.
Args
[
2
],
uniram
,
steps
,
false
,
false
,
nil
)
}
else
{
runTest
(
os
.
Args
[
1
],
20
,
2
)
}
}
else
{
files
,
err
:=
ioutil
.
ReadDir
(
"test/bin"
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
good
:=
true
gas
:=
uint64
(
0
)
for
_
,
f
:=
range
files
{
ret
,
lgas
:=
runTest
(
"test/bin/"
+
f
.
Name
(),
100
,
0
)
good
=
good
&&
(
ret
==
1
)
gas
+=
lgas
}
if
!
good
{
panic
(
"some tests failed"
)
}
fmt
.
Println
(
"used"
,
gas
,
"gas"
)
}
}
mipsevm/mips_run_test.go
View file @
eaa6641c
...
...
@@ -10,7 +10,7 @@ import (
func
TestMinigethUnicorn
(
t
*
testing
.
T
)
{
uniram
:=
make
(
map
[
uint32
](
uint32
))
RunUnicorn
(
"../mipigo/minigeth.bin"
,
uniram
,
-
1
,
false
,
true
,
nil
)
RunUnicorn
(
"../mipigo/minigeth.bin"
,
uniram
,
false
,
true
,
nil
)
}
func
TestSimpleEVM
(
t
*
testing
.
T
)
{
...
...
mipsevm/unicorn.go
View file @
eaa6641c
...
...
@@ -70,13 +70,10 @@ func SyncRegs(mu uc.Unicorn, ram map[uint32](uint32)) {
WriteRam
(
ram
,
REG_HEAP
,
uint32
(
heap_start
))
}
// reimplement simple.py in go
func
RunUnicorn
(
fn
string
,
ram
map
[
uint32
](
uint32
),
totalSteps
int
,
slowMode
bool
,
checkIO
bool
,
callback
func
(
int
,
uc
.
Unicorn
,
map
[
uint32
](
uint32
)))
{
func
GetHookedUnicorn
(
root
string
,
ram
map
[
uint32
](
uint32
),
slowMode
bool
,
callback
func
(
int
,
uc
.
Unicorn
,
map
[
uint32
](
uint32
)))
uc
.
Unicorn
{
mu
,
err
:=
uc
.
NewUnicorn
(
uc
.
ARCH_MIPS
,
uc
.
MODE_32
|
uc
.
MODE_BIG_ENDIAN
)
check
(
err
)
root
:=
"/tmp/eth/13284469"
mu
.
HookAdd
(
uc
.
HOOK_INTR
,
func
(
mu
uc
.
Unicorn
,
intno
uint32
)
{
if
intno
!=
17
{
log
.
Fatal
(
"invalid interrupt "
,
intno
,
" at step "
,
steps
)
...
...
@@ -161,15 +158,18 @@ func RunUnicorn(fn string, ram map[uint32](uint32), totalSteps int, slowMode boo
if
callback
!=
nil
{
callback
(
steps
,
mu
,
ram
)
}
if
totalSteps
==
steps
{
//os.Exit(0)
// immediate exit
mu
.
RegWrite
(
uc
.
MIPS_REG_PC
,
0x5ead0004
)
}
steps
+=
1
},
0
,
0x80000000
)
}
return
mu
}
// reimplement simple.py in go
func
RunUnicorn
(
fn
string
,
ram
map
[
uint32
](
uint32
),
slowMode
bool
,
checkIO
bool
,
callback
func
(
int
,
uc
.
Unicorn
,
map
[
uint32
](
uint32
)))
{
root
:=
"/tmp/eth/13284469"
mu
:=
GetHookedUnicorn
(
root
,
ram
,
slowMode
,
callback
)
// loop forever to match EVM
//mu.MemMap(0x5ead0000, 0x1000)
//mu.MemWrite(0xdead0000, []byte{0x08, 0x10, 0x00, 0x00})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment