Overview
Comment: | runtime: Add exception handling. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | runtime |
Files: | files | file ages | folders |
SHA3-256: |
a230197b486add405a1071296dc40d82 |
User & Date: | js on 2012-07-03 15:44:31 |
Other Links: | branch diff | manifest | tags |
Context
2012-07-03
| ||
15:50 | Merge branch 'runtime' check-in: 4c4fdb3429 user: js tags: trunk | |
15:44 | runtime: Add exception handling. Closed-Leaf check-in: a230197b48 user: js tags: runtime | |
2012-07-01
| ||
20:56 | Reference OFConstantString in OFString. check-in: e810b8ab15 user: js tags: runtime | |
Changes
Modified src/OFObject.m from [f73166965d] to [a5886ec856].
︙ | ︙ | |||
91 92 93 94 95 96 97 | size_t of_num_cpus; #ifdef OF_OBJFW_RUNTIME extern BOOL objc_sync_init(); extern BOOL objc_properties_init(); #endif | | | 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | size_t of_num_cpus; #ifdef OF_OBJFW_RUNTIME extern BOOL objc_sync_init(); extern BOOL objc_properties_init(); #endif #if !defined(OF_APPLE_RUNTIME) || defined(__OBJC2__) static void uncaught_exception_handler(id exception) { fprintf(stderr, "\nUnhandled exception:\n%s\n", [[exception description] UTF8String]); } #endif |
︙ | ︙ | |||
191 192 193 194 195 196 197 | if (!objc_properties_init()) { fputs("Runtime error: objc_properties_init() failed!\n", stderr); abort(); } #endif | | | 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | if (!objc_properties_init()) { fputs("Runtime error: objc_properties_init() failed!\n", stderr); abort(); } #endif #if !defined(OF_APPLE_RUNTIME) || defined(__OBJC2__) objc_setUncaughtExceptionHandler(uncaught_exception_handler); #endif #ifdef HAVE_OBJC_ENUMERATIONMUTATION objc_setEnumerationMutationHandler(enumeration_mutation_handler); #endif |
︙ | ︙ |
Modified src/runtime/Makefile from [22377f6c1a] to [756512ba6a].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | include ../../extra.mk STATIC_PIC_LIB_NOINST = ${RUNTIME_LIB_A} STATIC_LIB_NOINST = ${RUNTIME_A} SRCS = category.m \ class.m \ hashtable.m \ init.m \ lookup.m \ ${LOOKUP_S} \ property.m \ protocol.m \ selector.m \ | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | include ../../extra.mk STATIC_PIC_LIB_NOINST = ${RUNTIME_LIB_A} STATIC_LIB_NOINST = ${RUNTIME_A} SRCS = category.m \ class.m \ exception.m \ hashtable.m \ init.m \ lookup.m \ ${LOOKUP_S} \ property.m \ protocol.m \ selector.m \ |
︙ | ︙ |
Added src/runtime/exception.m version [c1024e0f89].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | /* * Copyright (c) 2008, 2009, 2010, 2011, 2012 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #include <stdlib.h> #import "runtime.h" static const uint64_t objc_exception_class = 0x474E55434F424A43; /* GNUCOBJC */ #define _UA_SEARCH_PHASE 0x01 #define _UA_CLEANUP_PHASE 0x02 #define _UA_HANDLER_FRAME 0x04 #define _UA_FORCE_UNWIND 0x08 #define DW_EH_PE_absptr 0x00 #define DW_EH_PE_uleb128 0x01 #define DW_EH_PE_udata2 0x02 #define DW_EH_PE_udata4 0x03 #define DW_EH_PE_udata8 0x04 #define DW_EH_PE_signed 0x08 #define DW_EH_PE_sleb128 (DW_EH_PE_signed | DW_EH_PE_uleb128) #define DW_EH_PE_sdata2 (DW_EH_PE_signed | DW_EH_PE_udata2) #define DW_EH_PE_sdata4 (DW_EH_PE_signed | DW_EH_PE_udata4) #define DW_EH_PE_sdata8 (DW_EH_PE_signed | DW_EH_PE_udata8) #define DW_EH_PE_pcrel 0x10 #define DW_EH_PE_textrel 0x20 #define DW_EH_PE_datarel 0x30 #define DW_EH_PE_funcrel 0x40 #define DW_EH_PE_aligned 0x50 #define DW_EH_PE_indirect 0x80 #define DW_EH_PE_omit 0xFF #define CLEANUP_FOUND 0x01 #define HANDLER_FOUND 0x02 struct _Unwind_Context; typedef enum { _URC_FATAL_PHASE1_ERROR = 3, _URC_END_OF_STACK = 5, _URC_HANDLER_FOUND = 6, _URC_INSTALL_CONTEXT = 7, _URC_CONTINUE_UNWIND = 8 } _Unwind_Reason_Code; struct objc_exception { struct _Unwind_Exception { uint64_t class; void (*cleanup)(_Unwind_Reason_Code, struct _Unwind_Exception*); /* * The Itanium Exception ABI says to have those and never touch * them. */ uintptr_t private1, private2; } exception; id object; uintptr_t landingpad; intptr_t filter; }; struct lsda { uintptr_t region_start, landingpads_start; uint8_t typestable_enc; const uint8_t *typestable; uintptr_t typestable_base; uint8_t callsites_enc; const uint8_t *callsites, *actiontable; }; extern _Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception*); extern void* _Unwind_GetLanguageSpecificData(struct _Unwind_Context*); extern uint64_t _Unwind_GetRegionStart(struct _Unwind_Context*); extern uint64_t _Unwind_GetDataRelBase(struct _Unwind_Context*); extern uint64_t _Unwind_GetTextRelBase(struct _Unwind_Context*); extern uint64_t _Unwind_GetIP(struct _Unwind_Context*); extern void _Unwind_SetIP(struct _Unwind_Context*, uint64_t); extern void _Unwind_SetGR(struct _Unwind_Context*, int, uint64_t); extern void _Unwind_DeleteException(struct _Unwind_Exception*); static objc_uncaught_exception_handler uncaught_exception_handler; static inline uint64_t read_leb128(const uint8_t **ptr, uint8_t *bits) { uint64_t value = 0; uint8_t shift = 0; do { value |= (**ptr & 0x7F) << shift; (*ptr)++; shift += 7; } while (*(*ptr - 1) & 0x80); if (bits != NULL) *bits = shift; return value; } static uint64_t read_uleb128(const uint8_t **ptr) { return read_leb128(ptr, NULL); } static int64_t read_sleb128(const uint8_t **ptr) { uint8_t bits; int64_t value; value = read_leb128(ptr, &bits); if (bits < 64 && value & (1 << (bits - 1))) value |= -(1 << bits); return value; } static uintptr_t get_base(struct _Unwind_Context *ctx, uint8_t enc) { if (enc == DW_EH_PE_omit) return 0; switch (enc & 0x70) { case DW_EH_PE_absptr: case DW_EH_PE_pcrel: case DW_EH_PE_aligned: return 0; case DW_EH_PE_funcrel: return _Unwind_GetRegionStart(ctx); case DW_EH_PE_datarel: return _Unwind_GetDataRelBase(ctx); case DW_EH_PE_textrel: return _Unwind_GetTextRelBase(ctx); } abort(); } static size_t size_for_encoding(uint8_t enc) { if (enc == DW_EH_PE_omit) return 0; switch (enc & 0x07) { case DW_EH_PE_absptr: return sizeof(void*); case DW_EH_PE_udata2: return 2; case DW_EH_PE_udata4: return 4; case DW_EH_PE_udata8: return 8; } abort(); } static uint64_t read_value(uint8_t enc, const uint8_t **ptr) { uint64_t value; if (enc == DW_EH_PE_aligned) /* Not implemented */ abort(); #define READ_TYPE(type, size) \ { \ value = *(type*)(void*)*ptr; \ *ptr += size; \ break; \ } switch (enc & 0x0F) { case DW_EH_PE_absptr: READ_TYPE(uintptr_t, sizeof(void*)) case DW_EH_PE_uleb128: value = read_uleb128(ptr); break; case DW_EH_PE_udata2: READ_TYPE(uint16_t, 2) case DW_EH_PE_udata4: READ_TYPE(uint32_t, 4) case DW_EH_PE_udata8: READ_TYPE(uint64_t, 8) case DW_EH_PE_sleb128: value = read_sleb128(ptr); break; case DW_EH_PE_sdata2: READ_TYPE(int16_t, 2) case DW_EH_PE_sdata4: READ_TYPE(int32_t, 4) case DW_EH_PE_sdata8: READ_TYPE(int64_t, 8) default: abort(); } #undef READ_TYPE return value; } static uint64_t resolve_value(uint64_t value, uint8_t enc, const uint8_t *start, uint64_t base) { if (value == 0) return 0; value += ((enc & 0x70) == DW_EH_PE_pcrel ? (uintptr_t)start : base); if (enc & DW_EH_PE_indirect) value = *(uint64_t*)value; return value; } static void read_lsda(struct _Unwind_Context *ctx, const uint8_t *ptr, struct lsda *lsda) { uint8_t landingpads_start_enc; uintptr_t callsites_size; lsda->region_start = _Unwind_GetRegionStart(ctx); lsda->landingpads_start = lsda->region_start; lsda->typestable = 0; if ((landingpads_start_enc = *ptr++) != DW_EH_PE_omit) lsda->landingpads_start = read_value(landingpads_start_enc, &ptr); if ((lsda->typestable_enc = *ptr++) != DW_EH_PE_omit) { uintptr_t tmp = read_uleb128(&ptr); lsda->typestable = ptr + tmp; } lsda->typestable_base = get_base(ctx, lsda->typestable_enc); lsda->callsites_enc = *ptr++; callsites_size = read_uleb128(&ptr); lsda->callsites = ptr; lsda->actiontable = lsda->callsites + callsites_size; } static BOOL find_callsite(struct _Unwind_Context *ctx, struct lsda *lsda, uintptr_t *landingpad, const uint8_t **actionrecords) { uintptr_t ip = _Unwind_GetIP(ctx); const uint8_t *ptr; *landingpad = 0; *actionrecords = NULL; ptr = lsda->callsites; while (ptr < lsda->actiontable) { uintptr_t callsite_start, callsite_len, callsite_landingpad; uintptr_t callsite_action; callsite_start = lsda->region_start + read_value(lsda->callsites_enc, &ptr); callsite_len = read_value(lsda->callsites_enc, &ptr); callsite_landingpad = read_value(lsda->callsites_enc, &ptr); callsite_action = read_uleb128(&ptr); /* We can stop if we passed IP, as the table is sorted */ if (callsite_start >= ip) break; if (callsite_start + callsite_len >= ip) { if (callsite_landingpad != 0) *landingpad = lsda->landingpads_start + callsite_landingpad; if (callsite_action != 0) *actionrecords = lsda->actiontable + callsite_action - 1; return YES; } } return NO; } static BOOL class_matches(Class class, id object) { Class iter; if (class == Nil) return YES; if (object == nil) return NO; for (iter = object->isa; iter != Nil; iter = class_getSuperclass(iter)) if (iter == class) return YES; return NO; } static uint8_t find_actionrecord(const uint8_t *actionrecords, struct lsda *lsda, int actions, BOOL foreign, struct objc_exception *e, intptr_t *filtervalue) { uint8_t found = 0; const uint8_t *ptr; intptr_t filter, displacement; do { ptr = actionrecords; filter = read_sleb128(&ptr); /* * Get the next action record. Since read_sleb128 modifies ptr, * we first set the actionrecord to the current ptr and then * add the displacement. */ actionrecords = ptr; displacement = read_sleb128(&ptr); actionrecords += displacement; if (filter > 0 && !(actions & _UA_FORCE_UNWIND) && !foreign) { Class class; uintptr_t i, c; const uint8_t *tmp; i = filter * size_for_encoding(lsda->typestable_enc); tmp = lsda->typestable - i; c = read_value(lsda->typestable_enc, &tmp); c = resolve_value(c, lsda->typestable_enc, lsda->typestable - i, lsda->typestable_base); class = (c != 0 ? objc_get_class((const char*)c) : Nil); if (class_matches(class, e->object)) { *filtervalue = filter; return (found | HANDLER_FOUND); } } else if (filter == 0) found |= CLEANUP_FOUND; else abort(); } while (displacement != 0); return found; } _Unwind_Reason_Code __gnu_objc_personality_v0(int version, int actions, uint64_t ex_class, struct _Unwind_Exception *ex, struct _Unwind_Context *ctx) { struct objc_exception *e = (struct objc_exception*)ex; BOOL foreign = (ex_class != objc_exception_class); const uint8_t *lsda_addr, *actionrecords; struct lsda lsda; uintptr_t landingpad = 0; uint8_t found = 0; intptr_t filter = 0; if (version != 1) return _URC_FATAL_PHASE1_ERROR; if (ctx == NULL) abort(); /* * We already cached everything we found in phase 1, so we only need * to install the context in phase 2. */ if (actions & _UA_HANDLER_FRAME && !foreign) { /* * For handlers, reg #0 must be the exception's object and reg * #1 the filter. */ _Unwind_SetGR(ctx, __builtin_eh_return_data_regno(0), __builtin_extend_pointer(e->object)); _Unwind_SetGR(ctx, __builtin_eh_return_data_regno(1), e->filter); _Unwind_SetIP(ctx, e->landingpad); return _URC_INSTALL_CONTEXT; } /* No LSDA -> nothing to handle */ if ((lsda_addr = _Unwind_GetLanguageSpecificData(ctx)) == NULL) return _URC_CONTINUE_UNWIND; read_lsda(ctx, lsda_addr, &lsda); if (!find_callsite(ctx, &lsda, &landingpad, &actionrecords)) return _URC_CONTINUE_UNWIND; if (landingpad != 0 && actionrecords == NULL) found = CLEANUP_FOUND; else if (landingpad != 0) found = find_actionrecord(actionrecords, &lsda, actions, foreign, e, &filter); if (!found) return _URC_CONTINUE_UNWIND; if (actions & _UA_SEARCH_PHASE) { if (!(found & HANDLER_FOUND)) return _URC_CONTINUE_UNWIND; /* Cache it so we don't have to search it again in phase 2 */ if (!foreign) { e->landingpad = landingpad; e->filter = filter; } return _URC_HANDLER_FOUND; } else if (actions & _UA_CLEANUP_PHASE) { /* For cleanup, reg #0 must be the exception and reg #1 zero */ _Unwind_SetGR(ctx, __builtin_eh_return_data_regno(0), __builtin_extend_pointer(e)); _Unwind_SetGR(ctx, __builtin_eh_return_data_regno(1), 0); _Unwind_SetIP(ctx, landingpad); return _URC_INSTALL_CONTEXT; } abort(); } static void cleanup(_Unwind_Reason_Code reason, struct _Unwind_Exception *ex) { free(ex); } void objc_exception_throw(id object) { struct objc_exception *e; if ((e = malloc(sizeof(*e))) == NULL) abort(); e->exception.class = objc_exception_class; e->exception.cleanup = cleanup; e->exception.private1 = e->exception.private2 = 0; e->object = object; if (_Unwind_RaiseException(&e->exception) == _URC_END_OF_STACK && uncaught_exception_handler != NULL) uncaught_exception_handler(object); abort(); } objc_uncaught_exception_handler objc_setUncaughtExceptionHandler(objc_uncaught_exception_handler handler) { objc_uncaught_exception_handler old = uncaught_exception_handler; uncaught_exception_handler = handler; return old; } |
Modified src/runtime/runtime.h from [97bbb9569f] to [37bafc00ae].
︙ | ︙ | |||
102 103 104 105 106 107 108 109 110 111 112 113 114 115 | }; #define Nil (Class)0 #define nil (id)0 #define YES (BOOL)1 #define NO (BOOL)0 extern SEL sel_registerName(const char*); extern const char* sel_getName(SEL); extern BOOL sel_isEqual(SEL, SEL); extern Class objc_get_class(const char*); extern Class objc_lookup_class(const char*); extern const char* class_getName(Class); extern Class class_getSuperclass(Class); | > > | 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | }; #define Nil (Class)0 #define nil (id)0 #define YES (BOOL)1 #define NO (BOOL)0 typedef void (*objc_uncaught_exception_handler)(id); extern SEL sel_registerName(const char*); extern const char* sel_getName(SEL); extern BOOL sel_isEqual(SEL, SEL); extern Class objc_get_class(const char*); extern Class objc_lookup_class(const char*); extern const char* class_getName(Class); extern Class class_getSuperclass(Class); |
︙ | ︙ | |||
124 125 126 127 128 129 130 131 | extern IMP objc_msg_lookup_super(struct objc_super*, SEL); extern const char* protocol_getName(Protocol*); extern BOOL protocol_isEqual(Protocol*, Protocol*); extern BOOL protocol_conformsToProtocol(Protocol*, Protocol*); extern void objc_thread_add(void); extern void objc_thread_remove(void); extern void objc_exit(void); #endif | > > > | 126 127 128 129 130 131 132 133 134 135 136 | extern IMP objc_msg_lookup_super(struct objc_super*, SEL); extern const char* protocol_getName(Protocol*); extern BOOL protocol_isEqual(Protocol*, Protocol*); extern BOOL protocol_conformsToProtocol(Protocol*, Protocol*); extern void objc_thread_add(void); extern void objc_thread_remove(void); extern void objc_exit(void); extern objc_uncaught_exception_handler objc_setUncaughtExceptionHandler( objc_uncaught_exception_handler); #endif |