Home | History | Annotate | Download | only in setjmp
      1 /* libunwind - a platform-independent unwind library
      2    Copyright (C) 2003-2005 Hewlett-Packard Co
      3 	Contributed by David Mosberger-Tang <davidm (at) hpl.hp.com>
      4 
      5 This file is part of libunwind.
      6 
      7 Permission is hereby granted, free of charge, to any person obtaining
      8 a copy of this software and associated documentation files (the
      9 "Software"), to deal in the Software without restriction, including
     10 without limitation the rights to use, copy, modify, merge, publish,
     11 distribute, sublicense, and/or sell copies of the Software, and to
     12 permit persons to whom the Software is furnished to do so, subject to
     13 the following conditions:
     14 
     15 The above copyright notice and this permission notice shall be
     16 included in all copies or substantial portions of the Software.
     17 
     18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
     25 
     26 #define UNW_LOCAL_ONLY
     27 
     28 #include <setjmp.h>
     29 
     30 #include "libunwind_i.h"
     31 #include "jmpbuf.h"
     32 #include "setjmp_i.h"
     33 
     34 #if !defined(_NSIG) && defined(_SIG_MAXSIG)
     35 # define _NSIG (_SIG_MAXSIG - 1)
     36 #endif
     37 
     38 #if defined(__GLIBC__)
     39 #if __GLIBC_PREREQ(2, 4)
     40 
     41 /* Starting with glibc-2.4, {sig,}setjmp in GLIBC obfuscates the
     42    register values in jmp_buf by XORing them with a "random"
     43    canary value.
     44 
     45    This makes it impossible to implement longjmp, as we
     46    can never match wp[JB_SP], unless we decode the canary first.
     47 
     48    Doing so is possible, but doesn't appear to be worth the trouble,
     49    so we simply defer to glibc siglongjmp here.  */
     50 
     51 #define siglongjmp __nonworking_siglongjmp
     52 static void siglongjmp (sigjmp_buf env, int val) UNUSED;
     53 #endif
     54 #endif /* __GLIBC_PREREQ */
     55 
     56 void
     57 siglongjmp (sigjmp_buf env, int val)
     58 {
     59   unw_word_t *wp = (unw_word_t *) env;
     60   extern int _UI_siglongjmp_cont;
     61   extern int _UI_longjmp_cont;
     62   unw_context_t uc;
     63   unw_cursor_t c;
     64   unw_word_t sp;
     65   int *cont;
     66 
     67   if (unw_getcontext (&uc) < 0 || unw_init_local (&c, &uc) < 0)
     68     abort ();
     69 
     70   do
     71     {
     72       if (unw_get_reg (&c, UNW_REG_SP, &sp) < 0)
     73 	abort ();
     74 #ifdef __FreeBSD__
     75       if (sp != wp[JB_SP] + sizeof(unw_word_t))
     76 #else
     77       if (sp != wp[JB_SP])
     78 #endif
     79 	continue;
     80 
     81       if (!bsp_match (&c, wp))
     82 	continue;
     83 
     84       /* found the right frame: */
     85 
     86       /* default to resuming without restoring signal-mask */
     87       cont = &_UI_longjmp_cont;
     88 
     89       /* Order of evaluation is important here: if unw_resume()
     90 	 restores signal mask, we must set it up appropriately, even
     91 	 if wp[JB_MASK_SAVED] is FALSE.  */
     92       if (!resume_restores_sigmask (&c, wp) && wp[JB_MASK_SAVED])
     93 	{
     94 	  /* sigmask was saved */
     95 #if defined(__linux__)
     96 	  if (UNW_NUM_EH_REGS < 4 || _NSIG > 16 * sizeof (unw_word_t))
     97 	    /* signal mask doesn't fit into EH arguments and we can't
     98 	       put it on the stack without overwriting something
     99 	       else... */
    100 	    abort ();
    101 	  else
    102 	    if (unw_set_reg (&c, UNW_REG_EH + 2, wp[JB_MASK]) < 0
    103 		|| (_NSIG > 8 * sizeof (unw_word_t)
    104 		    && unw_set_reg (&c, UNW_REG_EH + 3, wp[JB_MASK + 1]) < 0))
    105 	      abort ();
    106 #elif defined(__FreeBSD__)
    107 	  if (unw_set_reg (&c, UNW_REG_EH + 2, &wp[JB_MASK]) < 0)
    108 	      abort();
    109 #else
    110 #error Port me
    111 #endif
    112 	  cont = &_UI_siglongjmp_cont;
    113 	}
    114 
    115       if (unw_set_reg (&c, UNW_REG_EH + 0, wp[JB_RP]) < 0
    116 	  || unw_set_reg (&c, UNW_REG_EH + 1, val) < 0
    117 	  || unw_set_reg (&c, UNW_REG_IP, (unw_word_t) (uintptr_t) cont))
    118 	abort ();
    119 
    120       unw_resume (&c);
    121 
    122       abort ();
    123     }
    124   while (unw_step (&c) > 0);
    125 
    126   abort ();
    127 }
    128