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*);
25 #define NEW_MAP(prefix) \
26 void* prefix ## _map = NULL; \
27 void set_ ## prefix ## map(void *map) \
29 printf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \
30 prefix ## _map = map; \
32 void *get_ ## prefix ## map() \
34 printf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \
35 return prefix ## _map; \
44 void mainresult(unsigned int a)
46 printf("mainresult: 0x%08x\n", a);
49 void callertrap(int nSignal, siginfo_t *info, void *ctx)
51 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
52 unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
53 unsigned int *to_patch = (unsigned int *) (from + 1);
54 printf("callertrap(mctx) by 0x%08x\n", from);
55 if (*to_patch != 0x90ffff90) {
56 printf("callertrap: something is wrong here. abort\n");
59 unsigned int patchme = getMethodEntry(from, 0);
61 unsigned char *insn = (unsigned char *) from;
62 *insn = 0xe8; // call opcode
63 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
64 printf("*to_patch: 0x%08x\n", *to_patch);
65 *to_patch = patchme - (from + 5);
66 printf("*to_patch: 0x%08x\n", *to_patch);
67 mctx->gregs[REG_EIP] = (unsigned long) insn;
70 void staticfieldtrap(int nSignal, siginfo_t *info, void *ctx)
72 /* TODO(bernhard): more generic and cleaner please... */
73 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
74 unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
75 if (from < 0x10000) { // invokevirtual
77 printf("from: 0x%08x but should be 0 :-(\n", from);
79 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
80 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
81 /* get actual eip from stack storage */
82 unsigned int from = (*esp) - 3;
83 unsigned char offset = *((unsigned char *) (*esp) - 1);
84 /* method entry to patch */
85 unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
86 printf("invokevirtual by 0x%08x with offset 0x%08x\n", from, offset);
87 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
88 printf("*to_patch: 0x%08x\n", *to_patch);
89 *to_patch = getMethodEntry(from, method_table_ptr);
90 mctx->gregs[REG_EIP] = *to_patch;
91 printf("*to_patch: 0x%08x\n", *to_patch);
93 unsigned int *to_patch = (unsigned int *) (from + 2);
94 printf("staticfieldtrap by 0x%08x\n", from);
95 if (*to_patch != 0x00000000) {
96 printf("staticfieldtrap: something is wrong here. abort\n");
99 unsigned int patchme = getStaticFieldAddr(from, trap_map);
101 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
102 printf("*to_patch: 0x%08x\n", *to_patch);
104 printf("*to_patch: 0x%08x\n", *to_patch);
108 void register_signal(void)
110 struct sigaction illaction;
111 illaction.sa_sigaction = callertrap;
112 sigemptyset(&illaction.sa_mask);
113 illaction.sa_flags = SA_SIGINFO | SA_RESTART;
114 sigaction(SIGILL, &illaction, NULL);
116 struct sigaction segvaction;
117 segvaction.sa_sigaction = staticfieldtrap;
118 sigemptyset(&segvaction.sa_mask);
119 segvaction.sa_flags = SA_SIGINFO | SA_RESTART;
120 sigaction(SIGSEGV, &segvaction, NULL);
123 unsigned int getaddr(void)
125 return (unsigned int) mainresult;
128 unsigned int getMallocAddr(void)
130 return (unsigned int) malloc;