15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/app/startup_helper_win.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <crtdbg.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <new.h>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_switches.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/windows_version.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sandbox/win/src/process_mitigations.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sandbox/win/src/sandbox_factory.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#pragma optimize("", off)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Handlers for invalid parameter and pure call. They generate a breakpoint to
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// tell breakpad that it needs to dump the process.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void InvalidParameter(const wchar_t* expression, const wchar_t* function,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const wchar_t* file, unsigned int line,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      uintptr_t reserved) {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  __debugbreak();
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _exit(1);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PureCall() {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  __debugbreak();
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _exit(1);
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#pragma optimize("", on)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void InitializeSandboxInfo(sandbox::SandboxInterfaceInfo* info) {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  info->broker_services = sandbox::SandboxFactory::GetBrokerServices();
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!info->broker_services) {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    info->target_services = sandbox::SandboxFactory::GetTargetServices();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Ensure the proper mitigations are enforced for the browser process.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    sandbox::ApplyProcessMitigationsToCurrentProcess(
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        sandbox::MITIGATION_DEP |
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        sandbox::MITIGATION_DEP_NO_ATL_THUNK);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Register the invalid param handler and pure call handler to be able to
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notify breakpad when it happens.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void RegisterInvalidParamHandler() {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _set_invalid_parameter_handler(InvalidParameter);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _set_purecall_handler(PureCall);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Also enable the new handler for malloc() based failures.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _set_new_mode(1);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void SetupCRT(const base::CommandLine& command_line) {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(_CRTDBG_MAP_ALLOC)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!command_line.HasSwitch(switches::kDisableBreakpad)) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    _CrtSetReportMode(_CRT_ASSERT, 0);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
71