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(__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 (__i386__)
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))
59 static int map_hw_reg_to_dwarf_reg [16];
61 #define DWARF_DATA_ALIGN 0
62 #define DWARF_PC_REG -1
65 static gboolean dwarf_reg_to_hw_reg_inited;
67 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
70 * mono_hw_reg_to_dwarf_reg:
72 * Map the hardware register number REG to the register number used by DWARF.
75 mono_hw_reg_to_dwarf_reg (int reg)
78 g_assert_not_reached ();
81 return map_hw_reg_to_dwarf_reg [reg];
90 g_assert (NUM_REGS > 0);
91 g_assert (sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int) == NUM_REGS);
92 for (i = 0; i < NUM_REGS; ++i) {
93 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
96 mono_memory_barrier ();
97 dwarf_reg_to_hw_reg_inited = TRUE;
101 mono_dwarf_reg_to_hw_reg (int reg)
103 if (!dwarf_reg_to_hw_reg_inited)
106 return map_dwarf_reg_to_hw_reg [reg];
109 static G_GNUC_UNUSED void
110 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
115 guint8 b = value & 0x7f;
117 if (value != 0) /* more bytes to come */
125 static inline guint32
126 decode_uleb128 (guint8 *buf, guint8 **endbuf)
136 res = res | (((int)(b & 0x7f)) << shift);
148 decode_sleb128 (guint8 *buf, guint8 **endbuf)
158 res = res | (((int)(b & 0x7f)) << shift);
161 if (shift < 32 && (b & 0x40))
162 res |= - (1 << shift);
173 * mono_unwind_ops_encode:
175 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
176 * Return a pointer to malloc'ed memory.
179 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
184 guint8 *buf, *p, *res;
186 p = buf = g_malloc0 (4096);
190 for (; l; l = l->next) {
195 /* Convert the register from the hw encoding to the dwarf encoding */
196 reg = mono_hw_reg_to_dwarf_reg (op->reg);
198 /* Emit an advance_loc if neccesary */
199 while (op->when > loc) {
200 if (op->when - loc < 32) {
201 *p ++ = DW_CFA_advance_loc | (op->when - loc);
204 *p ++ = DW_CFA_advance_loc | (30);
212 encode_uleb128 (reg, p, &p);
213 encode_uleb128 (op->val, p, &p);
215 case DW_CFA_def_cfa_offset:
217 encode_uleb128 (op->val, p, &p);
219 case DW_CFA_def_cfa_register:
221 encode_uleb128 (reg, p, &p);
224 *p ++ = DW_CFA_offset | reg;
225 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
228 g_assert_not_reached ();
233 g_assert (p - buf < 4096);
235 res = g_malloc (p - buf);
236 memcpy (res, buf, p - buf);
242 #define UNW_DEBUG(stmt) do { stmt; } while (0)
244 #define UNW_DEBUG(stmt) do { } while (0)
247 static G_GNUC_UNUSED void
248 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
252 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
254 for (i = 0; i < nregs; ++i)
255 if (locations [i].loc_type == LOC_OFFSET)
256 printf ("r%d@%d(cfa) ", i, locations [i].offset);
261 * Given the state of the current frame as stored in REGS, execute the unwind
262 * operations in unwind_info until the location counter reaches POS. The result is
263 * stored back into REGS. OUT_CFA will receive the value of the CFA.
264 * This function is signal safe.
267 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
268 guint8 *start_ip, guint8 *end_ip, guint8 *ip, gssize *regs,
269 int nregs, guint8 **out_cfa)
271 Loc locations [NUM_REGS];
272 int i, pos, reg, cfa_reg, cfa_offset;
276 g_assert (nregs <= NUM_REGS);
278 for (i = 0; i < nregs; ++i)
279 locations [i].loc_type = LOC_SAME;
285 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
289 case DW_CFA_advance_loc:
290 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
295 reg = mono_dwarf_reg_to_hw_reg (*p & 0x3f);
297 locations [reg].loc_type = LOC_OFFSET;
298 locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
305 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
306 cfa_offset = decode_uleb128 (p, &p);
308 case DW_CFA_def_cfa_offset:
309 cfa_offset = decode_uleb128 (p, &p);
311 case DW_CFA_def_cfa_register:
312 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
314 case DW_CFA_advance_loc4:
319 g_assert_not_reached ();
324 g_assert_not_reached ();
328 cfa_val = (guint8*)regs [cfa_reg] + cfa_offset;
329 for (i = 0; i < nregs; ++i) {
330 if (locations [i].loc_type == LOC_OFFSET)
331 regs [i] = *(gssize*)(cfa_val + locations [i].offset);
338 mono_unwind_init (void)
340 InitializeCriticalSection (&unwind_mutex);
342 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
346 mono_unwind_cleanup (void)
350 DeleteCriticalSection (&unwind_mutex);
355 for (i = 0; i < cached_info_next; ++i) {
356 MonoUnwindInfo *cached = cached_info [i];
361 g_free (cached_info);
365 * mono_cache_unwind_info
367 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
368 * to mono_get_cached_unwind_info to get a cached copy of the info.
369 * A copy is made of the unwind info.
370 * This function is useful for two reasons:
371 * - many methods have the same unwind info
372 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
375 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
378 MonoUnwindInfo *info;
382 if (cached_info == NULL) {
383 cached_info_size = 16;
384 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
387 for (i = 0; i < cached_info_next; ++i) {
388 MonoUnwindInfo *cached = cached_info [i];
390 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
396 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
397 info->len = unwind_info_len;
398 memcpy (&info->info, unwind_info, unwind_info_len);
400 i = cached_info_next;
402 if (cached_info_next >= cached_info_size) {
403 MonoUnwindInfo **old_table, **new_table;
406 * Have to resize the table, while synchronizing with
407 * mono_get_cached_unwind_info () using hazard pointers.
410 old_table = cached_info;
411 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
413 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
415 mono_memory_barrier ();
417 cached_info = new_table;
419 mono_memory_barrier ();
421 mono_thread_hazardous_free_or_queue (old_table, g_free);
423 cached_info_size *= 2;
426 cached_info [cached_info_next ++] = info;
428 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
435 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
440 /* Get the pointer */
442 /* If we don't have hazard pointers just return the
446 /* Make it hazardous */
447 mono_hazard_pointer_set (hp, hazard_index, p);
448 /* Check that it's still the same. If not, try
451 mono_hazard_pointer_clear (hp, hazard_index);
461 * This function is signal safe.
464 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
466 MonoUnwindInfo **table;
467 MonoUnwindInfo *info;
469 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
471 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
473 info = table [index];
475 *unwind_info_len = info->len;
478 mono_hazard_pointer_clear (hp, 0);
484 * mono_unwind_get_dwarf_data_align:
486 * Return the data alignment used by the encoded unwind information.
489 mono_unwind_get_dwarf_data_align (void)
491 return DWARF_DATA_ALIGN;
495 * mono_unwind_get_dwarf_pc_reg:
497 * Return the dwarf register number of the register holding the ip of the
501 mono_unwind_get_dwarf_pc_reg (void)
507 decode_cie_op (guint8 *p, guint8 **endp)
512 case DW_CFA_advance_loc:
517 decode_uleb128 (p, &p);
524 decode_uleb128 (p, &p);
525 decode_uleb128 (p, &p);
527 case DW_CFA_def_cfa_offset:
528 decode_uleb128 (p, &p);
530 case DW_CFA_def_cfa_register:
531 decode_uleb128 (p, &p);
533 case DW_CFA_advance_loc4:
537 g_assert_not_reached ();
542 g_assert_not_reached ();
549 * mono_unwind_get_ops_from_fde:
551 * Return the unwind opcodes encoded in a DWARF FDE entry.
554 mono_unwind_get_ops_from_fde (guint8 *fde, guint32 *out_len)
556 guint8 *p, *cie, *code, *fde_cfi, *cie_cfi;
557 gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
558 gint32 cie_len, cie_id, cie_version, code_align, data_align, return_reg;
559 gint32 i, cie_aug_len, buf_len;
564 * http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
570 // FIXME: Endianess ?
571 fde_len = *(guint32*)p;
572 g_assert (fde_len != 0xffffffff && fde_len != 0);
574 cie_offset = *(guint32*)p;
575 cie = p - cie_offset;
577 pc_begin = *(gint32*)p;
580 pc_range = *(guint32*)p;
582 aug_len = decode_uleb128 (p, &p);
583 g_assert (aug_len == 0);
585 fde_data_len = fde + 4 + fde_len - p;
589 cie_len = *(guint32*)p;
591 cie_id = *(guint32*)p;
592 g_assert (cie_id == 0);
595 g_assert (cie_version == 1);
597 cie_aug_str = (char*)p;
598 p += strlen (cie_aug_str) + 1;
599 code_align = decode_uleb128 (p, &p);
600 data_align = decode_sleb128 (p, &p);
601 return_reg = decode_uleb128 (p, &p);
602 if (strstr (cie_aug_str, "z")) {
603 cie_aug_len = decode_uleb128 (p, &p);
608 /* Make sure the FDE uses the same constants as we do */
609 g_assert (code_align == 1);
610 g_assert (data_align == DWARF_DATA_ALIGN);
611 g_assert (return_reg == DWARF_PC_REG);
613 buf_len = (cie + cie_len + 4 - cie_cfi) + (fde + fde_len + 4 - fde_cfi);
614 buf = g_malloc0 (buf_len);
618 while (p < cie + cie_len + 4) {
619 if (*p == DW_CFA_nop)
622 decode_cie_op (p, &p);
624 memcpy (buf + i, cie_cfi, p - cie_cfi);
628 while (p < fde + fde_len + 4) {
629 if (*p == DW_CFA_nop)
632 decode_cie_op (p, &p);
634 memcpy (buf + i, fde_cfi, p - fde_cfi);
636 g_assert (i <= buf_len);
640 return g_realloc (buf, i);