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, void *, void *);
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; \
43 void mainresult(unsigned int a)
45 printf("mainresult: 0x%08x\n", a);
48 void callertrap(int nSignal, siginfo_t *info, void *ctx)
50 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
51 unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
52 unsigned int *to_patch = (unsigned int *) (from + 1);
53 printf("callertrap(mctx) by 0x%08x\n", from);
54 if (*to_patch != 0x90ffff90) {
55 printf("callertrap: something is wrong here. abort\n");
58 unsigned int patchme = getMethodEntry(from, method_map, trap_map);
60 unsigned char *insn = (unsigned char *) from;
61 *insn = 0xe8; // call opcode
62 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
63 printf("*to_patch: 0x%08x\n", *to_patch);
64 *to_patch = patchme - (from + 5);
65 printf("*to_patch: 0x%08x\n", *to_patch);
66 mctx->gregs[REG_EIP] = (unsigned long) insn;
69 void staticfieldtrap(int nSignal, siginfo_t *info, void *ctx)
71 /* TODO(bernhard): more generic and cleaner please... */
72 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
73 unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
74 if (from == 0) { // invokevirtual
75 unsigned int eax = (unsigned int) mctx->gregs[REG_EAX];
76 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
77 /* get actual eip from stack storage */
78 unsigned int from = (*esp) - 3;
79 unsigned char offset = *((unsigned char *) (*esp) - 1);
80 /* method entry to patch */
81 unsigned int *to_patch = (unsigned int*) (eax + offset);
82 printf("invokevirtual by 0x%08x with offset 0x%08x\n", from, offset);
83 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
84 printf("*to_patch: 0x%08x\n", *to_patch);
85 *to_patch = getMethodEntry(from, method_map, trap_map);
86 mctx->gregs[REG_EIP] = *to_patch;
87 printf("*to_patch: 0x%08x\n", *to_patch);
89 unsigned int *to_patch = (unsigned int *) (from + 2);
90 printf("staticfieldtrap by 0x%08x\n", from);
91 if (*to_patch != 0x00000000) {
92 printf("staticfieldtrap: something is wrong here. abort\n");
95 unsigned int patchme = getStaticFieldAddr(from, trap_map);
97 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
98 printf("*to_patch: 0x%08x\n", *to_patch);
100 printf("*to_patch: 0x%08x\n", *to_patch);
104 void register_signal(void)
106 struct sigaction illaction;
107 illaction.sa_sigaction = callertrap;
108 sigemptyset(&illaction.sa_mask);
109 illaction.sa_flags = SA_SIGINFO | SA_RESTART;
110 sigaction(SIGILL, &illaction, NULL);
112 struct sigaction segvaction;
113 segvaction.sa_sigaction = staticfieldtrap;
114 sigemptyset(&segvaction.sa_mask);
115 segvaction.sa_flags = SA_SIGINFO | SA_RESTART;
116 sigaction(SIGSEGV, &segvaction, NULL);
119 unsigned int getaddr(void)
121 return (unsigned int) mainresult;
124 unsigned int getMallocAddr(void)
126 return (unsigned int) malloc;