2 * unwind.c: Stack Unwinding Interface
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2008 Novell, Inc.
13 #include <mono/utils/mono-counters.h>
14 #include <mono/metadata/threads-types.h>
28 guint8 info [MONO_ZERO_LEN_ARRAY];
31 static CRITICAL_SECTION unwind_mutex;
33 static MonoUnwindInfo **cached_info;
34 static int cached_info_next, cached_info_size;
36 static int unwind_info_size;
38 #define unwind_lock() EnterCriticalSection (&unwind_mutex)
39 #define unwind_unlock() LeaveCriticalSection (&unwind_mutex)
42 static int map_hw_reg_to_dwarf_reg [] = { 0, 2, 1, 3, 7, 6, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
43 #define NUM_REGS AMD64_NREG
44 #define DWARF_DATA_ALIGN (-8)
45 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (AMD64_RIP))
46 #elif defined(TARGET_ARM)
47 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0040a/IHI0040A_aadwarf.pdf
48 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
50 #define DWARF_DATA_ALIGN (-4)
51 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (ARMREG_LR))
52 #elif defined (TARGET_X86)
53 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
55 #define NUM_REGS X86_NREG + 1
56 #define DWARF_DATA_ALIGN (-4)
57 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (X86_NREG))
58 #elif defined (TARGET_POWERPC)
59 // http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html
60 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
61 9, 10, 11, 12, 13, 14, 15, 16,
62 17, 18, 19, 20, 21, 22, 23, 24,
63 25, 26, 27, 28, 29, 30, 31 };
65 #define DWARF_DATA_ALIGN (-sizeof (mgreg_t))
66 #define DWARF_PC_REG 108
68 static int map_hw_reg_to_dwarf_reg [16];
70 #define DWARF_DATA_ALIGN 0
71 #define DWARF_PC_REG -1
74 static gboolean dwarf_reg_to_hw_reg_inited;
76 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
79 * mono_hw_reg_to_dwarf_reg:
81 * Map the hardware register number REG to the register number used by DWARF.
84 mono_hw_reg_to_dwarf_reg (int reg)
90 g_assert (reg < NUM_REGS);
94 g_assert_not_reached ();
97 return map_hw_reg_to_dwarf_reg [reg];
106 g_assert (NUM_REGS > 0);
107 g_assert (sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int) == NUM_REGS);
108 for (i = 0; i < NUM_REGS; ++i) {
109 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
112 mono_memory_barrier ();
113 dwarf_reg_to_hw_reg_inited = TRUE;
117 mono_dwarf_reg_to_hw_reg (int reg)
119 if (!dwarf_reg_to_hw_reg_inited)
122 return map_dwarf_reg_to_hw_reg [reg];
125 static G_GNUC_UNUSED void
126 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
131 guint8 b = value & 0x7f;
133 if (value != 0) /* more bytes to come */
141 static inline guint32
142 decode_uleb128 (guint8 *buf, guint8 **endbuf)
152 res = res | (((int)(b & 0x7f)) << shift);
164 decode_sleb128 (guint8 *buf, guint8 **endbuf)
174 res = res | (((int)(b & 0x7f)) << shift);
177 if (shift < 32 && (b & 0x40))
178 res |= - (1 << shift);
189 * mono_unwind_ops_encode:
191 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
192 * Return a pointer to malloc'ed memory.
195 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
200 guint8 *buf, *p, *res;
202 p = buf = g_malloc0 (4096);
206 for (; l; l = l->next) {
211 /* Convert the register from the hw encoding to the dwarf encoding */
212 reg = mono_hw_reg_to_dwarf_reg (op->reg);
214 /* Emit an advance_loc if neccesary */
215 while (op->when > loc) {
216 if (op->when - loc < 32) {
217 *p ++ = DW_CFA_advance_loc | (op->when - loc);
220 *p ++ = DW_CFA_advance_loc | (30);
228 encode_uleb128 (reg, p, &p);
229 encode_uleb128 (op->val, p, &p);
231 case DW_CFA_def_cfa_offset:
233 encode_uleb128 (op->val, p, &p);
235 case DW_CFA_def_cfa_register:
237 encode_uleb128 (reg, p, &p);
241 *p ++ = DW_CFA_offset_extended_sf;
242 encode_uleb128 (reg, p, &p);
243 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
245 *p ++ = DW_CFA_offset | reg;
246 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
250 g_assert_not_reached ();
255 g_assert (p - buf < 4096);
257 res = g_malloc (p - buf);
258 memcpy (res, buf, p - buf);
264 #define UNW_DEBUG(stmt) do { stmt; } while (0)
266 #define UNW_DEBUG(stmt) do { } while (0)
269 static G_GNUC_UNUSED void
270 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
274 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
276 for (i = 0; i < nregs; ++i)
277 if (locations [i].loc_type == LOC_OFFSET)
278 printf ("r%d@%d(cfa) ", i, locations [i].offset);
283 * Given the state of the current frame as stored in REGS, execute the unwind
284 * operations in unwind_info until the location counter reaches POS. The result is
285 * stored back into REGS. OUT_CFA will receive the value of the CFA.
286 * This function is signal safe.
289 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
290 guint8 *start_ip, guint8 *end_ip, guint8 *ip, gssize *regs,
291 int nregs, guint8 **out_cfa)
293 Loc locations [NUM_REGS];
294 int i, pos, reg, cfa_reg, cfa_offset, offset;
298 g_assert (nregs <= NUM_REGS);
300 for (i = 0; i < nregs; ++i)
301 locations [i].loc_type = LOC_SAME;
307 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
311 case DW_CFA_advance_loc:
312 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
317 reg = mono_dwarf_reg_to_hw_reg (*p & 0x3f);
319 locations [reg].loc_type = LOC_OFFSET;
320 locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
327 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
328 cfa_offset = decode_uleb128 (p, &p);
330 case DW_CFA_def_cfa_offset:
331 cfa_offset = decode_uleb128 (p, &p);
333 case DW_CFA_def_cfa_register:
334 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
336 case DW_CFA_offset_extended_sf:
337 reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
338 offset = decode_sleb128 (p, &p) * DWARF_DATA_ALIGN;
340 case DW_CFA_advance_loc4:
345 g_assert_not_reached ();
350 g_assert_not_reached ();
354 cfa_val = (guint8*)regs [cfa_reg] + cfa_offset;
355 for (i = 0; i < nregs; ++i) {
356 if (locations [i].loc_type == LOC_OFFSET)
357 regs [i] = *(gssize*)(cfa_val + locations [i].offset);
364 mono_unwind_init (void)
366 InitializeCriticalSection (&unwind_mutex);
368 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
372 mono_unwind_cleanup (void)
376 DeleteCriticalSection (&unwind_mutex);
381 for (i = 0; i < cached_info_next; ++i) {
382 MonoUnwindInfo *cached = cached_info [i];
387 g_free (cached_info);
391 * mono_cache_unwind_info
393 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
394 * to mono_get_cached_unwind_info to get a cached copy of the info.
395 * A copy is made of the unwind info.
396 * This function is useful for two reasons:
397 * - many methods have the same unwind info
398 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
401 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
404 MonoUnwindInfo *info;
408 if (cached_info == NULL) {
409 cached_info_size = 16;
410 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
413 for (i = 0; i < cached_info_next; ++i) {
414 MonoUnwindInfo *cached = cached_info [i];
416 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
422 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
423 info->len = unwind_info_len;
424 memcpy (&info->info, unwind_info, unwind_info_len);
426 i = cached_info_next;
428 if (cached_info_next >= cached_info_size) {
429 MonoUnwindInfo **old_table, **new_table;
432 * Have to resize the table, while synchronizing with
433 * mono_get_cached_unwind_info () using hazard pointers.
436 old_table = cached_info;
437 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
439 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
441 mono_memory_barrier ();
443 cached_info = new_table;
445 mono_memory_barrier ();
447 mono_thread_hazardous_free_or_queue (old_table, g_free);
449 cached_info_size *= 2;
452 cached_info [cached_info_next ++] = info;
454 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
461 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
466 /* Get the pointer */
468 /* If we don't have hazard pointers just return the
472 /* Make it hazardous */
473 mono_hazard_pointer_set (hp, hazard_index, p);
474 /* Check that it's still the same. If not, try
477 mono_hazard_pointer_clear (hp, hazard_index);
487 * This function is signal safe.
490 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
492 MonoUnwindInfo **table;
493 MonoUnwindInfo *info;
495 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
497 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
499 info = table [index];
501 *unwind_info_len = info->len;
504 mono_hazard_pointer_clear (hp, 0);
510 * mono_unwind_get_dwarf_data_align:
512 * Return the data alignment used by the encoded unwind information.
515 mono_unwind_get_dwarf_data_align (void)
517 return DWARF_DATA_ALIGN;
521 * mono_unwind_get_dwarf_pc_reg:
523 * Return the dwarf register number of the register holding the ip of the
527 mono_unwind_get_dwarf_pc_reg (void)
533 decode_cie_op (guint8 *p, guint8 **endp)
538 case DW_CFA_advance_loc:
543 decode_uleb128 (p, &p);
550 decode_uleb128 (p, &p);
551 decode_uleb128 (p, &p);
553 case DW_CFA_def_cfa_offset:
554 decode_uleb128 (p, &p);
556 case DW_CFA_def_cfa_register:
557 decode_uleb128 (p, &p);
559 case DW_CFA_advance_loc4:
563 g_assert_not_reached ();
568 g_assert_not_reached ();
575 * mono_unwind_get_ops_from_fde:
577 * Return the unwind opcodes encoded in a DWARF FDE entry.
580 mono_unwind_get_ops_from_fde (guint8 *fde, guint32 *out_len)
582 guint8 *p, *cie, *code, *fde_cfi, *cie_cfi;
583 gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
584 gint32 cie_len, cie_id, cie_version, code_align, data_align, return_reg;
585 gint32 i, cie_aug_len, buf_len;
590 * http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
596 // FIXME: Endianess ?
597 fde_len = *(guint32*)p;
598 g_assert (fde_len != 0xffffffff && fde_len != 0);
600 cie_offset = *(guint32*)p;
601 cie = p - cie_offset;
603 pc_begin = *(gint32*)p;
606 pc_range = *(guint32*)p;
608 aug_len = decode_uleb128 (p, &p);
609 g_assert (aug_len == 0);
611 fde_data_len = fde + 4 + fde_len - p;
615 cie_len = *(guint32*)p;
617 cie_id = *(guint32*)p;
618 g_assert (cie_id == 0);
621 g_assert (cie_version == 1);
623 cie_aug_str = (char*)p;
624 p += strlen (cie_aug_str) + 1;
625 code_align = decode_uleb128 (p, &p);
626 data_align = decode_sleb128 (p, &p);
627 return_reg = decode_uleb128 (p, &p);
628 if (strstr (cie_aug_str, "z")) {
629 cie_aug_len = decode_uleb128 (p, &p);
634 /* Make sure the FDE uses the same constants as we do */
635 g_assert (code_align == 1);
636 g_assert (data_align == DWARF_DATA_ALIGN);
637 g_assert (return_reg == DWARF_PC_REG);
639 buf_len = (cie + cie_len + 4 - cie_cfi) + (fde + fde_len + 4 - fde_cfi);
640 buf = g_malloc0 (buf_len);
644 while (p < cie + cie_len + 4) {
645 if (*p == DW_CFA_nop)
648 decode_cie_op (p, &p);
650 memcpy (buf + i, cie_cfi, p - cie_cfi);
654 while (p < fde + fde_len + 4) {
655 if (*p == DW_CFA_nop)
658 decode_cie_op (p, &p);
660 memcpy (buf + i, fde_cfi, p - fde_cfi);
662 g_assert (i <= buf_len);
666 return g_realloc (buf, i);