<?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>248631</bug_id>
          
          <creation_ts>2022-12-01 14:33:10 -0800</creation_ts>
          <short_desc>[JSC] Race condition in Atomic.wait, Atomic.waitAsync</short_desc>
          <delta_ts>2022-12-06 12:32:04 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</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></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Joseph Griego">joseph.j.griego</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>1916071</commentid>
    <comment_count>0</comment_count>
    <who name="Joseph Griego">joseph.j.griego</who>
    <bug_when>2022-12-01 14:33:10 -0800</bug_when>
    <thetext>At the moment, this is manifesting as intermittent timeouts in the arm32 linux test runner, for example: https://build.webkit.org/#/builders/24/builds/17430

After investigation, this is the test `stress/waitasync-waiter-list-order.js` hanging; this manifests as some of the workers hanging on the initial `Atomic.wait` call in that test--using a debugger to examine the memory being waited on reveals that they should all have woken up and moved on.

The implementation of both `Atomic.wait` and `Atomic.waitAsync` begin by atomically reading from the array and comparing the result to the expected value, then obtaining a lock on the list of waiters and proceeding from there--this is contrary to the spec [1], which requires that the atomic read happens while the list of waiters is exclusively locked and allows a race where the following interleaving results in A hanging indefinitely if no other calls to Atomic.notify on that address occur:

thread A: begin Atomic.wait(buffer, idx, expected_value)
thread A: read buffer[idx], get expected_value

thread B: perform Atomic.store(buffer, idx, unexpected_value)
thread B: perform Atomic.notify(buffer, idx) [nothing is notified since the list of waiters is empty]

thread A: lock list of waiters, wait on condition variable, hang waiting for next notify

---

This is fixed by performing the atomic read after the list of waiters is locked

Patch forthcoming.

[1] https://tc39.es/ecma262/multipage/structured-data.html#sec-atomics.wait</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1916072</commentid>
    <comment_count>1</comment_count>
    <who name="Joseph Griego">joseph.j.griego</who>
    <bug_when>2022-12-01 14:45:09 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/7048</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1916992</commentid>
    <comment_count>2</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2022-12-06 12:31:27 -0800</bug_when>
    <thetext>Committed 257423@main (bb3be5c8f260): &lt;https://commits.webkit.org/257423@main&gt;

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

    </bug>

</bugzilla>