mirror of
https://github.com/yuzu-emu/unicorn
synced 2024-11-24 09:48:21 +00:00
Go: add context api (#668)
This commit is contained in:
parent
19028f41f6
commit
64f4692c22
3 changed files with 57 additions and 0 deletions
28
bindings/go/unicorn/context.go
Normal file
28
bindings/go/unicorn/context.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package unicorn
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// #include <unicorn/unicorn.h>
|
||||
import "C"
|
||||
|
||||
type Context **C.uc_context
|
||||
|
||||
func (u *uc) ContextSave(reuse Context) (Context, error) {
|
||||
ctx := reuse
|
||||
if ctx == nil {
|
||||
ctx = new(*C.uc_context)
|
||||
}
|
||||
if err := errReturn(C.uc_context_alloc(u.handle, ctx)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runtime.SetFinalizer(ctx, func(p Context) { C.uc_context_free(*p) })
|
||||
if err := errReturn(C.uc_context_save(u.handle, *ctx)); err != nil {
|
||||
}
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (u *uc) ContextRestore(ctx Context) error {
|
||||
return errReturn(C.uc_context_restore(u.handle, *ctx))
|
||||
}
|
26
bindings/go/unicorn/context_test.go
Normal file
26
bindings/go/unicorn/context_test.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package unicorn
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestContext(t *testing.T) {
|
||||
u, err := NewUnicorn(ARCH_X86, MODE_32)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u.RegWrite(X86_REG_EBP, 100)
|
||||
ctx, err := u.ContextSave(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u.RegWrite(X86_REG_EBP, 200)
|
||||
err = u.ContextRestore(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
val, _ := u.RegRead(X86_REG_EBP)
|
||||
if val != 100 {
|
||||
t.Fatal("context restore failed")
|
||||
}
|
||||
}
|
|
@ -55,6 +55,9 @@ type Unicorn interface {
|
|||
HookDel(hook Hook) error
|
||||
Query(queryType int) (uint64, error)
|
||||
Close() error
|
||||
|
||||
ContextSave(reuse Context) (Context, error)
|
||||
ContextRestore(Context) error
|
||||
}
|
||||
|
||||
type uc struct {
|
||||
|
|
Loading…
Reference in a new issue