-
Notifications
You must be signed in to change notification settings - Fork 4
/
patch_handler.s
70 lines (62 loc) · 1.77 KB
/
patch_handler.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
.bss
gleaned_node_ret:
.align 4
.space 4*32*2
gleaned_cnt:
.align 4
.space 4
.text
.globl _patch_handler
.def _patch_handler
.scl 2
.type 32
.endef
_patch_handler:
/* Stack here: node, return address, arguments */
pushal
mov 4*8(%esp), %ebx /* struct pentry *node */
pushl 4*2(%ebx) /* node->data */
push %esp /* ctx */
mov (%ebx), %eax
test %eax, %eax
jz 1f
call *%eax /* call node->entry */
1:
add $8, %esp /* eat args */
/* Save node & orig return address in gleaned_node_ret array */
movl gleaned_cnt, %ecx
lea gleaned_node_ret(,%ecx,8), %eax
mov 4*8(%esp), %ebx /* struct pentry *node */
movl %ebx, (%eax) /* save node in gleaned_node_ret[gleaned_cnt*2] */
movl 4*8+4(%esp), %edx
movl %edx, 4(%eax) /* save return address in gleaned_node_ret[gleaned_cnt*2+1] */
incl %ecx
movl %ecx, gleaned_cnt
/* Patch the return address so the function reenters this handler when done */
movl $handle_return, 4*8+4(%esp) /* overwrite return address */
/* Call original function after restoring regs, by way of ret_code */
lea 4*3(%ebx), %ebx /* node->ret_code */
movl %ebx, 4*8(%esp) /* overwrite node with ret_code addr */
popal
ret /* jump to ret_code */
/* Function return */
handle_return:
pushl %eax /* to be overwritten with original return address */
pushal
movl gleaned_cnt, %ecx
decl %ecx
movl %ecx, gleaned_cnt
lea gleaned_node_ret(,%ecx,8), %ebx /* &gleaned_node_ret[gleaned_cnt*2] */
movl 4(%ebx), %eax /* reload saved return address */
movl %eax, 4*8(%esp) /* restore original return address */
mov (%ebx), %ebx /* node */
pushl 8(%ebx) /* node->data */
pushl %esp /* ctx */
movl 4(%ebx), %eax /* node->exit */
testl %eax, %eax
jz 3f
call *%eax /* call node->exit */
3:
addl $8, %esp /* eat args */
popal
ret /* return to original address */