<?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>278496</bug_id>
          
          <creation_ts>2024-08-21 15:14:16 -0700</creation_ts>
          <short_desc>[WASM] Expression stack element types should widen to match target types after conditional branch</short_desc>
          <delta_ts>2024-08-22 11:10:40 -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>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</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>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="David Degazio">d_degazio</reporter>
          <assigned_to name="David Degazio">d_degazio</assigned_to>
          <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2054537</commentid>
    <comment_count>0</comment_count>
    <who name="David Degazio">d_degazio</who>
    <bug_when>2024-08-21 15:14:16 -0700</bug_when>
    <thetext>When we execute a conditional branch instruction in WASM, like br_if (or br_on_null/br_on_cast_fail with the GC proposal), the spec tells us the following:

  br_if l
   - The label C.labels[l] must be defined in the context.
   - Let [t*] be the result type C.labels[l].
   - Then the instruction is valid with type [t* i32] -&gt; [t*].

So, even if we don&apos;t take the branch, the types of the values on the stack are taken from the label&apos;s result types. The spec does *not* merely say that the types on the stack must match the label&apos;s result types, the results *are* the results specified by the branch target. This was discussed and confirmed in https://github.com/WebAssembly/gc/issues/516.

As an example of why this is observable, consider the following:

  (func $f (param (ref func)))
  (func $g
    block (result funcref)
      ref.func $f
      i32.const 0
      br_if 0
      call $f
    end
  )

ref.func $f produces a non-nullable reference to a func, but the result type of the block is funcref aka (ref null func). We unify (ref func) onto (ref null func) successfully in br_if via subtyping. This *also* means we should *re-type* the value on the stack from (ref func) to (ref null func). As a result, we can no longer call $f at the end of the block, because $f expects a non-nullable reference but our stack type is now nullable. Weird!

We currently don&apos;t implement this behavior, but we should, in order to conform to the spec and pass more recent versions of the spec tests.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2054538</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2024-08-21 15:14:25 -0700</bug_when>
    <thetext>&lt;rdar://problem/134449160&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2054540</commentid>
    <comment_count>2</comment_count>
    <who name="David Degazio">d_degazio</who>
    <bug_when>2024-08-21 15:25:58 -0700</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/32553</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2054745</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2024-08-22 11:10:38 -0700</bug_when>
    <thetext>Committed 282624@main (e947e7ebf9d2): &lt;https://commits.webkit.org/282624@main&gt;

Reviewed commits have been landed. Closing PR #32553 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>