From 4f1c88e70c23c34e54e96cbdcdd9da08ebcdefbf Mon Sep 17 00:00:00 2001 From: Ryan Hileman Date: Sun, 28 Feb 2016 12:00:58 -0800 Subject: [PATCH] Go: add RegReadMmr and test --- bindings/go/unicorn/unicorn.go | 1 + bindings/go/unicorn/x86.go | 12 ++++++++++++ bindings/go/unicorn/x86_test.go | 15 +++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/bindings/go/unicorn/unicorn.go b/bindings/go/unicorn/unicorn.go index ecaa6336..2b0e456c 100644 --- a/bindings/go/unicorn/unicorn.go +++ b/bindings/go/unicorn/unicorn.go @@ -42,6 +42,7 @@ type Unicorn interface { MemWrite(addr uint64, data []byte) error RegRead(reg int) (uint64, error) RegWrite(reg int, value uint64) error + RegReadMmr(reg int) (*X86Mmr, error) RegWriteMmr(reg int, value *X86Mmr) error Start(begin, until uint64) error StartWithOptions(begin, until uint64, options *UcOptions) error diff --git a/bindings/go/unicorn/x86.go b/bindings/go/unicorn/x86.go index 490f7dfa..9097de2a 100644 --- a/bindings/go/unicorn/x86.go +++ b/bindings/go/unicorn/x86.go @@ -24,3 +24,15 @@ func (u *uc) RegWriteMmr(reg int, value *X86Mmr) error { ucerr := C.uc_reg_write(u.handle, C.int(reg), unsafe.Pointer(&val)) return errReturn(ucerr) } + +func (u *uc) RegReadMmr(reg int) (*X86Mmr, error) { + var val C.uc_x86_mmr + ucerr := C.uc_reg_read(u.handle, C.int(reg), unsafe.Pointer(&val)) + ret := &X86Mmr{ + Selector: uint16(val.selector), + Base: uint64(val.base), + Limit: uint32(val.limit), + Flags: uint32(val.flags), + } + return ret, errReturn(ucerr) +} diff --git a/bindings/go/unicorn/x86_test.go b/bindings/go/unicorn/x86_test.go index aca1d3be..c74a0611 100644 --- a/bindings/go/unicorn/x86_test.go +++ b/bindings/go/unicorn/x86_test.go @@ -143,3 +143,18 @@ func TestX86Syscall(t *testing.T) { t.Fatal("Incorrect syscall return value.") } } + +func TestX86Mmr(t *testing.T) { + mu, err := MakeUc(MODE_64, "") + if err != nil { + t.Fatal(err) + } + err = mu.RegWriteMmr(X86_REG_GDTR, &X86Mmr{Selector: 0, Base: 0x1000, Limit: 0x1fff, Flags: 0}) + if err != nil { + t.Fatal(err) + } + mmr, err := mu.RegReadMmr(X86_REG_GDTR) + if mmr.Selector != 0 || mmr.Base != 0x1000 || mmr.Limit != 0x1fff || mmr.Flags != 0 { + t.Fatalf("mmr read failed: %#v", mmr) + } +}