regen config
[binutils-gdb.git] / gprofng / libcollector / unwind.c
1 /* Copyright (C) 2021-2023 Free Software Foundation, Inc.
2 Contributed by Oracle.
3
4 This file is part of GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "config.h"
22 #include <alloca.h>
23 #include <dlfcn.h>
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <pthread.h>
28
29 #include "gp-defs.h"
30 #include "collector.h"
31 #include "gp-experiment.h"
32 #include "memmgr.h"
33 #include "tsd.h"
34
35 /* Get dynamic module interface*/
36 #include "collector_module.h"
37
38 /* Get definitions for SP_LEAF_CHECK_MARKER, SP_TRUNC_STACK_MARKER */
39 #include "data_pckts.h"
40
41 #if ARCH(SPARC)
42 struct frame
43 {
44 long fr_local[8]; /* saved locals */
45 long fr_arg[6]; /* saved arguments [0 - 5] */
46 struct frame *fr_savfp; /* saved frame pointer */
47 long fr_savpc; /* saved program counter */
48 #if WSIZE(32)
49 char *fr_stret; /* struct return addr */
50 #endif
51 long fr_argd[6]; /* arg dump area */
52 long fr_argx[1]; /* array of args past the sixth */
53 };
54
55 #elif ARCH(Intel)
56 struct frame
57 {
58 unsigned long fr_savfp;
59 unsigned long fr_savpc;
60 };
61 #endif
62
63 /* Set the debug trace level */
64 #define DBG_LT0 0
65 #define DBG_LT1 1
66 #define DBG_LT2 2
67 #define DBG_LT3 3
68
69 int (*__collector_VM_ReadByteInstruction)(unsigned char *) = NULL;
70 #define VM_NO_ACCESS (-1)
71 #define VM_NOT_VM_MEMORY (-2)
72 #define VM_NOT_X_SEGMENT (-3)
73
74 #define isInside(p, bgn, end) ((p) >= (bgn) && (p) < (end))
75
76 /*
77 * Weed through all the arch dependent stuff to get the right definition
78 * for 'pc' in the ucontext structure. The system header files are mess
79 * dealing with all the arch (just look for PC, R_PC, REG_PC).
80 *
81 */
82
83 #if ARCH(SPARC)
84
85 #define IN_BARRIER(x) \
86 ( barrier_hdl && \
87 (unsigned long)x >= barrier_hdl && \
88 (unsigned long)x < barrier_hdlx )
89 static unsigned long barrier_hdl = 0;
90 static unsigned long barrier_hdlx = 0;
91
92 #if WSIZE(64)
93 #define STACK_BIAS 2047
94 #define IN_TRAP_HANDLER(x) \
95 ( misalign_hdl && \
96 (unsigned long)x >= misalign_hdl && \
97 (unsigned long)x < misalign_hdlx )
98 static unsigned long misalign_hdl = 0;
99 static unsigned long misalign_hdlx = 0;
100 #elif WSIZE(32)
101 #define STACK_BIAS 0
102 #endif
103
104 #if WSIZE(64)
105 #define GET_GREG(ctx,reg) (((ucontext_t*)ctx)->uc_mcontext.mc_gregs[(reg)])
106 #define GET_SP(ctx) (((ucontext_t*)ctx)->uc_mcontext.mc_gregs[MC_O6])
107 #define GET_PC(ctx) (((ucontext_t*)ctx)->uc_mcontext.mc_gregs[MC_PC])
108 #else
109 #define GET_GREG(ctx,reg) (((ucontext_t*)ctx)->uc_mcontext.gregs[(reg)])
110 #define GET_SP(ctx) (((ucontext_t*)ctx)->uc_mcontext.gregs[REG_O6])
111 #define GET_PC(ctx) (((ucontext_t*)ctx)->uc_mcontext.gregs[REG_PC])
112 #endif
113
114 #elif ARCH(Intel)
115 #include "opcodes/disassemble.h"
116
117 static int
118 fprintf_func (void *arg ATTRIBUTE_UNUSED, const char *fmt ATTRIBUTE_UNUSED, ...)
119 {
120 return 0;
121 }
122
123 static int
124 fprintf_styled_func (void *arg ATTRIBUTE_UNUSED,
125 enum disassembler_style st ATTRIBUTE_UNUSED,
126 const char *fmt ATTRIBUTE_UNUSED, ...)
127 {
128 return 0;
129 }
130
131 /* Get LENGTH bytes from info's buffer, at target address memaddr.
132 Transfer them to myaddr. */
133 static int
134 read_memory_func (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
135 disassemble_info *info)
136 {
137 unsigned int opb = info->octets_per_byte;
138 size_t end_addr_offset = length / opb;
139 size_t max_addr_offset = info->buffer_length / opb;
140 size_t octets = (memaddr - info->buffer_vma) * opb;
141 if (memaddr < info->buffer_vma
142 || memaddr - info->buffer_vma > max_addr_offset
143 || memaddr - info->buffer_vma + end_addr_offset > max_addr_offset
144 || (info->stop_vma && (memaddr >= info->stop_vma
145 || memaddr + end_addr_offset > info->stop_vma)))
146 return -1;
147 memcpy (myaddr, info->buffer + octets, length);
148 return 0;
149 }
150
151 static void
152 print_address_func (bfd_vma addr ATTRIBUTE_UNUSED,
153 disassemble_info *info ATTRIBUTE_UNUSED) { }
154
155 static asymbol *
156 symbol_at_address_func (bfd_vma addr ATTRIBUTE_UNUSED,
157 disassemble_info *info ATTRIBUTE_UNUSED)
158 {
159 return NULL;
160 }
161
162 static bfd_boolean
163 symbol_is_valid (asymbol *sym ATTRIBUTE_UNUSED,
164 disassemble_info *info ATTRIBUTE_UNUSED)
165 {
166 return TRUE;
167 }
168
169 static void
170 memory_error_func (int status ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED,
171 disassemble_info *info ATTRIBUTE_UNUSED) { }
172
173
174 #if WSIZE(32)
175 #define GET_PC(ctx) (((ucontext_t*)ctx)->uc_mcontext.gregs[REG_EIP])
176 #define GET_SP(ctx) (((ucontext_t*)ctx)->uc_mcontext.gregs[REG_ESP])
177 #define GET_FP(ctx) (((ucontext_t*)ctx)->uc_mcontext.gregs[REG_EBP])
178
179 #elif WSIZE(64)
180 #define GET_PC(ctx) (((ucontext_t*)ctx)->uc_mcontext.gregs[REG_RIP])
181 #define GET_SP(ctx) (((ucontext_t*)ctx)->uc_mcontext.gregs[REG_RSP])
182 #define GET_FP(ctx) (((ucontext_t*)ctx)->uc_mcontext.gregs[REG_RBP])
183 #endif /* WSIZE() */
184
185 #elif ARCH(Aarch64)
186 #define GET_PC(ctx) (((ucontext_t*)ctx)->uc_mcontext.regs[15])
187 #define GET_SP(ctx) (((ucontext_t*)ctx)->uc_mcontext.regs[13])
188 #define GET_FP(ctx) (((ucontext_t*)ctx)->uc_mcontext.regs[14])
189 #endif /* ARCH() */
190
191 /*
192 * FILL_CONTEXT() for all platforms
193 * Could use getcontext() except:
194 * - it's not guaranteed to be async signal safe
195 * - it's a system call and not that lightweight
196 * - it's not portable as of POSIX.1-2008
197 * So we just use low-level mechanisms to fill in the few fields we need.
198 */
199 #if ARCH(SPARC)
200 #if WSIZE(32)
201 #define FILL_CONTEXT(context) \
202 { \
203 greg_t fp; \
204 __asm__ __volatile__( "mov %%i6, %0" : "=r" (fp) ); \
205 __asm__ __volatile__( "ta 3" ); \
206 GET_SP(context) = fp; \
207 GET_PC(context) = (greg_t)0; \
208 }
209
210 #elif WSIZE(64)
211 #define FILL_CONTEXT(context) \
212 { \
213 greg_t fp; \
214 __asm__ __volatile__( "mov %%i6, %0" : "=r" (fp) ); \
215 __asm__ __volatile__( "flushw" ); \
216 GET_SP(context) = fp; \
217 GET_PC(context) = (greg_t)0; \
218 }
219 #endif /* WSIZE() */
220
221 #elif ARCH(Intel)
222 #define FILL_CONTEXT(context) \
223 { \
224 context->uc_link = NULL; \
225 void *sp = __collector_getsp(); \
226 GET_SP(context) = (intptr_t)sp; \
227 GET_FP(context) = (intptr_t)__collector_getfp(); \
228 GET_PC(context) = (intptr_t)__collector_getpc(); \
229 context->uc_stack.ss_sp = sp; \
230 context->uc_stack.ss_size = 0x100000; \
231 }
232
233 #elif ARCH(Aarch64)
234 #define FILL_CONTEXT(context) \
235 { CALL_UTIL (getcontext) (context); \
236 context->uc_mcontext.sp = (__u64) __builtin_frame_address(0); \
237 }
238
239 #endif /* ARCH() */
240
241 static int
242 getByteInstruction (unsigned char *p)
243 {
244 if (__collector_VM_ReadByteInstruction)
245 {
246 int v = __collector_VM_ReadByteInstruction (p);
247 if (v != VM_NOT_VM_MEMORY)
248 return v;
249 }
250 return *p;
251 }
252
253 struct DataHandle *dhndl = NULL;
254
255 static unsigned unwind_key = COLLECTOR_TSD_INVALID_KEY;
256
257 /* To support two OpenMP API's we use a pointer
258 * to the actual function.
259 */
260 int (*__collector_omp_stack_trace)(char*, int, hrtime_t, void*) = NULL;
261 int (*__collector_mpi_stack_trace)(char*, int, hrtime_t) = NULL;
262
263 #define DEFAULT_MAX_NFRAMES 256
264 static int max_native_nframes = DEFAULT_MAX_NFRAMES;
265 static int max_java_nframes = DEFAULT_MAX_NFRAMES;
266
267 #define NATIVE_FRAME_BYTES(nframes) ( ((nframes)+1) * sizeof(long) )
268 #define JAVA_FRAME_BYTES(nframes) ( ((nframes)+1) * sizeof(long) * 2 + 16 )
269 #define OVERHEAD_BYTES ( 2 * sizeof(long) + 2 * sizeof(Stack_info) )
270
271 #define ROOT_UID 801425552975190205ULL
272 #define ROOT_UID_INV 92251691606677ULL
273 #define ROOT_IDX 13907816567264074199ULL
274 #define ROOT_IDX_INV 2075111ULL
275 #define UIDTableSize 1048576
276 static volatile uint64_t *UIDTable = NULL;
277 static volatile int seen_omp = 0;
278
279 static int stack_unwind (char *buf, int size, void *bptr, void *eptr, ucontext_t *context, int mode);
280 static FrameInfo compute_uid (Frame_packet *frp);
281 static int omp_no_walk = 0;
282
283 #if ARCH(Intel)
284 #define ValTableSize 1048576
285 #define OmpValTableSize 65536
286 static unsigned long *AddrTable_RA_FROMFP = NULL; // Cache for RA_FROMFP pcs
287 static unsigned long *AddrTable_RA_EOSTCK = NULL; // Cache for RA_EOSTCK pcs
288 static struct WalkContext *OmpCurCtxs = NULL;
289 static struct WalkContext *OmpCtxs = NULL;
290 static uint32_t *OmpVals = NULL;
291 static unsigned long *OmpRAs = NULL;
292 static unsigned long adjust_ret_addr (unsigned long ra, unsigned long segoff, unsigned long tend);
293 static int parse_x86_AVX_instruction (unsigned char *pc);
294
295 struct WalkContext
296 {
297 unsigned long pc;
298 unsigned long sp;
299 unsigned long fp;
300 unsigned long ln;
301 unsigned long sbase; /* stack boundary */
302 unsigned long tbgn; /* current memory segment start */
303 unsigned long tend; /* current memory segment end */
304 };
305 #endif
306
307 #if defined(DEBUG) && ARCH(Intel)
308 #include <execinfo.h>
309
310 static void
311 dump_stack (int nline)
312 {
313 if ((__collector_tracelevel & SP_DUMP_STACK) == 0)
314 return;
315
316 enum Constexpr { MAX_SIZE = 1024 };
317 void *array[MAX_SIZE];
318 size_t sz = backtrace (array, MAX_SIZE);
319 char **strings = backtrace_symbols (array, sz);
320 DprintfT (SP_DUMP_STACK, "\ndump_stack: %d size=%d\n", nline, (int) sz);
321 for (int i = 0; i < sz; i++)
322 DprintfT (SP_DUMP_STACK, " %3d: %p %s\n", i, array[i],
323 strings[i] ? strings[i] : "???");
324 }
325
326 #define dump_targets(nline, ntrg, targets) \
327 if ((__collector_tracelevel & SP_DUMP_UNWIND) != 0) \
328 for(int i = 0; i < ntrg; i++) \
329 DprintfT (SP_DUMP_UNWIND, " %2d: 0x%lx\n", i, (long) targets[i])
330 #else
331 #define dump_stack(x)
332 #define dump_targets(nline, ntrg, targets)
333 #endif
334
335 void
336 __collector_ext_unwind_key_init (int isPthread, void * stack)
337 {
338 void * ptr = __collector_tsd_get_by_key (unwind_key);
339 if (ptr == NULL)
340 {
341 TprintfT (DBG_LT2, "__collector_ext_unwind_key_init: cannot get tsd\n");
342 return;
343 }
344 if (isPthread)
345 {
346 size_t stack_size = 0;
347 void *stack_addr = 0;
348 pthread_t pthread = pthread_self ();
349 pthread_attr_t attr;
350 int err = pthread_getattr_np (pthread, &attr);
351 TprintfT (DBG_LT1, "__collector_ext_unwind_key_init: pthread: 0x%lx err: %d\n", pthread, err);
352 if (err == 0)
353 {
354 err = pthread_attr_getstack (&attr, &stack_addr, &stack_size);
355 if (err == 0)
356 stack_addr = (char*) stack_addr + stack_size;
357 TprintfT (DBG_LT1, "__collector_ext_unwind_key_init: stack_size=0x%lx eos=%p err=%d\n",
358 (long) stack_size, stack_addr, err);
359 err = pthread_attr_destroy (&attr);
360 TprintfT (DBG_LT1, "__collector_ext_unwind_key_init: destroy: %d\n", err);
361 }
362 *(void**) ptr = stack_addr;
363 }
364 else
365 *(void**) ptr = stack; // cloned thread
366 }
367
368 void
369 __collector_ext_unwind_init (int record)
370 {
371 int sz = UIDTableSize * sizeof (*UIDTable);
372 UIDTable = (uint64_t*) __collector_allocCSize (__collector_heap, sz, 1);
373 if (UIDTable == NULL)
374 {
375 __collector_terminate_expt ();
376 return;
377 }
378 CALL_UTIL (memset)((void*) UIDTable, 0, sz);
379
380 char *str = CALL_UTIL (getenv)("GPROFNG_JAVA_MAX_CALL_STACK_DEPTH");
381 if (str != NULL && *str != 0)
382 {
383 char *endptr;
384 int n = CALL_UTIL (strtol)(str, &endptr, 0);
385 if (endptr != str && n >= 0)
386 {
387 if (n < 5)
388 n = 5;
389 if (n > MAX_STACKDEPTH)
390 n = MAX_STACKDEPTH;
391 max_java_nframes = n;
392 }
393 }
394
395 str = CALL_UTIL (getenv)("GPROFNG_MAX_CALL_STACK_DEPTH");
396 if (str != NULL && *str != 0)
397 {
398 char *endptr = str;
399 int n = CALL_UTIL (strtol)(str, &endptr, 0);
400 if (endptr != str && n >= 0)
401 {
402 if (n < 5)
403 n = 5;
404 if (n > MAX_STACKDEPTH)
405 n = MAX_STACKDEPTH;
406 max_native_nframes = n;
407 }
408 }
409
410 TprintfT (DBG_LT0, "GPROFNG_MAX_CALL_STACK_DEPTH=%d GPROFNG_JAVA_MAX_CALL_STACK_DEPTH=%d\n",
411 max_native_nframes, max_java_nframes);
412 omp_no_walk = 1;
413
414 if (__collector_VM_ReadByteInstruction == NULL)
415 __collector_VM_ReadByteInstruction = (int(*)()) dlsym (RTLD_DEFAULT, "Async_VM_ReadByteInstruction");
416
417 #if ARCH(SPARC)
418 #if WSIZE(64)
419 misalign_hdl = (unsigned long) dlsym (RTLD_DEFAULT, "__misalign_trap_handler");
420 misalign_hdlx = (unsigned long) dlsym (RTLD_DEFAULT, "__misalign_trap_handler_end");
421 if (misalign_hdlx == 0)
422 misalign_hdlx = misalign_hdl + 292;
423 barrier_hdl = (unsigned long) dlsym (RTLD_DEFAULT, "__mt_EndOfTask_Barrier_");
424 barrier_hdlx = (unsigned long) dlsym (RTLD_DEFAULT, "__mt_EndOfTask_Barrier_Dummy_");
425 if (barrier_hdlx == 0)
426 barrier_hdl = 0;
427 #else
428 barrier_hdl = (unsigned long) dlsym (RTLD_DEFAULT, "__mt_EndOfTask_Barrier_");
429 barrier_hdlx = (unsigned long) dlsym (RTLD_DEFAULT, "__mt_EndOfTask_Barrier_Dummy_");
430 if (barrier_hdlx == 0)
431 barrier_hdl = 0;
432 #endif /* WSIZE() */
433
434 #elif ARCH(Intel)
435 sz = ValTableSize * sizeof (*AddrTable_RA_FROMFP);
436 AddrTable_RA_FROMFP = (unsigned long*) __collector_allocCSize (__collector_heap, sz, 1);
437 sz = ValTableSize * sizeof (*AddrTable_RA_EOSTCK);
438 AddrTable_RA_EOSTCK = (unsigned long*) __collector_allocCSize (__collector_heap, sz, 1);
439 if (omp_no_walk && (__collector_omp_stack_trace != NULL || __collector_mpi_stack_trace != NULL))
440 {
441 sz = OmpValTableSize * sizeof (*OmpCurCtxs);
442 OmpCurCtxs = (struct WalkContext *) __collector_allocCSize (__collector_heap, sz, 1);
443 sz = OmpValTableSize * sizeof (*OmpCtxs);
444 OmpCtxs = (struct WalkContext *) __collector_allocCSize (__collector_heap, sz, 1);
445 sz = OmpValTableSize * sizeof (*OmpVals);
446 OmpVals = (uint32_t*) __collector_allocCSize (__collector_heap, sz, 1);
447 sz = OmpValTableSize * sizeof (*OmpRAs);
448 OmpRAs = (unsigned long*) __collector_allocCSize (__collector_heap, sz, 1);
449 if (OmpCurCtxs == NULL || OmpCtxs == NULL || OmpVals == NULL || OmpRAs == NULL)
450 {
451 TprintfT (0, "unwind_init() ERROR: failed; terminating experiment\n");
452 __collector_terminate_expt ();
453 return;
454 }
455 }
456 #endif /* ARCH() */
457
458 if (record)
459 {
460 dhndl = __collector_create_handle (SP_FRINFO_FILE);
461 __collector_log_write ("<%s name=\"%s\" format=\"binary\"/>\n", SP_TAG_DATAPTR, SP_FRINFO_FILE);
462 }
463
464 unwind_key = __collector_tsd_create_key (sizeof (void*), NULL, NULL);
465 if (unwind_key == COLLECTOR_TSD_INVALID_KEY)
466 {
467 TprintfT (0, "unwind_init: ERROR: TSD key create failed.\n");
468 __collector_log_write ("<%s kind=\"%s\" id=\"%d\">TSD key not created</%s>\n",
469 SP_TAG_EVENT, SP_JCMD_CERROR, COL_ERROR_GENERAL, SP_TAG_EVENT);
470 return;
471 }
472 TprintfT (0, "unwind_init() completed normally\n");
473 return;
474 }
475
476 void
477 __collector_ext_unwind_close ()
478 {
479 __collector_delete_handle (dhndl);
480 dhndl = NULL;
481 }
482
483 void*
484 __collector_ext_return_address (unsigned level)
485 {
486 if (NULL == UIDTable) //unwind not initialized yet
487 return NULL;
488 unsigned size = (level + 4) * sizeof (long); // need to strip __collector_get_return_address and its caller
489 ucontext_t context;
490 FILL_CONTEXT ((&context));
491 char* buf = (char*) alloca (size);
492 if (buf == NULL)
493 {
494 TprintfT (DBG_LT0, "__collector_get_return_address: ERROR: alloca(%d) fails\n", size);
495 return NULL;
496 }
497 int sz = stack_unwind (buf, size, NULL, NULL, &context, 0);
498 if (sz < (level + 3) * sizeof (long))
499 {
500 TprintfT (DBG_LT0, "__collector_get_return_address: size=%d, but stack_unwind returns %d\n", size, sz);
501 return NULL;
502 }
503 long *lbuf = (long*) buf;
504 TprintfT (DBG_LT2, "__collector_get_return_address: return %lx\n", lbuf[level + 2]);
505 return (void *) (lbuf[level + 2]);
506 }
507 /*
508 * Collector interface method getFrameInfo
509 */
510 FrameInfo
511 __collector_get_frame_info (hrtime_t ts, int mode, void *arg)
512 {
513 ucontext_t *context = NULL;
514 void *bptr = NULL;
515 CM_Array *array = NULL;
516
517 int unwind_mode = 0;
518 int do_walk = 1;
519
520 if (mode & FRINFO_NO_WALK)
521 do_walk = 0;
522 int bmode = mode & 0xffff;
523 int pseudo_context = 0;
524 if (bmode == FRINFO_FROM_STACK_ARG || bmode == FRINFO_FROM_STACK)
525 {
526 bptr = arg;
527 context = (ucontext_t*) alloca (sizeof (ucontext_t));
528 FILL_CONTEXT (context);
529 unwind_mode |= bmode;
530 }
531 else if (bmode == FRINFO_FROM_UC)
532 {
533 context = (ucontext_t*) arg;
534 if (context == NULL)
535 return (FrameInfo) 0;
536 if (GET_SP (context) == 0)
537 pseudo_context = 1;
538 }
539 else if (bmode == FRINFO_FROM_ARRAY)
540 {
541 array = (CM_Array*) arg;
542 if (array == NULL || array->length <= 0)
543 return (FrameInfo) 0;
544 }
545 else
546 return (FrameInfo) 0;
547
548 int max_frame_size = OVERHEAD_BYTES + NATIVE_FRAME_BYTES (max_native_nframes);
549 if (__collector_java_mode && __collector_java_asyncgetcalltrace_loaded && context && !pseudo_context)
550 max_frame_size += JAVA_FRAME_BYTES (max_java_nframes);
551
552 Frame_packet *frpckt = alloca (sizeof (Frame_packet) + max_frame_size);
553 frpckt->type = FRAME_PCKT;
554 frpckt->hsize = sizeof (Frame_packet);
555
556 char *d = (char*) (frpckt + 1);
557 int size = max_frame_size;
558
559 #define MIN(a,b) ((a)<(b)?(a):(b))
560 #if defined(GPROFNG_JAVA_PROFILING)
561 /* get Java info */
562 if (__collector_java_mode && __collector_java_asyncgetcalltrace_loaded && context && !pseudo_context)
563 {
564 /* use only 2/3 of the buffer and leave the rest for the native stack */
565 int tmpsz = MIN (size, JAVA_FRAME_BYTES (max_java_nframes));
566 if (tmpsz > 0)
567 {
568 int sz = __collector_ext_jstack_unwind (d, tmpsz, context);
569 d += sz;
570 size -= sz;
571 }
572 }
573 #endif
574
575 /* get native stack */
576 if (context)
577 {
578 Stack_info *sinfo = (Stack_info*) d;
579 int sz = sizeof (Stack_info);
580 d += sz;
581 size -= sz;
582 #if ARCH(Intel)
583 if (omp_no_walk == 0)
584 do_walk = 1;
585 #endif
586 if (do_walk == 0)
587 unwind_mode |= FRINFO_NO_WALK;
588
589 int tmpsz = MIN (size, NATIVE_FRAME_BYTES (max_native_nframes));
590 if (tmpsz > 0)
591 {
592 sz = stack_unwind (d, tmpsz, bptr, NULL, context, unwind_mode);
593 d += sz;
594 size -= sz;
595 }
596 sinfo->kind = STACK_INFO;
597 sinfo->hsize = (d - (char*) sinfo);
598 }
599
600 /* create a stack image from user data */
601 if (array && array->length > 0)
602 {
603 Stack_info *sinfo = (Stack_info*) d;
604 int sz = sizeof (Stack_info);
605 d += sz;
606 size -= sz;
607 sz = array->length;
608 if (sz > size)
609 sz = size; // YXXX should we mark this with truncation frame?
610 __collector_memcpy (d, array->bytes, sz);
611 d += sz;
612 size -= sz;
613 sinfo->kind = STACK_INFO;
614 sinfo->hsize = (d - (char*) sinfo);
615 }
616
617 /* Compute the total size */
618 frpckt->tsize = d - (char*) frpckt;
619 FrameInfo uid = compute_uid (frpckt);
620 return uid;
621 }
622
623 FrameInfo
624 compute_uid (Frame_packet *frp)
625 {
626 uint64_t idxs[LAST_INFO];
627 uint64_t uid = ROOT_UID;
628 uint64_t idx = ROOT_IDX;
629
630 Common_info *cinfo = (Common_info*) ((char*) frp + frp->hsize);
631 char *end = (char*) frp + frp->tsize;
632 for (;;)
633 {
634 if ((char*) cinfo >= end || cinfo->hsize == 0 ||
635 (char*) cinfo + cinfo->hsize > end)
636 break;
637
638 /* Start with a different value to avoid matching with uid */
639 uint64_t uidt = 1;
640 uint64_t idxt = 1;
641 long *ptr = (long*) ((char*) cinfo + cinfo->hsize);
642 long *bnd = (long*) ((char*) cinfo + sizeof (Common_info));
643 TprintfT (DBG_LT2, "compute_uid: Cnt=%ld: ", (long) cinfo->hsize);
644 while (ptr > bnd)
645 {
646 long val = *(--ptr);
647 tprintf (DBG_LT2, "0x%8.8llx ", (unsigned long long) val);
648 uidt = (uidt + val) * ROOT_UID;
649 idxt = (idxt + val) * ROOT_IDX;
650 uid = (uid + val) * ROOT_UID;
651 idx = (idx + val) * ROOT_IDX;
652 }
653 if (cinfo->kind == STACK_INFO || cinfo->kind == JAVA_INFO)
654 {
655 cinfo->uid = uidt;
656 idxs[cinfo->kind] = idxt;
657 }
658 cinfo = (Common_info*) ((char*) cinfo + cinfo->hsize);
659 }
660 tprintf (DBG_LT2, "\n");
661
662 /* Check if we have already recorded that uid.
663 * The following fragment contains benign data races.
664 * It's important, though, that all reads from UIDTable
665 * happen before writes.
666 */
667 int found1 = 0;
668 int idx1 = (int) ((idx >> 44) % UIDTableSize);
669 if (UIDTable[idx1] == uid)
670 found1 = 1;
671 int found2 = 0;
672 int idx2 = (int) ((idx >> 24) % UIDTableSize);
673 if (UIDTable[idx2] == uid)
674 found2 = 1;
675 int found3 = 0;
676 int idx3 = (int) ((idx >> 4) % UIDTableSize);
677 if (UIDTable[idx3] == uid)
678 found3 = 1;
679 if (!found1)
680 UIDTable[idx1] = uid;
681 if (!found2)
682 UIDTable[idx2] = uid;
683 if (!found3)
684 UIDTable[idx3] = uid;
685
686 if (found1 || found2 || found3)
687 return (FrameInfo) uid;
688 frp->uid = uid;
689
690 /* Compress info's */
691 cinfo = (Common_info*) ((char*) frp + frp->hsize);
692 for (;;)
693 {
694 if ((char*) cinfo >= end || cinfo->hsize == 0 ||
695 (char*) cinfo + cinfo->hsize > end)
696 break;
697 if (cinfo->kind == STACK_INFO || cinfo->kind == JAVA_INFO)
698 {
699 long *ptr = (long*) ((char*) cinfo + sizeof (Common_info));
700 long *bnd = (long*) ((char*) cinfo + cinfo->hsize);
701 uint64_t uidt = cinfo->uid;
702 uint64_t idxt = idxs[cinfo->kind];
703 int found = 0;
704 int first = 1;
705 while (ptr < bnd - 1)
706 {
707 int idx1 = (int) ((idxt >> 44) % UIDTableSize);
708 if (UIDTable[idx1] == uidt)
709 {
710 found = 1;
711 break;
712 }
713 else if (first)
714 {
715 first = 0;
716 UIDTable[idx1] = uidt;
717 }
718 long val = *ptr++;
719 uidt = uidt * ROOT_UID_INV - val;
720 idxt = idxt * ROOT_IDX_INV - val;
721 }
722 if (found)
723 {
724 char *d = (char*) ptr;
725 char *s = (char*) bnd;
726 if (!first)
727 {
728 int i;
729 for (i = 0; i<sizeof (uidt); i++)
730 {
731 *d++ = (char) uidt;
732 uidt = uidt >> 8;
733 }
734 }
735 int delta = s - d;
736 while (s < end)
737 *d++ = *s++;
738 cinfo->kind |= COMPRESSED_INFO;
739 cinfo->hsize -= delta;
740 frp->tsize -= delta;
741 end -= delta;
742 }
743 }
744 cinfo = (Common_info*) ((char*) cinfo + cinfo->hsize);
745 }
746 __collector_write_packet (dhndl, (CM_Packet*) frp);
747 return (FrameInfo) uid;
748 }
749
750 FrameInfo
751 __collector_getUID (CM_Array *arg, FrameInfo suid)
752 {
753 if (arg->length % sizeof (long) != 0 ||
754 (long) arg->bytes % sizeof (long) != 0)
755 return (FrameInfo) - 1;
756 if (arg->length == 0)
757 return suid;
758
759 uint64_t uid = suid ? suid : 1;
760 uint64_t idx = suid ? suid : 1;
761 long *ptr = (long*) ((char*) arg->bytes + arg->length);
762 long *bnd = (long*) (arg->bytes);
763 while (ptr > bnd)
764 {
765 long val = *(--ptr);
766 uid = (uid + val) * ROOT_UID;
767 idx = (idx + val) * ROOT_IDX;
768 }
769
770 /* Check if we have already recorded that uid.
771 * The following fragment contains benign data races.
772 * It's important, though, that all reads from UIDTable
773 * happen before writes.
774 */
775 int found1 = 0;
776 int idx1 = (int) ((idx >> 44) % UIDTableSize);
777 if (UIDTable[idx1] == uid)
778 found1 = 1;
779 int found2 = 0;
780 int idx2 = (int) ((idx >> 24) % UIDTableSize);
781 if (UIDTable[idx2] == uid)
782 found2 = 1;
783 int found3 = 0;
784 int idx3 = (int) ((idx >> 4) % UIDTableSize);
785 if (UIDTable[idx3] == uid)
786 found3 = 1;
787
788 if (!found1)
789 UIDTable[idx1] = uid;
790 if (!found2)
791 UIDTable[idx2] = uid;
792 if (!found3)
793 UIDTable[idx3] = uid;
794 if (found1 || found2 || found3)
795 return (FrameInfo) uid;
796
797 int sz = sizeof (Uid_packet) + arg->length;
798 if (suid)
799 sz += sizeof (suid);
800 Uid_packet *uidp = alloca (sz);
801 uidp->tsize = sz;
802 uidp->type = UID_PCKT;
803 uidp->flags = 0;
804 uidp->uid = uid;
805
806 /* Compress */
807 ptr = (long*) (arg->bytes);
808 bnd = (long*) ((char*) arg->bytes + arg->length);
809 long *dst = (long*) (uidp + 1);
810 uint64_t uidt = uid;
811 uint64_t idxt = idx;
812 uint64_t luid = suid; /* link uid */
813
814 while (ptr < bnd)
815 {
816
817 long val = *ptr++;
818 *dst++ = val;
819
820 if ((bnd - ptr) > sizeof (uidt))
821 {
822 uidt = uidt * ROOT_UID_INV - val;
823 idxt = idxt * ROOT_IDX_INV - val;
824 int idx1 = (int) ((idxt >> 44) % UIDTableSize);
825 if (UIDTable[idx1] == uidt)
826 {
827 luid = uidt;
828 break;
829 }
830 }
831 }
832 if (luid)
833 {
834 char *d = (char*) dst;
835 for (int i = 0; i<sizeof (luid); i++)
836 {
837 *d++ = (char) luid;
838 luid = luid >> 8;
839 }
840 uidp->flags |= COMPRESSED_INFO;
841 uidp->tsize = d - (char*) uidp;
842 }
843 __collector_write_packet (dhndl, (CM_Packet*) uidp);
844
845 return (FrameInfo) uid;
846 }
847
848 int
849 __collector_getStackTrace (void *buf, int size, void *bptr, void *eptr, void *arg)
850 {
851 if (arg == (void*) __collector_omp_stack_trace)
852 seen_omp = 1;
853 int do_walk = 1;
854 if (arg == NULL || arg == (void*) __collector_omp_stack_trace)
855 {
856 do_walk = (arg == (void*) __collector_omp_stack_trace && omp_no_walk) ? 0 : 1;
857 ucontext_t *context = (ucontext_t*) alloca (sizeof (ucontext_t));
858 FILL_CONTEXT (context);
859 arg = context;
860 }
861 int unwind_mode = 0;
862 if (do_walk == 0)
863 unwind_mode |= FRINFO_NO_WALK;
864 return stack_unwind (buf, size, bptr, eptr, arg, unwind_mode);
865 }
866
867 #if ARCH(SPARC)
868 /*
869 * These are important data structures taken from the header files reg.h and
870 * ucontext.h. They are used for the stack trace algorithm explained below.
871 *
872 * typedef struct ucontext {
873 * u_long uc_flags;
874 * struct ucontext *uc_link;
875 * usigset_t uc_sigmask;
876 * stack_t uc_stack;
877 * mcontext_t uc_mcontext;
878 * long uc_filler[23];
879 * } ucontext_t;
880 *
881 * #define SPARC_MAXREGWINDOW 31
882 *
883 * struct rwindow {
884 * greg_t rw_local[8];
885 * greg_t rw_in[8];
886 * };
887 *
888 * #define rw_fp rw_in[6]
889 * #define rw_rtn rw_in[7]
890 *
891 * struct gwindows {
892 * int wbcnt;
893 * int *spbuf[SPARC_MAXREGWINDOW];
894 * struct rwindow wbuf[SPARC_MAXREGWINDOW];
895 * };
896 *
897 * typedef struct gwindows gwindows_t;
898 *
899 * typedef struct {
900 * gregset_t gregs;
901 * gwindows_t *gwins;
902 * fpregset_t fpregs;
903 * long filler[21];
904 * } mcontext_t;
905 *
906 * The stack would look like this when SIGPROF occurrs.
907 *
908 * ------------------------- <- high memory
909 * | |
910 * | |
911 * -------------------------
912 * | |
913 * ------------------------- <- fp' <-|
914 * | | |
915 * : : |
916 * | | |
917 * ------------------------- |
918 * | fp |----------|
919 * | |
920 * ------------------------- <- sp'
921 * | | | |
922 * | gwins | <- saved stack pointers & | |
923 * | | register windows | |- mcontext
924 * ------------------------- | |
925 * | gregs | <- saved registers | |
926 * ------------------------- |
927 * | | |- ucontext
928 * ------------------------- <- ucp (ucontext pointer) |
929 * | | |
930 * | | |- siginfo
931 * ------------------------- <- sip (siginfo pointer) |
932 * | |
933 * ------------------------- <- sp
934 *
935 * Then the signal handler is called with:
936 * handler( signo, sip, uip );
937 * When gwins is null, all the stack frames are saved in the user stack.
938 * In that case we can find sp' from gregs and walk the stack for a backtrace.
939 * However, if gwins is not null we will have a more complicated case.
940 * Wbcnt(in gwins) tells you how many saved register windows are valid.
941 * This is important because the kernel does not allocate the entire array.
942 * And the top most frame is saved in the lowest index element. The next
943 * paragraph explains the possible causes.
944 *
945 * There are two routines in the kernel to flush out user register windows.
946 * flush_user_windows and flush_user_windows_to_stack
947 * The first routine will not cause a page fault. Therefore if the user
948 * stack is not in memory, the register windows will be saved to the pcb.
949 * This can happen when the kernel is trying to deliver a signal and
950 * the user stack got swap out. The kernel will then build a new context for
951 * the signal handler and the saved register windows will
952 * be copied to the ucontext as show above. On the other hand,
953 * flush_user_windows_to_stack can cause a page fault, and if it failed
954 * then there is something wrong (stack overflow, misalign).
955 * The first saved register window does not necessary correspond to the
956 * first stack frame. So the current stack pointer must be compare with
957 * the stack pointers in spbuf to find a match.
958 *
959 * We will also follow the uc_link field in ucontext to trace also nested
960 * signal stack frames.
961 *
962 */
963
964 /* Dealing with trap handlers.
965 * When a user defined trap handler is invoked the return address
966 * (or actually the address of an instruction that raised the trap)
967 * is passed to the trap handler in %l6, whereas saved %o7 contains
968 * garbage. First, we need to find out if a particular pc belongs
969 * to the trap handler, and if so, take the %l6 value from the stack rather
970 * than %o7 from either the stack or the register.
971 * There are three possible situations represented
972 * by the following stacks:
973 *
974 * MARKER MARKER MARKER
975 * trap handler pc __func pc before 'save' __func pc after 'save'
976 * %l6 %o7 from reg %o7 (garbage)
977 * ... %l6 trap handler pc
978 * ... %l6
979 * ...
980 * where __func is a function called from the trap handler.
981 *
982 * Currently this is implemented to only deal with __misalign_trap_handler
983 * set for v9 FORTRAN applications. Implementation of IN_TRAP_HANDLER
984 * macro shows it. A general solution is postponed.
985 */
986
987 /* Special handling of unwind through the parallel loop barrier code:
988 *
989 * The library defines two symbols, __mt_EndOfTask_Barrier_ and
990 * __mt_EndOfTask_Barrier_Dummy_ representing the first word of
991 * the barrier sychronization code, and the first word following
992 * it. Whenever the leaf PC is between these two symbols,
993 * the unwind code is special-cased as follows:
994 * The __mt_EndOfTask_Barrier_ function is guaranteed to be a leaf
995 * function, so its return address is in a register, not saved on
996 * the stack.
997 *
998 * MARKER
999 * __mt_EndOfTask_Barrier_ PC -- the leaf PC
1000 * loop body function address for the task -- implied caller of __mt_EndOfTask_Barrier_
1001 * this address is taken from the %O0 register
1002 * {mt_master or mt_slave} -- real caller of __mt_EndOfTask_Barrier_
1003 * ...
1004 *
1005 * With this trick, the analyzer will show the time in the barrier
1006 * attributed to the loop at the end of which the barrier synchronization
1007 * is taking place. That loop body routine, will be shown as called
1008 * from the function from which it was extracted, which will be shown
1009 * as called from the real caller, either the slave or master library routine.
1010 */
1011
1012 /*
1013 * These no-fault-load (0x82) assembly functions are courtesy of Rob Gardner.
1014 *
1015 * Note that 0x82 is ASI_PNF. See
1016 * http://lxr.free-electrons.com/source/arch/sparc/include/uapi/asm/asi.h#L134
1017 * ASI address space identifier; PNF primary no fault
1018 */
1019
1020 /* load an int from an address */
1021
1022 /* if the address is illegal, return a 0 */
1023 static int
1024 SPARC_no_fault_load_int (void *addr)
1025 {
1026 int val;
1027 __asm__ __volatile__(
1028 "lda [%1] 0x82, %0\n\t"
1029 : "=r" (val)
1030 : "r" (addr)
1031 );
1032
1033 return val;
1034 }
1035
1036 /* check if an address is invalid
1037 *
1038 * A no-fault load of an illegal address still faults, but it does so silently to the calling process.
1039 * It returns a 0, but so could a load of a legal address.
1040 * So, we time the load. A "fast" load must be a successful load.
1041 * A "slow" load is probably a fault.
1042 * Since it could also be a cache/TLB miss or other abnormality,
1043 * it's safest to retry a slow load.
1044 * The cost of trying a valid address should be some nanosecs.
1045 * The cost of trying an invalid address up to 10 times could be some microsecs.
1046 */
1047 #if 0
1048 static
1049 int invalid_SPARC_addr(void *addr)
1050 {
1051 long t1, t2;
1052 int i;
1053
1054 for (i=0; i<10; i++) {
1055 __asm__ __volatile__(
1056 "rd %%tick, %0\n\t"
1057 "lduba [%2] 0x82, %%g0\n\t"
1058 "rd %%tick, %1\n\t"
1059 : "=r" (t1), "=r" (t2)
1060 : "r" (addr) );
1061 if ( (t2 - t1) < 100 )
1062 return 0;
1063 }
1064 return 1;
1065 }
1066 #endif
1067
1068 /*
1069 * The standard SPARC procedure-calling convention is that the
1070 * calling PC (for determining the return address when the procedure
1071 * is finished) is placed in register %o7. A called procedure
1072 * typically executes a "save" instruction that shifts the register
1073 * window, and %o7 becomes %i7.
1074 *
1075 * Optimized leaf procedures do not shift the register window.
1076 * They assume the return address will remain %o7. So when
1077 * we process a leaf PC, we walk instructions to see if there
1078 * is a call, restore, or other instruction that would indicate
1079 * we can IGNORE %o7 because this is NOT a leaf procedure.
1080 *
1081 * If a limited instruction walk uncovers no such hint, we save
1082 * not only the PC but the %o7 value as well... just to be safe.
1083 * Later, in DBE post-processing of the call stacks, we decide
1084 * whether any recorded %o7 value should be used as a caller
1085 * frame or should be discarded.
1086 */
1087
1088 #define IS_ILLTRAP(x) (((x) & 0xc1c00000) == 0)
1089 #define IS_SAVE(x) (((x) & 0xc1f80000) == 0x81e00000)
1090 #define IS_MOVO7R(x) (((x) & 0xc1f8201f) == 0x8160000f)
1091 #define IS_MOVRO7(x) (((x) & 0xfff82000) == 0x9f600000)
1092 #define IS_ORRG0O7(x) (((x) & 0xff78201f) == 0x9e100000)
1093 #define IS_ORG0RO7(x) (((x) & 0xff7fe000) == 0x9e100000)
1094 #define IS_ORG0O7R(x) (((x) & 0xc17fe01f) == 0x8010000f)
1095 #define IS_ORO7G0R(x) (((x) & 0xc17fe01f) == 0x8013c000)
1096 #define IS_RESTORE(x) (((x) & 0xc1f80000) == 0x81e80000)
1097 #define IS_RET(x) ((x) == 0x81c7e008)
1098 #define IS_RETL(x) ((x) == 0x81c3e008)
1099 #define IS_RETURN(x) (((x) & 0xc1f80000) == 0x81c80000)
1100 #define IS_BRANCH(x) ((((x) & 0xc0000000) == 0) && (((x) & 0x01c00000) != 0x01000000))
1101 #define IS_CALL(x) (((x) & 0xc0000000) == 0x40000000)
1102 #define IS_LDO7(x) (((x) & 0xfff80000) == 0xde000000)
1103
1104 static long pagesize = 0;
1105
1106 static int
1107 process_leaf (long *lbuf, int ind, int lsize, void *context)
1108 {
1109 greg_t pc = GET_PC (context);
1110 greg_t o7 = GET_GREG (context, REG_O7);
1111
1112 /* omazur: TBR START -- not used */
1113 if (IN_BARRIER (pc))
1114 {
1115 if (ind < lsize)
1116 lbuf[ind++] = pc;
1117 if (ind < lsize)
1118 lbuf[ind++] = GET_GREG (context, REG_O0);
1119 return ind;
1120 }
1121 /* omazur: TBR END */
1122 #if WSIZE(64)
1123 if (IN_TRAP_HANDLER (pc))
1124 {
1125 if (ind < lsize)
1126 lbuf[ind++] = pc;
1127 return ind;
1128 }
1129 #endif
1130 unsigned *instrp = (unsigned *) pc;
1131 unsigned *end_addr = instrp + 20;
1132 while (instrp < end_addr)
1133 {
1134 unsigned instr = *instrp++;
1135 if (IS_ILLTRAP (instr))
1136 break;
1137 else if (IS_SAVE (instr))
1138 {
1139 if (ind < lsize)
1140 lbuf[ind++] = pc;
1141 if (o7 && ind < lsize)
1142 lbuf[ind++] = o7;
1143 return ind;
1144 }
1145 else if (IS_MOVO7R (instr) || IS_ORG0O7R (instr) || IS_ORO7G0R (instr))
1146 break;
1147 else if (IS_MOVRO7 (instr) || IS_ORG0RO7 (instr))
1148 {
1149 int rs2 = (instr & 0x1f) + REG_G1 - 1;
1150 o7 = (rs2 <= REG_O7) ? GET_GREG (context, rs2) : 0;
1151 break;
1152 }
1153 else if (IS_ORRG0O7 (instr))
1154 {
1155 int rs2 = ((instr & 0x7c000) >> 14) + REG_G1 - 1;
1156 o7 = (rs2 <= REG_O7) ? GET_GREG (context, rs2) : 0;
1157 break;
1158 }
1159 else if (IS_RESTORE (instr))
1160 {
1161 o7 = 0;
1162 break;
1163 }
1164 else if (IS_RETURN (instr))
1165 {
1166 o7 = 0;
1167 break;
1168 }
1169 else if (IS_RET (instr))
1170 {
1171 o7 = 0;
1172 break;
1173 }
1174 else if (IS_RETL (instr))
1175 {
1176 /* process delay slot */
1177 instr = *instrp++;
1178 if (IS_RESTORE (instr))
1179 o7 = 0;
1180 break;
1181 }
1182 else if (IS_BRANCH (instr))
1183 {
1184 unsigned *backbegin = ((unsigned *) pc - 1);
1185 unsigned *backend = backbegin - 12 + (instrp - (unsigned *) pc);
1186 while (backbegin > backend)
1187 {
1188 // 21920143 stack unwind: SPARC process_leaf backtracks too far
1189 /*
1190 * We've already dereferenced backbegin+1.
1191 * So if backbegin is on the same page, we're fine.
1192 * If we've gone to a different page, possibly things are not fine.
1193 * We don't really know how to test that.
1194 * Let's just assume the worst: that dereferencing backbegin would segv.
1195 * We won't know if we're in a leaf function or not.
1196 */
1197 if (pagesize == 0)
1198 pagesize = CALL_UTIL (sysconf)(_SC_PAGESIZE);
1199 if ((((long) (backbegin + 1)) & (pagesize - 1)) < sizeof (unsigned*))
1200 break;
1201 unsigned backinstr = *backbegin--;
1202 if (IS_LDO7 (backinstr))
1203 {
1204 o7 = 0;
1205 break;
1206 }
1207 else if (IS_ILLTRAP (backinstr))
1208 break;
1209 else if (IS_RETURN (backinstr))
1210 break;
1211 else if (IS_RET (backinstr))
1212 break;
1213 else if (IS_RETL (backinstr))
1214 break;
1215 else if (IS_CALL (backinstr))
1216 break;
1217 else if (IS_SAVE (backinstr))
1218 {
1219 o7 = 0;
1220 break;
1221 }
1222 }
1223 break;
1224 }
1225 else if (IS_CALL (instr))
1226 o7 = 0;
1227 }
1228
1229 #if WSIZE(64)
1230 if (o7 != 0 && ((long) o7) < 32 && ((long) o7) > -32)
1231 {
1232 /* 20924821 SEGV in unwind code on SPARC/Linux
1233 * We've seen this condition in some SPARC-Linux runs.
1234 * o7 is non-zero but not a valid address.
1235 * Values like 4 or -7 have been seen.
1236 * Let's check if o7 is unreasonably small.
1237 * If so, set to 0 so that it won't be recorded.
1238 * Otherwise, there is risk of it being dereferenced in process_sigreturn().
1239 */
1240 // __collector_log_write("<event kind=\"%s\" id=\"%d\">time %lld, internal debug unwind at leaf; o7 = %ld, pc = %x</event>\n",
1241 // SP_JCMD_COMMENT, COL_COMMENT_NONE, __collector_gethrtime() - __collector_start_time, (long) o7, pc );
1242 o7 = 0;
1243 }
1244 #endif
1245
1246 if (o7)
1247 {
1248 if (ind < lsize)
1249 lbuf[ind++] = SP_LEAF_CHECK_MARKER;
1250 if (ind < lsize)
1251 lbuf[ind++] = pc;
1252 if (ind < lsize)
1253 lbuf[ind++] = o7;
1254 }
1255 else if (ind < lsize)
1256 lbuf[ind++] = pc;
1257 return ind;
1258 }
1259
1260 #if WSIZE(64)
1261 // detect signal handler
1262 static int
1263 process_sigreturn (long *lbuf, int ind, int lsize, unsigned char * tpc,
1264 struct frame **pfp, void * bptr, int extra_frame)
1265 {
1266 // cheap checks whether tpc is obviously not an instruction address
1267 if ((4096 > (unsigned long) tpc) // the first page is off limits
1268 || (3 & (unsigned long) tpc))
1269 return ind; // the address is not aligned
1270
1271 // get the instruction at tpc, skipping over as many as 7 nop's (0x01000000)
1272 int insn, i;
1273 for (i = 0; i < 7; i++)
1274 {
1275 insn = SPARC_no_fault_load_int ((void *) tpc);
1276 if (insn != 0x01000000)
1277 break;
1278 tpc += 4;
1279 }
1280
1281 // we're not expecting 0 (and it could mean an illegal address)
1282 if (insn == 0)
1283 return ind;
1284
1285 // We are looking for __rt_sigreturn_stub with the instruction
1286 // 0x82102065 : mov 0x65 /* __NR_rt_sigreturn */, %g1
1287 if (insn == 0x82102065)
1288 {
1289 /*
1290 * according to linux kernel source code,
1291 * syscall(_NR_rt_sigreturn) uses the following data in stack:
1292 * struct rt_signal_frame {
1293 * struct sparc_stackf ss;
1294 * siginfo_t info;
1295 * struct pt_regs regs;
1296 * ....};
1297 * sizeof(struct sparc_stackf) is 192;
1298 * sizeof(siginfo_t) is 128;
1299 * we need to get the register values from regs, which is defined as:
1300 * struct pt_regs {
1301 * unsigned long u_regs[16];
1302 * unsigned long tstate;
1303 * unsigned long tpc;
1304 * unsigned long tnpc;
1305 * ....};
1306 * pc and fp register has offset of 120 and 112;
1307 * the pc of kill() is stored in tnpc, whose offest is 136.
1308 */
1309 greg_t pc = *((unsigned long*) ((char*) ((*pfp)) + 192 + 128 + 136));
1310 greg_t pc1 = *((unsigned long*) ((char*) ((*pfp)) + 192 + 128 + 120));
1311 (*pfp) = *((struct frame**) ((char*) ((*pfp)) + 192 + 128 + 112));
1312 if (pc && pc1)
1313 {
1314 if (bptr != NULL && extra_frame && ((char*) (*pfp) + STACK_BIAS) < (char*) bptr && ind < 2)
1315 {
1316 lbuf[0] = pc1;
1317 if (ind == 0)
1318 ind++;
1319 }
1320 if (bptr == NULL || ((char*) (*pfp) + STACK_BIAS) >= (char*) bptr)
1321 {
1322 if (ind < lsize)
1323 lbuf[ind++] = (unsigned long) tpc;
1324 if (ind < lsize)
1325 lbuf[ind++] = pc;
1326 if (ind < lsize)
1327 lbuf[ind++] = pc1;
1328 }
1329 }
1330 DprintfT (SP_DUMP_UNWIND, "unwind.c: resolved sigreturn pc=0x%lx, pc1=0x%lx, fp=0x%lx\n", pc, pc1, *(pfp));
1331 }
1332 return ind;
1333 }
1334 #endif
1335
1336 /*
1337 * int stack_unwind( char *buf, int size, ucontext_t *context )
1338 * This routine looks into the mcontext and
1339 * trace stack frames to record return addresses.
1340 */
1341 int
1342 stack_unwind (char *buf, int size, void *bptr, void *eptr, ucontext_t *context, int mode)
1343 {
1344 /*
1345 * trace the stack frames from user stack.
1346 * We are assuming that the frame pointer and return address
1347 * are null when we are at the top level.
1348 */
1349 long *lbuf = (long*) buf;
1350 int lsize = size / sizeof (long);
1351 struct frame *fp = (struct frame *) GET_SP (context); /* frame pointer */
1352 greg_t pc; /* program counter */
1353 int extra_frame = 0;
1354 if ((mode & 0xffff) == FRINFO_FROM_STACK)
1355 extra_frame = 1;
1356
1357 int ind = 0;
1358 if (bptr == NULL)
1359 ind = process_leaf (lbuf, ind, lsize, context);
1360
1361 int extra_frame = 0;
1362 if ((mode & 0xffff) == FRINFO_FROM_STACK)
1363 extra_frame = 1;
1364 int ind = 0;
1365 if (bptr == NULL)
1366 ind = process_leaf (lbuf, ind, lsize, context);
1367
1368 while (fp)
1369 {
1370 if (ind >= lsize)
1371 break;
1372 fp = (struct frame *) ((char *) fp + STACK_BIAS);
1373 if (eptr && fp >= (struct frame *) eptr)
1374 {
1375 ind = ind >= 2 ? ind - 2 : 0;
1376 break;
1377 }
1378 #if WSIZE(64) // detect signal handler
1379 unsigned char * tpc = ((unsigned char*) (fp->fr_savpc));
1380 struct frame * tfp = (struct frame*) ((char*) (fp->fr_savfp) + STACK_BIAS);
1381 int old_ind = ind;
1382 ind = process_sigreturn (lbuf, old_ind, lsize, tpc, &tfp, bptr, extra_frame);
1383 if (ind != old_ind)
1384 {
1385 pc = (greg_t) tpc;
1386 fp = tfp;
1387 }
1388 else
1389 #endif
1390 {
1391 #if WSIZE(64)
1392 if (IN_TRAP_HANDLER (lbuf[ind - 1]))
1393 pc = fp->fr_local[6];
1394 else
1395 pc = fp->fr_savpc;
1396 #else
1397 pc = fp->fr_savpc;
1398 #endif
1399 fp = fp->fr_savfp;
1400 if (pc)
1401 {
1402 if (bptr != NULL && extra_frame && ((char*) fp + STACK_BIAS) < (char*) bptr && ind < 2)
1403 {
1404 lbuf[0] = pc;
1405 if (ind == 0)
1406 ind++;
1407 }
1408 if (bptr == NULL || ((char*) fp + STACK_BIAS) >= (char*) bptr)
1409 lbuf[ind++] = pc;
1410 }
1411 }
1412
1413 /* 4616238: _door_return may have a frame that has non-zero
1414 * saved stack pointer and zero pc
1415 */
1416 if (pc == (greg_t) NULL)
1417 break;
1418 }
1419
1420 if (ind >= lsize)
1421 { /* truncated stack handling */
1422 ind = lsize - 1;
1423 lbuf[ind++] = SP_TRUNC_STACK_MARKER;
1424 }
1425 return ind * sizeof (long);
1426 }
1427
1428 #elif ARCH(Intel)
1429
1430 /* get __NR_<syscall_name> constants */
1431 #include <syscall.h>
1432
1433 /*
1434 * From uts/intel/ia32/os/sendsig.c:
1435 *
1436 * An amd64 signal frame looks like this on the stack:
1437 *
1438 * old %rsp:
1439 * <128 bytes of untouched stack space>
1440 * <a siginfo_t [optional]>
1441 * <a ucontext_t>
1442 * <siginfo_t *>
1443 * <signal number>
1444 * new %rsp: <return address (deliberately invalid)>
1445 *
1446 * The signal number and siginfo_t pointer are only pushed onto the stack in
1447 * order to allow stack backtraces. The actual signal handling code expects the
1448 * arguments in registers.
1449 *
1450 * An i386 SVR4/ABI signal frame looks like this on the stack:
1451 *
1452 * old %esp:
1453 * <a siginfo32_t [optional]>
1454 * <a ucontext32_t>
1455 * <pointer to that ucontext32_t>
1456 * <pointer to that siginfo32_t>
1457 * <signo>
1458 * new %esp: <return address (deliberately invalid)>
1459 */
1460
1461 #if WSIZE(32)
1462 #define OPC_REG(x) ((x)&0x7)
1463 #define MRM_REGD(x) (((x)>>3)&0x7)
1464 #define MRM_REGS(x) ((x)&0x7)
1465 #define RED_ZONE 0
1466 #elif WSIZE(64)
1467 #define OPC_REG(x) (B|((x)&0x7))
1468 #define MRM_REGD(x) (R|(((x)>>3)&0x7))
1469 #define MRM_REGS(x) (B|((x)&0x7))
1470 #define RED_ZONE 16
1471 #endif
1472 #define MRM_EXT(x) (((x)>>3)&0x7)
1473 #define MRM_MOD(x) ((x)&0xc0)
1474
1475 #define RAX 0
1476 #define RDX 2
1477 #define RSP 4
1478 #define RBP 5
1479
1480 struct AdvWalkContext
1481 {
1482 unsigned char *pc;
1483 unsigned long *sp;
1484 unsigned long *sp_safe;
1485 unsigned long *fp;
1486 unsigned long *fp_sav;
1487 unsigned long *fp_loc;
1488 unsigned long rax;
1489 unsigned long rdx;
1490 unsigned long ra_sav;
1491 unsigned long *ra_loc;
1492 unsigned long regs[16];
1493 int tidx; /* targets table index */
1494 uint32_t cval; /* cache value */
1495 };
1496
1497 static unsigned long
1498 getRegVal (struct AdvWalkContext *cur, int r, int *undefRez)
1499 {
1500 if (cur->regs[r] == 0)
1501 {
1502 if (r == RBP)
1503 {
1504 tprintf (DBG_LT3, "getRegVal: returns cur->regs[RBP]=0x%lx cur->pc=0x%lx\n",
1505 (unsigned long) cur->fp, (unsigned long) cur->pc);
1506 return (unsigned long) cur->fp;
1507 }
1508 *undefRez = 1;
1509 }
1510 tprintf (DBG_LT3, "getRegVal: cur->regs[%d]=0x%lx cur->pc=0x%lx\n",
1511 r, (unsigned long) cur->regs[r], (unsigned long) cur->pc);
1512 return cur->regs[r];
1513 }
1514
1515 static unsigned char *
1516 check_modrm (unsigned char *pc)
1517 {
1518 unsigned char modrm = *pc++;
1519 unsigned char mod = MRM_MOD (modrm);
1520 if (mod == 0xc0)
1521 return pc;
1522 unsigned char regs = modrm & 0x07;
1523 if (regs == RSP)
1524 {
1525 if (mod == 0x40)
1526 return pc + 2; // SIB + disp8
1527 if (mod == 0x80)
1528 return pc + 5; // SIB + disp32
1529 return pc + 1; // SIB
1530 }
1531 if (mod == 0x0)
1532 {
1533 if (regs == RBP)
1534 pc += 4; // disp32
1535 }
1536 else if (mod == 0x40)
1537 pc += 1; /* byte */
1538 else if (mod == 0x80)
1539 pc += 4; /* word */
1540 return pc;
1541 }
1542
1543 static int
1544 read_int (unsigned char *pc, int w)
1545 {
1546 if (w == 1)
1547 return *((char *) pc);
1548 if (w == 2)
1549 return *(short*) pc;
1550 return *(int*) pc;
1551 }
1552
1553 /* Return codes */
1554 enum
1555 {
1556 RA_FAILURE = 0,
1557 RA_SUCCESS,
1558 RA_END_OF_STACK,
1559 RA_SIGRETURN,
1560 RA_RT_SIGRETURN
1561 };
1562
1563 /* Cache value encodings */
1564 static const uint32_t RA_FROMFP = (uint32_t) - 1; /* get the RA from the frame pointer */
1565 static const uint32_t RA_EOSTCK = (uint32_t) - 2; /* end-of-stack */
1566
1567
1568 #define MAXCTX 16
1569 #define MAXTRGTS 64
1570 #define MAXJMPREG 2
1571 #define MAXJMPREGCTX 3
1572
1573 #define DELETE_CURCTX() __collector_memcpy (cur, buf + (--nctx), sizeof (*cur))
1574
1575 /**
1576 * Look for pc in AddrTable_RA_FROMFP and in AddrTable_RA_EOSTCK
1577 * @param wctx
1578 * @return
1579 */
1580 static int
1581 cache_get (struct WalkContext *wctx)
1582 {
1583 unsigned long addr;
1584 if (AddrTable_RA_FROMFP != NULL)
1585 {
1586 uint64_t idx = wctx->pc % ValTableSize;
1587 addr = AddrTable_RA_FROMFP[ idx ];
1588 if (addr == wctx->pc)
1589 { // Found in AddrTable_RA_FROMFP
1590 unsigned long *sp = NULL;
1591 unsigned long fp = wctx->fp;
1592 /* validate fp before use */
1593 if (fp < wctx->sp || fp >= wctx->sbase - sizeof (*sp))
1594 return RA_FAILURE;
1595 sp = (unsigned long *) fp;
1596 fp = *sp++;
1597 unsigned long ra = *sp++;
1598 unsigned long tbgn = wctx->tbgn;
1599 unsigned long tend = wctx->tend;
1600 if (ra < tbgn || ra >= tend)
1601 if (!__collector_check_segment (ra, &tbgn, &tend, 0))
1602 return RA_FAILURE;
1603 unsigned long npc = adjust_ret_addr (ra, ra - tbgn, tend);
1604 if (npc == 0)
1605 return RA_FAILURE;
1606 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d cached pc=0x%lX\n", __LINE__, npc);
1607 wctx->pc = npc;
1608 wctx->sp = (unsigned long) sp;
1609 wctx->fp = fp;
1610 wctx->tbgn = tbgn;
1611 wctx->tend = tend;
1612 return RA_SUCCESS;
1613 }
1614 }
1615 if (NULL == AddrTable_RA_EOSTCK)
1616 return RA_FAILURE;
1617 uint64_t idx = wctx->pc % ValTableSize;
1618 addr = AddrTable_RA_EOSTCK[ idx ];
1619 if (addr != wctx->pc)
1620 return RA_FAILURE;
1621 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d cached RA_END_OF_STACK\n", __LINE__);
1622 return RA_END_OF_STACK;
1623 }
1624 /**
1625 * Save pc in RA_FROMFP or RA_EOSTCK cache depending on val
1626 * @param wctx
1627 */
1628 static void
1629 cache_put (struct WalkContext *wctx, const uint32_t val)
1630 {
1631 if (RA_FROMFP == val)
1632 {
1633 // save pc in RA_FROMFP cache
1634 if (NULL != AddrTable_RA_FROMFP)
1635 {
1636 uint64_t idx = wctx->pc % ValTableSize;
1637 AddrTable_RA_FROMFP[ idx ] = wctx->pc;
1638 if (NULL != AddrTable_RA_EOSTCK)
1639 if (AddrTable_RA_EOSTCK[ idx ] == wctx->pc)
1640 // invalidate pc in RA_EOSTCK cache
1641 AddrTable_RA_EOSTCK[ idx ] = 0;
1642 }
1643 return;
1644 }
1645 if (RA_EOSTCK == val)
1646 {
1647 // save pc in RA_EOSTCK cache
1648 if (NULL != AddrTable_RA_EOSTCK)
1649 {
1650 uint64_t idx = wctx->pc % ValTableSize;
1651 AddrTable_RA_EOSTCK[ idx ] = wctx->pc;
1652 if (NULL != AddrTable_RA_FROMFP)
1653 {
1654 if (AddrTable_RA_FROMFP[ idx ] == wctx->pc)
1655 // invalidate pc in RA_FROMFP cache
1656 AddrTable_RA_FROMFP[ idx ] = 0;
1657 }
1658 }
1659 return;
1660 }
1661 }
1662
1663 static int
1664 process_return_real (struct WalkContext *wctx, struct AdvWalkContext *cur, int cache_on)
1665 {
1666 if ((unsigned long) cur->sp >= wctx->sbase ||
1667 (unsigned long) cur->sp < wctx->sp)
1668 {
1669 DprintfT (SP_DUMP_UNWIND, "unwind.c: not in stack: %p [0x%lX-0x%lX]\n",
1670 cur->sp, wctx->sp, wctx->sbase);
1671 return RA_FAILURE;
1672 }
1673
1674 unsigned long ra;
1675 if (cur->sp == cur->ra_loc)
1676 {
1677 ra = cur->ra_sav;
1678 cur->sp++;
1679 }
1680 else if (cur->sp >= cur->sp_safe && (unsigned long) cur->sp < wctx->sbase)
1681 ra = *cur->sp++;
1682 else
1683 {
1684 DprintfT (SP_DUMP_UNWIND, "unwind.c: not safe: %p >= %p\n", cur->sp, cur->sp_safe);
1685 return RA_FAILURE;
1686 }
1687 if (ra == 0)
1688 {
1689 if (cache_on)
1690 cache_put (wctx, RA_EOSTCK);
1691 wctx->pc = ra;
1692 wctx->sp = (unsigned long) cur->sp;
1693 wctx->fp = (unsigned long) cur->fp;
1694 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d RA_END_OF_STACK\n", __LINE__);
1695 return RA_END_OF_STACK;
1696 }
1697
1698 unsigned long tbgn = wctx->tbgn;
1699 unsigned long tend = wctx->tend;
1700 if (ra < tbgn || ra >= tend)
1701 {
1702 if (!__collector_check_segment (ra, &tbgn, &tend, 0))
1703 {
1704 DprintfT (SP_DUMP_UNWIND, "unwind.c: not in segment: 0x%lX [0x%lX-0x%lX]\n",
1705 ra, wctx->tbgn, wctx->tend);
1706 return RA_FAILURE;
1707 }
1708 }
1709
1710 if (cur->cval == RA_FROMFP)
1711 {
1712 if (wctx->fp == (unsigned long) (cur->sp - 2))
1713 {
1714 if (cache_on)
1715 cache_put (wctx, RA_FROMFP);
1716 }
1717 else
1718 cur->cval = 0;
1719 }
1720
1721 unsigned long npc = adjust_ret_addr (ra, ra - tbgn, tend);
1722 if (npc == 0)
1723 {
1724 if (cur->cval == RA_FROMFP)
1725 {
1726 /* We have another evidence that we can trust this RA */
1727 DprintfT (SP_DUMP_UNWIND, "unwind.c: trusted fp, pc = 0x%lX\n", wctx->pc);
1728 wctx->pc = ra;
1729 }
1730 else
1731 {
1732 DprintfT (SP_DUMP_UNWIND, "unwind.c: 0 after adjustment\n");
1733 return RA_FAILURE;
1734 }
1735 }
1736 else
1737 wctx->pc = npc;
1738 wctx->sp = (unsigned long) cur->sp;
1739 wctx->fp = (unsigned long) cur->fp;
1740 wctx->tbgn = tbgn;
1741 wctx->tend = tend;
1742 return RA_SUCCESS;
1743 }
1744
1745 static int
1746 process_return (struct WalkContext *wctx, struct AdvWalkContext *cur)
1747 {
1748 return process_return_real (wctx, cur, 1);
1749 }
1750
1751 static void
1752 omp_cache_put (unsigned long *cur_sp_safe, struct WalkContext * wctx_pc_save,
1753 struct WalkContext *wctx, uint32_t val)
1754 {
1755 if (omp_no_walk && (OmpCurCtxs == NULL || OmpCtxs == NULL || OmpVals == NULL || OmpRAs == NULL))
1756 {
1757 size_t sz = OmpValTableSize * sizeof (*OmpCurCtxs);
1758 OmpCurCtxs = (struct WalkContext *) __collector_allocCSize (__collector_heap, sz, 1);
1759 sz = OmpValTableSize * sizeof (*OmpCtxs);
1760 OmpCtxs = (struct WalkContext *) __collector_allocCSize (__collector_heap, sz, 1);
1761 sz = OmpValTableSize * sizeof (*OmpVals);
1762 OmpVals = (uint32_t*) __collector_allocCSize (__collector_heap, sz, 1);
1763 sz = OmpValTableSize * sizeof (*OmpRAs);
1764 OmpRAs = (unsigned long*) __collector_allocCSize (__collector_heap, sz, 1);
1765 }
1766 if (OmpCurCtxs == NULL || OmpCtxs == NULL || OmpVals == NULL || OmpRAs == NULL)
1767 return;
1768
1769 #define USE_18434988_OMP_CACHE_WORKAROUND
1770 #ifndef USE_18434988_OMP_CACHE_WORKAROUND
1771 uint64_t idx = wctx_pc_save->pc * ROOT_IDX;
1772 OmpVals[ idx % OmpValTableSize ] = val;
1773 idx = (idx + val) * ROOT_IDX;
1774 __collector_memcpy (&(OmpCurCtxs[ idx % OmpValTableSize ]), wctx_pc_save, sizeof (struct WalkContext));
1775 idx = (idx + val) * ROOT_IDX;
1776 __collector_memcpy (&(OmpCtxs[ idx % OmpValTableSize ]), wctx, sizeof (struct WalkContext));
1777 #endif
1778 unsigned long *sp = NULL;
1779 unsigned long fp = wctx_pc_save->fp;
1780 int from_fp = 0;
1781 if (val == RA_END_OF_STACK)
1782 {
1783 sp = (unsigned long *) (wctx->sp);
1784 sp--;
1785 TprintfT (DBG_LT1, "omp_cache_put: get sp from EOS, sp=%p\n", sp);
1786 }
1787 else
1788 {
1789 if (fp < wctx_pc_save->sp || fp >= wctx_pc_save->sbase - sizeof (*sp))
1790 {
1791 sp = (unsigned long *) (wctx->sp);
1792 sp--;
1793 TprintfT (DBG_LT1, "omp_cache_put: get sp from sp, sp=%p\n", sp);
1794 }
1795 else
1796 {
1797 TprintfT (DBG_LT1, "omp_cache_put: get sp from fp=0x%lx\n", fp);
1798 sp = (unsigned long *) fp;
1799 from_fp = 1;
1800 }
1801 }
1802
1803 if (sp < cur_sp_safe || ((unsigned long) sp >= wctx->sbase))
1804 return;
1805
1806 unsigned long ra = *sp++;
1807 if (from_fp)
1808 {
1809 unsigned long tbgn = wctx_pc_save->tbgn;
1810 unsigned long tend = wctx_pc_save->tend;
1811 if (ra < tbgn || ra >= tend)
1812 {
1813 sp = (unsigned long *) (wctx->sp);
1814 sp--;
1815 ra = *sp++;
1816 }
1817 }
1818 #ifdef USE_18434988_OMP_CACHE_WORKAROUND
1819 uint64_t idx1 = wctx_pc_save->pc * ROOT_IDX;
1820 uint64_t idx2 = (idx1 + val) * ROOT_IDX;
1821 uint64_t idx3 = (idx2 + val) * ROOT_IDX;
1822 uint64_t idx4 = (idx3 + val) * ROOT_IDX;
1823 OmpRAs [ idx4 % OmpValTableSize ] = 0; // lock
1824 OmpVals[ idx1 % OmpValTableSize ] = val;
1825 __collector_memcpy (&(OmpCurCtxs[ idx2 % OmpValTableSize ]), wctx_pc_save, sizeof (struct WalkContext));
1826 __collector_memcpy (&(OmpCtxs [ idx3 % OmpValTableSize ]), wctx, sizeof (struct WalkContext));
1827 OmpRAs [ idx4 % OmpValTableSize ] = ra;
1828 #else
1829 idx = (idx + val) * ROOT_IDX;
1830 OmpRAs[ idx % OmpValTableSize ] = ra;
1831 #endif
1832 TprintfT (DBG_LT1, "omp_cache_put: pc=0x%lx\n", wctx_pc_save->pc);
1833 }
1834
1835 /*
1836 * See bug 17166877 - malloc_internal unwind failure.
1837 * Sometimes there are several calls right after ret, like:
1838 * leave
1839 * ret
1840 * call xxx
1841 * call xxxx
1842 * call xxxxx
1843 * If they are also jump targets, we should better not
1844 * create new jump context for those, since they may
1845 * end up into some other function.
1846 */
1847 static int
1848 is_after_ret (unsigned char * npc)
1849 {
1850 if (*npc != 0xe8)
1851 return 0;
1852 unsigned char * onpc = npc;
1853 int ncall = 1;
1854 int maxsteps = 10;
1855 int mincalls = 3;
1856 int steps = 0;
1857 while (*(npc - 5) == 0xe8 && steps < maxsteps)
1858 {
1859 npc -= 5;
1860 ncall++;
1861 steps++;
1862 }
1863 if (*(npc - 1) != 0xc3 || *(npc - 2) != 0xc9)
1864 return 0;
1865 steps = 0;
1866 while (*(onpc + 5) == 0xe8 && steps < maxsteps)
1867 {
1868 onpc += 5;
1869 ncall++;
1870 steps++;
1871 }
1872 if (ncall < mincalls)
1873 return 0;
1874 return 1;
1875 }
1876
1877 static int
1878 find_i386_ret_addr (struct WalkContext *wctx, int do_walk)
1879 {
1880 if (wctx->sp == 0)
1881 // Some artificial contexts may have %sp set to 0. See SETFUNCTIONCONTEXT()
1882 return RA_FAILURE;
1883
1884 /* Check cached values */
1885 int retc = cache_get (wctx);
1886 if (retc != RA_FAILURE)
1887 return retc;
1888
1889 /* An attempt to perform code analysis for call stack tracing */
1890 unsigned char opcode;
1891 unsigned char extop;
1892 unsigned char extop2;
1893 unsigned char modrm;
1894 int imm8; /* immediate operand, byte */
1895 int immv; /* immediate operand, word(2) or doubleword(4) */
1896 int reg; /* register code */
1897
1898 /* Buffer for branch targets (analysis stoppers) */
1899 unsigned char *targets[MAXTRGTS];
1900 int ntrg = 0; /* number of entries in the table */
1901 targets[ntrg++] = (unsigned char*) wctx->pc;
1902 targets[ntrg++] = (unsigned char*) - 1;
1903
1904 struct AdvWalkContext buf[MAXCTX];
1905 struct AdvWalkContext *cur = buf;
1906 CALL_UTIL (memset)((void*) cur, 0, sizeof (*cur));
1907
1908 cur->pc = (unsigned char*) wctx->pc;
1909 cur->sp = (unsigned long*) wctx->sp;
1910 cur->sp_safe = cur->sp - RED_ZONE; /* allow for the 128-byte red zone on amd64 */
1911 cur->fp = (unsigned long*) wctx->fp;
1912 cur->tidx = 1;
1913 DprintfT (SP_DUMP_UNWIND, "\nstack_unwind (x86 walk):%d %p start\n", __LINE__, cur->pc);
1914
1915 int nctx = 1; /* number of contexts being processed */
1916 int cnt = 8192; /* number of instructions to analyse */
1917
1918 /*
1919 * The basic idea of our x86 stack unwind is that we don't know
1920 * if we can trust the frame-pointer register. So we walk
1921 * instructions to find a return instruction, at which point
1922 * we know the return address is on the top of the stack, etc.
1923 *
1924 * A severe challenge to walking x86 instructions is when we
1925 * encounter "jmp *(reg)" instructions, where we are expected
1926 * to jump to the (unknown-to-us) contents of a register.
1927 *
1928 * The "jmp_reg" code here attempts to keep track of the
1929 * context for such a jump, deferring any handling of such
1930 * a difficult case. We continue with other contexts, hoping
1931 * that some other walk will take us to a return instruction.
1932 *
1933 * If no other walk helps, we return to "jmp_reg" contexts.
1934 * While we don't know the jump target, it is possible that the
1935 * bytes immediately following the jmp_reg instruction represent
1936 * one possible target, as might be the case when a "switch"
1937 * statement is compiled.
1938 *
1939 * Unfortunately, the bytes following a "jmp_reg" instruction might
1940 * instead be a jump target from somewhere else -- execution might
1941 * never "fall through" from the preceding "jmp_reg". Those bytes
1942 * might not even be instructions at all. There are many uses of
1943 * jmp_reg instructions beyond just compiling switch statements.
1944 *
1945 * So walking the bytes after a "jmp_reg" instruction can lead
1946 * to bugs and undefined behavior, including SEGV and core dump.
1947 *
1948 * We currently do not really understand the "jmp_reg" code below.
1949 */
1950 int jmp_reg_switch_mode = 0;
1951 int num_jmp_reg = 0; // number of jmp *reg met when switch mode is off or when in current switch case
1952 int total_num_jmp_reg = 0; // number of total jmp *reg met
1953 struct AdvWalkContext * jmp_reg_ctx[MAXJMPREG]; // context of jmp *reg met when switch mode is off or when in current switch case
1954 struct AdvWalkContext * jmp_reg_switch_ctx[MAXJMPREG]; // context of jmp *reg used in switch cases
1955 struct AdvWalkContext * jmp_reg_switch_backup_ctx = NULL; // context of the first jmp *reg used in switch cases
1956
1957 int cur_jmp_reg_switch = 0; // current switch table
1958 int num_jmp_reg_switch = 0; // number of switch table
1959 int jmp_reg_switch_case = 0; // case number in current switch table
1960 unsigned char * jmp_reg_switch_pc = NULL; // the start pc of current switch case
1961 unsigned char * jmp_reg_switch_pc_old = NULL; // backup for deleteing context of jump target
1962 unsigned char * jmp_reg_switch_base = NULL; // start pc for checking offsets
1963 int max_jmp_reg_switch_case = 2;
1964 #if WSIZE(32)
1965 int max_switch_pc_offset = 512;
1966 #else // WSIZE(64)
1967 int max_switch_pc_offset = 1024;
1968 #endif
1969 int expected_num_jmp_reg = 1; // should be smaller than MAXJMPREG
1970 int max_num_jmp_reg_seen = 4; // try to resolve return if there are so many such instructions
1971
1972
1973 int save_ctx = 0; // flag to save walk context in the cache to speed up unwind
1974 struct WalkContext wctx_pc_save;
1975 if (do_walk == 0)
1976 // do_walk is the flag indicating not walking through the instructions, resolving the RA from the stack fp first
1977 __collector_memcpy (&wctx_pc_save, wctx, sizeof (struct WalkContext));
1978
1979 startWalk:
1980 if (do_walk == 0)
1981 { // try to resolve RA from stack frame pointer
1982 if (OmpCurCtxs == NULL || OmpCtxs == NULL || OmpVals == NULL || OmpRAs == NULL)
1983 {
1984 do_walk = 1;
1985 goto startWalk;
1986 }
1987 // before goto checkFP, try the RA from cache (key: WalkContext -> value: caller's WalkContext))
1988 uint64_t idx = wctx->pc * ROOT_IDX;
1989 uint32_t val = OmpVals[idx % OmpValTableSize];
1990 idx = (idx + val) * ROOT_IDX;
1991 #ifdef USE_18434988_OMP_CACHE_WORKAROUND
1992 // Check ra: if it is 0 - then cache is invalid
1993 uint64_t idx4;
1994 idx4 = (idx + val) * ROOT_IDX;
1995 idx4 = (idx4 + val) * ROOT_IDX;
1996 if (0 == OmpRAs[ idx4 % OmpValTableSize ]) // Invalid cache
1997 goto checkFP;
1998 #endif
1999 struct WalkContext saved_ctx;
2000 __collector_memcpy (&saved_ctx, &OmpCurCtxs[ idx % OmpValTableSize ], sizeof (struct WalkContext));
2001 if (wctx->pc == saved_ctx.pc
2002 && wctx->sp == saved_ctx.sp
2003 && wctx->fp == saved_ctx.fp
2004 && wctx->tbgn == saved_ctx.tbgn
2005 && wctx->tend == saved_ctx.tend)
2006 { // key match, RA may be valid
2007 idx = (idx + val) * ROOT_IDX;
2008 unsigned long *sp = NULL;
2009 unsigned long fp = wctx->fp;
2010 int from_fp = 0;
2011 if (val == RA_END_OF_STACK)
2012 {
2013 DprintfT (SP_DUMP_UNWIND, "find_i386_ret_addr:%d -- RA_END_OF_STACK: pc=0x%lx\n", __LINE__, wctx->pc);
2014 __collector_memcpy (wctx, &OmpCtxs[ idx % OmpValTableSize ], sizeof (struct WalkContext));
2015 return val;
2016 }
2017 else
2018 {
2019 if (fp < wctx->sp || fp >= wctx->sbase - sizeof (*sp))
2020 {
2021 TprintfT (DBG_LT1, "omp_cache_get -- wrong fp: pc=0x%lx\n", wctx->pc);
2022 sp = (unsigned long *) (OmpCtxs[ idx % OmpValTableSize ].sp);
2023 sp--;
2024 if (sp < cur->sp_safe || (unsigned long) sp >= wctx->sbase)
2025 {
2026 goto checkFP;
2027 }
2028 unsigned long ra = *sp;
2029 uint64_t idx2 = (idx + val) * ROOT_IDX;
2030 if (OmpRAs[ idx2 % OmpValTableSize ] == ra)
2031 {
2032 __collector_memcpy (wctx, &OmpCtxs[ idx % OmpValTableSize ], sizeof (struct WalkContext));
2033 TprintfT (DBG_LT1, "omp_cache_get -- ra match with target sp: pc=0x%lx, ra=0x%lx, val=%d\n", wctx->pc, ra, val);
2034 return val;
2035 }
2036 TprintfT (DBG_LT1, "omp_cache_get -- ra mismatch: ra=0x%lx, expected ra=0x%lx, val=%d\n", ra, OmpRAs[ idx2 % OmpValTableSize ], val);
2037 goto checkFP;
2038 }
2039 sp = (unsigned long *) fp;
2040 from_fp = 1;
2041 }
2042
2043 uint64_t idx2 = (idx + val) * ROOT_IDX;
2044 unsigned long ra = *sp++;
2045 if (from_fp)
2046 {
2047 unsigned long tbgn = wctx->tbgn;
2048 unsigned long tend = wctx->tend;
2049 if (ra < tbgn || ra >= tend)
2050 {
2051 sp = (unsigned long *) (OmpCtxs[ idx % OmpValTableSize ].sp);
2052 sp--;
2053 //if (sp < cur->sp_safe - 16 || (unsigned long)sp >= wctx->sbase - sizeof(*sp)) {
2054 // The check above was replaced with the check below,
2055 // because we do not know why "- 16" and "- sizeof(*sp)" was used.
2056 if (sp < cur->sp_safe || (unsigned long) sp >= wctx->sbase)
2057 goto checkFP;
2058 else
2059 ra = *sp;
2060 }
2061 }
2062 if (OmpRAs[ idx2 % OmpValTableSize ] == ra)
2063 {
2064 TprintfT (DBG_LT1, "omp_cache_get -- ra match: pc=0x%lx\n", wctx->pc);
2065 __collector_memcpy (wctx, &OmpCtxs[ idx % OmpValTableSize ], sizeof (struct WalkContext));
2066 return val;
2067 }
2068 }
2069 goto checkFP;
2070 }
2071 else
2072 {
2073 CALL_UTIL (memset)(jmp_reg_ctx, 0, MAXJMPREG * sizeof (struct AdvWalkContext *));
2074 CALL_UTIL (memset)(jmp_reg_switch_ctx, 0, MAXJMPREG * sizeof (struct AdvWalkContext *));
2075 }
2076 while (cnt--)
2077 {
2078 if (nctx == 0 && (num_jmp_reg == expected_num_jmp_reg || jmp_reg_switch_mode == 1))
2079 { // no context available, try jmp switch mode
2080 int i = 0;
2081 if (num_jmp_reg == expected_num_jmp_reg)
2082 jmp_reg_switch_mode = 0; // first jmp reg expected, restart switch mode
2083 DprintfT (SP_DUMP_UNWIND, "unwind.c: begin switch mode, num_jmp_reg = %d, jmp_reg_switch_backup_ctx=%p, jmp_reg_switch_case=%d, jmp_reg_switch_mode=%d.\n",
2084 num_jmp_reg, jmp_reg_switch_backup_ctx, jmp_reg_switch_case, jmp_reg_switch_mode);
2085 // the ideal asm of switch is
2086 // jmp reg
2087 // ...//case 1
2088 // ret
2089 // ...//case 2
2090 // ret
2091 // ...//etc
2092 if (jmp_reg_switch_mode == 0)
2093 {
2094 num_jmp_reg_switch = num_jmp_reg; // backup num_jmp_reg
2095 jmp_reg_switch_mode = 1; // begin switch mode
2096 for (i = 0; i < num_jmp_reg_switch; i++)
2097 {
2098 if (jmp_reg_switch_ctx[i] == NULL)
2099 jmp_reg_switch_ctx[i] = (struct AdvWalkContext*) alloca (sizeof (*jmp_reg_switch_ctx[i]));
2100 if (jmp_reg_switch_ctx[i] != NULL)
2101 { // backup jmp_reg_ctx
2102 __collector_memcpy (jmp_reg_switch_ctx[i], jmp_reg_ctx[i], sizeof (*jmp_reg_switch_ctx[i]));
2103 cur_jmp_reg_switch = 0; // reset the current switch table
2104 jmp_reg_switch_case = 0; // reset the case number in current switch table
2105 }
2106 }
2107 if (jmp_reg_switch_backup_ctx == NULL)
2108 { // only backup when the first jmp *reg is met for restoring later, if switch mode fails to resolve RA
2109 jmp_reg_switch_backup_ctx = (struct AdvWalkContext*) alloca (sizeof (*jmp_reg_switch_backup_ctx));
2110 if (jmp_reg_switch_backup_ctx != NULL)
2111 __collector_memcpy (jmp_reg_switch_backup_ctx, cur, sizeof (*cur));
2112 DprintfT (SP_DUMP_UNWIND, "unwind.c: back up context for switch mode.\n");
2113 }
2114 }
2115 if (jmp_reg_switch_mode == 1)
2116 { // in the process of trying switch cases
2117 if (cur_jmp_reg_switch == num_jmp_reg_switch)
2118 {
2119 DprintfT (SP_DUMP_UNWIND, "unwind.c: have tried all switch with max_jmp_reg_switch_case for each\n");
2120 if (jmp_reg_switch_backup_ctx != NULL)
2121 __collector_memcpy (cur, jmp_reg_switch_backup_ctx, sizeof (*cur));
2122 int rc = process_return_real (wctx, cur, 0);
2123 if (rc == RA_SUCCESS)
2124 {
2125 if (save_ctx)
2126 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
2127 return rc;
2128 }
2129 break; // have tried all switch with max_jmp_reg_switch_case for each, goto checkFP
2130 }
2131 unsigned char *npc = jmp_reg_switch_ctx[cur_jmp_reg_switch]->pc;
2132 if (jmp_reg_switch_case == 0)
2133 // first switch case
2134 npc = check_modrm (npc); // pc next to "jmp reg" instruction
2135 else if (jmp_reg_switch_pc != NULL)
2136 npc = jmp_reg_switch_pc; // // pc next to "ret" instruction of previous case
2137 else
2138 {
2139 DprintfT (SP_DUMP_UNWIND, "unwind.c: unexpected jum switch mode situation, jmp_reg_switch_case=%d, jmp_reg_switch_pc=%p\n",
2140 jmp_reg_switch_case, jmp_reg_switch_pc);
2141 break; //goto checkFP
2142 }
2143 jmp_reg_switch_base = npc;
2144 struct AdvWalkContext *new = buf + nctx;
2145 nctx += 1;
2146 __collector_memcpy (new, jmp_reg_switch_ctx[cur_jmp_reg_switch], sizeof (*new));
2147 new->pc = npc;
2148 cur = new; /* advance the new context first */
2149 jmp_reg_switch_pc = NULL;
2150 jmp_reg_switch_case++;
2151 if (jmp_reg_switch_case == max_jmp_reg_switch_case)
2152 { // done many cases, change to another switch table
2153 cur_jmp_reg_switch++;
2154 jmp_reg_switch_case = 0;
2155 }
2156 }
2157 num_jmp_reg = 0;
2158 }
2159 if (jmp_reg_switch_mode == 1)
2160 { // when processing switch cases, check pc each time
2161 unsigned long tbgn = wctx->tbgn;
2162 unsigned long tend = wctx->tend;
2163 if ((unsigned long) (cur->pc) < tbgn || (unsigned long) (cur->pc) >= tend)
2164 {
2165 DprintfT (SP_DUMP_UNWIND, "unwind.c: pc out of range, pc=0x%lx\n", (unsigned long) (cur->pc));
2166 break;
2167 }
2168 if (jmp_reg_switch_base != NULL && cur->pc > jmp_reg_switch_base + max_switch_pc_offset)
2169 {
2170 DprintfT (SP_DUMP_UNWIND, "unwind.c: limit the walk offset after jmp reg instruction\n");
2171 if (jmp_reg_switch_backup_ctx != NULL)
2172 __collector_memcpy (cur, jmp_reg_switch_backup_ctx, sizeof (*cur));
2173 int rc = process_return_real (wctx, cur, 0);
2174 if (rc == RA_SUCCESS)
2175 {
2176 if (save_ctx)
2177 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
2178 return rc;
2179 }
2180 break; // limit the walk offset after jmp reg instruction, got checkFP
2181 }
2182 }
2183
2184 if (nctx == 0)
2185 break;
2186 // dump_targets (__LINE__, ntrg, targets);
2187 while (cur->pc > targets[cur->tidx])
2188 cur->tidx += 1;
2189 if (cur->pc == targets[cur->tidx])
2190 {
2191 /* Stop analysis. Delete context. */
2192 if (jmp_reg_switch_mode == 0 || cur->pc != jmp_reg_switch_pc_old)
2193 {
2194 if (jmp_reg_switch_mode == 1 && nctx == 1 && jmp_reg_switch_pc == NULL)
2195 {
2196 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d old target, cur->pc=%p, jmp_reg_switch_pc=%p, nctx=%d\n",
2197 __LINE__, cur->pc, jmp_reg_switch_pc, nctx);
2198 jmp_reg_switch_pc = cur->pc; // save cp before delete context, may be used as a start of switch case
2199 jmp_reg_switch_pc_old = jmp_reg_switch_pc;
2200 }
2201 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, old target.\n", __LINE__);
2202 DELETE_CURCTX ();
2203 if (cur >= buf + nctx)
2204 cur = buf;
2205 continue;
2206 }
2207 if (jmp_reg_switch_mode == 1 && cur->pc == jmp_reg_switch_pc_old)
2208 jmp_reg_switch_pc_old = NULL; // reset jmp_reg_switch_pc_old to delete the context later when cur->pc != jmp_reg_switch_pc_old
2209 }
2210
2211 /* let's walk the next x86 instruction */
2212 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d cur:%ld pc=0x%lx %02x %02x %02x %02x %02x %02x %02x sp=0x%lx\n",
2213 __LINE__, (long) (cur - buf), (unsigned long) cur->pc,
2214 (int) cur->pc[0], (int) cur->pc[1], (int) cur->pc[2],
2215 (int) cur->pc[3], (int) cur->pc[4], (int) cur->pc[5],
2216 (int) cur->pc[6], (unsigned long) cur->sp);
2217 int v = 4; /* Operand size */
2218 int a = 4; /* Address size */
2219 /* int W = 0; REX.W bit */
2220 #if WSIZE(64)
2221 int R = 0; /* REX.R bit */
2222 #endif
2223 int X = 0; /* REX.X bit */
2224 int B = 0; /* REX.B bit */
2225 /* Check prefixes */
2226 int done = 0;
2227 while (!done)
2228 {
2229 opcode = *cur->pc++;
2230 switch (opcode)
2231 {
2232 case 0x66: /* opd size override */
2233 v = 2;
2234 break;
2235 case 0x67: /*addr size override */
2236 a = 2;
2237 break;
2238 #if WSIZE(64)
2239 case 0x40: /* REX */
2240 case 0x41:
2241 case 0x42:
2242 case 0x43:
2243 case 0x44:
2244 case 0x45:
2245 case 0x46:
2246 case 0x47:
2247 case 0x48:
2248 case 0x49:
2249 case 0x4a:
2250 case 0x4b:
2251 case 0x4c:
2252 case 0x4d:
2253 case 0x4e:
2254 case 0x4f:
2255 B = (opcode & 0x1) ? 8 : 0;
2256 X = (opcode & 0x2) ? 8 : 0;
2257 R = (opcode & 0x4) ? 8 : 0;
2258 if (opcode & 0x8) /* 64 bit operand size */
2259 v = 8;
2260 opcode = *cur->pc++;
2261 done = 1;
2262 break;
2263 #endif
2264 default:
2265 done = 1;
2266 break;
2267 }
2268 }
2269 int z = (v == 8) ? 4 : v;
2270 switch (opcode)
2271 {
2272 case 0x0: /* add Eb,Gb */
2273 case 0x01: /* add Ev,Gv */
2274 case 0x02: /* add Gb,Eb */
2275 case 0x03: /* add Gv,Ev */
2276 cur->pc = check_modrm (cur->pc);
2277 break;
2278 case 0x04: /* add %al,Ib */
2279 cur->pc += 1;
2280 break;
2281 case 0x05: /* add %eax,Iz */
2282 cur->pc += z;
2283 break;
2284 case 0x06: /* push es */
2285 cur->sp -= 1;
2286 break;
2287 case 0x07: /* pop es */
2288 cur->sp += 1;
2289 if (cur->sp - RED_ZONE > cur->sp_safe)
2290 cur->sp_safe = cur->sp - RED_ZONE;
2291 break;
2292 case 0x08: /* or Eb,Gb */
2293 case 0x09: /* or Ev,Gv */
2294 case 0x0a: /* or Gb,Eb */
2295 case 0x0b: /* or Gv,Ev */
2296 cur->pc = check_modrm (cur->pc);
2297 break;
2298 case 0x0c: /* or %al,Ib */
2299 cur->pc += 1;
2300 break;
2301 case 0x0d: /* or %eax,Iz */
2302 cur->pc += z;
2303 break;
2304 case 0x0e: /* push cs */
2305 cur->sp -= 1;
2306 break;
2307 case 0x0f: /* two-byte opcodes */
2308 extop = *cur->pc++;
2309 switch (extop)
2310 { /* RTM or HLE */
2311 case 0x01:
2312 extop2 = *cur->pc;
2313 switch (extop2)
2314 {
2315 case 0xd5: /* xend */
2316 case 0xd6: /* xtest */
2317 cur->pc++;
2318 break;
2319 default:
2320 break;
2321 }
2322 break;
2323 case 0x03:
2324 cur->pc = check_modrm (cur->pc);
2325 break;
2326 case 0x0b:
2327 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, undefined instruction. opcode=0x%02x\n",
2328 __LINE__, (int) opcode);
2329 DELETE_CURCTX ();
2330 break;
2331 case 0x05: /* syscall */
2332 case 0x34: /* sysenter */
2333 if (cur->rax == __NR_exit)
2334 {
2335 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode=0x%02x\n",
2336 __LINE__, (int) opcode);
2337 DELETE_CURCTX ();
2338 break;
2339 }
2340 else if (cur->rax == __NR_rt_sigreturn)
2341 {
2342 if (jmp_reg_switch_mode == 1)
2343 {
2344 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d give up return address under jmp switch mode, opcode=0x%02x\n",
2345 __LINE__, (int) opcode);
2346 goto checkFP;
2347 }
2348 wctx->sp = (unsigned long) cur->sp;
2349 if (save_ctx)
2350 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_RT_SIGRETURN);
2351 return RA_RT_SIGRETURN;
2352 }
2353 #if WSIZE(32)
2354 else if (cur->rax == __NR_sigreturn)
2355 {
2356 if (jmp_reg_switch_mode == 1)
2357 {
2358 DprintfT (SP_DUMP_UNWIND, "unwind.c: give up return address under jmp switch mode, opcode = 0x34\n");
2359 goto checkFP;
2360 }
2361 wctx->sp = (unsigned long) cur->sp;
2362 if (save_ctx)
2363 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_SIGRETURN);
2364 return RA_SIGRETURN;
2365 }
2366 #endif
2367 /* Check for Linus' trick in the vsyscall page */
2368 while (*cur->pc == 0x90) /* nop */
2369 cur->pc++;
2370 if (*cur->pc == 0xeb) /* jmp imm8 */
2371 cur->pc += 2;
2372 break;
2373 case 0x0d: /* nop Ev */
2374 cur->pc = check_modrm (cur->pc);
2375 break;
2376 case 0x10: /* xmm Vq,Wq */
2377 case 0x11:
2378 case 0x12:
2379 case 0x13:
2380 case 0x14:
2381 case 0x15:
2382 case 0x16:
2383 case 0x17:
2384 cur->pc = check_modrm (cur->pc);
2385 break;
2386 case 0x18: /* prefetch */
2387 cur->pc = check_modrm (cur->pc);
2388 break;
2389 case 0x1E: /* endbr64/endbr32 (f3 0f 1e .. ) is parsing as repz nop edx */
2390 cur->pc += 2;
2391 break;
2392 case 0x1f: /* nop Ev */
2393 cur->pc = check_modrm (cur->pc);
2394 break;
2395 case 0x28: /* xmm Vq,Wq */
2396 case 0x29:
2397 case 0x2a:
2398 case 0x2b:
2399 case 0x2c:
2400 case 0x2d:
2401 case 0x2e:
2402 case 0x2f:
2403 cur->pc = check_modrm (cur->pc);
2404 break;
2405 case 0x30: /* wrmsr */
2406 case 0x31: /* rdtsc */
2407 case 0x32: /* rdmsr */
2408 case 0x33: /* rdpmc */
2409 break;
2410 /* case 0x34: sysenter (see above) */
2411 case 0x38: case 0x3a:
2412 extop2 = *cur->pc++;
2413 cur->pc = check_modrm (cur->pc);
2414 // 21275311 Unwind failure in native stack for java application running on jdk8
2415 // Three-byte opcodes "66 0f 3a ??" should consume an additional "immediate" byte.
2416 if (extop == 0x3a)
2417 cur->pc++;
2418 break;
2419 case 0x40: case 0x41: case 0x42: case 0x43: /* CMOVcc Gv,Ev */
2420 case 0x44: case 0x45: case 0x46: case 0x47:
2421 case 0x48: case 0x49: case 0x4a: case 0x4b:
2422 case 0x4c: case 0x4d: case 0x4e: case 0x4f:
2423 cur->pc = check_modrm (cur->pc);
2424 break;
2425 case 0x50: case 0x51: case 0x52: case 0x53:
2426 case 0x54: case 0x55: case 0x56: case 0x57:
2427 case 0x58: case 0x59: case 0x5a: case 0x5b:
2428 case 0x5c: case 0x5d: case 0x5e: case 0x5f:
2429 case 0x60: case 0x61: case 0x62: case 0x63:
2430 case 0x64: case 0x65: case 0x66: case 0x67:
2431 case 0x68: case 0x69: case 0x6a: case 0x6b:
2432 case 0x6c: case 0x6d: case 0x6e: case 0x6f:
2433 cur->pc = check_modrm (cur->pc);
2434 break;
2435 case 0x70: case 0x71: case 0x72: case 0x73:
2436 cur->pc = check_modrm (cur->pc) + 1;
2437 break;
2438 case 0x74: case 0x75: case 0x76:
2439 cur->pc = check_modrm (cur->pc);
2440 break;
2441 case 0x77:
2442 break;
2443 case 0x7c: case 0x7d: case 0x7e: case 0x7f:
2444 cur->pc = check_modrm (cur->pc);
2445 break;
2446 case 0x80: case 0x81: case 0x82: case 0x83: /* Jcc Jz */
2447 case 0x84: case 0x85: case 0x86: case 0x87:
2448 case 0x88: case 0x89: case 0x8a: case 0x8b:
2449 case 0x8c: case 0x8d: case 0x8e: case 0x8f:
2450 immv = read_int (cur->pc, z);
2451 cur->pc += z;
2452 if (nctx < (jmp_reg_switch_mode ? MAXJMPREGCTX : MAXCTX))
2453 {
2454 int tidx = 0;
2455 unsigned char *npc = cur->pc + immv;
2456 if ((unsigned long) npc < wctx->tbgn || (unsigned long) npc >= wctx->tend)
2457 {
2458 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode=0x%02x\n",
2459 __LINE__, (int) opcode);
2460 DELETE_CURCTX ();
2461 break;
2462 }
2463 if (is_after_ret (npc))
2464 break;
2465 while (npc > targets[tidx])
2466 tidx += 1;
2467 if (npc != targets[tidx])
2468 {
2469 if (ntrg < MAXTRGTS)
2470 {
2471 for (int i = 0; i < nctx; i++)
2472 if (buf[i].tidx >= tidx)
2473 buf[i].tidx++;
2474
2475 /* insert a new target */
2476 for (int i = ntrg; i > tidx; i--)
2477 targets[i] = targets[i - 1];
2478 ntrg += 1;
2479 targets[tidx++] = npc;
2480 }
2481 else
2482 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d ntrg=max(%d)\n",
2483 __LINE__, ntrg);
2484 struct AdvWalkContext *new = buf + nctx;
2485 nctx += 1;
2486 __collector_memcpy (new, cur, sizeof (*new));
2487 new->pc = npc;
2488 new->tidx = tidx;
2489 cur = new; /* advance the new context first */
2490 continue;
2491 }
2492 }
2493 else
2494 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d nctx=max(%d)\n",
2495 __LINE__, ntrg);
2496 break;
2497 case 0x90: case 0x91: case 0x92: case 0x93: /* setcc Eb */
2498 case 0x94: case 0x95: case 0x96: case 0x97:
2499 case 0x98: case 0x99: case 0x9a: case 0x9b:
2500 case 0x9c: case 0x9d: case 0x9e: case 0x9f:
2501 cur->pc = check_modrm (cur->pc);
2502 break;
2503 case 0xa0: /* push fs */
2504 cur->sp -= 1;
2505 break;
2506 case 0xa1: /* pop fs */
2507 cur->sp += 1;
2508 if (cur->sp - RED_ZONE > cur->sp_safe)
2509 cur->sp_safe = cur->sp - RED_ZONE;
2510 break;
2511 case 0xa2: /* cpuid */
2512 break;
2513 case 0xa3: /* bt Ev,Gv */
2514 cur->pc = check_modrm (cur->pc);
2515 break;
2516 case 0xa4: /* shld Ev,Gv,Ib */
2517 cur->pc = check_modrm (cur->pc);
2518 cur->pc += 1;
2519 break;
2520 case 0xa5: /* shld Ev,Gv,%cl */
2521 cur->pc = check_modrm (cur->pc);
2522 break;
2523 case 0xa8: /* push gs */
2524 cur->sp -= 1;
2525 break;
2526 case 0xa9: /* pop gs */
2527 cur->sp += 1;
2528 if (cur->sp - RED_ZONE > cur->sp_safe)
2529 cur->sp_safe = cur->sp - RED_ZONE;
2530 break;
2531 case 0xaa: /* rsm */
2532 break;
2533 case 0xab: /* bts Ev,Gv */
2534 cur->pc = check_modrm (cur->pc);
2535 break;
2536 case 0xac: /* shrd Ev,Gv,Ib */
2537 cur->pc = check_modrm (cur->pc);
2538 cur->pc += 1;
2539 break;
2540 case 0xad: /* shrd Ev,Gv,%cl */
2541 cur->pc = check_modrm (cur->pc);
2542 break;
2543 case 0xae: /* group15 */
2544 cur->pc = check_modrm (cur->pc);
2545 break;
2546 case 0xaf: /* imul Gv,Ev */
2547 cur->pc = check_modrm (cur->pc);
2548 break;
2549 case 0xb1: /* cmpxchg Ev,Gv */
2550 cur->pc = check_modrm (cur->pc);
2551 break;
2552 case 0xb3:
2553 case 0xb6: /* movzx Gv,Eb */
2554 case 0xb7: /* movzx Gv,Ew */
2555 cur->pc = check_modrm (cur->pc);
2556 break;
2557 case 0xba: /* group8 Ev,Ib */
2558 cur->pc = check_modrm (cur->pc);
2559 cur->pc += 1;
2560 break;
2561 case 0xbb: /* btc Ev,Gv */
2562 case 0xbc: /* bsf Gv,Ev */
2563 case 0xbd: /* bsr Gv,Ev */
2564 cur->pc = check_modrm (cur->pc);
2565 break;
2566 case 0xbe: /* movsx Gv,Eb */
2567 case 0xbf: /* movsx Gv,Ew */
2568 cur->pc = check_modrm (cur->pc);
2569 break;
2570 case 0xc0: /* xadd Eb,Gb */
2571 case 0xc1: /* xadd Ev,Gv */
2572 cur->pc = check_modrm (cur->pc);
2573 break;
2574 case 0xc2: /* cmpps V,W,Ib */
2575 cur->pc = check_modrm (cur->pc);
2576 cur->pc += 1;
2577 break;
2578 case 0xc3: /* movnti M,G */
2579 cur->pc = check_modrm (cur->pc);
2580 break;
2581 case 0xc6: /* shufps V,W,Ib */
2582 cur->pc = check_modrm (cur->pc);
2583 cur->pc += 1;
2584 break;
2585 case 0xc7: /* RDRAND */
2586 cur->pc = check_modrm (cur->pc);
2587 break;
2588 case 0xc8: case 0xc9: case 0xca: case 0xcb: /* bswap */
2589 case 0xcc: case 0xcd: case 0xce: case 0xcf:
2590 break;
2591 case 0xd0: case 0xd1: case 0xd2: case 0xd3:
2592 case 0xd4: case 0xd5: case 0xd6: case 0xd7:
2593 case 0xd8: case 0xd9: case 0xda: case 0xdb:
2594 case 0xdc: case 0xdd: case 0xde: case 0xdf:
2595 case 0xe0: case 0xe1: case 0xe2: case 0xe3:
2596 case 0xe4: case 0xe5: case 0xe6: case 0xe7:
2597 case 0xe8: case 0xe9: case 0xea: case 0xeb:
2598 case 0xec: case 0xed: case 0xee: case 0xef:
2599 case 0xf0: case 0xf1: case 0xf2: case 0xf3:
2600 case 0xf4: case 0xf5: case 0xf6: case 0xf7:
2601 case 0xf8: case 0xf9: case 0xfa: case 0xfb:
2602 case 0xfc: case 0xfd: case 0xfe: case 0xff:
2603 cur->pc = check_modrm (cur->pc);
2604 break;
2605 default:
2606 if (jmp_reg_switch_mode == 1 && extop == 0x0b)
2607 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d invalid opcode ub2: 0x0f %x jmp_reg_switch_mode=%d\n",
2608 __LINE__, (int) extop, jmp_reg_switch_mode);
2609 else
2610 {
2611 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d unknown opcode: 0x0f %x jmp_reg_switch_mode=%d\n",
2612 __LINE__, (int) extop, jmp_reg_switch_mode);
2613 DELETE_CURCTX ();
2614 }
2615 break;
2616 }
2617 break;
2618 case 0x10: /* adc Eb,Gb */
2619 case 0x11: /* adc Ev,Gv */
2620 case 0x12: /* adc Gb,Eb */
2621 case 0x13: /* adc Gv,Ev */
2622 cur->pc = check_modrm (cur->pc);
2623 break;
2624 case 0x14: /* adc %al,Ib */
2625 cur->pc += 1;
2626 break;
2627 case 0x15: /* adc %eax,Iz */
2628 cur->pc += z;
2629 break;
2630 case 0x16: /* push ss */
2631 cur->sp -= 1;
2632 break;
2633 case 0x17: /* pop ss */
2634 cur->sp += 1;
2635 if (cur->sp - RED_ZONE > cur->sp_safe)
2636 cur->sp_safe = cur->sp - RED_ZONE;
2637 break;
2638 case 0x18: /* sbb Eb,Gb */
2639 case 0x19: /* sbb Ev,Gv */
2640 case 0x1a: /* sbb Gb,Eb */
2641 case 0x1b: /* sbb Gv,Ev */
2642 cur->pc = check_modrm (cur->pc);
2643 break;
2644 case 0x1c: /* sbb %al,Ib */
2645 cur->pc += 1;
2646 break;
2647 case 0x1d: /* sbb %eax,Iz */
2648 cur->pc += z;
2649 break;
2650 case 0x1e: /* push ds */
2651 cur->sp -= 1;
2652 break;
2653 case 0x1f: /* pop ds */
2654 cur->sp += 1;
2655 if (cur->sp - RED_ZONE > cur->sp_safe)
2656 cur->sp_safe = cur->sp - RED_ZONE;
2657 break;
2658 case 0x20: /* and Eb,Gb */
2659 case 0x21: /* and Ev,Gv */
2660 case 0x22: /* and Gb,Eb */
2661 case 0x23: /* and Gv,Ev */
2662 cur->pc = check_modrm (cur->pc);
2663 break;
2664 case 0x24: /* and %al,Ib */
2665 cur->pc += 1;
2666 break;
2667 case 0x25: /* and %eax,Iz */
2668 cur->pc += z;
2669 break;
2670 case 0x26: /* seg=es prefix */
2671 break;
2672 case 0x27: /* daa */
2673 break;
2674 case 0x28: /* sub Eb,Gb */
2675 case 0x29: /* sub Ev,Gv */
2676 case 0x2a: /* sub Gb,Eb */
2677 case 0x2b: /* sub Gv,Ev */
2678 cur->pc = check_modrm (cur->pc);
2679 break;
2680 case 0x2c: /* sub %al,Ib */
2681 cur->pc += 1;
2682 break;
2683 case 0x2d: /* sub %eax,Iz */
2684 cur->pc += z;
2685 break;
2686 case 0x2e: /* seg=cs prefix */
2687 break;
2688 case 0x2f: /* das */
2689 break;
2690 case 0x30: /* xor Eb,Gb */
2691 case 0x31: /* xor Ev,Gv */
2692 case 0x32: /* xor Gb,Eb */
2693 case 0x33: /* xor Gv,Ev */
2694 cur->pc = check_modrm (cur->pc);
2695 break;
2696 case 0x34: /* xor %al,Ib */
2697 cur->pc += 1;
2698 break;
2699 case 0x35: /* xor %eax,Iz */
2700 cur->pc += z;
2701 break;
2702 case 0x36: /* seg=ss prefix */
2703 break;
2704 case 0x37: /* aaa */
2705 break;
2706 case 0x38: /* cmp Eb,Gb */
2707 case 0x39: /* cmp Ev,Gv */
2708 case 0x3a: /* cmp Gb,Eb */
2709 case 0x3b: /* cmp Gv,Ev */
2710 cur->pc = check_modrm (cur->pc);
2711 break;
2712 case 0x3c: /* cmp %al,Ib */
2713 cur->pc += 1;
2714 break;
2715 case 0x3d: /* cmp %eax,Iz */
2716 cur->pc += z;
2717 break;
2718 case 0x3e: /* seg=ds prefix */
2719 break;
2720 case 0x3f: /* aas */
2721 break;
2722 #if WSIZE(32)
2723 case 0x40: /* inc %eax */
2724 case 0x41: /* inc %ecx */
2725 case 0x42: /* inc %edx */
2726 case 0x43: /* inc %ebx */
2727 break;
2728 case 0x44: /* inc %esp */
2729 /* Can't be a valid stack pointer - delete context */
2730 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode 0x44.\n", __LINE__);
2731 DELETE_CURCTX ();
2732 break;
2733 case 0x45: /* inc %ebp */
2734 case 0x46: /* inc %esi */
2735 case 0x47: /* inc %edi */
2736 case 0x48: /* dec %eax */
2737 case 0x49: /* dec %ecx */
2738 case 0x4a: /* dec %edx */
2739 case 0x4b: /* dec %ebx */
2740 break;
2741 case 0x4c: /* dec %esp */
2742 /* Can't be a valid stack pointer - delete context */
2743 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode 0x4c.\n", __LINE__);
2744 DELETE_CURCTX ();
2745 break;
2746 case 0x4d: /* dec %ebp */
2747 case 0x4e: /* dec %esi */
2748 case 0x4f: /* dec %edi */
2749 break;
2750 #endif
2751 case 0x50: /* push %eax */
2752 case 0x51: /* push %ecx */
2753 case 0x52: /* push %edx */
2754 case 0x53: /* push %ebx */
2755 case 0x54: /* push %esp */
2756 case 0x55: /* push %ebp */
2757 case 0x56: /* push %esi */
2758 case 0x57: /* push %edi */
2759 cur->sp -= 1;
2760 reg = OPC_REG (opcode);
2761 if (reg == RBP)
2762 {
2763 #if 0
2764 /* Don't do this check yet. Affects tail calls. */
2765 /* avoid other function's prologue */
2766 if ((cur->pc[0] == 0x89 && cur->pc[1] == 0xe5) ||
2767 (cur->pc[0] == 0x8b && cur->pc[1] == 0xec))
2768 {
2769 /* mov %esp,%ebp */
2770 DELETE_CURCTX ();
2771 break;
2772 }
2773 #endif
2774 if (cur->fp_loc == NULL)
2775 {
2776 cur->fp_loc = cur->sp;
2777 cur->fp_sav = cur->fp;
2778 }
2779 }
2780 break;
2781 case 0x58: /* pop %eax */
2782 case 0x59: /* pop %ecx */
2783 case 0x5a: /* pop %edx */
2784 case 0x5b: /* pop %ebx */
2785 case 0x5c: /* pop %esp */
2786 case 0x5d: /* pop %ebp */
2787 case 0x5e: /* pop %esi */
2788 case 0x5f: /* pop %edi */
2789 reg = OPC_REG (opcode);
2790 cur->regs[reg] = 0;
2791 if (isInside ((unsigned long) cur->sp, (unsigned long) cur->sp_safe, wctx->sbase))
2792 cur->regs[reg] = *cur->sp;
2793 DprintfT (SP_DUMP_UNWIND, "stack_unwind:%d cur->regs[%d]=0x%lx\n",
2794 __LINE__, reg, (unsigned long) cur->regs[reg]);
2795 if (reg == RDX)
2796 {
2797 if (cur->sp >= cur->sp_safe &&
2798 (unsigned long) cur->sp < wctx->sbase)
2799 cur->rdx = *cur->sp;
2800 }
2801 else if (reg == RBP)
2802 {
2803 if (cur->fp_loc == cur->sp)
2804 {
2805 cur->fp = cur->fp_sav;
2806 cur->fp_loc = NULL;
2807 }
2808 else if (cur->sp >= cur->sp_safe &&
2809 (unsigned long) cur->sp < wctx->sbase)
2810 cur->fp = (unsigned long*) (*cur->sp);
2811 }
2812 else if (reg == RSP)
2813 {
2814 /* f.e. JVM I2CAdapter */
2815 if (cur->sp >= cur->sp_safe && (unsigned long) cur->sp < wctx->sbase)
2816 {
2817 unsigned long *nsp = (unsigned long*) (*cur->sp);
2818 if (nsp >= cur->sp && nsp <= cur->fp)
2819 {
2820 cur->sp = nsp;
2821 }
2822 else
2823 {
2824 DprintfT (SP_DUMP_UNWIND, "stack_unwind%d give up return address, opcode=0x%02x\n",
2825 __LINE__, opcode);
2826 goto checkFP;
2827 }
2828 }
2829 else
2830 {
2831 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d give up return address, opcode=0x%02x\n",
2832 __LINE__, opcode);
2833 goto checkFP;
2834 }
2835 break;
2836 }
2837 cur->sp += 1;
2838 if (cur->sp - RED_ZONE > cur->sp_safe)
2839 {
2840 cur->sp_safe = cur->sp - RED_ZONE;
2841 }
2842 break;
2843 case 0x60: /* pusha(d) */
2844 cur->sp -= 8;
2845 break;
2846 case 0x61: /* popa(d) */
2847 cur->sp += 8;
2848 if (cur->sp - RED_ZONE > cur->sp_safe)
2849 cur->sp_safe = cur->sp - RED_ZONE;
2850 break;
2851 case 0x62: /* group AVX, 4-bytes EVEX prefix */
2852 {
2853 unsigned char *pc = cur->pc - 1; // points to the beginning of the instruction
2854 int len = parse_x86_AVX_instruction (pc);
2855 if (len < 4)
2856 {
2857 DELETE_CURCTX ();
2858 }
2859 else
2860 {
2861 pc += len;
2862 cur->pc = pc;
2863 }
2864 }
2865 break;
2866 case 0x63: /* arpl Ew,Gw (32) movsxd Gv,Ev (64)*/
2867 cur->pc = check_modrm (cur->pc);
2868 break;
2869 case 0x64: /* seg=fs prefix */
2870 case 0x65: /* seg=gs prefix */
2871 break;
2872 case 0x66: /* opd size override */
2873 case 0x67: /* addr size override */
2874 break;
2875 case 0x68: /* push Iz */
2876 cur->sp = (unsigned long*) ((long) cur->sp - z);
2877 cur->pc += z;
2878 break;
2879 case 0x69: /* imul Gv,Ev,Iz */
2880 cur->pc = check_modrm (cur->pc);
2881 cur->pc += z;
2882 break;
2883 case 0x6a: /* push Ib */
2884 cur->sp = (unsigned long*) ((long) cur->sp - v);
2885 cur->pc += 1;
2886 break;
2887 case 0x6b: /* imul Gv,Ev,Ib */
2888 cur->pc = check_modrm (cur->pc);
2889 cur->pc += 1;
2890 break;
2891 case 0x6c: case 0x6d: case 0x6e: case 0x6f:
2892 cur->pc = check_modrm (cur->pc);
2893 break;
2894 case 0x70: /* jo Jb */
2895 case 0x71: /* jno Jb */
2896 case 0x72: /* jb Jb */
2897 case 0x73: /* jnb Jb */
2898 case 0x74: /* jz Jb */
2899 case 0x75: /* jnz Jb */
2900 case 0x76: /* jna Jb */
2901 case 0x77: /* ja Jb */
2902 case 0x78: /* js Jb */
2903 case 0x79: /* jns Jb */
2904 case 0x7a: /* jp Jb */
2905 case 0x7b: /* jnp Jb */
2906 case 0x7c: /* jl Jb */
2907 case 0x7d: /* jge Jb */
2908 case 0x7e: /* jle Jb */
2909 case 0x7f: /* jg Jb */
2910 imm8 = *(char*) cur->pc++;
2911 if (nctx < (jmp_reg_switch_mode ? MAXJMPREGCTX : MAXCTX))
2912 {
2913 int tidx = 0;
2914 unsigned char *npc = cur->pc + imm8;
2915 if (is_after_ret (npc))
2916 break;
2917 while (npc > targets[tidx])
2918 tidx += 1;
2919 if (npc != targets[tidx])
2920 {
2921 if (ntrg < MAXTRGTS)
2922 {
2923 for (int i = 0; i < nctx; i++)
2924 if (buf[i].tidx >= tidx)
2925 buf[i].tidx++;
2926
2927 /* insert a new target */
2928 for (int i = ntrg; i > tidx; i--)
2929 targets[i] = targets[i - 1];
2930 ntrg += 1;
2931 targets[tidx++] = npc;
2932 }
2933 else
2934 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d ntrg(%d)=max\n", __LINE__, ntrg);
2935 struct AdvWalkContext *new = buf + nctx;
2936 nctx += 1;
2937 __collector_memcpy (new, cur, sizeof (*new));
2938 new->pc = npc;
2939 new->tidx = tidx;
2940 cur = new; /* advance the new context first */
2941 continue;
2942 }
2943 }
2944 else
2945 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d nctx(%d)=max\n", __LINE__, nctx);
2946 break;
2947 case 0x80: /* group1 Eb,Ib */
2948 cur->pc = check_modrm (cur->pc);
2949 cur->pc += 1;
2950 break;
2951 case 0x81: /* group1 Ev,Iz */
2952 modrm = *cur->pc;
2953 if (MRM_MOD (modrm) == 0xc0 && MRM_REGS (modrm) == RSP)
2954 {
2955 int immz = read_int (cur->pc + 1, z);
2956 extop = MRM_EXT (modrm);
2957 if (extop == 0) /* add imm32,%esp */
2958 cur->sp = (unsigned long*) ((long) cur->sp + immz);
2959 else if (extop == 4) /* and imm32,%esp */
2960 cur->sp = (unsigned long*) ((long) cur->sp & immz);
2961 else if (extop == 5) /* sub imm32,%esp */
2962 cur->sp = (unsigned long*) ((long) cur->sp - immz);
2963 if (cur->sp - RED_ZONE > cur->sp_safe)
2964 cur->sp_safe = cur->sp - RED_ZONE;
2965 }
2966 cur->pc = check_modrm (cur->pc);
2967 cur->pc += z;
2968 break;
2969 case 0x82: /* group1 Eb,Ib */
2970 cur->pc = check_modrm (cur->pc);
2971 cur->pc += 1;
2972 break;
2973 case 0x83: /* group1 Ev,Ib */
2974 modrm = *cur->pc;
2975 if (MRM_MOD (modrm) == 0xc0 && MRM_REGS (modrm) == RSP)
2976 {
2977 imm8 = (char) cur->pc[1]; /* sign extension */
2978 extop = MRM_EXT (modrm);
2979 if (extop == 0) /* add imm8,%esp */
2980 cur->sp = (unsigned long*) ((long) cur->sp + imm8);
2981 else if (extop == 4) /* and imm8,%esp */
2982 cur->sp = (unsigned long*) ((long) cur->sp & imm8);
2983 else if (extop == 5) /* sub imm8,%esp */
2984 cur->sp = (unsigned long*) ((long) cur->sp - imm8);
2985 if (cur->sp - RED_ZONE > cur->sp_safe)
2986 cur->sp_safe = cur->sp - RED_ZONE;
2987 }
2988 cur->pc = check_modrm (cur->pc);
2989 cur->pc += 1;
2990 break;
2991 case 0x84: /* test Eb,Gb */
2992 case 0x85: /* test Ev,Gv */
2993 case 0x86: /* xchg Eb,Gb */
2994 case 0x87: /* xchg Ev,Gv */
2995 cur->pc = check_modrm (cur->pc);
2996 break;
2997 case 0x88: /* mov Eb,Gb */
2998 cur->pc = check_modrm (cur->pc);
2999 break;
3000 case 0x89: /* mov Ev,Gv */
3001 modrm = *cur->pc;
3002 if (MRM_MOD (modrm) == 0xc0)
3003 {
3004 if (MRM_REGS (modrm) == RBP && MRM_REGD (modrm) == RSP)
3005 /* movl %esp,%ebp */
3006 cur->fp = cur->sp;
3007 else if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RBP)
3008 { /* mov %ebp,%esp */
3009 cur->sp = cur->fp;
3010 if (cur->sp - RED_ZONE > cur->sp_safe)
3011 cur->sp_safe = cur->sp - RED_ZONE;
3012 if (wctx->fp == (unsigned long) cur->sp)
3013 cur->cval = RA_FROMFP;
3014 }
3015 }
3016 else if (MRM_MOD (modrm) == 0x80)
3017 {
3018 if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RBP)
3019 {
3020 if (cur->pc[1] == 0x24)
3021 { /* mov %ebp,disp32(%esp) - JVM */
3022 immv = read_int (cur->pc + 2, 4);
3023 cur->fp_loc = (unsigned long*) ((char*) cur->sp + immv);
3024 cur->fp_sav = cur->fp;
3025 }
3026 }
3027 }
3028 else if (MRM_MOD (modrm) == 0x40)
3029 {
3030 if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RDX)
3031 {
3032 if (cur->pc[1] == 0x24 && cur->pc[2] == 0x0)
3033 { /* movl %edx,0(%esp) */
3034 cur->ra_loc = cur->sp;
3035 cur->ra_sav = cur->rdx;
3036 }
3037 }
3038 else if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RBP)
3039 {
3040 if (cur->pc[1] == 0x24)
3041 { /* mov %ebp,disp8(%esp) - JVM */
3042 imm8 = ((char*) (cur->pc))[2];
3043 cur->fp_loc = (unsigned long*) ((char*) cur->sp + imm8);
3044 cur->fp_sav = cur->fp;
3045 }
3046 }
3047 }
3048 else if (MRM_MOD (modrm) == 0x0)
3049 {
3050 if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RBP)
3051 {
3052 if (cur->pc[1] == 0x24)
3053 { /* mov %ebp,(%esp) */
3054 cur->fp_loc = cur->sp;
3055 cur->fp_sav = cur->fp;
3056 }
3057 }
3058 else if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RDX)
3059 {
3060 if (cur->pc[1] == 0x24)
3061 { /* movl %edx,(%esp) */
3062 cur->ra_loc = cur->sp;
3063 cur->ra_sav = cur->rdx;
3064 }
3065 }
3066 }
3067 cur->pc = check_modrm (cur->pc);
3068 break;
3069 case 0x8a: /* mov Gb,Eb */
3070 cur->pc = check_modrm (cur->pc);
3071 break;
3072 case 0x8b: /* mov Gv,Ev */
3073 modrm = *cur->pc;
3074 if (MRM_MOD (modrm) == 0xc0)
3075 {
3076 if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RBP)
3077 /* mov %esp,%ebp */
3078 cur->fp = cur->sp;
3079 else if (MRM_REGS (modrm) == RBP && MRM_REGD (modrm) == RSP)
3080 { /* mov %ebp,%esp */
3081 cur->sp = cur->fp;
3082 if (cur->sp - RED_ZONE > cur->sp_safe)
3083 cur->sp_safe = cur->sp - RED_ZONE;
3084 if (wctx->fp == (unsigned long) cur->sp)
3085 cur->cval = RA_FROMFP;
3086 }
3087 }
3088 else if (MRM_MOD (modrm) == 0x80)
3089 {
3090 if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RBP)
3091 {
3092 if (cur->pc[1] == 0x24)
3093 { /* mov disp32(%esp),%ebp */
3094 immv = read_int (cur->pc + 2, 4);
3095 unsigned long *ptr = (unsigned long*) ((char*) cur->sp + immv);
3096 if (cur->fp_loc == ptr)
3097 {
3098 cur->fp = cur->fp_sav;
3099 cur->fp_loc = NULL;
3100 }
3101 else if (ptr >= cur->sp_safe && (unsigned long) ptr < wctx->sbase)
3102 cur->fp = (unsigned long*) (*ptr);
3103 }
3104 }
3105 }
3106 else if (MRM_MOD (modrm) == 0x40)
3107 {
3108 if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RBP)
3109 {
3110 if (cur->pc[1] == 0x24)
3111 { /* mov disp8(%esp),%ebp - JVM */
3112 imm8 = ((char*) (cur->pc))[2];
3113 unsigned long *ptr = (unsigned long*) ((char*) cur->sp + imm8);
3114 if (cur->fp_loc == ptr)
3115 {
3116 cur->fp = cur->fp_sav;
3117 cur->fp_loc = NULL;
3118 }
3119 else if (ptr >= cur->sp_safe && (unsigned long) ptr < wctx->sbase)
3120 cur->fp = (unsigned long*) (*ptr);
3121 }
3122 }
3123 }
3124 else if (MRM_MOD (modrm) == 0x0)
3125 {
3126 if (MRM_REGS (modrm) == RSP && MRM_REGD (modrm) == RBP)
3127 {
3128 if (cur->pc[1] == 0x24)
3129 { /* mov (%esp),%ebp */
3130 if (cur->fp_loc == cur->sp)
3131 {
3132 cur->fp = cur->fp_sav;
3133 cur->fp_loc = NULL;
3134 }
3135 else if (cur->sp >= cur->sp_safe &&
3136 (unsigned long) cur->sp < wctx->sbase)
3137 cur->fp = (unsigned long*) *cur->sp;
3138 }
3139 }
3140 }
3141 cur->pc = check_modrm (cur->pc);
3142 break;
3143 case 0x8c: /* mov Mw,Sw */
3144 cur->pc = check_modrm (cur->pc);
3145 break;
3146 case 0x8d: /* lea Gv,M */
3147 modrm = *cur->pc;
3148 if (MRM_REGD (modrm) == RSP)
3149 {
3150 unsigned char *pc = cur->pc;
3151 // Mez: need to use always regs[RSP/RBP] instead cur->sp(or fp):
3152 cur->regs[RSP] = (unsigned long) cur->sp;
3153 cur->regs[RBP] = (unsigned long) cur->fp;
3154 cur->pc++;
3155 int mod = (modrm >> 6) & 3;
3156 int r_m = modrm & 7;
3157 long val = 0;
3158 int undefRez = 0;
3159 if (mod == 0x3)
3160 val = getRegVal (cur, MRM_REGS (modrm), &undefRez);
3161 else if (r_m == 4)
3162 { // SP or R12. Decode SIB-byte.
3163 int sib = *cur->pc++;
3164 int scale = 1 << (sib >> 6);
3165 int index = X | ((sib >> 3) & 7);
3166 int base = B | (sib & 7);
3167 if (mod == 0)
3168 {
3169 if ((base & 7) == 5)
3170 { // BP or R13
3171 if (index != 4) // SP
3172 val += getRegVal (cur, index, &undefRez) * scale;
3173 val += read_int (cur->pc, 4);
3174 cur->pc += 4;
3175 }
3176 else
3177 {
3178 val += getRegVal (cur, base, &undefRez);
3179 if (index != 4) // SP
3180 val += getRegVal (cur, index, &undefRez) * scale;
3181 }
3182 }
3183 else
3184 {
3185 val += getRegVal (cur, base, &undefRez);
3186 if (index != 4) // SP
3187 val += getRegVal (cur, index, &undefRez) * scale;
3188 if (mod == 1)
3189 {
3190 val += read_int (cur->pc, 1);
3191 cur->pc++;
3192 }
3193 else
3194 { // mod == 2
3195 val += read_int (cur->pc, 4);
3196 cur->pc += 4;
3197 }
3198 }
3199 }
3200 else if (mod == 0)
3201 {
3202 if (r_m == 5)
3203 { // BP or R13
3204 val += read_int (cur->pc, 4);
3205 cur->pc += 4;
3206 }
3207 else
3208 val += getRegVal (cur, MRM_REGS (modrm), &undefRez);
3209 }
3210 else
3211 { // mod == 1 || mod == 2
3212 val += getRegVal (cur, MRM_REGS (modrm), &undefRez);
3213 if (mod == 1)
3214 {
3215 val += read_int (cur->pc, 1);
3216 cur->pc++;
3217 }
3218 else
3219 { // mod == 2
3220 val += read_int (cur->pc, 4);
3221 cur->pc += 4;
3222 }
3223 }
3224 if (undefRez)
3225 {
3226 DprintfT (SP_DUMP_UNWIND, "stack_unwind%d cannot calculate RSP. cur->pc=0x%lx val=0x%lx\n",
3227 __LINE__, (unsigned long) cur->pc, (unsigned long) val);
3228 goto checkFP;
3229 }
3230 cur->regs[MRM_REGD (modrm)] = val;
3231 DprintfT (SP_DUMP_UNWIND, "stack_unwind%d cur->pc=0x%lx val=0x%lx wctx->sp=0x%lx wctx->sbase=0x%lx\n",
3232 __LINE__, (unsigned long) cur->pc, (unsigned long) val,
3233 (unsigned long) wctx->sp, (unsigned long) wctx->sbase);
3234 if (cur->pc != check_modrm (pc))
3235 DprintfT (SP_DUMP_UNWIND, "stack_unwind%d ERROR: cur->pc=0x%lx != check_modrm(0x%lx)=0x%lx\n",
3236 __LINE__, (unsigned long) cur->pc, (unsigned long) pc,
3237 (unsigned long) check_modrm (pc));
3238 if (MRM_REGD (modrm) == RSP)
3239 {
3240 if (!isInside ((unsigned long) val, wctx->sp, wctx->sbase))
3241 {
3242 DprintfT (SP_DUMP_UNWIND, "stack_unwind%d cannot calculate RSP. cur->pc=0x%lx opcode=0x%02x val=0x%lx wctx->sp=0x%lx wctx->sbase=0x%lx\n",
3243 __LINE__, (unsigned long) cur->pc, opcode, (unsigned long) val,
3244 (unsigned long) wctx->sp, (unsigned long) wctx->sbase);
3245 goto checkFP;
3246 }
3247 cur->sp = (unsigned long *) val;
3248 if (cur->sp - RED_ZONE > cur->sp_safe)
3249 cur->sp_safe = cur->sp - RED_ZONE;
3250 }
3251 }
3252 else
3253 cur->pc = check_modrm (cur->pc);
3254 break;
3255 case 0x8e: /* mov Sw,Ew */
3256 cur->pc = check_modrm (cur->pc);
3257 break;
3258 case 0x8f: /* pop Ev */
3259 cur->pc = check_modrm (cur->pc);
3260 cur->sp += 1;
3261 if (cur->sp - RED_ZONE > cur->sp_safe)
3262 cur->sp_safe = cur->sp - RED_ZONE;
3263 break;
3264 case 0x90: /* nop */
3265 break;
3266 case 0x91: /* xchg %eax,%ecx */
3267 case 0x92: /* xchg %eax,%edx */
3268 case 0x93: /* xchg %eax,%ebx */
3269 case 0x94: /* xchg %eax,%esp XXXX */
3270 case 0x95: /* xchg %eax,%ebp XXXX */
3271 case 0x96: /* xchg %eax,%esi */
3272 case 0x97: /* xchg %eax,%edi */
3273 break;
3274 case 0x98: /* cbw/cwde */
3275 case 0x99: /* cwd/cwq */
3276 break;
3277 case 0x9a: /* callf Ap */
3278 if (jmp_reg_switch_mode == 1)
3279 {
3280 struct AdvWalkContext* tmpctx = (struct AdvWalkContext *) alloca (sizeof (*cur));
3281 __collector_memcpy (tmpctx, cur, sizeof (*cur));
3282 int rc = process_return (wctx, tmpctx);
3283 if (rc != RA_FAILURE)
3284 {
3285 if (save_ctx)
3286 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3287 return rc;
3288 }
3289 }
3290 cur->pc += 2 + a;
3291 break;
3292 case 0x9b: /* fwait */
3293 case 0x9c: /* pushf Fv */
3294 case 0x9d: /* popf Fv */
3295 case 0x9e: /* sahf */
3296 case 0x9f: /* lahf */
3297 break;
3298 case 0xa0: /* mov al,Ob */
3299 case 0xa1: /* mov eax,Ov */
3300 case 0xa2: /* mov Ob,al */
3301 case 0xa3: /* mov Ov,eax */
3302 cur->pc += a;
3303 break;
3304 case 0xa4: /* movsb Yb,Xb */
3305 case 0xa5: /* movsd Yv,Xv */
3306 case 0xa6: /* cmpsb Yb,Xb */
3307 case 0xa7: /* cmpsd Xv,Yv */
3308 break;
3309 case 0xa8: /* test al,Ib */
3310 cur->pc += 1;
3311 break;
3312 case 0xa9: /* test eax,Iz */
3313 cur->pc += z;
3314 break;
3315 case 0xaa: /* stosb Yb,%al */
3316 case 0xab: /* stosd Yv,%eax */
3317 case 0xac: /* lodsb %al,Xb */
3318 case 0xad: /* lodsd %eax,Xv */
3319 case 0xae: /* scasb %al,Yb */
3320 case 0xaf: /* scasd %eax,Yv */
3321 break;
3322 case 0xb0: /* mov %al,Ib */
3323 case 0xb1: /* mov %cl,Ib */
3324 case 0xb2: /* mov %dl,Ib */
3325 case 0xb3: /* mov %bl,Ib */
3326 case 0xb4: /* mov %ah,Ib */
3327 case 0xb5: /* mov %ch,Ib */
3328 case 0xb6: /* mov %dh,Ib */
3329 case 0xb7: /* mov %bh,Ib */
3330 cur->pc += 1;
3331 break;
3332 case 0xb8: /* mov Iv,%eax */
3333 case 0xb9: /* mov Iv,%ecx */
3334 case 0xba: /* mov Iv,%edx */
3335 case 0xbb: /* mov Iv,%ebx */
3336 case 0xbc: /* mov Iv,%esp */
3337 case 0xbd: /* mov Iv,%rbp */
3338 case 0xbe: /* mov Iv,%esi */
3339 case 0xbf: /* mov Iv,%edi */
3340 reg = OPC_REG (opcode);
3341 if (reg == RAX)
3342 cur->rax = read_int (cur->pc, v);
3343 cur->pc += v;
3344 break;
3345 case 0xc0: /* group2 Eb,Ib */
3346 case 0xc1: /* group2 Ev,Ib */
3347 cur->pc = check_modrm (cur->pc) + 1;
3348 break;
3349 case 0xc2: /* ret Iw */
3350 /* In the dynamic linker we may see that
3351 * the actual return address is at sp+immv,
3352 * while sp points to the resolved address.
3353 */
3354 {
3355 immv = read_int (cur->pc, 2);
3356 int rc = process_return (wctx, cur);
3357 if (rc != RA_FAILURE)
3358 {
3359 if (jmp_reg_switch_mode == 1)
3360 {
3361 DprintfT (SP_DUMP_UNWIND, "stack_unwind%d give up return address under jmp switch mode, opcode = 0xc2\n", __LINE__);
3362 goto checkFP;
3363 }
3364 wctx->sp += immv;
3365 if (save_ctx)
3366 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3367 return rc;
3368 }
3369 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode 0xc2.\n", __LINE__);
3370 DELETE_CURCTX ();
3371 }
3372 break;
3373 case 0xc3: /* ret */
3374 {
3375 int rc = process_return (wctx, cur);
3376 if (rc != RA_FAILURE)
3377 {
3378 if (save_ctx)
3379 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3380 return rc;
3381 }
3382 if (jmp_reg_switch_mode == 1)
3383 jmp_reg_switch_pc = cur->pc;
3384 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode 0xc3.\n", __LINE__);
3385 DELETE_CURCTX ();
3386 }
3387 break;
3388 case 0xc4: /* group AVX, 3-bytes VEX prefix */
3389 {
3390 unsigned char *pc = cur->pc - 1; // points to the beginning of the instruction
3391 int len = parse_x86_AVX_instruction (pc);
3392 if (len < 3)
3393 DELETE_CURCTX ();
3394 else
3395 {
3396 pc += len;
3397 cur->pc = pc;
3398 }
3399 }
3400 break;
3401 case 0xc5: /* group AVX, 2-bytes VEX prefix */
3402 {
3403 unsigned char *pc = cur->pc - 1; // points to the beginning of the instruction
3404 int len = parse_x86_AVX_instruction (pc);
3405 if (len < 2)
3406 DELETE_CURCTX ();
3407 else
3408 {
3409 pc += len;
3410 cur->pc = pc;
3411 }
3412 }
3413 break;
3414 case 0xc6:
3415 modrm = *cur->pc;
3416 if (modrm == 0xf8) /* xabort */
3417 cur->pc += 2;
3418 else /* mov Eb,Ib */
3419 cur->pc = check_modrm (cur->pc) + 1;
3420 break;
3421 case 0xc7:
3422 modrm = *cur->pc;
3423 if (modrm == 0xf8) /* xbegin */
3424 cur->pc += v + 1;
3425 else
3426 { /* mov Ev,Iz */
3427 extop = MRM_EXT (modrm);
3428 if (extop != 0)
3429 {
3430 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d give up return address, opcode = 0xc7\n", __LINE__);
3431 goto checkFP;
3432 }
3433 if (MRM_MOD (modrm) == 0xc0 && MRM_REGS (modrm) == RAX)
3434 cur->rax = read_int (cur->pc + 1, z);
3435 cur->pc = check_modrm (cur->pc) + z;
3436 }
3437 break;
3438 case 0xc8: /* enter Iw,Ib */
3439 cur->pc += 3;
3440 break;
3441 case 0xc9: /* leave */
3442 /* mov %ebp,%esp */
3443 cur->sp = cur->fp;
3444 /* pop %ebp */
3445 if (cur->fp_loc == cur->sp)
3446 {
3447 cur->fp = cur->fp_sav;
3448 cur->fp_loc = NULL;
3449 }
3450 else if (cur->sp >= cur->sp_safe &&
3451 (unsigned long) cur->sp < wctx->sbase)
3452 {
3453 cur->fp = (unsigned long*) (*cur->sp);
3454 if (wctx->fp == (unsigned long) cur->sp)
3455 cur->cval = RA_FROMFP;
3456 }
3457 cur->sp += 1;
3458 if (cur->sp - RED_ZONE > cur->sp_safe)
3459 cur->sp_safe = cur->sp - RED_ZONE;
3460 break;
3461 case 0xca: /* retf Iw */
3462 cur->pc += 2; /* XXXX process return */
3463 break;
3464 case 0xcb: /* retf */
3465 break; /* XXXX process return */
3466 case 0xcc: /* int 3 */
3467 break;
3468 case 0xcd: /* int Ib */
3469 if (*cur->pc == 0x80)
3470 {
3471 if (cur->rax == __NR_exit)
3472 {
3473 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode 0xcd.\n", __LINE__);
3474 DELETE_CURCTX ();
3475 break;
3476 }
3477 else if (cur->rax == __NR_rt_sigreturn)
3478 {
3479 if (jmp_reg_switch_mode == 1)
3480 {
3481 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d give up return address under jmp switch mode, opcode=0xcd\n",
3482 __LINE__);
3483 goto checkFP;
3484 }
3485 wctx->sp = (unsigned long) cur->sp;
3486 if (save_ctx)
3487 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_RT_SIGRETURN);
3488 return RA_RT_SIGRETURN;
3489 }
3490 #if WSIZE(32)
3491 else if (cur->rax == __NR_sigreturn)
3492 {
3493 if (jmp_reg_switch_mode == 1)
3494 {
3495 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d give up return address under jmp switch mode, opcode = 0xc2\n",
3496 __LINE__);
3497 goto checkFP;
3498 }
3499 wctx->sp = (unsigned long) cur->sp;
3500 if (save_ctx)
3501 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_SIGRETURN);
3502 return RA_SIGRETURN;
3503 }
3504 #endif
3505 }
3506 cur->pc += 1;
3507 break;
3508 case 0xce: /* into */
3509 case 0xcf: /* iret */
3510 break;
3511 case 0xd0: /* shift group2 Eb,1 */
3512 case 0xd1: /* shift group2 Ev,1 */
3513 case 0xd2: /* shift group2 Eb,%cl */
3514 case 0xd3: /* shift group2 Ev,%cl */
3515 cur->pc = check_modrm (cur->pc);
3516 break;
3517 case 0xd4: /* aam Ib */
3518 cur->pc += 1;
3519 break;
3520 case 0xd5: /* aad Ib */
3521 cur->pc += 1;
3522 break;
3523 case 0xd6: /* falc? */
3524 break;
3525 case 0xd7:
3526 cur->pc = check_modrm (cur->pc);
3527 cur->pc++;
3528 break;
3529 case 0xd8: /* esc instructions */
3530 case 0xd9:
3531 case 0xda:
3532 case 0xdb:
3533 case 0xdc:
3534 case 0xdd:
3535 case 0xde:
3536 case 0xdf:
3537 cur->pc = check_modrm (cur->pc);
3538 break;
3539 case 0xe0: /* loopne Jb */
3540 case 0xe1: /* loope Jb */
3541 case 0xe2: /* loop Jb */
3542 case 0xe3: /* jcxz Jb */
3543 imm8 = *(char*) cur->pc++;
3544 if (nctx < (jmp_reg_switch_mode ? MAXJMPREGCTX : MAXCTX))
3545 {
3546 int tidx = 0;
3547 unsigned char *npc = cur->pc + imm8;
3548 if (is_after_ret (npc))
3549 break;
3550 while (npc > targets[tidx])
3551 tidx += 1;
3552 if (npc != targets[tidx])
3553 {
3554 if (ntrg < MAXTRGTS)
3555 {
3556 for (int i = 0; i < nctx; i++)
3557 if (buf[i].tidx >= tidx)
3558 buf[i].tidx++;
3559 /* insert a new target */
3560 for (int i = ntrg; i > tidx; i--)
3561 targets[i] = targets[i - 1];
3562 ntrg += 1;
3563 targets[tidx++] = npc;
3564 }
3565 else
3566 DprintfT (SP_DUMP_UNWIND, "unwind.c: ntrg = max\n");
3567 struct AdvWalkContext *new = buf + nctx;
3568 nctx += 1;
3569 __collector_memcpy (new, cur, sizeof (*new));
3570 new->pc = npc;
3571 new->tidx = tidx;
3572 cur = new; /* advance the new context first */
3573 continue;
3574 }
3575 }
3576 else
3577 DprintfT (SP_DUMP_UNWIND, "unwind.c: nctx = max\n");
3578 break;
3579 case 0xe4: case 0xe5:
3580 cur->pc = check_modrm (cur->pc);
3581 cur->pc++;
3582 break;
3583 case 0xe6: case 0xe7:
3584 cur->pc++;
3585 cur->pc = check_modrm (cur->pc);
3586 break;
3587 case 0xec: case 0xed: case 0xee: case 0xef:
3588 cur->pc = check_modrm (cur->pc);
3589 break;
3590 case 0xe8: /* call Jz (f64) */
3591 {
3592 if (jmp_reg_switch_mode == 1)
3593 {
3594 struct AdvWalkContext* tmpctx = (struct AdvWalkContext *) alloca (sizeof (*cur));
3595 __collector_memcpy (tmpctx, cur, sizeof (*cur));
3596 int rc = process_return (wctx, tmpctx);
3597 if (rc != RA_FAILURE)
3598 {
3599 if (save_ctx)
3600 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3601 return rc;
3602 }
3603 }
3604 int immz = read_int (cur->pc, z);
3605 if (immz == 0)
3606 /* special case in PIC code */
3607 cur->sp -= 1;
3608 cur->pc += z;
3609 }
3610 break;
3611 case 0xe9: /* jump Jz */
3612 {
3613 int immz = read_int (cur->pc, z);
3614 unsigned char *npc = cur->pc + z + immz;
3615 if ((unsigned long) npc < wctx->tbgn || (unsigned long) npc >= wctx->tend)
3616 {
3617 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode 0xe9.\n", __LINE__);
3618 DELETE_CURCTX ();
3619 break;
3620 }
3621 int tidx = 0;
3622 while (npc > targets[tidx])
3623 tidx += 1;
3624 if (npc != targets[tidx])
3625 {
3626 if (ntrg < MAXTRGTS)
3627 {
3628 for (int i = 0; i < nctx; i++)
3629 if (buf[i].tidx >= tidx)
3630 buf[i].tidx++;
3631 /* insert a new target */
3632 for (int i = ntrg; i > tidx; i--)
3633 targets[i] = targets[i - 1];
3634 ntrg += 1;
3635 targets[tidx++] = npc;
3636 }
3637 else
3638 DprintfT (SP_DUMP_UNWIND, "unwind.c: ntrg = max\n");
3639 cur->pc = npc;
3640 cur->tidx = tidx;
3641 continue; /* advance this context first */
3642 }
3643 else
3644 {
3645 /* Delete context */
3646 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode 0xe9.\n", __LINE__);
3647 DELETE_CURCTX ();
3648 }
3649 }
3650 break;
3651 case 0xeb: /* jump imm8 */
3652 {
3653 imm8 = *(char*) cur->pc++;
3654 int tidx = 0;
3655 unsigned char *npc = cur->pc + imm8;
3656 while (npc > targets[tidx])
3657 tidx += 1;
3658 if (npc != targets[tidx])
3659 {
3660 if (ntrg < MAXTRGTS)
3661 {
3662 for (int i = 0; i < nctx; i++)
3663 if (buf[i].tidx >= tidx)
3664 buf[i].tidx++;
3665 /* insert a new target */
3666 for (int i = ntrg; i > tidx; i--)
3667 targets[i] = targets[i - 1];
3668 ntrg += 1;
3669 targets[tidx++] = npc;
3670 }
3671 else
3672 DprintfT (SP_DUMP_UNWIND, "unwind.c: ntrg = max\n");
3673 cur->pc = npc;
3674 cur->tidx = tidx;
3675 continue; /* advance this context first */
3676 }
3677 else
3678 {
3679 /* Delete context */
3680 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode 0xeb.\n", __LINE__);
3681 DELETE_CURCTX ();
3682 }
3683 }
3684 break;
3685 case 0xf0: /* lock prefix */
3686 case 0xf2: /* repne prefix */
3687 case 0xf3: /* repz prefix */
3688 break;
3689 case 0xf4: /* hlt */
3690 extop2 = *(cur->pc - 3);
3691 if (extop2 == 0x90)
3692 {
3693 // 17851712 occasional SEGV in find_i386_ret_addr in unwind.c during attach
3694 if (save_ctx)
3695 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_END_OF_STACK);
3696 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d returns RA_END_OF_STACK\n", __LINE__);
3697 return RA_END_OF_STACK;
3698 }
3699 /* We see 'hlt' in _start. Stop analysis, revert to FP */
3700 /* A workaround for the Linux main stack */
3701 if (nctx > 1)
3702 {
3703 DELETE_CURCTX ();
3704 break;
3705 }
3706 if (cur->fp == 0)
3707 {
3708 if (jmp_reg_switch_mode == 1)
3709 {
3710 DprintfT (SP_DUMP_UNWIND, "unwind.c: give up return address under jmp switch mode, opcode = 0xf4\n");
3711 goto checkFP;
3712 }
3713 cache_put (wctx, RA_EOSTCK);
3714 wctx->pc = 0;
3715 wctx->sp = 0;
3716 wctx->fp = 0;
3717 if (save_ctx)
3718 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_END_OF_STACK);
3719 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d returns RA_END_OF_STACK\n", __LINE__);
3720 return RA_END_OF_STACK;
3721 }
3722 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d give up return address, opcode = 0xf4\n", __LINE__);
3723 goto checkFP;
3724 case 0xf5: /* cmc */
3725 break;
3726 case 0xf6: /* group3 Eb */
3727 modrm = *cur->pc;
3728 extop = MRM_EXT (modrm);
3729 cur->pc = check_modrm (cur->pc);
3730 if (extop == 0x0) /* test Ib */
3731 cur->pc += 1;
3732 break;
3733 case 0xf7: /* group3 Ev */
3734 modrm = *cur->pc;
3735 extop = MRM_EXT (modrm);
3736 cur->pc = check_modrm (cur->pc);
3737 if (extop == 0x0) /* test Iz */
3738 cur->pc += z;
3739 break;
3740 case 0xf8: /* clc */
3741 case 0xf9: /* stc */
3742 case 0xfa: /* cli */
3743 case 0xfb: /* sti */
3744 case 0xfc: /* cld */
3745 case 0xfd: /* std */
3746 break;
3747 case 0xfe: /* group4 */
3748 modrm = *cur->pc;
3749 extop = MRM_EXT (modrm);
3750 switch (extop)
3751 {
3752 case 0x0: /* inc Eb */
3753 case 0x1: /* dec Eb */
3754 cur->pc = check_modrm (cur->pc);
3755 break;
3756 case 0x7:
3757 cur->pc = check_modrm (cur->pc);
3758 break;
3759 default:
3760 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d unknown opcode: 0xfe %x\n",
3761 __LINE__, extop);
3762 DELETE_CURCTX ();
3763 break;
3764 }
3765 break;
3766 case 0xff: /* group5 */
3767 modrm = *cur->pc;
3768 extop = MRM_EXT (modrm);
3769 switch (extop)
3770 {
3771 case 0x0: /* inc Ev */
3772 case 0x1: /* dec Ev */
3773 cur->pc = check_modrm (cur->pc);
3774 break;
3775 case 0x2: /* calln Ev */
3776 if (jmp_reg_switch_mode == 1)
3777 {
3778 struct AdvWalkContext* tmpctx = (struct AdvWalkContext *) alloca (sizeof (*cur));
3779 __collector_memcpy (tmpctx, cur, sizeof (*cur));
3780 int rc = process_return (wctx, tmpctx);
3781 if (rc != RA_FAILURE)
3782 {
3783 if (save_ctx)
3784 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3785 return rc;
3786 }
3787 }
3788 cur->pc = check_modrm (cur->pc);
3789 break;
3790 case 0x3: /* callf Ep */
3791 if (jmp_reg_switch_mode == 1)
3792 {
3793 struct AdvWalkContext* tmpctx = (struct AdvWalkContext *) alloca (sizeof (*cur));
3794 __collector_memcpy (tmpctx, cur, sizeof (*cur));
3795 int rc = process_return (wctx, tmpctx);
3796 if (rc != RA_FAILURE)
3797 {
3798 if (save_ctx)
3799 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3800 return rc;
3801 }
3802 }
3803 cur->pc = check_modrm (cur->pc); /* XXXX */
3804 break;
3805 case 0x4: /* jumpn Ev */
3806 /* This instruction appears in PLT or
3807 * in tail call optimization.
3808 * In both cases treat it as return.
3809 * Save jump *(reg) - switch, etc, for later use when no ctx left
3810 */
3811 if (modrm == 0x25 || /* jumpn *disp32 */
3812 MRM_MOD (modrm) == 0x40 || /* jumpn byte(reg) */
3813 MRM_MOD (modrm) == 0x80) /* jumpn word(reg) */
3814 {
3815 DprintfT (SP_DUMP_UNWIND, "unwind.c: PLT or tail call: %p\n", cur->pc - 1);
3816 int rc = process_return (wctx, cur);
3817 if (rc != RA_FAILURE)
3818 {
3819 if (jmp_reg_switch_mode == 1 && total_num_jmp_reg < max_num_jmp_reg_seen)
3820 {
3821 DprintfT (SP_DUMP_UNWIND, "unwind.c: give up return address under jmp switch mode, opcode = 0xff\n");
3822 goto checkFP;
3823 }
3824 if (save_ctx)
3825 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3826 return rc;
3827 }
3828 }
3829 else if (modrm != 0x24 /*ignore SIB*/) /* jumpn *(reg) or jumpn reg */
3830 {
3831 // 22846120 stack unwind does not find caller of __memcpy_ssse3_back with B64 intel-Linux
3832 /*
3833 * For now, let's deal rather narrowly with this scenario. If:
3834 * - we are in the middle of an "ff e2" instruction, and
3835 * - the next instruction is undefined ( 0f 0b == ud2 )
3836 * then test return. (Might eventually have to broaden the scope
3837 * of this fix to other registers/etc.)
3838 */
3839 if (cur->pc[0] == 0xe2 && cur->pc[1] == 0x0f && cur->pc[2] == 0x0b)
3840 {
3841 int rc = process_return_real (wctx, cur, 0);
3842 if (rc == RA_SUCCESS)
3843 {
3844 if (save_ctx)
3845 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3846 return rc;
3847 }
3848 }
3849
3850 // 22691241 shjsynprog, jsynprog core dump from find_i386_ret_addr
3851 /*
3852 * Here is another oddity. Java 9 seems to emit dynamically generated
3853 * code where a code block ends with a "jmp *reg" and then padding to a
3854 * multiple-of-16 boundary and then a bunch of 0s. In this case, let's
3855 * not continue to walk bytes since we would be walking off the end of
3856 * the instructions into ... something. Treating them as instructions
3857 * can lead to unexpected results, including SEGV.
3858 */
3859 /*
3860 * While the general problem deserves a better solution, let's look
3861 * here only for one particular case:
3862 * 0xff 0xe7 jmp *reg
3863 * nop to bring us to a multiple-of-16 boundary
3864 * 0x0000000000000a00 something that does not look like an instruction
3865 *
3866 * A different nop might be used depending on how much padding is needed
3867 * to reach that multiple-of-16 boundary. We've seen two:
3868 * 0x90 one byte
3869 * 0x0f 0x1f 0x40 0x00 four bytes
3870 */
3871 // confirm the instruction is 0xff 0xe7
3872 if (cur->pc[0] == 0xe7)
3873 {
3874 // check for correct-length nop and find next 16-byte boundary
3875 int found_nop = 0;
3876 unsigned long long *boundary = 0;
3877 switch ((((unsigned long) (cur->pc)) & 0xf))
3878 {
3879 case 0xb: // look for 4-byte nop
3880 if (*((unsigned *) (cur->pc + 1)) == 0x00401f0f)
3881 found_nop = 1;
3882 boundary = (unsigned long long *) (cur->pc + 5);
3883 break;
3884 case 0xe: // look for 1-byte nop
3885 if (cur->pc[1] == 0x90)
3886 found_nop = 1;
3887 boundary = (unsigned long long *) (cur->pc + 2);
3888 break;
3889 default:
3890 break;
3891 }
3892
3893 // if nop is found, check what's at the boundary
3894 if (found_nop && *boundary == 0x000000000a00)
3895 {
3896 DELETE_CURCTX ();
3897 break;
3898 }
3899 }
3900
3901 DprintfT (SP_DUMP_UNWIND, "unwind.c: probably PLT or tail call or switch table: %p\n",
3902 cur->pc - 1);
3903 if (num_jmp_reg < expected_num_jmp_reg)
3904 {
3905 if (jmp_reg_ctx[num_jmp_reg] == NULL)
3906 jmp_reg_ctx[num_jmp_reg] = (struct AdvWalkContext *) alloca (sizeof (*cur));
3907 if (jmp_reg_ctx[num_jmp_reg] != NULL)
3908 __collector_memcpy (jmp_reg_ctx[num_jmp_reg], cur, sizeof (*cur));
3909 }
3910 if (num_jmp_reg < expected_num_jmp_reg ||
3911 (num_jmp_reg >= expected_num_jmp_reg &&
3912 jmp_reg_ctx[expected_num_jmp_reg - 1] != NULL &&
3913 cur->pc != jmp_reg_ctx[expected_num_jmp_reg - 1]->pc))
3914 {
3915 num_jmp_reg++;
3916 total_num_jmp_reg++;
3917 }
3918 if (jmp_reg_switch_mode == 1 && total_num_jmp_reg >= max_num_jmp_reg_seen)
3919 {
3920 int rc = process_return_real (wctx, cur, 0);
3921 if (rc == RA_SUCCESS)
3922 {
3923 if (save_ctx)
3924 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3925 return rc;
3926 }
3927 }
3928 }
3929 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d delete context, opcode 0xff.\n", __LINE__);
3930 DELETE_CURCTX ();
3931 break;
3932 case 0x5: /* jmpf Ep */
3933 cur->pc = check_modrm (cur->pc); /* XXXX */
3934 break;
3935 case 0x6: /* push Ev */
3936 cur->pc = check_modrm (cur->pc);
3937 cur->sp -= 1;
3938 break;
3939 case 0x7:
3940 cur->pc = check_modrm (cur->pc); /* XXXX */
3941 if (jmp_reg_switch_mode == 1)
3942 {
3943 int rc = process_return_real (wctx, cur, 0);
3944 if (rc == RA_SUCCESS)
3945 {
3946 if (save_ctx)
3947 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, rc);
3948 return rc;
3949 }
3950 }
3951 break;
3952 default:
3953 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d unknown opcode: 0xff %x\n",
3954 __LINE__, (int) extop);
3955 DELETE_CURCTX ();
3956 break;
3957 }
3958 break;
3959 default:
3960 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d unknown opcode: 0x%x\n",
3961 __LINE__, (int) opcode);
3962 DELETE_CURCTX ();
3963 break;
3964 }
3965
3966 /* switch to next context */
3967 if (++cur >= buf + nctx)
3968 cur = buf;
3969 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d switch context: cur=0x%lx(%ld) nctx=%d cnt=%d\n",
3970 __LINE__, (unsigned long) cur, (long) (cur - buf), (int) nctx, (int) cnt);
3971 }
3972
3973 checkFP:
3974 Tprintf (DBG_LT3, "find_i386_ret_addr:%d checkFP: wctx=0x%lx fp=0x%lx ln=0x%lx pc=0x%lx sbase=0x%lx sp=0x%lx tbgn=0x%lx tend=0x%lx\n",
3975 __LINE__, (unsigned long) wctx, (unsigned long) wctx->fp,
3976 (unsigned long) wctx->ln, (unsigned long) wctx->pc, (unsigned long) wctx->sbase,
3977 (unsigned long) wctx->sp, (unsigned long) wctx->tbgn, (unsigned long) wctx->tend);
3978
3979 if (jmp_reg_switch_mode == 1)
3980 { // not deal with switch cases not ending with ret
3981 if (jmp_reg_switch_backup_ctx != NULL)
3982 __collector_memcpy (cur, jmp_reg_switch_backup_ctx, sizeof (*cur));
3983 DprintfT (SP_DUMP_UNWIND, "stack_unwind jmp reg mode on: pc = 0x%lx cnt = %d, nctx = %d\n", wctx->pc, cnt, nctx);
3984 }
3985
3986 unsigned long *cur_fp = cur->fp;
3987 unsigned long *cur_sp = cur->sp;
3988 if (do_walk == 0)
3989 __collector_memcpy (&wctx_pc_save, wctx, sizeof (struct WalkContext));
3990
3991 /* Resort to the frame pointer */
3992 if (cur->fp_loc)
3993 cur->fp = cur->fp_sav;
3994 cur->sp = cur->fp;
3995 if ((unsigned long) cur->sp >= wctx->sbase ||
3996 (unsigned long) cur->sp < wctx->sp)
3997 {
3998 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d do_walk=%d cur->sp=0x%p out of range. wctx->sbase=0x%lx wctx->sp=0x%lx wctx->pc=0x%lx\n",
3999 __LINE__, (int) do_walk, cur->sp, (unsigned long) wctx->sbase,
4000 (unsigned long) wctx->sp, (unsigned long) wctx->pc);
4001 if (do_walk == 0)
4002 {
4003 cur->sp = cur_sp;
4004 cur->fp = cur_fp;
4005 do_walk = 1;
4006 save_ctx = 1;
4007 goto startWalk;
4008 }
4009 if (save_ctx)
4010 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_FAILURE);
4011 return RA_FAILURE;
4012 }
4013
4014 unsigned long fp = *cur->sp++;
4015 if (fp <= (unsigned long) cur->sp || fp >= wctx->sbase)
4016 {
4017 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d fp=0x%016llx out of range. cur->sp=%p wctx->sbase=0x%lx wctx->pc=0x%lx\n",
4018 __LINE__, (unsigned long long) fp, cur->sp,
4019 (unsigned long) wctx->sbase, (unsigned long) wctx->pc);
4020 if (do_walk == 0)
4021 {
4022 cur->sp = cur_sp;
4023 cur->fp = cur_fp;
4024 do_walk = 1;
4025 save_ctx = 1;
4026 goto startWalk;
4027 }
4028 if (save_ctx)
4029 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_FAILURE);
4030 return RA_FAILURE;
4031 }
4032
4033 unsigned long ra = *cur->sp++;
4034 if (ra == 0)
4035 {
4036 cache_put (wctx, RA_EOSTCK);
4037 DprintfT (SP_DUMP_UNWIND, "unwind.c:%d returns RA_END_OF_STACK wctx->pc = 0x%lx\n", __LINE__, wctx->pc);
4038 if (save_ctx)
4039 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_END_OF_STACK);
4040 return RA_END_OF_STACK;
4041 }
4042
4043 unsigned long tbgn = wctx->tbgn;
4044 unsigned long tend = wctx->tend;
4045 if (ra < tbgn || ra >= tend)
4046 {
4047 // We do not know yet if update_map_segments is really needed
4048 if (!__collector_check_segment (ra, &tbgn, &tend, 0))
4049 {
4050 DprintfT (SP_DUMP_UNWIND, "unwind.c: __collector_check_segment fail. wctx->pc = 0x%lx\n", wctx->pc);
4051 if (do_walk == 0)
4052 {
4053 cur->sp = cur_sp;
4054 cur->fp = cur_fp;
4055 do_walk = 1;
4056 save_ctx = 1;
4057 goto startWalk;
4058 }
4059 if (save_ctx)
4060 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_FAILURE);
4061 return RA_FAILURE;
4062 }
4063 }
4064
4065 unsigned long npc = adjust_ret_addr (ra, ra - tbgn, tend);
4066 if (npc == 0)
4067 {
4068 DprintfT (SP_DUMP_UNWIND, "unwind.c: adjust_ret_addr fail. wctx->pc = 0x%lx\n", wctx->pc);
4069 if (do_walk == 0)
4070 {
4071 cur->sp = cur_sp;
4072 cur->fp = cur_fp;
4073 do_walk = 1;
4074 save_ctx = 1;
4075 goto startWalk;
4076 }
4077 if (save_ctx)
4078 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_FAILURE);
4079 return RA_FAILURE;
4080 }
4081 wctx->pc = npc;
4082 wctx->sp = (unsigned long) cur->sp;
4083 wctx->fp = fp;
4084 wctx->tbgn = tbgn;
4085 wctx->tend = tend;
4086
4087 if (save_ctx)
4088 {
4089 omp_cache_put (cur->sp_safe, &wctx_pc_save, wctx, RA_SUCCESS);
4090 DprintfT (SP_DUMP_UNWIND, "unwind.c: cache walk context. wctx_pc_save->pc = 0x%lx\n", wctx_pc_save.pc);
4091 }
4092 return RA_SUCCESS;
4093 }
4094
4095 /*
4096 * We have the return address, but we would like to report to the user
4097 * the calling PC, which is the instruction immediately preceding the
4098 * return address. Unfortunately, x86 instructions can have variable
4099 * length. So we back up 8 bytes and try to figure out where the
4100 * calling PC starts. (FWIW, call instructions are often 5-bytes long.)
4101 */
4102 unsigned long
4103 adjust_ret_addr (unsigned long ra, unsigned long segoff, unsigned long tend)
4104 {
4105 unsigned long npc = 0;
4106 int i = segoff < 8 ? segoff : 8;
4107 for (; i > 1; i--)
4108 {
4109 unsigned char *ptr = (unsigned char*) ra - i;
4110 int z = 4;
4111 int a = 4;
4112 int done = 0;
4113 int bVal;
4114 while (!done)
4115 {
4116 bVal = getByteInstruction (ptr);
4117 if (bVal < 0)
4118 return 0;
4119 switch (bVal)
4120 {
4121 case 0x26:
4122 case 0x36:
4123 #if WSIZE(64)
4124 ptr += 1;
4125 break;
4126 #endif
4127 case 0x64:
4128 case 0x65:
4129 bVal = getByteInstruction (ptr + 1);
4130 if (bVal < 0)
4131 return 0;
4132 if (bVal == 0xe8)
4133 // a workaround for bug 16193041, assuming "call Jz" has no segment override prefix
4134 done = 1;
4135 else
4136 ptr += 1;
4137 break;
4138 case 0x66:
4139 z = 2;
4140 ptr += 1;
4141 break;
4142 case 0x67:
4143 a = 2;
4144 ptr += 1;
4145 break;
4146 default:
4147 done = 1;
4148 break;
4149 }
4150 }
4151 #if WSIZE(64)
4152 bVal = getByteInstruction (ptr);
4153 if (bVal < 0)
4154 return 0;
4155 if (bVal >= 0x40 && bVal <= 0x4f)
4156 { /* XXXX not all REX codes applicable */
4157 if (bVal & 0x8)
4158 z = 4;
4159 ptr += 1;
4160 }
4161 #endif
4162 int opcode = getByteInstruction (ptr);
4163 if (opcode < 0)
4164 return 0;
4165 ptr++;
4166 switch (opcode)
4167 {
4168 case 0xe8: /* call Jz (f64) */
4169 ptr += z;
4170 break;
4171 case 0x9a: /* callf Ap */
4172 ptr += 2 + a;
4173 break;
4174 case 0xff: /* calln Ev , callf Ep */
4175 {
4176 int extop = MRM_EXT (*ptr);
4177 if (extop == 2 || extop == 3)
4178 ptr = check_modrm (ptr);
4179 }
4180 break;
4181 default:
4182 continue;
4183 }
4184 if ((unsigned long) ptr == ra)
4185 {
4186 npc = ra - i;
4187 break;
4188 }
4189 }
4190 if (npc == 0)
4191 {
4192 unsigned char * ptr = (unsigned char *) ra;
4193 #if WSIZE(32)
4194 // test __kernel_sigreturn or __kernel_rt_sigreturn
4195 if ((ra + 7 < tend && getByteInstruction (ptr) == 0x58
4196 && getByteInstruction (ptr + 1) == 0xb8
4197 && getByteInstruction (ptr + 6) == 0xcd
4198 && getByteInstruction (ptr + 7) == 0x80) /* pop %eax; mov $NNNN, %eax; int */
4199 || (ra + 7 < tend && getByteInstruction (ptr) == 0x58
4200 && getByteInstruction (ptr + 1) == 0xb8
4201 && getByteInstruction (ptr + 6) == 0x0f
4202 && getByteInstruction (ptr + 7) == 0x05) /* pop %eax; mov $NNNN, %eax; syscall */
4203 || (ra + 6 < tend && getByteInstruction (ptr) == 0xb8
4204 && getByteInstruction (ptr + 5) == 0xcd
4205 && getByteInstruction (ptr + 6) == 0x80) /* mov $NNNN, %eax; int */
4206 || (ra + 6 < tend && getByteInstruction (ptr) == 0xb8
4207 && getByteInstruction (ptr + 5) == 0x0f
4208 && getByteInstruction (ptr + 6) == 0x05)) /* mov $NNNN, %eax; syscall */
4209 #else //WSIZE(64)
4210 // test __restore_rt
4211 if (ra + 8 < tend && getByteInstruction (ptr) == 0x48
4212 && getByteInstruction (ptr + 7) == 0x0f
4213 && getByteInstruction (ptr + 8) == 0x05) /* mov $NNNNNNNN, %rax; syscall */
4214 #endif
4215 {
4216 npc = ra;
4217 }
4218 }
4219 if (npc == 0 && __collector_java_mode
4220 && __collector_java_asyncgetcalltrace_loaded)
4221 { // detect jvm interpreter code for java user threads
4222 unsigned char * ptr = (unsigned char *) ra;
4223 #if WSIZE(32)
4224 // up to J170
4225 /*
4226 * ff 24 9d e0 64 02 f5 jmp *-0xafd9b20(,%ebx,4)
4227 * 8b 4e 01 movl 1(%esi),%ecx
4228 * f7 d1 notl %ecx
4229 * 8b 5d ec movl -0x14(%ebp),%ebx
4230 * c1 e1 02 shll $2,%ecx
4231 * eb d8 jmp .-0x26 [ 0x92a ]
4232 * 83 ec 08 subl $8,%esp || 8b 65 f8 movl -8(%ebp),%esp
4233 * */
4234 if (ra - 20 >= (ra - segoff) && ((*ptr == 0x83 && *(ptr + 1) == 0xec) || (*ptr == 0x8b && *(ptr + 1) == 0x65))
4235 && *(ptr - 2) == 0xeb
4236 && *(ptr - 5) == 0xc1 && *(ptr - 4) == 0xe1
4237 && *(ptr - 8) == 0x8b && *(ptr - 7) == 0x5d
4238 && *(ptr - 10) == 0xf7 && *(ptr - 9) == 0xd1
4239 && *(ptr - 13) == 0x8b && *(ptr - 12) == 0x4e
4240 && *(ptr - 20) == 0xff && *(ptr - 19) == 0x24 && *(ptr - 18) == 0x9d)
4241 {
4242 npc = ra - 20;
4243 }
4244 // J180 J190
4245 // ff 24 9d ** ** ** ** jmp *-0x*******(,%ebx,4)
4246 if (npc == 0
4247 && ra - 7 >= (ra - segoff)
4248 && *(ptr - 7) == 0xff
4249 && *(ptr - 6) == 0x24
4250 && *(ptr - 5) == 0x9d)
4251 {
4252 npc = ra - 7;
4253 }
4254 #else //WSIZE(64)
4255 // up to J170
4256 /*
4257 * 41 ff 24 da jmp *(%r10,%rbx,8)
4258 * 41 8b 4d 01 movl 1(%r13),%ecx
4259 * f7 d1 notl %ecx
4260 * 48 8b 5d d8 movq -0x28(%rbp),%rbx
4261 * c1 e1 02 shll $2,%ecx
4262 * eb cc jmp .-0x32 [ 0xd23 ]
4263 * 48 8b 65 f0 movq -0x10(%rbp),%rsp
4264 */
4265 if (ra - 19 >= (ra - segoff) && *ptr == 0x48 && ((*(ptr + 1) == 0x8b && *(ptr + 2) == 0x65) || (*(ptr + 1) == 0x83 && *(ptr + 2) == 0xec))
4266 && *(ptr - 2) == 0xeb
4267 && *(ptr - 5) == 0xc1 && *(ptr - 4) == 0xe1
4268 && *(ptr - 9) == 0x48 && *(ptr - 8) == 0x8b && *(ptr - 7) == 0x5d
4269 && *(ptr - 11) == 0xf7 && *(ptr - 10) == 0xd1
4270 && *(ptr - 15) == 0x41 && *(ptr - 14) == 0x8b && *(ptr - 13) == 0x4d
4271 && *(ptr - 19) == 0x41 && *(ptr - 18) == 0xff)
4272 npc = ra - 19;
4273 // J180 J190
4274 // 41 ff 24 da jmp *(%r10,%rbx,8)
4275 if (npc == 0
4276 && ra - 4 >= (ra - segoff)
4277 && *(ptr - 4) == 0x41
4278 && *(ptr - 3) == 0xff
4279 && *(ptr - 2) == 0x24
4280 && *(ptr - 1) == 0xda)
4281 npc = ra - 4;
4282 #endif
4283 }
4284
4285 return npc;
4286 }
4287
4288 /*
4289 * Parses AVX instruction and returns its length.
4290 * Returns 0 if parsing failed.
4291 * https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf
4292 */
4293 static int
4294 parse_x86_AVX_instruction (unsigned char *pc)
4295 {
4296 /*
4297 * VEX prefix has a two-byte form (0xc5) and a three byte form (0xc4).
4298 * If an instruction syntax can be encoded using the two-byte form,
4299 * it can also be encoded using the three byte form of VEX.
4300 * The latter increases the length of the instruction by one byte.
4301 * This may be helpful in some situations for code alignment.
4302 *
4303 Byte 0 Byte 1 Byte 2 Byte 3
4304 (Bit Position) 7 0 7 6 5 4 0 7 6 3 2 10
4305 3-byte VEX [ 11000100 ] [ R X B | m-mmmm ] [ W | vvvv | L | pp ]
4306 7 0 7 6 3 2 10
4307 2-byte VEX [ 11000101 ] [ R | vvvv | L | pp ]
4308 7 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
4309 4-byte EVEX [ 01100010 ] [ R X B R1 0 0 m m ] [ W v v v v 1 p p ] [ z L1 L B1 V1 a a a ]
4310
4311 R: REX.R in 1's complement (inverted) form
4312 0: Same as REX.R=1 (64-bit mode only)
4313 1: Same as REX.R=0 (must be 1 in 32-bit mode)
4314
4315 X: REX.X in 1's complement (inverted) form
4316 0: Same as REX.X=1 (64-bit mode only)
4317 1: Same as REX.X=0 (must be 1 in 32-bit mode)
4318
4319 B: REX.B in 1's complement (inverted) form
4320 0: Same as REX.B=1 (64-bit mode only)
4321 1: Same as REX.B=0 (Ignored in 32-bit mode).
4322
4323 W: opcode specific (use like REX.W, or used for opcode
4324 extension, or ignored, depending on the opcode byte)
4325
4326 m-mmmm:
4327 00000: Reserved for future use (will #UD)
4328 00001: implied 0F leading opcode byte
4329 00010: implied 0F 38 leading opcode bytes
4330 00011: implied 0F 3A leading opcode bytes
4331 00100-11111: Reserved for future use (will #UD)
4332
4333 vvvv: a register specifier (in 1's complement form) or 1111 if unused.
4334
4335 L: Vector Length
4336 0: scalar or 128-bit vector
4337 1: 256-bit vector
4338
4339 pp: opcode extension providing equivalent functionality of a SIMD prefix
4340 00: None
4341 01: 66
4342 10: F3
4343 11: F2
4344 *
4345 * Example: 0xc5f877L vzeroupper
4346 * VEX prefix: 0xc5 0x77
4347 * Opcode: 0xf8
4348 *
4349 */
4350 int len = 0;
4351 disassemble_info dis_info;
4352 dis_info.arch = bfd_arch_i386;
4353 dis_info.mach = bfd_mach_x86_64;
4354 dis_info.flavour = bfd_target_unknown_flavour;
4355 dis_info.endian = BFD_ENDIAN_UNKNOWN;
4356 dis_info.endian_code = dis_info.endian;
4357 dis_info.octets_per_byte = 1;
4358 dis_info.disassembler_needs_relocs = FALSE;
4359 dis_info.fprintf_func = fprintf_func;
4360 dis_info.fprintf_styled_func = fprintf_styled_func;
4361 dis_info.stream = NULL;
4362 dis_info.disassembler_options = NULL;
4363 dis_info.read_memory_func = read_memory_func;
4364 dis_info.memory_error_func = memory_error_func;
4365 dis_info.print_address_func = print_address_func;
4366 dis_info.symbol_at_address_func = symbol_at_address_func;
4367 dis_info.symbol_is_valid = symbol_is_valid;
4368 dis_info.display_endian = BFD_ENDIAN_UNKNOWN;
4369 dis_info.symtab = NULL;
4370 dis_info.symtab_size = 0;
4371 dis_info.buffer_vma = 0;
4372 dis_info.buffer = pc;
4373 dis_info.buffer_length = 8;
4374
4375 disassembler_ftype disassemble = print_insn_i386;
4376 if (disassemble == NULL)
4377 {
4378 DprintfT (SP_DUMP_UNWIND, "parse_x86_AVX_instruction ERROR: unsupported disassemble\n");
4379 return 0;
4380 }
4381 len = disassemble (0, &dis_info);
4382 DprintfT (SP_DUMP_UNWIND, "parse_x86_AVX_instruction: returned %d pc: %p\n", len, pc);
4383 return len;
4384 }
4385
4386 /*
4387 * In the Intel world, a stack frame looks like this:
4388 *
4389 * %fp0->| |
4390 * |-------------------------------|
4391 * | Args to next subroutine |
4392 * |-------------------------------|-\
4393 * %sp0->| One word struct-ret address | |
4394 * |-------------------------------| > minimum stack frame (8 bytes)
4395 * | Previous frame pointer (%fp0)| |
4396 * %fp1->|-------------------------------|-/
4397 * | Local variables |
4398 * %sp1->|-------------------------------|
4399 *
4400 */
4401
4402 int
4403 stack_unwind (char *buf, int size, void *bptr, void *eptr, ucontext_t *context, int mode)
4404 {
4405 long *lbuf = (long*) buf;
4406 int lsize = size / sizeof (long);
4407 int ind = 0;
4408 int do_walk = 1;
4409 int extra_frame = 0;
4410 if (mode & FRINFO_NO_WALK)
4411 do_walk = 0;
4412 if ((mode & 0xffff) == FRINFO_FROM_STACK)
4413 extra_frame = 1;
4414
4415 /*
4416 * trace the stack frames from user stack.
4417 * We are assuming that the frame pointer and return address
4418 * are null when we are at the top level.
4419 */
4420 struct WalkContext wctx;
4421 wctx.pc = GET_PC (context);
4422 wctx.sp = GET_SP (context);
4423 wctx.fp = GET_FP (context);
4424 wctx.ln = (unsigned long) context->uc_link;
4425 unsigned long *sbase = (unsigned long*) __collector_tsd_get_by_key (unwind_key);
4426 if (sbase && *sbase > wctx.sp)
4427 wctx.sbase = *sbase;
4428 else
4429 {
4430 wctx.sbase = wctx.sp + 0x100000;
4431 if (wctx.sbase < wctx.sp) /* overflow */
4432 wctx.sbase = (unsigned long) - 1;
4433 }
4434 // We do not know yet if update_map_segments is really needed
4435 __collector_check_segment (wctx.pc, &wctx.tbgn, &wctx.tend, 0);
4436
4437 for (;;)
4438 {
4439 if (ind >= lsize || wctx.pc == 0)
4440 break;
4441 if (bptr != NULL && extra_frame && wctx.sp <= (unsigned long) bptr && ind < 2)
4442 {
4443 lbuf[0] = wctx.pc;
4444 if (ind == 0)
4445 {
4446 ind++;
4447 if (ind >= lsize)
4448 break;
4449 }
4450 }
4451 if (bptr == NULL || wctx.sp > (unsigned long) bptr)
4452 {
4453 lbuf[ind++] = wctx.pc;
4454 if (ind >= lsize)
4455 break;
4456 }
4457
4458 for (;;)
4459 {
4460 if (eptr != NULL && wctx.sp >= (unsigned long) eptr)
4461 {
4462 ind = ind >= 2 ? ind - 2 : 0;
4463 goto exit;
4464 }
4465 int ret = find_i386_ret_addr (&wctx, do_walk);
4466 DprintfT (SP_DUMP_UNWIND, "stack_unwind (x86 walk):%d find_i386_ret_addr returns %d\n", __LINE__, ret);
4467 if (ret == RA_FAILURE)
4468 {
4469 /* lbuf[ind++] = SP_FAILED_UNWIND_MARKER; */
4470 goto exit;
4471 }
4472
4473 if (ret == RA_END_OF_STACK)
4474 goto exit;
4475 #if WSIZE(32)
4476 if (ret == RA_RT_SIGRETURN)
4477 {
4478 struct SigFrame
4479 {
4480 unsigned long arg0;
4481 unsigned long arg1;
4482 unsigned long arg2;
4483 } *sframe = (struct SigFrame*) wctx.sp;
4484 ucontext_t *ncontext = (ucontext_t*) sframe->arg2;
4485 wctx.pc = GET_PC (ncontext);
4486 if (!__collector_check_segment (wctx.pc, &wctx.tbgn, &wctx.tend, 0))
4487 {
4488 /* lbuf[ind++] = SP_FAILED_UNWIND_MARKER; */
4489 goto exit;
4490 }
4491 unsigned long nsp = GET_SP (ncontext);
4492 /* Check the new stack pointer */
4493 if (nsp <= sframe->arg2 || nsp > sframe->arg2 + sizeof (ucontext_t) + 1024)
4494 {
4495 /* lbuf[ind++] = SP_FAILED_UNWIND_MARKER; */
4496 goto exit;
4497 }
4498 wctx.sp = nsp;
4499 wctx.fp = GET_FP (ncontext);
4500 break;
4501 }
4502 else if (ret == RA_SIGRETURN)
4503 {
4504 struct sigcontext *sctx = (struct sigcontext*) wctx.sp;
4505 wctx.pc = sctx->eip;
4506 if (!__collector_check_segment (wctx.pc, &wctx.tbgn, &wctx.tend, 0))
4507 {
4508 /* lbuf[ind++] = SP_FAILED_UNWIND_MARKER; */
4509 goto exit;
4510 }
4511 wctx.sp = sctx->esp;
4512 wctx.fp = sctx->ebp;
4513 break;
4514 }
4515 #elif WSIZE(64)
4516 if (ret == RA_RT_SIGRETURN)
4517 {
4518 ucontext_t *ncontext = (ucontext_t*) wctx.sp;
4519 wctx.pc = GET_PC (ncontext);
4520 if (!__collector_check_segment (wctx.pc, &wctx.tbgn, &wctx.tend, 0))
4521 {
4522 /* lbuf[ind++] = SP_FAILED_UNWIND_MARKER; */
4523 goto exit;
4524 }
4525 unsigned long nsp = GET_SP (ncontext);
4526 /* Check the new stack pointer */
4527 if (nsp <= wctx.sp || nsp > wctx.sp + sizeof (ucontext_t) + 1024)
4528 {
4529 /* lbuf[ind++] = SP_FAILED_UNWIND_MARKER; */
4530 goto exit;
4531 }
4532 wctx.sp = nsp;
4533 wctx.fp = GET_FP (ncontext);
4534 break;
4535 }
4536 #endif /* WSIZE() */
4537 if (bptr != NULL && extra_frame && wctx.sp <= (unsigned long) bptr && ind < 2)
4538 {
4539 lbuf[0] = wctx.pc;
4540 if (ind == 0)
4541 {
4542 ind++;
4543 if (ind >= lsize)
4544 break;
4545 }
4546 }
4547 if (bptr == NULL || wctx.sp > (unsigned long) bptr)
4548 {
4549 lbuf[ind++] = wctx.pc;
4550 if (ind >= lsize)
4551 goto exit;
4552 }
4553 }
4554 }
4555
4556 exit:
4557 #if defined(DEBUG)
4558 if ((SP_DUMP_UNWIND & __collector_tracelevel) != 0)
4559 {
4560 DprintfT (SP_DUMP_UNWIND, "stack_unwind (x86 walk):%d found %d frames\n\n", __LINE__, ind);
4561 for (int i = 0; i < ind; i++)
4562 DprintfT (SP_DUMP_UNWIND, " %3d: 0x%lx\n", i, (unsigned long) lbuf[i]);
4563 }
4564 #endif
4565 dump_stack (__LINE__);
4566 if (ind >= lsize)
4567 {
4568 ind = lsize - 1;
4569 lbuf[ind++] = (unsigned long) SP_TRUNC_STACK_MARKER;
4570 }
4571 return ind * sizeof (long);
4572 }
4573
4574 #elif ARCH(Aarch64)
4575
4576 static int
4577 stack_unwind (char *buf, int size, void *bptr, void *eptr, ucontext_t *context, int mode)
4578 {
4579 if (buf && bptr && eptr && context && size + mode > 0)
4580 getByteInstruction ((unsigned char *) eptr);
4581 int ind = 0;
4582 __u64 *lbuf = (void *) buf;
4583 int lsize = size / sizeof (__u64);
4584 __u64 pc = context->uc_mcontext.pc;
4585 __u64 sp = context->uc_mcontext.sp;
4586 __u64 stack_base;
4587 unsigned long tbgn = 0;
4588 unsigned long tend = 0;
4589
4590 unsigned long *sbase = (unsigned long*) __collector_tsd_get_by_key (unwind_key);
4591 if (sbase && *sbase > sp)
4592 stack_base = *sbase;
4593 else
4594 {
4595 stack_base = sp + 0x100000;
4596 if (stack_base < sp) // overflow
4597 stack_base = (__u64) -1;
4598 }
4599 DprintfT (SP_DUMP_UNWIND,
4600 "unwind.c:%d stack_unwind %2d pc=0x%llx sp=0x%llx stack_base=0x%llx\n",
4601 __LINE__, ind, (unsigned long long) pc, (unsigned long long) sp,
4602 (unsigned long long) stack_base);
4603
4604 while (sp && pc)
4605 {
4606 DprintfT (SP_DUMP_UNWIND,
4607 "unwind.c:%d stack_unwind %2d pc=0x%llx sp=0x%llx\n",
4608 __LINE__, ind, (unsigned long long) pc, (unsigned long long) sp);
4609 // Dl_info dlinfo;
4610 // if (!dladdr ((void *) pc, &dlinfo))
4611 // break;
4612 // DprintfT (SP_DUMP_UNWIND, "%2d: %llx <%s+%llu> (%s)\n",
4613 // ind, (unsigned long long) pc,
4614 // dlinfo.dli_sname ? dlinfo.dli_sname : "(?)",
4615 // (unsigned long long) pc - (unsigned long long) dlinfo.dli_saddr,
4616 // dlinfo.dli_fname);
4617 lbuf[ind++] = pc;
4618 if (ind >= lsize || sp >= stack_base || (sp & 15) != 0)
4619 break;
4620 if (pc < tbgn || pc >= tend)
4621 if (!__collector_check_segment ((unsigned long) pc, &tbgn, &tend, 0))
4622 {
4623 DprintfT (SP_DUMP_UNWIND,
4624 "unwind.c:%d __collector_check_segment failed. sp=0x%lx\n",
4625 __LINE__, (unsigned long) sp);
4626 break;
4627 }
4628 pc = ((__u64 *) sp)[1];
4629 __u64 old_sp = sp;
4630 sp = ((__u64 *) sp)[0];
4631 if (sp < old_sp)
4632 break;
4633 }
4634 if (ind >= lsize)
4635 {
4636 ind = lsize - 1;
4637 lbuf[ind++] = (__u64) SP_TRUNC_STACK_MARKER;
4638 }
4639 return ind * sizeof (__u64);
4640 }
4641 #endif /* ARCH() */