6 /* TODO(bernhard): use {u,}int* types */
9 // Note by hs: my signal.h includes sys/uconctext which conflicts with
10 // asm/ucontext - this hack kinda solves the problem for me ;-)
11 // so feel free to blame me for that s**t
12 #if defined __USE_XOPEN2K8
15 #warning hs-hack: undefining __USE_XOPEN2K8 for signal.h
19 #define __USE_XOPEN2K8
22 #include <sys/ucontext.h>
24 unsigned int getMethodEntry(unsigned int, unsigned int);
25 unsigned int getStaticFieldAddr(unsigned int, void*);
26 unsigned int getTrapType(unsigned int, unsigned int);
29 #define dprintf(args...) do { printf (args); } while (0);
31 #define dprintf(args...)
34 #define NEW_MAP(prefix) \
35 void* prefix ## _map = NULL; \
36 void set_ ## prefix ## map(void *map) \
38 dprintf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \
39 prefix ## _map = map; \
41 void *get_ ## prefix ## map() \
43 dprintf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \
44 return prefix ## _map; \
53 NEW_MAP(interfacemethod)
56 void mainresult(unsigned int a)
58 dprintf("mainresult: 0x%08x\n", a);
61 void staticcalltrap(int nSignal, siginfo_t *info, void *ctx)
63 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
64 unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
65 unsigned int *to_patch = (unsigned int *) (from + 1);
66 dprintf("callertrap(mctx) by 0x%08x\n", from);
67 if (*to_patch != 0x90ffff90) {
68 dprintf("callertrap: something is wrong here. abort\n");
71 unsigned int patchme = getMethodEntry(from, 0);
73 unsigned char *insn = (unsigned char *) from;
74 *insn = 0xe8; // call opcode
75 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
76 dprintf("*to_patch: 0x%08x\n", *to_patch);
77 *to_patch = patchme - (from + 5);
78 dprintf("*to_patch: 0x%08x\n", *to_patch);
79 mctx->gregs[REG_EIP] = (unsigned long) insn;
82 void sigsegvtrap(int nSignal, siginfo_t *info, void *ctx)
84 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
85 unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
86 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
88 /* if from is not *the* eip: get actual eip from stack storage */
89 unsigned int from_stack = (*esp) - 3;
90 switch(getTrapType(from, from_stack)) {
92 dprintf("something is wrong here: abort\n");
95 case 1: { // invokevirtual
97 dprintf("from: 0x%08x but should be 0 :-(\n", from);
99 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
100 unsigned char offset = *((unsigned char *) (*esp) - 1);
101 /* method entry to patch */
102 unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
103 dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from_stack, offset);
104 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
105 dprintf("*to_patch: 0x%08x\n", *to_patch);
106 *to_patch = getMethodEntry(from_stack, method_table_ptr);
107 mctx->gregs[REG_EIP] = *to_patch;
108 dprintf("*to_patch: 0x%08x\n", *to_patch);
110 case 4: { // invokeinterface
112 dprintf("from: 0x%08x but should be 0 :-(\n", from);
114 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
115 unsigned int interface_table_ptr = (unsigned int) mctx->gregs[REG_EBX];
116 unsigned char offset = *((unsigned char *) (*esp) - 1);
117 /* interface entry to patch */
118 unsigned int *to_patch = (unsigned int*) (interface_table_ptr + offset);
119 dprintf("invokeinterface by 0x%08x with offset 0x%08x\n", from_stack, offset);
120 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
121 dprintf("*to_patch: 0x%08x\n", *to_patch);
122 *to_patch = getMethodEntry(from_stack, method_table_ptr);
123 mctx->gregs[REG_EIP] = *to_patch;
124 dprintf("*to_patch: 0x%08x\n", *to_patch);
126 case 2: { // static field patch
127 unsigned int *to_patch = (unsigned int *) (from + 2);
128 dprintf("staticfieldtrap by 0x%08x\n", from);
129 if (*to_patch != 0x00000000) {
130 dprintf("staticfieldtrap: something is wrong here. abort\n");
133 unsigned int patchme = getStaticFieldAddr(from, trap_map);
135 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
136 dprintf("*to_patch: 0x%08x\n", *to_patch);
138 dprintf("*to_patch: 0x%08x\n", *to_patch);
143 void register_signal(void)
145 struct sigaction illaction;
146 illaction.sa_sigaction = staticcalltrap;
147 sigemptyset(&illaction.sa_mask);
148 illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
149 sigaction(SIGILL, &illaction, NULL);
151 struct sigaction segvaction;
152 segvaction.sa_sigaction = sigsegvtrap;
153 sigemptyset(&segvaction.sa_mask);
154 segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
155 sigaction(SIGSEGV, &segvaction, NULL);
158 unsigned int getaddr(void)
160 return (unsigned int) mainresult;
163 unsigned int getMallocAddr(void)
165 return (unsigned int) malloc;