| [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 | |
| 20 | #include "base/bind.h" |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | #include "base/command_line.h" |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 22 | #include "base/compiler_specific.h" |
| georgesak | 80353b5 | 2017-01-10 21:18:51 | [diff] [blame] | 23 | #include "base/debug/crash_logging.h" |
| Eric Holk | dc499db | 2017-07-17 17:57:35 | [diff] [blame] | 24 | #include "base/debug/stack_trace.h" |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 25 | #include "base/feature_list.h" |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 26 | #include "base/memory/ptr_util.h" |
| [email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 27 | #include "base/sys_info.h" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 28 | #include "base/task_scheduler/initialization_util.h" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 29 | #include "base/time/time.h" |
| Francois Doray | 6d3c64969 | 2017-06-16 19:20:25 | [diff] [blame] | 30 | #include "content/common/task_scheduler.h" |
| sammc | 7f6c6a0 | 2017-01-30 00:53:51 | [diff] [blame] | 31 | #include "content/public/common/bindings_policy.h" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 32 | #include "content/public/common/content_client.h" |
| bradnelson | c79f5a6f | 2016-10-10 18:31:14 | [diff] [blame] | 33 | #include "content/public/common/content_features.h" |
| [email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 34 | #include "content/public/common/content_switches.h" |
| [email protected] | d344114c | 2011-10-01 01:24:34 | [diff] [blame] | 35 | #include "content/public/renderer/content_renderer_client.h" |
| John Abd-El-Malek | 6b56ef71 | 2017-10-21 22:52:46 | [diff] [blame] | 36 | #include "content/renderer/loader/site_isolation_stats_gatherer.h" |
| [email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 37 | #include "third_party/WebKit/public/web/WebFrame.h" |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 38 | #include "v8/include/v8.h" |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | |
| georgesak | 80353b5 | 2017-01-10 21:18:51 | [diff] [blame] | 40 | #if defined(OS_WIN) |
| 41 | #include "base/win/win_util.h" |
| 42 | #endif |
| 43 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 44 | namespace { |
| 45 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 46 | void 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 | |
| bradnelson | 2730e351 | 2017-01-21 20:32:21 | [diff] [blame] | 52 | void 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 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 58 | void 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 | |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 64 | std::unique_ptr<base::TaskScheduler::InitParams> |
| 65 | GetDefaultTaskSchedulerInitParams() { |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 66 | |
| fdoray | 743f8d6d | 2017-02-09 15:24:23 | [diff] [blame] | 67 | constexpr int kMaxNumThreadsInBackgroundPool = 1; |
| fdoray | e68b6f9 | 2017-02-21 19:39:46 | [diff] [blame] | 68 | constexpr int kMaxNumThreadsInBackgroundBlockingPool = 1; |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 69 | constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 2; |
| fdoray | e68b6f9 | 2017-02-21 19:39:46 | [diff] [blame] | 70 | constexpr int kMaxNumThreadsInForegroundBlockingPool = 1; |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 71 | constexpr auto kSuggestedReclaimTime = base::TimeDelta::FromSeconds(30); |
| 72 | |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 73 | return base::MakeUnique<base::TaskScheduler::InitParams>( |
| Jeffrey He | fbf000f4 | 2017-07-26 22:44:45 | [diff] [blame] | 74 | base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundPool, |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 75 | kSuggestedReclaimTime), |
| Jeffrey He | fbf000f4 | 2017-07-26 22:44:45 | [diff] [blame] | 76 | base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundBlockingPool, |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 77 | kSuggestedReclaimTime), |
| 78 | base::SchedulerWorkerPoolParams( |
| Francois Doray | 6d3c64969 | 2017-06-16 19:20:25 | [diff] [blame] | 79 | std::max( |
| 80 | kMaxNumThreadsInForegroundPoolLowerBound, |
| 81 | content::GetMinThreadsInRendererTaskSchedulerForegroundPool()), |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 82 | kSuggestedReclaimTime), |
| Jeffrey He | fbf000f4 | 2017-07-26 22:44:45 | [diff] [blame] | 83 | base::SchedulerWorkerPoolParams(kMaxNumThreadsInForegroundBlockingPool, |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 84 | kSuggestedReclaimTime)); |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 85 | } |
| 86 | |
| Sigurdur Asgeirsson | 379c51e4 | 2017-09-21 12:52:45 | [diff] [blame] | 87 | #if DCHECK_IS_ON() && defined(SYZYASAN) |
| 88 | void 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 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 95 | } // namespace |
| 96 | |
| [email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 97 | namespace content { |
| 98 | |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 99 | RenderProcessImpl::RenderProcessImpl( |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 100 | std::unique_ptr<base::TaskScheduler::InitParams> task_scheduler_init_params) |
| 101 | : RenderProcess("Renderer", std::move(task_scheduler_init_params)), |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 102 | enabled_bindings_(0) { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 103 | #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] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 114 | if (gdi_init_lpk) { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 115 | gdi_init_lpk(0); |
| [email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 116 | } |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 117 | } |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 118 | #endif |
| 119 | |
| Sigurdur Asgeirsson | 379c51e4 | 2017-09-21 12:52:45 | [diff] [blame] | 120 | #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] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 142 | if (base::SysInfo::IsLowEndDevice()) { |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 143 | 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] | 987422f | 2013-10-01 10:33:31 | [diff] [blame] | 147 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 148 | SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping, |
| 149 | "--noharmony-shipping"); |
| 150 | SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony"); |
| Kouhei Ueno | 7f2eeeb | 2017-09-20 10:34:54 | [diff] [blame] | 151 | SetV8FlagIfFeature(features::kModuleScriptsDynamicImport, |
| 152 | "--harmony-dynamic-import"); |
| Hiroshige Hayashizaki | 15c2eb9 | 2017-10-20 04:57:05 | [diff] [blame] | 153 | SetV8FlagIfFeature(features::kModuleScriptsImportMetaUrl, |
| 154 | "--harmony-import-meta"); |
| bradnelson | c79f5a6f | 2016-10-10 18:31:14 | [diff] [blame] | 155 | SetV8FlagIfFeature(features::kAsmJsToWebAssembly, "--validate-asm"); |
| Brad Nelson | 2b2ec4a | 2017-08-17 21:56:07 | [diff] [blame] | 156 | SetV8FlagIfNotFeature(features::kAsmJsToWebAssembly, "--no-validate-asm"); |
| bradnelson | 2730e351 | 2017-01-21 20:32:21 | [diff] [blame] | 157 | SetV8FlagIfNotFeature(features::kWebAssembly, |
| 158 | "--wasm-disable-structured-cloning"); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 159 | |
| Michael Hablich | 896d5266 | 2017-10-23 15:59:57 | [diff] [blame^] | 160 | SetV8FlagIfFeature(features::kV8VmFuture, "--future"); |
| 161 | SetV8FlagIfNotFeature(features::kV8VmFuture, "--no-future"); |
| 162 | |
| Eric Holk | dc499db | 2017-07-17 17:57:35 | [diff] [blame] | 163 | SetV8FlagIfFeature(features::kWebAssemblyTrapHandler, "--wasm-trap-handler"); |
| Eric Holk | b4f6013 | 2017-08-18 19:37:41 | [diff] [blame] | 164 | SetV8FlagIfNotFeature(features::kWebAssemblyTrapHandler, |
| 165 | "--no-wasm-trap-handler"); |
| Eric Holk | dc499db | 2017-07-17 17:57:35 | [diff] [blame] | 166 | #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 | |
| avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 172 | const base::CommandLine& command_line = |
| 173 | *base::CommandLine::ForCurrentProcess(); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 174 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 175 | if (command_line.HasSwitch(switches::kJavaScriptFlags)) { |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 176 | std::string flags( |
| [email protected] | 95edc39 | 2010-07-30 22:00:38 | [diff] [blame] | 177 | command_line.GetSwitchValueASCII(switches::kJavaScriptFlags)); |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 178 | v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size())); |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 179 | } |
| [email protected] | 55dd933 | 2013-09-04 17:17:50 | [diff] [blame] | 180 | |
| nick | 88299ba | 2015-06-16 23:57:41 | [diff] [blame] | 181 | SiteIsolationStatsGatherer::SetEnabled( |
| 182 | GetContentClient()->renderer()->ShouldGatherSiteIsolationStats()); |
| sammc | 7f6c6a0 | 2017-01-30 00:53:51 | [diff] [blame] | 183 | |
| 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] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 188 | } |
| 189 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 190 | RenderProcessImpl::~RenderProcessImpl() { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 191 | #ifndef NDEBUG |
| Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 192 | int count = blink::WebFrame::InstanceCount(); |
| [email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 193 | if (count) |
| 194 | DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES"; |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 195 | #endif |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 196 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 197 | GetShutDownEvent()->Signal(); |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 198 | } |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 199 | |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 200 | std::unique_ptr<RenderProcess> RenderProcessImpl::Create() { |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 201 | auto task_scheduler_init_params = |
| 202 | content::GetContentClient()->renderer()->GetTaskSchedulerInitParams(); |
| 203 | if (!task_scheduler_init_params) |
| 204 | task_scheduler_init_params = GetDefaultTaskSchedulerInitParams(); |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 205 | |
| 206 | return base::WrapUnique( |
| fdoray | a70d717 | 2017-04-04 20:52:20 | [diff] [blame] | 207 | new RenderProcessImpl(std::move(task_scheduler_init_params))); |
| fdoray | 31cc6f8 | 2017-02-10 23:31:10 | [diff] [blame] | 208 | } |
| 209 | |
| [email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame] | 210 | void RenderProcessImpl::AddBindings(int bindings) { |
| 211 | enabled_bindings_ |= bindings; |
| 212 | } |
| 213 | |
| 214 | int RenderProcessImpl::GetEnabledBindings() const { |
| 215 | return enabled_bindings_; |
| 216 | } |
| 217 | |
| [email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 218 | } // namespace content |