blob: 905685045429b8c492bfb3416caf724223f4748a [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
20#include "base/bind.h"
initial.commit09911bf2008-07-26 23:55:2921#include "base/command_line.h"
[email protected]037fce02009-01-22 01:42:1522#include "base/compiler_specific.h"
georgesak80353b52017-01-10 21:18:5123#include "base/debug/crash_logging.h"
Eric Holkdc499db2017-07-17 17:57:3524#include "base/debug/stack_trace.h"
ishell75fddc12016-04-12 14:03:1425#include "base/feature_list.h"
fdoray31cc6f82017-02-10 23:31:1026#include "base/memory/ptr_util.h"
[email protected]35b4f0c2014-06-26 16:55:2727#include "base/sys_info.h"
fdorayd2233a72016-12-13 17:18:2128#include "base/task_scheduler/initialization_util.h"
fdorayd2233a72016-12-13 17:18:2129#include "base/time/time.h"
Francois Doray6d3c649692017-06-16 19:20:2530#include "content/common/task_scheduler.h"
sammc7f6c6a02017-01-30 00:53:5131#include "content/public/common/bindings_policy.h"
fdorayd2233a72016-12-13 17:18:2132#include "content/public/common/content_client.h"
bradnelsonc79f5a6f2016-10-10 18:31:1433#include "content/public/common/content_features.h"
[email protected]c08950d22011-10-13 22:20:2934#include "content/public/common/content_switches.h"
[email protected]d344114c2011-10-01 01:24:3435#include "content/public/renderer/content_renderer_client.h"
John Abd-El-Malek6b56ef712017-10-21 22:52:4636#include "content/renderer/loader/site_isolation_stats_gatherer.h"
[email protected]6bd867b2013-07-24 22:10:2037#include "third_party/WebKit/public/web/WebFrame.h"
[email protected]067f5192014-01-29 05:22:0938#include "v8/include/v8.h"
initial.commit09911bf2008-07-26 23:55:2939
georgesak80353b52017-01-10 21:18:5140#if defined(OS_WIN)
41#include "base/win/win_util.h"
42#endif
43
ishell75fddc12016-04-12 14:03:1444namespace {
45
ishell75fddc12016-04-12 14:03:1446void SetV8FlagIfFeature(const base::Feature& feature, const char* v8_flag) {
47 if (base::FeatureList::IsEnabled(feature)) {
48 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
49 }
50}
51
bradnelson2730e3512017-01-21 20:32:2152void SetV8FlagIfNotFeature(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
ishell75fddc12016-04-12 14:03:1458void SetV8FlagIfHasSwitch(const char* switch_name, const char* v8_flag) {
59 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
60 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
61 }
62}
63
fdoraya70d7172017-04-04 20:52:2064std::unique_ptr<base::TaskScheduler::InitParams>
65GetDefaultTaskSchedulerInitParams() {
fdoraya70d7172017-04-04 20:52:2066
fdoray743f8d6d2017-02-09 15:24:2367 constexpr int kMaxNumThreadsInBackgroundPool = 1;
fdoraye68b6f92017-02-21 19:39:4668 constexpr int kMaxNumThreadsInBackgroundBlockingPool = 1;
fdorayd2233a72016-12-13 17:18:2169 constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 2;
fdoraye68b6f92017-02-21 19:39:4670 constexpr int kMaxNumThreadsInForegroundBlockingPool = 1;
fdorayd2233a72016-12-13 17:18:2171 constexpr auto kSuggestedReclaimTime = base::TimeDelta::FromSeconds(30);
72
fdoraya70d7172017-04-04 20:52:2073 return base::MakeUnique<base::TaskScheduler::InitParams>(
Jeffrey Hefbf000f42017-07-26 22:44:4574 base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundPool,
fdoraya70d7172017-04-04 20:52:2075 kSuggestedReclaimTime),
Jeffrey Hefbf000f42017-07-26 22:44:4576 base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundBlockingPool,
fdoraya70d7172017-04-04 20:52:2077 kSuggestedReclaimTime),
78 base::SchedulerWorkerPoolParams(
Francois Doray6d3c649692017-06-16 19:20:2579 std::max(
80 kMaxNumThreadsInForegroundPoolLowerBound,
81 content::GetMinThreadsInRendererTaskSchedulerForegroundPool()),
fdoraya70d7172017-04-04 20:52:2082 kSuggestedReclaimTime),
Jeffrey Hefbf000f42017-07-26 22:44:4583 base::SchedulerWorkerPoolParams(kMaxNumThreadsInForegroundBlockingPool,
fdoraya70d7172017-04-04 20:52:2084 kSuggestedReclaimTime));
fdorayd2233a72016-12-13 17:18:2185}
86
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4587#if DCHECK_IS_ON() && defined(SYZYASAN)
88void V8DcheckCallbackHandler(const char* file, int line, const char* message) {
89 // TODO(siggi): Set a crash key or a breadcrumb so the fact that we hit a
90 // V8 DCHECK gets out in the crash report.
91 ::logging::LogMessage(file, line, logging::LOG_DCHECK).stream() << message;
92}
93#endif // DCHECK_IS_ON() && defined(SYZYASAN)
94
ishell75fddc12016-04-12 14:03:1495} // namespace
96
[email protected]eb398192012-10-22 20:16:1997namespace content {
98
fdoray31cc6f82017-02-10 23:31:1099RenderProcessImpl::RenderProcessImpl(
fdoraya70d7172017-04-04 20:52:20100 std::unique_ptr<base::TaskScheduler::InitParams> task_scheduler_init_params)
101 : RenderProcess("Renderer", std::move(task_scheduler_init_params)),
fdoray31cc6f82017-02-10 23:31:10102 enabled_bindings_(0) {
[email protected]396c3a462010-03-03 05:03:22103#if defined(OS_WIN)
104 // HACK: See http://b/issue?id=1024307 for rationale.
105 if (GetModuleHandle(L"LPK.DLL") == NULL) {
106 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
107 // when buffering into a EMF buffer for printing.
108 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
109 GdiInitializeLanguagePack gdi_init_lpk =
110 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
111 GetModuleHandle(L"GDI32.DLL"),
112 "GdiInitializeLanguagePack"));
113 DCHECK(gdi_init_lpk);
[email protected]00c39612010-03-06 02:53:28114 if (gdi_init_lpk) {
[email protected]396c3a462010-03-03 05:03:22115 gdi_init_lpk(0);
[email protected]00c39612010-03-06 02:53:28116 }
[email protected]396c3a462010-03-03 05:03:22117 }
[email protected]e68e62fa2009-02-20 02:00:04118#endif
119
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45120#if DCHECK_IS_ON() && defined(SYZYASAN)
121 // SyzyASAN official builds can ship with DCHECKs compiled in. Failing DCHECKs
122 // then are either fatal or simply log the error, based on a feature flag.
123 // Make sure V8 follows suit by setting a Dcheck handler that forwards to
124 // the Chrome base logging implementation.
125 v8::V8::SetDcheckErrorHandler(&V8DcheckCallbackHandler);
126
127 if (!base::FeatureList::IsEnabled(base::kSyzyAsanDCheckIsFatalFeature)) {
128 // These V8 flags default on in this build configuration. This triggers
129 // additional verification and code generation, which both slows down V8,
130 // and can lead to fatal CHECKs. Turn these flags down to get something
131 // closer to V8s normal performance and behavior.
132 constexpr char kDisabledFlags[] =
133 "--noturbo_verify "
134 "--noverify_csa "
135 "--noturbo_verify_allocation "
136 "--nodebug_code";
137
138 v8::V8::SetFlagsFromString(kDisabledFlags, sizeof(kDisabledFlags));
139 }
140#endif // DCHECK_IS_ON() && defined(SYZYASAN)
141
[email protected]35b4f0c2014-06-26 16:55:27142 if (base::SysInfo::IsLowEndDevice()) {
[email protected]067f5192014-01-29 05:22:09143 std::string optimize_flag("--optimize-for-size");
144 v8::V8::SetFlagsFromString(optimize_flag.c_str(),
145 static_cast<int>(optimize_flag.size()));
146 }
[email protected]987422f2013-10-01 10:33:31147
ishell75fddc12016-04-12 14:03:14148 SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping,
149 "--noharmony-shipping");
150 SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony");
Kouhei Ueno7f2eeeb2017-09-20 10:34:54151 SetV8FlagIfFeature(features::kModuleScriptsDynamicImport,
152 "--harmony-dynamic-import");
Hiroshige Hayashizaki15c2eb92017-10-20 04:57:05153 SetV8FlagIfFeature(features::kModuleScriptsImportMetaUrl,
154 "--harmony-import-meta");
bradnelsonc79f5a6f2016-10-10 18:31:14155 SetV8FlagIfFeature(features::kAsmJsToWebAssembly, "--validate-asm");
Brad Nelson2b2ec4a2017-08-17 21:56:07156 SetV8FlagIfNotFeature(features::kAsmJsToWebAssembly, "--no-validate-asm");
bradnelson2730e3512017-01-21 20:32:21157 SetV8FlagIfNotFeature(features::kWebAssembly,
158 "--wasm-disable-structured-cloning");
ishell75fddc12016-04-12 14:03:14159
Michael Hablich896d52662017-10-23 15:59:57160 SetV8FlagIfFeature(features::kV8VmFuture, "--future");
161 SetV8FlagIfNotFeature(features::kV8VmFuture, "--no-future");
162
Eric Holkdc499db2017-07-17 17:57:35163 SetV8FlagIfFeature(features::kWebAssemblyTrapHandler, "--wasm-trap-handler");
Eric Holkb4f60132017-08-18 19:37:41164 SetV8FlagIfNotFeature(features::kWebAssemblyTrapHandler,
165 "--no-wasm-trap-handler");
Eric Holkdc499db2017-07-17 17:57:35166#if defined(OS_LINUX) && defined(ARCH_CPU_X86_64) && !defined(OS_ANDROID)
167 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
168 base::debug::SetStackDumpFirstChanceCallback(v8::V8::TryHandleSignal);
169 }
170#endif
171
avi83883c82014-12-23 00:08:49172 const base::CommandLine& command_line =
173 *base::CommandLine::ForCurrentProcess();
ishell75fddc12016-04-12 14:03:14174
[email protected]396c3a462010-03-03 05:03:22175 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
[email protected]067f5192014-01-29 05:22:09176 std::string flags(
[email protected]95edc392010-07-30 22:00:38177 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
[email protected]067f5192014-01-29 05:22:09178 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
[email protected]396c3a462010-03-03 05:03:22179 }
[email protected]55dd9332013-09-04 17:17:50180
nick88299ba2015-06-16 23:57:41181 SiteIsolationStatsGatherer::SetEnabled(
182 GetContentClient()->renderer()->ShouldGatherSiteIsolationStats());
sammc7f6c6a02017-01-30 00:53:51183
184 if (command_line.HasSwitch(switches::kDomAutomationController))
185 enabled_bindings_ |= BINDINGS_POLICY_DOM_AUTOMATION;
186 if (command_line.HasSwitch(switches::kStatsCollectionController))
187 enabled_bindings_ |= BINDINGS_POLICY_STATS_COLLECTION;
[email protected]e68e62fa2009-02-20 02:00:04188}
189
[email protected]396c3a462010-03-03 05:03:22190RenderProcessImpl::~RenderProcessImpl() {
[email protected]396c3a462010-03-03 05:03:22191#ifndef NDEBUG
Blink Reformat1c4d759e2017-04-09 16:34:54192 int count = blink::WebFrame::InstanceCount();
[email protected]6bd867b2013-07-24 22:10:20193 if (count)
194 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
[email protected]396c3a462010-03-03 05:03:22195#endif
[email protected]e68e62fa2009-02-20 02:00:04196
[email protected]396c3a462010-03-03 05:03:22197 GetShutDownEvent()->Signal();
[email protected]396c3a462010-03-03 05:03:22198}
[email protected]e68e62fa2009-02-20 02:00:04199
fdoray31cc6f82017-02-10 23:31:10200std::unique_ptr<RenderProcess> RenderProcessImpl::Create() {
fdoraya70d7172017-04-04 20:52:20201 auto task_scheduler_init_params =
202 content::GetContentClient()->renderer()->GetTaskSchedulerInitParams();
203 if (!task_scheduler_init_params)
204 task_scheduler_init_params = GetDefaultTaskSchedulerInitParams();
fdoray31cc6f82017-02-10 23:31:10205
206 return base::WrapUnique(
fdoraya70d7172017-04-04 20:52:20207 new RenderProcessImpl(std::move(task_scheduler_init_params)));
fdoray31cc6f82017-02-10 23:31:10208}
209
[email protected]744c2a22012-03-15 18:42:04210void RenderProcessImpl::AddBindings(int bindings) {
211 enabled_bindings_ |= bindings;
212}
213
214int RenderProcessImpl::GetEnabledBindings() const {
215 return enabled_bindings_;
216}
217
[email protected]eb398192012-10-22 20:16:19218} // namespace content