Skip to content

Throw when Global.SetValue fails instead of ignoring the error - #393

Merged
martindevans merged 1 commit into
bytecodealliance:mainfrom
RaphaelFakhri:fix-global-set-error
Nov 3, 2026
Merged

martindevans merged 1 commit into
bytecodealliance:mainfrom
RaphaelFakhri:fix-global-set-error

Conversation

@RaphaelFakhri

Copy link
Copy Markdown
Contributor

Summary

wasmtime_global_set returns a wasmtime_error_t*, but the P/Invoke in Global.Native declares it as void. When the native call fails, the error is dropped and its allocation leaks, so Global.SetValue and Global.Accessor<T>.SetValue return normally even though the global keeps its old value.

One way to trigger this is to set a funcref global to a Function that belongs to a different Store. Table.SetElement throws a WasmtimeException in that case. Global.SetValue throws nothing and leaves the global unchanged.

Fixes #392.

Changes

  • Declare wasmtime_global_set as returning IntPtr, matching include/wasmtime/global.h.
  • In Global.SetValue and Global.Accessor<T>.SetValue, throw WasmtimeException.FromOwnedError when the call returns an error. Table.SetElement already handles errors the same way.
  • Add tests that set a funcref global to a function from another store through both APIs. The tests expect a WasmtimeException and check that the global still holds a null funcref.

Testing

  • dotnet test tests/Wasmtime.Tests.csproj --filter "FullyQualifiedName~GlobalImportBindingTests": before the fix, the 2 new tests fail with "no exception was thrown". After the fix, all 14 tests pass.
  • dotnet test tests/Wasmtime.Tests.csproj: 479 passed, 2 skipped, 0 failed (Linux x64, net9.0).

`wasmtime_global_set` returns a `wasmtime_error_t*`, but the binding declared it as returning `void`. Errors such as setting a funcref from another store were silently dropped and leaked. `Global.SetValue` and `Global.Accessor<T>.SetValue` now throw a `WasmtimeException`, matching `Table.SetElement`.

@martindevans martindevans left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for investigating and fixing this 👍

@martindevans
martindevans merged commit a69a32a into bytecodealliance:main Nov 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global.SetValue ignores errors returned by wasmtime_global_set

2 participants