Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IKVM NativeAOT support #480

Open
LEI-Hongfaan opened this issue Jan 26, 2024 · 2 comments
Open

IKVM NativeAOT support #480

LEI-Hongfaan opened this issue Jan 26, 2024 · 2 comments

Comments

@LEI-Hongfaan
Copy link

Description

I'm trying to make a HelloWorld console app by using IKVM and Native AOT. The main method is written in C# and invokes a JDK method. However, when I run the app, it throws a System.TypeInitializationException.

Unhandled Exception: System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
   at System.ArgumentNullException.Throw(String) + 0x2b
   at System.IO.Path.Combine(String, String) + 0x23
   at IKVM.Runtime.JVM.Properties.GetHomePath() + 0x157
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode) + 0xb0
   at System.Lazy`1.ExecutionAndPublication(LazyHelper, Boolean) + 0x77
   at System.Lazy`1.CreateValue() + 0xc8
   at IKVM.Runtime.JVM.Internal..cctor() + 0xc9
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xb9
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x14a
   at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
   at IKVM.Runtime.JVM.Init() + 0x1b4
   at IKVM.Runtime.RuntimeInit.Init() + 0x66
   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x39
   at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x36
   at IKVMDemoAppNativeAotLibrary!<BaseAddress>+0x575887

Warnings during AOT compilation:

warning IL3000: IKVMDemoAppNativeAotLibrary.Program
.GetHomePath(): 'System.Reflection.Assembly.Location.get' always returns an empty string for assemblies embedded in a s
ingle-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. [C:\Pro
jects\IKVMDemoApp\IKVMDemoAppNativeAotLibrary\IKVMDemoAppNativeAotLibrary.csproj]
ILC : warning IL3000: IKVM.Runtime.JVM.Properties.GetIkvmProperties(): 'System.Reflection.Assembly.Location.get' always
 returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, cons
ider calling 'System.AppContext.BaseDirectory'. [C:\Projects\IKVMDemoApp\IKVMDemoAppNativeAotLibrary\IKVMDemoAppNativeA
otLibrary.csproj]
ILC : warning IL3000: IKVM.Runtime.JVM.Properties.GetHomePath(): 'System.Reflection.Assembly.Location.get' always retur
ns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider c
alling 'System.AppContext.BaseDirectory'. [C:\Projects\IKVMDemoApp\IKVMDemoAppNativeAotLibrary\IKVMDemoAppNativeAotLibr
ary.csproj]

Dynamic linking has the same outcome as static linking.

Steps to reproduce

  1. Create a C# console app that uses IKVM to call a JDK method. For example:
namespace IKVMDemoApp;

using System = java.lang.System;

public class ConsoleApp {

    public static void main(String[] args) {
        System.@out.println("Hello, World!");
    }
}
  1. Create a C# class library that exports the main function. For example:
namespace IKVMDemoAppNativeAotLibrary {
    public class Exports {

        [UnmanagedCallersOnly(EntryPoint = nameof(main0))]
        public static void main0() {
            var args = Environment.GetCommandLineArgs().Skip(1).ToArray();
            ConsoleApp.main(args);
        }
    }
}
  1. AOT compile the class library to IKVMDemoAppNativeAotLibrary.lib using the dotnet publish command with the nativeaot option.
dotnet publish -c Release -p:NativeLib=Static --use-current-runtime
  1. Create a C/C++ program that consumes the IKVMDemoAppNativeAotLibrary.lib and calls the main0 function.
extern void main0(void);
    
int main(void) {
    main0();
}
  1. Compile and link the C/C++ program with the IKVMDemoAppNativeAotLibrary.lib and other necessary libraries.
bootstrapperdll.obj
Runtime.WorkstationGC.lib
eventpipe-disabled.lib
Runtime.VxsortEnabled.lib
System.Globalization.Native.Aot.lib
System.IO.Compression.Native.Aot.lib
bcrypt.lib
  1. Run the executable and observe the error message.
@wasabii
Copy link
Contributor

wasabii commented Jan 26, 2024

As neat as it would be for NAOT to work on IKVM, it is likely darn near impossible as it stands, and very low priority. I'll leave this open, since it's something to tackle some day..... But that's probably way down the road.

@wasabii wasabii changed the title IKVM and Native AOT HelloWorld app throws System.TypeInitializationException IKVM NativeAOT support Feb 6, 2024
@google-mirror
Copy link

the same as #498

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants