Mobile Development 17 min read

iOS Crash Analysis and Mitigation: Principles, Mechanisms, and a Case Study of WebView Container Crash on iOS 15

This article explains the fundamentals of iOS crashes, describes exception types such as Mach and Signal, details stack memory layout and recovery techniques, and walks through several real‑world crash investigations—including WKWebView, ImageIO, and fishhook issues—culminating in practical mitigation steps that reduced crash rates by over 60%.

Beike Product & Technology
Beike Product & Technology
Beike Product & Technology
iOS Crash Analysis and Mitigation: Principles, Mechanisms, and a Case Study of WebView Container Crash on iOS 15

App quality depends on stability, and crashes act as hidden bombs that interrupt business flow, degrade user experience, and increase churn; they can be triggered by business iteration, system upgrades, or unknown runtime conditions that cannot be fully reproduced in offline testing.

Statistics from 2021 show an overall crash rate of 0.293% (Android 0.32%, iOS 0.10%), with iOS top‑three exceptions being NSInvalidArgumentException , NSGenericException , and NSRangeException .

Principle

Crashes originate from two broad categories of exceptions: hardware‑generated (e.g., invalid instruction, illegal address) and software‑generated (e.g., system‑kill, language‑level exceptions, assertions). System‑kill translates to SIGKILL , while language‑level exceptions become SIGABRT .

Synchronous exceptions are further divided into processor‑detected (fault, trap, abort) and programmed exceptions (software interrupts). Faults can often be corrected, traps aid debugging, and aborts report severe errors.

Mach Exceptions and Signals

iOS crash types include Mach exceptions (kernel‑level), Signal exceptions, and language‑level exceptions (Obj‑C/C/C++). Mach exceptions are captured by the kernel and converted to Signals, which are then delivered to user‑space threads via threadsignal . The conversion flow involves mach_exc_server , catch_mach_exception_raise , and handle_ux_exception (see XNU source). Example code snippet:

kern_return_t
handle_ux_exception(thread_t thread, int exception, mach_exception_code_t code, mach_exception_subcode_t subcode)
{
    // ...
    int ux_signal = ux_exception(exception, code, subcode);
    // ...
    if (ux_signal != 0) {
        threadsignal(thread, ux_signal, code, TRUE);
    }
    return KERN_SUCCESS;
}

Call‑Stack Memory Layout

Each iOS process has its own address space containing stack, heap, global, constant, and code regions. The stack holds local variables and is allocated per thread; the heap is for dynamic allocation. Function calls generate stack frames that are linked from high to low addresses, with the frame pointer (FP) and link register (LR) enabling back‑trace.

Stack Recovery

Recovering a stack involves three steps: locating the image via LC_SEGMENT_64 in Mach‑O, finding symbols through LC_SYMTAB , and matching the closest symbol address to the crash address.

Protection, Capture, and Handling

Online protection targets frequent crash patterns (e.g., unrecognized selector, KVO, container, bad access). Simultaneously, crash capture records Mach, Signal, and C++ exceptions in three phases: replace the original handler, pause non‑crash collection threads to fetch other threads' stacks, and restore the original handler. For Mach exceptions, a custom port must be registered and later restored.

Post‑capture remediation follows three steps: gather context (crash logs, user actions, timestamps, API calls), perform root‑cause analysis by reproducing and debugging, and implement a fix.

Case Study 1: iOS 14 WKWebView Crash

Crash occurs when long‑pressing an image in WKWebView, leading to a WebKit::ShareableBitmap call stack. Investigation of WKContentViewInteraction.m and ShareableBitmapCG.cpp points to image rendering as the trigger.

Mitigation: disable the long‑press image preview in the affected business flow.

Case Study 2: iOS 14 ImageIO Crash

Crash originates from CGImageSourceCopyPropertiesAtIndex in ImageIO. The fix replaces the API call or terminates image‑decoding threads on app exit (e.g., via atexit ).

CFDictionaryRef CGImageSourceCopyPropertiesAtIndex(CGImageSourceRef isrc, size_t index, CFDictionaryRef options) Case Study 3: iOS 14.5+ fishhook Crash fishhook’s attempt to rewrite symbols fails because system libraries’ __DATA_CONST sections become read‑only after iOS 14.5, causing a write‑permission crash at indirect_symbol_bindings[i] = cur->rebindings[j].replacement; . The fix aligns the address to page boundaries before calling mprotect . indirect_symbol_bindings[i] = cur->rebindings[j].replacement; Practical Investigation Workflow (iOS 15 WebView Crash) 1. Gather context: crash appears only on iOS 15, on the main thread, with SIGSEGV in CFRelease of a CFURLRef . 2. Disassemble to locate the faulty memory access ( ldr x1, [x19, #0x28] ) and trace back to __CFURLDeallocate where CFRelease(sanitizedString) dereferences an invalid pointer. 3. Examine CFURLRef structure; the _sanitizedString member resides at offset 0x28 and is being released after it has already been freed. 4. Identify that a custom hook on NSURL’s release method introduced the invalid state; removing the hook lines eliminates the crash. 5. Verify that iOS 15’s WebKit now routes loadRequest through WKSecureCodingURLWrapper , which creates an empty URL string that triggers the faulty release path. 6. Conclude that the hook interferes with the new WebKit path, and removing it resolves the issue. Conclusion The article demonstrates a systematic approach to iOS crash analysis—from understanding exception mechanisms and stack layout to practical debugging, protection, and regression testing—showing that targeted mitigation can reduce crash rates by more than 60%, but continuous vigilance is required as OS updates introduce new failure modes. References https://github.com/apple/darwin-xnu https://opensource.apple.com/source/CF/ https://opensource.apple.com/tarballs/WebKit2/

debuggingiOSWebViewmemorycrashsignalMachException
Beike Product & Technology
Written by

Beike Product & Technology

As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.