Recent

Author Topic: This simple app crashes on Apple M1  (Read 4853 times)

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 316
This simple app crashes on Apple M1
« on: December 08, 2020, 03:24:26 pm »
This simpliest test app (for aarch64) crashes on Mac with Apple M1.

Can you help? Initially this problem occured in my main app with Metal graphics. Now I reproduced it with a simple test app (36 lines).

FPC 3.2.1 fixes + Lazarus 2.0.10 on Intel Mac Big Sur + Xcode 12.2.

This test app runs fine on Apple DTK (aarch64 version) and on any Intel Mac (x86-64). And crashes on M1 (aarch64) even with no optimizations (O0).

Code: Pascal  [Select][+][-]
  1. program SimpleApp;
  2.  
  3. {$mode objfpc}
  4. {$modeswitch objectivec1}
  5.  
  6. uses
  7.   CocoaAll, MacOSAll;
  8.  
  9. type
  10.   TMyWndDelegate = objcclass(NSObject, NSWindowDelegateProtocol)
  11.     function windowShouldClose (sender: id): ObjCBOOL; message 'windowShouldClose:';
  12.   end;
  13.  
  14. var
  15.   App:    NSApplication;
  16.   Wnd:    NSWindow;
  17.   wDeleg: TMyWndDelegate;
  18.  
  19. function TMyWndDelegate.windowShouldClose (sender: id): ObjCBOOL;
  20. begin
  21.   Result:=True;
  22.   App.terminate(sender);
  23. end;
  24.  
  25. begin
  26.   App:=NSApplication.sharedapplication;
  27.   App.finishLaunching;
  28.   Wnd:=NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 10, 10),
  29.       NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask,
  30.       NSBackingStoreBuffered, False);
  31.   wDeleg:=TMyWndDelegate.alloc.init;
  32.   Wnd.setDelegate(wDeleg);
  33.   Wnd.setFrame_display(NSMakeRect(200,200,400,300), True);
  34.   Wnd.makeKeyandOrderFront(nil);
  35.   App.run;
  36. end.

It crashes on creating of NSWindow initWithContentRect_styleMask_backing_defer()

Code: Pascal  [Select][+][-]
  1. Dec  8 05:37:07 Toms-Mac-mini com.apple.xpc.launchd[1]: Coalition Cache Hit: app<application.SimpleApp.2109617.2109623(501)> [14248]
  2. Dec  8 05:37:09 Toms-Mac-mini com.apple.xpc.launchd[1] (application.SimpleApp.2109617.2109623[14271]): Service exited with abnormal code: 217

Full source code is attached.

If you try to replace all StyleMask parameters in initWithContentRect_styleMask_backing_defer() with 0 (zero), this function will work. But then app crashes on this line:
device:=MTLCreateSystemDefaultDevice;  (we initialize Metal graphics)

UPD.
This problem doesn't appear with fpc 3.3.1 trunk.
« Last Edit: December 09, 2020, 04:07:01 pm by Igor Kokarev »

ChrisR

  • Full Member
  • ***
  • Posts: 187
Re: This simple app crashes on Apple M1
« Reply #1 on: December 08, 2020, 06:55:14 pm »
Runs fine on my M1 - I compile using Free Pascal Compiler version 3.3.1 [2020/12/01] for aarch64.


It also compiles and runs fine using FPC-LLVM

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 316
Re: This simple app crashes on Apple M1
« Reply #2 on: December 08, 2020, 07:08:40 pm »
Chris, thanks for checking!
I didn't try FPC 3.3.1 yet. Also I signed, hardened (no entitlements), notarized my app before send to a person with M1 Mac.

P.S. Please make sure that you compiled aarch64 version, because Intel code works fine with Rosetta.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1277
  • Former Delphi 1-7, 10.2 User
Re: This simple app crashes on Apple M1
« Reply #3 on: December 08, 2020, 09:19:23 pm »
Compiles fine with FPC 3.3.1 per signature below and runs without crashing (M1 Mac mini 8/256).

simpleapp: Mach-O 64-bit executable arm64
« Last Edit: December 08, 2020, 11:20:37 pm by trev »
Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r64455 3.3.1 r48688  macOS 11.2 aarch64 Xcode 12.4
Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 3.2.0 Win10 Parallels VM

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2562
    • havefunsoft.com
Re: This simple app crashes on Apple M1
« Reply #4 on: December 08, 2020, 10:12:04 pm »
sounds like an issue with fpc 3.2.1 fixes
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 316
Re: This simple app crashes on Apple M1
« Reply #5 on: December 09, 2020, 05:34:45 am »
Should I migrate to fpc 3.3.1 to avoid this issue? With optimizations O0?

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 316
Re: This simple app crashes on Apple M1
« Reply #6 on: December 09, 2020, 04:04:41 pm »
I re-compiled my apps with fpc 3.3.1 trunk and boths apps - test project and the main app were launched.

An user reported about unstable work of our main program in aarch64 code. I used O1 optimizations. I'll try with O0 now.
« Last Edit: December 09, 2020, 04:12:30 pm by Igor Kokarev »

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 316
Re: This simple app crashes on Apple M1
« Reply #7 on: December 09, 2020, 05:17:13 pm »
With no optimizations (O0) my main app still works unstable.
I'm concerned, because I didn't expect that M1 will bring so much problems.

aarch64 version worked fine on Apple DTK and x86_64 version with Rosetta works fine on M1.

It seems that Apple uses agressive optimizations in M1 chip for native aarch64 apps.

ChrisR

  • Full Member
  • ***
  • Posts: 187
Re: This simple app crashes on Apple M1
« Reply #8 on: December 09, 2020, 09:43:46 pm »
I think you have two approaches:
 1. Isolate the bugs in your application to create minimal demonstrates and provide those as bug reports so the FPC compiler team can address them. Jonas has been very rapid at fixing bugs for unoptimized code. Kit has been steadily fixing bigs that are specific to optimization.
 2. Use Jonas' FPC-LLVM to deploy you applications. This leverages Apple's mature LLVM tuning and is able to extract terrific performance. A fantastic feature of FPC-LLVM is that it runs on identical source code as FPC. Over time, the compiler team can learn some of the tricks of FPC-LLVM by comparing the output of the two compilers from identical code.
  https://wiki.lazarus.freepascal.org/LLVM

My own experience is very different from yours. I found the pretty smooth. The compiler team has been very responsive. The crucial thing is to provide them with simple examples that demonstrate an issue.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 316
Re: This simple app crashes on Apple M1
« Reply #9 on: December 10, 2020, 05:59:08 pm »
Hi Chris,

Today I solved almost all issues with my app (aarch64) on M1 Mac. It seems to work now with fpc 3.3.1 trunk.

And I'm very grateful to FPC team for their work and help!

I submitted one bug-report for fpc 3.2.1 fixes on M1:
https://bugs.freepascal.org/view.php?id=38189

I'll try LLVM a bit later, thanks for the advice

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 316
Re: This simple app crashes on Apple M1
« Reply #10 on: December 18, 2020, 04:33:54 pm »
I found a similar problem - macOS function crashes on Apple M1 when it called from another thread (non-main). I submitted a bug-report for FPC 3.3.1 trunk:

https://bugs.freepascal.org/view.php?id=38230

 

TinyPortal © 2005-2018