xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.47k stars 514 forks source link

XPC crashes while using .net7 and/or .net8 #18242

Open JonnHard opened 1 year ago

JonnHard commented 1 year ago

XPC Service does not work on .net7/8 and crashes in runtime with an NSXPCDecoder exception The same code works fine when targeting .net framework 4.7.2

Steps to Reproduce

  1. Build and run the attached sample code. The repro code provides a bare minimum skeleton for running an XPC service using .net7 or .net8
  2. To properly sign the project you might need to make some tweaks, since signing is broken for xpc (see the readme file with instructions)
  3. Run the project
  4. The exception will be thrown and available in the Console app

Expected Behavior

The XPC Service should receive a Ping call from the main app

Actual Behavior

The XPC Service crashes with a runtime exception

<NSXPCConnection: 0x600000a5c000> connection from pid 458 on anonymousListener or serviceListener: Exception caught during decoding of received selector ping, dropping incoming message.
Exception: <NSXPCDecoder: 0x131836200> received a message or reply block that is not in the interface of the remote object (ping), dropping.
(
    0   CoreFoundation                      0x000000019bac5138 __exceptionPreprocess + 240
    1   libobjc.A.dylib                     0x000000019b80fe04 objc_exception_throw + 60
    2   Foundation                          0x000000019cbc84fc -[NSXPCDecoder __decodeXPCObject:allowingSimpleMessageSend:outInvocation:outArguments:outArgumentsMaxCount:outMethodSignature:outSelector:isReply:replySelector:] + 1472
    3   Foundation                          0x000000019cbc7eb0 -[NSXPCDecoder _decodeMessageFromXPCObject:allowingSimpleMessageSend:outInvocation:outArguments:outArgumentsMaxCount:outMethodSignature:outSelector:] + 44
    4   Foundation                          0x000000019c9310a0 -[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:] + 348
    5   Foundation                          0x000000019c8e5b08 message_handler + 228
    6   libxpc.dylib                        0x000000019b6ad168 _xpc_connection_call_event_handler + 152
    7   libxpc.dylib                        0x000000019b6abbb4 _xpc_connection_mach_event + 1380
    8   libdispatch.dylib                   0x000000019b7b4274 _dispatch_client_callout4 + 20
    9   libdispatch.dylib                   0x000000019b7d053c _dispatch_mach_msg_invoke + 464
    10  libdispatch.dylib                   0x000000019b7bb784 _dispatch_lane_serial_drain + 376
    11  libdispatch.dylib                   0x000000019b7d125c _dispatch_mach_invoke + 456
    12  libdispatch.dylib                   0x000000019b7bb784 _dispatch_lane_serial_drain + 376
    13  libdispatch.dylib                   0x000000019b7bc438 _dispatch_lane_invoke + 444
    14  libdispatch.dylib                   0x000000019b7c6c98 _dispatch_workloop_worker_thread + 648
    15  libsystem_pthread.dylib             0x000000019b974360 _pthread_wqthread + 288
    16  libsystem_pthread.dylib             0x000000019b973080 start_wqthread + 8
)

Environment

Version information ``` Visual Studio Community 2022 for Mac Version 17.5.4 (build 8) Runtime .NET 7.0.1 (64-bit) Architecture: Arm64 Microsoft.macOS.Sdk 12.3.2372; git-rev-head:754abbf6a3563f6267e5717ae832b4ac25b1f2fb; git-branch:release/7.0.1xx-xcode13.3 Roslyn (Language Service) 4.5.0-3.23056.2+97881342e427ff5cdcba8f12b12ff8e6f3564431 NuGet Version: 6.4.0.117 .NET SDK (Arm64) SDK: /usr/local/share/dotnet/sdk/7.0.203/Sdks SDK Versions: 7.0.203 6.0.408 MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks .NET Runtime (Arm64) Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 7.0.5 6.0.16 Xamarin.Profiler Version: 1.8.0.49 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler Updater Version: 11 Apple Developer Tools Xcode: 14.2 21534 Build: 14C18 Xamarin.Mac Version: 9.1.1.228 Visual Studio Community Hash: aacedd12c Branch: main Build date: 2023-03-14 19:59:39-0400 Xamarin.iOS Version: 16.4.0.6 Visual Studio Community Hash: 97731c92c Branch: xcode14.3 Build date: 2023-04-11 22:38:36-0400 Xamarin Designer Version: 17.5.3.47 Hash: e8b5d371c3 Branch: remotes/origin/d17-5 Build date: 2023-04-05 15:58:40 UTC Xamarin.Android Not Installed Microsoft Build of OpenJDK Java SDK: Not Found Eclipse Temurin JDK Java SDK: Not Found Android SDK Manager Version: 17.5.0.33 Hash: f0c0c52 Branch: remotes/origin/d17-5~2 Build date: 2023-04-05 15:58:45 UTC Android Device Manager Version: 0.0.0.1245 Hash: 7f8a990 Branch: 7f8a990 Build date: 2023-04-05 15:58:45 UTC Build Information Release ID: 1705040008 Git revision: 9a2f0e1a7e2107e6b1174c241a1ca232cde57c49 Build date: 2023-04-05 15:56:57+00 Build branch: release-17.5 Build lane: release-17.5 Operating System Mac OS X 12.6.5 Darwin 21.6.0 Darwin Kernel Version 21.6.0 Thu Mar 9 20:12:21 PST 2023 root:xnu-8020.240.18.700.8~1/RELEASE_ARM64_T6000 arm64 ```

Example Project

XpcDotNet.zip

JonnHard commented 1 year ago

Extra details While doing a comparison between the ".net framework 4.7" and ".net 7" variants by extracting metadata of the binaries I found some differences between the two

command that was used: objdump --macho --objc-meta-data ./XpcDotNet.Service > macho.objc-meta-data.dump

the .net 7 variant gives

000000010013dbe8 0x100183c38 _OBJC_CLASS_$_XpcServiceBridge
    isa        0x100183c10 _OBJC_METACLASS_$_XpcServiceBridge
    superclass 0x0 _OBJC_CLASS_$_NSObject
    cache      0x0 __objc_empty_cache
    vtable     0x0
    data       0x100140868 __OBJC_CLASS_RO_$_XpcServiceBridge
        flags          0x104 RO_HAS_CXX_STRUCTORS
        instanceStart  8
        instanceSize   32
        reserved       0x00000000
        ivarLayout     0x0
        name           0x10010b674 XpcServiceBridge
        baseMethods    0x1000dc370 __OBJC_$_INSTANCE_METHODS_XpcServiceBridge
            entsize 12 (relative)
            count   11
            name    0xa6178 (0x1001824f0 extends past end of file)
            types   0x3812f (0x1001144ab extends past end of file)
            imp     0xfff56908 (0x100032c88 extends past end of file)
            name    0xa61c4 (0x100182548 extends past end of file)
            types   0x3804d (0x1001143d5 extends past end of file)
            imp     0xfff56928 (0x100032cb4 extends past end of file)
            name    0xa7150 (0x1001834e0 extends past end of file)
            types   0x3811f (0x1001144b3 extends past end of file)
            imp     0xfff56948 (0x100032ce0 extends past end of file)
            name    0xa7154 (0x1001834f0 extends past end of file)
            types   0x3811c (0x1001144bc extends past end of file)
            imp     0xfff56958 (0x100032cfc extends past end of file)
            name    0xa7130 (0x1001834d8 extends past end of file)
            types   0x3811f (0x1001144cb extends past end of file)
            imp     0xfff569ec (0x100032d9c extends past end of file)
            name    0xa7134 (0x1001834e8 extends past end of file)
            types   0x3811b (0x1001144d3 extends past end of file)
            imp     0xfff569fc (0x100032db8 extends past end of file)
            name    0xa5a88 (0x100181e48 extends past end of file)
            types   0x380ab (0x10011446f extends past end of file)
            imp     0xfff56a14 (0x100032ddc extends past end of file)
            name    0xa6064 (0x100182430 extends past end of file)
            types   0x380db (0x1001144ab extends past end of file)
            imp     0xfff56e18 (0x1000331ec extends past end of file)
            name    0xa5598 (0x100181970 extends past end of file)
            types   0x38102 (0x1001144de extends past end of file)
            imp     0xfff56fc0 (0x1000333a0 extends past end of file)
            name    0xa597c (0x100181d60 extends past end of file)
            types   0x37fed (0x1001143d5 extends past end of file)
            imp     0xfff57004 (0x1000333f0 extends past end of file)
            name    0xa5068 (0x100181458 extends past end of file)
            types   0x380b7 (0x1001144ab extends past end of file)
            imp     0xfff57248 (0x100033640 extends past end of file)

and the .net framework 4.7 variant looks like

0000000100507278 0x1005749d0 _OBJC_CLASS_$_XpcServiceBridge
    isa        0x1005749a8 _OBJC_METACLASS_$_XpcServiceBridge
    superclass 0x0 _OBJC_CLASS_$_NSObject
    cache      0x0 __objc_empty_cache
    vtable     0x0
    data       0x100567290 __OBJC_CLASS_RO_$_XpcServiceBridge
        flags          0x104 RO_HAS_CXX_STRUCTORS
        instanceStart  8
        instanceSize   32
        reserved       0x00000000
        ivarLayout     0x0
        name           0x1004708fc XpcServiceBridge
        baseMethods    0x100567110 __OBJC_$_INSTANCE_METHODS_XpcServiceBridge
            entsize 24
            count   11
            name    0x1003f63ed release
            types   0x100470969 v16@0:8
            imp     0x1003e2dc0 -[XpcServiceBridge release]
            name    0x1003f63e6 retain
            types   0x1004709bb @16@0:8
            imp     0x1003e2df0 -[XpcServiceBridge retain]
            name    0x1003f6663 xamarinGetGCHandle
            types   0x10047098b ^v16@0:8
            imp     0x1003e2e20 -[XpcServiceBridge xamarinGetGCHandle]
            name    0x1003f6699 xamarinSetGCHandle:flags:
            types   0x100473916 B28@0:8^v16I24
            imp     0x1003e2e40 -[XpcServiceBridge xamarinSetGCHandle:flags:]
            name    0x1003f6676 xamarinGetFlags
            types   0x100470994 I16@0:8
            imp     0x1003e2ec0 -[XpcServiceBridge xamarinGetFlags]
            name    0x1003f66b3 xamarinSetFlags:
            types   0x100473925 v20@0:8I16
            imp     0x1003e2ee0 -[XpcServiceBridge xamarinSetFlags:]
            name    0x100407113 listener:shouldAcceptNewConnection:
            types   0x100471481 c32@0:8@16@24
            imp     0x1003e2f00 -[XpcServiceBridge listener:shouldAcceptNewConnection:]
            name    0x100407542 getHelloString:returnBlock:
            types   0x100471ee2 v32@0:8@16@?24
            imp     0x1003e2f50 -[XpcServiceBridge getHelloString:returnBlock:]
            name    0x1003f6d8b conformsToProtocol:
            types   0x100473930 c24@0:8^v16
            imp     0x1003e3360 -[XpcServiceBridge conformsToProtocol:]
            name    0x1003f6493 init
            types   0x1004709bb @16@0:8
            imp     0x1003e33a0 -[XpcServiceBridge init]
            name    0x1003fa26d .cxx_destruct
            types   0x100470969 v16@0:8
            imp     0x1003e3430 -[XpcServiceBridge .cxx_destruct]

not sure if it's expected, but "extends past end of file" could be a good lead

rolfbjarne commented 1 year ago

Could you attach a working test project using .net framework 4.7.2, so that I can compare and see how it's supposed to work?

ghost commented 1 year ago

Hi @JonnHard. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

JonnHard commented 1 year ago

Hi, attaching the 4.7.2 version -- the code itself is the same, the only difference is that configuration is adjusted for 4.7.2

the code is based on https://github.com/wjk/XamarinXpcSample

XpcNetFramework472.zip

rolfbjarne commented 1 year ago

I can reproduce this, but a bit differently:

I get the NSXPCDecoder exception on an M1 machine, for both the 4.7.2 and the .net7 version (and it doesn't matter if I build the .net7 version for arm64 or x64).

Also both projects work on an x64 machine.

JonnHard commented 1 year ago

interesting, In my case the platform is limited to an M1 machine - both projects were built and ran on Apple Silicon