Skip to content

c-api: implement wasm_tagtype_t for exception tag types (#10252) - #12763

Merged
alexcrichton merged 5 commits into
bytecodealliance:mainfrom
bacek:bacek/fix-10252
Mar 12, 2026
Merged

alexcrichton merged 5 commits into
bytecodealliance:mainfrom
bacek:bacek/fix-10252

Conversation

@bacek

@bacek bacek commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Part of #10252.

I was hitting todo!() when was playing with ClickHouse WASM UDF, which caused whole ClickHouse to crash. It was only happening with Debug WASM builds and assert firing all over the place.

Implement the C embedder API for WebAssembly exception tags, resolving the long-standing todo!() in CExternType::new() that caused a Rust panic whenever a module with tag exports had its exports enumerated via the C API (e.g. wasm_module_exports / wasmtime_module_exports).

Changes:

  • Add wasm_tagtype_t type with wasm_tagtype_new/params/delete/copy and the standard as_externtype / externtype_as_tagtype cast functions
  • Add WASM_EXTERN_TAG = 4 constant to wasm_externkind_enum in wasm.h
  • Add CExternType::Tag(CTagType) variant; wire into CExternType::new() and wasm_externtype_kind()
  • Add wasmtime/types/tag.hh C++ wrapper (TagType / TagType::Ref)
  • Extend ExternType::Ref variant and ref_from_c() switch in extern.hh
  • Add TagType tests: construction, module export enumeration (regression for Implement WebAssembly Tags (exceptions + stack-switching) #10252), and wasm_externtype_kind / cast function correctness

…ance#10252)

Implement the C embedder API for WebAssembly exception tags, resolving
the long-standing todo!() in CExternType::new() that caused a Rust panic
whenever a module with tag exports had its exports enumerated via the
C API (e.g. wasm_module_exports / wasmtime_module_exports).

Changes:
- Add wasm_tagtype_t type with wasm_tagtype_new/params/delete/copy and
  the standard as_externtype / externtype_as_tagtype cast functions
- Add WASM_EXTERN_TAG = 4 constant to wasm_externkind_enum in wasm.h
- Add CExternType::Tag(CTagType) variant; wire into CExternType::new()
  and wasm_externtype_kind()
- Add wasmtime/types/tag.hh C++ wrapper (TagType / TagType::Ref)
- Extend ExternType::Ref variant and ref_from_c() switch in extern.hh
- Add TagType tests: construction, module export enumeration (regression
  for bytecodealliance#10252), and wasm_externtype_kind / cast function correctness

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@bacek
bacek requested a review from a team as a code owner March 11, 2026 19:47
@bacek
bacek requested review from alexcrichton and removed request for a team March 11, 2026 19:47
@alexcrichton

Copy link
Copy Markdown
Member

Thanks! There's a number of things that CI is turning up here, but one of the more substantive things is that while wasm.h is vendored in this repository it's not the source of truth, we acquire it from elsewhere. Effectively we cannot change wasm.h, so modifications to that will need to be reverted here.

To handle that, though, it's fine to add functions to a wasmtime-specific header (e.g. a new tag.h or something like that). The functions can have a wasmtime_* specific constant/function associated with them then.

- Revert wasm.h to its vendored upstream state (it must not be modified
  locally; tag support is not yet in the upstream wasm-c-api spec header)
- Add crates/c-api/include/wasmtime/tag.h with wasm_tagtype_t forward
  declaration, WASM_EXTERN_TAG constant, and all wasm_tagtype_* /
  wasm_externtype_as_tagtype* function declarations
- Include wasmtime/tag.h from wasmtime.h

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@bacek

bacek commented Mar 11, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! There's a number of things that CI is turning up here, but one of the more substantive things is that while wasm.h is vendored in this repository it's not the source of truth, we acquire it from elsewhere. Effectively we cannot change wasm.h, so modifications to that will need to be reverted here.

Where is wasm.h coming from?

To handle that, though, it's fine to add functions to a wasmtime-specific header (e.g. a new tag.h or something like that). The functions can have a wasmtime_* specific constant/function associated with them then.

Updated.

@alexcrichton

Copy link
Copy Markdown
Member

The script we use for vendoring is here.

For Wasmtime-specific functions, can you use the wasmtime_* prefix instead of wasm_*? We don't want to clash with any possible future additions to wasm.h. Additionally since we control the signatures we can make the internal implementations easier to work with. For example the constructor can take an engine argument, and instead of returning a list of types this could do something similar to component type accessors (also Wasmtime-specific) and return one-at-a-time through an iterator style pattern.

Drop unused `wasm_tagtype_t` import from extern.rs, and remove the
`force`/`ty`/`wasm_tagtype_t::new` dead-code paths from tag.rs that
were written for future engine-resolution support but are not yet called.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@bacek
bacek force-pushed the bacek/fix-10252 branch from 7448c94 to 02a033c Compare March 11, 2026 20:36
Comment thread crates/c-api/include/wasmtime/tag.h Outdated
Comment on lines +41 to +43
WASM_API_EXTERN wasmtime_tagtype_t *
wasmtime_tagtype_new(wasm_engine_t *engine, const wasm_valkind_t *params,
size_t nparams);

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.

For the signature of this function I'd recommend following the same rough signature of Wasmtime itself. Namely in Wasmtime a tag is constructed from a full FuncType, whereas here all the pieces are provided.

In the original exception-handling proposal tags could only have parameters, but in the stack switching proposal this was relaxed to additionally allow result types too. Given that, and looking forward, I think we'll want to support constructing tags both with and without params/results, so at the very least this constructor needs to grow parameters to communicate the tag's result types. At that point I think it's probably easiest to just take a function type in the constructor.

Comment thread crates/c-api/include/wasmtime/tag.h Outdated
Comment on lines +58 to +64
/**
* \brief Returns the kind of the nth exception payload type.
*
* Returns 0 if `nth` is out of bounds.
*/
WASM_API_EXTERN wasm_valkind_t
wasmtime_tagtype_param(const wasmtime_tagtype_t *, size_t nth);

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.

Here tag parameters are going to want to be communicated with their full value, not just their kind. Similar to above though I'd recommend following the Rust API here which is to just return a function type. This could return either a const wasm_functype_t* which is owned by the tag, or it's probably easier to return an owned wasm_functype_t* which the caller must deallocate.

Comment thread crates/c-api/include/wasmtime/extern.h Outdated
Comment on lines +108 to +110
/// \brief Value of #wasmtime_extern_kind_t meaning that #wasm_externtype_t is a
/// tag (WebAssembly exception-handling proposal)
#define WASMTIME_EXTERN_TAG 5

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.

This is a bit subtle and will need to change. The definitions in this file of WASMTIME_EXTERN_* are discriminants for the wasmtime_extern_t structure below. Eventually that'll want a tag variant as well, in which case this WASMTIME_EXTERN_TAG subtly would serve a double-meaning.

To resolve this I'd recommend moving this #define into tag.h and renaming it perhaps to WASMTIME_EXTERNTYPE_TAG to make it clear that it's for the wasm_externtype_t discriminant rather than wasmtime_extern_t.

Comment thread crates/c-api/src/types/extern.rs Outdated
Comment on lines +40 to +42
/// Value returned by `wasm_externtype_kind` for exception tags.
/// Lives alongside `WASMTIME_EXTERN_SHAREDMEMORY` (4) in the `wasmtime_extern_kind_t` space.
pub const WASMTIME_EXTERN_TAG: wasm_externkind_t = 5;

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.

As a further note on this, there's no need to leave room for the SHAREDMEMORY type here. This is ok to have value 4 since it's the next-in-sequence for the wasm_externkind_t union.

@github-actions github-actions Bot added the wasmtime:c-api Issues pertaining to the C API. label Mar 12, 2026
- Rename wasm_tagtype_t → wasmtime_tagtype_t and all wasm_tagtype_*
  functions → wasmtime_tagtype_* to avoid clashing with future wasm.h
  additions (as requested by alexcrichton)
- Constructor now takes wasm_engine_t* + wasm_valkind_t array + count
  instead of wasm_valtype_vec_t, eliminating the lazy-init complexity
- Replace wasm_tagtype_params() (returning a vec) with
  wasmtime_tagtype_param_count() + wasmtime_tagtype_param(nth),
  matching the component type accessor iterator pattern
- Rename WASM_EXTERN_TAG → WASMTIME_EXTERN_TAG
- Simplify CTagType: stores TagType directly, no Arc<Mutex<LazyTagType>>
- Update C++ TagType wrapper: constructor takes Engine&, Ref exposes
  param_count() / param(nth) instead of params() returning ListRef
- Add ValType::c_to_kind() static helper used by TagType::Ref::param()
- Fix null-pointer panic for zero-length param arrays in Rust

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@bacek
bacek force-pushed the bacek/fix-10252 branch from ab01e72 to 301b225 Compare March 12, 2026 08:47
@bacek

bacek commented Mar 12, 2026

Copy link
Copy Markdown
Contributor Author

Erm. How I can re-run test which looks like some transient DNS error?

error: downloading 'https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip' failed

@alexcrichton alexcrichton 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 this!

@alexcrichton
alexcrichton added this pull request to the merge queue Mar 12, 2026
Merged via the queue into bytecodealliance:main with commit 546f6db Mar 12, 2026
96 of 98 checks passed
@bacek
bacek deleted the bacek/fix-10252 branch March 12, 2026 19:56
bacek added a commit to bacek/ClickHouse that referenced this pull request Mar 13, 2026
Fix for wasm_tagtype_t / C API panic on tag exports was merged
upstream as bytecodealliance/wasmtime#12763.

Includes all changes from wasmtime 41.0.1 → upstream main, including
wasmtime 42.0.x releases.

commit-id:7ceabee1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:c-api Issues pertaining to the C API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants