Skip to content

Exception: "wasm trap: uninitialized element" #97

@ignatz

Description

@ignatz

I'm successfully building a WASM component:

using System.Text;

namespace TrailbaseWorld.wit.exports.trailbase.runtime
{
    public class InitEndpointImpl : IInitEndpoint
    {
        public static IInitEndpoint.InitResult Init()
        {
            throw new Exception("test");
            return new IInitEndpoint.InitResult(httpHandlers: [], jobHandlers: []);
        }
    }
}

namespace ProxyWorld.wit.exports.wasi.http.v0_2_0
{
    using ProxyWorld.wit.imports.wasi.http.v0_2_0;

    public class IncomingHandlerImpl : IIncomingHandler
    {
        public static void Handle(ITypes.IncomingRequest request, ITypes.ResponseOutparam responseOut)
        {
           // ...
        }
    }
}
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
    <UseAppHost>false</UseAppHost>
    <PublishTrimmed>true</PublishTrimmed>
    <InvariantGlobalization>true</InvariantGlobalization>
    <SelfContained>true</SelfContained>

    <!-- <IlcExportUnmanagedEntrypoints>true</IlcExportUnmanagedEntrypoints> -->
  </PropertyGroup>

  <ItemGroup>
    <!-- <PackageReference Update="Microsoft.DotNet.ILCompiler.LLVM" Version="10.0.0-preview.2.25220.1" /> -->
    <PackageReference Include="BytecodeAlliance.Componentize.DotNet.Wasm.SDK" Version="0.7.0-preview00010" />
    <PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).microsoft.dotnet.ilcompiler.llvm" Version="10.0.0-preview.2.25220.1" />
  </ItemGroup>

  <ItemGroup>
    <Wit Include="wit/http.wasm" World="proxy" Registry="ghcr.io/webassembly/wasi/http:0.2.0" />
  </ItemGroup>
</Project>

However, when I call my InitEndpoint, i'm getting the following exception:

Err(Wasmtime(error while executing at wasm backtrace:
    0: 0x52d47f - S_P_CoreLib_System_Runtime_EH__DispatchException
                    at /_/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.wasm.cs:68:0
    1: 0x43f5dd - S_P_CoreLib_System_Runtime_EH__RhpThrowEx
                    at /_/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.wasm.cs:39:0
    2: 0x2d91ac - S_P_CoreLib_Internal_Runtime_CompilerHelpers_ThrowHelpers__ThrowNullReferenceException
                    at /_/src/libraries/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/ThrowHelpers.cs:20:0
    3: 0x2d9403 - S_P_CoreLib_Internal_Runtime_CompilerHelpers_StartupCodeHelpers__InitializeGlobalTablesForModule
                    at /_/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/StartupCodeHelpers.cs:128:0
    4:  0x3f4d5 - S_P_CoreLib_Internal_Runtime_CompilerHelpers_StartupCodeHelpers__InitializeModules
                    at /_/src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/StartupCodeHelpers.cs:39:0
    5:   0x4d94 - .tmpzokCgm!InitializeRuntime()
    6:   0x87b8 - .tmpzokCgm!Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame*)
    7:   0x88c4 - .tmpzokCgm!RhpReversePInvokeAttachOrTrapThread2
    8:  0x3e663 - .tmpzokCgm!RhpReversePInvoke
    9: 0x2168fe - Guest_TrailbaseWorld_wit_exports_trailbase_runtime_InitEndpointInterop__wasmExportInit
                    at /home/sebastian/projects/trailbase/guests/dotnet/obj/Debug/net10.0/wasi-wasm/wit_bindgen/TrailbaseWorld.wit.exports.trailbase.runtime.InitEndpointInterop.cs:13:0

Caused by:
    wasm trap: uninitialized element

When looking at the generated referenced code TrailbaseWorld.wit.exports.trailbase.runtime.InitEndpointInterop.cs:13:, in obj/Release/net10.0/wasi-wasm/wit_bindgen/TrailbaseWorld.wit.exports.trailbase.runtime.InitEndpointInterop.cs, it looks pretty innocent:

// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
// <auto-generated />
#nullable enable

using System.Text;
using System.Runtime.InteropServices;

namespace TrailbaseWorld.wit.exports.trailbase.runtime
{
    public static class InitEndpointInterop {

        [UnmanagedCallersOnly(EntryPoint = "trailbase:runtime/init-endpoint#init")]
        public static unsafe nint wasmExportInit() {

            global::TrailbaseWorld.wit.exports.trailbase.runtime.IInitEndpoint.InitResult ret;
            ret = InitEndpointImpl.Init();

            var ptr = InteropReturnArea.returnArea.AddressOfReturnArea();

            var bufferSize = 12 * (nuint)ret.httpHandlers.Count;
            void* address = NativeMemory.AlignedAlloc(bufferSize, 4);

// ...

In fact, the C# exception seems to be thrown when calling my implementation of IInitEndpoint.Init() but before exercising the implementation itself.

I'm getting the same result for Debug and Release builds. I also couldn't find anyone else experiencing the same issue. Also FWIW, the same host runtime works for WASM components built with rust or JS.

Any thoughts on what I'm doing wrong? Wanted to ask before building a more elaborate reproduction.

Thanks 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions