rallhook 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +3 -0
- data/CHANGELOG +82 -0
- data/README +207 -0
- data/Rakefile +49 -0
- data/TODO +8 -0
- data/examples/hook/example1.rb +19 -0
- data/examples/hook/example2.rb +30 -0
- data/examples/hook/example3.rb +24 -0
- data/examples/hook/example4.rb +18 -0
- data/examples/hook/intercept.rb +41 -0
- data/examples/hook/intercept2.rb +49 -0
- data/examples/hook/redirect.rb +55 -0
- data/examples/hook/redirect_inherited.rb +28 -0
- data/examples/hook/shadow.rb +44 -0
- data/examples/instrospection/main.rb +13 -0
- data/examples/instrospection/source1.rb +4 -0
- data/examples/instrospection/source2.rb +4 -0
- data/ext/rallhook_base/distorm.h +401 -0
- data/ext/rallhook_base/extconf.rb +21 -0
- data/ext/rallhook_base/hook.c +165 -0
- data/ext/rallhook_base/hook.h +30 -0
- data/ext/rallhook_base/hook_rb_call.c +88 -0
- data/ext/rallhook_base/hook_rb_call.h +33 -0
- data/ext/rallhook_base/method_node.c +212 -0
- data/ext/rallhook_base/method_node.h +27 -0
- data/ext/rallhook_base/node_defs.h +294 -0
- data/ext/rallhook_base/rallhook.c +396 -0
- data/ext/rallhook_base/rb_call_fake.c +398 -0
- data/ext/rallhook_base/rb_call_fake.h +138 -0
- data/ext/rallhook_base/restrict_def.c +176 -0
- data/ext/rallhook_base/restrict_def.h +37 -0
- data/ext/rallhook_base/ruby_redirect.c +122 -0
- data/ext/rallhook_base/ruby_redirect.h +33 -0
- data/ext/rallhook_base/ruby_symbols.c +43 -0
- data/ext/rallhook_base/ruby_symbols.h +28 -0
- data/ext/rallhook_base/ruby_version.h +21 -0
- data/lib/rallhook/thread_hook.rb +37 -0
- data/lib/rallhook.rb +384 -0
- data/test/basic_proc.rb +45 -0
- data/test/integrity/test_array.rb +42 -0
- data/test/integrity/test_binding.rb +26 -0
- data/test/integrity/test_block.rb +37 -0
- data/test/integrity/test_call.rb +1 -0
- data/test/integrity/test_class_methods.rb +1 -0
- data/test/integrity/test_exception.rb +1 -0
- data/test/integrity/test_super.rb +34 -0
- data/test/introspection/test_call.rb +29 -0
- data/test/introspection/test_class_method.rb +41 -0
- data/test/introspection/test_file.rb +15 -0
- metadata +113 -0
data/test/basic_proc.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "rallhook"
|
3
|
+
|
4
|
+
class BasicHookProc
|
5
|
+
|
6
|
+
include RallHook::Helper
|
7
|
+
|
8
|
+
class BasicMethodWrapper < MethodWrapper
|
9
|
+
|
10
|
+
def call(*args)
|
11
|
+
if block_given?
|
12
|
+
original_call(*args) do |*x|
|
13
|
+
yield(*x)
|
14
|
+
end
|
15
|
+
else
|
16
|
+
original_call(*args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def handle_method(klass,recv_,m, method_id)
|
22
|
+
return BasicMethodWrapper.redirect_handler(klass,recv_,m,method_id)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module BasicHookProcTest
|
27
|
+
def hook
|
28
|
+
rallhook = RallHook::Hook
|
29
|
+
basichook = BasicHookProc.new
|
30
|
+
|
31
|
+
rallhook.hook basichook do
|
32
|
+
yield
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def oracle_hook_assert_equal
|
37
|
+
result = nil
|
38
|
+
hook do
|
39
|
+
result = yield
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_equal result, yield
|
43
|
+
end
|
44
|
+
alias oracle_hook_assert oracle_hook_assert_equal
|
45
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "test/basic_proc"
|
3
|
+
|
4
|
+
class TestArray < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include BasicHookProcTest
|
7
|
+
|
8
|
+
|
9
|
+
def test_array_basic
|
10
|
+
oracle_hook_assert { [1,2,3,4] }
|
11
|
+
end
|
12
|
+
|
13
|
+
def f0(input)
|
14
|
+
input.select{|i| i%2==0}
|
15
|
+
end
|
16
|
+
def f1(input)
|
17
|
+
input.map{|i| i*5+2}
|
18
|
+
end
|
19
|
+
def f2(input)
|
20
|
+
input.inject{|x,y| x*y}
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_array_blocks
|
24
|
+
|
25
|
+
# oracle testing, the result of the method out of hook must be the same in the hook
|
26
|
+
25.times do
|
27
|
+
ary = [rand(20),rand(20),rand(20),rand(20),rand(20)]
|
28
|
+
oracle_hook_assert do
|
29
|
+
f0(ary)
|
30
|
+
end
|
31
|
+
oracle_hook_assert do
|
32
|
+
f1(ary)
|
33
|
+
end
|
34
|
+
oracle_hook_assert do
|
35
|
+
f2(ary)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "test/basic_proc"
|
3
|
+
|
4
|
+
class TestBinding < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include BasicHookProcTest
|
7
|
+
|
8
|
+
def generate_binding
|
9
|
+
a = 1
|
10
|
+
b = 2
|
11
|
+
c = 3
|
12
|
+
|
13
|
+
binding
|
14
|
+
end
|
15
|
+
|
16
|
+
def f0()
|
17
|
+
eval("a+b+c", generate_binding)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_array_blocks
|
21
|
+
oracle_hook_assert do
|
22
|
+
f0
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "test/basic_proc"
|
3
|
+
|
4
|
+
class TestBlock < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include BasicHookProcTest
|
7
|
+
|
8
|
+
def block_func(*a)
|
9
|
+
yield(*a)
|
10
|
+
end
|
11
|
+
|
12
|
+
def f0(test_args)
|
13
|
+
|
14
|
+
result = nil
|
15
|
+
block_func(*test_args) do |*args|
|
16
|
+
result = args
|
17
|
+
end
|
18
|
+
|
19
|
+
result
|
20
|
+
end
|
21
|
+
|
22
|
+
def _test_block_passing(*test_args)
|
23
|
+
# oracle testing, the result with hook and without hook must be the same
|
24
|
+
oracle_hook_assert do f0(test_args) end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_block
|
28
|
+
_test_block_passing(1)
|
29
|
+
_test_block_passing(1,2)
|
30
|
+
_test_block_passing("3333")
|
31
|
+
_test_block_passing("3333",9999, 1.32)
|
32
|
+
_test_block_passing([1,2,3])
|
33
|
+
_test_block_passing([1])
|
34
|
+
_test_block_passing([[1]])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "test/basic_proc"
|
3
|
+
|
4
|
+
class TestSuper < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include BasicHookProcTest
|
7
|
+
|
8
|
+
class X
|
9
|
+
def foo
|
10
|
+
[1]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
class Y < X
|
14
|
+
def foo
|
15
|
+
super+[2]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
class Z < Y
|
19
|
+
def foo
|
20
|
+
super+[3]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def f0(klass)
|
25
|
+
z = klass.new
|
26
|
+
z.foo
|
27
|
+
end
|
28
|
+
def test_basic
|
29
|
+
oracle_hook_assert_equal do f0(X) end
|
30
|
+
oracle_hook_assert_equal do f0(Y) end
|
31
|
+
oracle_hook_assert_equal do f0(Z) end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "test/basic_proc"
|
3
|
+
|
4
|
+
class InstrospectionTestCall < Test::Unit::TestCase
|
5
|
+
|
6
|
+
class X
|
7
|
+
def foo
|
8
|
+
"fooX"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Y < X
|
13
|
+
def foo
|
14
|
+
"fooY"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_file
|
19
|
+
y = Y.new
|
20
|
+
|
21
|
+
assert_equal y.specific_method(:foo).call, "fooY"
|
22
|
+
assert_equal y.specific_method(X,:foo).call, "fooX"
|
23
|
+
assert_equal y.specific_method(Y,:foo).call, "fooY"
|
24
|
+
|
25
|
+
assert_raise NameError do
|
26
|
+
y.specific_method(Fixnum,:foo)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "test/basic_proc"
|
3
|
+
|
4
|
+
|
5
|
+
class TestClassMethod < Test::Unit::TestCase
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :global_binding
|
9
|
+
end
|
10
|
+
|
11
|
+
TestClassMethod.global_binding = binding
|
12
|
+
|
13
|
+
class X
|
14
|
+
def self.foo
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class MethodHandler < RallHook::HookHandler
|
19
|
+
def handle_method (klass,self_,m,method_id)
|
20
|
+
if m
|
21
|
+
file = klass.instance_method(m).body.file
|
22
|
+
end
|
23
|
+
|
24
|
+
nil # do nothing
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def test_file
|
30
|
+
assert_nothing_raised do
|
31
|
+
MethodHandler.hook do
|
32
|
+
eval( "
|
33
|
+
class Y < X
|
34
|
+
end
|
35
|
+
Y.foo
|
36
|
+
", TestClassMethod.global_binding)
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "test/basic_proc"
|
3
|
+
|
4
|
+
class InstrospectionTestFile < Test::Unit::TestCase
|
5
|
+
def _test_file(name)
|
6
|
+
eval("def xmethod; end", binding, name)
|
7
|
+
assert_equal self.method(:xmethod).body.file, name
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_file
|
11
|
+
10.times do
|
12
|
+
_test_file("xxx" + rand(999).to_s)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rallhook
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dario Seminara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-06-20 00:00:00 -03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ruby-cymbol
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.1.0
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: robertodarioseminara@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions:
|
30
|
+
- ext/rallhook_base/extconf.rb
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README
|
33
|
+
files:
|
34
|
+
- examples/hook/shadow.rb
|
35
|
+
- examples/hook/intercept.rb
|
36
|
+
- examples/hook/redirect.rb
|
37
|
+
- examples/hook/example1.rb
|
38
|
+
- examples/hook/example3.rb
|
39
|
+
- examples/hook/example4.rb
|
40
|
+
- examples/hook/intercept2.rb
|
41
|
+
- examples/hook/redirect_inherited.rb
|
42
|
+
- examples/hook/example2.rb
|
43
|
+
- examples/instrospection/source1.rb
|
44
|
+
- examples/instrospection/main.rb
|
45
|
+
- examples/instrospection/source2.rb
|
46
|
+
- lib/rallhook.rb
|
47
|
+
- lib/rallhook/thread_hook.rb
|
48
|
+
- test/integrity/test_exception.rb
|
49
|
+
- test/integrity/test_call.rb
|
50
|
+
- test/integrity/test_block.rb
|
51
|
+
- test/integrity/test_class_methods.rb
|
52
|
+
- test/integrity/test_binding.rb
|
53
|
+
- test/integrity/test_super.rb
|
54
|
+
- test/integrity/test_array.rb
|
55
|
+
- test/basic_proc.rb
|
56
|
+
- test/introspection/test_call.rb
|
57
|
+
- test/introspection/test_file.rb
|
58
|
+
- test/introspection/test_class_method.rb
|
59
|
+
- ext/rallhook_base/ruby_symbols.c
|
60
|
+
- ext/rallhook_base/rb_call_fake.c
|
61
|
+
- ext/rallhook_base/ruby_redirect.c
|
62
|
+
- ext/rallhook_base/hook.c
|
63
|
+
- ext/rallhook_base/rallhook.c
|
64
|
+
- ext/rallhook_base/restrict_def.c
|
65
|
+
- ext/rallhook_base/hook_rb_call.c
|
66
|
+
- ext/rallhook_base/method_node.c
|
67
|
+
- ext/rallhook_base/hook.h
|
68
|
+
- ext/rallhook_base/ruby_version.h
|
69
|
+
- ext/rallhook_base/node_defs.h
|
70
|
+
- ext/rallhook_base/hook_rb_call.h
|
71
|
+
- ext/rallhook_base/ruby_symbols.h
|
72
|
+
- ext/rallhook_base/ruby_redirect.h
|
73
|
+
- ext/rallhook_base/restrict_def.h
|
74
|
+
- ext/rallhook_base/rb_call_fake.h
|
75
|
+
- ext/rallhook_base/distorm.h
|
76
|
+
- ext/rallhook_base/method_node.h
|
77
|
+
- ext/rallhook_base/extconf.rb
|
78
|
+
- AUTHORS
|
79
|
+
- CHANGELOG
|
80
|
+
- README
|
81
|
+
- Rakefile
|
82
|
+
- TODO
|
83
|
+
has_rdoc: true
|
84
|
+
homepage: http://github.com/tario/rallhook
|
85
|
+
licenses: []
|
86
|
+
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options:
|
89
|
+
- --main
|
90
|
+
- README
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: "0"
|
98
|
+
version:
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: "0"
|
104
|
+
version:
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.3.5
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: Allow hooking of all method invocations transparently to control and / or monitor the behavior of a ruby program
|
112
|
+
test_files: []
|
113
|
+
|