unicorn/bindings/ruby/test_hook_gc.rb
misson20000 9cb64915c7 fix Ruby bindings (#830)
* fix mem_unmap and query for Ruby bindings

* ruby bindings: fix issues with GC freeing callbacks while we still have references to them

* ruby bindings: add test for garbage collection of hooks

* ruby bindings: let the VM garbage collect hooks properly this time

* ruby bindings: update garbage collection test to make sure Proc is garbage collected after Uc is collected

* ruby bindings: fix m_uc_hook_add to return the ruby VALUE with proper memory management instead of making another one with bad memory management

* ruby bindings: fix cb_hook_intr signature

* add architecture query

* ruby bindings: only treat certain x86 registers specially if we're actually on x86

* only treat certain x86 registers specially if we're actually on x86 (uc_reg_read and uc_reg_write)

* ruby bindings: read and write ARM64's 128-bit NEON registers
2017-05-22 20:46:30 +08:00

60 lines
1.5 KiB
Ruby

#!/usr/bin/env ruby
require 'unicorn'
require 'unicorn/x86_const'
require 'weakref'
include Unicorn
X86_CODE32 = "\x41" # INC ecx; DEC edx
# memory address where emulation starts
ADDRESS = 0x1000000
# callback for tracing instructions
hook_code = Proc.new do |uc, address, size, user_data|
puts("proc was run")
end
hook_code_weak = WeakRef.new hook_code
begin
# Initialize emulator in X86-32bit mode
mu = Uc.new UC_ARCH_X86, UC_MODE_32
# map 2MB memory for this emulation
mu.mem_map(ADDRESS, 2 * 1024 * 1024)
# write machine code to be emulated to memory
mu.mem_write(ADDRESS, X86_CODE32)
# initialize machine registers
mu.reg_write(UC_X86_REG_ECX, 0x1234)
mu.reg_write(UC_X86_REG_EDX, 0x7890)
# tracing all instructions with customized callback
mu.hook_add(UC_HOOK_CODE, hook_code)
hook_code = nil # erase reference to proc
GC.start() # force garbage collection to test if proc is garbage collected
# emulate machine code in infinite time
mu.emu_start(ADDRESS, ADDRESS + X86_CODE32.bytesize)
mu = nil # erase reference to Uc because apparently it doesn't go out of scope after this?
rescue UcError => e
puts("ERROR: %s" % e)
exit 1
rescue NoMethodError => e
puts("proc was garbage collected and we tried to invoke `call` on something strange")
exit 1
end
GC.start()
if hook_code_weak.weakref_alive?() then
puts("proc was not garbage collected")
exit 1
end
puts "test passed"
exit 0