#include "chrome/app/chrome_exe_main_win.h"
#include <tchar.h>
#include <windows.h>
#include <malloc.h>
#include <stddef.h>
#include <algorithm>
#include <array>
#include <string>
#include <vector>
#include "base/at_exit.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/debug/alias.h"
#include "base/debug/handle_hooks_win.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/process/memory.h"
#include "base/process/process.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/win/current_module.h"
#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "build/build_config.h"
#include "chrome/app/delay_load_failure_hook_win.h"
#include "chrome/app/exit_code_watcher_win.h"
#include "chrome/app/main_dll_loader_win.h"
#include "chrome/app/packed_resources_integrity.h"
#include "chrome/browser/policy/policy_path_parser.h"
#include "chrome/browser/win/chrome_process_finder.h"
#include "chrome/chrome_elf/chrome_elf_main.h"
#include "chrome/common/chrome_paths_internal.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/install_static/initialize_from_primary_module.h"
#include "chrome/install_static/install_util.h"
#include "chrome/install_static/user_data_dir.h"
#include "components/crash/core/app/crash_switches.h"
#include "components/crash/core/app/crashpad.h"
#include "components/crash/core/app/fallback_crash_handling_win.h"
#include "components/crash/core/app/run_as_crashpad_handler_win.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/result_codes.h"
#include "third_party/crashpad/crashpad/util/win/initial_client_data.h"
#if defined(WIN_CONSOLE_APP)
int main();
#endif
namespace {
void SetCwdForBrowserProcess() {
if (!::IsBrowserProcess())
return;
std::array<wchar_t, MAX_PATH + 1> buffer;
buffer[0] = L'\0';
DWORD length = ::GetModuleFileName(nullptr, &buffer[0], buffer.size());
if (!length || length >= buffer.size())
return;
base::SetCurrentDirectory(
base::FilePath(base::FilePath::StringViewType(&buffer[0], length))
.DirName());
}
bool IsFastStartSwitch(const std::string& command_line_switch) {
return command_line_switch == switches::kProfileDirectory;
}
bool ContainsNonFastStartFlag(const base::CommandLine& command_line) {
const base::CommandLine::SwitchMap& switches = command_line.GetSwitches();
if (switches.size() > 1)
return true;
for (base::CommandLine::SwitchMap::const_iterator it = switches.begin();
it != switches.end(); ++it) {
if (!IsFastStartSwitch(it->first))
return true;
}
return false;
}
bool AttemptFastNotify(const base::CommandLine& command_line) {
if (ContainsNonFastStartFlag(command_line))
return false;
base::FilePath user_data_dir;
if (!chrome::GetDefaultUserDataDirectory(&user_data_dir))
return false;
policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
HWND chrome = FindRunningChromeWindow(user_data_dir);
if (!chrome)
return false;
return AttemptToNotifyRunningChrome(chrome) == NotifyChromeResult::kSuccess;
}
bool HasValidWindowsPrefetchArgument(const base::CommandLine& command_line) {
static constexpr std::wstring_view kPrefetchArgumentPrefix(L"/prefetch:");
for (const auto& arg : command_line.argv()) {
if (!base::StartsWith(arg, kPrefetchArgumentPrefix)) {
continue;
}
auto value = std::wstring_view(arg).substr(kPrefetchArgumentPrefix.size());
int profile = 0;
return base::StringToInt(value, &profile) && profile >= 1 &&
profile <=
(base::win::GetVersion() < base::win::Version::WIN11 ? 8 : 16);
}
return false;
}
int RunFallbackCrashHandler(const base::CommandLine& cmd_line) {
wchar_t exe_file[MAX_PATH] = {};
CHECK(::GetModuleFileName(nullptr, exe_file, std::size(exe_file)));
std::wstring product_name, version, channel_name, special_build;
install_static::GetExecutableVersionDetails(exe_file, &product_name, &version,
&special_build, &channel_name);
return crash_reporter::RunAsFallbackCrashHandler(
cmd_line, base::WideToUTF8(product_name), base::WideToUTF8(version),
base::WideToUTF8(channel_name));
}
#if defined(ARCH_CPU_32_BITS)
struct FiberState {
HINSTANCE instance;
LPVOID original_fiber;
int fiber_result;
};
void WINAPI FiberBinder(void* params) {
auto* fiber_state = static_cast<FiberState*>(params);
#if !defined(WIN_CONSOLE_APP)
fiber_state->fiber_result =
wWinMain(fiber_state->instance, nullptr, nullptr, 0);
#else
fiber_state->fiber_result = main();
#endif
::SwitchToFiber(fiber_state->original_fiber);
}
#endif
}
__declspec(dllexport) __cdecl void GetPakFileHashes(
const uint8_t** resources_pak,
const uint8_t** chrome_100_pak,
const uint8_t** chrome_200_pak) {
*resources_pak = kSha256_resources_pak.data();
*chrome_100_pak = kSha256_chrome_100_percent_pak.data();
*chrome_200_pak = kSha256_chrome_200_percent_pak.data();
}
#if !defined(WIN_CONSOLE_APP)
int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t*, int) {
#else
int main() {
HINSTANCE instance = GetModuleHandle(nullptr);
#endif
#if defined(ARCH_CPU_32_BITS)
enum class FiberStatus { kConvertFailed, kCreateFiberFailed, kSuccess };
FiberStatus fiber_status = FiberStatus::kSuccess;
DWORD fiber_error = ERROR_SUCCESS;
if (!::IsThreadAFiber()) {
constexpr size_t kStackSize = 4 * 1024 * 1024;
LPVOID original_fiber =
::ConvertThreadToFiberEx(nullptr, FIBER_FLAG_FLOAT_SWITCH);
if (original_fiber) {
FiberState fiber_state = {instance, original_fiber};
LPVOID big_stack_fiber = ::CreateFiberEx(
0, kStackSize, FIBER_FLAG_FLOAT_SWITCH, FiberBinder, &fiber_state);
if (big_stack_fiber) {
::SwitchToFiber(big_stack_fiber);
::DeleteFiber(big_stack_fiber);
::ConvertFiberToThread();
return fiber_state.fiber_result;
}
fiber_status = FiberStatus::kCreateFiberFailed;
} else {
fiber_status = FiberStatus::kConvertFailed;
}
fiber_error = ::GetLastError();
base::debug::Alias(&fiber_error);
}
#endif
SetCwdForBrowserProcess();
install_static::InitializeFromPrimaryModule();
SignalInitializeCrashReporting();
if (IsBrowserProcess())
DisableDelayLoadFailureHooksForMainExecutable();
#if defined(ARCH_CPU_32_BITS)
CHECK_EQ(fiber_status, FiberStatus::kSuccess);
#endif
base::EnableTerminationOnOutOfMemory();
logging::RegisterAbslAbortHook();
base::CommandLine::Init(0, nullptr);
const base::CommandLine* command_line =
base::CommandLine::ForCurrentProcess();
const std::string process_type =
command_line->GetSwitchValueASCII(switches::kProcessType);
#if !defined(COMPONENT_BUILD) && DCHECK_IS_ON()
base::FeatureList::FailOnFeatureAccessWithoutFeatureList();
base::debug::HandleHooks::AddIATPatch(CURRENT_MODULE());
#endif
DCHECK(process_type.empty() ||
HasValidWindowsPrefetchArgument(*command_line));
if (process_type == crash_reporter::switches::kCrashpadHandler) {
std::unique_ptr<ExitCodeWatcher> exit_code_watcher;
crash_reporter::SetupFallbackCrashHandling(*command_line);
if (!command_line->HasSwitch("no-periodic-tasks")) {
crashpad::InitialClientData initial_client_data;
if (initial_client_data.InitializeFromString(
command_line->GetSwitchValueASCII("initial-client-data"))) {
HANDLE duplicate_handle = INVALID_HANDLE_VALUE;
if (DuplicateHandle(
::GetCurrentProcess(), initial_client_data.client_process(),
::GetCurrentProcess(), &duplicate_handle,
PROCESS_QUERY_INFORMATION, FALSE, DUPLICATE_SAME_ACCESS)) {
base::Process parent_process(duplicate_handle);
exit_code_watcher = std::make_unique<ExitCodeWatcher>();
if (exit_code_watcher->Initialize(std::move(parent_process))) {
exit_code_watcher->StartWatching();
}
}
}
}
DCHECK(command_line->HasSwitch(switches::kUserDataDir));
base::FilePath user_data_dir =
command_line->GetSwitchValuePath(switches::kUserDataDir);
int crashpad_status = crash_reporter::RunAsCrashpadHandler(
*base::CommandLine::ForCurrentProcess(), user_data_dir,
switches::kProcessType, switches::kUserDataDir);
if (crashpad_status != 0 && exit_code_watcher) {
exit_code_watcher->StopWatching();
}
return crashpad_status;
} else if (process_type == crash_reporter::switches::kFallbackCrashHandler) {
return RunFallbackCrashHandler(*command_line);
}
const base::TimeTicks exe_entry_point_ticks = base::TimeTicks::Now();
SignalChromeElf();
base::AtExitManager exit_manager;
if (AttemptFastNotify(*command_line))
return 0;
VLOG(1) << "About to load main DLL.";
MainDllLoader* loader = MakeMainDllLoader();
int rc = loader->Launch(instance, exe_entry_point_ticks);
loader->RelaunchChromeBrowserWithNewCommandLineIfNeeded();
delete loader;
if (process_type == switches::kUtilityProcess) {
base::Process::TerminateCurrentProcessImmediately(rc);
}
return rc;
}