blob: f1bfead6c2ecf6ed1c1a1eecc137957002469831 [file] [log] [blame]
[email protected]fd911dd2012-01-27 01:57:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d353541f2012-05-03 22:45:415#include "content/renderer/render_process_impl.h"
6
[email protected]037fce02009-01-22 01:42:157#include "build/build_config.h"
8
[email protected]037fce02009-01-22 01:42:159#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2910#include <windows.h>
11#include <objidl.h>
12#include <mlang.h>
[email protected]037fce02009-01-22 01:42:1513#endif
initial.commit09911bf2008-07-26 23:55:2914
fdorayd2233a72016-12-13 17:18:2115#include <stddef.h>
16
fdoray743f8d6d2017-02-09 15:24:2317#include <algorithm>
fdoray31cc6f82017-02-10 23:31:1018#include <utility>
fdorayd2233a72016-12-13 17:18:2119
Eric Holk1384f6d2018-01-05 00:49:3620#include "base/base_switches.h"
fdorayd2233a72016-12-13 17:18:2121#include "base/bind.h"
initial.commit09911bf2008-07-26 23:55:2922#include "base/command_line.h"
[email protected]037fce02009-01-22 01:42:1523#include "base/compiler_specific.h"
georgesak80353b52017-01-10 21:18:5124#include "base/debug/crash_logging.h"
Eric Holkdc499db2017-07-17 17:57:3525#include "base/debug/stack_trace.h"
ishell75fddc12016-04-12 14:03:1426#include "base/feature_list.h"
fdoray31cc6f82017-02-10 23:31:1027#include "base/memory/ptr_util.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0328#include "base/system/sys_info.h"
Gabriel Charette44db1422018-08-06 11:19:3329#include "base/task/task_scheduler/initialization_util.h"
fdorayd2233a72016-12-13 17:18:2130#include "base/time/time.h"
Francois Doray6d3c649692017-06-16 19:20:2531#include "content/common/task_scheduler.h"
sammc7f6c6a02017-01-30 00:53:5132#include "content/public/common/bindings_policy.h"
fdorayd2233a72016-12-13 17:18:2133#include "content/public/common/content_client.h"
bradnelsonc79f5a6f2016-10-10 18:31:1434#include "content/public/common/content_features.h"
[email protected]c08950d22011-10-13 22:20:2935#include "content/public/common/content_switches.h"
[email protected]d344114c2011-10-01 01:24:3436#include "content/public/renderer/content_renderer_client.h"
Eric Holk1384f6d2018-01-05 00:49:3637#include "services/service_manager/embedder/switches.h"
Blink Reformata30d4232018-04-07 15:31:0638#include "third_party/blink/public/web/web_frame.h"
[email protected]067f5192014-01-29 05:22:0939#include "v8/include/v8.h"
initial.commit09911bf2008-07-26 23:55:2940
georgesak80353b52017-01-10 21:18:5141#if defined(OS_WIN)
42#include "base/win/win_util.h"
43#endif
44
ishell75fddc12016-04-12 14:03:1445namespace {
46
ishell75fddc12016-04-12 14:03:1447void SetV8FlagIfFeature(const base::Feature& feature, const char* v8_flag) {
48 if (base::FeatureList::IsEnabled(feature)) {
49 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
50 }
51}
52
bradnelson2730e3512017-01-21 20:32:2153void SetV8FlagIfNotFeature(const base::Feature& feature, const char* v8_flag) {
54 if (!base::FeatureList::IsEnabled(feature)) {
55 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
56 }
57}
58
ishell75fddc12016-04-12 14:03:1459void SetV8FlagIfHasSwitch(const char* switch_name, const char* v8_flag) {
60 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
61 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
62 }
63}
64
fdoraya70d7172017-04-04 20:52:2065std::unique_ptr<base::TaskScheduler::InitParams>
66GetDefaultTaskSchedulerInitParams() {
fdoraya70d7172017-04-04 20:52:2067
fdoray743f8d6d2017-02-09 15:24:2368 constexpr int kMaxNumThreadsInBackgroundPool = 1;
fdoraye68b6f92017-02-21 19:39:4669 constexpr int kMaxNumThreadsInBackgroundBlockingPool = 1;
fdorayd2233a72016-12-13 17:18:2170 constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 2;
fdoraye68b6f92017-02-21 19:39:4671 constexpr int kMaxNumThreadsInForegroundBlockingPool = 1;
fdorayd2233a72016-12-13 17:18:2172 constexpr auto kSuggestedReclaimTime = base::TimeDelta::FromSeconds(30);
73
Jeremy Roman04f27c372017-10-27 15:20:5574 return std::make_unique<base::TaskScheduler::InitParams>(
Jeffrey Hefbf000f42017-07-26 22:44:4575 base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundPool,
fdoraya70d7172017-04-04 20:52:2076 kSuggestedReclaimTime),
Jeffrey Hefbf000f42017-07-26 22:44:4577 base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundBlockingPool,
fdoraya70d7172017-04-04 20:52:2078 kSuggestedReclaimTime),
79 base::SchedulerWorkerPoolParams(
Francois Doray6d3c649692017-06-16 19:20:2580 std::max(
81 kMaxNumThreadsInForegroundPoolLowerBound,
82 content::GetMinThreadsInRendererTaskSchedulerForegroundPool()),
fdoraya70d7172017-04-04 20:52:2083 kSuggestedReclaimTime),
Jeffrey Hefbf000f42017-07-26 22:44:4584 base::SchedulerWorkerPoolParams(kMaxNumThreadsInForegroundBlockingPool,
fdoraya70d7172017-04-04 20:52:2085 kSuggestedReclaimTime));
fdorayd2233a72016-12-13 17:18:2186}
87
Tomas Popelaafffa972018-11-13 20:42:0588#if defined(DCHECK_IS_CONFIGURABLE)
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4589void V8DcheckCallbackHandler(const char* file, int line, const char* message) {
90 // TODO(siggi): Set a crash key or a breadcrumb so the fact that we hit a
91 // V8 DCHECK gets out in the crash report.
92 ::logging::LogMessage(file, line, logging::LOG_DCHECK).stream() << message;
93}
Tomas Popelaafffa972018-11-13 20:42:0594#endif // defined(DCHECK_IS_CONFIGURABLE)
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4595
ishell75fddc12016-04-12 14:03:1496} // namespace
97
[email protected]eb398192012-10-22 20:16:1998namespace content {
99
fdoray31cc6f82017-02-10 23:31:10100RenderProcessImpl::RenderProcessImpl(
fdoraya70d7172017-04-04 20:52:20101 std::unique_ptr<base::TaskScheduler::InitParams> task_scheduler_init_params)
102 : RenderProcess("Renderer", std::move(task_scheduler_init_params)),
fdoray31cc6f82017-02-10 23:31:10103 enabled_bindings_(0) {
Tomas Popelaafffa972018-11-13 20:42:05104#if defined(DCHECK_IS_CONFIGURABLE)
Weza6ca5b92018-03-23 19:03:07105 // Some official builds ship with DCHECKs compiled in. Failing DCHECKs then
106 // are either fatal or simply log the error, based on a feature flag.
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45107 // Make sure V8 follows suit by setting a Dcheck handler that forwards to
108 // the Chrome base logging implementation.
109 v8::V8::SetDcheckErrorHandler(&V8DcheckCallbackHandler);
110
Weza6ca5b92018-03-23 19:03:07111 if (!base::FeatureList::IsEnabled(base::kDCheckIsFatalFeature)) {
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45112 // These V8 flags default on in this build configuration. This triggers
113 // additional verification and code generation, which both slows down V8,
114 // and can lead to fatal CHECKs. Turn these flags down to get something
115 // closer to V8s normal performance and behavior.
116 constexpr char kDisabledFlags[] =
117 "--noturbo_verify "
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45118 "--noturbo_verify_allocation "
119 "--nodebug_code";
120
121 v8::V8::SetFlagsFromString(kDisabledFlags, sizeof(kDisabledFlags));
122 }
Tomas Popelaafffa972018-11-13 20:42:05123#endif // defined(DCHECK_IS_CONFIGURABLE)
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45124
[email protected]35b4f0c2014-06-26 16:55:27125 if (base::SysInfo::IsLowEndDevice()) {
[email protected]067f5192014-01-29 05:22:09126 std::string optimize_flag("--optimize-for-size");
127 v8::V8::SetFlagsFromString(optimize_flag.c_str(),
128 static_cast<int>(optimize_flag.size()));
129 }
[email protected]987422f2013-10-01 10:33:31130
ishell75fddc12016-04-12 14:03:14131 SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping,
132 "--noharmony-shipping");
133 SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony");
Kouhei Ueno14d350d2018-10-01 02:36:27134
135 constexpr char kModuleFlags[] =
136 "--harmony-dynamic-import --harmony-import-meta";
137 v8::V8::SetFlagsFromString(kModuleFlags, sizeof(kModuleFlags));
138
Hannes Payer1130d5672018-06-05 07:37:31139 SetV8FlagIfFeature(features::kV8Orinoco, "--no-single-threaded-gc");
140 SetV8FlagIfNotFeature(features::kV8Orinoco, "--single-threaded-gc");
141
Michael Hablich896d52662017-10-23 15:59:57142 SetV8FlagIfFeature(features::kV8VmFuture, "--future");
143 SetV8FlagIfNotFeature(features::kV8VmFuture, "--no-future");
Clemens Hammacher0c8a15a2018-04-27 13:45:32144
Clemens Hammacherfab55102018-07-24 16:19:19145 SetV8FlagIfFeature(features::kWebAssemblyBaseline,
146 "--liftoff --wasm-tier-up");
147 SetV8FlagIfNotFeature(features::kWebAssemblyBaseline,
148 "--no-liftoff --no-wasm-tier-up");
Clemens Hammacher0c8a15a2018-04-27 13:45:32149
Ben Smith24c1e5c2018-06-20 01:09:02150 if (base::FeatureList::IsEnabled(features::kWebAssemblyThreads)) {
151 constexpr char kFlags[] =
152 "--harmony-sharedarraybuffer "
153 "--no-wasm-disable-structured-cloning "
154 "--experimental-wasm-threads";
155
156 v8::V8::SetFlagsFromString(kFlags, sizeof(kFlags));
157 } else {
158 SetV8FlagIfNotFeature(features::kWebAssembly,
159 "--wasm-disable-structured-cloning");
160 SetV8FlagIfFeature(features::kSharedArrayBuffer,
161 "--harmony-sharedarraybuffer");
162 SetV8FlagIfNotFeature(features::kSharedArrayBuffer,
163 "--no-harmony-sharedarraybuffer");
164 }
Michael Hablich896d52662017-10-23 15:59:57165
Maya Lekova4bbfdb7f2018-10-16 08:15:03166 SetV8FlagIfFeature(features::kAwaitOptimization,
167 "--harmony-await-optimization");
168 SetV8FlagIfNotFeature(features::kAwaitOptimization,
169 "--no-harmony-await-optimization");
170
Eric Holkb4f60132017-08-18 19:37:41171 SetV8FlagIfNotFeature(features::kWebAssemblyTrapHandler,
172 "--no-wasm-trap-handler");
Lei Zhang2d3ead602018-07-10 01:15:50173#if defined(OS_LINUX) && defined(ARCH_CPU_X86_64)
Eric Holkdc499db2017-07-17 17:57:35174 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
Eric Holk90f2d932018-03-21 23:07:47175 bool use_v8_signal_handler = false;
Eric Holk1384f6d2018-01-05 00:49:36176 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
177 if (!command_line->HasSwitch(
178 service_manager::switches::kDisableInProcessStackTraces)) {
179 base::debug::SetStackDumpFirstChanceCallback(v8::V8::TryHandleSignal);
180 } else if (!command_line->HasSwitch(switches::kEnableCrashReporter) &&
181 !command_line->HasSwitch(
182 switches::kEnableCrashReporterForTesting)) {
183 // If we are using WebAssembly trap handling but both Breakpad and
184 // in-process stack traces are disabled then there will be no signal
185 // handler. In this case, we fall back on V8's default handler
186 // (https://crbug.com/798150).
Eric Holk90f2d932018-03-21 23:07:47187 use_v8_signal_handler = true;
Eric Holk1384f6d2018-01-05 00:49:36188 }
Eric Holk2c28bc9e2018-04-02 21:59:44189 // TODO(eholk): report UMA stat for how often this succeeds
190 v8::V8::EnableWebAssemblyTrapHandler(use_v8_signal_handler);
Eric Holkdc499db2017-07-17 17:57:35191 }
192#endif
Andreas Haas5ed0f502018-11-06 09:05:06193#if defined(OS_WIN) && defined(ARCH_CPU_X86_64)
194 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
195 // On Windows we use the default trap handler provided by V8.
196 bool use_v8_trap_handler = true;
197 v8::V8::EnableWebAssemblyTrapHandler(use_v8_trap_handler);
198 }
199#endif
Andreas Haas3bd45322018-11-21 07:45:42200#if defined(OS_MACOSX) && defined(ARCH_CPU_X86_64)
201 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
202 // On macOS, Crashpad uses exception ports to handle signals in a different
203 // process. As we cannot just pass a callback to this other process, we ask
204 // V8 to install its own signal handler to deal with WebAssembly traps.
205 bool use_v8_signal_handler = true;
206 v8::V8::EnableWebAssemblyTrapHandler(use_v8_signal_handler);
207 }
208#endif // defined(OS_MACOSX) && defined(ARCH_CPU_X86_64)
Eric Holkdc499db2017-07-17 17:57:35209
avi83883c82014-12-23 00:08:49210 const base::CommandLine& command_line =
211 *base::CommandLine::ForCurrentProcess();
ishell75fddc12016-04-12 14:03:14212
Ross McIlroy3ba92072018-08-01 00:43:30213 if (command_line.HasSwitch(switches::kNoV8UntrustedCodeMitigations)) {
214 const char* disable_mitigations = "--no-untrusted-code-mitigations";
215 v8::V8::SetFlagsFromString(disable_mitigations,
216 strlen(disable_mitigations));
217 }
218
[email protected]396c3a462010-03-03 05:03:22219 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
[email protected]067f5192014-01-29 05:22:09220 std::string flags(
[email protected]95edc392010-07-30 22:00:38221 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
[email protected]067f5192014-01-29 05:22:09222 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
[email protected]396c3a462010-03-03 05:03:22223 }
[email protected]55dd9332013-09-04 17:17:50224
sammc7f6c6a02017-01-30 00:53:51225 if (command_line.HasSwitch(switches::kDomAutomationController))
226 enabled_bindings_ |= BINDINGS_POLICY_DOM_AUTOMATION;
227 if (command_line.HasSwitch(switches::kStatsCollectionController))
228 enabled_bindings_ |= BINDINGS_POLICY_STATS_COLLECTION;
[email protected]e68e62fa2009-02-20 02:00:04229}
230
[email protected]396c3a462010-03-03 05:03:22231RenderProcessImpl::~RenderProcessImpl() {
[email protected]396c3a462010-03-03 05:03:22232#ifndef NDEBUG
Blink Reformat1c4d759e2017-04-09 16:34:54233 int count = blink::WebFrame::InstanceCount();
[email protected]6bd867b2013-07-24 22:10:20234 if (count)
235 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
[email protected]396c3a462010-03-03 05:03:22236#endif
[email protected]e68e62fa2009-02-20 02:00:04237
[email protected]396c3a462010-03-03 05:03:22238 GetShutDownEvent()->Signal();
[email protected]396c3a462010-03-03 05:03:22239}
[email protected]e68e62fa2009-02-20 02:00:04240
fdoray31cc6f82017-02-10 23:31:10241std::unique_ptr<RenderProcess> RenderProcessImpl::Create() {
fdoraya70d7172017-04-04 20:52:20242 auto task_scheduler_init_params =
243 content::GetContentClient()->renderer()->GetTaskSchedulerInitParams();
244 if (!task_scheduler_init_params)
245 task_scheduler_init_params = GetDefaultTaskSchedulerInitParams();
fdoray31cc6f82017-02-10 23:31:10246
247 return base::WrapUnique(
fdoraya70d7172017-04-04 20:52:20248 new RenderProcessImpl(std::move(task_scheduler_init_params)));
fdoray31cc6f82017-02-10 23:31:10249}
250
[email protected]744c2a22012-03-15 18:42:04251void RenderProcessImpl::AddBindings(int bindings) {
252 enabled_bindings_ |= bindings;
253}
254
255int RenderProcessImpl::GetEnabledBindings() const {
256 return enabled_bindings_;
257}
258
Arthur Sonzognic4f8dee2018-09-05 08:51:33259void RenderProcessImpl::AddRefProcess() {
260 NOTREACHED();
261}
262
263void RenderProcessImpl::ReleaseProcess() {
264 NOTREACHED();
265}
266
[email protected]eb398192012-10-22 20:16:19267} // namespace content