Skip to content

Commit 9b8ca54

Browse files
jakobkummerowV8 LUCI CQ
authored andcommitted
[wasm] Fix lookup of wrappers marked "is_dying"
We must not add a dying wrapper to the current WasmCodeRefScope. Doing so creates a possibility that a wrapper that's about to be deleted by another thread (after going through a Wasm Code GC cycle) will be deleted again by that WasmCodeRefScope. To fix this, this patch merges the "is_dying" bit and the refcount into a single bit field that can be updated atomically, and updates the WasmImportWrapperCache to use the new, more careful machinery. This definitely fixes issue 409379692, hopefully also 407003348. Bug: 407003348 Change-Id: Ib83e9df034e8ddf3590828aea86741779ef92ab9 Fixed: 409379692 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6450127 Reviewed-by: Matthias Liedtke <[email protected]> Auto-Submit: Jakob Kummerow <[email protected]> Commit-Queue: Jakob Kummerow <[email protected]> Cr-Commit-Position: refs/heads/main@{#99765}
1 parent 3b04471 commit 9b8ca54

3 files changed

Lines changed: 75 additions & 49 deletions

File tree

‎src/wasm/wasm-code-manager.cc‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,9 +2780,10 @@ void NativeModule::FreeCode(base::Vector<WasmCode* const> codes) {
27802780
// Free the {WasmCode} objects. This will also unregister trap handler data.
27812781
for (WasmCode* code : codes) {
27822782
DCHECK_EQ(1, owned_code_.count(code->instruction_start()));
2783-
// TODO(407003348): Drop these checks if they don't trigger in the wild.
2784-
CHECK(code->is_dying());
2785-
CHECK_EQ(code->ref_count_.load(std::memory_order_acquire), 0);
2783+
// TODO(407003348): Drop this check if it doesn't trigger in the wild.
2784+
CHECK_EQ(WasmCode::refcount(
2785+
code->ref_count_bitfield_.load(std::memory_order_acquire)),
2786+
0);
27862787
owned_code_.erase(code->instruction_start());
27872788
}
27882789
// Remove debug side tables for all removed code objects, after releasing our
@@ -3002,6 +3003,16 @@ void WasmCodeRefScope::AddRef(WasmCode* code) {
30023003
code->IncRef();
30033004
}
30043005

3006+
// static
3007+
WasmCode* WasmCodeRefScope::AddRefIfNotDying(WasmCode* code) {
3008+
DCHECK_NOT_NULL(code);
3009+
WasmCodeRefScope* current_scope = current_code_refs_scope;
3010+
DCHECK_NOT_NULL(current_scope);
3011+
if (!code->IncRefIfNotDying()) return nullptr;
3012+
current_scope->code_ptrs_.push_back(code);
3013+
return code;
3014+
}
3015+
30053016
void WasmCodeLookupCache::Flush() {
30063017
for (int i = 0; i < kWasmCodeLookupCacheSize; i++)
30073018
cache_[i].pc.store(kNullAddress, std::memory_order_release);

‎src/wasm/wasm-code-manager.h‎

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -261,41 +261,51 @@ class V8_EXPORT_PRIVATE WasmCode final {
261261
~WasmCode();
262262

263263
void IncRef() {
264-
[[maybe_unused]] int old_val =
265-
ref_count_.fetch_add(1, std::memory_order_acq_rel);
266-
DCHECK_LE(1, old_val);
267-
DCHECK_GT(kMaxInt, old_val);
264+
[[maybe_unused]] uint32_t old_field =
265+
ref_count_bitfield_.fetch_add(1, std::memory_order_acq_rel);
266+
DCHECK_LE(1, refcount(old_field));
267+
DCHECK_GT(kMaxInt, refcount(old_field));
268+
}
269+
270+
// Returns true if the refcount was incremented, false if {this->is_dying()}.
271+
bool IncRefIfNotDying() {
272+
uint32_t old_field = ref_count_bitfield_.load(std::memory_order_acquire);
273+
while (true) {
274+
if (is_dying(old_field)) return false;
275+
if (ref_count_bitfield_.compare_exchange_weak(
276+
old_field, old_field + 1, std::memory_order_acq_rel)) {
277+
return true;
278+
}
279+
}
268280
}
269281

270282
// Decrement the ref count. Returns whether this code becomes dead and needs
271283
// to be freed.
272284
V8_WARN_UNUSED_RESULT bool DecRef() {
273-
int old_count = ref_count_.load(std::memory_order_acquire);
285+
uint32_t old_field = ref_count_bitfield_.load(std::memory_order_acquire);
274286
while (true) {
275-
DCHECK_LE(1, old_count);
276-
if (V8_UNLIKELY(old_count == 1)) {
277-
if (is_dying()) {
287+
DCHECK_LE(1, refcount(old_field));
288+
if (V8_UNLIKELY(refcount(old_field) == 1)) {
289+
if (is_dying(old_field)) {
278290
// The code was already on the path to deletion, only temporary
279291
// C++ references to it are left. Decrement the refcount, and
280292
// return true if it drops to zero.
281293
return DecRefOnDeadCode();
282294
}
283295
// Otherwise, the code enters the path to destruction now.
284-
mark_as_dying();
285-
old_count = ref_count_.load(std::memory_order_acquire);
286-
if (V8_LIKELY(old_count == 1)) {
296+
if (ref_count_bitfield_.compare_exchange_weak(
297+
old_field, old_field | kIsDyingMask,
298+
std::memory_order_acq_rel)) {
287299
// No other thread got in the way. Commit to the decision.
288300
DecRefOnPotentiallyDeadCode();
289301
return false;
290302
}
291-
// Another thread managed to increment the refcount again, just
292-
// before we set the "dying" bit. So undo that, and resume the
293-
// loop to evaluate again what needs to be done.
294-
undo_mark_as_dying();
303+
// Another thread interfered. Re-evaluate what to do.
304+
continue;
295305
}
296-
DCHECK_LT(1, old_count);
297-
if (ref_count_.compare_exchange_weak(old_count, old_count - 1,
298-
std::memory_order_acq_rel)) {
306+
DCHECK_LT(1, refcount(old_field));
307+
if (ref_count_bitfield_.compare_exchange_weak(
308+
old_field, old_field - 1, std::memory_order_acq_rel)) {
299309
return false;
300310
}
301311
}
@@ -304,16 +314,18 @@ class V8_EXPORT_PRIVATE WasmCode final {
304314
// Decrement the ref count on code that is known to be in use (i.e. the ref
305315
// count cannot drop to zero here).
306316
void DecRefOnLiveCode() {
307-
[[maybe_unused]] int old_count =
308-
ref_count_.fetch_sub(1, std::memory_order_acq_rel);
309-
DCHECK_LE(2, old_count);
317+
[[maybe_unused]] uint32_t old_bitfield_value =
318+
ref_count_bitfield_.fetch_sub(1, std::memory_order_acq_rel);
319+
DCHECK_LE(2, refcount(old_bitfield_value));
310320
}
311321

312322
// Decrement the ref count on code that is known to be dead, even though there
313323
// might still be C++ references. Returns whether this drops the last
314324
// reference and the code needs to be freed.
315325
V8_WARN_UNUSED_RESULT bool DecRefOnDeadCode() {
316-
return ref_count_.fetch_sub(1, std::memory_order_acq_rel) == 1;
326+
uint32_t old_bitfield_value =
327+
ref_count_bitfield_.fetch_sub(1, std::memory_order_acq_rel);
328+
return refcount(old_bitfield_value) == 1;
317329
}
318330

319331
// Decrement the ref count on a set of {WasmCode} objects, potentially
@@ -322,9 +334,9 @@ class V8_EXPORT_PRIVATE WasmCode final {
322334

323335
// Called by the WasmEngine when it shuts down for code it thinks is
324336
// probably dead (i.e. is in the "potentially_dead_code_" set). Wrapped
325-
// in a method only because {ref_count_} is private.
337+
// in a method only because {ref_count_bitfield_} is private.
326338
void DcheckRefCountIsOne() {
327-
DCHECK_EQ(1, ref_count_.load(std::memory_order_acquire));
339+
DCHECK_EQ(1, refcount(ref_count_bitfield_.load(std::memory_order_acquire)));
328340
}
329341

330342
// Returns the last source position before {offset}.
@@ -341,7 +353,15 @@ class V8_EXPORT_PRIVATE WasmCode final {
341353
return ForDebuggingField::decode(flags_);
342354
}
343355

344-
bool is_dying() const { return dying_.load(std::memory_order_acquire); }
356+
bool is_dying() const {
357+
return is_dying(ref_count_bitfield_.load(std::memory_order_acquire));
358+
}
359+
static bool is_dying(uint32_t bit_field_value) {
360+
return (bit_field_value & kIsDyingMask) != 0;
361+
}
362+
static uint32_t refcount(uint32_t bit_field_value) {
363+
return bit_field_value & ~kIsDyingMask;
364+
}
345365

346366
// Returns {true} for Liftoff code that sets up a feedback vector slot in its
347367
// stack frame.
@@ -428,11 +448,6 @@ class V8_EXPORT_PRIVATE WasmCode final {
428448
// for consideration in the next Code GC cycle.
429449
V8_NOINLINE void DecRefOnPotentiallyDeadCode();
430450

431-
void mark_as_dying() { dying_.store(true, std::memory_order_release); }
432-
// This is rarely necessary to mitigate a race condition. See the comment
433-
// at its (only) call site.
434-
void undo_mark_as_dying() { dying_.store(false, std::memory_order_release); }
435-
436451
NativeModule* const native_module_ = nullptr;
437452
uint8_t* const instructions_;
438453
const uint64_t signature_hash_;
@@ -477,10 +492,6 @@ class V8_EXPORT_PRIVATE WasmCode final {
477492
using ForDebuggingField = ExecutionTierField::Next<ForDebugging, 2>;
478493
using FrameHasFeedbackSlotField = ForDebuggingField::Next<bool, 1>;
479494

480-
// Will be set to {true} the first time this code object is considered
481-
// "potentially dead" (to be confirmed by the next Wasm Code GC cycle).
482-
std::atomic<bool> dying_{false};
483-
484495
// WasmCode is ref counted. Counters are held by:
485496
// 1) The jump table / code table.
486497
// 2) {WasmCodeRefScope}s.
@@ -491,7 +502,11 @@ class V8_EXPORT_PRIVATE WasmCode final {
491502
// it's being used. Once the ref count drops to zero (i.e. after being removed
492503
// from (3) and all (2)), the code object is deleted and the memory for the
493504
// machine code is freed.
494-
std::atomic<int> ref_count_{1};
505+
// The topmost bit is used to indicate that the code is in (3). It is stored
506+
// in this same field to avoid race conditions between atomic updates to
507+
// that state and the refcount.
508+
static constexpr uint32_t kIsDyingMask = 0x8000'0000u;
509+
std::atomic<uint32_t> ref_count_bitfield_{1};
495510
};
496511

497512
WasmCode::Kind GetCodeKind(const WasmCompilationResult& result);
@@ -1238,6 +1253,10 @@ class V8_EXPORT_PRIVATE V8_NODISCARD WasmCodeRefScope {
12381253
// Register a {WasmCode} reference in the current {WasmCodeRefScope}. Fails if
12391254
// there is no current scope.
12401255
static void AddRef(WasmCode*);
1256+
// Same, but conditional:
1257+
// - if the {code} is marked as dying, do nothing, return nullptr.
1258+
// - otherwise add a ref and return {code}.
1259+
static WasmCode* AddRefIfNotDying(WasmCode* code);
12411260

12421261
private:
12431262
WasmCodeRefScope* const previous_scope_;

‎src/wasm/wasm-import-wrapper-cache.cc‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ WasmCode* WasmImportWrapperCache::FindWrapper(WasmCodePointer call_target) {
146146
GetProcessWideWasmCodePointerTable()->GetEntrypointWithoutSignatureCheck(
147147
call_target));
148148
if (iter == codes_.end()) return nullptr;
149-
WasmCodeRefScope::AddRef(iter->second);
150-
if (iter->second->is_dying()) return nullptr;
151-
return iter->second;
149+
return WasmCodeRefScope::AddRefIfNotDying(iter->second);
152150
}
153151

154152
WasmCode* WasmImportWrapperCache::CompileWasmImportCallWrapper(
@@ -165,8 +163,8 @@ WasmCode* WasmImportWrapperCache::CompileWasmImportCallWrapper(
165163
// again whether another thread has just created the wrapper.
166164
wasm_code = cache_scope[key];
167165
if (wasm_code) {
168-
WasmCodeRefScope::AddRef(wasm_code);
169-
if (!wasm_code->is_dying()) return wasm_code;
166+
wasm_code = WasmCodeRefScope::AddRefIfNotDying(wasm_code);
167+
if (wasm_code) return wasm_code;
170168
}
171169

172170
wasm_code = cache_scope.AddWrapper(key, std::move(result),
@@ -218,9 +216,9 @@ void WasmImportWrapperCache::Free(std::vector<WasmCode*>& wrappers) {
218216
}
219217
code_allocator_->FreeCode(base::VectorOf(wrappers));
220218
for (WasmCode* wrapper : wrappers) {
221-
// TODO(407003348): Drop these checks if they don't trigger in the wild.
222-
CHECK(wrapper->is_dying());
223-
CHECK_EQ(wrapper->ref_count_.load(std::memory_order_acquire), 0);
219+
// TODO(407003348): Drop this check if it doesn't trigger in the wild.
220+
CHECK_EQ(wrapper->ref_count_bitfield_.load(std::memory_order_acquire),
221+
WasmCode::kIsDyingMask);
224222
delete wrapper;
225223
}
226224
// Make sure nobody tries to access stale pointers.
@@ -235,9 +233,7 @@ WasmCode* WasmImportWrapperCache::MaybeGet(ImportCallKind kind,
235233

236234
auto it = entry_map_.find({kind, type_index, expected_arity, suspend});
237235
if (it == entry_map_.end()) return nullptr;
238-
WasmCodeRefScope::AddRef(it->second);
239-
if (it->second->is_dying()) return nullptr;
240-
return it->second;
236+
return WasmCodeRefScope::AddRefIfNotDying(it->second);
241237
}
242238

243239
WasmCode* WasmImportWrapperCache::Lookup(Address pc) const {

0 commit comments

Comments
 (0)