From d11a0311e023cc26acceb4b48468c704c8ba3936 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Wed, 11 Apr 2018 12:31:38 +0100 Subject: [PATCH] Add checks to prevent mixing incompatible ABI versions. --- loader.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/loader.c b/loader.c index 0d651e2..0f905f8 100644 --- a/loader.c +++ b/loader.c @@ -11,6 +11,7 @@ #include #endif #include +#include /** * Runtime lock. This is exposed in @@ -166,6 +167,12 @@ struct objc_init // end: objc_init #include +static enum { + LegacyABI, + NewABI, + UnknownABI +} CurrentABI = UnknownABI; + void registerProtocol(Protocol *proto); void __objc_load(struct objc_init *init) @@ -183,6 +190,19 @@ void __objc_load(struct objc_init *init) } #endif LOCK_RUNTIME_FOR_SCOPE(); + BOOL isFirstLoad = NO; + switch (CurrentABI) + { + case LegacyABI: + fprintf(stderr, "Version 2 Objective-C ABI may not be mixed with earlier versions.\n"); + abort(); + case UnknownABI: + isFirstLoad = YES; + CurrentABI = NewABI; + break; + case NewABI: + break; + } assert(init->version == 0); assert((((uintptr_t)init->sel_end-(uintptr_t)init->sel_begin) % sizeof(*init->sel_begin)) == 0); assert((((uintptr_t)init->cls_end-(uintptr_t)init->cls_begin) % sizeof(*init->cls_begin)) == 0); @@ -211,6 +231,11 @@ void __objc_load(struct objc_init *init) { continue; } + // As a special case, allow using legacy ABI code with a new runtime. + if (isFirstLoad && (strcmp((*cls)->name, "Protocol"))) + { + CurrentABI = UnknownABI; + } objc_load_class(*cls); } #if 0 @@ -260,6 +285,18 @@ void __objc_exec_class(struct objc_module_abi_8 *module) { init_runtime(); + switch (CurrentABI) + { + case UnknownABI: + CurrentABI = LegacyABI; + break; + case LegacyABI: + break; + case NewABI: + fprintf(stderr, "Version 2 Objective-C ABI may not be mixed with earlier versions.\n"); + abort(); + } + // Check that this module uses an ABI version that we recognise. // In future, we should pass the ABI version to the class / category load // functions so that we can change various structures more easily.