blob: ab0a696a29b247a37b7087c478788bcc9634e360 [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>
initial.commit09911bf2008-07-26 23:55:2911#include <mlang.h>
Gabriel Charetteeadf58862019-08-29 05:20:2712#include <objidl.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"
Ross McIlroy900375b2019-05-16 20:17:4228#include "base/strings/string_split.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0329#include "base/system/sys_info.h"
Gabriel Charette52fa3ae2019-04-15 21:44:3730#include "base/task/thread_pool/initialization_util.h"
Gabriel Charetteeadf58862019-08-29 05:20:2731#include "base/task/thread_pool/thread_pool_instance.h"
fdorayd2233a72016-12-13 17:18:2132#include "base/time/time.h"
Gabriel Charette52fa3ae2019-04-15 21:44:3733#include "content/common/thread_pool_util.h"
sammc7f6c6a02017-01-30 00:53:5134#include "content/public/common/bindings_policy.h"
fdorayd2233a72016-12-13 17:18:2135#include "content/public/common/content_client.h"
bradnelsonc79f5a6f2016-10-10 18:31:1436#include "content/public/common/content_features.h"
[email protected]c08950d22011-10-13 22:20:2937#include "content/public/common/content_switches.h"
[email protected]d344114c2011-10-01 01:24:3438#include "content/public/renderer/content_renderer_client.h"
Bill Budge85fcdf82020-09-29 03:17:0539#include "services/network/public/cpp/features.h"
Camillo Brunia19d7fc2020-03-17 07:53:3540#include "third_party/blink/public/common/features.h"
Blink Reformata30d4232018-04-07 15:31:0641#include "third_party/blink/public/web/web_frame.h"
[email protected]067f5192014-01-29 05:22:0942#include "v8/include/v8.h"
initial.commit09911bf2008-07-26 23:55:2943
georgesak80353b52017-01-10 21:18:5144#if defined(OS_WIN)
45#include "base/win/win_util.h"
46#endif
Sean McAllister82700412020-08-19 20:10:3547#if (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(ARCH_CPU_X86_64)
Andreas Haas54c0c022019-06-14 17:33:5348#include "v8/include/v8-wasm-trap-handler-posix.h"
49#endif
ishell75fddc12016-04-12 14:03:1450namespace {
51
ishell75fddc12016-04-12 14:03:1452void SetV8FlagIfFeature(const base::Feature& feature, const char* v8_flag) {
53 if (base::FeatureList::IsEnabled(feature)) {
54 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
55 }
56}
57
bradnelson2730e3512017-01-21 20:32:2158void SetV8FlagIfNotFeature(const base::Feature& feature, const char* v8_flag) {
59 if (!base::FeatureList::IsEnabled(feature)) {
60 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
61 }
62}
63
ishell75fddc12016-04-12 14:03:1464void SetV8FlagIfHasSwitch(const char* switch_name, const char* v8_flag) {
65 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
66 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
67 }
68}
69
Gabriel Charette43fd3702019-05-29 16:36:5170std::unique_ptr<base::ThreadPoolInstance::InitParams>
71GetThreadPoolInitParams() {
Etienne Pierre-dorayce562962019-02-08 18:50:4872 constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 3;
Gabriel Charette43fd3702019-05-29 16:36:5173 return std::make_unique<base::ThreadPoolInstance::InitParams>(
Francois Doray7f777312019-05-16 12:26:3174 std::max(kMaxNumThreadsInForegroundPoolLowerBound,
75 content::GetMinForegroundThreadsInRendererThreadPool()));
fdorayd2233a72016-12-13 17:18:2176}
77
Tomas Popelaafffa972018-11-13 20:42:0578#if defined(DCHECK_IS_CONFIGURABLE)
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4579void V8DcheckCallbackHandler(const char* file, int line, const char* message) {
80 // TODO(siggi): Set a crash key or a breadcrumb so the fact that we hit a
81 // V8 DCHECK gets out in the crash report.
82 ::logging::LogMessage(file, line, logging::LOG_DCHECK).stream() << message;
83}
Tomas Popelaafffa972018-11-13 20:42:0584#endif // defined(DCHECK_IS_CONFIGURABLE)
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4585
ishell75fddc12016-04-12 14:03:1486} // namespace
87
[email protected]eb398192012-10-22 20:16:1988namespace content {
89
Francois Doray7f777312019-05-16 12:26:3190RenderProcessImpl::RenderProcessImpl()
Nasko Oskovd0dfddf2020-04-20 16:53:0291 : RenderProcess("Renderer", GetThreadPoolInitParams()) {
Tomas Popelaafffa972018-11-13 20:42:0592#if defined(DCHECK_IS_CONFIGURABLE)
Weza6ca5b92018-03-23 19:03:0793 // Some official builds ship with DCHECKs compiled in. Failing DCHECKs then
94 // are either fatal or simply log the error, based on a feature flag.
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4595 // Make sure V8 follows suit by setting a Dcheck handler that forwards to
96 // the Chrome base logging implementation.
97 v8::V8::SetDcheckErrorHandler(&V8DcheckCallbackHandler);
98
Weza6ca5b92018-03-23 19:03:0799 if (!base::FeatureList::IsEnabled(base::kDCheckIsFatalFeature)) {
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45100 // These V8 flags default on in this build configuration. This triggers
101 // additional verification and code generation, which both slows down V8,
102 // and can lead to fatal CHECKs. Turn these flags down to get something
103 // closer to V8s normal performance and behavior.
104 constexpr char kDisabledFlags[] =
105 "--noturbo_verify "
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45106 "--noturbo_verify_allocation "
107 "--nodebug_code";
108
109 v8::V8::SetFlagsFromString(kDisabledFlags, sizeof(kDisabledFlags));
110 }
Tomas Popelaafffa972018-11-13 20:42:05111#endif // defined(DCHECK_IS_CONFIGURABLE)
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45112
[email protected]35b4f0c2014-06-26 16:55:27113 if (base::SysInfo::IsLowEndDevice()) {
[email protected]067f5192014-01-29 05:22:09114 std::string optimize_flag("--optimize-for-size");
Clemens Hammacherc4a139a2019-04-25 13:55:09115 v8::V8::SetFlagsFromString(optimize_flag.c_str(), optimize_flag.size());
[email protected]067f5192014-01-29 05:22:09116 }
[email protected]987422f2013-10-01 10:33:31117
ishell75fddc12016-04-12 14:03:14118 SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping,
119 "--noharmony-shipping");
120 SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony");
Andreas Haasb400d912019-08-28 18:54:10121 SetV8FlagIfHasSwitch(switches::kEnableExperimentalWebAssemblyFeatures,
122 "--wasm-staging");
Kouhei Ueno14d350d2018-10-01 02:36:27123
Maya Lekova4785fd22020-08-31 10:36:07124 SetV8FlagIfHasSwitch(switches::kEnableUnsafeFastJSCalls,
125 "--turbo-fast-api-calls");
126
Kouhei Ueno14d350d2018-10-01 02:36:27127 constexpr char kModuleFlags[] =
128 "--harmony-dynamic-import --harmony-import-meta";
129 v8::V8::SetFlagsFromString(kModuleFlags, sizeof(kModuleFlags));
130
Michael Hablich896d52662017-10-23 15:59:57131 SetV8FlagIfFeature(features::kV8VmFuture, "--future");
132 SetV8FlagIfNotFeature(features::kV8VmFuture, "--no-future");
Clemens Hammacher0c8a15a2018-04-27 13:45:32133
Clemens Backes963eb37be2020-01-10 11:56:49134 SetV8FlagIfFeature(features::kWebAssemblyBaseline, "--liftoff");
135 SetV8FlagIfNotFeature(features::kWebAssemblyBaseline, "--no-liftoff");
Clemens Hammacher0c8a15a2018-04-27 13:45:32136
Clemens Backes50e0ecd2020-01-20 10:43:24137 SetV8FlagIfFeature(features::kWebAssemblyLazyCompilation,
138 "--wasm-lazy-compilation");
139 SetV8FlagIfNotFeature(features::kWebAssemblyLazyCompilation,
140 "--no-wasm-lazy-compilation");
141
Deepti Gandluri11734cc42019-05-02 18:00:20142 SetV8FlagIfFeature(features::kWebAssemblySimd, "--experimental-wasm-simd");
143 SetV8FlagIfNotFeature(features::kWebAssemblySimd,
144 "--no-experimental-wasm-simd");
145
Camillo Brunia19d7fc2020-03-17 07:53:35146 SetV8FlagIfFeature(blink::features::kTopLevelAwait,
147 "--harmony-top-level-await");
148
Bill Budge85fcdf82020-09-29 03:17:05149 constexpr char kAtomicsFlag[] = "--harmony-atomics";
150 v8::V8::SetFlagsFromString(kAtomicsFlag, sizeof(kAtomicsFlag));
Ben Smith24c1e5c2018-06-20 01:09:02151
Bill Budge85fcdf82020-09-29 03:17:05152 // SharedArrayBuffers require the feature flag, or site isolation. On Android,
153 // the feature is disabled by default, so site isolation is required. On
154 // desktop, site isolation is optional while we migrate existing apps to use
155 // COOP+COEP.
156 bool enableSharedArrayBuffer = false;
157 if (base::FeatureList::IsEnabled(features::kWebAssemblyThreads)) {
158 constexpr char kWasmThreadsFlag[] = "--experimental-wasm-threads";
159 v8::V8::SetFlagsFromString(kWasmThreadsFlag, sizeof(kWasmThreadsFlag));
160 enableSharedArrayBuffer = true;
Ben Smith24c1e5c2018-06-20 01:09:02161 } else {
Bill Budge85fcdf82020-09-29 03:17:05162 enableSharedArrayBuffer =
163 base::FeatureList::IsEnabled(features::kSharedArrayBuffer) ||
164 base::FeatureList::IsEnabled(network::features::kCrossOriginIsolated);
165 }
166
167 if (enableSharedArrayBuffer) {
Ben Smith24c1e5c2018-06-20 01:09:02168 SetV8FlagIfFeature(features::kSharedArrayBuffer,
169 "--harmony-sharedarraybuffer");
Bill Budge85fcdf82020-09-29 03:17:05170 } else {
Ben Smith24c1e5c2018-06-20 01:09:02171 SetV8FlagIfNotFeature(features::kSharedArrayBuffer,
172 "--no-harmony-sharedarraybuffer");
173 }
Michael Hablich896d52662017-10-23 15:59:57174
Clemens Backes963eb37be2020-01-10 11:56:49175 SetV8FlagIfFeature(features::kWebAssemblyTiering, "--wasm-tier-up");
176 SetV8FlagIfNotFeature(features::kWebAssemblyTiering, "--no-wasm-tier-up");
177
Eric Holkb4f60132017-08-18 19:37:41178 SetV8FlagIfNotFeature(features::kWebAssemblyTrapHandler,
179 "--no-wasm-trap-handler");
Sean McAllister82700412020-08-19 20:10:35180#if (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(ARCH_CPU_X86_64)
Eric Holkdc499db2017-07-17 17:57:35181 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
Eric Holk1384f6d2018-01-05 00:49:36182 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
Ken Rockot4e805fd2020-10-01 03:07:39183 if (!command_line->HasSwitch(switches::kDisableInProcessStackTraces)) {
Andreas Haasef19d592019-04-30 18:16:51184 // Only enable WebAssembly trap handler if we can set the callback.
185 if (base::debug::SetStackDumpFirstChanceCallback(
Andreas Haas54c0c022019-06-14 17:33:53186 v8::TryHandleWebAssemblyTrapPosix)) {
Andreas Haasef19d592019-04-30 18:16:51187 // We registered the WebAssembly trap handler callback with the stack
188 // dump signal handler successfully. We can tell V8 that it can enable
189 // WebAssembly trap handler without using the V8 signal handler.
190 v8::V8::EnableWebAssemblyTrapHandler(/*use_v8_signal_handler=*/false);
191 }
Eric Holk1384f6d2018-01-05 00:49:36192 } else if (!command_line->HasSwitch(switches::kEnableCrashReporter) &&
193 !command_line->HasSwitch(
194 switches::kEnableCrashReporterForTesting)) {
195 // If we are using WebAssembly trap handling but both Breakpad and
196 // in-process stack traces are disabled then there will be no signal
197 // handler. In this case, we fall back on V8's default handler
198 // (https://crbug.com/798150).
Andreas Haasef19d592019-04-30 18:16:51199 v8::V8::EnableWebAssemblyTrapHandler(/*use_v8_signal_handler=*/true);
Eric Holk1384f6d2018-01-05 00:49:36200 }
Eric Holkdc499db2017-07-17 17:57:35201 }
202#endif
Andreas Haas5ed0f502018-11-06 09:05:06203#if defined(OS_WIN) && defined(ARCH_CPU_X86_64)
204 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
205 // On Windows we use the default trap handler provided by V8.
206 bool use_v8_trap_handler = true;
207 v8::V8::EnableWebAssemblyTrapHandler(use_v8_trap_handler);
208 }
209#endif
Avi Drissman7c57be72020-07-29 20:09:46210#if defined(OS_MAC) && defined(ARCH_CPU_X86_64)
Andreas Haas3bd45322018-11-21 07:45:42211 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
212 // On macOS, Crashpad uses exception ports to handle signals in a different
213 // process. As we cannot just pass a callback to this other process, we ask
214 // V8 to install its own signal handler to deal with WebAssembly traps.
215 bool use_v8_signal_handler = true;
216 v8::V8::EnableWebAssemblyTrapHandler(use_v8_signal_handler);
217 }
Avi Drissman7c57be72020-07-29 20:09:46218#endif // defined(OS_MAC) && defined(ARCH_CPU_X86_64)
Eric Holkdc499db2017-07-17 17:57:35219
avi83883c82014-12-23 00:08:49220 const base::CommandLine& command_line =
221 *base::CommandLine::ForCurrentProcess();
ishell75fddc12016-04-12 14:03:14222
Ross McIlroy3ba92072018-08-01 00:43:30223 if (command_line.HasSwitch(switches::kNoV8UntrustedCodeMitigations)) {
224 const char* disable_mitigations = "--no-untrusted-code-mitigations";
225 v8::V8::SetFlagsFromString(disable_mitigations,
226 strlen(disable_mitigations));
227 }
228
[email protected]396c3a462010-03-03 05:03:22229 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
Ross McIlroy900375b2019-05-16 20:17:42230 std::string js_flags =
231 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags);
232 std::vector<base::StringPiece> flag_list = base::SplitStringPiece(
233 js_flags, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
234 for (const auto& flag : flag_list) {
235 v8::V8::SetFlagsFromString(flag.as_string().c_str(), flag.size());
236 }
[email protected]396c3a462010-03-03 05:03:22237 }
[email protected]e68e62fa2009-02-20 02:00:04238}
239
[email protected]396c3a462010-03-03 05:03:22240RenderProcessImpl::~RenderProcessImpl() {
[email protected]396c3a462010-03-03 05:03:22241#ifndef NDEBUG
Blink Reformat1c4d759e2017-04-09 16:34:54242 int count = blink::WebFrame::InstanceCount();
[email protected]6bd867b2013-07-24 22:10:20243 if (count)
244 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
[email protected]396c3a462010-03-03 05:03:22245#endif
[email protected]e68e62fa2009-02-20 02:00:04246
[email protected]396c3a462010-03-03 05:03:22247 GetShutDownEvent()->Signal();
[email protected]396c3a462010-03-03 05:03:22248}
[email protected]e68e62fa2009-02-20 02:00:04249
fdoray31cc6f82017-02-10 23:31:10250std::unique_ptr<RenderProcess> RenderProcessImpl::Create() {
Francois Doray7f777312019-05-16 12:26:31251 return base::WrapUnique(new RenderProcessImpl());
fdoray31cc6f82017-02-10 23:31:10252}
253
Arthur Sonzognic4f8dee2018-09-05 08:51:33254void RenderProcessImpl::AddRefProcess() {
255 NOTREACHED();
256}
257
258void RenderProcessImpl::ReleaseProcess() {
259 NOTREACHED();
260}
261
[email protected]eb398192012-10-22 20:16:19262} // namespace content