Skip to content

Instantly share code, notes, and snippets.

View dicej's full-sized avatar

Joel Dice dicej

  • Denver, Colorado
View GitHub Profile
@dicej
dicej / snippets.rs
Last active February 19, 2025 16:34
wasmtime-wit-bindgen factors
thread_local! {
static FINISH_HOST: std::cell::Cell<*mut u8> = std::cell::Cell::new(std::ptr::null_mut());
static FINISH_SPAWNED: std::cell::RefCell<Vec<std::pin::Pin<Box<dyn std::future::Future<Output = wasmtime::component::HostTaskOutput> + Send + Sync + 'static>>>> = std::cell::RefCell::new(Vec::new());
}
fn poll<T, G: for<'a> GetHost<&'a mut T>, F: std::future::Future + ?Sized>(
getter: G,
store: wasmtime::VMStoreRawPtr,
cx: &mut std::task::Context,
future: std::pin::Pin<&mut F>,
@dicej
dicej / concurrent-bindgen-sketch.rs
Last active February 4, 2025 13:16
Sketch of alternative `wasmtime-wit-bindgen` `concurrent-imports` approach based on thread-local storage
// Somewhere in Wasmtime:
thread_local! {
static LOCAL_STORE: Cell<*mut dyn VMStore> = Cell::new(ptr::null_mut());
}
pub struct LocalStore<'a, T> {
// We only use `T` in the `with` method below.
_phatom_type: PhantomData<T>,
@dicej
dicej / 001-current-handler.wit
Last active October 18, 2024 15:31
Async bindgen snippets
interface handler {
use types.{request, response, error-code};
handle: func(
request: request,
) -> result<response, error-code>;
}
@dicej
dicej / snippets.rs
Created October 4, 2024 23:41
wasmtime-wit-bindgen lifetime fun
pub fn for_any<F, R, T>(fun: F) -> F
where
F: FnOnce(StoreContextMut<T>) -> R + 'static,
R: 'static,
{
fun
}
pub fn for_any_future<F, R, T, FF>(fut: FF) -> FF
where
(func $App_SpinHttpWorld_wit_exports_wasi_http_v0_2_0_AppJsonSerializerContext__global__System_Text_Json_Serialization_Metadata_IJsonTypeInfoResolver_GetTypeInfo (;20990;) (type 6) (param i32 i32 i32
i32) (result i32)
(local i32)
block ;; label = @1
block ;; label = @2
block ;; label = @3
local.get 2
i32.const 2670040
i32.eq
br_if 0 (;@3;)

State of WASIp2 on .NET (primarily using NativeAOT-LLVM) according to Joel

Note that I've generally been focusing on NativeAOT-LLVM because that's the only thing that worked with the component model when I started working on .NET, plus it's currently the best option from a performance and binary size perspective. Since then, Pavel has moved Mono and its interpreter forward so it's in much better shape, but I haven't played with it myself.

Status

  • Basic WASIp2 support is upstreamed into runtime and will ship as part of .NET 9.0
    • But there are a couple of bugs, and it's too late to fix them for .NET 9.0, so you probably don't want to use that anyway
  • dotnet/runtimelab#2614 includes all the latest fixes, including Linux and MacOS build fixes, a couple of patches to Microsoft.Extensions used by AspNetCore, etc.
  • Some of that PR needs to be upstreamed to the runtime repo first, then downstreamed to the runtimelab repo; the rest (i.e. the NativeAOT-LLVM parts) ca
@dicej
dicej / log.txt
Created June 27, 2024 22:59
infinite wait in `System.Net.Http.Functional.Tests` on WASI
#0 0x0000fffe0b05764c in S_P_CoreLib_Interop_Sys__LowLevelMonitor_Wait (var0=<optimized out>, var1=<optimized out>) at .tmp8dNCCP.wasm:14100932
#1 0x0000fffe14464950 in S_P_CoreLib_System_Threading_LowLevelMonitor__WaitCore (this=<error reading variable: Cannot access memory at address 0x20e84>)
#2 0x0000fffe0c342a40 in S_P_CoreLib_System_Threading_LowLevelMonitor__Wait (this=<error reading variable: Cannot access memory at address 0x20e84>)
#3 0x0000fffe140e4b48 in S_P_CoreLib_System_Threading_WaitSubsystem_ThreadWaitInfo__Wait (this=<error reading variable: Cannot access memory at address 0x20e84>,
timeoutMilliseconds=<error reading variable: Cannot access memory at address 0x20e84>, interruptible=<error reading variable: Cannot access memory at address 0x20e84>,
isSleep=<error reading variable: Cannot access memory at address 0x20e84>, lockHolder=<error reading variable: Cannot access memory at address 0x20e84>)
#4 0x0000fffe0d7bf2ec in S_P_CoreLib_System_Threading_WaitSubsystem_WaitableObject
@dicej
dicej / log.txt
Last active June 27, 2024 16:25
System.Net.Http functional tests on WASI: backtrace
#0 0x0000fffe0591a948 in S_P_CoreLib_System_Runtime_EH__RhpThrowEx (exception=...)
#1 0x0000fffdfcbe662c in S_P_CoreLib_System_Reflection_Runtime_Assemblies_RuntimeAssemblyInfo__GetRuntimeAssembly (
assemblyRefName=...)
#2 0x0000fffe060785f0 in S_P_CoreLib_System_Reflection_Runtime_General_ReflectionCoreCallbacksImplementation__Load (
this=..., assemblyRef=..., throwOnFileNotFound=<optimized out>)
#3 0x0000fffe05a1d4c4 in S_P_CoreLib_System_Reflection_Assembly__Load (assemblyRef=...)
#4 0x0000fffdfc25e910 in xunit_execution_dotnet_Xunit_Sdk_ReflectionAssemblyInfo___ctor_0 (this=...,
assemblyFileName=...)
#5 0x0000fffdfc10f7fc in Internal_CompilerGenerated__Module___<DynamicInvoke>None<S_P_CoreLib_System_Void__Object> (
var0=<optimized out>, var1=<optimized out>, var2=<optimized out>, var3=<optimized out>, var4=<optimized out>)
@dicej
dicej / log.txt
Created June 26, 2024 17:56
more .net output
$ (cd ~/p/runtimelab/src/libraries/System.Net.Http/tests/FunctionalTests/ && dotnet build System.Net.Http.Functional.Tests.csproj /p:TestNativeAot=true /p:TargetArchitecture=wasm /p:TargetOS=wasi -c Release -f net9.0-wasi) && WASMTIME_BACKTRACE_DETAILS=1 wasmtime run -S http /home/dicej/p/runtimelab/artifacts/bin/System.Net.Http.Functional.Tests/Release/net9.0-wasi/publish/System.Net.Http.Functional.Tests.wasm
Determining projects to restore...
Restored /home/dicej/p/runtimelab/src/tools/illink/src/linker/ref/Mono.Linker.csproj (in 110 ms).
Restored /home/dicej/p/runtimelab/src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/ILCompiler.DependencyAnalysisFramework.csproj (in 114 ms).
Restored /home/dicej/p/runtimelab/src/libraries/System.DirectoryServices.Protocols/ref/System.DirectoryServices.Protocols.csproj (in 110 ms).
Restored /home/dicej/p/runtimelab/src/libraries/Common/tests/StreamConformanceTests/StreamConformanceTests.csproj (in 3 ms).
Restored /home/dicej/p/runtimelab/src/tool
@dicej
dicej / log.txt
Created June 26, 2024 16:19
System.Net.Http.Functional.Tests.wasm output
$ WASMTIME_BACKTRACE_DETAILS=1 wasmtime run -S http /home/dicej/p/runtimelab/artifacts/bin/System.Net.Http.Functional.Tests/Release/net9.0-wasi/publish/System.Net.Http.Functional.Tests.wasm
Using threadless Xunit runner
Unhandled exception. System.IO.FileNotFoundException: Cannot load assembly 'System.Net.Http.Functional, Version=0.0.0.0'. No metadata found for this assembly.
Error: failed to run main module `/home/dicej/p/runtimelab/artifacts/bin/System.Net.Http.Functional.Tests/Release/net9.0-wasi/publish/System.Net.Http.Functional.Tests.wasm`
Caused by:
0: failed to invoke `run` function
1: error while executing at wasm backtrace:
0: 0x2389e53 - abort