| [email protected] | fd911dd | 2012-01-27 01:57:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| [email protected] | d353541f | 2012-05-03 22:45:41 | [diff] [blame] | 5 | #include "content/renderer/render_process_impl.h" |
| 6 | |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 7 | #include "build/build_config.h" |
| 8 | |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 9 | #if defined(OS_WIN) |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include <windows.h> |
| 11 | #include <objidl.h> |
| 12 | #include <mlang.h> |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 13 | #endif |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 15 | #include <stddef.h> |
| 16 | |
| fdoray | 743f8d6d | 2017-02-09 15:24:23 | [diff] [blame] | 17 | #include <algorithm> |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 18 | #include <utility> |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 19 | |
| Eric Holk | 1384f6d | 2018-01-05 00:49:36 | [diff] [blame] | 20 | #include "base/base_switches.h" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 21 | #include "base/bind.h" |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | #include "base/command_line.h" |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 23 | #include "base/compiler_specific.h" |
| georgesak | 80353b5 | 2017-01-10 21:18:51 | [diff] [blame] | 24 | #include "base/debug/crash_logging.h" |
| Eric Holk | dc499db | 2017-07-17 17:57:35 | [diff] [blame] | 25 | #include "base/debug/stack_trace.h" |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 26 | #include "base/feature_list.h" |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 27 | #include "base/memory/ptr_util.h" |
| [email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 28 | #include "base/sys_info.h" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 29 | #include "base/task_scheduler/initialization_util.h" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 30 | #include "base/time/time.h" |
| Francois Doray | 6d3c64969 | 2017-06-16 19:20:25 | [diff] [blame] | 31 | #include "content/common/task_scheduler.h" |
| sammc | 7f6c6a0 | 2017-01-30 00:53:51 | [diff] [blame] | 32 | #include "content/public/common/bindings_policy.h" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 33 | #include "content/public/common/content_client.h" |
| bradnelson | c79f5a6f | 2016-10-10 18:31:14 | [diff] [blame] | 34 | #include "content/public/common/content_features.h" |
| [email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 35 | #include "content/public/common/content_switches.h" |
| [email protected] | d344114c | 2011-10-01 01:24:34 | [diff] [blame] | 36 | #include "content/public/renderer/content_renderer_client.h" |
| Eric Holk | 1384f6d | 2018-01-05 00:49:36 | [diff] [blame] | 37 | #include "services/service_manager/embedder/switches.h" |
| Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame^] | 38 | #include "third_party/blink/public/web/web_frame.h" |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 39 | #include "v8/include/v8.h" |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | |
| georgesak | 80353b5 | 2017-01-10 21:18:51 | [diff] [blame] | 41 | #if defined(OS_WIN) |
| 42 | #include "base/win/win_util.h" |
| 43 | #endif |
| 44 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 45 | namespace { |
| 46 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 47 | void 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 | |
| bradnelson | 2730e351 | 2017-01-21 20:32:21 | [diff] [blame] | 53 | void 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 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 59 | void 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 | |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 65 | std::unique_ptr<base::TaskScheduler::InitParams> |
| 66 | GetDefaultTaskSchedulerInitParams() { |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 67 | |
| fdoray | 743f8d6d | 2017-02-09 15:24:23 | [diff] [blame] | 68 | constexpr int kMaxNumThreadsInBackgroundPool = 1; |
| fdoray | e68b6f9 | 2017-02-21 19:39:46 | [diff] [blame] | 69 | constexpr int kMaxNumThreadsInBackgroundBlockingPool = 1; |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 70 | constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 2; |
| fdoray | e68b6f9 | 2017-02-21 19:39:46 | [diff] [blame] | 71 | constexpr int kMaxNumThreadsInForegroundBlockingPool = 1; |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 72 | constexpr auto kSuggestedReclaimTime = base::TimeDelta::FromSeconds(30); |
| 73 | |
| Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 74 | return std::make_unique<base::TaskScheduler::InitParams>( |
| Jeffrey He | fbf000f4 | 2017-07-26 22:44:45 | [diff] [blame] | 75 | base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundPool, |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 76 | kSuggestedReclaimTime), |
| Jeffrey He | fbf000f4 | 2017-07-26 22:44:45 | [diff] [blame] | 77 | base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundBlockingPool, |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 78 | kSuggestedReclaimTime), |
| 79 | base::SchedulerWorkerPoolParams( |
| Francois Doray | 6d3c64969 | 2017-06-16 19:20:25 | [diff] [blame] | 80 | std::max( |
| 81 | kMaxNumThreadsInForegroundPoolLowerBound, |
| 82 | content::GetMinThreadsInRendererTaskSchedulerForegroundPool()), |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 83 | kSuggestedReclaimTime), |
| Jeffrey He | fbf000f4 | 2017-07-26 22:44:45 | [diff] [blame] | 84 | base::SchedulerWorkerPoolParams(kMaxNumThreadsInForegroundBlockingPool, |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 85 | kSuggestedReclaimTime)); |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 86 | } |
| 87 | |
| Wez | a6ca5b9 | 2018-03-23 19:03:07 | [diff] [blame] | 88 | #if DCHECK_IS_CONFIGURABLE |
| Sigurdur Asgeirsson | 379c51e4 | 2017-09-21 12:52:45 | [diff] [blame] | 89 | void 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 | } |
| Wez | a6ca5b9 | 2018-03-23 19:03:07 | [diff] [blame] | 94 | #endif // DCHECK_IS_CONFIGURABLE |
| Sigurdur Asgeirsson | 379c51e4 | 2017-09-21 12:52:45 | [diff] [blame] | 95 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 96 | } // namespace |
| 97 | |
| [email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 98 | namespace content { |
| 99 | |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 100 | RenderProcessImpl::RenderProcessImpl( |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 101 | std::unique_ptr<base::TaskScheduler::InitParams> task_scheduler_init_params) |
| 102 | : RenderProcess("Renderer", std::move(task_scheduler_init_params)), |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 103 | enabled_bindings_(0) { |
| Wez | a6ca5b9 | 2018-03-23 19:03:07 | [diff] [blame] | 104 | #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 Asgeirsson | 379c51e4 | 2017-09-21 12:52:45 | [diff] [blame] | 107 | // 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 | |
| Wez | a6ca5b9 | 2018-03-23 19:03:07 | [diff] [blame] | 111 | if (!base::FeatureList::IsEnabled(base::kDCheckIsFatalFeature)) { |
| Sigurdur Asgeirsson | 379c51e4 | 2017-09-21 12:52:45 | [diff] [blame] | 112 | // 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 " |
| 118 | "--noverify_csa " |
| 119 | "--noturbo_verify_allocation " |
| 120 | "--nodebug_code"; |
| 121 | |
| 122 | v8::V8::SetFlagsFromString(kDisabledFlags, sizeof(kDisabledFlags)); |
| 123 | } |
| Wez | a6ca5b9 | 2018-03-23 19:03:07 | [diff] [blame] | 124 | #endif // DCHECK_IS_CONFIGURABLE |
| Sigurdur Asgeirsson | 379c51e4 | 2017-09-21 12:52:45 | [diff] [blame] | 125 | |
| [email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 126 | if (base::SysInfo::IsLowEndDevice()) { |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 127 | std::string optimize_flag("--optimize-for-size"); |
| 128 | v8::V8::SetFlagsFromString(optimize_flag.c_str(), |
| 129 | static_cast<int>(optimize_flag.size())); |
| 130 | } |
| [email protected] | 987422f | 2013-10-01 10:33:31 | [diff] [blame] | 131 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 132 | SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping, |
| 133 | "--noharmony-shipping"); |
| 134 | SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony"); |
| Kouhei Ueno | 7f2eeeb | 2017-09-20 10:34:54 | [diff] [blame] | 135 | SetV8FlagIfFeature(features::kModuleScriptsDynamicImport, |
| 136 | "--harmony-dynamic-import"); |
| Hiroshige Hayashizaki | 15c2eb9 | 2017-10-20 04:57:05 | [diff] [blame] | 137 | SetV8FlagIfFeature(features::kModuleScriptsImportMetaUrl, |
| 138 | "--harmony-import-meta"); |
| bradnelson | c79f5a6f | 2016-10-10 18:31:14 | [diff] [blame] | 139 | SetV8FlagIfFeature(features::kAsmJsToWebAssembly, "--validate-asm"); |
| Brad Nelson | 2b2ec4a | 2017-08-17 21:56:07 | [diff] [blame] | 140 | SetV8FlagIfNotFeature(features::kAsmJsToWebAssembly, "--no-validate-asm"); |
| bradnelson | 2730e351 | 2017-01-21 20:32:21 | [diff] [blame] | 141 | SetV8FlagIfNotFeature(features::kWebAssembly, |
| 142 | "--wasm-disable-structured-cloning"); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 143 | |
| Michael Hablich | 896d5266 | 2017-10-23 15:59:57 | [diff] [blame] | 144 | SetV8FlagIfFeature(features::kV8VmFuture, "--future"); |
| 145 | SetV8FlagIfNotFeature(features::kV8VmFuture, "--no-future"); |
| Brad Nelson | 5cd500e | 2018-01-04 00:50:19 | [diff] [blame] | 146 | SetV8FlagIfFeature(features::kSharedArrayBuffer, |
| 147 | "--harmony-sharedarraybuffer"); |
| 148 | SetV8FlagIfNotFeature(features::kSharedArrayBuffer, |
| 149 | "--no-harmony-sharedarraybuffer"); |
| Michael Hablich | 896d5266 | 2017-10-23 15:59:57 | [diff] [blame] | 150 | |
| Eric Holk | b4f6013 | 2017-08-18 19:37:41 | [diff] [blame] | 151 | SetV8FlagIfNotFeature(features::kWebAssemblyTrapHandler, |
| 152 | "--no-wasm-trap-handler"); |
| Sathya Gunasekaran | bc706d6 | 2018-02-09 21:44:22 | [diff] [blame] | 153 | SetV8FlagIfFeature(features::kArrayPrototypeValues, |
| 154 | "--harmony-array-prototype-values"); |
| 155 | SetV8FlagIfNotFeature(features::kArrayPrototypeValues, |
| Eric Holk | 90f2d93 | 2018-03-21 23:07:47 | [diff] [blame] | 156 | "--no-harmony-array-prototype-values"); |
| Eric Holk | 2c28bc9e | 2018-04-02 21:59:44 | [diff] [blame] | 157 | #if defined(OS_LINUX) && defined(ARCH_CPU_X86_64) && !defined(OS_ANDROID) |
| Eric Holk | dc499db | 2017-07-17 17:57:35 | [diff] [blame] | 158 | if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) { |
| Eric Holk | 90f2d93 | 2018-03-21 23:07:47 | [diff] [blame] | 159 | bool use_v8_signal_handler = false; |
| Eric Holk | 1384f6d | 2018-01-05 00:49:36 | [diff] [blame] | 160 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 161 | if (!command_line->HasSwitch( |
| 162 | service_manager::switches::kDisableInProcessStackTraces)) { |
| 163 | base::debug::SetStackDumpFirstChanceCallback(v8::V8::TryHandleSignal); |
| 164 | } else if (!command_line->HasSwitch(switches::kEnableCrashReporter) && |
| 165 | !command_line->HasSwitch( |
| 166 | switches::kEnableCrashReporterForTesting)) { |
| 167 | // If we are using WebAssembly trap handling but both Breakpad and |
| 168 | // in-process stack traces are disabled then there will be no signal |
| 169 | // handler. In this case, we fall back on V8's default handler |
| 170 | // (https://crbug.com/798150). |
| Eric Holk | 90f2d93 | 2018-03-21 23:07:47 | [diff] [blame] | 171 | use_v8_signal_handler = true; |
| Eric Holk | 1384f6d | 2018-01-05 00:49:36 | [diff] [blame] | 172 | } |
| Eric Holk | 2c28bc9e | 2018-04-02 21:59:44 | [diff] [blame] | 173 | // TODO(eholk): report UMA stat for how often this succeeds |
| 174 | v8::V8::EnableWebAssemblyTrapHandler(use_v8_signal_handler); |
| Eric Holk | dc499db | 2017-07-17 17:57:35 | [diff] [blame] | 175 | } |
| 176 | #endif |
| 177 | |
| avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 178 | const base::CommandLine& command_line = |
| 179 | *base::CommandLine::ForCurrentProcess(); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 180 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 181 | if (command_line.HasSwitch(switches::kJavaScriptFlags)) { |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 182 | std::string flags( |
| [email protected] | 95edc39 | 2010-07-30 22:00:38 | [diff] [blame] | 183 | command_line.GetSwitchValueASCII(switches::kJavaScriptFlags)); |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 184 | v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size())); |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 185 | } |
| [email protected] | 55dd933 | 2013-09-04 17:17:50 | [diff] [blame] | 186 | |
| sammc | 7f6c6a0 | 2017-01-30 00:53:51 | [diff] [blame] | 187 | if (command_line.HasSwitch(switches::kDomAutomationController)) |
| 188 | enabled_bindings_ |= BINDINGS_POLICY_DOM_AUTOMATION; |
| 189 | if (command_line.HasSwitch(switches::kStatsCollectionController)) |
| 190 | enabled_bindings_ |= BINDINGS_POLICY_STATS_COLLECTION; |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 191 | } |
| 192 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 193 | RenderProcessImpl::~RenderProcessImpl() { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 194 | #ifndef NDEBUG |
| Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 195 | int count = blink::WebFrame::InstanceCount(); |
| [email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 196 | if (count) |
| 197 | DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES"; |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 198 | #endif |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 199 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 200 | GetShutDownEvent()->Signal(); |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 201 | } |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 202 | |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 203 | std::unique_ptr<RenderProcess> RenderProcessImpl::Create() { |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 204 | auto task_scheduler_init_params = |
| 205 | content::GetContentClient()->renderer()->GetTaskSchedulerInitParams(); |
| 206 | if (!task_scheduler_init_params) |
| 207 | task_scheduler_init_params = GetDefaultTaskSchedulerInitParams(); |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 208 | |
| 209 | return base::WrapUnique( |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 210 | new RenderProcessImpl(std::move(task_scheduler_init_params))); |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 211 | } |
| 212 | |
| [email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame] | 213 | void RenderProcessImpl::AddBindings(int bindings) { |
| 214 | enabled_bindings_ |= bindings; |
| 215 | } |
| 216 | |
| 217 | int RenderProcessImpl::GetEnabledBindings() const { |
| 218 | return enabled_bindings_; |
| 219 | } |
| 220 | |
| [email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 221 | } // namespace content |