except-gcc.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /* except-gcc.cpp -*-C++-*-
  2. *
  3. *************************************************************************
  4. *
  5. * @copyright
  6. * Copyright (C) 2009-2013, Intel Corporation
  7. * All rights reserved.
  8. *
  9. * @copyright
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. * * Neither the name of Intel Corporation nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * @copyright
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  35. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. **************************************************************************/
  38. #include "except-gcc.h"
  39. #include "except.h"
  40. #include "sysdep.h"
  41. #include "bug.h"
  42. #include "local_state.h"
  43. #include "full_frame.h"
  44. #include "scheduler.h"
  45. #include "frame_malloc.h"
  46. #include "pedigrees.h"
  47. #include <stdint.h>
  48. #include <typeinfo>
  49. #define DEBUG_EXCEPTIONS 0
  50. struct pending_exception_info
  51. {
  52. void make(__cxa_eh_globals *, _Unwind_Exception *, bool);
  53. void destruct();
  54. bool empty() const;
  55. void check() const;
  56. /* Active exception at time of suspend. */
  57. _Unwind_Exception *active;
  58. /* If true the most recently caught exception is to be rethrown
  59. on resume. This handling is technically incorrect but allows
  60. running without compiler support; the proper standards-compliant
  61. method is to save the exception in the previous field. */
  62. bool rethrow;
  63. struct __cxa_eh_globals runtime_state;
  64. };
  65. void pending_exception_info::check() const
  66. {
  67. if (active)
  68. CILK_ASSERT((int)runtime_state.uncaughtExceptions > 0);
  69. }
  70. void pending_exception_info::make(__cxa_eh_globals *state_in,
  71. _Unwind_Exception *exc_in, bool rethrow_in)
  72. {
  73. active = exc_in;
  74. rethrow = rethrow_in;
  75. runtime_state = *state_in;
  76. /* Read and clear C++ runtime state. */
  77. state_in->caughtExceptions = 0;
  78. state_in->uncaughtExceptions = 0;
  79. #if CILK_LIB_DEBUG
  80. check();
  81. #endif
  82. }
  83. bool
  84. pending_exception_info::empty() const
  85. {
  86. return !active && !rethrow && !runtime_state.caughtExceptions &&
  87. !runtime_state.uncaughtExceptions;
  88. }
  89. #if DEBUG_EXCEPTIONS
  90. #include <stdio.h>
  91. static void
  92. decode_exceptions(char *out, size_t len, struct pending_exception_info *info)
  93. {
  94. if (info->empty())
  95. snprintf(out, len, "[empty]");
  96. else if (info->rethrow)
  97. snprintf(out, len, "[rethrow %p]",
  98. info->runtime_state.caughtExceptions);
  99. else
  100. snprintf(out, len, "[throw %p]", (void *)info->active);
  101. }
  102. #endif
  103. static void
  104. save_exception_info(__cilkrts_worker *w,
  105. __cxa_eh_globals *state,
  106. _Unwind_Exception *exc,
  107. bool rethrow,
  108. const char *why)
  109. {
  110. struct pending_exception_info *info =
  111. (struct pending_exception_info *)__cilkrts_frame_malloc(w, sizeof (struct pending_exception_info));
  112. CILK_ASSERT(info);
  113. info->make(state, exc, rethrow);
  114. #if DEBUG_EXCEPTIONS
  115. {
  116. char buf[40];
  117. decode_exceptions(buf, sizeof buf, info);
  118. fprintf(stderr, "make exception info W%u %p %s (%s)\n",
  119. w->self, info, buf, why);
  120. }
  121. #endif
  122. CILK_ASSERT(w->l->pending_exception == 0);
  123. w->l->pending_exception = info;
  124. }
  125. #if DEBUG_EXCEPTIONS
  126. #include <stdio.h> /* DEBUG */
  127. static void decode_flags(int flags, char out[9])
  128. {
  129. out[0] = (flags & CILK_FRAME_STOLEN) ? 'S' : '_';
  130. out[1] = (flags & CILK_FRAME_UNSYNCHED) ? 'U' : '_';
  131. out[2] = (flags & CILK_FRAME_DETACHED) ? 'D' : '_';
  132. out[3] = (flags & CILK_FRAME_EXCEPTING) ? 'X' : '_';
  133. out[4] = '\0';
  134. }
  135. #endif
  136. /* __cilkrts_save_except is called from the runtime epilogue
  137. when a function is returning with an exception pending.
  138. If the function has a parent to which it could return normally,
  139. return and have the caller call _Unwind_Resume, the same as if
  140. an exception filter had not matched.
  141. Otherwise save the exception in the worker.
  142. If this is a return from a ordinary call that must go through
  143. the runtime, the assembly epilogue must have saved the call-saved
  144. register state in the parent frame. */
  145. extern "C"
  146. CILK_ABI_THROWS_VOID
  147. __cilkrts_return_exception(__cilkrts_stack_frame *sf)
  148. {
  149. __cilkrts_worker *w = sf->worker;
  150. _Unwind_Exception *exc = (_Unwind_Exception *)sf->except_data;
  151. CILK_ASSERT(sf->flags & CILK_FRAME_DETACHED);
  152. sf->flags &= ~CILK_FRAME_DETACHED;
  153. /*
  154. * If we are in replay mode, and a steal occurred during the recording
  155. * phase, stall till a steal actually occurs.
  156. */
  157. replay_wait_for_steal_if_parent_was_stolen(w);
  158. /* If this is to be an abnormal return, save the active exception. */
  159. if (!__cilkrts_pop_tail(w)) {
  160. /* Write a record to the replay log for an attempt to return to a
  161. stolen parent. This must be done before the exception handler
  162. invokes __cilkrts_leave_frame which will bump the pedigree so
  163. the replay_wait_for_steal_if_parent_was_stolen() above will match on
  164. replay */
  165. replay_record_orphaned(w);
  166. /* Now that the record/replay stuff is done, update the pedigree */
  167. update_pedigree_on_leave_frame(w, sf);
  168. /* Inline pop_frame; this may not be needed. */
  169. w->current_stack_frame = sf->call_parent;
  170. sf->call_parent = 0;
  171. __cxa_eh_globals *state = __cxa_get_globals();
  172. #if DEBUG_EXCEPTIONS
  173. fflush(stdout);
  174. char decoded[9];
  175. decode_flags(sf->flags, decoded);
  176. fprintf(stderr, "__cilkrts_save_except W%u sf %p/%s exc %p [%u %p] suspend\n",
  177. w->self, sf, decoded, exc,
  178. state->uncaughtExceptions,
  179. state->caughtExceptions);
  180. #endif
  181. /* Like __cilkrts_save_exception_state except for setting the
  182. rethrow flag. */
  183. save_exception_info(w, state, exc, exc == NULL, "save_except");
  184. {
  185. full_frame *ff = w->l->frame_ff;
  186. CILK_ASSERT(NULL == ff->pending_exception);
  187. ff->pending_exception = w->l->pending_exception;
  188. w->l->pending_exception = NULL;
  189. }
  190. __cilkrts_exception_from_spawn(w, sf); /* does not return */
  191. }
  192. /* This code path is taken when the parent is attached. It is on
  193. the same stack and part of the same full frame. The caller is
  194. cleaning up the Cilk frame during unwind and will reraise the
  195. exception */
  196. /* Now that the record/replay stuff is done, update the pedigree */
  197. update_pedigree_on_leave_frame(w, sf);
  198. #if DEBUG_EXCEPTIONS /* DEBUG ONLY */
  199. {
  200. __cxa_eh_globals *state = __cxa_get_globals();
  201. fflush(stdout);
  202. char decoded[9];
  203. decode_flags(sf->flags, decoded);
  204. fprintf(stderr, "__cilkrts_save_except W%d %p/%s %p->%p [%u %p] escape\n",
  205. w->self, sf, decoded, exc,
  206. exc ? to_cxx(exc)->nextException : 0,
  207. state->uncaughtExceptions,
  208. state->caughtExceptions);
  209. /* XXX This is triggering in the user thread which gets an exception
  210. from somewhere but does not get the corresponding runtime exception
  211. state.
  212. XXX There might be two or more uncaught exceptions. Test could be
  213. (uncaught != 0) == (exc != 0). First, design tests to see if that
  214. case is otherwise handled correctly. And what if there's an uncaught
  215. exception that does not belong to this function? I.e. this is a return
  216. from spawn in a destructor. */
  217. if (exc)
  218. CILK_ASSERT((int)state->uncaughtExceptions > 0);
  219. /*CILK_ASSERT(state->uncaughtExceptions == (exc != 0));*/
  220. }
  221. #endif
  222. /* The parent is attached so this exception can be propagated normally. */
  223. return;
  224. }
  225. /* Save the exception state into the full frame, which is exiting
  226. or suspending. */
  227. extern "C"
  228. void __cilkrts_save_exception_state(__cilkrts_worker *w, full_frame *ff)
  229. {
  230. save_exception_info(w, __cxa_get_globals(), 0, false, "undo-detach");
  231. CILK_ASSERT(NULL == ff->pending_exception);
  232. ff->pending_exception = w->l->pending_exception;
  233. w->l->pending_exception = NULL;
  234. }
  235. /* __cilkrts_c_sync_except is like __cilkrts_c_sync except that it
  236. saves exception state. __cilkrts_c_sync never returns here and
  237. always reinstalls the saved exception state.
  238. This function must be used because a parent of this function may
  239. be propagating an uncaught exception. The uncaught exception
  240. count must be saved by the child and passed back to the parent. */
  241. extern "C"
  242. NORETURN __cilkrts_c_sync_except (__cilkrts_worker *w, __cilkrts_stack_frame *sf)
  243. {
  244. __cxa_eh_globals *state = __cxa_get_globals();
  245. _Unwind_Exception *exc = (_Unwind_Exception *)sf->except_data;
  246. CILK_ASSERT((sf->flags & (CILK_FRAME_UNSYNCHED|CILK_FRAME_EXCEPTING)) ==
  247. (CILK_FRAME_UNSYNCHED|CILK_FRAME_EXCEPTING));
  248. sf->flags &= ~CILK_FRAME_EXCEPTING;
  249. #if DEBUG_EXCEPTIONS
  250. fflush(stdout);
  251. char decoded[9];
  252. decode_flags(sf->flags, decoded);
  253. if (exc)
  254. fprintf(stderr, "__cilkrts_sync_except W%u %p/%s %p->%p [%u %p]\n",
  255. w->self, sf, decoded, exc,
  256. to_cxx(exc)->nextException,
  257. state->uncaughtExceptions,
  258. state->caughtExceptions);
  259. else
  260. fprintf(stderr, "__cilkrts_sync_except W%d %p/%s none [%u %p]\n",
  261. w->self, sf, decoded,
  262. state->uncaughtExceptions,
  263. state->caughtExceptions);
  264. #endif
  265. /* Here the identity of an rethrown exception is always known.
  266. If exc is NULL this call is only to preserve parent state. */
  267. save_exception_info(w, state, exc, false, "sync_except");
  268. #if 0
  269. {
  270. full_frame *ff = w->l->frame_ff;
  271. CILK_ASSERT(NULL == ff->pending_exception);
  272. ff->pending_exception = w->l->pending_exception;
  273. w->l->pending_exception = NULL;
  274. }
  275. #endif
  276. CILK_ASSERT(!std::uncaught_exception());
  277. __cilkrts_c_sync(w, sf);
  278. }
  279. void
  280. pending_exception_info::destruct()
  281. {
  282. if (active) {
  283. #if DEBUG_EXCEPTIONS
  284. fprintf(stderr, "destroy exception info %p %p\n", this, active);
  285. #endif
  286. _Unwind_DeleteException(active);
  287. active = 0;
  288. } else {
  289. #if DEBUG_EXCEPTIONS
  290. fprintf(stderr, "destroy exception info %p\n", this);
  291. #endif
  292. }
  293. while (runtime_state.caughtExceptions) {
  294. __cxa_exception *exc = runtime_state.caughtExceptions;
  295. runtime_state.caughtExceptions = exc->nextException;
  296. #if DEBUG_EXCEPTIONS
  297. fprintf(stderr, "destroy caught exception %p\n", this);
  298. #endif
  299. _Unwind_DeleteException(&exc->unwindHeader);
  300. }
  301. }
  302. /*
  303. * __cilkrts_merge_pending_exceptions
  304. *
  305. * Merge the right exception record into the left. The left is logically
  306. * earlier.
  307. *
  308. * The active exception of E is
  309. * E->active if it is non-NULL (in which case E->rethrow is false)
  310. * unresolved if E->active is NULL and E->rethrow is true
  311. * nil if E->active is NULL and E->rethrow is false
  312. *
  313. * The merged active exception is left active exception if it is not
  314. * nil, otherwise the right.
  315. *
  316. * On entry the left state is synched and can not have an unresolved
  317. * exception. The merge may result in an unresolved exception.
  318. *
  319. * Due to scoping rules at most one of the caught exception lists is
  320. * non-NULL.
  321. */
  322. struct pending_exception_info *
  323. __cilkrts_merge_pending_exceptions (
  324. __cilkrts_worker *w,
  325. struct pending_exception_info *left,
  326. struct pending_exception_info *right)
  327. {
  328. /* If we've only got one exception, return it */
  329. if (NULL == left) {
  330. #if DEBUG_EXCEPTIONS
  331. if (right) {
  332. char buf[40];
  333. decode_exceptions(buf, sizeof buf, right);
  334. fprintf(stderr, "__cilkrts merge W%u nil %p -> %p %s\n",
  335. w->self, right, right, buf);
  336. }
  337. #endif
  338. return right;
  339. }
  340. if (NULL == right) {
  341. #if DEBUG_EXCEPTIONS
  342. if (left) {
  343. char buf[40];
  344. decode_exceptions(buf, sizeof buf, left);
  345. fprintf(stderr, "__cilkrts merge W%u %p nil -> %p %s\n",
  346. w->self, left, left, buf);
  347. }
  348. #endif
  349. return left;
  350. }
  351. #if CILK_LIB_DEBUG
  352. /*volatile struct pending_exception_info left_in = *left, right_in = *right;*/
  353. left->check();
  354. right->check();
  355. #endif
  356. #if DEBUG_EXCEPTIONS
  357. {
  358. char buf1[40], buf2[40];
  359. decode_exceptions(buf1, sizeof buf1, left);
  360. decode_exceptions(buf2, sizeof buf2, right);
  361. fprintf(stderr, "__cilkrts merge W%u %p %s %p %s\n",
  362. w->self, left, buf1, right, buf2);
  363. }
  364. #endif
  365. /* It should not be possible for both left and right to
  366. have accumulated catch blocks.
  367. The left exception record may always have a catch
  368. chain it kept when its parent was stolen.
  369. If they are siblings, the right sibling should not
  370. have accumulated any net catches. (Catch is lexically
  371. scoped.)
  372. If the right frame is a parent, it should not have entered
  373. a catch block without syncing first. If it spawned in a
  374. catch block, the child got its catch. */
  375. __cxa_exception *caught = left->runtime_state.caughtExceptions;
  376. if (caught)
  377. CILK_ASSERT(!right->runtime_state.caughtExceptions);
  378. else {
  379. CILK_ASSERT(!left->rethrow);
  380. left->rethrow = right->rethrow;
  381. left->runtime_state.caughtExceptions = caught = right->runtime_state.caughtExceptions;
  382. right->runtime_state.caughtExceptions = NULL;
  383. }
  384. /* Merge the uncaught exception and count of uncaught exceptions. */
  385. const unsigned int right_uncaught = right->runtime_state.uncaughtExceptions;
  386. if (!left->active){
  387. left->active = right->active; /* could be NULL */
  388. right->active = 0;
  389. left->runtime_state.uncaughtExceptions += right_uncaught;
  390. if (left->active)
  391. /* assert is C++ exception */
  392. /*CILK_ASSERT(__cxxabiv1::__is_gxx_exception_class(left->active->exception_class))*/;
  393. } else {
  394. /* Subtract 1 if the right exception is being destructed. */
  395. left->runtime_state.uncaughtExceptions += right_uncaught - (right->active != 0);
  396. }
  397. right->destruct();
  398. __cilkrts_frame_free(w, right, sizeof *right);
  399. /* If there is no state left, return NULL. */
  400. if (left->empty()) {
  401. left->destruct();
  402. __cilkrts_frame_free(w, left, sizeof *left);
  403. left = NULL;
  404. }
  405. #if CILK_LIB_DEBUG
  406. if (left)
  407. left->check();
  408. #endif
  409. return left;
  410. }
  411. #if 0
  412. /* __cilkrts_c_resume_except is called from the assembly language
  413. restart code when a resumed frame has a pending exception.
  414. The handler count negation on rethrow was done when the throw was
  415. resolved.
  416. The assembly language runtime must make the throw unwind to
  417. the sync, spawn, or other location where the exception should
  418. be injected. (This should not happen after a spawn but nothing
  419. here depends on there being no exception on steal.)
  420. This function is unused in the Intel stack based system. */
  421. extern "C"
  422. void __cilkrts_c_resume_except (_Unwind_Exception *exc)
  423. {
  424. #if DEBUG_EXCEPTIONS
  425. fprintf(stderr, "resume exception %p\n", exc);
  426. #endif
  427. _Unwind_Reason_Code why = _Unwind_RaiseException(exc);
  428. __cilkrts_bug ("Cilk runtime error: failed to reinstate suspended exception %p (%d)\n", exc, why);
  429. }
  430. #endif
  431. /* Restore the caught exception chain. This assumes no C++ exception
  432. code will run before the frame is resumed. If there is no exception
  433. to be resumed free the object. */
  434. extern "C"
  435. void __cilkrts_setup_for_execution_sysdep(__cilkrts_worker *w, full_frame *ff)
  436. {
  437. // ASSERT: We own w->lock and ff->lock || P == 1
  438. __cxa_eh_globals *state = __cxa_get_globals ();
  439. struct pending_exception_info *info = w->l->pending_exception;
  440. if (info == NULL)
  441. return;
  442. w->l->pending_exception = 0;
  443. #if DEBUG_EXCEPTIONS
  444. _Unwind_Exception *exc = info->active;
  445. if (exc) {
  446. fflush(stdout);
  447. fprintf(stderr, "__cilkrts_resume_except W%u %p->%p [%u %p]\n",
  448. w->self, exc,
  449. to_cxx(exc)->nextException,
  450. info->runtime_state.uncaughtExceptions,
  451. info->runtime_state.caughtExceptions);
  452. /*CILK_ASSERT(info->runtime_state.uncaughtExceptions > 0);*/
  453. }
  454. #endif
  455. if (state->uncaughtExceptions || state->caughtExceptions)
  456. __cilkrts_bug("W%u: resuming with non-empty prior exception state %u %p\n", state->uncaughtExceptions, state->caughtExceptions);
  457. *state = info->runtime_state;
  458. info->runtime_state.caughtExceptions = 0;
  459. info->runtime_state.uncaughtExceptions = 0;
  460. if (info->rethrow) {
  461. info->rethrow = false;
  462. /* Resuming function will rethrow. Runtime calls
  463. std::terminate if there is no caught exception. */
  464. ff->call_stack->flags |= CILK_FRAME_EXCEPTING;
  465. }
  466. if (info->active) {
  467. ff->call_stack->flags |= CILK_FRAME_EXCEPTING;
  468. ff->call_stack->except_data = info->active;
  469. info->active = 0;
  470. }
  471. if (info->empty()) {
  472. info->destruct();
  473. __cilkrts_frame_free(w, info, sizeof *info);
  474. w->l->pending_exception = NULL;
  475. }
  476. #if CILK_LIB_DEBUG
  477. if (ff->call_stack->except_data)
  478. CILK_ASSERT(std::uncaught_exception());
  479. #endif
  480. }
  481. #if 0
  482. extern "C"
  483. struct pending_exception_info *__cilkrts_get_exception(__cilkrts_worker *w,
  484. __cilkrts_stack_frame *sf)
  485. {
  486. struct pending_exception_info *info = w->l->pending_exception;
  487. if (info == NULL) {
  488. sf->flags &= ~CILK_FRAME_EXCEPTING;
  489. return 0;
  490. }
  491. w->l->pending_exception = NULL;
  492. /* This exception goes into the frame. */
  493. _Unwind_Exception *exc = info->active;
  494. info->active = NULL;
  495. info->destruct();
  496. __cilkrts_frame_free(w, info, sizeof *info);
  497. info = 0;
  498. sf->flags |= CILK_FRAME_EXCEPTING;
  499. sf->exception = exc;
  500. return 0;
  501. }
  502. #endif
  503. extern "C"
  504. void __attribute__((nonnull)) __cilkrts_gcc_rethrow(__cilkrts_stack_frame *sf)
  505. {
  506. #ifdef __CYGWIN__
  507. // Cygwin doesn't support exceptions, so _Unwind_Resume isn't available
  508. // Which means we can't support exceptions either
  509. __cilkrts_bug("The Cygwin implementation of the Intel Cilk Plus runtime doesn't support exceptions\n");
  510. #else
  511. if (sf->except_data) {
  512. #if CILK_LIB_DEBUG
  513. CILK_ASSERT(std::uncaught_exception());
  514. #endif
  515. _Unwind_Resume ((_Unwind_Exception *)sf->except_data);
  516. } else {
  517. throw;
  518. }
  519. #endif // __CYGWIN__
  520. }
  521. /* End except-gcc.cpp */