dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.11k stars 1.57k forks source link

On macOS in JIT mode, missing `com.apple.security.cs.allow-jit` entitlement crashes with confusing `Exhausted heap space, trying to allocate 32 bytes` error #50719

Closed jmagman closed 1 year ago

jmagman commented 1 year ago

On macOS in JIT mode, when the com.apple.security.cs.allow-jit entitlement is missing, the app crashes on launch with the error Exhausted heap space, trying to allocate 32 bytes.... error: Out of memory..

It would be far better if the crash was instead actionable and informed the developer that they should add that entitlement.

Reported in https://github.com/flutter/flutter/issues/115916, @dubik was testing out macOS add-to-app, so Flutter wasn't able to generate the Debug entitlement file.

2022-11-23 13:49:01.183731+0200 weatherMacOSSwift[41929:261526] Metal API Validation Enabled
Exhausted heap space, trying to allocate 32 bytes.
../../third_party/dart/runtime/vm/object.cc: 2727: error: Out of memory.
version=2.18.4 (stable) (Tue Nov 1 15:15:07 2022 +0000) on "macos_arm64"
pid=41929, thread=259, isolate_group=vm-isolate(0x13388d800), isolate=vm-isolate(0x133892400)
isolate_instructions=1088cf140, vm_instructions=1088cf140
  pc 0x0000000108a2a874 fp 0x000000016d70a5a0 dart::Profiler::DumpStackTrace(void*)+0x90
  pc 0x00000001088cf31c fp 0x000000016d70a5c0 dart::Assert::Fail(char const*, ...) const+0x28
  pc 0x00000001089a2bd0 fp 0x000000016d70a620 dart::Object::Allocate(long, long, dart::Heap::Space, bool)+0x134
  pc 0x00000001089d56b4 fp 0x000000016d70a6b0 dart::Code::FinalizeCode(dart::FlowGraphCompiler*, dart::compiler::Assembler*, dart::Code::PoolAttachment, bool, dart::CodeStatistics*)+0xcc
  pc 0x00000001089d5b18 fp 0x000000016d70a6f0 dart::Code::FinalizeCodeAndNotify(char const*, dart::FlowGraphCompiler*, dart::compiler::Assembler*, dart::Code::PoolAttachment, bool, dart::CodeStatistics*)+0x40
  pc 0x0000000108aa864c fp 0x000000016d70a820 dart::StubCode::Generate(char const*, dart::compiler::ObjectPoolBuilder*, void (*)(dart::compiler::Assembler*))+0xb0
  pc 0x0000000108aa8500 fp 0x000000016d70a8e0 dart::StubCode::Init()+0xa8
  pc 0x000000010891b5f4 fp 0x000000016d70af30 dart::Dart::DartInit(Dart_InitializeParams const*)+0x438
  pc 0x000000010891b894 fp 0x000000016d70af60 dart::Dart::Init(Dart_InitializeParams const*)+0x48
  pc 0x0000000108890154 fp 0x000000016d70b0d0 DartVMInitializer::Initialize(Dart_InitializeParams*, bool, bool)+0x34
  pc 0x000000010888ebb8 fp 0x000000016d70b2f0 flutter::DartVM::DartVM(std::__1::shared_ptr<flutter::DartVMData const>, std::__1::shared_ptr<flutter::IsolateNameServer>)+0x8ac
  pc 0x000000010888e1dc fp 0x000000016d70b960 flutter::DartVM::Create(flutter::Settings, fml::RefPtr<flutter::DartSnapshot const>, fml::RefPtr<flutter::DartSnapshot const>, std::__1::shared_ptr<flutter::IsolateNameServer>)+0xf4
  pc 0x0000000108890cfc fp 0x000000016d70bff0 flutter::DartVMRef::Create(flutter::Settings, fml::RefPtr<flutter::DartSnapshot const>, fml::RefPtr<flutter::DartSnapshot const>)+0x208
  pc 0x00000001086fdaac fp 0x000000016d70cbc0 flutter::Shell::Create(flutter::PlatformData const&, flutter::TaskRunners, flutter::Settings, std::__1::function<std::__1::unique_ptr<flutter::PlatformView, std::__1::default_delete<flutter::PlatformView> > (flutter::Shell&)> const&, std::__1::function<std::__1::unique_ptr<flutter::Rasterizer, std::__1::default_delete<flutter::Rasterizer> > (flutter::Shell&)> const&, bool)+0x124
  pc 0x00000001088b97fc fp 0x000000016d70d270 flutter::EmbedderEngine::LaunchShell()+0x74
  pc 0x00000001088a5274 fp 0x000000016d70d3e0 FlutterEngineRunInitialized+0xd8
  pc 0x0000000108056e98 fp 0x000000016d70d770 -[FlutterEngine runWithEntrypoint:]+0x548
  pc 0x00000001026f2424 fp 0x000000016d70d870 $s17weatherMacOSSwift11AppDelegateC29applicationDidFinishLaunchingyy10Foundation12NotificationVF+0x110
  pc 0x00000001026f2820 fp 0x000000016d70d8f0 $s17weatherMacOSSwift11AppDelegateC29applicationDidFinishLaunchingyy10Foundation12NotificationVFTo+0x8c
  pc 0x000000018eab5468 fp 0x000000016d70d900 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__+0x1c
  pc 0x020c00018eb59bb8 fp 0x000000016d70d920 ___CFXRegistrationPost_block_invoke+0x20c000000000034
  pc 0xac0e80018eb59b24 fp 0x000000016d70d9e0 _CFXRegistrationPost+0xac0e8000000001c8
  pc 0x8f5e80018ea840f8 fp 0x000000016d70e390 _CFXNotificationPost+0x8f5e8000000002dc
  pc 0x375000018f949c28 fp 0x000000016d70e3c0 -[NSNotificationCenter postNotificationName:object:userInfo:]+0x3750000000000060
  pc 0x467c80019162f28c fp 0x000000016d70e420 -[NSApplication _postDidFinishNotification]+0x467c800000000154
  pc 0x5d7680019162efa4 fp 0x000000016d70e450 -[NSApplication _sendFinishLaunchingNotification]+0x5d768000000000ec
  pc 0x5a2200019162c9c8 fp 0x000000016d70e4f0 -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:]+0x5a2200000000024c
  pc 0x7d5c00019162c5a8 fp 0x000000016d70e550 -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:]+0x7d5c00000000030c
  pc 0x7b4c00018f9767d8 fp 0x000000016d70e5a0 -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:]+0x7b4c00000000015c
  pc 0x2c7600018f976610 fp 0x000000016d70e5e0 _NSAppleEventManagerGenericHandler+0x2c76000000000060
  pc 0x1a06800195266ac4 fp 0x000000016d70e8c0 _AppleEventsCheckInAppWithBlock+0x1a06800000003b04
  pc 0xde4a00019526641c fp 0x000000016d70e8f0 _AppleEventsCheckInAppWithBlock+0xde4a00000000345c
  pc 0x784000019525f4b4 fp 0x000000016d70e960 aeProcessAppleEvent+0x78400000000001e8
  pc 0x832480019770f95c fp 0x000000016d70e9a0 AEProcessAppleEvent+0x8324800000000044
  pc 0x4e170001916268f4 fp 0x000000016d70edf0 _DPSNextEvent+0x4e17000000000734
  pc 0x485f800191624e14 fp 0x000000016d70f070 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:]+0x485f800000000530
  pc 0xa853800191616fe0 fp 0x000000016d70f0f0 -[NSApplication run]+0xa853800000000254
  pc 0xc11f0001915e86fc fp 0x000000016d70f160 NSApplicationMain+0xc11f00000000046c
  pc 0xca620001026f2b28 fp 0x000000016d70f180 $sSo21NSApplicationDelegateP6AppKitE4mainyyFZ+0xca62000000000028
  pc 0x00000001026f2af0 fp 0x000000016d70f1b0 $s17weatherMacOSSwift11AppDelegateC5$mainyyFZ+0x2c
  pc 0x00000001026f2bc0 fp 0x000000016d70f1d0 main+0x1c
  pc 0x000000010287108c fp 0x000000016d70f320 Unknown symbol
-- End of DumpStackTrace

Steps to Reproduce (check WeatherApp.zip)

  1. Create flutter application flutter create --template app --platforms macos weather_app
  2. cd weather_app
  3. flutter build macos-framework

Create macOS application using XCode (Swift and storyboard is fine).

  1. Find App.xcframework and FlutterMacOS.xcframework inside weather_app/build/macos/frameworks/Debug, drop them to project settings like so:

image

  1. Replace AppDelegate.swift with this:
import Cocoa
import FlutterMacOS

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    var engine: FlutterEngine?;

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        engine = FlutterEngine.init(name: "Weather", project: nil, allowHeadlessExecution: true)
        engine?.run(withEntrypoint: nil); // <<<--- this is where it crashes

        var channel = FlutterMethodChannel(name: "mychannel", binaryMessenger: engine!.binaryMessenger)
        channel.invokeMethod("makeString", arguments: 123)
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
        return true
    }
}
rmacnak-google commented 1 year ago

https://dart-review.googlesource.com/c/sdk/+/275783

jmagman commented 1 year ago

@rmacnak-google what does the error look like now when the entitlement is missing?