Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix race condition for wasm referenced functions
https://bugs.webkit.org/show_bug.cgi?id=250847 rdar://104332636 Reviewed by Mark Lam. Fix race condition for updating referenced functions while compiling wasm functions concurrently. * JSTests/wasm/stress/referenced-function.js: Added. (async let): (j.catch): * JSTests/wasm/stress/resources/funcref-race.wasm: Added. * Source/JavaScriptCore/wasm/WasmBBQPlan.cpp: (JSC::Wasm::BBQPlan::compileFunction): * Source/JavaScriptCore/wasm/WasmLLIntPlan.cpp: (JSC::Wasm::LLIntPlan::didCompleteCompilation): * Source/JavaScriptCore/wasm/WasmModuleInformation.h: (JSC::Wasm::ModuleInformation::referencedFunctions const): (JSC::Wasm::ModuleInformation::hasReferencedFunction const): (JSC::Wasm::ModuleInformation::addReferencedFunction const): * Source/WTF/WTF.xcodeproj/project.pbxproj: * Source/WTF/wtf/CMakeLists.txt: * Source/WTF/wtf/FixedBitVector.h: Added. (WTF::FixedBitVector::concurrentAllocateOnce): (WTF::FixedBitVector::concurrentTest): (WTF::FixedBitVector::concurrentTestAndSet): (WTF::FixedBitVector::testAndSet): (WTF::FixedBitVector::test): (WTF::FixedBitVector::findBit const): (WTF::FixedBitVector::operator== const): (WTF::FixedBitVector::hash const): (WTF::FixedBitVector::get const): (WTF::FixedBitVector::dump const): (WTF::FixedBitVectorHash::hash): (WTF::FixedBitVectorHash::equal): * Tools/TestWebKitAPI/CMakeLists.txt: * Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * Tools/TestWebKitAPI/Tests/WTF/FixedBitVector.cpp: Added. (TestWebKitAPI::testFixedBitVectorSize): (TestWebKitAPI::testFixedBitVectorTest): (TestWebKitAPI::testFixedBitVectorTestAndSet): (TestWebKitAPI::testFixedBitVectorConcurrentTest): (TestWebKitAPI::testFixedBitVectorConcurrentTestAndSet): (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/259323@main
- Loading branch information
Showing
13 changed files
with
508 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
let bytes = readFile('./resources/funcref-race.wasm', 'binary'); | ||
(async function () { | ||
let importObject = { | ||
m: { | ||
ifn0() { }, | ||
ifn1() { }, | ||
ifn2() { }, | ||
t0: new WebAssembly.Table({ initial: 18, element: 'externref' }), | ||
t1: new WebAssembly.Table({ initial: 84, element: 'anyfunc' }), | ||
t2: new WebAssembly.Table({ initial: 2, element: 'externref' }), | ||
t3: new WebAssembly.Table({ initial: 6, element: 'anyfunc' }), | ||
t4: new WebAssembly.Table({ initial: 67, element: 'anyfunc', maximum: 579 }), | ||
t5: new WebAssembly.Table({ initial: 39, element: 'externref', maximum: 690 }), | ||
}, | ||
}; | ||
for (let j = 0; j < 10000; j++) { | ||
try { | ||
let i = await WebAssembly.instantiate(bytes, importObject); | ||
i.instance.exports.fn10(); | ||
} catch (e) { | ||
} | ||
} | ||
})(); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/* | ||
* Copyright (C) 2023 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <wtf/Assertions.h> | ||
#include <wtf/Atomics.h> | ||
#include <wtf/BitVector.h> | ||
#include <wtf/HashTraits.h> | ||
#include <wtf/PrintStream.h> | ||
#include <wtf/StdIntExtras.h> | ||
#include <wtf/StdLibExtras.h> | ||
|
||
namespace WTF { | ||
|
||
class FixedBitVector final { | ||
WTF_MAKE_FAST_ALLOCATED; | ||
using WordType = uintptr_t; | ||
|
||
public: | ||
FixedBitVector() = default; | ||
|
||
FixedBitVector(size_t size) | ||
: m_bitVector(size) | ||
{ | ||
} | ||
|
||
bool concurrentTestAndSet(size_t bitIndex, Dependency = Dependency()); | ||
bool concurrentTestAndClear(size_t bitIndex, Dependency = Dependency()); | ||
|
||
bool testAndSet(size_t bitIndex); | ||
bool testAndClear(size_t bitIndex); | ||
bool test(size_t bitIndex); | ||
|
||
// Note that BitVector will be in inline mode with fixed size when | ||
// the BitVector is constructed with size less or equal to `maxInlineBits`. | ||
size_t size() const { return m_bitVector.size(); } | ||
|
||
size_t findBit(size_t startIndex, bool value) const; | ||
|
||
bool operator==(const FixedBitVector&) const; | ||
|
||
unsigned hash() const; | ||
|
||
void dump(PrintStream& out) const; | ||
|
||
BitVector::iterator begin() const { return m_bitVector.begin(); } | ||
BitVector::iterator end() const { return m_bitVector.end(); } | ||
|
||
private: | ||
static constexpr unsigned wordSize = sizeof(WordType) * 8; | ||
static constexpr WordType one = 1; | ||
|
||
BitVector m_bitVector; | ||
}; | ||
|
||
ALWAYS_INLINE bool FixedBitVector::concurrentTestAndSet(size_t bitIndex, Dependency dependency) | ||
{ | ||
if (UNLIKELY(bitIndex >= size())) | ||
return false; | ||
|
||
WordType mask = one << (bitIndex % wordSize); | ||
size_t wordIndex = bitIndex / wordSize; | ||
WordType* data = dependency.consume(m_bitVector.bits()) + wordIndex; | ||
return !bitwise_cast<Atomic<WordType>*>(data)->transactionRelaxed( | ||
[&](WordType& value) -> bool { | ||
if (value & mask) | ||
return false; | ||
|
||
value |= mask; | ||
return true; | ||
}); | ||
} | ||
|
||
ALWAYS_INLINE bool FixedBitVector::concurrentTestAndClear(size_t bitIndex, Dependency dependency) | ||
{ | ||
if (UNLIKELY(bitIndex >= size())) | ||
return false; | ||
|
||
WordType mask = one << (bitIndex % wordSize); | ||
size_t wordIndex = bitIndex / wordSize; | ||
WordType* data = dependency.consume(m_bitVector.bits()) + wordIndex; | ||
return bitwise_cast<Atomic<WordType>*>(data)->transactionRelaxed( | ||
[&](WordType& value) -> bool { | ||
if (!(value & mask)) | ||
return false; | ||
|
||
value &= ~mask; | ||
return true; | ||
}); | ||
} | ||
|
||
ALWAYS_INLINE bool FixedBitVector::testAndSet(size_t bitIndex) | ||
{ | ||
if (UNLIKELY(bitIndex >= size())) | ||
return false; | ||
|
||
WordType mask = one << (bitIndex % wordSize); | ||
size_t wordIndex = bitIndex / wordSize; | ||
WordType* bits = m_bitVector.bits(); | ||
bool previousValue = bits[wordIndex] & mask; | ||
bits[wordIndex] |= mask; | ||
return previousValue; | ||
} | ||
|
||
ALWAYS_INLINE bool FixedBitVector::testAndClear(size_t bitIndex) | ||
{ | ||
if (UNLIKELY(bitIndex >= size())) | ||
return false; | ||
|
||
WordType mask = one << (bitIndex % wordSize); | ||
size_t wordIndex = bitIndex / wordSize; | ||
WordType* bits = m_bitVector.bits(); | ||
bool previousValue = bits[wordIndex] & mask; | ||
bits[wordIndex] &= ~mask; | ||
return previousValue; | ||
} | ||
|
||
ALWAYS_INLINE bool FixedBitVector::test(size_t bitIndex) | ||
{ | ||
if (UNLIKELY(bitIndex >= size())) | ||
return false; | ||
|
||
WordType mask = one << (bitIndex % wordSize); | ||
size_t wordIndex = bitIndex / wordSize; | ||
return m_bitVector.bits()[wordIndex] & mask; | ||
} | ||
|
||
ALWAYS_INLINE size_t FixedBitVector::findBit(size_t startIndex, bool value) const | ||
{ | ||
return m_bitVector.findBit(startIndex, value); | ||
} | ||
|
||
ALWAYS_INLINE bool FixedBitVector::operator==(const FixedBitVector& other) const | ||
{ | ||
return m_bitVector == other.m_bitVector; | ||
} | ||
|
||
ALWAYS_INLINE unsigned FixedBitVector::hash() const | ||
{ | ||
return m_bitVector.hash(); | ||
} | ||
|
||
ALWAYS_INLINE void FixedBitVector::dump(PrintStream& out) const | ||
{ | ||
m_bitVector.dump(out); | ||
} | ||
|
||
struct FixedBitVectorHash { | ||
static unsigned hash(const FixedBitVector& vector) { return vector.hash(); } | ||
static bool equal(const FixedBitVector& a, const FixedBitVector& b) { return a == b; } | ||
static constexpr bool safeToCompareToEmptyOrDeleted = false; | ||
}; | ||
|
||
template<typename T> struct DefaultHash; | ||
template<> struct DefaultHash<FixedBitVector> : FixedBitVectorHash { | ||
}; | ||
|
||
template<> struct HashTraits<FixedBitVector> : public CustomHashTraits<FixedBitVector> { | ||
}; | ||
|
||
} // namespace WTF | ||
|
||
using WTF::FixedBitVector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.