unwind.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <setjmp.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include "pthreadP.h"
  21. #include <jmpbuf-unwind.h>
  22. #ifdef HAVE_FORCED_UNWIND
  23. #ifdef _STACK_GROWS_DOWN
  24. # define FRAME_LEFT(frame, other, adj) \
  25. ((uintptr_t) frame - adj >= (uintptr_t) other - adj)
  26. #elif defined _STACK_GROWS_UP
  27. # define FRAME_LEFT(frame, other, adj) \
  28. ((uintptr_t) frame - adj <= (uintptr_t) other - adj)
  29. #else
  30. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  31. #endif
  32. static _Unwind_Reason_Code
  33. unwind_stop (int version, _Unwind_Action actions,
  34. _Unwind_Exception_Class exc_class,
  35. struct _Unwind_Exception *exc_obj,
  36. struct _Unwind_Context *context, void *stop_parameter)
  37. {
  38. struct pthread_unwind_buf *buf = stop_parameter;
  39. struct pthread *self = THREAD_SELF;
  40. struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
  41. int do_longjump = 0;
  42. /* Adjust all pointers used in comparisons, so that top of thread's
  43. stack is at the top of address space. Without that, things break
  44. if stack is allocated above the main stack. */
  45. uintptr_t adj = (uintptr_t) self->stackblock + self->stackblock_size;
  46. /* Do longjmp if we're at "end of stack", aka "end of unwind data".
  47. We assume there are only C frame without unwind data in between
  48. here and the jmp_buf target. Otherwise simply note that the CFA
  49. of a function is NOT within it's stack frame; it's the SP of the
  50. previous frame. */
  51. if ((actions & _UA_END_OF_STACK)
  52. || ! _JMPBUF_CFA_UNWINDS_ADJ (buf->cancel_jmp_buf[0].jmp_buf, context,
  53. adj))
  54. do_longjump = 1;
  55. if (__builtin_expect (curp != NULL, 0))
  56. {
  57. /* Handle the compatibility stuff. Execute all handlers
  58. registered with the old method which would be unwound by this
  59. step. */
  60. struct _pthread_cleanup_buffer *oldp = buf->priv.data.cleanup;
  61. void *cfa = (void *) _Unwind_GetCFA (context);
  62. if (curp != oldp && (do_longjump || FRAME_LEFT (cfa, curp, adj)))
  63. {
  64. do
  65. {
  66. /* Pointer to the next element. */
  67. struct _pthread_cleanup_buffer *nextp = curp->__prev;
  68. /* Call the handler. */
  69. curp->__routine (curp->__arg);
  70. /* To the next. */
  71. curp = nextp;
  72. }
  73. while (curp != oldp
  74. && (do_longjump || FRAME_LEFT (cfa, curp, adj)));
  75. /* Mark the current element as handled. */
  76. THREAD_SETMEM (self, cleanup, curp);
  77. }
  78. }
  79. if (do_longjump)
  80. __libc_unwind_longjmp ((struct __jmp_buf_tag *) buf->cancel_jmp_buf, 1);
  81. return _URC_NO_REASON;
  82. }
  83. static attribute_noreturn void
  84. unwind_cleanup (_Unwind_Reason_Code reason, struct _Unwind_Exception *exc)
  85. {
  86. /* When we get here a C++ catch block didn't rethrow the object. We
  87. cannot handle this case and therefore abort. */
  88. # define STR_N_LEN(str) str, strlen (str)
  89. INTERNAL_SYSCALL_DECL (err);
  90. INTERNAL_SYSCALL (write, err, 3, STDERR_FILENO,
  91. STR_N_LEN ("FATAL: exception not rethrown\n"));
  92. abort ();
  93. }
  94. #endif /* have forced unwind */
  95. void
  96. /*does not apply due to hidden_proto(): attribute_protected*/
  97. __cleanup_fct_attribute __attribute ((noreturn))
  98. #if !defined SHARED && !defined IS_IN_libpthread
  99. weak_function
  100. #endif
  101. __pthread_unwind (__pthread_unwind_buf_t *buf)
  102. {
  103. struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
  104. struct pthread *self = THREAD_SELF;
  105. #ifdef HAVE_FORCED_UNWIND
  106. /* This is not a catchable exception, so don't provide any details about
  107. the exception type. We do need to initialize the field though. */
  108. THREAD_SETMEM (self, exc.exception_class, 0);
  109. THREAD_SETMEM (self, exc.exception_cleanup, unwind_cleanup);
  110. _Unwind_ForcedUnwind (&self->exc, unwind_stop, ibuf);
  111. #else
  112. /* Handle the compatibility stuff first. Execute all handlers
  113. registered with the old method. We don't execute them in order,
  114. instead, they will run first. */
  115. struct _pthread_cleanup_buffer *oldp = ibuf->priv.data.cleanup;
  116. struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
  117. if (curp != oldp)
  118. {
  119. do
  120. {
  121. /* Pointer to the next element. */
  122. struct _pthread_cleanup_buffer *nextp = curp->__prev;
  123. /* Call the handler. */
  124. curp->__routine (curp->__arg);
  125. /* To the next. */
  126. curp = nextp;
  127. }
  128. while (curp != oldp);
  129. /* Mark the current element as handled. */
  130. THREAD_SETMEM (self, cleanup, curp);
  131. }
  132. /* We simply jump to the registered setjmp buffer. */
  133. __libc_unwind_longjmp ((struct __jmp_buf_tag *) ibuf->cancel_jmp_buf, 1);
  134. #endif
  135. /* NOTREACHED */
  136. /* We better do not get here. */
  137. abort ();
  138. }
  139. hidden_def (__pthread_unwind)
  140. void
  141. __cleanup_fct_attribute __attribute ((noreturn))
  142. __pthread_unwind_next (__pthread_unwind_buf_t *buf)
  143. {
  144. struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
  145. __pthread_unwind ((__pthread_unwind_buf_t *) ibuf->priv.data.prev);
  146. }
  147. hidden_def (__pthread_unwind_next)