123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879 |
- /*
- need exception-safe ARC for exception deallocation tests
- TEST_CFLAGS -fobjc-arc-exceptions -framework Foundation
- */
- #include "test.h"
- #include "testroot.i"
- #include <objc/runtime.h>
- #include <objc/objc-exception.h>
- static volatile int state = 0;
- static volatile int dealloced = 0;
- #define BAD 1000000
- #if defined(USE_FOUNDATION)
- #include <Foundation/Foundation.h>
- @interface Super : NSException @end
- @implementation Super
- +(id)exception { return AUTORELEASE([[self alloc] initWithName:@"Super" reason:@"reason" userInfo:nil]); }
- -(void)check { state++; }
- +(void)check { testassert(!"caught class object, not instance"); }
- -(void)dealloc { dealloced++; SUPER_DEALLOC(); }
- @end
- #define FILENAME "nsexc.m"
- #else
- @interface Super : TestRoot @end
- @implementation Super
- +(id)exception { return AUTORELEASE([self new]); }
- -(void)check { state++; }
- +(void)check { testassert(!"caught class object, not instance"); }
- -(void)dealloc { dealloced++; SUPER_DEALLOC(); }
- @end
- #define FILENAME "exc.m"
- #endif
- @interface Sub : Super @end
- @implementation Sub
- @end
- #if TARGET_OS_OSX
- void altHandlerFail(id unused __unused, void *context __unused)
- {
- fail("altHandlerFail called");
- }
- #define ALT_HANDLER(n) \
- void altHandler##n(id unused __unused, void *context) \
- { \
- testassert(context == (void*)&altHandler##n); \
- testassert(state == n); \
- state++; \
- }
- ALT_HANDLER(1)
- ALT_HANDLER(2)
- ALT_HANDLER(3)
- ALT_HANDLER(4)
- ALT_HANDLER(5)
- ALT_HANDLER(6)
- ALT_HANDLER(7)
- static void throwWithAltHandler(void) __attribute__((noinline, used));
- static void throwWithAltHandler(void)
- {
- @try {
- state++;
- uintptr_t token = objc_addExceptionHandler(altHandler3, (void*)altHandler3);
- // state++ inside alt handler
- @throw [Super exception];
- state = BAD;
- objc_removeExceptionHandler(token);
- }
- @catch (Sub *e) {
- state = BAD;
- }
- state = BAD;
- }
- static void throwWithAltHandlerAndRethrow(void) __attribute__((noinline, used));
- static void throwWithAltHandlerAndRethrow(void)
- {
- @try {
- state++;
- uintptr_t token = objc_addExceptionHandler(altHandler3, (void*)altHandler3);
- // state++ inside alt handler
- @throw [Super exception];
- state = BAD;
- objc_removeExceptionHandler(token);
- }
- @catch (...) {
- testassert(state == 4);
- state++;
- @throw;
- }
- state = BAD;
- }
- #endif
- #if __cplusplus
- #include <exception>
- void terminator() {
- succeed(FILENAME);
- }
- #endif
- #define TEST(code) \
- do { \
- testonthread(^{ PUSH_POOL { code } POP_POOL; }); \
- testcollect(); \
- } while (0)
- int main()
- {
- testprintf("try-catch-finally, exception caught exactly\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (Super *e) {
- state++;
- [e check]; // state++
- }
- @finally {
- state++;
- }
- state++;
- }
- @catch (...) {
- state = BAD;
- }
- });
- testassert(state == 6);
- testassert(dealloced == 1);
- testprintf("try-finally, no exception thrown\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- }
- @finally {
- state++;
- }
- state++;
- }
- @catch (...) {
- state = BAD;
- }
- });
- testassert(state == 4);
- testassert(dealloced == 0);
-
-
- testprintf("try-finally, with exception\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @finally {
- state++;
- }
- state = BAD;
- }
- @catch (id e) {
- state++;
- [e check]; // state++
- }
- });
- testassert(state == 5);
- testassert(dealloced == 1);
- testprintf("try-finally, with autorelease pool pop during unwind\n");
- // Popping an autorelease pool during unwind used to deallocate the
- // exception object, but now we retain them while in flight.
- // This use-after-free is undetected without MallocScribble or guardmalloc.
- if (!getenv("MallocScribble") &&
- (!getenv("DYLD_INSERT_LIBRARIES") ||
- !strstr(getenv("DYLD_INSERT_LIBRARIES"), "libgmalloc")))
- {
- testwarn("MallocScribble not set");
- }
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- void *pool2 = objc_autoreleasePoolPush();
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @finally {
- state++;
- objc_autoreleasePoolPop(pool2);
- }
- state = BAD;
- }
- @catch (id e) {
- state++;
- [e check]; // state++
- }
- });
- testassert(state == 5);
- testassert(dealloced == 1);
-
- testprintf("try-catch-finally, no exception\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- }
- @catch (...) {
- state = BAD;
- }
- @finally {
- state++;
- }
- state++;
- } @catch (...) {
- state = BAD;
- }
- });
- testassert(state == 4);
- testassert(dealloced == 0);
-
-
- testprintf("try-catch-finally, exception not caught\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (Sub *e) {
- state = BAD;
- }
- @finally {
- state++;
- }
- state = BAD;
- }
- @catch (id e) {
- state++;
- [e check]; // state++
- }
- });
- testassert(state == 5);
- testassert(dealloced == 1);
-
-
- testprintf("try-catch-finally, exception caught exactly, rethrown\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (Super *e) {
- state++;
- [e check]; // state++
- @throw;
- state = BAD;
- }
- @finally {
- state++;
- }
- state = BAD;
- }
- @catch (id e) {
- state++;
- [e check]; // state++
- }
- });
- testassert(state == 7);
- testassert(dealloced == 1);
-
-
- testprintf("try-catch, no exception\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- }
- @catch (...) {
- state = BAD;
- }
- state++;
- } @catch (...) {
- state = BAD;
- }
- });
- testassert(state == 3);
- testassert(dealloced == 0);
-
- testprintf("try-catch, exception not caught\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (Sub *e) {
- state = BAD;
- }
- state = BAD;
- }
- @catch (id e) {
- state++;
- [e check]; // state++
- }
- });
- testassert(state == 4);
- testassert(dealloced == 1);
-
-
- testprintf("try-catch, exception caught exactly\n");
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (Super *e) {
- state++;
- [e check]; // state++
- }
- state++;
- }
- @catch (...) {
- state = BAD;
- }
- });
- testassert(state == 5);
- testassert(dealloced == 1);
-
-
- testprintf("try-catch, exception caught exactly, rethrown\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (Super *e) {
- state++;
- [e check]; // state++
- @throw;
- state = BAD;
- }
- state = BAD;
- }
- @catch (id e) {
- state++;
- [e check]; // state++
- }
- });
- testassert(state == 6);
- testassert(dealloced == 1);
-
- testprintf("try-catch, exception caught exactly, thrown again explicitly\n");
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (Super *e) {
- state++;
- [e check]; // state++
- @throw e;
- state = BAD;
- }
- state = BAD;
- }
- @catch (id e) {
- state++;
- [e check]; // state++
- }
- });
- testassert(state == 6);
- testassert(dealloced == 1);
-
-
- testprintf("try-catch, default catch, rethrown\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (...) {
- state++;
- @throw;
- state = BAD;
- }
- state = BAD;
- }
- @catch (id e) {
- state++;
- [e check]; // state++
- }
- });
- testassert(state == 5);
- testassert(dealloced == 1);
-
-
- testprintf("try-catch, default catch, rethrown and caught inside nested handler\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (...) {
- state++;
-
- @try {
- state++;
- @throw;
- state = BAD;
- } @catch (Sub *e) {
- state = BAD;
- } @catch (Super *e) {
- state++;
- [e check]; // state++
- } @catch (...) {
- state = BAD;
- } @finally {
- state++;
- }
-
- state++;
- }
- state++;
- }
- @catch (...) {
- state = BAD;
- }
- });
- testassert(state == 9);
- testassert(dealloced == 1);
-
-
- testprintf("try-catch, default catch, rethrown inside nested handler but not caught\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- @try {
- state++;
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (...) {
- state++;
-
- @try {
- state++;
- @throw;
- state = BAD;
- }
- @catch (Sub *e) {
- state = BAD;
- }
- @finally {
- state++;
- }
-
- state = BAD;
- }
- state = BAD;
- }
- @catch (id e) {
- state++;
- [e check]; // state++
- }
- });
- testassert(state == 7);
- testassert(dealloced == 1);
-
-
- #if __cplusplus
- testprintf("C++ try/catch, Objective-C exception superclass\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- try {
- state++;
- try {
- state++;
- try {
- state++;
- @throw [Super exception];
- state = BAD;
- } catch (...) {
- state++;
- throw;
- state = BAD;
- }
- state = BAD;
- } catch (void *e) {
- state = BAD;
- } catch (int e) {
- state = BAD;
- } catch (Sub *e) {
- state = BAD;
- } catch (Super *e) {
- state++;
- [e check]; // state++
- throw;
- } catch (...) {
- state = BAD;
- }
- } catch (id e) {
- state++;
- [e check]; // state++;
- }
- });
- testassert(state == 8);
- testassert(dealloced == 1);
-
-
- testprintf("C++ try/catch, Objective-C exception subclass\n");
-
- TEST({
- state = 0;
- dealloced = 0;
- try {
- state++;
- try {
- state++;
- try {
- state++;
- @throw [Sub exception];
- state = BAD;
- } catch (...) {
- state++;
- throw;
- state = BAD;
- }
- state = BAD;
- } catch (void *e) {
- state = BAD;
- } catch (int e) {
- state = BAD;
- } catch (Super *e) {
- state++;
- [e check]; // state++
- throw;
- } catch (Sub *e) {
- state = BAD;
- } catch (...) {
- state = BAD;
- }
- } catch (id e) {
- state++;
- [e check]; // state++;
- }
- });
- testassert(state == 8);
- testassert(dealloced == 1);
- #endif
-
-
- #if !TARGET_OS_OSX
- // alt handlers are for macOS only
- #else
- {
- // alt handlers
- // run a lot to catch failed unregistration (runtime complains at 1000)
- #define ALT_HANDLER_REPEAT 2000
-
- testprintf("alt handler, no exception\n");
-
- TEST({
- dealloced = 0;
- for (int i = 0; i < ALT_HANDLER_REPEAT; i++) {
- state = 0;
- @try {
- state++;
- @try {
- uintptr_t token = objc_addExceptionHandler(altHandlerFail, 0);
- state++;
- objc_removeExceptionHandler(token);
- }
- @catch (...) {
- state = BAD;
- }
- state++;
- } @catch (...) {
- state = BAD;
- }
- testassert(state == 3);
- }
- });
- testassert(dealloced == 0);
-
-
- testprintf("alt handler, exception thrown through\n");
-
- TEST({
- dealloced = 0;
- for (int i = 0; i < ALT_HANDLER_REPEAT; i++) {
- state = 0;
- @try {
- state++;
- @try {
- state++;
- uintptr_t token = objc_addExceptionHandler(altHandler2, (void*)altHandler2);
- // state++ inside alt handler
- @throw [Super exception];
- state = BAD;
- objc_removeExceptionHandler(token);
- }
- @catch (Sub *e) {
- state = BAD;
- }
- state = BAD;
- }
- @catch (id e) {
- testassert(state == 3);
- state++;
- [e check]; // state++
- }
- testassert(state == 5);
- }
- });
- testassert(dealloced == ALT_HANDLER_REPEAT);
-
-
- testprintf("alt handler, nested\n");
- #if 1
- testwarn("fixme compiler no longer cooperative for local nested?");
- // Nested alt handlers inside the same function require that each
- // catch group have its own landing pad descriptor. The compiler is
- // probably not doing that anymore. For now we assume that the
- // combination of nested exception handlers and alt handlers is
- // rare enough that nobody cares.
- #else
- TEST({
- dealloced = 0;
- for (int i = 0; i < ALT_HANDLER_REPEAT; i++) {
- state = 0;
- @try {
- state++;
- @try {
- state++;
- // same-level handlers called in FIFO order (not stack-like)
- uintptr_t token = objc_addExceptionHandler(altHandler4, (void*)altHandler4);
- // state++ inside alt handler
- uintptr_t token2 = objc_addExceptionHandler(altHandler5, (void*)altHandler5);
- // state++ inside alt handler
- throwWithAltHandler(); // state += 2 inside
- state = BAD;
- objc_removeExceptionHandler(token);
- objc_removeExceptionHandler(token2);
- }
- @catch (id e) {
- testassert(state == 6);
- state++;
- [e check]; // state++;
- }
- state++;
- }
- @catch (...) {
- state = BAD;
- }
- testassert(state == 9);
- }
- });
- testassert(dealloced == ALT_HANDLER_REPEAT);
- #endif
-
- testprintf("alt handler, nested, rethrows in between\n");
- #if 1
- testwarn("fixme compiler no longer cooperative for local nested?");
- // See above.
- #else
- TEST({
- dealloced = 0;
- for (int i = 0; i < ALT_HANDLER_REPEAT; i++) {
- state = 0;
- @try {
- state++;
- @try {
- state++;
- // same-level handlers called in FIFO order (not stack-like)
- uintptr_t token = objc_addExceptionHandler(altHandler5, (void*)altHandler5);
- // state++ inside alt handler
- uintptr_t token2 = objc_addExceptionHandler(altHandler6, (void*)altHandler6);
- // state++ inside alt handler
- throwWithAltHandlerAndRethrow(); // state += 3 inside
- state = BAD;
- objc_removeExceptionHandler(token);
- objc_removeExceptionHandler(token2);
- }
- @catch (...) {
- testassert(state == 7);
- state++;
- @throw;
- }
- state = BAD;
- }
- @catch (id e) {
- testassert(state == 8);
- state++;
- [e check]; // state++
- }
- testassert(state == 10);
- }
- });
- testassert(dealloced == ALT_HANDLER_REPEAT);
- #endif
-
- testprintf("alt handler, exception thrown and caught inside\n");
-
- TEST({
- dealloced = 0;
- for (int i = 0; i < ALT_HANDLER_REPEAT; i++) {
- state = 0;
- @try {
- state++;
- uintptr_t token = objc_addExceptionHandler(altHandlerFail, 0);
- @try {
- state++;
- @throw [Super exception];
- state = BAD;
- }
- @catch (Super *e) {
- state++;
- [e check]; // state++
- }
- state++;
- objc_removeExceptionHandler(token);
- }
- @catch (...) {
- state = BAD;
- }
- testassert(state == 5);
- }
- });
- testassert(dealloced == ALT_HANDLER_REPEAT);
- #if defined(USE_FOUNDATION)
- testprintf("alt handler, rdar://10055775\n");
-
- TEST({
- dealloced = 0;
- for (int i = 0; i < ALT_HANDLER_REPEAT; i++) {
- state = 0;
- @try {
- uintptr_t token = objc_addExceptionHandler(altHandler1, (void*)altHandler1);
- {
- id x = [NSArray array];
- x = [NSArray array];
- }
- state++;
- // state++ inside alt handler
- [Super raise:@"foo" format:@"bar"];
- state = BAD;
- objc_removeExceptionHandler(token);
- } @catch (id e) {
- state++;
- testassert(state == 3);
- }
- testassert(state == 3);
- }
- });
- testassert(dealloced == ALT_HANDLER_REPEAT);
- // defined(USE_FOUNDATION)
- #endif
- }
- // alt handlers
- #endif
- #if __cplusplus
- std::set_terminate(terminator);
- objc_terminate();
- fail("should not have returned from objc_terminate()");
- #else
- succeed(FILENAME);
- #endif
- }
|