Skip to content

Commit

Permalink
Crash fast in case of an exception from ATL.
Browse files Browse the repository at this point in the history
BUG=108176
TBR=fdoray@chromium.org, nicolaso@chromium.org

Change-Id: I076fa8233729c8006c1963492ccf3a3bce36a78b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2212046
Reviewed-by: Greg Thompson <grt@chromium.org>
Commit-Queue: Greg Thompson <grt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#771244}
  • Loading branch information
GregTho authored and Commit Bot committed May 21, 2020
1 parent 871bbbf commit 2e1efe3
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 9 deletions.
2 changes: 2 additions & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,8 @@ jumbo_component("base") {
"trace_event/trace_event_etw_export_win.h",
"win/async_operation.h",
"win/atl.h",
"win/atl_throw.cc",
"win/atl_throw.h",
"win/com_init_check_hook.cc",
"win/com_init_check_hook.h",
"win/com_init_util.cc",
Expand Down
8 changes: 4 additions & 4 deletions base/win/atl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_WIN_ATL_H_
#define BASE_WIN_ATL_H_

Expand All @@ -9,10 +10,6 @@
// Undefine before windows header will make the poisonous defines
#include "base/win/windows_undefines.inc"

#ifndef _ATL_NO_EXCEPTIONS
#define _ATL_NO_EXCEPTIONS
#endif

// atlwin.h relies on std::void_t, but libc++ doesn't define it unless
// _LIBCPP_STD_VER > 14. Workaround this by manually defining it.
#include <type_traits>
Expand All @@ -23,6 +20,9 @@ using void_t = void;
}
#endif

// Declare our own exception thrower (atl_throw.h includes atldef.h).
#include "base/win/atl_throw.h"

#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
Expand Down
25 changes: 25 additions & 0 deletions base/win/atl_throw.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/win/atl_throw.h"

#include <winerror.h>

#include "base/compiler_specific.h"
#include "base/debug/alias.h"
#include "base/immediate_crash.h"
#include "base/process/memory.h"

namespace base {
namespace win {

NOINLINE void __stdcall AtlThrowImpl(HRESULT hr) {
base::debug::Alias(&hr);
if (hr == E_OUTOFMEMORY)
base::TerminateBecauseOutOfMemory(0);
IMMEDIATE_CRASH();
}

} // namespace win
} // namespace base
43 changes: 43 additions & 0 deletions base/win/atl_throw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_WIN_ATL_THROW_H_
#define BASE_WIN_ATL_THROW_H_

#ifdef __ATLDEF_H__
#error atl_throw.h must be included before atldef.h.
#endif

#include "base/base_export.h"
#include "base/win/windows_types.h"

// Defining _ATL_NO_EXCEPTIONS causes ATL to raise a structured exception
// instead of throwing a CAtlException. While crashpad will eventually handle
// this, the HRESULT that caused the problem is lost. So, in addition, define
// our own custom AtlThrow function (_ATL_CUSTOM_THROW).
#ifndef _ATL_NO_EXCEPTIONS
#define _ATL_NO_EXCEPTIONS
#endif

#define _ATL_CUSTOM_THROW
#define AtlThrow ::base::win::AtlThrowImpl

namespace base {
namespace win {

// Crash the process forthwith in case of ATL errors.
[[noreturn]] BASE_EXPORT void __stdcall AtlThrowImpl(HRESULT hr);

} // namespace win
} // namespace base

#include <atldef.h>

// atldef.h mistakenly leaves out the declaration of this function when
// _ATL_CUSTOM_THROW is defined.
namespace ATL {
ATL_NOINLINE __declspec(noreturn) inline void WINAPI AtlThrowLastWin32();
}

#endif // BASE_WIN_ATL_THROW_H_
2 changes: 1 addition & 1 deletion chrome/browser/browser_switcher/bho/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ shared_library("browser_switcher_bho") {
]
} else {
sources = [
"//base/win/atl.h",
"atl.h",
"bho.cc",
"bho.h",
"browser_switcher_core.cc",
Expand Down
29 changes: 29 additions & 0 deletions chrome/browser/browser_switcher/bho/atl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_BROWSER_SWITCHER_BHO_ATL_H_
#define CHROME_BROWSER_BROWSER_SWITCHER_BHO_ATL_H_

#ifndef _ATL_NO_EXCEPTIONS
#define _ATL_NO_EXCEPTIONS
#endif

// atlwin.h relies on std::void_t, but libc++ doesn't define it unless
// _LIBCPP_STD_VER > 14. Workaround this by manually defining it.
#include <type_traits>
#if defined(_LIBCPP_STD_VER) && _LIBCPP_STD_VER <= 14
namespace std {
template <class...>
using void_t = void;
}
#endif

#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
#include <atlhost.h>
#include <atlsecurity.h>
#include <atlwin.h>

#endif // CHROME_BROWSER_BROWSER_SWITCHER_BHO_ATL_H_
2 changes: 1 addition & 1 deletion chrome/browser/browser_switcher/bho/bho.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef CHROME_BROWSER_BROWSER_SWITCHER_BHO_BHO_H_
#define CHROME_BROWSER_BROWSER_SWITCHER_BHO_BHO_H_

#include "base/win/atl.h"
#include "chrome/browser/browser_switcher/bho/atl.h"

#include <exdispid.h> // NOLINT(build/include_order)
#include <shlguid.h> // NOLINT(build/include_order)
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/browser_switcher/bho/ie_bho.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

#include <ShlObj.h>

#include "base/win/atl.h"

#include "chrome/browser/browser_switcher/bho/atl.h"
#include "chrome/browser/browser_switcher/bho/ie_bho_idl.h"
#include "chrome/browser/browser_switcher/bho/logging.h"
#include "chrome/browser/browser_switcher/bho/resource.h"
Expand Down
3 changes: 2 additions & 1 deletion chrome/installer/setup/install_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

#include "chrome/installer/setup/install_worker.h"

#include <atlsecurity.h>
#include "base/win/atl.h"

#include <oaidl.h>
#include <sddl.h>
#include <shlobj.h>
Expand Down

0 comments on commit 2e1efe3

Please sign in to comment.