4 /* TODO(bernhard): use {u,}int* types */
7 // Note by hs: my signal.h includes sys/uconctext which conflicts with
8 // asm/ucontext - this hack kinda solves the problem for me ;-)
9 // so feel free to blame me for that s**t
10 #if defined __USE_XOPEN2K8
13 #warning hs-hack: undefining __USE_XOPEN2K8 for signal.h
17 #define __USE_XOPEN2K8
20 #include <sys/ucontext.h>
22 unsigned int getMethodEntry(unsigned int, unsigned int);
23 unsigned int getStaticFieldAddr(unsigned int, void*);
24 unsigned int getTrapType(unsigned int, unsigned int);
27 #define dprintf(args...) do { printf (args); } while (0);
29 #define dprintf(args...)
32 #define NEW_MAP(prefix) \
33 void* prefix ## _map = NULL; \
34 void set_ ## prefix ## map(void *map) \
36 dprintf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \
37 prefix ## _map = map; \
39 void *get_ ## prefix ## map() \
41 dprintf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \
42 return prefix ## _map; \
51 NEW_MAP(interfacemethod)
54 void mainresult(unsigned int a)
56 dprintf("mainresult: 0x%08x\n", a);
59 void staticcalltrap(int nSignal, siginfo_t *info, void *ctx)
61 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
62 unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
63 unsigned int *to_patch = (unsigned int *) (from + 1);
64 dprintf("callertrap(mctx) by 0x%08x\n", from);
65 if (*to_patch != 0x90ffff90) {
66 dprintf("callertrap: something is wrong here. abort\n");
69 unsigned int patchme = getMethodEntry(from, 0);
71 unsigned char *insn = (unsigned char *) from;
72 *insn = 0xe8; // call opcode
73 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
74 dprintf("*to_patch: 0x%08x\n", *to_patch);
75 *to_patch = patchme - (from + 5);
76 dprintf("*to_patch: 0x%08x\n", *to_patch);
77 mctx->gregs[REG_EIP] = (unsigned long) insn;
80 void sigsegvtrap(int nSignal, siginfo_t *info, void *ctx)
82 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
83 unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
84 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
86 /* if from is not *the* eip: get actual eip from stack storage */
87 unsigned int from_stack = (*esp) - 3;
88 switch(getTrapType(from, from_stack)) {
90 dprintf("something is wrong here: abort\n");
93 case 1: { // invokevirtual
95 dprintf("from: 0x%08x but should be 0 :-(\n", from);
97 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
98 unsigned char offset = *((unsigned char *) (*esp) - 1);
99 /* method entry to patch */
100 unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
101 dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from_stack, offset);
102 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
103 dprintf("*to_patch: 0x%08x\n", *to_patch);
104 *to_patch = getMethodEntry(from_stack, method_table_ptr);
105 mctx->gregs[REG_EIP] = *to_patch;
106 dprintf("*to_patch: 0x%08x\n", *to_patch);
108 case 4: { // invokeinterface
110 dprintf("from: 0x%08x but should be 0 :-(\n", from);
112 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
113 unsigned int interface_table_ptr = (unsigned int) mctx->gregs[REG_EBX];
114 unsigned char offset = *((unsigned char *) (*esp) - 1);
115 /* interface entry to patch */
116 unsigned int *to_patch = (unsigned int*) (interface_table_ptr + offset);
117 dprintf("invokeinterface by 0x%08x with offset 0x%08x\n", from_stack, offset);
118 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
119 dprintf("*to_patch: 0x%08x\n", *to_patch);
120 *to_patch = getMethodEntry(from_stack, method_table_ptr);
121 mctx->gregs[REG_EIP] = *to_patch;
122 dprintf("*to_patch: 0x%08x\n", *to_patch);
124 case 2: { // static field patch
125 unsigned int *to_patch = (unsigned int *) (from + 2);
126 dprintf("staticfieldtrap by 0x%08x\n", from);
127 if (*to_patch != 0x00000000) {
128 dprintf("staticfieldtrap: something is wrong here. abort\n");
131 unsigned int patchme = getStaticFieldAddr(from, trap_map);
133 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
134 dprintf("*to_patch: 0x%08x\n", *to_patch);
136 dprintf("*to_patch: 0x%08x\n", *to_patch);
141 void register_signal(void)
143 struct sigaction illaction;
144 illaction.sa_sigaction = staticcalltrap;
145 sigemptyset(&illaction.sa_mask);
146 illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
147 sigaction(SIGILL, &illaction, NULL);
149 struct sigaction segvaction;
150 segvaction.sa_sigaction = sigsegvtrap;
151 sigemptyset(&segvaction.sa_mask);
152 segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
153 sigaction(SIGSEGV, &segvaction, NULL);
156 unsigned int getaddr(void)
158 return (unsigned int) mainresult;
161 unsigned int getMallocAddr(void)
163 return (unsigned int) malloc;