dotnet / runtime

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

`StackOverflowException` for WASM app with `tail.` instruction #109245

Open johnW-ret opened 2 days ago

johnW-ret commented 2 days ago

Description

Referencing F# code that utilizes tail calls in a C# WASM app does not perform TCO at runtime and a large enough stack will result in a StackOverflowException.

Reproduction Steps

See johnW-ret/FsDataStackOverflowBug000 reproduction.

  1. mkdir uselessdelete
    cd uselessdelete
    
    dotnet new blazorwasm -o blazor
    dotnet new classlib -o library -lang F#
    
    dotnet new sln
    dotnet sln add blazor
    dotnet sln add classlib
    
    cd blazorwasm
    dotnet add reference ../classlib
  2. add some F# code that should not stack overflow with TCO but would otherwise
  3. call it from the Blazor app
  4. cd ../blazor
    dotnet run -c Release

Expected behavior

(See repro) Weather.razor L31 should not throw a StackOverflowException

Actual behavior

(See repro) Weather.razor L31 throws a StackOverflowException

Regression?

Around 2024-10-03, the exceptions would not be throw on Release configuration but they would on Debug. I am not sure if this was a regression at the time or not but it fixed my issue. Now it happens on all configurations.

Known Workarounds

In theory, I could pass --tailcalls- to fsc to avoid emitting the tail. op, but the tail call is part of a library I am consuming and I do not want to setup a git submodule and rebuild the library.

Configuration

dotnet --list-sdks
6.0.427 [C:\Program Files\dotnet\sdk]
8.0.403 [C:\Program Files\dotnet\sdk]

Other information

No matter whether I pass true or false for BlazorWebAssemblyJiterpreter, the exception is still thrown.

dotnet-policy-service[bot] commented 2 days ago

Tagging subscribers to this area: @steveisok, @lambdageek See info in area-owners.md if you want to be subscribed.

dotnet-policy-service[bot] commented 1 day ago

Tagging subscribers to 'arch-wasm': @lewing See info in area-owners.md if you want to be subscribed.

lambdageek commented 1 day ago

In theory the interpreter should be able to handle tailcalls, but maybe we regressed? or there's some non-obvious interaction with AOT?