[NSAttributedString initWithHtml] crashing on Apple Silicon

Hi,

[NSAttributedString initWithHTML:] fails when running natively on Apple Silicon, but doesn't fail when running on Intel.

In our app, it crashes with "EXC_BREAKPOINT (code=1, subcode=0x1bade08f4)" on this simple example (a unit test with our app as a host application):
Code Block
-(void)testExample {
    NSString *string = @"a";
    NSData *htmlData = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSAttributedString *htmlString = [[NSAttributedString alloc] initWithHTML: htmlData
options: @{
NSDocumentTypeDocumentOption: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentOption: @(NSUTF8StringEncoding),
        }
        documentAttributes: nil];
    XCTAssertNotNil(htmlString);
}


The last of the stack trace when this happens look like this:

Code Block
* thread #1, name = 'UI', queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x1bade08f4)
    frame #0: 0x00000001bade08f4 JavaScriptCore`void JSC::LinkBuffer::copyCompactAndLinkCode<unsigned int>(JSC::MacroAssembler&, JSC::JITCompilationEffort) + 2784
    frame #1: 0x00000001baddfd14 JavaScriptCore`JSC::LinkBuffer::linkCode(JSC::MacroAssembler&, JSC::JITCompilationEffort) + 60
    frame #2: 0x00000001bb3e08f0 JavaScriptCore`JSC::nativeForGenerator(JSC::VM&, JSC::ThunkFunctionType, JSC::CodeSpecializationKind, JSC::ThunkEntryType) + 1856
    frame #3: 0x00000001bb3cdc80 JavaScriptCore`JSC::JITThunks::ctiStub(JSC::VM&, JSC::MacroAssemblerCodeRef<(***::PtrTag)3497> (*)(JSC::VM&)) + 140
    frame #4: 0x00000001bb3cde50 JavaScriptCore`JSC::JITThunks::ctiInternalFunctionCall(JSC::VM&) + 36
    frame #5: 0x00000001bb6e30b0 JavaScriptCore`JSC::VM::VM(JSC::VM::VMType, JSC::HeapType, ***::RunLoop*, bool*) + 14648
    frame #6: 0x00000001bb6e3db0 JavaScriptCore`JSC::VM::create(JSC::HeapType, ***::RunLoop*) + 60
    frame #7: 0x00000001bf09d81c WebCore`WebCore::PageScriptDebugServer::PageScriptDebugServer(WebCore::Page&) + 72
    frame #8: 0x00000001bf07fb98 WebCore`WebCore::InspectorController::InspectorController(WebCore::Page&, WebCore::InspectorClient*) + 436
    frame #9: 0x00000001bf266718 WebCore`WebCore::Page::Page(WebCore::PageConfiguration&&) + 360
    frame #10: 0x00000001b913ae48 WebKitLegacy`-[WebView(WebPrivate) _commonInitializationWithFrameName:groupName:] + 2752
    frame #11: 0x00000001b92547b8 WebKitLegacy`-[WebView(WebPrivate) _initWithFrame:frameName:groupName:] + 116
    frame #12: 0x00000001a8f03568 UIFoundation`-[NSHTMLReader _loadUsingWebKit] + 824
    frame #13: 0x00000001a8f02eec UIFoundation`-[NSHTMLReader attributedString] + 32
    frame #14: 0x00000001a8eee0b8 UIFoundation`_NSReadAttributedStringFromURLOrData + 7072
    frame #15: 0x00000001a8eec48c UIFoundation`-[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 156
  * frame #16: 0x0000000113e4e860 Tests`-[CrashTests testExample](self=0x0000600002ba5e00, _cmd="testExample") at CrashTests.m:45:38


I am not seeing anything obvious we're doing wrong, or that there should be problems calling [NSAttributedString initWithHTML:] on Apple Silicon. I saw a mention somewhere that it has to happen on the main thread, and this is what we're doing, so I'm not sure why it would crash.

Any help wold be appreciated.
A potential workaround is the more modern (asynchronous, out-of-process) API described at https://developer.apple.com/documentation/foundation/nsattributedstring/3182824-loadfromhtmlwithdata?language=objc

(You should also file a feedback assistant ticket about your problem with a reproducing sample project).
Additional info: Inspired by this thread, I tried adding the JIT entitlement to our app, and then the problem goes away. It no longer crashes on Apple Silicon.

But should it really be necessary to add the JIT entitlement to use [NSAttributedString initWithHTML:]? And why only on Apple Silicon?
Thanks for the suggestion about the new API. Unfortunately, we still need to support macOS 10.13 and 10.14, where that API is not available (as far as I can see).

I have been trying to replicate the problem in a clean project, but no luck so far.

Unfortunately, we still need to support macOS 10.13 and 10.14

Can’t you use the new API where it’s available and the old one otherwise? That seems like the most forward-looking solution here.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[NSAttributedString initWithHtml] crashing on Apple Silicon
 
 
Q