-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhook.template
58 lines (47 loc) · 1.37 KB
/
hook.template
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
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#include "{{ leakname }}.provider.h"
{%- macro arglist(args, typed=True) -%}
{% for type in args -%}
{% if typed %}{{ type }}{% endif %} arg{{ loop.index0 + 2 }}{% if not loop.last %}, {% endif %}
{%- endfor %}
{%- endmacro %}
{%- macro imphook(hook) -%}
static IMP original$${{ hook.func }};
static {{ hook.rtype }}
newimp$${{ hook.func }}(id arg0, SEL arg1, {{ arglist(hook.args) }})
{
{% for probe, probeargs in hook.probes.items() %}
if ({{ probe }}_ENABLED()) {
{{ probe }}(
{% for arg in probeargs -%}
[[{{ arg }} description] UTF8String]{% if not loop.last %}, {% endif %}
{%- endfor %}
);
}
{%- endfor %}
{% if hook.rtype != "void" %}return{% endif %} original$${{ hook.func }}(arg0, arg1, {{ arglist(hook.args, typed=False) }});
}
{%- endmacro %}
{% macro impreplace(cls, method, func) %}
{
Class class = objc_getClass("{{ cls }}");
SEL selector = sel_getUid("{{ method }}");
Method method = class_getInstanceMethod(class, selector);
original$${{ func }} = method_getImplementation(method);
method_setImplementation(method, (IMP)newimp$${{ func }});
}
{%- endmacro %}
{% for hook in hooks %}
{{ imphook(hook) }}
{% endfor %}
void
hook_{{ leakname }}_apply(void)
{
{% for hook in hooks -%}
{{ impreplace(hook.cls, hook.method, hook.func) }}
{%- endfor %}
}
{#
# vim: ft=jinja cin ts=4 sw=4
#}