<?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>252538</bug_id>
          
          <creation_ts>2023-02-18 16:31:20 -0800</creation_ts>
          <short_desc>[Wasm-GC] Incorrect generated LLInt code for structs containing ref fields</short_desc>
          <delta_ts>2023-03-20 18:19:53 -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 Local 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>
          
          <blocked>247394</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="Tim Chevalier">tjc</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1934941</commentid>
    <comment_count>0</comment_count>
    <who name="Tim Chevalier">tjc</who>
    <bug_when>2023-02-18 16:31:20 -0800</bug_when>
    <thetext>The following test:

```
//@ runWebAssemblySuite(&quot;--useWebAssemblyTypedFunctionReferences=true&quot;, &quot;--useWebAssemblyGC=true&quot;)

import * as assert from &quot;../assert.js&quot;;
import { compile, instantiate } from &quot;./wast-wrapper.js&quot;;

function module(bytes, valid = true) {
  let buffer = new ArrayBuffer(bytes.length);
  let view = new Uint8Array(buffer);
  for (let i = 0; i &lt; bytes.length; ++i) {
    view[i] = bytes.charCodeAt(i);
  }
  return new WebAssembly.Module(buffer);
}

function testStructDeclaration() {

  let m = instantiate(`
    (module
      (type $a (array i32))
      (type $s (struct (field (ref $a)) (field (ref $a)) (field (ref $a))))

      (func $new (result (ref $s))
         (struct.new_canon $s (array.new_canon_default $a (i32.const 5))
                              (array.new_canon_default $a (i32.const 17))
                              (array.new_canon_default $a (i32.const 0))))

      (func (export &quot;len0&quot;) (result i32)
         (struct.get $s 0 (call $new))
         (array.len))
      (func (export &quot;len1&quot;) (result i32)
         (struct.get $s 1 (call $new))
         (array.len))
      (func (export &quot;len2&quot;) (result i32)
         (struct.get $s 2 (call $new))
         (array.len)))`);

    assert.eq(m.exports.len0(), 5);
    assert.eq(m.exports.len1(), 17);
    assert.eq(m.exports.len2(), 0);
}

testStructDeclaration();
```

fails on the second `assert.eq` call. LLInt generates the following bytecode for the `$new` function:

```
&lt;?&gt;.wasm-function[0] : () -&gt; [Ref]
wasm size: 20 bytes
bytecode: 10 instructions (0 16-bit instructions, 3 32-bit instructions); 85 bytes; 0 parameter(s); 18 local(s); 22 callee register(s)
[   0] enter              
[   1] **array_new        dst:loc18, size:5(const0), value:&lt;invalid&gt;, typeIndex:0, arrayNewKind:1
[  23] **array_new        dst:loc19, size:17(const1), value:&lt;invalid&gt;, typeIndex:0, arrayNewKind:1
[  45] **array_new        dst:loc20, size:0(const2), value:&lt;invalid&gt;, typeIndex:0, arrayNewKind:1
[  67] mov                dst:loc19, src:loc20
[  70] mov                dst:loc20, src:loc19
[  73] mov                dst:loc21, src:loc18
[  76] struct_new         dst:loc18, typeIndex:1, useDefault:false, firstValue:loc21
[  81] mov                dst:loc4, src:loc18
[  84] ret                
```

This assigns loc20 (the third argument to struct.new) into loc19, overwriting the second argument to struct.new. So if we write `(struct.new_canon $s e1 e2 e3)`, the result is the struct `{ e1, e3, e3 }`.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1934950</commentid>
    <comment_count>1</comment_count>
    <who name="Tim Chevalier">tjc</who>
    <bug_when>2023-02-18 21:18:33 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/10342</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1936804</commentid>
    <comment_count>2</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2023-02-25 16:32:20 -0800</bug_when>
    <thetext>&lt;rdar://problem/105931873&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1942741</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2023-03-20 18:19:37 -0700</bug_when>
    <thetext>Committed 261902@main (400ec97f086f): &lt;https://commits.webkit.org/261902@main&gt;

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

    </bug>

</bugzilla>