blob: 84a7c79ae7e10407e387fb8deba173013e8f90fc [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 Charette52fa3ae2019-04-15 21:44:3729#include "base/task/thread_pool/initialization_util.h"
fdorayd2233a72016-12-13 17:18:2130#include "base/time/time.h"
Gabriel Charette52fa3ae2019-04-15 21:44:3731#include "content/common/thread_pool_util.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
Gabriel Charette52fa3ae2019-04-15 21:44:3765std::unique_ptr<base::ThreadPool::InitParams> GetDefaultThreadPoolInitParams() {
Etienne Pierre-dorayce562962019-02-08 18:50:4866 constexpr int kMaxNumThreadsInBackgroundPool = 2;
67 constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 3;
fdorayd2233a72016-12-13 17:18:2168 constexpr auto kSuggestedReclaimTime = base::TimeDelta::FromSeconds(30);
69
Gabriel Charette52fa3ae2019-04-15 21:44:3770 return std::make_unique<base::ThreadPool::InitParams>(
Gabriel Charette3e2898f2019-05-01 14:55:0171 base::ThreadGroupParams(kMaxNumThreadsInBackgroundPool,
72 kSuggestedReclaimTime),
73 base::ThreadGroupParams(
Gabriel Charette52fa3ae2019-04-15 21:44:3774 std::max(kMaxNumThreadsInForegroundPoolLowerBound,
75 content::GetMinForegroundThreadsInRendererThreadPool()),
Etienne Pierre-dorayce562962019-02-08 18:50:4876 kSuggestedReclaimTime));
fdorayd2233a72016-12-13 17:18:2177}
78
Tomas Popelaafffa972018-11-13 20:42:0579#if defined(DCHECK_IS_CONFIGURABLE)
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4580void V8DcheckCallbackHandler(const char* file, int line, const char* message) {
81 // TODO(siggi): Set a crash key or a breadcrumb so the fact that we hit a
82 // V8 DCHECK gets out in the crash report.
83 ::logging::LogMessage(file, line, logging::LOG_DCHECK).stream() << message;
84}
Tomas Popelaafffa972018-11-13 20:42:0585#endif // defined(DCHECK_IS_CONFIGURABLE)
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4586
ishell75fddc12016-04-12 14:03:1487} // namespace
88
[email protected]eb398192012-10-22 20:16:1989namespace content {
90
fdoray31cc6f82017-02-10 23:31:1091RenderProcessImpl::RenderProcessImpl(
Gabriel Charette52fa3ae2019-04-15 21:44:3792 std::unique_ptr<base::ThreadPool::InitParams> thread_pool_init_params)
93 : RenderProcess("Renderer", std::move(thread_pool_init_params)),
fdoray31cc6f82017-02-10 23:31:1094 enabled_bindings_(0) {
Tomas Popelaafffa972018-11-13 20:42:0595#if defined(DCHECK_IS_CONFIGURABLE)
Weza6ca5b92018-03-23 19:03:0796 // Some official builds ship with DCHECKs compiled in. Failing DCHECKs then
97 // are either fatal or simply log the error, based on a feature flag.
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4598 // Make sure V8 follows suit by setting a Dcheck handler that forwards to
99 // the Chrome base logging implementation.
100 v8::V8::SetDcheckErrorHandler(&V8DcheckCallbackHandler);
101
Weza6ca5b92018-03-23 19:03:07102 if (!base::FeatureList::IsEnabled(base::kDCheckIsFatalFeature)) {
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45103 // These V8 flags default on in this build configuration. This triggers
104 // additional verification and code generation, which both slows down V8,
105 // and can lead to fatal CHECKs. Turn these flags down to get something
106 // closer to V8s normal performance and behavior.
107 constexpr char kDisabledFlags[] =
108 "--noturbo_verify "
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45109 "--noturbo_verify_allocation "
110 "--nodebug_code";
111
112 v8::V8::SetFlagsFromString(kDisabledFlags, sizeof(kDisabledFlags));
113 }
Tomas Popelaafffa972018-11-13 20:42:05114#endif // defined(DCHECK_IS_CONFIGURABLE)
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45115
[email protected]35b4f0c2014-06-26 16:55:27116 if (base::SysInfo::IsLowEndDevice()) {
[email protected]067f5192014-01-29 05:22:09117 std::string optimize_flag("--optimize-for-size");
Clemens Hammacherc4a139a2019-04-25 13:55:09118 v8::V8::SetFlagsFromString(optimize_flag.c_str(), optimize_flag.size());
[email protected]067f5192014-01-29 05:22:09119 }
[email protected]987422f2013-10-01 10:33:31120
ishell75fddc12016-04-12 14:03:14121 SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping,
122 "--noharmony-shipping");
123 SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony");
Kouhei Ueno14d350d2018-10-01 02:36:27124
125 constexpr char kModuleFlags[] =
126 "--harmony-dynamic-import --harmony-import-meta";
127 v8::V8::SetFlagsFromString(kModuleFlags, sizeof(kModuleFlags));
128
Hannes Payer1130d5672018-06-05 07:37:31129 SetV8FlagIfFeature(features::kV8Orinoco, "--no-single-threaded-gc");
130 SetV8FlagIfNotFeature(features::kV8Orinoco, "--single-threaded-gc");
131
Michael Hablich896d52662017-10-23 15:59:57132 SetV8FlagIfFeature(features::kV8VmFuture, "--future");
133 SetV8FlagIfNotFeature(features::kV8VmFuture, "--no-future");
Clemens Hammacher0c8a15a2018-04-27 13:45:32134
Clemens Hammacherfab55102018-07-24 16:19:19135 SetV8FlagIfFeature(features::kWebAssemblyBaseline,
136 "--liftoff --wasm-tier-up");
137 SetV8FlagIfNotFeature(features::kWebAssemblyBaseline,
138 "--no-liftoff --no-wasm-tier-up");
Clemens Hammacher0c8a15a2018-04-27 13:45:32139
Clemens Hammacher082fa572019-04-25 08:22:29140 SetV8FlagIfFeature(features::kWebAssemblyCodeGC, "--wasm-code-gc");
141 SetV8FlagIfNotFeature(features::kWebAssemblyCodeGC, "--no-wasm-code-gc");
142
Deepti Gandluri11734cc42019-05-02 18:00:20143 SetV8FlagIfFeature(features::kWebAssemblySimd, "--experimental-wasm-simd");
144 SetV8FlagIfNotFeature(features::kWebAssemblySimd,
145 "--no-experimental-wasm-simd");
146
Ben Smith24c1e5c2018-06-20 01:09:02147 if (base::FeatureList::IsEnabled(features::kWebAssemblyThreads)) {
148 constexpr char kFlags[] =
149 "--harmony-sharedarraybuffer "
150 "--no-wasm-disable-structured-cloning "
151 "--experimental-wasm-threads";
152
153 v8::V8::SetFlagsFromString(kFlags, sizeof(kFlags));
154 } else {
155 SetV8FlagIfNotFeature(features::kWebAssembly,
156 "--wasm-disable-structured-cloning");
157 SetV8FlagIfFeature(features::kSharedArrayBuffer,
158 "--harmony-sharedarraybuffer");
159 SetV8FlagIfNotFeature(features::kSharedArrayBuffer,
160 "--no-harmony-sharedarraybuffer");
161 }
Michael Hablich896d52662017-10-23 15:59:57162
Maya Lekova4bbfdb7f2018-10-16 08:15:03163 SetV8FlagIfFeature(features::kAwaitOptimization,
164 "--harmony-await-optimization");
165 SetV8FlagIfNotFeature(features::kAwaitOptimization,
166 "--no-harmony-await-optimization");
167
Eric Holkb4f60132017-08-18 19:37:41168 SetV8FlagIfNotFeature(features::kWebAssemblyTrapHandler,
169 "--no-wasm-trap-handler");
Lei Zhang2d3ead602018-07-10 01:15:50170#if defined(OS_LINUX) && defined(ARCH_CPU_X86_64)
Eric Holkdc499db2017-07-17 17:57:35171 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
Eric Holk1384f6d2018-01-05 00:49:36172 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
173 if (!command_line->HasSwitch(
174 service_manager::switches::kDisableInProcessStackTraces)) {
Andreas Haasef19d592019-04-30 18:16:51175 // Only enable WebAssembly trap handler if we can set the callback.
176 if (base::debug::SetStackDumpFirstChanceCallback(
177 v8::V8::TryHandleSignal)) {
178 // We registered the WebAssembly trap handler callback with the stack
179 // dump signal handler successfully. We can tell V8 that it can enable
180 // WebAssembly trap handler without using the V8 signal handler.
181 v8::V8::EnableWebAssemblyTrapHandler(/*use_v8_signal_handler=*/false);
182 }
Eric Holk1384f6d2018-01-05 00:49:36183 } else if (!command_line->HasSwitch(switches::kEnableCrashReporter) &&
184 !command_line->HasSwitch(
185 switches::kEnableCrashReporterForTesting)) {
186 // If we are using WebAssembly trap handling but both Breakpad and
187 // in-process stack traces are disabled then there will be no signal
188 // handler. In this case, we fall back on V8's default handler
189 // (https://crbug.com/798150).
Andreas Haasef19d592019-04-30 18:16:51190 v8::V8::EnableWebAssemblyTrapHandler(/*use_v8_signal_handler=*/true);
Eric Holk1384f6d2018-01-05 00:49:36191 }
Eric Holkdc499db2017-07-17 17:57:35192 }
193#endif
Andreas Haas5ed0f502018-11-06 09:05:06194#if defined(OS_WIN) && defined(ARCH_CPU_X86_64)
195 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
196 // On Windows we use the default trap handler provided by V8.
197 bool use_v8_trap_handler = true;
198 v8::V8::EnableWebAssemblyTrapHandler(use_v8_trap_handler);
199 }
200#endif
Andreas Haas3bd45322018-11-21 07:45:42201#if defined(OS_MACOSX) && defined(ARCH_CPU_X86_64)
202 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
203 // On macOS, Crashpad uses exception ports to handle signals in a different
204 // process. As we cannot just pass a callback to this other process, we ask
205 // V8 to install its own signal handler to deal with WebAssembly traps.
206 bool use_v8_signal_handler = true;
207 v8::V8::EnableWebAssemblyTrapHandler(use_v8_signal_handler);
208 }
209#endif // defined(OS_MACOSX) && defined(ARCH_CPU_X86_64)
Eric Holkdc499db2017-07-17 17:57:35210
avi83883c82014-12-23 00:08:49211 const base::CommandLine& command_line =
212 *base::CommandLine::ForCurrentProcess();
ishell75fddc12016-04-12 14:03:14213
Ross McIlroy3ba92072018-08-01 00:43:30214 if (command_line.HasSwitch(switches::kNoV8UntrustedCodeMitigations)) {
215 const char* disable_mitigations = "--no-untrusted-code-mitigations";
216 v8::V8::SetFlagsFromString(disable_mitigations,
217 strlen(disable_mitigations));
218 }
219
[email protected]396c3a462010-03-03 05:03:22220 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
[email protected]067f5192014-01-29 05:22:09221 std::string flags(
[email protected]95edc392010-07-30 22:00:38222 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
Clemens Hammacherc4a139a2019-04-25 13:55:09223 v8::V8::SetFlagsFromString(flags.c_str(), flags.size());
[email protected]396c3a462010-03-03 05:03:22224 }
[email protected]55dd9332013-09-04 17:17:50225
sammc7f6c6a02017-01-30 00:53:51226 if (command_line.HasSwitch(switches::kDomAutomationController))
227 enabled_bindings_ |= BINDINGS_POLICY_DOM_AUTOMATION;
228 if (command_line.HasSwitch(switches::kStatsCollectionController))
229 enabled_bindings_ |= BINDINGS_POLICY_STATS_COLLECTION;
[email protected]e68e62fa2009-02-20 02:00:04230}
231
[email protected]396c3a462010-03-03 05:03:22232RenderProcessImpl::~RenderProcessImpl() {
[email protected]396c3a462010-03-03 05:03:22233#ifndef NDEBUG
Blink Reformat1c4d759e2017-04-09 16:34:54234 int count = blink::WebFrame::InstanceCount();
[email protected]6bd867b2013-07-24 22:10:20235 if (count)
236 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
[email protected]396c3a462010-03-03 05:03:22237#endif
[email protected]e68e62fa2009-02-20 02:00:04238
[email protected]396c3a462010-03-03 05:03:22239 GetShutDownEvent()->Signal();
[email protected]396c3a462010-03-03 05:03:22240}
[email protected]e68e62fa2009-02-20 02:00:04241
fdoray31cc6f82017-02-10 23:31:10242std::unique_ptr<RenderProcess> RenderProcessImpl::Create() {
Gabriel Charette52fa3ae2019-04-15 21:44:37243 auto thread_pool_init_params =
244 content::GetContentClient()->renderer()->GetThreadPoolInitParams();
245 if (!thread_pool_init_params)
246 thread_pool_init_params = GetDefaultThreadPoolInitParams();
fdoray31cc6f82017-02-10 23:31:10247
248 return base::WrapUnique(
Gabriel Charette52fa3ae2019-04-15 21:44:37249 new RenderProcessImpl(std::move(thread_pool_init_params)));
fdoray31cc6f82017-02-10 23:31:10250}
251
[email protected]744c2a22012-03-15 18:42:04252void RenderProcessImpl::AddBindings(int bindings) {
253 enabled_bindings_ |= bindings;
254}
255
256int RenderProcessImpl::GetEnabledBindings() const {
257 return enabled_bindings_;
258}
259
Arthur Sonzognic4f8dee2018-09-05 08:51:33260void RenderProcessImpl::AddRefProcess() {
261 NOTREACHED();
262}
263
264void RenderProcessImpl::ReleaseProcess() {
265 NOTREACHED();
266}
267
[email protected]eb398192012-10-22 20:16:19268} // namespace content