dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.29k stars 4.74k forks source link

[mono] Reproducible Crash iOS AOT GlobalizationNative_LoadICUData with library mode #97599

Closed usertoroot closed 4 months ago

usertoroot commented 9 months ago

Description

I am not sure if it should be here or on the MAUI so this is posted on both (see https://github.com/dotnet/maui/issues/20197).

iOS app crashes instantly when SayHello is called. I have isolated it to the .ToString() call on the double object.

Here is the stack trace.

image

Reproduction Steps

On the C# side

Program.cs

using System;
using System.Runtime.InteropServices;

namespace ManagedProject
{
    unsafe class Program
    {
        [UnmanagedCallersOnly(EntryPoint = nameof(SayHello))]
        public static int SayHello(int a, int b)
        {
            //return ThisFunctionWorks(a, b);
            return (int)ThisFunctionCrashes(a, b);
        }

        public static int ThisFunctionWorks(int a, int b)
        {
            Console.WriteLine($"sum1({a}, {b})");
            return a + b;
        }

        public static double ThisFunctionCrashes(double a, double b)
        {
            Console.WriteLine($"sum2({a}, {b})"); //<- crash on this line (due to .ToString)
            return (double)(a + b);
        }

        public static void Main(string[] args)
        {

        }
    }
}

ManagedProject.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>    
    <SelfContained>True</SelfContained>
    <NativeLib>Shared</NativeLib>
    <RunAOTCompilation>True</RunAOTCompilation>
    <ForceFullAOT>True</ForceFullAOT>
    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
    <UseInterpreter>False</UseInterpreter>
    <StaticICULinking>True</StaticICULinking>
    <MtouchLink>None</MtouchLink>
  </PropertyGroup>

  <ItemGroup>
    <!-- Preserves the UnmanagedCallersOnly method -->
    <TrimmerRootDescriptor Include="$(MSBuildThisFileDirectory)ILLink.Descriptor.xml" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="System.Globalization" Version="4.3.0" />
  </ItemGroup>
</Project>

ILLink.Descriptor.xml

<linker>
  <assembly fullname="ManagedProject">
    <type fullname="ManagedProject.Program">
      <method name="SayHello" />
    </type>
  </assembly>
</linker>

On the Swift side just a empty SwiftUI project with the following ContentView

import SwiftUI
import Pan

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")

        }
        .padding()
        .onAppear(perform: {
            let c = SayHello(1, 2)
            print("test \(c)")
        })
    }
}

#Preview {
    ContentView()
}

And bridging header Cs-Bridging-Header.h

#ifndef Cs_Bridging_Header_h
#define Cs_Bridging_Header_h
int SayHello(int a, int b);
#endif

Expected behavior

  1. It should not crash
  2. The interpreter should not be used when disabled

Actual behavior

  1. It crashes
  2. It seems to ignore the UseInterpreter flag?

Regression?

No response

Known Workarounds

No response

Configuration

MacOS: Sonoma 14.2.1 iOS Swift Deployment Target: 15.2 Xcode: 15.2 Dotnet 8.0.101 o maui-ios 8.0.3/8.0.100 o maui 8.0.3/8.0.100 o mobile-librarybuilder 8.0.1/8.0.100

Other information

No response

filipnavara commented 9 months ago

StaticICULinking has no effect on iOS. You would either need to initialize the ICU manually, like the Xamarin workload does, or use the InvariantGlobalization mode.

The static library support in NativeAOT on iOS is going to be officially supported only in .NET 9, even if it works to some extent on .NET 8. There will also be the hybrid globalization mode that relies on system APIs instead of ICU.

ghost commented 9 months ago

Tagging subscribers to 'os-ios': @steveisok, @akoeplinger, @kotlarmilos See info in area-owners.md if you want to be subscribed.

Issue Details
### Description I am not sure if it should be here or on the MAUI so this is posted on both (see https://github.com/dotnet/maui/issues/20197). iOS app crashes instantly when `SayHello` is called. I have isolated it to the `.ToString()` call on the double object. Here is the stack trace. ![image](https://github.com/dotnet/runtime/assets/129423944/dd34f23d-43eb-473c-a92b-a1f39aeb01df) ### Reproduction Steps On the C# side `Program.cs` ```C# using System; using System.Runtime.InteropServices; namespace ManagedProject { unsafe class Program { [UnmanagedCallersOnly(EntryPoint = nameof(SayHello))] public static int SayHello(int a, int b) { //return ThisFunctionWorks(a, b); return (int)ThisFunctionCrashes(a, b); } public static int ThisFunctionWorks(int a, int b) { Console.WriteLine($"sum1({a}, {b})"); return a + b; } public static double ThisFunctionCrashes(double a, double b) { Console.WriteLine($"sum2({a}, {b})"); //<- crash on this line (due to .ToString) return (double)(a + b); } public static void Main(string[] args) { } } } ``` `ManagedProject.csproj` ```xml Exe net8.0 True Shared True True True False True None ``` `ILLink.Descriptor.xml` ```xml ``` On the Swift side just a empty SwiftUI project with the following ContentView ```swift import SwiftUI import Pan struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() .onAppear(perform: { let c = SayHello(1, 2) print("test \(c)") }) } } #Preview { ContentView() } ``` And bridging header `Cs-Bridging-Header.h` ```c #ifndef Cs_Bridging_Header_h #define Cs_Bridging_Header_h int SayHello(int a, int b); #endif ``` ### Expected behavior 1. It should not crash 2. The interpreter should not be used when disabled ### Actual behavior 1. It crashes 2. It seems to ignore the UseInterpreter flag? ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration MacOS: Sonoma 14.2.1 iOS Swift Deployment Target: 15.2 Xcode: 15.2 Dotnet 8.0.101 o maui-ios 8.0.3/8.0.100 o maui 8.0.3/8.0.100 o mobile-librarybuilder 8.0.1/8.0.100 ### Other information _No response_
Author: usertoroot
Assignees: -
Labels: `untriaged`, `os-ios`, `needs-area-label`
Milestone: -
steveisok commented 9 months ago

I suspect you're hitting https://github.com/dotnet/runtime/issues/95124

ivanpovazan commented 9 months ago

To avoid confusion regarding the reported issue - The reported sample uses MonoAOT library mode for iOS platforms.

I suspect you're hitting https://github.com/dotnet/runtime/issues/95124

I tried manually including icudt.dat files in the app bundle and that resolved the issue, making this a duplicate of https://github.com/dotnet/runtime/issues/95124

As previously said, the iOS library support is experimental in .NET8 and the plan is to officially support it in .NET9, for both MonoAOT and NativeAOT.

vitek-karas commented 4 months ago

Closing as duplicate of #95124