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_LINUX_EXCEPTION_HANDLER_PROTOCOL_H_
16 #define CRASHPAD_UTIL_LINUX_EXCEPTION_HANDLER_PROTOCOL_H_
17 
18 #include <errno.h>
19 #include <signal.h>
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include "base/macros.h"
24 #include "build/build_config.h"
25 #include "util/file/file_io.h"
26 #include "util/misc/address_types.h"
27 
28 namespace crashpad {
29 
30 class ExceptionHandlerProtocol {
31  public:
32 #pragma pack(push, 1)
33 
34   //! \brief The type used for error reporting.
35   using Errno = int32_t;
36   static_assert(sizeof(Errno) >= sizeof(errno), "Errno type is too small");
37 
38   //! \brief A boolean status suitable for communication between processes.
39   enum Bool : char { kBoolFalse, kBoolTrue };
40 
41   //! \brief Information about a client registered with an
42   //!     ExceptionHandlerServer.
43   struct ClientInformation {
44     //! \brief Constructs this object.
45     ClientInformation();
46 
47     //! \brief The address in the client's address space of an
48     //!     ExceptionInformation struct.
49     VMAddress exception_information_address;
50 
51     //! \brief The address in the client's address space of a
52     //!     SanitizationInformation struct, or 0 if there is no such struct.
53     VMAddress sanitization_information_address;
54 
55 #if defined(OS_LINUX)
56     //! \brief Indicates that the client is likely in a crash loop if a crash
57     //!     occurs before this timestamp. This value is only used by ChromeOS's
58     //!     `/sbin/crash_reporter`.
59     uint64_t crash_loop_before_time;
60 #endif
61   };
62 
63   //! \brief The signal used to indicate a crash dump is complete.
64   //!
65   //! When multiple clients share a single socket connection with the handler,
66   //! the handler sends this signal to the dump requestor to indicate when the
67   //! the dump is either done or has failed and the client may continue.
68   static constexpr int kDumpDoneSignal = SIGCONT;
69 
70   //! \brief The message passed from client to server.
71   struct ClientToServerMessage {
72     static constexpr int32_t kVersion = 1;
73 
74     //! \brief Constructs this object.
75     ClientToServerMessage();
76 
77     //! \brief Indicates what message version is being used.
78     int32_t version;
79 
80     enum Type : uint32_t {
81       //! \brief Request that the server respond with its credentials.
82       kTypeCheckCredentials,
83 
84       //! \brief Used to request a crash dump for the sending client.
85       kTypeCrashDumpRequest
86     };
87 
88     Type type;
89 
90     //! \brief A stack address of the thread sending the message.
91     VMAddress requesting_thread_stack_address;
92 
93     union {
94       //! \brief Valid for type == kCrashDumpRequest
95       ClientInformation client_info;
96     };
97   };
98 
99   //! \brief The message passed from server to client.
100   struct ServerToClientMessage {
101     enum Type : uint32_t {
102       //! \brief Used to pass credentials with `SCM_CREDENTIALS`.
103       kTypeCredentials,
104 
105       //! \brief Indicates that the client should fork a PtraceBroker process.
106       kTypeForkBroker,
107 
108       //! \brief Inidicates that the client should set allow the handler to
109       //!     trace it using PR_SET_PTRACER.
110       kTypeSetPtracer,
111 
112       //! \brief Indicates that the handler has completed a requested crash
113       //!     dump.
114       kTypeCrashDumpComplete,
115 
116       //! \brief Indicicates that the handler was unable to produce a crash
117       //!     dump.
118       kTypeCrashDumpFailed
119     };
120 
121     Type type;
122 
123     //! \brief The handler's process ID. Valid for kTypeSetPtracer.
124     pid_t pid;
125   };
126 
127 #pragma pack(pop)
128 
129   DISALLOW_IMPLICIT_CONSTRUCTORS(ExceptionHandlerProtocol);
130 };
131 
132 }  // namespace crashpad
133 
134 #endif  // CRASHPAD_UTIL_LINUX_EXCEPTION_HANDLER_PROTOCOL_H_
135