import Darwin import Foundation /// How much main-thread stack a piece of work touches. The unused stack below /// the caller is filled with a pattern first; the bytes no longer holding it /// after the work are what the work used. It runs on the main thread because /// SwiftUI's builders check that they do (a view's body cannot be evaluated /// on a thread of its own), and the test host's main stack (8 MB) is far /// larger than a surface may use, so a surface over its budget fails its /// guard with a number instead of faulting the run — the phone's main stack /// is 1 MB, the simulator's and the Mac's 8 MB, which is why no walk sees it. enum StackHighWater { private static let pattern: UInt8 = 0xA5 /// Left untouched between the fill and the caller's frame, for the /// frames the measuring itself pushes (memset, the call into the work). private static let margin = 8 << 10 /// A block of untouched stack, to compare the filled stack against. private static let clean = [UInt8](repeating: pattern, count: 4096) /// THE PHONE'S MAIN-THREAD STACK, which every budget here is a share of: /// 1 MB on an iPhone. Never the host's — the simulator's and the Mac's /// main stacks are 8 MB, and measuring against those is how builds 227, /// 230 and 231 each passed every walk and then overflowed on the phone /// (`testTheBudgetsAreThePhonesStackNotTheHosts` holds it). static let phoneMainStack = 1 << 20 /// The most one surface's body evaluation may touch, in a debug build (an /// upper bound: release frames are smaller). The phone's main thread has /// 1 MB of stack, and beneath every body SwiftUI's own update frames, /// UIKit's run loop and the screens below on the navigation stack are /// already live; build 227 (the record page's type) and build 230 (the /// list's body, 1.5 MB here) overflowed it. A quarter of it per surface. static let surfaceBudget = phoneMainStack / 4 /// The most the deepest route chain — Home, then a table's list, then a /// record's page — may touch summed, in a debug build: half the phone's /// 1 MB main stack, leaving the other half for the frames beneath /// (SwiftUI's update recursion, UIKit, the run loop). static let chainBudget = phoneMainStack / 2 /// Bytes of stack `work` touched beyond what an empty closure touches. @MainActor static func measure(_ work: () -> Void) -> Int { let empty = touched {} return max(0, touched(work) - empty) } @MainActor @inline(never) private static func touched(_ work: () -> Void) -> Int { precondition(pthread_main_np() != 0, "the measure reads the main thread's stack") let thread = pthread_self() let top = UInt(bitPattern: pthread_get_stackaddr_np(thread)) let bottom = top - UInt(pthread_get_stacksize_np(thread)) var here = 0 let caller = withUnsafeMutablePointer(to: &here) { UInt(bitPattern: $0) } // A page above the bottom, clear of the guard page. let low = bottom + UInt(getpagesize()) * 4 let high = caller - UInt(margin) precondition(high > low, "no stack left below the caller to measure with") let fill = UnsafeMutablePointer(bitPattern: low)! let length = Int(high - low) memset(fill, Int32(pattern), length) work() // Block by block against a clean block, then byte by byte inside the // first block that differs: a byte loop over the ~8 MB below the // caller takes ~50 ms in a debug build, and FaceStackTests measures // thousands of bodies. The same answer, to the byte. var lowest = 0 clean.withUnsafeBufferPointer { clean in while lowest + clean.count <= length, memcmp(fill + lowest, clean.baseAddress!, clean.count) == 0 { lowest += clean.count } } while lowest < length, fill[lowest] == pattern { lowest += 1 } return lowest == length ? 0 : Int(high - low) - lowest + margin } }