Go: add MemRegions

This commit is contained in:
Ryan Hileman 2016-02-27 11:10:15 -08:00
parent 74f783a274
commit 9f1603c157
2 changed files with 47 additions and 0 deletions

View file

@ -25,12 +25,18 @@ func errReturn(err C.uc_err) error {
return nil return nil
} }
type MemRegion struct {
Begin, End uint64
Prot int
}
type Unicorn interface { type Unicorn interface {
MemMap(addr, size uint64) error MemMap(addr, size uint64) error
MemMapProt(addr, size uint64, prot int) error MemMapProt(addr, size uint64, prot int) error
MemMapPtr(addr, size uint64, prot int, ptr unsafe.Pointer) error MemMapPtr(addr, size uint64, prot int, ptr unsafe.Pointer) error
MemProtect(addr, size uint64, prot int) error MemProtect(addr, size uint64, prot int) error
MemUnmap(addr, size uint64) error MemUnmap(addr, size uint64) error
MemRegions() ([]*MemRegion, error)
MemRead(addr, size uint64) ([]byte, error) MemRead(addr, size uint64) ([]byte, error)
MemReadInto(dst []byte, addr uint64) error MemReadInto(dst []byte, addr uint64) error
MemWrite(addr uint64, data []byte) error MemWrite(addr uint64, data []byte) error
@ -104,6 +110,25 @@ func (u *uc) RegRead(reg int) (uint64, error) {
return uint64(val), errReturn(ucerr) return uint64(val), errReturn(ucerr)
} }
func (u *uc) MemRegions() ([]*MemRegion, error) {
var regions *C.struct_uc_mem_region
var count C.uint32_t
ucerr := C.uc_mem_regions(u.handle, &regions, &count)
if ucerr != C.UC_ERR_OK {
return nil, errReturn(ucerr)
}
ret := make([]*MemRegion, count)
tmp := (*[1 << 30]C.struct_uc_mem_region)(unsafe.Pointer(regions))[:count]
for i, v := range tmp {
ret[i] = &MemRegion{
Begin: uint64(v.begin),
End: uint64(v.end),
Prot: int(v.perms),
}
}
return ret, nil
}
func (u *uc) MemWrite(addr uint64, data []byte) error { func (u *uc) MemWrite(addr uint64, data []byte) error {
if len(data) == 0 { if len(data) == 0 {
return nil return nil

View file

@ -37,3 +37,25 @@ func TestDoubleClose(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestMemRegions(t *testing.T) {
mu, err := NewUnicorn(ARCH_X86, MODE_32)
if err != nil {
t.Fatal(err)
}
err = mu.MemMap(0x1000, 0x1000)
if err != nil {
t.Fatal(err)
}
regions, err := mu.MemRegions()
if err != nil {
t.Fatal(err)
}
if len(regions) != 1 {
t.Fatalf("returned wrong number of regions: %d != 1", len(regions))
}
r := regions[0]
if r.Begin != 0x1000 || r.End != 0x1fff || r.Prot != 7 {
t.Fatalf("incorrect region: %#v", r)
}
}