forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathRaceAmplifier.h
More file actions
129 lines (118 loc) · 5.76 KB
/
Copy pathRaceAmplifier.h
File metadata and controls
129 lines (118 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (C) 2026 Oven, 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 THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT HOLDERS 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 <cstdint>
#include <wtf/Compiler.h>
#include <wtf/Noncopyable.h>
// RaceAmplifier: randomized scheduling perturbation for hunting data races.
//
// When enabled (Options::randomYieldPeriod() != 0), RaceAmplifier::perturb()
// injects a sched_yield or a short randomized sleep, on average once every
// `randomYieldPeriod` calls per thread. The per-thread decision stream is
// deterministic for a given Options::randomYieldSeed(), so a seed that
// surfaces a crash can be replayed (modulo OS scheduling). When the option is
// off (the default), perturb() is a single load of a process-global word and
// a never-taken predicted branch, and the static initialization is trivial —
// effectively zero cost, and exactly zero cost at the sites that gate it
// behind an existing slow-path branch.
//
// Intended call sites (slow paths ONLY — never in JIT-emitted fast paths,
// LLInt assembly, or allocation fast paths). These are the safepoint-adjacent
// windows where the shared-memory Thread work (see THREAD.md and
// docs/threads/SPEC-*.md) is most likely to harbor interleaving bugs.
// The list below is the integration plan; call sites land with the
// workstream that owns each file:
//
// Object model / transitions:
// - JSObject::putDirectSlow / putByIdSlow paths (JSObject.cpp), between
// deciding a transition is needed and publishing the new butterfly —
// widens the flat->segmented conversion race window.
// - Structure::addPropertyTransition / nonPropertyTransition
// (Structure.cpp), before and after the transition-table lookup.
// - JSObject butterfly (re)allocation: allocateMoreOutOfLineStorage,
// ensureLengthSlow / array storage conversions (JSObject.cpp,
// JSArray.cpp), between allocate and store-with-fence.
// - JSCellLock::lockSlow / unlockSlow (JSCellInlines.h / Heap.cpp):
// immediately after acquiring and immediately before releasing the
// per-object lock.
//
// Heap / GC:
// - LocalAllocator::allocateSlowCase (heap/LocalAllocator.cpp), at the
// marked FIXMEs where synchronized block handout will live.
// - BlockDirectory block handout / FreeList refill paths.
// - Mutator-side handshake points: Heap::stopIfNecessarySlow, the
// VMTraps deferred-work loop, and VMManager stop-the-world
// entry/exit (the N-mutator safepoint machinery).
//
// JIT / code lifecycle:
// - Watchpoint fire sites: WatchpointSet::fireAllSlow (bytecode/
// Watchpoint.cpp) — perturb just before invalidation is published,
// to chase code running between watchpoint check and fire.
// - Handler IC case append under CodeBlock::m_lock
// (bytecode/StructureStubInfo.cpp / InlineCacheCompiler.cpp), between
// building the new case list and the atomic publish.
// - CodeBlock::jettison (CodeBlock.cpp), before reclamation is queued —
// stresses epoch-based reclamation once it exists.
//
// Shared VM state:
// - Atom table insertion slow path (AtomStringTable / Identifier::add),
// once the table goes process-global.
// - Structure allocation lock in StructureIDTable / Structure creation.
//
// Usage at a call site:
//
// #include "RaceAmplifier.h"
// ...
// RaceAmplifier::perturb();
//
// Initialization: RaceAmplifier::initialize() must be called once after
// Options are finalized (from initializeThreading() / VM construction).
// Calling perturb() before initialize() is safe and does nothing.
namespace JSC {
class RaceAmplifier {
WTF_MAKE_NONCOPYABLE(RaceAmplifier);
RaceAmplifier() = delete;
public:
// Reads Options::randomYieldPeriod() / Options::randomYieldSeed() /
// Options::randomYieldMaxMicroseconds() and arms the amplifier.
// Idempotent; safe to call from multiple VM constructions.
JS_EXPORT_PRIVATE static void initialize();
static bool isEnabled() { return !!s_period; }
// The injection point. On the off path this is one non-atomic load and a
// fall-through branch; keep call sites on slow paths regardless.
ALWAYS_INLINE static void perturb()
{
if (s_period) [[unlikely]]
perturbSlow();
}
private:
JS_EXPORT_PRIVATE static void perturbSlow();
// 0 means disabled. Written once during initialize(), before any
// amplified thread can observe it; read racily (benign) thereafter.
JS_EXPORT_PRIVATE static unsigned s_period;
JS_EXPORT_PRIVATE static uint64_t s_seed;
JS_EXPORT_PRIVATE static unsigned s_maxSleepMicroseconds;
};
} // namespace JSC