1 // -*- C++ -*- Exception handling routines for Transactional Memory.
2 // Copyright (C) 2009-2018 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
6 // GCC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 //
11 // GCC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 #include <cstdlib>
26 #include "unwind-cxx.h"
27 #include "eh_atomics.h"
28 
29 using namespace __cxxabiv1;
30 
31 // Free one C++ exception.
32 
33 static void
34 free_any_cxa_exception (_Unwind_Exception *eo)
35 {
36   __cxa_refcounted_exception *h
37     = __get_refcounted_exception_header_from_ue (eo);
38 
39   if (__is_dependent_exception (eo->exception_class))
40     {
41       __cxa_dependent_exception *dep
42 	= __get_dependent_exception_from_ue (eo);
43 
44       h = __get_refcounted_exception_header_from_obj (dep->primaryException);
45 
46       __cxa_free_dependent_exception (dep);
47     }
48 
49   if (__gnu_cxx::__eh_atomic_dec (&h->referenceCount))
50     __cxa_free_exception (h + 1);
51 }
52 
53 // Cleanup exception handling state while rolling back state for
54 // a software transactional memory transaction.
55 //
56 // UNTHROWN_OBJ is non-null if we've called __cxa_allocate_exception
57 // but not yet called __cxa_throw for it.
58 //
59 // CLEANUP_EXC is non-null if we're currently processing a cleanup
60 // along an exception path, but we've not caught the exception yet.
61 //
62 // CAUGHT_COUNT is the nesting depth of __cxa_begin_catch within
63 // the transaction; undo as if calling __cxa_end_catch that many times.
64 
65 extern "C" void
66 __cxxabiv1::__cxa_tm_cleanup (void *unthrown_obj,
67 			      void *cleanup_exc,
68 			      unsigned int caught_count) throw()
69 {
70   __cxa_eh_globals *globals = __cxa_get_globals_fast ();
71 
72   // Handle a C++ exception not yet thrown.
73   if (unthrown_obj)
74     {
75       globals->uncaughtExceptions -= 1;
76       __cxa_free_exception (unthrown_obj);
77     }
78 
79   // Handle an exception not yet caught ie. processing a cleanup
80   // in between the throw and the catch.
81   if (cleanup_exc)
82     {
83       _Unwind_Exception *eo
84 	= reinterpret_cast <_Unwind_Exception *>(cleanup_exc);
85       if (__is_gxx_exception_class (eo->exception_class))
86 	free_any_cxa_exception (eo);
87       else
88 	_Unwind_DeleteException (eo);
89     }
90 
91   // Do __cxa_end_catch caught_count times, but don't bother running
92   // the destructors for the objects involved.  All of that is being
93   // undone by the transaction restart.
94   if (caught_count > 0)
95     {
96       __cxa_exception *h = globals->caughtExceptions;
97 
98       // Rethrown foreign exceptions are removed from the stack immediately.
99       // We would have freed this exception via THIS_EXC above.
100       if (h == NULL)
101 	return;
102 
103       do
104 	{
105 	  __cxa_exception *next;
106 	  _Unwind_Exception *eo = &h->unwindHeader;
107 
108 	  if (__is_gxx_exception_class (eo->exception_class))
109 	    {
110 	      next = h->nextException;
111 	      free_any_cxa_exception (eo);
112 	    }
113 	  else
114 	    {
115 	      _Unwind_DeleteException (eo);
116 	      next = 0;
117 	    }
118 
119 	  h = next;
120 	}
121       while (--caught_count);
122 
123       globals->caughtExceptions = h;
124     }
125 }
126