1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_buildflags.h"
10 #include "base/command_line.h"
11 #include "base/functional/bind.h"
12 #include "base/functional/callback_helpers.h"
13 #include "base/sampling_heap_profiler/poisson_allocation_sampler.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
16 #include "chrome/app/chrome_main_delegate.h"
17 #include "chrome/browser/headless/headless_mode_util.h"
18 #include "chrome/common/buildflags.h"
19 #include "chrome/common/chrome_result_codes.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/profiler/main_thread_stack_sampling_profiler.h"
22 #include "content/public/app/content_main.h"
23 #include "content/public/common/content_switches.h"
24 #include "headless/public/headless_shell.h"
25 #include "headless/public/switches.h"
28 #include "chrome/app/chrome_main_mac.h"
29 #include "chrome/app/notification_metrics.h"
32 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
33 #include "base/base_switches.h"
36 #if BUILDFLAG(IS_LINUX)
37 #include "chrome/app/chrome_main_linux.h"
41 #include "base/dcheck_is_on.h"
42 #include "base/debug/handle_hooks_win.h"
43 #include "base/win/current_module.h"
47 #include "base/debug/dump_without_crashing.h"
48 #include "base/win/win_util.h"
49 #include "chrome/chrome_elf/chrome_elf_main.h"
50 #include "chrome/common/chrome_constants.h"
51 #include "chrome/install_static/initialize_from_primary_module.h"
52 #include "chrome/install_static/install_details.h"
54 #define DLLEXPORT __declspec(dllexport)
56 // We use extern C for the prototype DLLEXPORT to avoid C++ name mangling.
58 DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance,
59 sandbox::SandboxInterfaceInfo* sandbox_info,
60 int64_t exe_entry_point_ticks);
62 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
64 // This function must be marked with NO_STACK_PROTECTOR or it may crash on
65 // return, see the --change-stack-guard-on-fork command line flag.
66 __attribute__((visibility("default"))) int NO_STACK_PROTECTOR
67 ChromeMain(int argc, const char** argv);
70 #error Unknown platform.
74 DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance,
75 sandbox::SandboxInterfaceInfo* sandbox_info,
76 int64_t exe_entry_point_ticks) {
77 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
78 int ChromeMain(int argc, const char** argv) {
79 int64_t exe_entry_point_ticks = 0;
81 #error Unknown platform.
85 install_static::InitializeFromPrimaryModule();
86 #if !defined(COMPONENT_BUILD) && DCHECK_IS_ON()
87 // Patch the main EXE on non-component builds when DCHECKs are enabled.
88 // This allows detection of third party code that might attempt to meddle with
89 // Chrome's handles. This must be done when single-threaded to avoid other
90 // threads attempting to make calls through the hooks while they are being
92 // Note: The EXE is patched separately, in chrome/app/chrome_exe_main_win.cc.
93 base::debug::HandleHooks::AddIATPatch(CURRENT_MODULE());
94 #endif // !defined(COMPONENT_BUILD) && DCHECK_IS_ON()
95 #endif // BUILDFLAG(IS_WIN)
97 ChromeMainDelegate chrome_main_delegate(
98 base::TimeTicks::FromInternalValue(exe_entry_point_ticks));
99 content::ContentMainParams params(&chrome_main_delegate);
101 #if BUILDFLAG(IS_WIN)
102 // The process should crash when going through abnormal termination, but we
103 // must be sure to reset this setting when ChromeMain returns normally.
104 auto crash_on_detach_resetter = base::ScopedClosureRunner(
105 base::BindOnce(&base::win::SetShouldCrashOnProcessDetach,
106 base::win::ShouldCrashOnProcessDetach()));
107 base::win::SetShouldCrashOnProcessDetach(true);
108 base::win::SetAbortBehaviorForCrashReporting();
109 params.instance = instance;
110 params.sandbox_info = sandbox_info;
112 // Pass chrome_elf's copy of DumpProcessWithoutCrash resolved via load-time
114 base::debug::SetDumpWithoutCrashingFunction(&DumpProcessWithoutCrash);
116 // Verify that chrome_elf and this module (chrome.dll and chrome_child.dll)
117 // have the same version.
118 if (install_static::InstallDetails::Get().VersionMismatch())
119 base::debug::DumpWithoutCrashing();
123 base::CommandLine::Init(params.argc, params.argv);
124 #endif // BUILDFLAG(IS_WIN)
125 base::CommandLine::Init(0, nullptr);
126 [[maybe_unused]] base::CommandLine* command_line(
127 base::CommandLine::ForCurrentProcess());
129 #if BUILDFLAG(IS_WIN)
130 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
131 ::switches::kRaiseTimerFrequency)) {
132 // Raise the timer interrupt frequency and leave it raised.
137 #if BUILDFLAG(IS_MAC)
138 SetUpBundleOverrides();
141 #if BUILDFLAG(IS_LINUX)
142 AppendExtraArgumentsToCommandLine(command_line);
145 // PoissonAllocationSampler's TLS slots need to be set up before
146 // MainThreadStackSamplingProfiler, which can allocate TLS slots of its own.
147 // On some platforms pthreads can malloc internally to access higher-numbered
148 // TLS slots, which can cause reentry in the heap profiler. (See the comment
149 // on ReentryGuard::InitTLSSlot().)
150 // TODO(https://crbug.com/1411454): Clean up other paths that call this Init()
151 // function, which are now redundant.
152 base::PoissonAllocationSampler::Init();
154 // Start the sampling profiler as early as possible - namely, once the command
155 // line data is available. Allocated as an object on the stack to ensure that
156 // the destructor runs on shutdown, which is important to avoid the profiler
157 // thread's destruction racing with main thread destruction.
158 MainThreadStackSamplingProfiler scoped_sampling_profiler;
160 // Chrome-specific process modes.
161 std::unique_ptr<headless::HeadlessModeHandle> headless_mode_handle;
162 if (headless::IsHeadlessMode()) {
163 if (command_line->GetArgs().size() > 1) {
164 LOG(ERROR) << "Multiple targets are not supported in headless mode.";
165 return chrome::RESULT_CODE_UNSUPPORTED_PARAM;
167 headless_mode_handle = headless::InitHeadlessMode();
169 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) || \
171 if (headless::IsOldHeadlessMode()) {
172 #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
173 command_line->AppendSwitch(::headless::switches::kEnableCrashReporter);
175 return headless::HeadlessShellMain(std::move(params));
177 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) ||
181 #if BUILDFLAG(IS_MAC)
182 // Gracefully exit if the system tried to launch the macOS notification helper
183 // app when a user clicked on a notification.
184 if (IsAlertsHelperLaunchedViaNotificationAction()) {
185 LogLaunchedViaNotificationAction(NotificationActionSource::kHelperApp);
190 int rv = content::ContentMain(std::move(params));
192 if (chrome::IsNormalResultCode(static_cast<chrome::ResultCode>(rv)))
193 return content::RESULT_CODE_NORMAL_EXIT;