siglongjmp.c 3.7 KB

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