Objective-C & Swift Runtime Internals
iOS Internals · Mobile Internals track
7 min read
In this module
Dynamic Dispatch
Apple's frameworks are stitched together with Objective-C, and its runtime plays by very different rules than C or C++. A C function call compiles to a direct branch — the target address is baked in at compile time. An Objective-C method call is a message send, resolved on the fly. That late binding is what makes Cocoa and UIKit feel so malleable. It's also what hands attackers a fairly unusual playground.
objc_msgSend: The Central Hub
Every Objective-C method call compiles down to a call into objc_msgSend, almost certainly the busiest function on any Apple platform. Write this:
[myObject doSomething:argument];The compiler quietly turns it into this:
objc_msgSend(myObject, @selector(doSomething:), argument);All method dispatch funnels through this one function. It takes the target object, a selector (the method name as a unique identifier), and the arguments, then races to find the matching implementation and call it.
The ISA Pointer and Method Lookup
The lookup process follows a well-defined chain:
-
Read the ISA pointer. Every Objective-C object opens with an
isapointer to its class structure. That pointer is how the runtime knows what kind of thing it's holding. -
Hit the method cache. Each class keeps a hash table of recently dispatched selectors mapped to their implementation pointers (IMPs). Always checked first, because most calls hit it.
-
Search the method list. Cache miss?
objc_msgSendwalks the class's method list, an array of selector-to-IMP mappings. -
Walk the superclass chain. Still nothing? The runtime climbs the inheritance hierarchy, asking each superclass in turn.
If the climb reaches the root class with no match, the runtime doesn't give up straight away. It offers the class three chances to handle the message before raising an exception. The diagram traces the whole path, forwarding included.
Every stage after the cache is slower than the one before it, which is why the cache exists. For an attacker it also means a fake class only needs a convincing cache: the lookup never looks any further.
objc_msgSend is the central dispatch function of the Objective-C runtime. Every method call passes through it. It reads the ISA pointer to find the class, checks the method cache, then walks the method list and superclass chain. Because dispatch is fully dynamic, flipping an object's ISA pointer redirects every method call on that object.
Security Implications of Dynamic Dispatch
The Objective-C runtime's dynamism creates several exploitation opportunities:
ISA Pointer Corruption
Overwrite an object's ISA pointer with the address of a fake class structure and you own every message that object receives. The fake class can carry a method cache that maps everyday selectors to attacker-chosen function pointers. It was one of the cleanest primitives in iOS exploitation and has been weaponised plenty of times. On modern devices it's much harder. Since the move to arm64, most objects use a non-pointer isa that packs the class pointer together with reference-count and flag bits, and on arm64e (A12 and later) the class pointer inside it is signed with PAC, so a forged isa fails authentication unless you can also forge the signature.
Method Swizzling
Method swizzling lets you swap a method's implementation while the program is running. One call to method_exchangeImplementations and any method becomes whatever you want it to be:
// Swizzling: swap implementations of two methodsMethod original = class_getInstanceMethod( [NSURLSession class], @selector(dataTaskWithURL:));Method swizzled = class_getInstanceMethod( [NSURLSession class], @selector(hookedDataTaskWithURL:));method_exchangeImplementations(original, swizzled);// Now every call to dataTaskWithURL: runs our code insteadFrameworks and debugging tools use swizzling for legitimate reasons. So do iOS hooking libraries — Frida, Cydia Substrate, the lot — used by researchers and the people they're studying alike.
Selector-Based Attacks
Method dispatch is name-based, so anyone who can slip a crafted Objective-C object into a deserialization path can pick which selector gets dispatched. A system service that unarchives untrusted input and then sends it a message hands the runtime a wishlist — and the runtime dutifully looks the selector up and runs whatever it finds.
What About Swift?
Swift classes dispatch differently. A non-@objc method call on a Swift class goes through a vtable in the class metadata (a fixed slot, much like C++), protocol calls go through witness tables, and final or struct methods are called directly. Only members exposed with @objc or dynamic go through objc_msgSend, so swizzling and selector tricks reach them but not ordinary Swift methods. For an attacker, Swift metadata and vtable pointers are corruption targets in their own right, and they too are increasingly PAC-signed on arm64e.
Pointer Authentication Codes (PAC)
Apple shipped PAC (Pointer Authentication Codes) with the A12, riding on the ARMv8.3-A extension. PAC is a hardware-level answer to pointer corruption attacks, including ISA hijacking and the ever-popular return-address overwrite.
The trick is to cram a cryptographic signature into the unused top bits of a 64-bit pointer. There are five keys for five flavours of pointer. PACIA signs; AUTIA verifies:
; Sign a return address before storing it pacia lr, sp ; Sign LR using key A and SP as context
; Verify before returning autia lr, sp ; Authenticate LR; corrupts pointer on failure ret ; Return only if authentication succeededIf verification fails, the hardware deliberately mangles the pointer so the next dereference crashes the process instead of redirecting control flow. PAC raises the cost of exploitation noticeably. Attackers either chase PAC bypass primitives or learn to live within the bounds of authenticated pointers.
PPL, KTRR, and APRR
Beyond PAC, iOS devices have deployed additional hardware-backed mitigations. The three below defined the A12-to-A14 era; newer chips replace or extend them, as the list after them explains.
- PPL (Page Protection Layer): a separate execution context inside the kernel that holds exclusive rights to the page tables. Even with full kernel read/write, an attacker cannot flip a code page to writable or a data page to executable without going through PPL.
- KTRR (Kernel Text Read-only Region): a hardware mechanism that locks kernel code pages as read-only forever after boot. The KTRR control register itself is locked too, so software can't undo it. Kernel patching just stops being a thing.
- APRR (Alternative Permission Remap Register): a CPU register that reinterprets virtual-memory permission bits, letting the system change how perms are read without rewriting page tables.
On current hardware the picture has moved on:
- CTRR (Configurable Text Read-only Region) is the successor to KTRR on later Apple chips, doing the same job of locking kernel text.
- SPTM and TXM (Secure Page Table Monitor and Trusted Execution Monitor) replace PPL on A15, M2 and later chips. They run at a higher privilege than the kernel, so page-table changes and code-signing decisions are made outside XNU altogether.
- Memory Integrity Enforcement (MIE), introduced with the iPhone 17 generation in 2025, adds always-on hardware memory tagging (Arm's Enhanced MTE) to catch memory-corruption bugs as they happen.
These change between chip generations, so check the current Apple Platform Security guide for which ones apply to a given device.
Modern iOS layers PAC (pointer signing, A12+), page-table protection (PPL, now SPTM/TXM on A15/M2+), immutable kernel text (KTRR, then CTRR), and on the newest devices MIE memory tagging. Each one shuts the door on a different exploitation technique, and stacking them forces bypass chains to grow longer and weirder by the year.
These mitigations have rewritten the iOS exploitation landscape. The same dynamic dispatch that makes Objective-C such a joy to write also paints a target on every object. Hardware now pushes back hard against the pointer-corruption tricks that used to make the flexibility profitable.