Detect if a process runs under Rosetta 2 on Apple Silicon M1 or native. Works for macOS and iOS.
@objc(PSTArchitecture) class Architecture: NSObject { | |
/// Check if process runs under Rosetta 2. | |
/// | |
/// Use to disable tests that use WebKit when running on Apple Silicon | |
/// FB8920323: Crash in WebKit memory allocator on Apple Silicon when iOS below 14 | |
/// Crash is in JavaScriptCore: bmalloc::HeapConstants::HeapConstants(std::__1::lock_guard<bmalloc::Mutex> const&) | |
@objc class var isRosettaEmulated: Bool { | |
// Issue is specific to Simulator, not real devices | |
#if targetEnvironment(simulator) | |
return processIsTranslated() == EMULATED_EXECUTION | |
#else | |
return false | |
#endif | |
} | |
} | |
let NATIVE_EXECUTION = Int32(0) | |
let EMULATED_EXECUTION = Int32(1) | |
let UNKNOWN_EXECUTION = -Int32(1) | |
/// Test if the process runs natively or under Rosetta | |
/// https://developer.apple.com/forums/thread/652667?answerId=618217022&page=1#622923022 | |
private func processIsTranslated() -> Int32 { | |
let key = "sysctl.proc_translated" | |
var ret = Int32(0) | |
var size: Int = 0 | |
sysctlbyname(key, nil, &size, nil, 0) | |
let result = sysctlbyname(key, &ret, &size, nil, 0) | |
if result == -1 { | |
if errno == ENOENT { | |
return 0 | |
} | |
return -1 | |
} | |
return ret | |
} |
This comment has been minimized.
This comment has been minimized.
You have a typo on row 19: |
This comment has been minimized.
This comment has been minimized.
@mladenny Thanks, fixed! |
This comment has been minimized.
This comment has been minimized.
@steipete I'd be most grateful if you could post the full crash dump, perhaps onto pastebin.com, because I am researching Rosetta 2 crashes with WebKit (and Safari) at the moment for my book (iOS Crash Dump Analysis). |
This comment has been minimized.
This comment has been minimized.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
This works around the WebKit/JavaScriptCore memory allocator issue mentioned in https://steipete.com/posts/apple-silicon-m1-a-developer-perspective/
Usage: (Our iOS PDF SDK offers a Reader Mode "liquid mode" to reformat PDF documents for small screens. Formatted text is extracted and displayed via
WKWebView
. This initializes WebKit and doesn't currently work on Apple Silicon when running on iOS < 14.)A typical crash log looks like this: