jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/child_process_launcher_helper.h" |
| 6 | |
Ken Rockot | 86bea1c | 2017-05-16 06:21:35 | [diff] [blame] | 7 | #include "base/command_line.h" |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 8 | #include "base/metrics/histogram_macros.h" |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 9 | #include "base/no_destructor.h" |
| 10 | #include "base/single_thread_task_runner.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 11 | #include "base/task/lazy_task_runner.h" |
| 12 | #include "base/task/post_task.h" |
| 13 | #include "base/task/single_thread_task_runner_thread_mode.h" |
| 14 | #include "base/task/task_traits.h" |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 15 | #include "content/browser/child_process_launcher.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 16 | #include "content/public/browser/browser_task_traits.h" |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 17 | #include "content/public/browser/child_process_launcher_utils.h" |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 18 | #include "content/public/common/content_switches.h" |
| 19 | #include "content/public/common/sandboxed_process_launcher_delegate.h" |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 20 | #include "mojo/public/cpp/platform/platform_channel.h" |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 21 | |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 22 | #if defined(OS_ANDROID) |
| 23 | #include "content/browser/android/launcher_thread.h" |
| 24 | #endif |
| 25 | |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 26 | namespace content { |
| 27 | namespace internal { |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | void RecordHistogramsOnLauncherThread(base::TimeDelta launch_time) { |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 32 | DCHECK(CurrentlyOnProcessLauncherTaskRunner()); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 33 | // Log the launch time, separating out the first one (which will likely be |
| 34 | // slower due to the rest of the browser initializing at the same time). |
| 35 | static bool done_first_launch = false; |
| 36 | if (done_first_launch) { |
| 37 | UMA_HISTOGRAM_TIMES("MPArch.ChildProcessLaunchSubsequent", launch_time); |
| 38 | } else { |
| 39 | UMA_HISTOGRAM_TIMES("MPArch.ChildProcessLaunchFirst", launch_time); |
| 40 | done_first_launch = true; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | ChildProcessLauncherHelper::Process::Process(Process&& other) |
Tom Sepez | 006b815 | 2018-01-17 20:45:01 | [diff] [blame] | 47 | : process(std::move(other.process)) |
| 48 | #if BUILDFLAG(USE_ZYGOTE_HANDLE) |
| 49 | , |
| 50 | zygote(other.zygote) |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 51 | #endif |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | ChildProcessLauncherHelper::Process& |
| 56 | ChildProcessLauncherHelper::Process::Process::operator=( |
| 57 | ChildProcessLauncherHelper::Process&& other) { |
| 58 | DCHECK_NE(this, &other); |
| 59 | process = std::move(other.process); |
Tom Sepez | 006b815 | 2018-01-17 20:45:01 | [diff] [blame] | 60 | #if BUILDFLAG(USE_ZYGOTE_HANDLE) |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 61 | zygote = other.zygote; |
| 62 | #endif |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | ChildProcessLauncherHelper::ChildProcessLauncherHelper( |
| 67 | int child_process_id, |
| 68 | BrowserThread::ID client_thread_id, |
| 69 | std::unique_ptr<base::CommandLine> command_line, |
| 70 | std::unique_ptr<SandboxedProcessLauncherDelegate> delegate, |
| 71 | const base::WeakPtr<ChildProcessLauncher>& child_process_launcher, |
Dmitry Skiba | 1b7dbaf8 | 2017-11-09 19:32:02 | [diff] [blame] | 72 | bool terminate_on_shutdown, |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 73 | mojo::OutgoingInvitation mojo_invitation, |
| 74 | const mojo::ProcessErrorCallback& process_error_callback) |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 75 | : child_process_id_(child_process_id), |
| 76 | client_thread_id_(client_thread_id), |
| 77 | command_line_(std::move(command_line)), |
| 78 | delegate_(std::move(delegate)), |
| 79 | child_process_launcher_(child_process_launcher), |
Dmitry Skiba | 1b7dbaf8 | 2017-11-09 19:32:02 | [diff] [blame] | 80 | terminate_on_shutdown_(terminate_on_shutdown), |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 81 | mojo_invitation_(std::move(mojo_invitation)), |
Dmitry Skiba | 1b7dbaf8 | 2017-11-09 19:32:02 | [diff] [blame] | 82 | process_error_callback_(process_error_callback) {} |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 83 | |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 84 | ChildProcessLauncherHelper::~ChildProcessLauncherHelper() = default; |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 85 | |
| 86 | void ChildProcessLauncherHelper::StartLaunchOnClientThread() { |
| 87 | DCHECK_CURRENTLY_ON(client_thread_id_); |
| 88 | |
| 89 | BeforeLaunchOnClientThread(); |
| 90 | |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 91 | mojo_named_channel_ = CreateNamedPlatformChannelOnClientThread(); |
| 92 | if (!mojo_named_channel_) |
| 93 | mojo_channel_.emplace(); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 94 | |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 95 | GetProcessLauncherTaskRunner()->PostTask( |
| 96 | FROM_HERE, |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 97 | base::BindOnce(&ChildProcessLauncherHelper::LaunchOnLauncherThread, |
| 98 | this)); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void ChildProcessLauncherHelper::LaunchOnLauncherThread() { |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 102 | DCHECK(CurrentlyOnProcessLauncherTaskRunner()); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 103 | |
| 104 | begin_launch_time_ = base::TimeTicks::Now(); |
| 105 | |
| 106 | std::unique_ptr<FileMappedForLaunch> files_to_register = GetFilesToMap(); |
| 107 | |
| 108 | bool is_synchronous_launch = true; |
| 109 | int launch_result = LAUNCH_RESULT_FAILURE; |
| 110 | base::LaunchOptions options; |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 111 | |
Greg Kerr | a1bc9d0 | 2018-01-04 23:22:31 | [diff] [blame] | 112 | Process process; |
| 113 | if (BeforeLaunchOnLauncherThread(*files_to_register, &options)) { |
| 114 | process = |
| 115 | LaunchProcessOnLauncherThread(options, std::move(files_to_register), |
| 116 | &is_synchronous_launch, &launch_result); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 117 | |
Greg Kerr | a1bc9d0 | 2018-01-04 23:22:31 | [diff] [blame] | 118 | AfterLaunchOnLauncherThread(process, options); |
| 119 | } |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 120 | |
| 121 | if (is_synchronous_launch) { |
boliu | 5674fee | 2017-04-26 23:41:59 | [diff] [blame] | 122 | PostLaunchOnLauncherThread(std::move(process), launch_result); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | void ChildProcessLauncherHelper::PostLaunchOnLauncherThread( |
| 127 | ChildProcessLauncherHelper::Process process, |
boliu | 5674fee | 2017-04-26 23:41:59 | [diff] [blame] | 128 | int launch_result) { |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 129 | if (mojo_channel_) |
Wez | dc98dbfd | 2018-06-05 19:49:42 | [diff] [blame] | 130 | mojo_channel_->RemoteProcessLaunchAttempted(); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 131 | |
| 132 | if (process.process.IsValid()) { |
boliu | 5674fee | 2017-04-26 23:41:59 | [diff] [blame] | 133 | RecordHistogramsOnLauncherThread(base::TimeTicks::Now() - |
| 134 | begin_launch_time_); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 135 | } |
| 136 | |
Dmitry Skiba | 1b7dbaf8 | 2017-11-09 19:32:02 | [diff] [blame] | 137 | // Take ownership of the broker client invitation here so it's destroyed when |
| 138 | // we go out of scope regardless of the outcome below. |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 139 | mojo::OutgoingInvitation invitation = std::move(mojo_invitation_); |
Dmitry Skiba | 1b7dbaf8 | 2017-11-09 19:32:02 | [diff] [blame] | 140 | if (process.process.IsValid()) { |
| 141 | // Set up Mojo IPC to the new process. |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 142 | if (mojo_channel_) { |
| 143 | DCHECK(mojo_channel_->local_endpoint().is_valid()); |
| 144 | mojo::OutgoingInvitation::Send( |
| 145 | std::move(invitation), process.process.Handle(), |
| 146 | mojo_channel_->TakeLocalEndpoint(), process_error_callback_); |
| 147 | } else { |
| 148 | DCHECK(mojo_named_channel_); |
| 149 | mojo::OutgoingInvitation::Send( |
| 150 | std::move(invitation), process.process.Handle(), |
| 151 | mojo_named_channel_->TakeServerEndpoint(), process_error_callback_); |
| 152 | } |
Dmitry Skiba | 1b7dbaf8 | 2017-11-09 19:32:02 | [diff] [blame] | 153 | } |
| 154 | |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 155 | base::PostTaskWithTraits( |
| 156 | FROM_HERE, {client_thread_id_}, |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 157 | base::BindOnce(&ChildProcessLauncherHelper::PostLaunchOnClientThread, |
tzik | ccf160c | 2018-02-20 12:43:13 | [diff] [blame] | 158 | this, std::move(process), launch_result)); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void ChildProcessLauncherHelper::PostLaunchOnClientThread( |
| 162 | ChildProcessLauncherHelper::Process process, |
| 163 | int error_code) { |
| 164 | if (child_process_launcher_) { |
Dmitry Skiba | 1b7dbaf8 | 2017-11-09 19:32:02 | [diff] [blame] | 165 | child_process_launcher_->Notify(std::move(process), error_code); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 166 | } else if (process.process.IsValid() && terminate_on_shutdown_) { |
| 167 | // Client is gone, terminate the process. |
| 168 | ForceNormalProcessTerminationAsync(std::move(process)); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | std::string ChildProcessLauncherHelper::GetProcessType() { |
| 173 | return command_line()->GetSwitchValueASCII(switches::kProcessType); |
| 174 | } |
| 175 | |
| 176 | // static |
| 177 | void ChildProcessLauncherHelper::ForceNormalProcessTerminationAsync( |
| 178 | ChildProcessLauncherHelper::Process process) { |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 179 | if (CurrentlyOnProcessLauncherTaskRunner()) { |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 180 | ForceNormalProcessTerminationSync(std::move(process)); |
| 181 | return; |
| 182 | } |
| 183 | // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! |
| 184 | // So don't do this on the UI/IO threads. |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 185 | GetProcessLauncherTaskRunner()->PostTask( |
| 186 | FROM_HERE, |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 187 | base::BindOnce( |
| 188 | &ChildProcessLauncherHelper::ForceNormalProcessTerminationSync, |
tzik | ccf160c | 2018-02-20 12:43:13 | [diff] [blame] | 189 | std::move(process))); |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // namespace internal |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 193 | |
| 194 | // static |
| 195 | base::SingleThreadTaskRunner* GetProcessLauncherTaskRunner() { |
| 196 | #if defined(OS_ANDROID) |
| 197 | // Android specializes Launcher thread so it is accessible in java. |
| 198 | // Note Android never does clean shutdown, so shutdown use-after-free |
| 199 | // concerns are not a problem in practice. |
| 200 | // This process launcher thread will use the Java-side process-launching |
| 201 | // thread, instead of creating its own separate thread on C++ side. Note |
| 202 | // that means this thread will not be joined on shutdown, and may cause |
| 203 | // use-after-free if anything tries to access objects deleted by |
| 204 | // AtExitManager, such as non-leaky LazyInstance. |
| 205 | static base::NoDestructor<scoped_refptr<base::SingleThreadTaskRunner>> |
| 206 | launcher_task_runner( |
| 207 | android::LauncherThread::GetMessageLoop()->task_runner()); |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 208 | return (*launcher_task_runner).get(); |
Sunny Sachanandani | c16fdeb | 2018-04-25 00:25:48 | [diff] [blame] | 209 | #else // defined(OS_ANDROID) |
| 210 | // TODO(http://crbug.com/820200): Investigate whether we could use |
| 211 | // SequencedTaskRunner on platforms other than Windows. |
Xi Han | 9340143c | 2018-03-16 20:50:49 | [diff] [blame] | 212 | static base::LazySingleThreadTaskRunner launcher_task_runner = |
| 213 | LAZY_SINGLE_THREAD_TASK_RUNNER_INITIALIZER( |
| 214 | base::TaskTraits({base::MayBlock(), base::TaskPriority::USER_BLOCKING, |
Sunny Sachanandani | c16fdeb | 2018-04-25 00:25:48 | [diff] [blame] | 215 | base::TaskShutdownBehavior::BLOCK_SHUTDOWN}), |
Xi Han | 9340143c | 2018-03-16 20:50:49 | [diff] [blame] | 216 | base::SingleThreadTaskRunnerThreadMode::DEDICATED); |
| 217 | return launcher_task_runner.Get().get(); |
| 218 | #endif // defined(OS_ANDROID) |
Xi Han | 89d93df | 2018-03-09 20:55:07 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // static |
| 222 | bool CurrentlyOnProcessLauncherTaskRunner() { |
| 223 | return GetProcessLauncherTaskRunner()->RunsTasksInCurrentSequence(); |
| 224 | } |
| 225 | |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 226 | } // namespace content |