Wrong value when unwinding for the IP register on Mac M1 program

Viewed 26

I am trying to use unwind in Mac M1 but getting the wrong results. here is my program (I took one of the unwind tests).

    /* libunwind - a platform-independent unwind library
   Copyright (C) 2001-2004 Hewlett-Packard Co
    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
                                                            "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

/* This illustrates the basics of using the unwind interface for
   exception handling.  */

// #include "compiler.h"

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <libunwind.h>

#ifdef HAVE_IA64INTRIN_H
# include <ia64intrin.h>
#endif

#define panic(args...)              \
    { ++nerrors; fprintf (stderr, args); }

    int nerrors = 0;
int verbose = 1;
int depth = 13;
volatile int got_here = 0;

extern void b (int);

void
raise_exception (void)
{
      unw_cursor_t cursor;
      unw_context_t uc;
      int i;
      unw_word_t ip;
      unw_word_t sp;

      unw_getcontext (&uc);
      if (unw_init_local (&cursor, &uc) < 0)
            {
              panic ("unw_init_local() failed!\n");
              return;
            }

      if (verbose)
        {
              printf ("raise_exception(): sp=%p\n", (void*) &sp);
              unw_get_reg (&cursor, UNW_REG_IP, &ip);
              unw_get_reg (&cursor, UNW_REG_SP, &sp);
              printf ("\t #%-3d ip=%p sp=%p\n", 0, (void*) ip, (void*) sp);
            }

      /* unwind to top-most frame a(), skipping over b() and raise_exception(): */
      for (i = 0; i < depth + 2; ++i)
               if (unw_step (&cursor) < 0)
                 {
                panic ("unw_step() failed!\n");
                return;
                  }
        else if (verbose)
          {
                unw_get_reg (&cursor, UNW_REG_IP, &ip);
                unw_get_reg (&cursor, UNW_REG_SP, &sp);
                printf ("\t #%-3d ip=%p sp=%p\n", i + 1, (void*) ip, (void*) sp);
                  }
      unw_resume (&cursor); /* transfer control to exception handler */
}

uintptr_t
get_bsp (void)
{
    #if UNW_TARGET_IA64
    # ifdef __INTEL_COMPILER
      return __getReg (_IA64_REG_AR_BSP);
    # else
  return (uintptr_t) __builtin_ia64_bsp ();
    # endif
    #else
  return 0;
    #endif
}
#define NOINLINE __attribute__((noinline))

static __inline__ void * get_pc(void)  {
        void *pc;
        asm("ADR %0, ." : "=r"(pc));
            return pc;
}

int NOINLINE
a (int n)
{
      long stack;
      int result = 99;

      if (verbose)
        printf ("a(n=%d): sp=%p bsp=0x%lx, pc=%#x\n",
                        n, &stack, (unsigned long) get_bsp (), get_pc());

              if (n > 0)
        a (n - 1);
          else
        b (16);

          if (verbose)
        {
              printf ("exception handler: here we go (sp=%p, bsp=0x%lx)...\n",
                              &stack, (unsigned long) get_bsp ());
                  /* This call works around a bug in gcc (up-to pre3.4) which
                 causes invalid assembly code to be generated when
             __builtin_ia64_bsp() gets predicated.  */
              getpid ();
            }
      if (n == depth)
        {
              result = 0;
              got_here = 1;
            }
      return result;
}

void NOINLINE
b (int n)
{
      if ((n & 1) == 0)
        {
              if (verbose)
                printf ("b(n=%d) calling raise_exception()\n", n);
                  raise_exception ();
            }
      panic ("FAILURE: b() returned from raise_exception()!!\n");
}

int
main (int argc, char **argv)
{
      int result;

      if (argc > 1)
        {
              ++verbose;
              depth = atol (argv[1]);
              if (depth < 1)
                {
                  fprintf (stderr, "Usage: %s depth\n"
                                   "  depth must be >= 1\n", argv[0]);
                      exit (-1);
                }
            }

      result = a (depth);
      if (result != 0 || !got_here || nerrors > 0)
        {
              fprintf (stderr,
                               "FAILURE: test failed: result=%d got_here=%d nerrors=%d\n",
                               result, got_here, nerrors);
                  exit (-1);
            }

      if (verbose)
        printf ("SUCCESS!\n");
          return 0;
}

I added the get_pc function myself. the output that I am getting is:

a(n=13): sp=0x16b1eacf0 bsp=0x0, pc=0x4c17bc4 <-- this is the pc offset from the beginning of the image. It is always the same (it should be the same)...I debugged it with lldb and it is correct...
a(n=12): sp=0x16b1eac90 bsp=0x0, pc=0x4c17bc4
a(n=11): sp=0x16b1eac30 bsp=0x0, pc=0x4c17bc4
a(n=10): sp=0x16b1eabd0 bsp=0x0, pc=0x4c17bc4
a(n=9): sp=0x16b1eab70 bsp=0x0, pc=0x4c17bc4
a(n=8): sp=0x16b1eab10 bsp=0x0, pc=0x4c17bc4
a(n=7): sp=0x16b1eaab0 bsp=0x0, pc=0x4c17bc4
a(n=6): sp=0x16b1eaa50 bsp=0x0, pc=0x4c17bc4
a(n=5): sp=0x16b1ea9f0 bsp=0x0, pc=0x4c17bc4
a(n=4): sp=0x16b1ea990 bsp=0x0, pc=0x4c17bc4
a(n=3): sp=0x16b1ea930 bsp=0x0, pc=0x4c17bc4
a(n=2): sp=0x16b1ea8d0 bsp=0x0, pc=0x4c17bc4
a(n=1): sp=0x16b1ea870 bsp=0x0, pc=0x4c17bc4
a(n=0): sp=0x16b1ea810 bsp=0x0, pc=0x4c17bc4
b(n=16) calling raise_exception()
raise_exception(): sp=0x16b1e9ca8
         #0   ip=0x6670000104c178ec sp=0x16b1e9c70 <-- the IP is not correct... and it changes all the time...
         #1   ip=0xa71f800104c17c28 sp=0x16b1ea7b0
         #2   ip=0x476f000104c17b48 sp=0x16b1ea7d0
         #3   ip=0x831e000104c17b3c sp=0x16b1ea830
         #4   ip=0x7e6d000104c17b3c sp=0x16b1ea890
         #5   ip=0xfa57800104c17b3c sp=0x16b1ea8f0
         #6   ip=0xf3e000104c17b3c sp=0x16b1ea950
         #7   ip=0x2029800104c17b3c sp=0x16b1ea9b0
         #8   ip=0x647e800104c17b3c sp=0x16b1eaa10
         #9   ip=0xa105800104c17b3c sp=0x16b1eaa70
         #10  ip=0x6c78000104c17b3c sp=0x16b1eaad0
         #11  ip=0xe96d800104c17b3c sp=0x16b1eab30
         #12  ip=0x4009000104c17b3c sp=0x16b1eab90
         #13  ip=0xc647800104c17b3c sp=0x16b1eabf0
         #14  ip=0x5426800104c17b3c sp=0x16b1eac50
         #15  ip=0x1837800104c17b3c sp=0x16b1eacb0
exception handler: here we go (sp=0x16b1eacf0, bsp=0x0)...
SUCCESS!

What am I doing wrong ?

0 Answers
Related