unwind.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>
  4. and Richard Henderson <rth@redhat.com>, 2003.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <setjmp.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include "pthreadP.h"
  22. #include <jmpbuf-unwind.h>
  23. #ifdef HAVE_FORCED_UNWIND
  24. #ifdef _STACK_GROWS_DOWN
  25. # define FRAME_LEFT(frame, other, adj) \
  26. ((uintptr_t) frame - adj >= (uintptr_t) other - adj)
  27. #elif _STACK_GROWS_UP
  28. # define FRAME_LEFT(frame, other, adj) \
  29. ((uintptr_t) frame - adj <= (uintptr_t) other - adj)
  30. #else
  31. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  32. #endif
  33. static _Unwind_Reason_Code
  34. unwind_stop (int version, _Unwind_Action actions,
  35. _Unwind_Exception_Class exc_class,
  36. struct _Unwind_Exception *exc_obj,
  37. struct _Unwind_Context *context, void *stop_parameter)
  38. {
  39. struct pthread_unwind_buf *buf = stop_parameter;
  40. struct pthread *self = THREAD_SELF;
  41. struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
  42. int do_longjump = 0;
  43. /* Adjust all pointers used in comparisons, so that top of thread's
  44. stack is at the top of address space. Without that, things break
  45. if stack is allocated above the main stack. */
  46. uintptr_t adj = (uintptr_t) self->stackblock + self->stackblock_size;
  47. /* Do longjmp if we're at "end of stack", aka "end of unwind data".
  48. We assume there are only C frame without unwind data in between
  49. here and the jmp_buf target. Otherwise simply note that the CFA
  50. of a function is NOT within it's stack frame; it's the SP of the
  51. previous frame. */
  52. if ((actions & _UA_END_OF_STACK)
  53. || ! _JMPBUF_CFA_UNWINDS_ADJ (buf->cancel_jmp_buf[0].jmp_buf, context,
  54. adj))
  55. do_longjump = 1;
  56. if (__builtin_expect (curp != NULL, 0))
  57. {
  58. /* Handle the compatibility stuff. Execute all handlers
  59. registered with the old method which would be unwound by this
  60. step. */
  61. struct _pthread_cleanup_buffer *oldp = buf->priv.data.cleanup;
  62. void *cfa = (void *) _Unwind_GetCFA (context);
  63. if (curp != oldp && (do_longjump || FRAME_LEFT (cfa, curp, adj)))
  64. {
  65. do
  66. {
  67. /* Pointer to the next element. */
  68. struct _pthread_cleanup_buffer *nextp = curp->__prev;
  69. /* Call the handler. */
  70. curp->__routine (curp->__arg);
  71. /* To the next. */
  72. curp = nextp;
  73. }
  74. while (curp != oldp
  75. && (do_longjump || FRAME_LEFT (cfa, curp, adj)));
  76. /* Mark the current element as handled. */
  77. THREAD_SETMEM (self, cleanup, curp);
  78. }
  79. }
  80. if (do_longjump)
  81. __libc_unwind_longjmp ((struct __jmp_buf_tag *) buf->cancel_jmp_buf, 1);
  82. return _URC_NO_REASON;
  83. }
  84. static void
  85. unwind_cleanup (_Unwind_Reason_Code reason, struct _Unwind_Exception *exc)
  86. {
  87. /* When we get here a C++ catch block didn't rethrow the object. We
  88. cannot handle this case and therefore abort. */
  89. # define STR_N_LEN(str) str, strlen (str)
  90. INTERNAL_SYSCALL_DECL (err);
  91. INTERNAL_SYSCALL (write, err, 3, STDERR_FILENO,
  92. STR_N_LEN ("FATAL: exception not rethrown\n"));
  93. abort ();
  94. }
  95. #endif /* have forced unwind */
  96. void
  97. attribute_protected
  98. __cleanup_fct_attribute __attribute ((noreturn))
  99. __pthread_unwind (__pthread_unwind_buf_t *buf)
  100. {
  101. struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
  102. struct pthread *self = THREAD_SELF;
  103. #ifdef HAVE_FORCED_UNWIND
  104. /* This is not a catchable exception, so don't provide any details about
  105. the exception type. We do need to initialize the field though. */
  106. THREAD_SETMEM (self, exc.exception_class, 0);
  107. THREAD_SETMEM (self, exc.exception_cleanup, unwind_cleanup);
  108. _Unwind_ForcedUnwind (&self->exc, unwind_stop, ibuf);
  109. #else
  110. /* Handle the compatibility stuff first. Execute all handlers
  111. registered with the old method. We don't execute them in order,
  112. instead, they will run first. */
  113. struct _pthread_cleanup_buffer *oldp = ibuf->priv.data.cleanup;
  114. struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
  115. if (curp != oldp)
  116. {
  117. do
  118. {
  119. /* Pointer to the next element. */
  120. struct _pthread_cleanup_buffer *nextp = curp->__prev;
  121. /* Call the handler. */
  122. curp->__routine (curp->__arg);
  123. /* To the next. */
  124. curp = nextp;
  125. }
  126. while (curp != oldp);
  127. /* Mark the current element as handled. */
  128. THREAD_SETMEM (self, cleanup, curp);
  129. }
  130. /* We simply jump to the registered setjmp buffer. */
  131. __libc_unwind_longjmp ((struct __jmp_buf_tag *) ibuf->cancel_jmp_buf, 1);
  132. #endif
  133. /* NOTREACHED */
  134. /* We better do not get here. */
  135. abort ();
  136. }
  137. hidden_def (__pthread_unwind)
  138. void
  139. __cleanup_fct_attribute __attribute ((noreturn))
  140. __pthread_unwind_next (__pthread_unwind_buf_t *buf)
  141. {
  142. struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
  143. __pthread_unwind ((__pthread_unwind_buf_t *) ibuf->priv.data.prev);
  144. }
  145. hidden_def (__pthread_unwind_next)