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
|
/* -*- 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 https://mozilla.org/MPL/2.0/. */
#define MOZ_USE_LAUNCHER_ERROR
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include "SameBinary.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/CmdLineAndEnvUtils.h"
#include "mozilla/NativeNt.h"
#include "mozilla/Unused.h"
#include "mozilla/Vector.h"
#include "mozilla/WinHeaderOnlyUtils.h"
#include "nsWindowsHelpers.h"
#define EXPECT_SAMEBINARY_IS(expected, option, message) \
do { \
mozilla::LauncherResult<bool> isSame = \
mozilla::IsSameBinaryAsParentProcess(option); \
if (isSame.isErr()) { \
PrintLauncherError(isSame, \
"IsSameBinaryAsParentProcess returned error " \
"when we were expecting success."); \
return 1; \
} \
if (isSame.unwrap() != expected) { \
PrintErrorMsg(message); \
return 1; \
} \
} while (0)
/**
* This test involves three processes:
* 1. The "Monitor" process, which is executed by |MonitorMain|. This process
* is responsible for integrating with the test harness, so it spawns the
* "Parent" process (2), and then waits for the other two processes to
* finish.
* 2. The "Parent" process, which is executed by |ParentMain|. This process
* creates the "Child" process (3) and then waits indefinitely.
* 3. The "Child" process, which is executed by |ChildMain| and carries out
* the actual test. It terminates the Parent process during its execution,
* using the Child PID as the Parent process's exit code. This serves as a
* hacky yet effective way to signal to the Monitor process which PID it
* should wait on to ensure that the Child process has exited.
*/
static const char kMsgStart[] = "TEST-FAILED | SameBinary | ";
inline void PrintErrorMsg(const char* aMsg) {
printf("%s%s\n", kMsgStart, aMsg);
}
inline void PrintWinError(const char* aMsg) {
mozilla::WindowsError err(mozilla::WindowsError::FromLastError());
printf("%s%s: %S\n", kMsgStart, aMsg, err.AsString().get());
}
template <typename T>
inline void PrintLauncherError(const mozilla::LauncherResult<T>& aResult,
const char* aMsg = nullptr) {
const char* const kSep = aMsg ? ": " : "";
const char* msg = aMsg ? aMsg : "";
const mozilla::LauncherError& err = aResult.inspectErr();
printf("%s%s%s%S (%s:%d)\n", kMsgStart, msg, kSep,
err.mError.AsString().get(), err.mFile, err.mLine);
}
static int ChildMain(DWORD aExpectedParentPid) {
mozilla::LauncherResult<DWORD> parentPid = mozilla::nt::GetParentProcessId();
if (parentPid.isErr()) {
PrintLauncherError(parentPid);
return 1;
}
if (parentPid.inspect() != aExpectedParentPid) {
PrintErrorMsg("Unexpected mismatch of parent PIDs");
return 1;
}
const DWORD kAccess = PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
nsAutoHandle parentProcess(
::OpenProcess(kAccess, FALSE, parentPid.inspect()));
if (!parentProcess) {
PrintWinError("Unexpectedly failed to call OpenProcess on parent");
return 1;
}
EXPECT_SAMEBINARY_IS(
true, mozilla::ImageFileCompareOption::Default,
"IsSameBinaryAsParentProcess returned incorrect result for identical "
"binaries");
EXPECT_SAMEBINARY_IS(
true, mozilla::ImageFileCompareOption::CompareNtPathsOnly,
"IsSameBinaryAsParentProcess(CompareNtPathsOnly) returned incorrect "
"result for identical binaries");
// Total hack, but who cares? We'll set the parent's exit code as our PID
// so that the monitor process knows who to wait for!
if (!::TerminateProcess(parentProcess.get(), ::GetCurrentProcessId())) {
PrintWinError("Unexpected failure in TerminateProcess");
return 1;
}
// Close our handle to the parent process so that no references are held.
::CloseHandle(parentProcess.disown());
// Querying a pid on a terminated process may still succeed some time after
// that process has been terminated. For the purposes of this test, we'll poll
// the OS until we cannot succesfully open the parentPid anymore.
const uint32_t kMaxAttempts = 100;
uint32_t curAttempt = 0;
while (HANDLE p = ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE,
parentPid.inspect())) {
::CloseHandle(p);
::Sleep(100);
++curAttempt;
if (curAttempt >= kMaxAttempts) {
PrintErrorMsg(
"Exhausted retry attempts waiting for parent pid to become invalid");
return 1;
}
}
EXPECT_SAMEBINARY_IS(
false, mozilla::ImageFileCompareOption::Default,
"IsSameBinaryAsParentProcess returned incorrect result for dead parent "
"process");
EXPECT_SAMEBINARY_IS(
false, mozilla::ImageFileCompareOption::CompareNtPathsOnly,
"IsSameBinaryAsParentProcess(CompareNtPathsOnly) returned incorrect "
"result for dead parent process");
return 0;
}
static nsReturnRef<HANDLE> CreateSelfProcess(int argc, wchar_t* argv[]) {
nsAutoHandle empty;
DWORD myPid = ::GetCurrentProcessId();
wchar_t strPid[11] = {};
#if defined(__MINGW32__)
_ultow(myPid, strPid, 16);
#else
if (_ultow_s(myPid, strPid, 16)) {
PrintErrorMsg("_ultow_s failed");
return empty.out();
}
#endif // defined(__MINGW32__)
wchar_t* extraArgs[] = {strPid};
auto cmdLine = mozilla::MakeCommandLine(
argc, argv, mozilla::ArrayLength(extraArgs), extraArgs);
if (!cmdLine) {
PrintErrorMsg("MakeCommandLine failed");
return empty.out();
}
STARTUPINFOW si = {sizeof(si)};
PROCESS_INFORMATION pi;
BOOL ok =
::CreateProcessW(argv[0], cmdLine.get(), nullptr, nullptr, FALSE,
CREATE_UNICODE_ENVIRONMENT, nullptr, nullptr, &si, &pi);
if (!ok) {
PrintWinError("CreateProcess failed");
return empty.out();
}
nsAutoHandle proc(pi.hProcess);
nsAutoHandle thd(pi.hThread);
return proc.out();
}
static int ParentMain(int argc, wchar_t* argv[]) {
nsAutoHandle childProc(CreateSelfProcess(argc, argv));
if (!childProc) {
return 1;
}
if (::WaitForSingleObject(childProc.get(), INFINITE) != WAIT_OBJECT_0) {
PrintWinError(
"Unexpected result from WaitForSingleObject on child process");
return 1;
}
MOZ_ASSERT_UNREACHABLE("This process should be terminated by now");
return 0;
}
static int MonitorMain(int argc, wchar_t* argv[]) {
// In this process, "parent" means the process that will be running
// ParentMain, which is our child process (confusing, I know...)
nsAutoHandle parentProc(CreateSelfProcess(argc, argv));
if (!parentProc) {
return 1;
}
if (::WaitForSingleObject(parentProc.get(), 60000) != WAIT_OBJECT_0) {
PrintWinError("Unexpected result from WaitForSingleObject on parent");
return 1;
}
DWORD childPid;
if (!::GetExitCodeProcess(parentProc.get(), &childPid)) {
PrintWinError("GetExitCodeProcess failed");
return 1;
}
nsAutoHandle childProc(::OpenProcess(SYNCHRONIZE, FALSE, childPid));
if (!childProc) {
// Nothing to wait on anymore, which is OK.
return 0;
}
// We want no more references to parentProc
::CloseHandle(parentProc.disown());
if (::WaitForSingleObject(childProc.get(), 60000) != WAIT_OBJECT_0) {
PrintWinError("Unexpected result from WaitForSingleObject on child");
return 1;
}
return 0;
}
extern "C" int wmain(int argc, wchar_t* argv[]) {
if (argc == 3) {
return ChildMain(wcstoul(argv[2], nullptr, 16));
}
if (!mozilla::SetArgv0ToFullBinaryPath(argv)) {
return 1;
}
if (argc == 1) {
return MonitorMain(argc, argv);
}
if (argc == 2) {
return ParentMain(argc, argv);
}
PrintErrorMsg("Unexpected argc");
return 1;
}
|