blob: f641614fcc40a5b0705f54704914564702c46e65 [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"
[email protected]35b4f0c2014-06-26 16:55:2728#include "base/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
Weza6ca5b92018-03-23 19:03:0788#if 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}
Weza6ca5b92018-03-23 19:03:0794#endif // 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) {
Weza6ca5b92018-03-23 19:03:07104#if DCHECK_IS_CONFIGURABLE
105 // 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 }
Weza6ca5b92018-03-23 19:03:07123#endif // 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
bradnelsonc79f5a6f2016-10-10 18:31:14139 SetV8FlagIfFeature(features::kAsmJsToWebAssembly, "--validate-asm");
Brad Nelson2b2ec4a2017-08-17 21:56:07140 SetV8FlagIfNotFeature(features::kAsmJsToWebAssembly, "--no-validate-asm");
ishell75fddc12016-04-12 14:03:14141
Hannes Payer1130d5672018-06-05 07:37:31142 SetV8FlagIfFeature(features::kV8Orinoco, "--no-single-threaded-gc");
143 SetV8FlagIfNotFeature(features::kV8Orinoco, "--single-threaded-gc");
144
Michael Hablich896d52662017-10-23 15:59:57145 SetV8FlagIfFeature(features::kV8VmFuture, "--future");
146 SetV8FlagIfNotFeature(features::kV8VmFuture, "--no-future");
Clemens Hammacher0c8a15a2018-04-27 13:45:32147
Clemens Hammacherfab55102018-07-24 16:19:19148 SetV8FlagIfFeature(features::kWebAssemblyBaseline,
149 "--liftoff --wasm-tier-up");
150 SetV8FlagIfNotFeature(features::kWebAssemblyBaseline,
151 "--no-liftoff --no-wasm-tier-up");
Clemens Hammacher0c8a15a2018-04-27 13:45:32152
Ben Smith24c1e5c2018-06-20 01:09:02153 if (base::FeatureList::IsEnabled(features::kWebAssemblyThreads)) {
154 constexpr char kFlags[] =
155 "--harmony-sharedarraybuffer "
156 "--no-wasm-disable-structured-cloning "
157 "--experimental-wasm-threads";
158
159 v8::V8::SetFlagsFromString(kFlags, sizeof(kFlags));
160 } else {
161 SetV8FlagIfNotFeature(features::kWebAssembly,
162 "--wasm-disable-structured-cloning");
163 SetV8FlagIfFeature(features::kSharedArrayBuffer,
164 "--harmony-sharedarraybuffer");
165 SetV8FlagIfNotFeature(features::kSharedArrayBuffer,
166 "--no-harmony-sharedarraybuffer");
167 }
Michael Hablich896d52662017-10-23 15:59:57168
Eric Holkb4f60132017-08-18 19:37:41169 SetV8FlagIfNotFeature(features::kWebAssemblyTrapHandler,
170 "--no-wasm-trap-handler");
Lei Zhang2d3ead602018-07-10 01:15:50171#if defined(OS_LINUX) && defined(ARCH_CPU_X86_64)
Eric Holkdc499db2017-07-17 17:57:35172 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
Eric Holk90f2d932018-03-21 23:07:47173 bool use_v8_signal_handler = false;
Eric Holk1384f6d2018-01-05 00:49:36174 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
175 if (!command_line->HasSwitch(
176 service_manager::switches::kDisableInProcessStackTraces)) {
177 base::debug::SetStackDumpFirstChanceCallback(v8::V8::TryHandleSignal);
178 } else if (!command_line->HasSwitch(switches::kEnableCrashReporter) &&
179 !command_line->HasSwitch(
180 switches::kEnableCrashReporterForTesting)) {
181 // If we are using WebAssembly trap handling but both Breakpad and
182 // in-process stack traces are disabled then there will be no signal
183 // handler. In this case, we fall back on V8's default handler
184 // (https://crbug.com/798150).
Eric Holk90f2d932018-03-21 23:07:47185 use_v8_signal_handler = true;
Eric Holk1384f6d2018-01-05 00:49:36186 }
Eric Holk2c28bc9e2018-04-02 21:59:44187 // TODO(eholk): report UMA stat for how often this succeeds
188 v8::V8::EnableWebAssemblyTrapHandler(use_v8_signal_handler);
Eric Holkdc499db2017-07-17 17:57:35189 }
190#endif
191
avi83883c82014-12-23 00:08:49192 const base::CommandLine& command_line =
193 *base::CommandLine::ForCurrentProcess();
ishell75fddc12016-04-12 14:03:14194
Ross McIlroy3ba92072018-08-01 00:43:30195 if (command_line.HasSwitch(switches::kNoV8UntrustedCodeMitigations)) {
196 const char* disable_mitigations = "--no-untrusted-code-mitigations";
197 v8::V8::SetFlagsFromString(disable_mitigations,
198 strlen(disable_mitigations));
199 }
200
[email protected]396c3a462010-03-03 05:03:22201 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
[email protected]067f5192014-01-29 05:22:09202 std::string flags(
[email protected]95edc392010-07-30 22:00:38203 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
[email protected]067f5192014-01-29 05:22:09204 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
[email protected]396c3a462010-03-03 05:03:22205 }
[email protected]55dd9332013-09-04 17:17:50206
sammc7f6c6a02017-01-30 00:53:51207 if (command_line.HasSwitch(switches::kDomAutomationController))
208 enabled_bindings_ |= BINDINGS_POLICY_DOM_AUTOMATION;
209 if (command_line.HasSwitch(switches::kStatsCollectionController))
210 enabled_bindings_ |= BINDINGS_POLICY_STATS_COLLECTION;
[email protected]e68e62fa2009-02-20 02:00:04211}
212
[email protected]396c3a462010-03-03 05:03:22213RenderProcessImpl::~RenderProcessImpl() {
[email protected]396c3a462010-03-03 05:03:22214#ifndef NDEBUG
Blink Reformat1c4d759e2017-04-09 16:34:54215 int count = blink::WebFrame::InstanceCount();
[email protected]6bd867b2013-07-24 22:10:20216 if (count)
217 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
[email protected]396c3a462010-03-03 05:03:22218#endif
[email protected]e68e62fa2009-02-20 02:00:04219
[email protected]396c3a462010-03-03 05:03:22220 GetShutDownEvent()->Signal();
[email protected]396c3a462010-03-03 05:03:22221}
[email protected]e68e62fa2009-02-20 02:00:04222
fdoray31cc6f82017-02-10 23:31:10223std::unique_ptr<RenderProcess> RenderProcessImpl::Create() {
fdoraya70d7172017-04-04 20:52:20224 auto task_scheduler_init_params =
225 content::GetContentClient()->renderer()->GetTaskSchedulerInitParams();
226 if (!task_scheduler_init_params)
227 task_scheduler_init_params = GetDefaultTaskSchedulerInitParams();
fdoray31cc6f82017-02-10 23:31:10228
229 return base::WrapUnique(
fdoraya70d7172017-04-04 20:52:20230 new RenderProcessImpl(std::move(task_scheduler_init_params)));
fdoray31cc6f82017-02-10 23:31:10231}
232
[email protected]744c2a22012-03-15 18:42:04233void RenderProcessImpl::AddBindings(int bindings) {
234 enabled_bindings_ |= bindings;
235}
236
237int RenderProcessImpl::GetEnabledBindings() const {
238 return enabled_bindings_;
239}
240
Arthur Sonzognic4f8dee2018-09-05 08:51:33241void RenderProcessImpl::AddRefProcess() {
242 NOTREACHED();
243}
244
245void RenderProcessImpl::ReleaseProcess() {
246 NOTREACHED();
247}
248
[email protected]eb398192012-10-22 20:16:19249} // namespace content