• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stdlib.h>
6 
7 #if defined(OS_WIN)
8 #include <dwmapi.h>
9 #include <windows.h>
10 #endif
11 
12 #include "base/debug/trace_event.h"
13 #include "base/lazy_instance.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/metrics/histogram.h"
16 #include "base/rand_util.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/threading/platform_thread.h"
20 #include "build/build_config.h"
21 #include "content/child/child_process.h"
22 #include "content/common/content_constants_internal.h"
23 #include "content/common/gpu/gpu_config.h"
24 #include "content/common/gpu/gpu_messages.h"
25 #include "content/common/sandbox_linux/sandbox_linux.h"
26 #include "content/gpu/gpu_child_thread.h"
27 #include "content/gpu/gpu_process.h"
28 #include "content/gpu/gpu_watchdog_thread.h"
29 #include "content/public/common/content_client.h"
30 #include "content/public/common/content_switches.h"
31 #include "content/public/common/main_function_params.h"
32 #include "gpu/command_buffer/service/gpu_switches.h"
33 #include "gpu/config/gpu_info_collector.h"
34 #include "gpu/config/gpu_util.h"
35 #include "ui/events/platform/platform_event_source.h"
36 #include "ui/gl/gl_implementation.h"
37 #include "ui/gl/gl_surface.h"
38 #include "ui/gl/gl_switches.h"
39 #include "ui/gl/gpu_switching_manager.h"
40 
41 #if defined(OS_WIN)
42 #include "base/win/windows_version.h"
43 #include "base/win/scoped_com_initializer.h"
44 #include "sandbox/win/src/sandbox.h"
45 #endif
46 
47 #if defined(USE_X11)
48 #include "ui/base/x/x11_util.h"
49 #endif
50 
51 #if defined(OS_LINUX)
52 #include "content/public/common/sandbox_init.h"
53 #endif
54 
55 #if defined(OS_MACOSX)
56 #include "base/message_loop/message_pump_mac.h"
57 #endif
58 
59 #if defined(ADDRESS_SANITIZER)
60 #include <sanitizer/asan_interface.h>
61 #endif
62 
63 const int kGpuTimeout = 10000;
64 
65 namespace content {
66 
67 namespace {
68 
69 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
70                                const CommandLine& command_line);
71 bool WarmUpSandbox(const CommandLine& command_line);
72 
73 #if !defined(OS_MACOSX)
74 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info);
75 #endif
76 
77 #if defined(OS_LINUX)
78 #if !defined(OS_CHROMEOS)
79 bool CanAccessNvidiaDeviceFile();
80 #endif
81 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
82 #elif defined(OS_WIN)
83 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
84 #endif
85 
86 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
87     LAZY_INSTANCE_INITIALIZER;
88 
GpuProcessLogMessageHandler(int severity,const char * file,int line,size_t message_start,const std::string & str)89 bool GpuProcessLogMessageHandler(int severity,
90                                  const char* file, int line,
91                                  size_t message_start,
92                                  const std::string& str) {
93   std::string header = str.substr(0, message_start);
94   std::string message = str.substr(message_start);
95   deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
96       severity, header, message));
97   return false;
98 }
99 
100 }  // namespace anonymous
101 
102 // Main function for starting the Gpu process.
GpuMain(const MainFunctionParams & parameters)103 int GpuMain(const MainFunctionParams& parameters) {
104   TRACE_EVENT0("gpu", "GpuMain");
105   base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process");
106   base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
107       kTraceEventGpuProcessSortIndex);
108 
109   const CommandLine& command_line = parameters.command_line;
110   if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
111     ChildProcess::WaitForDebugger("Gpu");
112   }
113 
114   base::Time start_time = base::Time::Now();
115 
116 #if defined(OS_WIN)
117   // Prevent Windows from displaying a modal dialog on failures like not being
118   // able to load a DLL.
119   SetErrorMode(
120       SEM_FAILCRITICALERRORS |
121       SEM_NOGPFAULTERRORBOX |
122       SEM_NOOPENFILEERRORBOX);
123 #elif defined(USE_X11)
124   ui::SetDefaultX11ErrorHandlers();
125 #endif
126 
127   logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
128 
129   if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
130     std::string types = command_line.GetSwitchValueASCII(
131         switches::kGpuDriverBugWorkarounds);
132     std::set<int> workarounds;
133     gpu::StringToFeatureSet(types, &workarounds);
134     if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
135       ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
136     else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
137       ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
138   }
139 
140   // Initialization of the OpenGL bindings may fail, in which case we
141   // will need to tear down this process. However, we can not do so
142   // safely until the IPC channel is set up, because the detection of
143   // early return of a child process is implemented using an IPC
144   // channel error. If the IPC channel is not fully set up between the
145   // browser and GPU process, and the GPU process crashes or exits
146   // early, the browser process will never detect it.  For this reason
147   // we defer tearing down the GPU process until receiving the
148   // GpuMsg_Initialize message from the browser.
149   bool dead_on_arrival = false;
150 
151 #if defined(OS_WIN)
152   base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO;
153   // Unless we're running on desktop GL, we don't need a UI message
154   // loop, so avoid its use to work around apparent problems with some
155   // third-party software.
156   if (command_line.HasSwitch(switches::kUseGL) &&
157       command_line.GetSwitchValueASCII(switches::kUseGL) ==
158           gfx::kGLImplementationDesktopName) {
159     message_loop_type = base::MessageLoop::TYPE_UI;
160   }
161   base::MessageLoop main_message_loop(message_loop_type);
162 #elif defined(OS_LINUX) && defined(USE_X11)
163   // We need a UI loop so that we can grab the Expose events. See GLSurfaceGLX
164   // and https://crbug.com/326995.
165   base::MessageLoop main_message_loop(base::MessageLoop::TYPE_UI);
166   scoped_ptr<ui::PlatformEventSource> event_source =
167       ui::PlatformEventSource::CreateDefault();
168 #elif defined(OS_LINUX)
169   base::MessageLoop main_message_loop(base::MessageLoop::TYPE_DEFAULT);
170 #elif defined(OS_MACOSX)
171   // This is necessary for CoreAnimation layers hosted in the GPU process to be
172   // drawn. See http://crbug.com/312462.
173   scoped_ptr<base::MessagePump> pump(new base::MessagePumpCFRunLoop());
174   base::MessageLoop main_message_loop(pump.Pass());
175 #else
176   base::MessageLoop main_message_loop(base::MessageLoop::TYPE_IO);
177 #endif
178 
179   base::PlatformThread::SetName("CrGpuMain");
180 
181   // In addition to disabling the watchdog if the command line switch is
182   // present, disable the watchdog on valgrind because the code is expected
183   // to run slowly in that case.
184   bool enable_watchdog =
185       !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
186       !RunningOnValgrind();
187 
188   // Disable the watchdog in debug builds because they tend to only be run by
189   // developers who will not appreciate the watchdog killing the GPU process.
190 #ifndef NDEBUG
191   enable_watchdog = false;
192 #endif
193 
194   bool delayed_watchdog_enable = false;
195 
196 #if defined(OS_CHROMEOS)
197   // Don't start watchdog immediately, to allow developers to switch to VT2 on
198   // startup.
199   delayed_watchdog_enable = true;
200 #endif
201 
202   scoped_refptr<GpuWatchdogThread> watchdog_thread;
203 
204   // Start the GPU watchdog only after anything that is expected to be time
205   // consuming has completed, otherwise the process is liable to be aborted.
206   if (enable_watchdog && !delayed_watchdog_enable) {
207     watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
208     watchdog_thread->Start();
209   }
210 
211   gpu::GPUInfo gpu_info;
212   // Get vendor_id, device_id, driver_version from browser process through
213   // commandline switches.
214   GetGpuInfoFromCommandLine(gpu_info, command_line);
215 
216   base::TimeDelta collect_context_time;
217   base::TimeDelta initialize_one_off_time;
218 
219   // Warm up resources that don't need access to GPUInfo.
220   if (WarmUpSandbox(command_line)) {
221 #if defined(OS_LINUX)
222     bool initialized_sandbox = false;
223     bool initialized_gl_context = false;
224     bool should_initialize_gl_context = false;
225 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
226     // On Chrome OS ARM Mali, GPU driver userspace creates threads when
227     // initializing a GL context, so start the sandbox early.
228     if (!command_line.HasSwitch(
229              switches::kGpuSandboxStartAfterInitialization)) {
230       gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
231                                              should_initialize_gl_context);
232       initialized_sandbox = true;
233     }
234 #endif
235 #endif  // defined(OS_LINUX)
236 
237     base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
238 
239     // Determine if we need to initialize GL here or it has already been done.
240     bool gl_already_initialized = false;
241 #if defined(OS_MACOSX)
242     if (!command_line.HasSwitch(switches::kNoSandbox)) {
243       // On Mac, if the sandbox is enabled, then GLSurface::InitializeOneOff()
244       // is called from the sandbox warmup code before getting here.
245       gl_already_initialized = true;
246     }
247 #endif
248     if (command_line.HasSwitch(switches::kInProcessGPU)) {
249       // With in-process GPU, GLSurface::InitializeOneOff() is called from
250       // GpuChildThread before getting here.
251       gl_already_initialized = true;
252     }
253 
254     // Load and initialize the GL implementation and locate the GL entry points.
255     bool gl_initialized =
256         gl_already_initialized
257             ? gfx::GetGLImplementation() != gfx::kGLImplementationNone
258             : gfx::GLSurface::InitializeOneOff();
259     if (gl_initialized) {
260       // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
261       // purposes. However, on Mac we don't actually use them. As documented in
262       // crbug.com/222934, due to some driver issues, glGetString could take
263       // multiple seconds to finish, which in turn cause the GPU process to
264       // crash.
265       // By skipping the following code on Mac, we don't really lose anything,
266       // because the basic GPU information is passed down from browser process
267       // and we already registered them through SetGpuInfo() above.
268       base::TimeTicks before_collect_context_graphics_info =
269           base::TimeTicks::Now();
270 #if !defined(OS_MACOSX)
271       if (!CollectGraphicsInfo(gpu_info))
272         dead_on_arrival = true;
273 
274 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
275       // Recompute gpu driver bug workarounds - this is specifically useful
276       // on systems where vendor_id/device_id aren't available.
277       if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
278         gpu::ApplyGpuDriverBugWorkarounds(
279             gpu_info, const_cast<CommandLine*>(&command_line));
280       }
281 #endif
282 
283 #if defined(OS_LINUX)
284       initialized_gl_context = true;
285 #if !defined(OS_CHROMEOS)
286       if (gpu_info.gpu.vendor_id == 0x10de &&  // NVIDIA
287           gpu_info.driver_vendor == "NVIDIA" &&
288           !CanAccessNvidiaDeviceFile())
289         dead_on_arrival = true;
290 #endif  // !defined(OS_CHROMEOS)
291 #endif  // defined(OS_LINUX)
292 #endif  // !defined(OS_MACOSX)
293       collect_context_time =
294           base::TimeTicks::Now() - before_collect_context_graphics_info;
295     } else {  // gl_initialized
296       VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
297       dead_on_arrival = true;
298     }
299 
300     initialize_one_off_time =
301         base::TimeTicks::Now() - before_initialize_one_off;
302 
303     if (enable_watchdog && delayed_watchdog_enable) {
304       watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
305       watchdog_thread->Start();
306     }
307 
308     // OSMesa is expected to run very slowly, so disable the watchdog in that
309     // case.
310     if (enable_watchdog &&
311         gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
312       watchdog_thread->Stop();
313       watchdog_thread = NULL;
314     }
315 
316 #if defined(OS_LINUX)
317     should_initialize_gl_context = !initialized_gl_context &&
318                                    !dead_on_arrival;
319 
320     if (!initialized_sandbox) {
321       gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
322                                              should_initialize_gl_context);
323     }
324 #elif defined(OS_WIN)
325     gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
326 #endif
327   } else {
328     dead_on_arrival = true;
329   }
330 
331   logging::SetLogMessageHandler(NULL);
332 
333   GpuProcess gpu_process;
334 
335   // These UMA must be stored after GpuProcess is constructed as it
336   // initializes StatisticsRecorder which tracks the histograms.
337   UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo", collect_context_time);
338   UMA_HISTOGRAM_TIMES("GPU.InitializeOneOffTime", initialize_one_off_time);
339 
340   GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
341                                                     dead_on_arrival,
342                                                     gpu_info,
343                                                     deferred_messages.Get());
344   while (!deferred_messages.Get().empty())
345     deferred_messages.Get().pop();
346 
347   child_thread->Init(start_time);
348 
349   gpu_process.set_main_thread(child_thread);
350 
351   if (watchdog_thread)
352     watchdog_thread->AddPowerObserver();
353 
354   {
355     TRACE_EVENT0("gpu", "Run Message Loop");
356     main_message_loop.Run();
357   }
358 
359   child_thread->StopWatchdog();
360 
361   return 0;
362 }
363 
364 namespace {
365 
GetGpuInfoFromCommandLine(gpu::GPUInfo & gpu_info,const CommandLine & command_line)366 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
367                                const CommandLine& command_line) {
368   DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
369          command_line.HasSwitch(switches::kGpuDeviceID) &&
370          command_line.HasSwitch(switches::kGpuDriverVersion));
371   bool success = base::HexStringToUInt(
372       command_line.GetSwitchValueASCII(switches::kGpuVendorID),
373       &gpu_info.gpu.vendor_id);
374   DCHECK(success);
375   success = base::HexStringToUInt(
376       command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
377       &gpu_info.gpu.device_id);
378   DCHECK(success);
379   gpu_info.driver_vendor =
380       command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
381   gpu_info.driver_version =
382       command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
383   GetContentClient()->SetGpuInfo(gpu_info);
384 }
385 
WarmUpSandbox(const CommandLine & command_line)386 bool WarmUpSandbox(const CommandLine& command_line) {
387   {
388     TRACE_EVENT0("gpu", "Warm up rand");
389     // Warm up the random subsystem, which needs to be done pre-sandbox on all
390     // platforms.
391     (void) base::RandUint64();
392   }
393   return true;
394 }
395 
396 #if !defined(OS_MACOSX)
CollectGraphicsInfo(gpu::GPUInfo & gpu_info)397 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info) {
398   bool res = true;
399   gpu::CollectInfoResult result = gpu::CollectContextGraphicsInfo(&gpu_info);
400   switch (result) {
401     case gpu::kCollectInfoFatalFailure:
402       LOG(ERROR) << "gpu::CollectGraphicsInfo failed (fatal).";
403       res = false;
404       break;
405     case gpu::kCollectInfoNonFatalFailure:
406       VLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
407       break;
408     case gpu::kCollectInfoSuccess:
409       break;
410   }
411   GetContentClient()->SetGpuInfo(gpu_info);
412   return res;
413 }
414 #endif
415 
416 #if defined(OS_LINUX)
417 #if !defined(OS_CHROMEOS)
CanAccessNvidiaDeviceFile()418 bool CanAccessNvidiaDeviceFile() {
419   bool res = true;
420   base::ThreadRestrictions::AssertIOAllowed();
421   if (access("/dev/nvidiactl", R_OK) != 0) {
422     VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
423     res = false;
424   }
425   return res;
426 }
427 #endif
428 
CreateDummyGlContext()429 void CreateDummyGlContext() {
430   scoped_refptr<gfx::GLSurface> surface(
431       gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
432   if (!surface.get()) {
433     VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
434     return;
435   }
436 
437   // On Linux, this is needed to make sure /dev/nvidiactl has
438   // been opened and its descriptor cached.
439   scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
440       NULL, surface.get(), gfx::PreferDiscreteGpu));
441   if (!context.get()) {
442     VLOG(1) << "gfx::GLContext::CreateGLContext failed";
443     return;
444   }
445 
446   // Similarly, this is needed for /dev/nvidia0.
447   if (context->MakeCurrent(surface.get())) {
448     context->ReleaseCurrent(surface.get());
449   } else {
450     VLOG(1)  << "gfx::GLContext::MakeCurrent failed";
451   }
452 }
453 
WarmUpSandboxNvidia(const gpu::GPUInfo & gpu_info,bool should_initialize_gl_context)454 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
455                          bool should_initialize_gl_context) {
456   // We special case Optimus since the vendor_id we see may not be Nvidia.
457   bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de &&  // NVIDIA.
458                              gpu_info.driver_vendor == "NVIDIA") ||
459                             gpu_info.optimus;
460   if (uses_nvidia_driver && should_initialize_gl_context) {
461     // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
462     CreateDummyGlContext();
463   }
464 }
465 
StartSandboxLinux(const gpu::GPUInfo & gpu_info,GpuWatchdogThread * watchdog_thread,bool should_initialize_gl_context)466 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
467                        GpuWatchdogThread* watchdog_thread,
468                        bool should_initialize_gl_context) {
469   TRACE_EVENT0("gpu", "Initialize sandbox");
470 
471   bool res = false;
472 
473   WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
474 
475   if (watchdog_thread) {
476     // LinuxSandbox needs to be able to ensure that the thread
477     // has really been stopped.
478     LinuxSandbox::StopThread(watchdog_thread);
479   }
480 
481 #if defined(ADDRESS_SANITIZER)
482   const std::string sancov_file_name =
483       "gpu." + base::Uint64ToString(base::RandUint64());
484   LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance();
485   linux_sandbox->sanitizer_args()->coverage_sandboxed = 1;
486   linux_sandbox->sanitizer_args()->coverage_fd =
487       __sanitizer_maybe_open_cov_file(sancov_file_name.c_str());
488   linux_sandbox->sanitizer_args()->coverage_max_block_size = 0;
489 #endif
490 
491   // LinuxSandbox::InitializeSandbox() must always be called
492   // with only one thread.
493   res = LinuxSandbox::InitializeSandbox();
494   if (watchdog_thread) {
495     watchdog_thread->Start();
496   }
497 
498   return res;
499 }
500 #endif  // defined(OS_LINUX)
501 
502 #if defined(OS_WIN)
StartSandboxWindows(const sandbox::SandboxInterfaceInfo * sandbox_info)503 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
504   TRACE_EVENT0("gpu", "Lower token");
505 
506   // For Windows, if the target_services interface is not zero, the process
507   // is sandboxed and we must call LowerToken() before rendering untrusted
508   // content.
509   sandbox::TargetServices* target_services = sandbox_info->target_services;
510   if (target_services) {
511     target_services->LowerToken();
512     return true;
513   }
514 
515   return false;
516 }
517 #endif  // defined(OS_WIN)
518 
519 }  // namespace.
520 
521 }  // namespace content
522