<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>288722</bug_id>
          
          <creation_ts>2025-02-27 07:54:54 -0800</creation_ts>
          <short_desc>Tail call result type validation not working correctly for nullable types</short_desc>
          <delta_ts>2025-03-10 14:24:17 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebAssembly</component>
          <version>Other</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Blocker</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="David Thompson">dave</reporter>
          <assigned_to>daniel_liu4</assigned_to>
          <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2098771</commentid>
    <comment_count>0</comment_count>
      <attachid>474371</attachid>
    <who name="David Thompson">dave</who>
    <bug_when>2025-02-27 07:54:54 -0800</bug_when>
    <thetext>Created attachment 474371
Wasm binary that fails validation

Tail calling a function that returns a non-nullable reference type from a caller that returns a nullable reference type throws an error at validation time.

Here&apos;s a minimal WAT program (Wasm binary attached) to demonstrate:

```
(module
 (func $bar (param $x i32) (result (ref i31))
       (ref.i31 (local.get $x)))
 (func $foo (export &quot;foo&quot;) (param $x i32) (result (ref null i31))
       (if (result (ref null i31))
           (i32.eqz (local.get $x))
           (then (ref.null i31))
           (else (return_call $bar (local.get $x))))))
```

I believe this program is valid. I used this JavaScript code to instantiate and call the exported &quot;foo&quot; function:

```
const module = await WebAssembly.instantiateStreaming(fetch(&quot;foo.wasm&quot;));
console.log(module.instance.exports.foo(42));
```

The expected result is to see &quot;42&quot; printed to the console. Instead, I see this error:

```
WebAssembly.Module doesn&apos;t validate: tail call function index 0 return type mismatch: expected (ref null i31), got (ref i31), in function at index 1
```

Relevant line in the WebKit source: https://github.com/WebKit/WebKit/blob/bda8d91633ad360c8cb949ff20e19c1276f80a5d/Source/JavaScriptCore/wasm/WasmFunctionParser.h#L3116

I observed this issue in my own local Epiphany build using WebKitGTK 2.47.90 on Linux. The above program was also tested on Chrome and Firefox where it ran successfully.

While the above WAT code is a minimal example of the problem, this is a real-world blocker issue that was found when attempting to run Scheme programs built with the Hoot compiler (https://spritely.institute/hoot/) on WebKit based browsers.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2100982</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2025-03-06 07:55:15 -0800</bug_when>
    <thetext>&lt;rdar://problem/146391286&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2102020</commentid>
    <comment_count>2</comment_count>
    <who name="">daniel_liu4</who>
    <bug_when>2025-03-10 13:25:58 -0700</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/42206</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2102033</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2025-03-10 14:24:15 -0700</bug_when>
    <thetext>Committed 291918@main (61e1a1710342): &lt;https://commits.webkit.org/291918@main&gt;

Reviewed commits have been landed. Closing PR #42206 and removing active labels.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>474371</attachid>
            <date>2025-02-27 07:54:54 -0800</date>
            <delta_ts>2025-02-27 07:54:54 -0800</delta_ts>
            <desc>Wasm binary that fails validation</desc>
            <filename>foo.wasm</filename>
            <type>application/wasm</type>
            <size>64</size>
            <attacher name="David Thompson">dave</attacher>
            
              <data encoding="base64">AGFzbQEAAAABDQJgAX8BZGxgAX8BY2wDAwIAAQcHAQNmb28AAQoZAgYAIAD7HAsQACAARQRjbNBs
BSAAEgALCw==
</data>

          </attachment>
      

    </bug>

</bugzilla>