2005-03-23 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libjava / exception.cc
blob367df3618b90f62a119405259add71df898e3d13
1 // Functions for Exception Support for Java.
3 /* Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include <stddef.h>
14 #include <stdlib.h>
16 #include <java/lang/Class.h>
17 #include <java/lang/NullPointerException.h>
18 #include <gnu/gcj/RawData.h>
19 #include <gcj/cni.h>
20 #include <jvm.h>
22 // unwind-pe.h uses std::abort(), but sometimes we compile libjava
23 // without libstdc++-v3. The following hack forces it to use
24 // stdlib.h's abort().
25 namespace std
27 static __attribute__ ((__noreturn__)) void
28 abort ()
30 ::abort ();
33 #include "unwind.h"
35 struct alignment_test_struct
37 char space;
38 char end[0] __attribute__((aligned));
41 struct java_exception_header
43 /* Cache handler details between Phase 1 and Phase 2. */
44 _Unwind_Ptr landingPad;
45 int handlerSwitchValue;
47 /* The object being thrown. Compiled code expects this to be immediately
48 before the generic exception header. Which is complicated by the fact
49 that _Unwind_Exception is ((aligned)). */
51 char pad[sizeof(jthrowable) < sizeof(alignment_test_struct)
52 ? sizeof(alignment_test_struct) - sizeof(jthrowable) : 0]
53 __attribute__((aligned));
55 jthrowable value;
57 /* The generic exception header. */
58 _Unwind_Exception unwindHeader;
61 // This is the exception class we report -- "GNUCJAVA".
62 const _Unwind_Exception_Class __gcj_exception_class
63 = ((((((((_Unwind_Exception_Class) 'G'
64 << 8 | (_Unwind_Exception_Class) 'N')
65 << 8 | (_Unwind_Exception_Class) 'U')
66 << 8 | (_Unwind_Exception_Class) 'C')
67 << 8 | (_Unwind_Exception_Class) 'J')
68 << 8 | (_Unwind_Exception_Class) 'A')
69 << 8 | (_Unwind_Exception_Class) 'V')
70 << 8 | (_Unwind_Exception_Class) 'A');
73 static inline java_exception_header *
74 get_exception_header_from_ue (_Unwind_Exception *exc)
76 return reinterpret_cast<java_exception_header *>(exc + 1) - 1;
79 /* Perform a throw, Java style. Throw will unwind through this call,
80 so there better not be any handlers or exception thrown here. */
82 extern "C" void
83 _Jv_Throw (jthrowable value)
85 java_exception_header *xh
86 = static_cast<java_exception_header *>(_Jv_AllocRawObj (sizeof (*xh)));
88 if (value == NULL)
89 value = new java::lang::NullPointerException ();
90 xh->value = value;
92 xh->unwindHeader.exception_class = __gcj_exception_class;
93 xh->unwindHeader.exception_cleanup = NULL;
95 /* We're happy with setjmp/longjmp exceptions or region-based
96 exception handlers: entry points are provided here for both. */
97 _Unwind_Reason_Code code;
98 #ifdef SJLJ_EXCEPTIONS
99 code = _Unwind_SjLj_RaiseException (&xh->unwindHeader);
100 #else
101 code = _Unwind_RaiseException (&xh->unwindHeader);
102 #endif
104 /* If code == _URC_END_OF_STACK, then we reached top of stack without
105 finding a handler for the exception. Since each thread is run in
106 a try/catch, this oughtn't happen. If code is something else, we
107 encountered some sort of heinous lossage from which we could not
108 recover. As is the way of such things, almost certainly we will have
109 crashed before now, rather than actually being able to diagnose the
110 problem. */
111 abort();
115 #include "unwind-pe.h"
117 struct lsda_header_info
119 _Unwind_Ptr Start;
120 _Unwind_Ptr LPStart;
121 const unsigned char *TType;
122 const unsigned char *action_table;
123 unsigned char ttype_encoding;
124 unsigned char call_site_encoding;
127 static const unsigned char *
128 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
129 lsda_header_info *info)
131 _Unwind_Word tmp;
132 unsigned char lpstart_encoding;
134 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
136 // Find @LPStart, the base to which landing pad offsets are relative.
137 lpstart_encoding = *p++;
138 if (lpstart_encoding != DW_EH_PE_omit)
139 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
140 else
141 info->LPStart = info->Start;
143 // Find @TType, the base of the handler and exception spec type data.
144 info->ttype_encoding = *p++;
145 if (info->ttype_encoding != DW_EH_PE_omit)
147 p = read_uleb128 (p, &tmp);
148 info->TType = p + tmp;
150 else
151 info->TType = 0;
153 // The encoding and length of the call-site table; the action table
154 // immediately follows.
155 info->call_site_encoding = *p++;
156 p = read_uleb128 (p, &tmp);
157 info->action_table = p + tmp;
159 return p;
162 static void **
163 get_ttype_entry (_Unwind_Context *context, lsda_header_info *info, long i)
165 _Unwind_Ptr ptr;
167 i *= size_of_encoded_value (info->ttype_encoding);
168 read_encoded_value (context, info->ttype_encoding, info->TType - i, &ptr);
170 return reinterpret_cast<void **>(ptr);
174 // Using a different personality function name causes link failures
175 // when trying to mix code using different exception handling models.
176 #ifdef SJLJ_EXCEPTIONS
177 #define PERSONALITY_FUNCTION __gcj_personality_sj0
178 #define __builtin_eh_return_data_regno(x) x
179 #else
180 #define PERSONALITY_FUNCTION __gcj_personality_v0
181 #endif
183 extern "C" _Unwind_Reason_Code
184 PERSONALITY_FUNCTION (int version,
185 _Unwind_Action actions,
186 _Unwind_Exception_Class exception_class,
187 struct _Unwind_Exception *ue_header,
188 struct _Unwind_Context *context)
190 java_exception_header *xh = get_exception_header_from_ue (ue_header);
192 lsda_header_info info;
193 const unsigned char *language_specific_data;
194 const unsigned char *action_record;
195 const unsigned char *p;
196 _Unwind_Ptr landing_pad, ip;
197 int handler_switch_value;
198 bool saw_cleanup;
199 bool saw_handler;
202 // Interface version check.
203 if (version != 1)
204 return _URC_FATAL_PHASE1_ERROR;
206 // Shortcut for phase 2 found handler for domestic exception.
207 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
208 && exception_class == __gcj_exception_class)
210 handler_switch_value = xh->handlerSwitchValue;
211 landing_pad = xh->landingPad;
212 goto install_context;
215 // FIXME: In Phase 1, record _Unwind_GetIP in xh->obj as a part of
216 // the stack trace for this exception. This will only collect Java
217 // frames, but perhaps that is acceptable.
218 // FIXME2: _Unwind_GetIP is nonsensical for SJLJ, being a call-site
219 // index instead of a PC value. We could perhaps arrange for
220 // _Unwind_GetRegionStart to return context->fc->jbuf[1], which
221 // is the address of the handler label for __builtin_longjmp, but
222 // there is no solution for DONT_USE_BUILTIN_SETJMP.
224 language_specific_data = (const unsigned char *)
225 _Unwind_GetLanguageSpecificData (context);
227 // If no LSDA, then there are no handlers or cleanups.
228 if (! language_specific_data)
229 return _URC_CONTINUE_UNWIND;
231 // Parse the LSDA header.
232 p = parse_lsda_header (context, language_specific_data, &info);
233 ip = _Unwind_GetIP (context) - 1;
234 landing_pad = 0;
235 action_record = 0;
236 handler_switch_value = 0;
238 #ifdef SJLJ_EXCEPTIONS
239 // The given "IP" is an index into the call-site table, with two
240 // exceptions -- -1 means no-action, and 0 means terminate. But
241 // since we're using uleb128 values, we've not got random access
242 // to the array.
243 if ((int) ip <= 0)
244 return _URC_CONTINUE_UNWIND;
245 else
247 _Unwind_Word cs_lp, cs_action;
250 p = read_uleb128 (p, &cs_lp);
251 p = read_uleb128 (p, &cs_action);
253 while (--ip);
255 // Can never have null landing pad for sjlj -- that would have
256 // been indicated by a -1 call site index.
257 landing_pad = cs_lp + 1;
258 if (cs_action)
259 action_record = info.action_table + cs_action - 1;
260 goto found_something;
262 #else
263 // Search the call-site table for the action associated with this IP.
264 while (p < info.action_table)
266 _Unwind_Ptr cs_start, cs_len, cs_lp;
267 _Unwind_Word cs_action;
269 // Note that all call-site encodings are "absolute" displacements.
270 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
271 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
272 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
273 p = read_uleb128 (p, &cs_action);
275 // The table is sorted, so if we've passed the ip, stop.
276 if (ip < info.Start + cs_start)
277 p = info.action_table;
278 else if (ip < info.Start + cs_start + cs_len)
280 if (cs_lp)
281 landing_pad = info.LPStart + cs_lp;
282 if (cs_action)
283 action_record = info.action_table + cs_action - 1;
284 goto found_something;
287 #endif // SJLJ_EXCEPTIONS
289 // If ip is not present in the table, C++ would call terminate.
290 // ??? It is perhaps better to tweek the LSDA so that no-action
291 // is mapped to no-entry for Java.
292 return _URC_CONTINUE_UNWIND;
294 found_something:
295 saw_cleanup = false;
296 saw_handler = false;
298 if (landing_pad == 0)
300 // If ip is present, and has a null landing pad, there are
301 // no cleanups or handlers to be run.
303 else if (action_record == 0)
305 // If ip is present, has a non-null landing pad, and a null
306 // action table offset, then there are only cleanups present.
307 // Cleanups use a zero switch value, as set above.
308 saw_cleanup = true;
310 else
312 // Otherwise we have a catch handler.
313 _Unwind_Sword ar_filter, ar_disp;
315 while (1)
317 p = action_record;
318 p = read_sleb128 (p, &ar_filter);
319 read_sleb128 (p, &ar_disp);
321 if (ar_filter == 0)
323 // Zero filter values are cleanups.
324 saw_cleanup = true;
327 // During forced unwinding, we only run cleanups. With a
328 // foreign exception class, we have no class info to match.
329 else if ((actions & _UA_FORCE_UNWIND)
330 || exception_class != __gcj_exception_class)
333 else if (ar_filter > 0)
335 // Positive filter values are handlers.
337 void **catch_word = get_ttype_entry (context, &info, ar_filter);
338 jclass catch_type = (jclass)*catch_word;
340 // FIXME: This line is a kludge to work around exception
341 // handlers written in C++, which don't yet use indirect
342 // dispatch.
343 if (catch_type == *(void **)&java::lang::Class::class$)
344 catch_type = (jclass)catch_word;
346 if (_Jv_IsInstanceOf (xh->value, catch_type))
348 handler_switch_value = ar_filter;
349 saw_handler = true;
350 break;
353 else
355 // Negative filter values are exception specifications,
356 // which Java does not use.
357 // ??? Perhaps better to make them an index into a table
358 // of null-terminated strings instead of playing games
359 // with Utf8Const+1 as above.
360 abort ();
363 if (ar_disp == 0)
364 break;
365 action_record = p + ar_disp;
369 if (! saw_handler && ! saw_cleanup)
370 return _URC_CONTINUE_UNWIND;
372 if (actions & _UA_SEARCH_PHASE)
374 if (! saw_handler)
375 return _URC_CONTINUE_UNWIND;
377 // For domestic exceptions, we cache data from phase 1 for phase 2.
378 if (exception_class == __gcj_exception_class)
380 xh->handlerSwitchValue = handler_switch_value;
381 xh->landingPad = landing_pad;
383 return _URC_HANDLER_FOUND;
386 install_context:
387 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
388 (_Unwind_Ptr) &xh->unwindHeader);
389 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
390 handler_switch_value);
391 _Unwind_SetIP (context, landing_pad);
392 return _URC_INSTALL_CONTEXT;