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