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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
//
// Autogenerated from Python template. Hands off.
//
#include <stdlib.h>
#include <string.h>
#include "IPDLUnitTests.h"
#include "base/command_line.h"
#include "base/string_util.h"
#include "base/task.h"
#include "base/thread.h"
#include "nsRegion.h"
#include "IPDLUnitTestSubprocess.h"
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${INCLUDES}
//-----------------------------------------------------------------------------
// clang-format on
using namespace std;
using base::Thread;
namespace mozilla {
namespace _ipdltest {
void* gParentActor;
IPDLUnitTestSubprocess* gSubprocess;
void* gChildActor;
// Note: in threaded mode, this will be non-null (for both parent and
// child, since they share one set of globals).
Thread* gChildThread;
MessageLoop* gParentMessageLoop;
bool gParentDone;
bool gChildDone;
void DeleteChildActor();
//-----------------------------------------------------------------------------
// data/functions accessed by both parent and child processes
char* gIPDLUnitTestName = nullptr;
const char* IPDLUnitTestName() {
if (!gIPDLUnitTestName) {
#if defined(XP_WIN)
vector<wstring> args = CommandLine::ForCurrentProcess()->GetLooseValues();
gIPDLUnitTestName = ::strdup(WideToUTF8(args[0]).c_str());
#else
vector<string> argv = CommandLine::ForCurrentProcess()->argv();
gIPDLUnitTestName = ::moz_xstrdup(argv[1].c_str());
#endif
}
return gIPDLUnitTestName;
}
} // namespace _ipdltest
} // namespace mozilla
namespace {
enum IPDLUnitTestType {
NoneTest = 0,
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${ENUM_VALUES}
LastTest = ${LAST_ENUM}
//-----------------------------------------------------------------------------
//clang-format on
};
IPDLUnitTestType IPDLUnitTestFromString(const char* const aString) {
if (!aString) return static_cast<IPDLUnitTestType>(0);
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${STRING_TO_ENUMS}
//-----------------------------------------------------------------------------
// clang-format on
else return static_cast<IPDLUnitTestType>(0);
}
IPDLUnitTestType IPDLUnitTest() {
return IPDLUnitTestFromString(::mozilla::_ipdltest::IPDLUnitTestName());
}
} // namespace
//-----------------------------------------------------------------------------
// parent process only
namespace mozilla {
namespace _ipdltest {
void DeferredParentShutdown();
void IPDLUnitTestThreadMain(char* testString);
void IPDLUnitTestMain(void* aData) {
char* testString = reinterpret_cast<char*>(aData);
// Check if we are to run the test using threads instead:
const char* prefix = "thread:";
const int prefixLen = strlen(prefix);
if (!strncmp(testString, prefix, prefixLen)) {
IPDLUnitTestThreadMain(testString + prefixLen);
return;
}
IPDLUnitTestType test = IPDLUnitTestFromString(testString);
if (!test) {
// use this instead of |fail()| because we don't know what the test is
fprintf(stderr, MOZ_IPDL_TESTFAIL_LABEL "| %s | unknown unit test %s\n",
"<--->", testString);
MOZ_CRASH("can't continue");
}
gIPDLUnitTestName = testString;
// Check whether this test is enabled for processes:
switch (test) {
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${PARENT_ENABLED_CASES_PROC}
//-----------------------------------------------------------------------------
// clang-format on
default:
fail("not reached");
return; // unreached
}
printf(MOZ_IPDL_TESTINFO_LABEL "| running test | %s\n", gIPDLUnitTestName);
std::vector<std::string> testCaseArgs;
testCaseArgs.push_back(testString);
gSubprocess = new IPDLUnitTestSubprocess();
if (!gSubprocess->SyncLaunch(testCaseArgs))
fail("problem launching subprocess");
IPC::Channel* transport = gSubprocess->GetChannel();
if (!transport) fail("no transport");
base::ProcessId child = base::GetProcId(gSubprocess->GetChildProcessHandle());
switch (test) {
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${PARENT_MAIN_CASES_PROC}
//-----------------------------------------------------------------------------
// clang-format on
default:
fail("not reached");
return; // unreached
}
}
void IPDLUnitTestThreadMain(char* testString) {
IPDLUnitTestType test = IPDLUnitTestFromString(testString);
if (!test) {
// use this instead of |fail()| because we don't know what the test is
fprintf(stderr, MOZ_IPDL_TESTFAIL_LABEL "| %s | unknown unit test %s\n",
"<--->", testString);
MOZ_CRASH("can't continue");
}
gIPDLUnitTestName = testString;
// Check whether this test is enabled for threads:
switch (test) {
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${PARENT_ENABLED_CASES_THREAD}
//-----------------------------------------------------------------------------
// clang-format on
default:
fail("not reached");
return; // unreached
}
printf(MOZ_IPDL_TESTINFO_LABEL "| running test | %s\n", gIPDLUnitTestName);
std::vector<std::string> testCaseArgs;
testCaseArgs.push_back(testString);
gChildThread = new Thread("ParentThread");
if (!gChildThread->Start()) fail("starting parent thread");
gParentMessageLoop = MessageLoop::current();
MessageLoop* childMessageLoop = gChildThread->message_loop();
switch (test) {
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${PARENT_MAIN_CASES_THREAD}
//-----------------------------------------------------------------------------
// clang-format on
default:
fail("not reached");
return; // unreached
}
}
void DeleteParentActor() {
if (!gParentActor) return;
switch (IPDLUnitTest()) {
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${PARENT_DELETE_CASES}
//-----------------------------------------------------------------------------
// clang-format on
default:
::mozilla::_ipdltest::fail("???");
}
}
void QuitXPCOM() {
DeleteParentActor();
static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
nsCOMPtr<nsIAppShell> appShell(do_GetService(kAppShellCID));
appShell->Exit();
}
void DeleteSubprocess(MessageLoop* uiLoop) {
// pong to QuitXPCOM
gSubprocess->Destroy();
gSubprocess = nullptr;
uiLoop->PostTask(NewRunnableFunction("QuitXPCOM", QuitXPCOM));
}
void DeferredParentShutdown() {
// ping to DeleteSubprocess
XRE_GetIOMessageLoop()->PostTask(NewRunnableFunction(
"DeleteSubprocess", DeleteSubprocess, MessageLoop::current()));
}
void TryThreadedShutdown() {
// Stop if either:
// - the child has not finished,
// - the parent has not finished,
// - or this code has already executed.
// Remember: this TryThreadedShutdown() task is enqueued
// by both parent and child (though always on parent's msg loop).
if (!gChildDone || !gParentDone || !gChildThread) return;
delete gChildThread;
gChildThread = 0;
DeferredParentShutdown();
}
void ChildCompleted() {
// Executes on the parent message loop once child has completed.
gChildDone = true;
TryThreadedShutdown();
}
void QuitParent() {
if (gChildThread) {
gParentDone = true;
MessageLoop::current()->PostTask(
NewRunnableFunction("TryThreadedShutdown", TryThreadedShutdown));
} else {
// defer "real" shutdown to avoid *Channel::Close() racing with the
// deletion of the subprocess
MessageLoop::current()->PostTask(
NewRunnableFunction("DeferredParentShutdown", DeferredParentShutdown));
}
}
static void ChildDie() {
DeleteChildActor();
XRE_ShutdownChildProcess();
}
void QuitChild() {
if (gChildThread) { // Threaded-mode test
gParentMessageLoop->PostTask(
NewRunnableFunction("ChildCompleted", ChildCompleted));
} else { // Process-mode test
MessageLoop::current()->PostTask(NewRunnableFunction("ChildDie", ChildDie));
}
}
} // namespace _ipdltest
} // namespace mozilla
//-----------------------------------------------------------------------------
// child process only
namespace mozilla {
namespace _ipdltest {
void DeleteChildActor() {
if (!gChildActor) return;
switch (IPDLUnitTest()) {
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${CHILD_DELETE_CASES}
//-----------------------------------------------------------------------------
// clang-format on
default:
::mozilla::_ipdltest::fail("???");
}
}
void IPDLUnitTestChildInit(IPC::Channel* transport, base::ProcessId parentPid,
MessageLoop* worker) {
switch (IPDLUnitTest()) {
// clang-format off
//-----------------------------------------------------------------------------
//===== TEMPLATED =====
${CHILD_INIT_CASES}
//-----------------------------------------------------------------------------
// clang-format on
default:
fail("not reached");
return; // unreached
}
}
} // namespace _ipdltest
} // namespace mozilla
|