-
Notifications
You must be signed in to change notification settings - Fork 54
/
ProcessRewind.cpp
211 lines (160 loc) · 6.01 KB
/
ProcessRewind.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ProcessRewind.h"
#include "ipc/ChildInternal.h"
#include "ipc/ParentInternal.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/StaticMutex.h"
#include "InfallibleVector.h"
#include "Monitor.h"
#include "ProcessRecordReplay.h"
namespace mozilla {
namespace recordreplay {
// The most recent checkpoint which was encountered.
static size_t gLastCheckpoint = InvalidCheckpointId;
// Lock for managing pending main thread callbacks.
static Monitor* gMainThreadCallbackMonitor;
// Callbacks to execute on the main thread, in FIFO order. Protected by
// gMainThreadCallbackMonitor.
static StaticInfallibleVector<std::function<void()>> gMainThreadCallbacks;
void InitializeRewindState() { gMainThreadCallbackMonitor = new Monitor(); }
void NewCheckpoint() {
MOZ_RELEASE_ASSERT(Thread::CurrentIsMainThread());
MOZ_RELEASE_ASSERT(!AreThreadEventsPassedThrough());
MOZ_RELEASE_ASSERT(!HasDivergedFromRecording());
gLastCheckpoint++;
js::HitCheckpoint(gLastCheckpoint);
}
static bool gUnhandledDivergeAllowed;
void DivergeFromRecording() {
MOZ_RELEASE_ASSERT(IsReplaying());
Thread* thread = Thread::Current();
MOZ_RELEASE_ASSERT(thread->IsMainThread());
gUnhandledDivergeAllowed = true;
if (!thread->HasDivergedFromRecording()) {
thread->DivergeFromRecording();
// Direct all other threads to diverge from the recording as well.
Thread::WaitForIdleThreads();
for (size_t i = MainThreadId + 1; i <= MaxThreadId; i++) {
Thread::GetById(i)->SetShouldDivergeFromRecording();
}
Thread::ResumeIdleThreads();
}
}
extern "C" {
MOZ_EXPORT bool RecordReplayInterface_InternalHasDivergedFromRecording() {
Thread* thread = Thread::Current();
return thread && thread->HasDivergedFromRecording();
}
} // extern "C"
void DisallowUnhandledDivergeFromRecording() {
MOZ_RELEASE_ASSERT(Thread::CurrentIsMainThread());
gUnhandledDivergeAllowed = false;
}
void EnsureNotDivergedFromRecording(const Maybe<int>& aCallId) {
AssertEventsAreNotPassedThrough();
if (HasDivergedFromRecording()) {
MOZ_RELEASE_ASSERT(gUnhandledDivergeAllowed);
PrintSpew("Unhandled recording divergence: %s\n",
aCallId.isSome() ? GetRedirection(aCallId.ref()).mName : "");
child::ReportUnhandledDivergence();
Unreachable();
}
}
size_t GetLastCheckpoint() { return gLastCheckpoint; }
static bool gMainThreadShouldPause = false;
bool MainThreadShouldPause() { return gMainThreadShouldPause; }
void PauseMainThreadAndServiceCallbacks() {
MOZ_RELEASE_ASSERT(Thread::CurrentIsMainThread());
AssertEventsAreNotPassedThrough();
// Whether there is a PauseMainThreadAndServiceCallbacks frame on the stack.
static bool gMainThreadIsPaused = false;
if (gMainThreadIsPaused) {
return;
}
gMainThreadIsPaused = true;
MOZ_RELEASE_ASSERT(!HasDivergedFromRecording());
MonitorAutoLock lock(*gMainThreadCallbackMonitor);
// Loop and invoke callbacks until one of them unpauses this thread.
while (gMainThreadShouldPause) {
if (!gMainThreadCallbacks.empty()) {
std::function<void()> callback = gMainThreadCallbacks[0];
gMainThreadCallbacks.erase(&gMainThreadCallbacks[0]);
{
MonitorAutoUnlock unlock(*gMainThreadCallbackMonitor);
AutoDisallowThreadEvents disallow;
callback();
}
} else {
gMainThreadCallbackMonitor->Wait();
}
}
// We shouldn't resume the main thread while it still has callbacks.
MOZ_RELEASE_ASSERT(gMainThreadCallbacks.empty());
// If we diverge from the recording we can't resume normal execution.
MOZ_RELEASE_ASSERT(!HasDivergedFromRecording());
gMainThreadIsPaused = false;
}
void PauseMainThreadAndInvokeCallback(const std::function<void()>& aCallback) {
{
MonitorAutoLock lock(*gMainThreadCallbackMonitor);
gMainThreadShouldPause = true;
gMainThreadCallbacks.append(aCallback);
gMainThreadCallbackMonitor->Notify();
}
if (Thread::CurrentIsMainThread()) {
PauseMainThreadAndServiceCallbacks();
}
}
// After forking, the child process does not respawn its threads until
// needed. Child processes will generally either sit idle and only fork more
// processes, or run forward a brief distance, do some operation and then
// terminate. Avoiding respawning the non-main threads in the former case
// greatly reduces pressure on the kernel from having many forks around.
static bool gNeedRespawnThreads;
void EnsureNonMainThreadsAreSpawned() {
if (gNeedRespawnThreads) {
AutoPassThroughThreadEvents pt;
Thread::RespawnAllThreadsAfterFork();
Thread::OperateOnIdleThreadLocks(Thread::OwnedLockState::NeedAcquire);
Thread::ResumeIdleThreads();
gNeedRespawnThreads = false;
}
}
void ResumeExecution() {
EnsureNonMainThreadsAreSpawned();
MonitorAutoLock lock(*gMainThreadCallbackMonitor);
gMainThreadShouldPause = false;
gMainThreadCallbackMonitor->Notify();
}
bool ForkProcess() {
MOZ_RELEASE_ASSERT(IsReplaying());
if (!gNeedRespawnThreads) {
Thread::WaitForIdleThreads();
// Before forking all other threads need to release any locks they are
// holding. After the fork the new process will only have a main thread and
// will not be able to acquire any lock held at the time of the fork.
Thread::OperateOnIdleThreadLocks(Thread::OwnedLockState::NeedRelease);
}
AutoEnsurePassThroughThreadEvents pt;
pid_t pid = fork();
if (pid > 0) {
if (!gNeedRespawnThreads) {
Thread::OperateOnIdleThreadLocks(Thread::OwnedLockState::NeedAcquire);
Thread::ResumeIdleThreads();
}
return true;
}
Print("FORKED %d\n", getpid());
if (TestEnv("MOZ_REPLAYING_WAIT_AT_FORK")) {
BusyWait();
}
ResetPid();
gNeedRespawnThreads = true;
return false;
}
} // namespace recordreplay
} // namespace mozilla