Go: improve hook callback speed by 30% and add a HOOK_CODE benchmark (#835)

* add x86 hook benchmark

* Go: improve hook callback speed by 30%
This commit is contained in:
Ryan Hileman
2017-05-13 10:12:57 -07:00
committed by Nguyen Anh Quynh
parent d39c20acfe
commit 4b50ca5cec
3 changed files with 71 additions and 22 deletions

View File

@@ -158,3 +158,28 @@ func TestX86Mmr(t *testing.T) {
t.Fatalf("mmr read failed: %#v", mmr)
}
}
func BenchmarkX86Hook(b *testing.B) {
// loop rax times
code := "\x48\xff\xc8\x48\x83\xf8\x00\x0f\x8f\xf3\xff\xff\xff"
mu, err := MakeUc(MODE_64, code)
if err != nil {
b.Fatal(err)
}
count := 0
mu.HookAdd(HOOK_CODE, func(_ Unicorn, addr uint64, size uint32) {
count++
}, 1, 0)
mu.RegWrite(X86_REG_RAX, uint64(b.N))
b.ResetTimer()
if err := mu.Start(ADDRESS, ADDRESS+uint64(len(code))); err != nil {
b.Fatal(err)
}
rax, _ := mu.RegRead(X86_REG_RAX)
if rax != 0 {
b.Errorf("benchmark fell short: rax (%d) != 0", rax)
}
if count != b.N*3 {
b.Fatalf("benchmark fell short: %d < %d", count, b.N)
}
}