blob: 3843e3d8620d2afe2881f0765c5f6e130b5f28f8 [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"
fdorayd2233a72016-12-13 17:18:2129#include "base/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"
John Abd-El-Malek6b56ef712017-10-21 22:52:4637#include "content/renderer/loader/site_isolation_stats_gatherer.h"
Eric Holk1384f6d2018-01-05 00:49:3638#include "services/service_manager/embedder/switches.h"
[email protected]6bd867b2013-07-24 22:10:2039#include "third_party/WebKit/public/web/WebFrame.h"
[email protected]067f5192014-01-29 05:22:0940#include "v8/include/v8.h"
initial.commit09911bf2008-07-26 23:55:2941
georgesak80353b52017-01-10 21:18:5142#if defined(OS_WIN)
43#include "base/win/win_util.h"
44#endif
45
ishell75fddc12016-04-12 14:03:1446namespace {
47
ishell75fddc12016-04-12 14:03:1448void SetV8FlagIfFeature(const base::Feature& feature, const char* v8_flag) {
49 if (base::FeatureList::IsEnabled(feature)) {
50 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
51 }
52}
53
bradnelson2730e3512017-01-21 20:32:2154void SetV8FlagIfNotFeature(const base::Feature& feature, const char* v8_flag) {
55 if (!base::FeatureList::IsEnabled(feature)) {
56 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
57 }
58}
59
ishell75fddc12016-04-12 14:03:1460void SetV8FlagIfHasSwitch(const char* switch_name, const char* v8_flag) {
61 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
62 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
63 }
64}
65
fdoraya70d7172017-04-04 20:52:2066std::unique_ptr<base::TaskScheduler::InitParams>
67GetDefaultTaskSchedulerInitParams() {
fdoraya70d7172017-04-04 20:52:2068
fdoray743f8d6d2017-02-09 15:24:2369 constexpr int kMaxNumThreadsInBackgroundPool = 1;
fdoraye68b6f92017-02-21 19:39:4670 constexpr int kMaxNumThreadsInBackgroundBlockingPool = 1;
fdorayd2233a72016-12-13 17:18:2171 constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 2;
fdoraye68b6f92017-02-21 19:39:4672 constexpr int kMaxNumThreadsInForegroundBlockingPool = 1;
fdorayd2233a72016-12-13 17:18:2173 constexpr auto kSuggestedReclaimTime = base::TimeDelta::FromSeconds(30);
74
Jeremy Roman04f27c372017-10-27 15:20:5575 return std::make_unique<base::TaskScheduler::InitParams>(
Jeffrey Hefbf000f42017-07-26 22:44:4576 base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundPool,
fdoraya70d7172017-04-04 20:52:2077 kSuggestedReclaimTime),
Jeffrey Hefbf000f42017-07-26 22:44:4578 base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundBlockingPool,
fdoraya70d7172017-04-04 20:52:2079 kSuggestedReclaimTime),
80 base::SchedulerWorkerPoolParams(
Francois Doray6d3c649692017-06-16 19:20:2581 std::max(
82 kMaxNumThreadsInForegroundPoolLowerBound,
83 content::GetMinThreadsInRendererTaskSchedulerForegroundPool()),
fdoraya70d7172017-04-04 20:52:2084 kSuggestedReclaimTime),
Jeffrey Hefbf000f42017-07-26 22:44:4585 base::SchedulerWorkerPoolParams(kMaxNumThreadsInForegroundBlockingPool,
fdoraya70d7172017-04-04 20:52:2086 kSuggestedReclaimTime));
fdorayd2233a72016-12-13 17:18:2187}
88
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4589#if DCHECK_IS_ON() && defined(SYZYASAN)
90void V8DcheckCallbackHandler(const char* file, int line, const char* message) {
91 // TODO(siggi): Set a crash key or a breadcrumb so the fact that we hit a
92 // V8 DCHECK gets out in the crash report.
93 ::logging::LogMessage(file, line, logging::LOG_DCHECK).stream() << message;
94}
95#endif // DCHECK_IS_ON() && defined(SYZYASAN)
96
ishell75fddc12016-04-12 14:03:1497} // namespace
98
[email protected]eb398192012-10-22 20:16:1999namespace content {
100
fdoray31cc6f82017-02-10 23:31:10101RenderProcessImpl::RenderProcessImpl(
fdoraya70d7172017-04-04 20:52:20102 std::unique_ptr<base::TaskScheduler::InitParams> task_scheduler_init_params)
103 : RenderProcess("Renderer", std::move(task_scheduler_init_params)),
fdoray31cc6f82017-02-10 23:31:10104 enabled_bindings_(0) {
[email protected]396c3a462010-03-03 05:03:22105#if defined(OS_WIN)
106 // HACK: See http://b/issue?id=1024307 for rationale.
107 if (GetModuleHandle(L"LPK.DLL") == NULL) {
108 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
109 // when buffering into a EMF buffer for printing.
110 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
111 GdiInitializeLanguagePack gdi_init_lpk =
112 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
113 GetModuleHandle(L"GDI32.DLL"),
114 "GdiInitializeLanguagePack"));
115 DCHECK(gdi_init_lpk);
[email protected]00c39612010-03-06 02:53:28116 if (gdi_init_lpk) {
[email protected]396c3a462010-03-03 05:03:22117 gdi_init_lpk(0);
[email protected]00c39612010-03-06 02:53:28118 }
[email protected]396c3a462010-03-03 05:03:22119 }
[email protected]e68e62fa2009-02-20 02:00:04120#endif
121
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45122#if DCHECK_IS_ON() && defined(SYZYASAN)
123 // SyzyASAN official builds can ship with DCHECKs compiled in. Failing DCHECKs
124 // then are either fatal or simply log the error, based on a feature flag.
125 // Make sure V8 follows suit by setting a Dcheck handler that forwards to
126 // the Chrome base logging implementation.
127 v8::V8::SetDcheckErrorHandler(&V8DcheckCallbackHandler);
128
129 if (!base::FeatureList::IsEnabled(base::kSyzyAsanDCheckIsFatalFeature)) {
130 // These V8 flags default on in this build configuration. This triggers
131 // additional verification and code generation, which both slows down V8,
132 // and can lead to fatal CHECKs. Turn these flags down to get something
133 // closer to V8s normal performance and behavior.
134 constexpr char kDisabledFlags[] =
135 "--noturbo_verify "
136 "--noverify_csa "
137 "--noturbo_verify_allocation "
138 "--nodebug_code";
139
140 v8::V8::SetFlagsFromString(kDisabledFlags, sizeof(kDisabledFlags));
141 }
142#endif // DCHECK_IS_ON() && defined(SYZYASAN)
143
[email protected]35b4f0c2014-06-26 16:55:27144 if (base::SysInfo::IsLowEndDevice()) {
[email protected]067f5192014-01-29 05:22:09145 std::string optimize_flag("--optimize-for-size");
146 v8::V8::SetFlagsFromString(optimize_flag.c_str(),
147 static_cast<int>(optimize_flag.size()));
148 }
[email protected]987422f2013-10-01 10:33:31149
ishell75fddc12016-04-12 14:03:14150 SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping,
151 "--noharmony-shipping");
152 SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony");
Kouhei Ueno7f2eeeb2017-09-20 10:34:54153 SetV8FlagIfFeature(features::kModuleScriptsDynamicImport,
154 "--harmony-dynamic-import");
Hiroshige Hayashizaki15c2eb92017-10-20 04:57:05155 SetV8FlagIfFeature(features::kModuleScriptsImportMetaUrl,
156 "--harmony-import-meta");
bradnelsonc79f5a6f2016-10-10 18:31:14157 SetV8FlagIfFeature(features::kAsmJsToWebAssembly, "--validate-asm");
Brad Nelson2b2ec4a2017-08-17 21:56:07158 SetV8FlagIfNotFeature(features::kAsmJsToWebAssembly, "--no-validate-asm");
bradnelson2730e3512017-01-21 20:32:21159 SetV8FlagIfNotFeature(features::kWebAssembly,
160 "--wasm-disable-structured-cloning");
Ross McIlroy824a40482017-12-13 12:17:58161 SetV8FlagIfFeature(features::kV8BackgroundCompile, "--background-compile");
ishell75fddc12016-04-12 14:03:14162
Michael Hablich896d52662017-10-23 15:59:57163 SetV8FlagIfFeature(features::kV8VmFuture, "--future");
164 SetV8FlagIfNotFeature(features::kV8VmFuture, "--no-future");
Brad Nelson5cd500e2018-01-04 00:50:19165 SetV8FlagIfFeature(features::kSharedArrayBuffer,
166 "--harmony-sharedarraybuffer");
167 SetV8FlagIfNotFeature(features::kSharedArrayBuffer,
168 "--no-harmony-sharedarraybuffer");
Michael Hablich896d52662017-10-23 15:59:57169
Eric Holkdc499db2017-07-17 17:57:35170 SetV8FlagIfFeature(features::kWebAssemblyTrapHandler, "--wasm-trap-handler");
Eric Holkb4f60132017-08-18 19:37:41171 SetV8FlagIfNotFeature(features::kWebAssemblyTrapHandler,
172 "--no-wasm-trap-handler");
Eric Holkdc499db2017-07-17 17:57:35173#if defined(OS_LINUX) && defined(ARCH_CPU_X86_64) && !defined(OS_ANDROID)
174 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
Eric Holk1384f6d2018-01-05 00:49:36175 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
176 if (!command_line->HasSwitch(
177 service_manager::switches::kDisableInProcessStackTraces)) {
178 base::debug::SetStackDumpFirstChanceCallback(v8::V8::TryHandleSignal);
179 } else if (!command_line->HasSwitch(switches::kEnableCrashReporter) &&
180 !command_line->HasSwitch(
181 switches::kEnableCrashReporterForTesting)) {
182 // If we are using WebAssembly trap handling but both Breakpad and
183 // in-process stack traces are disabled then there will be no signal
184 // handler. In this case, we fall back on V8's default handler
185 // (https://crbug.com/798150).
186 v8::V8::RegisterDefaultSignalHandler();
187 }
Eric Holkdc499db2017-07-17 17:57:35188 }
189#endif
190
avi83883c82014-12-23 00:08:49191 const base::CommandLine& command_line =
192 *base::CommandLine::ForCurrentProcess();
ishell75fddc12016-04-12 14:03:14193
[email protected]396c3a462010-03-03 05:03:22194 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
[email protected]067f5192014-01-29 05:22:09195 std::string flags(
[email protected]95edc392010-07-30 22:00:38196 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
[email protected]067f5192014-01-29 05:22:09197 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
[email protected]396c3a462010-03-03 05:03:22198 }
[email protected]55dd9332013-09-04 17:17:50199
nick88299ba2015-06-16 23:57:41200 SiteIsolationStatsGatherer::SetEnabled(
201 GetContentClient()->renderer()->ShouldGatherSiteIsolationStats());
sammc7f6c6a02017-01-30 00:53:51202
203 if (command_line.HasSwitch(switches::kDomAutomationController))
204 enabled_bindings_ |= BINDINGS_POLICY_DOM_AUTOMATION;
205 if (command_line.HasSwitch(switches::kStatsCollectionController))
206 enabled_bindings_ |= BINDINGS_POLICY_STATS_COLLECTION;
[email protected]e68e62fa2009-02-20 02:00:04207}
208
[email protected]396c3a462010-03-03 05:03:22209RenderProcessImpl::~RenderProcessImpl() {
[email protected]396c3a462010-03-03 05:03:22210#ifndef NDEBUG
Blink Reformat1c4d759e2017-04-09 16:34:54211 int count = blink::WebFrame::InstanceCount();
[email protected]6bd867b2013-07-24 22:10:20212 if (count)
213 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
[email protected]396c3a462010-03-03 05:03:22214#endif
[email protected]e68e62fa2009-02-20 02:00:04215
[email protected]396c3a462010-03-03 05:03:22216 GetShutDownEvent()->Signal();
[email protected]396c3a462010-03-03 05:03:22217}
[email protected]e68e62fa2009-02-20 02:00:04218
fdoray31cc6f82017-02-10 23:31:10219std::unique_ptr<RenderProcess> RenderProcessImpl::Create() {
fdoraya70d7172017-04-04 20:52:20220 auto task_scheduler_init_params =
221 content::GetContentClient()->renderer()->GetTaskSchedulerInitParams();
222 if (!task_scheduler_init_params)
223 task_scheduler_init_params = GetDefaultTaskSchedulerInitParams();
fdoray31cc6f82017-02-10 23:31:10224
225 return base::WrapUnique(
fdoraya70d7172017-04-04 20:52:20226 new RenderProcessImpl(std::move(task_scheduler_init_params)));
fdoray31cc6f82017-02-10 23:31:10227}
228
[email protected]744c2a22012-03-15 18:42:04229void RenderProcessImpl::AddBindings(int bindings) {
230 enabled_bindings_ |= bindings;
231}
232
233int RenderProcessImpl::GetEnabledBindings() const {
234 return enabled_bindings_;
235}
236
[email protected]eb398192012-10-22 20:16:19237} // namespace content