<?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>275596</bug_id>
          
          <creation_ts>2024-06-17 20:16:09 -0700</creation_ts>
          <short_desc>Incorrect WebAssembly computation result</short_desc>
          <delta_ts>2024-06-29 00:27:45 -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>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>mfcc64</reporter>
          <assigned_to name="Yusuke Suzuki">ysuzuki</assigned_to>
          <cc>jmichaud</cc>
    
    <cc>keith_miller</cc>
    
    <cc>mark.lam</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>ysuzuki</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2041796</commentid>
    <comment_count>0</comment_count>
    <who name="">mfcc64</who>
    <bug_when>2024-06-17 20:16:09 -0700</bug_when>
    <thetext>// Run this script using browser console / bun

(async function() {
    var buf = Uint8Array.from([0,97,115,109,1,0,0,0,1,10,2,96,2,127,124,0,96,1,127,0,2,17,1,3,101,110,118,9,100,101,98,117,103,95,108,111,103,0,0,3,
    2,1,1,5,3,1,0,2,6,8,1,127,1,65,128,136,4,11,7,17,2,6,109,101,109,111,114,121,2,0,4,116,101,115,116,0,1,10,213,1,
    1,210,1,3,1,127,2,124,4,127,65,16,33,1,2,64,32,0,65,16,72,13,0,3,64,68,0,0,0,0,0,0,0,64,32,1,183,34,2,163,
    33,3,32,1,65,2,118,34,4,33,5,32,4,65,1,32,4,65,1,75,27,34,6,33,7,3,64,32,5,32,3,16,128,128,128,128,0,32,5,65,
    1,106,33,5,32,7,65,127,106,34,7,13,0,11,32,1,65,1,118,33,5,68,0,0,0,0,0,0,16,64,32,2,163,33,3,32,6,33,7,3,
    64,32,5,32,3,16,128,128,128,128,0,32,5,65,1,106,33,5,32,7,65,127,106,34,7,13,0,11,32,4,65,3,108,33,5,68,0,0,0,0,
    0,0,24,64,32,2,163,33,3,3,64,32,5,32,3,16,128,128,128,128,0,32,5,65,1,106,33,5,32,6,65,127,106,34,6,13,0,11,32,1,
    65,2,116,34,1,32,0,76,13,0,11,11,11,0,45,4,110,97,109,101,1,18,2,0,9,100,101,98,117,103,95,108,111,103,1,4,116,101,115,116,
    7,18,1,0,15,95,95,115,116,97,99,107,95,112,111,105,110,116,101,114,0,45,9,112,114,111,100,117,99,101,114,115,1,12,112,114,111,99,101,115,
    115,101,100,45,98,121,1,12,85,98,117,110,116,117,32,99,108,97,110,103,6,49,52,46,48,46,54]);

    var m = true;
    function debug_log(idx, value) {
        if (idx == 4095)
            console.log(idx, value), m = false;
        if (m)
            console.log(idx, value);
    }
    var {instance} = await WebAssembly.instantiate(buf, {env: { debug_log }});

    for (let x = 0; x &lt; 100; x++)
        instance.exports.test(4096);
})();

// end

The result is incorrect starting at idx = 512. But, after several calls to instance.exports.test, it becomes correct.

Here are the last logs:
[Log] 4095 – 1 (x21)
[Log] 4095 – 0.00146484375 (x79)

This is C source code of wasm file:

void debug_log(int, double);

void
test(int n) {
    for (int k = 16; k &lt;= n; k *= 4) {
        int q = k / 4;
        double mul;
        for (int j = 1; j &lt; 4; j++)
            for (int x = 0; x &lt; q; x++)
                mul = 2.0 * j / k, debug_log(j*q+x, mul);
    }
}

Previously, I reported this on bun:
https://github.com/oven-sh/bun/issues/11667</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2042861</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2024-06-24 20:17:12 -0700</bug_when>
    <thetext>&lt;rdar://problem/130468600&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2042870</commentid>
    <comment_count>2</comment_count>
    <who name="Yusuke Suzuki">ysuzuki</who>
    <bug_when>2024-06-24 20:46:18 -0700</bug_when>
    <thetext>Thanks. ToT WebKit / JavaScriptCore can run this as expected (matching against V8 and SpiderMonkey). So please update to the latest WebKit / JavaScriptCore!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2042918</commentid>
    <comment_count>3</comment_count>
    <who name="">mfcc64</who>
    <bug_when>2024-06-25 03:16:26 -0700</bug_when>
    <thetext>Using latest jsc, it still doesn&apos;t work (on x84_64).

(async function() {
    var buf = Uint8Array.from([0,97,115,109,1,0,0,0,1,10,2,96,2,127,124,0,96,1,127,0,2,17,1,3,101,110,118,9,100,101,98,117,103,95,108,111,103,0,0,3,
    2,1,1,5,3,1,0,2,6,8,1,127,1,65,128,136,4,11,7,17,2,6,109,101,109,111,114,121,2,0,4,116,101,115,116,0,1,10,213,1,
    1,210,1,3,1,127,2,124,4,127,65,16,33,1,2,64,32,0,65,16,72,13,0,3,64,68,0,0,0,0,0,0,0,64,32,1,183,34,2,163,
    33,3,32,1,65,2,118,34,4,33,5,32,4,65,1,32,4,65,1,75,27,34,6,33,7,3,64,32,5,32,3,16,128,128,128,128,0,32,5,65,
    1,106,33,5,32,7,65,127,106,34,7,13,0,11,32,1,65,1,118,33,5,68,0,0,0,0,0,0,16,64,32,2,163,33,3,32,6,33,7,3,
    64,32,5,32,3,16,128,128,128,128,0,32,5,65,1,106,33,5,32,7,65,127,106,34,7,13,0,11,32,4,65,3,108,33,5,68,0,0,0,0,
    0,0,24,64,32,2,163,33,3,3,64,32,5,32,3,16,128,128,128,128,0,32,5,65,1,106,33,5,32,6,65,127,106,34,6,13,0,11,32,1,
    65,2,116,34,1,32,0,76,13,0,11,11,11,0,45,4,110,97,109,101,1,18,2,0,9,100,101,98,117,103,95,108,111,103,1,4,116,101,115,116,
    7,18,1,0,15,95,95,115,116,97,99,107,95,112,111,105,110,116,101,114,0,45,9,112,114,111,100,117,99,101,114,115,1,12,112,114,111,99,101,115,
    115,101,100,45,98,121,1,12,85,98,117,110,116,117,32,99,108,97,110,103,6,49,52,46,48,46,54]);

    function debug_log(idx, value) {
        print(idx, value);
    }
    var {instance} = await WebAssembly.instantiate(buf, {env: { debug_log }});
    instance.exports.test(1024);
})();

More specifically, using BBQ JIT gives incorrect result:
jsc --useConcurrentJIT=false --useBBQJIT=true --useOMGJIT=false
...
189 0.015625
190 0.015625
191 0.015625
192 1
193 1
194 1
...

Disabling BBQ JIT gives correct result:
jsc --useBBQJIT=false
...
189 0.015625
190 0.015625
191 0.015625
192 0.0234375
193 0.0234375
194 0.0234375
...

Thank&apos;s.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2042978</commentid>
    <comment_count>4</comment_count>
    <who name="Yusuke Suzuki">ysuzuki</who>
    <bug_when>2024-06-25 09:31:32 -0700</bug_when>
    <thetext>(In reply to mfcc64 from comment #3)
&gt; Using latest jsc, it still doesn&apos;t work (on x84_64).
&gt; 
&gt; (async function() {
&gt;     var buf =
&gt; Uint8Array.from([0,97,115,109,1,0,0,0,1,10,2,96,2,127,124,0,96,1,127,0,2,17,
&gt; 1,3,101,110,118,9,100,101,98,117,103,95,108,111,103,0,0,3,
&gt;    
&gt; 2,1,1,5,3,1,0,2,6,8,1,127,1,65,128,136,4,11,7,17,2,6,109,101,109,111,114,121,
&gt; 2,0,4,116,101,115,116,0,1,10,213,1,
&gt;    
&gt; 1,210,1,3,1,127,2,124,4,127,65,16,33,1,2,64,32,0,65,16,72,13,0,3,64,68,0,0,0,
&gt; 0,0,0,0,64,32,1,183,34,2,163,
&gt;    
&gt; 33,3,32,1,65,2,118,34,4,33,5,32,4,65,1,32,4,65,1,75,27,34,6,33,7,3,64,32,5,
&gt; 32,3,16,128,128,128,128,0,32,5,65,
&gt;    
&gt; 1,106,33,5,32,7,65,127,106,34,7,13,0,11,32,1,65,1,118,33,5,68,0,0,0,0,0,0,16,
&gt; 64,32,2,163,33,3,32,6,33,7,3,
&gt;    
&gt; 64,32,5,32,3,16,128,128,128,128,0,32,5,65,1,106,33,5,32,7,65,127,106,34,7,13,
&gt; 0,11,32,4,65,3,108,33,5,68,0,0,0,0,
&gt;    
&gt; 0,0,24,64,32,2,163,33,3,3,64,32,5,32,3,16,128,128,128,128,0,32,5,65,1,106,33,
&gt; 5,32,6,65,127,106,34,6,13,0,11,32,1,
&gt;    
&gt; 65,2,116,34,1,32,0,76,13,0,11,11,11,0,45,4,110,97,109,101,1,18,2,0,9,100,101,
&gt; 98,117,103,95,108,111,103,1,4,116,101,115,116,
&gt;    
&gt; 7,18,1,0,15,95,95,115,116,97,99,107,95,112,111,105,110,116,101,114,0,45,9,
&gt; 112,114,111,100,117,99,101,114,115,1,12,112,114,111,99,101,115,
&gt;    
&gt; 115,101,100,45,98,121,1,12,85,98,117,110,116,117,32,99,108,97,110,103,6,49,
&gt; 52,46,48,46,54]);
&gt; 
&gt;     function debug_log(idx, value) {
&gt;         print(idx, value);
&gt;     }
&gt;     var {instance} = await WebAssembly.instantiate(buf, {env: { debug_log
&gt; }});
&gt;     instance.exports.test(1024);
&gt; })();
&gt; 
&gt; More specifically, using BBQ JIT gives incorrect result:
&gt; jsc --useConcurrentJIT=false --useBBQJIT=true --useOMGJIT=false
&gt; ...
&gt; 189 0.015625
&gt; 190 0.015625
&gt; 191 0.015625
&gt; 192 1
&gt; 193 1
&gt; 194 1
&gt; ...
&gt; 
&gt; Disabling BBQ JIT gives correct result:
&gt; jsc --useBBQJIT=false
&gt; ...
&gt; 189 0.015625
&gt; 190 0.015625
&gt; 191 0.015625
&gt; 192 0.0234375
&gt; 193 0.0234375
&gt; 194 0.0234375
&gt; ...
&gt; 
&gt; Thank&apos;s.


How did you get that JSC shell? ToT build (revision 280337@main) is showing the following.

```
188 0.015625
189 0.015625
190 0.015625
191 0.015625
192 0.0234375
193 0.0234375
194 0.0234375
195 0.0234375
196 0.0234375
197 0.0234375
198 0.0234375
199 0.0234375
```</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043082</commentid>
    <comment_count>5</comment_count>
    <who name="">mfcc64</who>
    <bug_when>2024-06-25 17:07:12 -0700</bug_when>
    <thetext>jsc is generated by Tools/Scripts/build-jsc --jsc-only (on revision 280337@main). Updating to revision 280352@main doesn&apos;t fix the problem.

Thank&apos;s.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043084</commentid>
    <comment_count>6</comment_count>
    <who name="Yusuke Suzuki">ysuzuki</who>
    <bug_when>2024-06-25 17:15:38 -0700</bug_when>
    <thetext>(In reply to mfcc64 from comment #5)
&gt; jsc is generated by Tools/Scripts/build-jsc --jsc-only (on revision
&gt; 280337@main). Updating to revision 280352@main doesn&apos;t fix the problem.
&gt; 
&gt; Thank&apos;s.

Can you generate build-webkit and use JSC shell via

VM=/path/to/WebKitBuild/Release; DYLD_FRAMEWORK_PATH=$VM $VM/jsc

We cannot reproduce the issue so far with built JSC.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043085</commentid>
    <comment_count>7</comment_count>
    <who name="Yusuke Suzuki">ysuzuki</who>
    <bug_when>2024-06-25 17:15:48 -0700</bug_when>
    <thetext>Is it macOS?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043089</commentid>
    <comment_count>8</comment_count>
    <who name="">mfcc64</who>
    <bug_when>2024-06-25 17:36:58 -0700</bug_when>
    <thetext>I run it on x86_64 Linux.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043220</commentid>
    <comment_count>9</comment_count>
    <who name="Justin Michaud">jmichaud</who>
    <bug_when>2024-06-26 11:00:22 -0700</bug_when>
    <thetext>:looking:</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043660</commentid>
    <comment_count>10</comment_count>
    <who name="Justin Michaud">jmichaud</who>
    <bug_when>2024-06-28 14:21:00 -0700</bug_when>
    <thetext>I was unable to reproduce this on x86_64 linux. Can you show how you launch jsc? You will need to set LD_PRELOAD_PATH to get the right version of jsc:

```
export VM=$BUILDDIR/WebKitBuild/JSCOnly/$CONFIG/
LD_LIBRARY_PATH=$VM $VM/bin/jsc --validateOptions=1
```</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043715</commentid>
    <comment_count>11</comment_count>
    <who name="">mfcc64</who>
    <bug_when>2024-06-28 17:59:10 -0700</bug_when>
    <thetext>It still give incorrect results.

Trying debug build, got assertion failed:
ASSERTION FAILED: op1 == dest || op2 != dest
178 0.015625
/home/data/git/WebKit/Source/JavaScriptCore/assembler/MacroAssemblerX86Common.h(1814) : void JSC::MacroAssemblerX86Common::divDouble(JSC::AbstractMacroAssembler&lt;JSC::X86Assembler&gt;::FPRegisterID, JSC::AbstractMacroAssembler&lt;JSC::X86Assembler&gt;::FPRegisterID, JSC::AbstractMacroAssembler&lt;JSC::X86Assembler&gt;::FPRegisterID)

Looking at the source, maybe this only affect processors that don&apos;t have AVX support.
So, It seems that you should disable AVX to reproduce this bug.

Thank&apos;s.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043735</commentid>
    <comment_count>12</comment_count>
    <who name="Yusuke Suzuki">ysuzuki</who>
    <bug_when>2024-06-28 20:56:16 -0700</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/30299</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043740</commentid>
    <comment_count>13</comment_count>
    <who name="">mfcc64</who>
    <bug_when>2024-06-28 23:56:33 -0700</bug_when>
    <thetext>The patch works for me.

Thank&apos;s.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2043742</commentid>
    <comment_count>14</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2024-06-29 00:27:43 -0700</bug_when>
    <thetext>Committed 280495@main (2fdb35959e51): &lt;https://commits.webkit.org/280495@main&gt;

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

    </bug>

</bugzilla>