Skip to content

Add an externref type, translate null references and accept reference subtypes - #31

Merged
cfallin merged 5 commits into
bytecodealliance:mainfrom
tschneidereit:reference-type-fixes
Nov 17, 2026
Merged

cfallin merged 5 commits into
bytecodealliance:mainfrom
tschneidereit:reference-type-fixes

Conversation

@tschneidereit

Copy link
Copy Markdown
Member

This fixes some issues with handling of references and reference types, which I found in a fuzzing campaign. See copious amounts of details in the individual commit messages. (But also, the changes aren't all that involved.)

@cfallin cfallin 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! Rather than the complexity of conditionally erasing the types (which requires the predicate that passes over the whole body, etc), what do you think about retaining the types always? It seems fine to me to require compilation within the context of a given module; I'd rather have one "kind" of compilation rather than separate modes like this.

@tschneidereit

Copy link
Copy Markdown
Member Author

The reason for the erasure is that without it, Waffle can turn inputs that are valid Wasm 2.0 into outputs that aren't, because it adds a requirement for the function-references proposal. E.g., for this input:

(module (table 1 funcref) (elem (i32.const 0) func 0) (func (drop (ref.func 0))))

expanding the function and compiling it back gives this output:

(func (local (ref null 0))
  ref.func 0
  local.set 0
  return)

which then fails validation with "function references required for index reference types".

If you think that's acceptable, I can just drop the last commit here and adapt the fuzzing PR to make it work without erasure.

I definitely agree on the two modes otherwise, and could alternatively change FunctionBody::compile() to take the module and apply the erasure too.

@cfallin

cfallin commented Nov 17, 2026

Copy link
Copy Markdown
Member

Ah, OK, I see where this originated. I think I'm fine with targeting Wasm 3.0 generally (or more specifically, requiring typed-funcrefs): it meets our needs, and waffle at this time isn't meant to be (or staffed/resourced to be) a fully general configurable backend for anyone who wants to target any variant of Wasm. I'd rather try to keep the core of it simpler, without passes that try to infer which kind of types are needed. Thanks!

`From<wasmparser::RefType> for Type` mapped every reference type
without a type index to `Type::FuncRef`. An `externref` in a
signature, local, global or table came out as `funcref`, so the output
of `(module (func (result externref) (ref.null extern)))` failed
validation. Reference types the IR has no variant for, such as
`(ref func)` or `anyref`, were also turned into `funcref`.

Add `Type::ExternRef` for nullable `extern` references, with its
display name and its encoding. Replace the `From` conversions from
`wasmparser::ValType` and `wasmparser::RefType` to `Type`, and from
`wasmparser::FuncType` to `SignatureData`, with `TryFrom` conversions
that return `FrontendError::UnsupportedFeature` for non-nullable
abstract references, shared abstract references, exact references,
references to a rec-group type index and abstract heap types other
than `func` and `extern`. The frontend propagates the error from the
type section, imports, globals, tables, locals and block types. A
`select` whose type cannot be converted, and any other operator that
`Operator::try_from` does not convert, now returns
`FrontendError::UnsupportedFeature` instead of panicking.

The type section also returns `FrontendError::UnsupportedFeature` for
a shared function type. `SignatureData` has no field for `shared`, so
the backend emitted such a type as an unshared function type.
`Operator::RefNull` held a signature index, and `Operator::try_from`
converted `ref.null` only with a concrete type index. Expanding a body
with `ref.null func` or `ref.null extern` panicked in the frontend
because the conversion failed.

`Operator::RefNull` now holds its result type: `Type::FuncRef`,
`Type::ExternRef`, or a nullable `Type::TypedFuncRef`. `ref.null`
converts with any heap type that converts to one of these, and the
backend encodes the heap type of the result type. Other heap types
return `FrontendError::UnsupportedFeature`.
`LocalTracker::create_default_value` produced a zero constant for
numeric and vector types and hit `todo!` for every reference type, so
expanding a body that read a `funcref` local before setting it
panicked.

Nullable reference types (`Type::FuncRef`, `Type::ExternRef` and a
nullable `Type::TypedFuncRef`) now default to a `ref.null` of that
type. Non-nullable typed function references still hit `todo!`.
Validation requires every read of a non-nullable local to follow a set
of that local.

The backend's global initializer had the same gap: a defined global
with no value emitted `ref.null` only for a nullable
`Type::TypedFuncRef` and hit `unimplemented!` for `Type::FuncRef`. It
now emits `ref.null` for all three nullable reference types.
…pected type.

The frontend checked each operand type against the type the operator
expects with `assert_eq!`. A valid module panicked when an operand's
type was a proper subtype of the expected type: a non-nullable
`(ref $t)` passed to `call_ref $t`, which expects `(ref null $t)`, or
the `(ref null $t)` result of `ref.func` passed to `table.set`,
`global.set` or `call` where `funcref` is expected.

Add `Type::is_subtype_of` and use it for the operand check. A typed
function reference is a subtype of `funcref`. It is also a subtype of
a typed function reference with the same signature index, unless it is
nullable and the other is not. Signature indices are compared by index.
…ter type.

The backend asserted that each block argument has exactly the type of
the block parameter it is passed to. The frontend creates the
parameters of a join block with the declared types of the locals, so a
`funcref` local that is assigned the result of `ref.func` on one path
passes a `(ref null $t)` argument to a `funcref` parameter, and the
backend panicked.

Check the argument type with `Type::is_subtype_of` instead.
@tschneidereit

Copy link
Copy Markdown
Member Author

Okay, that makes sense. I dropped the last commit and resolved the conflicts in the others.

@cfallin cfallin 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!

@cfallin
cfallin merged commit 41f4f2b into bytecodealliance:main Nov 17, 2026
3 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.

2 participants