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; \
45 void mainresult(unsigned int a)
47 printf("mainresult: 0x%08x\n", a);
50 void callertrap(int nSignal, siginfo_t *info, void *ctx)
52 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
53 unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
54 unsigned int *to_patch = (unsigned int *) (from + 1);
55 printf("callertrap(mctx) by 0x%08x\n", from);
56 if (*to_patch != 0x90ffff90) {
57 printf("callertrap: something is wrong here. abort\n");
60 unsigned int patchme = getMethodEntry(from, 0);
62 unsigned char *insn = (unsigned char *) from;
63 *insn = 0xe8; // call opcode
64 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
65 printf("*to_patch: 0x%08x\n", *to_patch);
66 *to_patch = patchme - (from + 5);
67 printf("*to_patch: 0x%08x\n", *to_patch);
68 mctx->gregs[REG_EIP] = (unsigned long) insn;
71 void staticfieldtrap(int nSignal, siginfo_t *info, void *ctx)
73 /* TODO(bernhard): more generic and cleaner please... */
74 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
75 unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
76 if (from < 0x10000) { // invokevirtual
78 printf("from: 0x%08x but should be 0 :-(\n", from);
80 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
81 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
82 /* get actual eip from stack storage */
83 unsigned int from = (*esp) - 3;
84 unsigned char offset = *((unsigned char *) (*esp) - 1);
85 /* method entry to patch */
86 unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
87 printf("invokevirtual by 0x%08x with offset 0x%08x\n", from, offset);
88 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
89 printf("*to_patch: 0x%08x\n", *to_patch);
90 *to_patch = getMethodEntry(from, method_table_ptr);
91 mctx->gregs[REG_EIP] = *to_patch;
92 printf("*to_patch: 0x%08x\n", *to_patch);
94 unsigned int *to_patch = (unsigned int *) (from + 2);
95 printf("staticfieldtrap by 0x%08x\n", from);
96 if (*to_patch != 0x00000000) {
97 printf("staticfieldtrap: something is wrong here. abort\n");
100 unsigned int patchme = getStaticFieldAddr(from, trap_map);
102 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
103 printf("*to_patch: 0x%08x\n", *to_patch);
105 printf("*to_patch: 0x%08x\n", *to_patch);
109 void register_signal(void)
111 struct sigaction illaction;
112 illaction.sa_sigaction = callertrap;
113 sigemptyset(&illaction.sa_mask);
114 illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
115 sigaction(SIGILL, &illaction, NULL);
117 struct sigaction segvaction;
118 segvaction.sa_sigaction = staticfieldtrap;
119 sigemptyset(&segvaction.sa_mask);
120 segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
121 sigaction(SIGSEGV, &segvaction, NULL);
124 unsigned int getaddr(void)
126 return (unsigned int) mainresult;
129 unsigned int getMallocAddr(void)
131 return (unsigned int) malloc;