1 // Copyright 2017 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CRASHPAD_UTIL_WIN_SAFE_TERMINATE_PROCESS_H_
16 #define CRASHPAD_UTIL_WIN_SAFE_TERMINATE_PROCESS_H_
17 
18 #include <windows.h>
19 
20 #include "build/build_config.h"
21 
22 namespace crashpad {
23 
24 //! \brief Calls `TerminateProcess()`.
25 //!
26 //! `TerminateProcess()` has been observed in the wild as being patched badly on
27 //! 32-bit x86: it’s patched with code adhering to the `cdecl` (caller clean-up)
28 //! convention, although it’s supposed to be `stdcall` (callee clean-up). The
29 //! mix-up means that neither caller nor callee perform parameter clean-up from
30 //! the stack, causing the stack pointer to have an unexpected value on return
31 //! from the patched function. This typically results in a crash shortly
32 //! thereafter. See <a href="https://crashpad.chromium.org/bug/179">Crashpad bug
33 //! 179</a>.
34 //!
35 //! On 32-bit x86, this replacement function calls `TerminateProcess()` without
36 //! making any assumptions about the stack pointer on its return. As such, it’s
37 //! compatible with the badly patched `cdecl` version as well as the native
38 //! `stdcall` version (and other less badly patched versions).
39 //!
40 //! Elsewhere, this function calls `TerminateProcess()` directly without any
41 //! additional fanfare.
42 //!
43 //! Call this function instead of `TerminateProcess()` anywhere that
44 //! `TerminateProcess()` would normally be called.
45 bool SafeTerminateProcess(HANDLE process, UINT exit_code);
46 
47 #if !defined(ARCH_CPU_X86)
SafeTerminateProcess(HANDLE process,UINT exit_code)48 inline bool SafeTerminateProcess(HANDLE process, UINT exit_code) {
49   return TerminateProcess(process, exit_code) != FALSE;
50 }
51 #endif  // !ARCH_CPU_X86
52 
53 }  // namespace crashpad
54 
55 #endif  // CRASHPAD_UTIL_WIN_SAFE_TERMINATE_PROCESS_H_
56