longjmp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2003-2004 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. #undef _FORTIFY_SOURCE
  23. #include <assert.h>
  24. #include <libunwind.h>
  25. #include <setjmp.h>
  26. #include <signal.h>
  27. #include <stdlib.h>
  28. #include "jmpbuf.h"
  29. #include "setjmp_i.h"
  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 longjmp here. */
  39. #define _longjmp __nonworking__longjmp
  40. #define longjmp __nonworking_longjmp
  41. static void _longjmp (jmp_buf env, int val);
  42. static void longjmp (jmp_buf env, int val);
  43. #endif
  44. #endif /* __GLIBC__ */
  45. void
  46. _longjmp (jmp_buf env, int val)
  47. {
  48. extern int _UI_longjmp_cont;
  49. unw_context_t uc;
  50. unw_cursor_t c;
  51. unw_word_t sp;
  52. unw_word_t *wp = (unw_word_t *) env;
  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. assert (UNW_NUM_EH_REGS >= 2);
  69. if (unw_set_reg (&c, UNW_REG_EH + 0, wp[JB_RP]) < 0
  70. || unw_set_reg (&c, UNW_REG_EH + 1, val) < 0
  71. || unw_set_reg (&c, UNW_REG_IP,
  72. (unw_word_t) (uintptr_t) &_UI_longjmp_cont))
  73. abort ();
  74. unw_resume (&c);
  75. abort ();
  76. }
  77. while (unw_step (&c) > 0);
  78. abort ();
  79. }
  80. #ifdef __GNUC__
  81. #define STRINGIFY1(x) #x
  82. #define STRINGIFY(x) STRINGIFY1(x)
  83. void longjmp (jmp_buf env, int val)
  84. __attribute__ ((alias (STRINGIFY(_longjmp))));
  85. #else
  86. void
  87. longjmp (jmp_buf env, int val)
  88. {
  89. _longjmp (env, val);
  90. }
  91. #endif /* __GNUC__ */