Skip to content

WASM build fails to link C setjmp/longjmp with undefined __wasm_setjmp symbols #129266

Description

@thomak-dev

Description

A minimal Microsoft.NET.Sdk.WebAssembly project fails to link when a native C file referenced with NativeFileReference uses <setjmp.h> / setjmp / longjmp.

The project explicitly passes Emscripten's wasm longjmp mode:

<EmccFlags>-sSUPPORT_LONGJMP=wasm</EmccFlags>

A standalone compile with the bundled Emscripten toolchain succeeds for the same C file using -fwasm-exceptions -sSUPPORT_LONGJMP=wasm, but the .NET WebAssembly SDK build fails with unresolved __wasm_setjmp symbols.

Reproduction Steps

Either download JumpTest.zip or:

  1. Create a new WASM browser project:
dotnet new wasmbrowser -n JumpTest
cd JumpTest
  1. Add this to the project file:
<EmccFlags>-sSUPPORT_LONGJMP=wasm</EmccFlags>

and reference the native C file:

<NativeFileReference Include="jump_test.c" />
  1. Add jump_test.c:
#include <setjmp.h>

int setjmp_roundtrip(void)
{
    jmp_buf buffer;
    int result = setjmp(buffer);

    if (result == 0)
        longjmp(buffer, 42);

    return result;
}
  1. Call it from Program.cs:
using System;
using System.Runtime.InteropServices;

Console.WriteLine($"setjmp_roundtrip() = {NativeMethods.SetjmpRoundtrip()}");

internal static partial class NativeMethods
{
    [LibraryImport("__Internal", EntryPoint = "setjmp_roundtrip")]
    internal static partial int SetjmpRoundtrip();
}
  1. Build:
dotnet build

Expected behavior

The project should compile and link successfully, or the SDK should document/report that native setjmp/longjmp is unsupported for this target.

Since the project passes -sSUPPORT_LONGJMP=wasm, Emscripten's wasm SjLj support should be included during linking.

Actual behavior

The build fails during the native link step:

undefined symbol: __wasm_setjmp
undefined symbol: __wasm_setjmp_test

The failure occurs even with:

<EmccFlags>-sSUPPORT_LONGJMP=wasm</EmccFlags>

Allowing undefined symbols lets the build finish, but the produced wasm still imports env.__wasm_setjmp and env.__wasm_setjmp_test, so that is not a valid workaround.

Regression?

Unknown

Known Workarounds

Unknown

Configuration

.NET configuration:

.NET SDK:
 Version:           10.0.301
 Commit:            96856fd726
 Workload version:  10.0.301.1
 MSBuild version:   18.6.4+96856fd72

Host:
 Version:      10.0.9
 Architecture: x64
 Commit:       901ca94124

Installed workloads:
 wasm-tools        10.0.109/10.0.100
 wasm-experimental 10.0.109/10.0.100

Which version of .NET is the code running on?

TargetFramework: net10.0
Microsoft.NETCore.App: 10.0.9
SDK: 10.0.301

OS and version:

Windows 10.0.26200
RID: win-x64

Other information

No response

Metadata

Metadata

Assignees

Labels

arch-wasmWebAssembly architecturearea-Build-mononeeds-author-actionAn issue or pull request that requires more info or actions from the author.os-browserBrowser variant of arch-wasm

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions