Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/ipc/GPUProcessHost.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "GPUProcessHost.h"
8
#include "chrome/common/process_watcher.h"
9
#include "gfxPrefs.h"
10
#include "mozilla/gfx/Logging.h"
11
#include "nsITimer.h"
12
#include "mozilla/Preferences.h"
13
14
namespace mozilla {
15
namespace gfx {
16
17
using namespace ipc;
18
19
GPUProcessHost::GPUProcessHost(Listener* aListener)
20
 : GeckoChildProcessHost(GeckoProcessType_GPU),
21
   mListener(aListener),
22
   mTaskFactory(this),
23
   mLaunchPhase(LaunchPhase::Unlaunched),
24
   mProcessToken(0),
25
   mShutdownRequested(false),
26
   mChannelClosed(false)
27
0
{
28
0
  MOZ_COUNT_CTOR(GPUProcessHost);
29
0
}
30
31
GPUProcessHost::~GPUProcessHost()
32
0
{
33
0
  MOZ_COUNT_DTOR(GPUProcessHost);
34
0
}
35
36
bool
37
GPUProcessHost::Launch(StringVector aExtraOpts)
38
0
{
39
0
  MOZ_ASSERT(mLaunchPhase == LaunchPhase::Unlaunched);
40
0
  MOZ_ASSERT(!mGPUChild);
41
0
  MOZ_ASSERT(!gfxPlatform::IsHeadless());
42
0
43
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
44
  mSandboxLevel = Preferences::GetInt("security.sandbox.gpu.level");
45
#endif
46
47
0
  mLaunchPhase = LaunchPhase::Waiting;
48
0
  mLaunchTime = TimeStamp::Now();
49
0
50
0
  if (!GeckoChildProcessHost::AsyncLaunch(aExtraOpts)) {
51
0
    mLaunchPhase = LaunchPhase::Complete;
52
0
    return false;
53
0
  }
54
0
  return true;
55
0
}
56
57
bool
58
GPUProcessHost::WaitForLaunch()
59
0
{
60
0
  if (mLaunchPhase == LaunchPhase::Complete) {
61
0
    return !!mGPUChild;
62
0
  }
63
0
64
0
  int32_t timeoutMs = gfxPrefs::GPUProcessTimeoutMs();
65
0
66
0
  // If one of the following environment variables are set we can effectively
67
0
  // ignore the timeout - as we can guarantee the compositor process will be terminated
68
0
  if (PR_GetEnv("MOZ_DEBUG_CHILD_PROCESS") || PR_GetEnv("MOZ_DEBUG_CHILD_PAUSE")) {
69
0
    timeoutMs = 0;
70
0
  }
71
0
72
0
  // Our caller expects the connection to be finished after we return, so we
73
0
  // immediately set up the IPDL actor and fire callbacks. The IO thread will
74
0
  // still dispatch a notification to the main thread - we'll just ignore it.
75
0
  bool result = GeckoChildProcessHost::WaitUntilConnected(timeoutMs);
76
0
  InitAfterConnect(result);
77
0
  return result;
78
0
}
79
80
void
81
GPUProcessHost::OnChannelConnected(int32_t peer_pid)
82
0
{
83
0
  MOZ_ASSERT(!NS_IsMainThread());
84
0
85
0
  GeckoChildProcessHost::OnChannelConnected(peer_pid);
86
0
87
0
  // Post a task to the main thread. Take the lock because mTaskFactory is not
88
0
  // thread-safe.
89
0
  RefPtr<Runnable> runnable;
90
0
  {
91
0
    MonitorAutoLock lock(mMonitor);
92
0
    runnable = mTaskFactory.NewRunnableMethod(&GPUProcessHost::OnChannelConnectedTask);
93
0
  }
94
0
  NS_DispatchToMainThread(runnable);
95
0
}
96
97
void
98
GPUProcessHost::OnChannelError()
99
0
{
100
0
  MOZ_ASSERT(!NS_IsMainThread());
101
0
102
0
  GeckoChildProcessHost::OnChannelError();
103
0
104
0
  // Post a task to the main thread. Take the lock because mTaskFactory is not
105
0
  // thread-safe.
106
0
  RefPtr<Runnable> runnable;
107
0
  {
108
0
    MonitorAutoLock lock(mMonitor);
109
0
    runnable = mTaskFactory.NewRunnableMethod(&GPUProcessHost::OnChannelErrorTask);
110
0
  }
111
0
  NS_DispatchToMainThread(runnable);
112
0
}
113
114
void
115
GPUProcessHost::OnChannelConnectedTask()
116
0
{
117
0
  if (mLaunchPhase == LaunchPhase::Waiting) {
118
0
    InitAfterConnect(true);
119
0
  }
120
0
}
121
122
void
123
GPUProcessHost::OnChannelErrorTask()
124
0
{
125
0
  if (mLaunchPhase == LaunchPhase::Waiting) {
126
0
    InitAfterConnect(false);
127
0
  }
128
0
}
129
130
static uint64_t sProcessTokenCounter = 0;
131
132
void
133
GPUProcessHost::InitAfterConnect(bool aSucceeded)
134
0
{
135
0
  MOZ_ASSERT(mLaunchPhase == LaunchPhase::Waiting);
136
0
  MOZ_ASSERT(!mGPUChild);
137
0
138
0
  mLaunchPhase = LaunchPhase::Complete;
139
0
140
0
  if (aSucceeded) {
141
0
    mProcessToken = ++sProcessTokenCounter;
142
0
    mGPUChild = MakeUnique<GPUChild>(this);
143
0
    DebugOnly<bool> rv =
144
0
      mGPUChild->Open(GetChannel(), base::GetProcId(GetChildProcessHandle()));
145
0
    MOZ_ASSERT(rv);
146
0
147
0
    mGPUChild->Init();
148
0
  }
149
0
150
0
  if (mListener) {
151
0
    mListener->OnProcessLaunchComplete(this);
152
0
  }
153
0
}
154
155
void
156
GPUProcessHost::Shutdown()
157
0
{
158
0
  MOZ_ASSERT(!mShutdownRequested);
159
0
160
0
  mListener = nullptr;
161
0
162
0
  if (mGPUChild) {
163
0
    mGPUChild->SendShutdownVR();
164
0
165
0
    // OnChannelClosed uses this to check if the shutdown was expected or
166
0
    // unexpected.
167
0
    mShutdownRequested = true;
168
0
169
0
    // The channel might already be closed if we got here unexpectedly.
170
0
    if (!mChannelClosed) {
171
0
      mGPUChild->Close();
172
0
    }
173
0
174
0
#ifndef NS_FREE_PERMANENT_DATA
175
0
    // No need to communicate shutdown, the GPU process doesn't need to
176
0
    // communicate anything back.
177
0
    KillHard("NormalShutdown");
178
0
#endif
179
0
180
0
    // If we're shutting down unexpectedly, we're in the middle of handling an
181
0
    // ActorDestroy for PGPUChild, which is still on the stack. We'll return
182
0
    // back to OnChannelClosed.
183
0
    //
184
0
    // Otherwise, we'll wait for OnChannelClose to be called whenever PGPUChild
185
0
    // acknowledges shutdown.
186
0
    return;
187
0
  }
188
0
189
0
  DestroyProcess();
190
0
}
191
192
void
193
GPUProcessHost::OnChannelClosed()
194
0
{
195
0
  mChannelClosed = true;
196
0
197
0
  if (!mShutdownRequested && mListener) {
198
0
    // This is an unclean shutdown. Notify our listener that we're going away.
199
0
    mListener->OnProcessUnexpectedShutdown(this);
200
0
  } else {
201
0
    DestroyProcess();
202
0
  }
203
0
204
0
  // Release the actor.
205
0
  GPUChild::Destroy(std::move(mGPUChild));
206
0
  MOZ_ASSERT(!mGPUChild);
207
0
}
208
209
void
210
GPUProcessHost::KillHard(const char* aReason)
211
0
{
212
0
  ProcessHandle handle = GetChildProcessHandle();
213
0
  if (!base::KillProcess(handle, base::PROCESS_END_KILLED_BY_USER, false)) {
214
0
    NS_WARNING("failed to kill subprocess!");
215
0
  }
216
0
217
0
  SetAlreadyDead();
218
0
}
219
220
uint64_t
221
GPUProcessHost::GetProcessToken() const
222
0
{
223
0
  return mProcessToken;
224
0
}
225
226
static void
227
DelayedDeleteSubprocess(GeckoChildProcessHost* aSubprocess)
228
0
{
229
0
  XRE_GetIOMessageLoop()->
230
0
    PostTask(mozilla::MakeAndAddRef<DeleteTask<GeckoChildProcessHost>>(aSubprocess));
231
0
}
232
233
void
234
GPUProcessHost::KillProcess()
235
0
{
236
0
  KillHard("DiagnosticKill");
237
0
}
238
239
void
240
GPUProcessHost::DestroyProcess()
241
0
{
242
0
  // Cancel all tasks. We don't want anything triggering after our caller
243
0
  // expects this to go away.
244
0
  {
245
0
    MonitorAutoLock lock(mMonitor);
246
0
    mTaskFactory.RevokeAll();
247
0
  }
248
0
249
0
  MessageLoop::current()->
250
0
    PostTask(NewRunnableFunction("DestroyProcessRunnable", DelayedDeleteSubprocess, this));
251
0
}
252
253
} // namespace gfx
254
} // namespace mozilla