MichalStrehovsky / zerosharp

Demo of the potential of C# for systems programming with the .NET native ahead-of-time compilation technology.
2k stars 103 forks source link

Adding a reference to Microsoft.DotNet.ILCompiler 8.0 preview to no-runtime project results in no console output #44

Closed Michael-Kelley closed 1 year ago

Michael-Kelley commented 1 year ago

I want to use the preview version of NativeAot in order to make use of the new UnmanagedEntryPointsAssembly MSBuild property, but this results in no console output when invoking WriteConsoleW.


Reproduction steps:


Expected result: Console should display Hello world!.


Observed result: Console displays no text and no errors.

Wenveo commented 1 year ago

I had a similar issue today when compiling with ILC, but it seems to be related to strings. I also couldn't display the content and title correctly when I tried MessageBoxW.

I later removed the --dehydrate argument and the string output worked.

image image

Or add the prop <IlcDehydrate>false</IlcDehydrate> to your project. Hope this useful to you.😄

Michael-Kelley commented 1 year ago

That did the trick. Thanks! I assume dehydration affects the memory layout of the string and that this is what is causing it to fail with dehydration enabled?

Wenveo commented 1 year ago

I assume dehydration affects the memory layout of the string and that this is what is causing it to fail with dehydration enabled?

Because the dehydrated data is only supported at runtime.

When "Dehydrate" is enabled (which is enabled by default in .NET 8 Aot), the runtime data structures are dehydrate encoded and later dehydrate decoded in Bootstrap/main.cpp (and this StartupCodeHelpers.cs -> RehydrateData).

If you remove the runtime of your app, it will not be able to decode the dehydrated data at startup...

See more:

Wenveo commented 1 year ago

When "Dehydrate" is disabled, the raw data can be found in the pointed address.

image

But if "Dehydrate" is enabled, you won't be able to find the raw data at the pointer address.

image

But it will exist in another place.

image

Only when "runtime" is left in the program will the dehydrated data be decoded at startup to the pointer address it originally pointed to. 🤔