OSDN Git Service

Move ChangeLog entry to correct file
[pf3gnuchains/gcc-fork.git] / libjava / exception.cc
1 // Functions for Exception Support for Java.
2
3 /* Copyright (C) 1998, 1999, 2001  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12
13 #include <stddef.h>
14 #include <stdlib.h>
15
16 #include <java/lang/Class.h>
17 #include <java/lang/NullPointerException.h>
18 #include <gcj/cni.h>
19 #include <jvm.h>
20
21 #include "unwind.h"
22
23 \f
24 // More nastiness: the GC wants to define TRUE and FALSE.  We don't
25 // need the Java definitions (themselves a hack), so we undefine them.
26 #undef TRUE
27 #undef FALSE
28
29 extern "C"
30 {
31 #include <gc_priv.h>
32 #include <gc_mark.h>
33 #include <include/gc_gcj.h>
34 };
35
36 \f
37 struct alignment_test_struct
38 {
39   char space;
40   char end[0] __attribute__((aligned));
41 };
42
43 struct java_exception_header
44 {
45   /* Cache handler details between Phase 1 and Phase 2.  */
46   _Unwind_Ptr landingPad;
47   int handlerSwitchValue;
48
49   /* The object being thrown.  Compiled code expects this to be immediately
50      before the generic exception header.  Which is complicated by the fact
51      that _Unwind_Exception is ((aligned)).  */
52
53   char pad[sizeof(jthrowable) < sizeof(alignment_test_struct)
54            ? sizeof(alignment_test_struct) - sizeof(jthrowable) : 0]
55     __attribute__((aligned));
56
57   jthrowable value;
58
59   /* The generic exception header.  */
60   _Unwind_Exception unwindHeader;
61 };
62
63 // This is the exception class we report -- "GNUCJAVA".
64 const _Unwind_Exception_Class __gcj_exception_class
65 = ((((((((_Unwind_Exception_Class) 'G' 
66          << 8 | (_Unwind_Exception_Class) 'N')
67         << 8 | (_Unwind_Exception_Class) 'U')
68        << 8 | (_Unwind_Exception_Class) 'C')
69       << 8 | (_Unwind_Exception_Class) 'J')
70      << 8 | (_Unwind_Exception_Class) 'A')
71     << 8 | (_Unwind_Exception_Class) 'V')
72    << 8 | (_Unwind_Exception_Class) 'A');
73
74
75 static inline java_exception_header *
76 get_exception_header_from_ue (_Unwind_Exception *exc)
77 {
78   return reinterpret_cast<java_exception_header *>(exc + 1) - 1;
79 }
80
81 /* Perform a throw, Java style. Throw will unwind through this call,
82    so there better not be any handlers or exception thrown here. */
83
84 extern "C" void
85 _Jv_Throw (jthrowable value)
86 {
87   /* FIXME: Use the proper API to the collector.  */
88   java_exception_header *xh
89     = static_cast<java_exception_header *>(GC_malloc (sizeof (*xh)));
90
91   if (value == NULL)
92     value = new java::lang::NullPointerException ();
93   xh->value = value;
94
95   xh->unwindHeader.exception_class = __gcj_exception_class;
96   xh->unwindHeader.exception_cleanup = NULL;
97
98   /* We're happy with setjmp/longjmp exceptions or region-based
99      exception handlers: entry points are provided here for both.  */
100   _Unwind_Reason_Code code;
101 #ifdef SJLJ_EXCEPTIONS
102   code = _Unwind_SjLj_RaiseException (&xh->unwindHeader);
103 #else
104   code = _Unwind_RaiseException (&xh->unwindHeader);
105 #endif
106
107   /* If code == _URC_END_OF_STACK, then we reached top of stack without
108      finding a handler for the exception.  Since each thread is run in
109      a try/catch, this oughtn't happen.  If code is something else, we
110      encountered some sort of heinous lossage from which we could not
111      recover.  As is the way of such things, almost certainly we will have
112      crashed before now, rather than actually being able to diagnose the
113      problem.  */
114   abort ();
115 }
116
117 \f
118 #include "unwind-pe.h"
119 \f
120 struct lsda_header_info
121 {
122   _Unwind_Ptr Start;
123   _Unwind_Ptr LPStart;
124   const unsigned char *TType;
125   const unsigned char *action_table;
126   unsigned char ttype_encoding;
127   unsigned char call_site_encoding;
128 };
129
130 static const unsigned char *
131 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
132                    lsda_header_info *info)
133 {
134   _Unwind_Ptr tmp;
135   unsigned char lpstart_encoding;
136
137   info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
138
139   // Find @LPStart, the base to which landing pad offsets are relative.
140   lpstart_encoding = *p++;
141   if (lpstart_encoding != DW_EH_PE_omit)
142     p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
143   else
144     info->LPStart = info->Start;
145
146   // Find @TType, the base of the handler and exception spec type data.
147   info->ttype_encoding = *p++;
148   if (info->ttype_encoding != DW_EH_PE_omit)
149     {
150       p = read_uleb128 (p, &tmp);
151       info->TType = p + tmp;
152     }
153   else
154     info->TType = 0;
155
156   // The encoding and length of the call-site table; the action table
157   // immediately follows.
158   info->call_site_encoding = *p++;
159   p = read_uleb128 (p, &tmp);
160   info->action_table = p + tmp;
161
162   return p;
163 }
164
165 static jclass
166 get_ttype_entry (_Unwind_Context *context, lsda_header_info *info, long i)
167 {
168   _Unwind_Ptr ptr;
169
170   i *= size_of_encoded_value (info->ttype_encoding);
171   read_encoded_value (context, info->ttype_encoding, info->TType - i, &ptr);
172
173   return reinterpret_cast<jclass>(ptr);
174 }
175
176
177 // Using a different personality function name causes link failures
178 // when trying to mix code using different exception handling models.
179 #ifdef SJLJ_EXCEPTIONS
180 #define PERSONALITY_FUNCTION    __gcj_personality_sj0
181 #define __builtin_eh_return_data_regno(x) x
182 #else
183 #define PERSONALITY_FUNCTION    __gcj_personality_v0
184 #endif
185
186 extern "C" _Unwind_Reason_Code
187 PERSONALITY_FUNCTION (int version,
188                       _Unwind_Action actions,
189                       _Unwind_Exception_Class exception_class,
190                       struct _Unwind_Exception *ue_header,
191                       struct _Unwind_Context *context)
192 {
193   java_exception_header *xh = get_exception_header_from_ue (ue_header);
194
195   lsda_header_info info;
196   const unsigned char *language_specific_data;
197   const unsigned char *action_record;
198   const unsigned char *p;
199   _Unwind_Ptr landing_pad, ip;
200   int handler_switch_value;
201   bool saw_cleanup;
202   bool saw_handler;
203
204
205   // Interface version check.
206   if (version != 1)
207     return _URC_FATAL_PHASE1_ERROR;
208
209   // Shortcut for phase 2 found handler for domestic exception.
210   if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
211       && exception_class == __gcj_exception_class)
212     {
213       handler_switch_value = xh->handlerSwitchValue;
214       landing_pad = xh->landingPad;
215       goto install_context;
216     }
217
218   // FIXME: In Phase 1, record _Unwind_GetIP in xh->obj as a part of
219   // the stack trace for this exception.  This will only collect Java
220   // frames, but perhaps that is acceptable.
221   // FIXME2: _Unwind_GetIP is nonsensical for SJLJ, being a call-site
222   // index instead of a PC value.  We could perhaps arrange for
223   // _Unwind_GetRegionStart to return context->fc->jbuf[1], which
224   // is the address of the handler label for __builtin_longjmp, but
225   // there is no solution for DONT_USE_BUILTIN_SETJMP.
226
227   language_specific_data = (const unsigned char *)
228     _Unwind_GetLanguageSpecificData (context);
229
230   // If no LSDA, then there are no handlers or cleanups.
231   if (! language_specific_data)
232     return _URC_CONTINUE_UNWIND;
233
234   // Parse the LSDA header.
235   p = parse_lsda_header (context, language_specific_data, &info);
236   ip = _Unwind_GetIP (context) - 1;
237   landing_pad = 0;
238   action_record = 0;
239   handler_switch_value = 0;
240
241 #ifdef SJLJ_EXCEPTIONS
242   // The given "IP" is an index into the call-site table, with two
243   // exceptions -- -1 means no-action, and 0 means terminate.  But
244   // since we're using uleb128 values, we've not got random access
245   // to the array.
246   if ((int) ip <= 0)
247     return _URC_CONTINUE_UNWIND;
248   else
249     {
250       _Unwind_Ptr cs_lp, cs_action;
251       do
252         {
253           p = read_uleb128 (p, &cs_lp);
254           p = read_uleb128 (p, &cs_action);
255         }
256       while (--ip);
257
258       // Can never have null landing pad for sjlj -- that would have
259       // been indicated by a -1 call site index.
260       landing_pad = cs_lp + 1;
261       if (cs_action)
262         action_record = info.action_table + cs_action - 1;
263       goto found_something;
264     }
265 #else
266   // Search the call-site table for the action associated with this IP.
267   while (p < info.action_table)
268     {
269       _Unwind_Ptr cs_start, cs_len, cs_lp, cs_action;
270
271       // Note that all call-site encodings are "absolute" displacements.
272       p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
273       p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
274       p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
275       p = read_uleb128 (p, &cs_action);
276
277       // The table is sorted, so if we've passed the ip, stop.
278       if (ip < info.Start + cs_start)
279         p = info.action_table;
280       else if (ip < info.Start + cs_start + cs_len)
281         {
282           if (cs_lp)
283             landing_pad = info.LPStart + cs_lp;
284           if (cs_action)
285             action_record = info.action_table + cs_action - 1;
286           goto found_something;
287         }
288     }
289 #endif // SJLJ_EXCEPTIONS
290
291   // If ip is not present in the table, C++ would call terminate.
292   // ??? It is perhaps better to tweek the LSDA so that no-action
293   // is mapped to no-entry for Java.
294   return _URC_CONTINUE_UNWIND;
295
296  found_something:
297   saw_cleanup = false;
298   saw_handler = false;
299
300   if (landing_pad == 0)
301     {
302       // If ip is present, and has a null landing pad, there are
303       // no cleanups or handlers to be run.
304     }
305   else if (action_record == 0)
306     {
307       // If ip is present, has a non-null landing pad, and a null
308       // action table offset, then there are only cleanups present.
309       // Cleanups use a zero switch value, as set above.
310       saw_cleanup = true;
311     }
312   else
313     {
314       // Otherwise we have a catch handler.
315       signed long ar_filter, ar_disp;
316
317       while (1)
318         {
319           _Unwind_Ptr tmp;
320
321           p = action_record;
322           p = read_sleb128 (p, &tmp); ar_filter = tmp;
323           read_sleb128 (p, &tmp); ar_disp = tmp;
324
325           if (ar_filter == 0)
326             {
327               // Zero filter values are cleanups.
328               saw_cleanup = true;
329             }
330
331           // During forced unwinding, we only run cleanups.  With a
332           // foreign exception class, we have no class info to match.
333           else if ((actions & _UA_FORCE_UNWIND)
334               || exception_class != __gcj_exception_class)
335             ;
336
337           else if (ar_filter > 0)
338             {
339               // Positive filter values are handlers.
340
341               jclass catch_type = get_ttype_entry (context, &info, ar_filter);
342
343               // The catch_type is either a (java::lang::Class*) or
344               // is one more than a (Utf8Const*).
345               if ((size_t)catch_type & 1)
346                 catch_type = _Jv_FindClass ((Utf8Const*)catch_type - 1, NULL);
347
348               if (_Jv_IsInstanceOf (xh->value, catch_type))
349                 {
350                   handler_switch_value = ar_filter;
351                   saw_handler = true;
352                   break;
353                 }
354             }
355           else
356             {
357               // Negative filter values are exception specifications,
358               // which Java does not use.
359               // ??? Perhaps better to make them an index into a table
360               // of null-terminated strings instead of playing games
361               // with Utf8Const+1 as above.
362               abort ();
363             }
364
365           if (ar_disp == 0)
366             break;
367           action_record = p + ar_disp;
368         }
369     }
370
371   if (! saw_handler && ! saw_cleanup)
372     return _URC_CONTINUE_UNWIND;
373
374   if (actions & _UA_SEARCH_PHASE)
375     {
376       if (! saw_handler)
377         return _URC_CONTINUE_UNWIND;
378
379       // For domestic exceptions, we cache data from phase 1 for phase 2.
380       if (exception_class == __gcj_exception_class)
381         {
382           xh->handlerSwitchValue = handler_switch_value;
383           xh->landingPad = landing_pad;
384         }
385       return _URC_HANDLER_FOUND;
386     }
387
388  install_context:
389   _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
390                  (_Unwind_Ptr) &xh->unwindHeader);
391   _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
392                  handler_switch_value);
393   _Unwind_SetIP (context, landing_pad);
394   return _URC_INSTALL_CONTEXT;
395 }