forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecordReplay.cpp
179 lines (151 loc) · 7.18 KB
/
RecordReplay.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
/* -*- 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 "RecordReplay.h"
#include "js/GCAnnotations.h"
#include "mozilla/Atomics.h"
#include "mozilla/Casting.h"
#include "mozilla/Utf8.h"
#include <stdlib.h>
// Recording and replaying is only enabled on Mac nightlies.
#if defined(XP_MACOSX) && defined(NIGHTLY_BUILD)
# define ENABLE_RECORD_REPLAY
#endif
#ifdef ENABLE_RECORD_REPLAY
# include <dlfcn.h>
#endif
namespace mozilla {
namespace recordreplay {
// clang-format off
#define FOR_EACH_INTERFACE(Macro) \
Macro(InternalAreThreadEventsPassedThrough, bool, (), ()) \
Macro(InternalAreThreadEventsDisallowed, bool, (), ()) \
Macro(InternalRecordReplayValue, size_t, (size_t aValue), (aValue)) \
Macro(InternalHasDivergedFromRecording, bool, (), ()) \
Macro(InternalGeneratePLDHashTableCallbacks, const PLDHashTableOps*, \
(const PLDHashTableOps* aOps), (aOps)) \
Macro(InternalUnwrapPLDHashTableCallbacks, const PLDHashTableOps*, \
(const PLDHashTableOps* aOps), (aOps)) \
Macro(InternalThingIndex, size_t, (void* aThing), (aThing)) \
Macro(InternalVirtualThingName, const char*, (void* aThing), (aThing)) \
Macro(ExecutionProgressCounter, ProgressCounter*, (), ()) \
Macro(NewTimeWarpTarget, ProgressCounter, (), ()) \
Macro(ShouldUpdateProgressCounter, bool, (const char* aURL), (aURL)) \
Macro(DefineRecordReplayControlObject, bool, (void* aCx, void* aObj), \
(aCx, aObj))
#define FOR_EACH_INTERFACE_VOID(Macro) \
Macro(InternalBeginOrderedAtomicAccess, (const void* aValue), (aValue)) \
Macro(InternalEndOrderedAtomicAccess, (), ()) \
Macro(InternalBeginPassThroughThreadEvents, (), ()) \
Macro(InternalEndPassThroughThreadEvents, (), ()) \
Macro(InternalBeginDisallowThreadEvents, (), ()) \
Macro(InternalEndDisallowThreadEvents, (), ()) \
Macro(InternalRecordReplayBytes, (void* aData, size_t aSize), \
(aData, aSize)) \
Macro(InternalInvalidateRecording, (const char* aWhy), (aWhy)) \
Macro(InternalDestroyPLDHashTableCallbacks, (const PLDHashTableOps* aOps), \
(aOps)) \
Macro(InternalMovePLDHashTableContents, \
(const PLDHashTableOps* aFirstOps, const PLDHashTableOps* aSecondOps), \
(aFirstOps, aSecondOps)) \
Macro(InternalHoldJSObject, (void* aJSObj), (aJSObj)) \
Macro(InternalRecordReplayAssert, (const char* aFormat, va_list aArgs), \
(aFormat, aArgs)) \
Macro(InternalRecordReplayAssertBytes, (const void* aData, size_t aSize), \
(aData, aSize)) \
Macro(InternalRegisterThing, (void* aThing), (aThing)) \
Macro(InternalUnregisterThing, (void* aThing), (aThing)) \
Macro(BeginContentParse, \
(const void* aToken, const char* aURL, const char* aContentType), \
(aToken, aURL, aContentType)) \
Macro(AddContentParseData8, \
(const void* aToken, const mozilla::Utf8Unit* aUtf8Buffer, \
size_t aLength), \
(aToken, aUtf8Buffer, aLength)) \
Macro(AddContentParseData16, \
(const void* aToken, const char16_t* aBuffer, size_t aLength), \
(aToken, aBuffer, aLength)) \
Macro(EndContentParse, (const void* aToken), (aToken))
// clang-format on
#define DECLARE_SYMBOL(aName, aReturnType, aFormals, _) \
static aReturnType(*gPtr##aName) aFormals;
#define DECLARE_SYMBOL_VOID(aName, aFormals, _) \
DECLARE_SYMBOL(aName, void, aFormals, _)
FOR_EACH_INTERFACE(DECLARE_SYMBOL)
FOR_EACH_INTERFACE_VOID(DECLARE_SYMBOL_VOID)
#undef DECLARE_SYMBOL
#undef DECLARE_SYMBOL_VOID
static void* LoadSymbol(const char* aName) {
#ifdef ENABLE_RECORD_REPLAY
void* rv = dlsym(RTLD_DEFAULT, aName);
MOZ_RELEASE_ASSERT(rv);
return rv;
#else
return nullptr;
#endif
}
void Initialize(int aArgc, char* aArgv[]) {
// Only initialize if the right command line option was specified.
bool found = false;
for (int i = 0; i < aArgc; i++) {
if (!strcmp(aArgv[i], gProcessKindOption)) {
found = true;
break;
}
}
if (!found) {
return;
}
void (*initialize)(int, char**);
BitwiseCast(LoadSymbol("RecordReplayInterface_Initialize"), &initialize);
if (!initialize) {
return;
}
#define INIT_SYMBOL(aName, _1, _2, _3) \
BitwiseCast(LoadSymbol("RecordReplayInterface_" #aName), &gPtr##aName);
#define INIT_SYMBOL_VOID(aName, _2, _3) INIT_SYMBOL(aName, void, _2, _3)
FOR_EACH_INTERFACE(INIT_SYMBOL)
FOR_EACH_INTERFACE_VOID(INIT_SYMBOL_VOID)
#undef INIT_SYMBOL
#undef INIT_SYMBOL_VOID
initialize(aArgc, aArgv);
}
// Record/replay API functions can't GC, but we can't use
// JS::AutoSuppressGCAnalysis here due to linking issues.
struct AutoSuppressGCAnalysis {
AutoSuppressGCAnalysis() {}
~AutoSuppressGCAnalysis() {
#ifdef DEBUG
// Need nontrivial destructor.
static Atomic<int, SequentiallyConsistent, Behavior::DontPreserve> dummy;
dummy++;
#endif
}
} JS_HAZ_GC_SUPPRESSED;
#define DEFINE_WRAPPER(aName, aReturnType, aFormals, aActuals) \
aReturnType aName aFormals { \
AutoSuppressGCAnalysis suppress; \
MOZ_ASSERT(IsRecordingOrReplaying() || IsMiddleman()); \
return gPtr##aName aActuals; \
}
#define DEFINE_WRAPPER_VOID(aName, aFormals, aActuals) \
void aName aFormals { \
AutoSuppressGCAnalysis suppress; \
MOZ_ASSERT(IsRecordingOrReplaying() || IsMiddleman()); \
gPtr##aName aActuals; \
}
FOR_EACH_INTERFACE(DEFINE_WRAPPER)
FOR_EACH_INTERFACE_VOID(DEFINE_WRAPPER_VOID)
#undef DEFINE_WRAPPER
#undef DEFINE_WRAPPER_VOID
#ifdef ENABLE_RECORD_REPLAY
bool gIsRecordingOrReplaying;
bool gIsRecording;
bool gIsReplaying;
bool gIsMiddleman;
#endif // ENABLE_RECORD_REPLAY
#undef ENABLE_RECORD_REPLAY
} // namespace recordreplay
} // namespace mozilla