OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #ifndef CRASHPAD_SNAPSHOT_WIN_PE_IMAGE_READER_H_ | 15 #ifndef CRASHPAD_SNAPSHOT_WIN_PE_IMAGE_READER_H_ |
16 #define CRASHPAD_SNAPSHOT_WIN_PE_IMAGE_READER_H_ | 16 #define CRASHPAD_SNAPSHOT_WIN_PE_IMAGE_READER_H_ |
17 | 17 |
18 #include <windows.h> | 18 #include <windows.h> |
19 | 19 |
20 #include <string> | 20 #include <string> |
21 | 21 |
22 #include "base/basictypes.h" | 22 #include "base/basictypes.h" |
| 23 #include "snapshot/win/process_subrange_reader.h" |
23 #include "util/misc/initialization_state_dcheck.h" | 24 #include "util/misc/initialization_state_dcheck.h" |
24 #include "util/misc/uuid.h" | 25 #include "util/misc/uuid.h" |
25 #include "util/win/address_types.h" | 26 #include "util/win/address_types.h" |
26 #include "util/win/checked_win_address_range.h" | |
27 #include "util/win/process_structs.h" | 27 #include "util/win/process_structs.h" |
28 | 28 |
29 namespace crashpad { | 29 namespace crashpad { |
30 | 30 |
31 class ProcessReaderWin; | 31 class ProcessReaderWin; |
32 | 32 |
33 namespace process_types { | 33 namespace process_types { |
34 | 34 |
35 template <class Traits> | 35 template <class Traits> |
36 struct CrashpadInfo { | 36 struct CrashpadInfo { |
37 uint32_t signature; | 37 uint32_t signature; |
38 uint32_t size; | 38 uint32_t size; |
39 uint32_t version; | 39 uint32_t version; |
40 uint8_t crashpad_handler_behavior; // TriState. | 40 uint8_t crashpad_handler_behavior; // TriState. |
41 uint8_t system_crash_reporter_forwarding; // TriState. | 41 uint8_t system_crash_reporter_forwarding; // TriState. |
42 uint16_t padding_0; | 42 uint16_t padding_0; |
43 typename Traits::Pointer simple_annotations; | 43 typename Traits::Pointer simple_annotations; |
44 }; | 44 }; |
45 | 45 |
46 } // namespace process_types | 46 } // namespace process_types |
47 | 47 |
48 //! \brief A reader for PE images mapped into another process. | 48 //! \brief A reader for PE images mapped into another process. |
49 //! | 49 //! |
50 //! This class is capable of reading both 32-bit and 64-bit images based on the | 50 //! This class is capable of reading both 32-bit and 64-bit images based on the |
51 //! bitness of the remote process. | 51 //! bitness of the remote process. |
52 //! | 52 //! |
53 //! \sa PEImageAnnotationsReader | 53 //! \sa PEImageAnnotationsReader |
| 54 //! \sa PEImageResourceReader |
54 class PEImageReader { | 55 class PEImageReader { |
55 public: | 56 public: |
56 PEImageReader(); | 57 PEImageReader(); |
57 ~PEImageReader(); | 58 ~PEImageReader(); |
58 | 59 |
59 //! \brief Initializes the reader. | 60 //! \brief Initializes the reader. |
60 //! | 61 //! |
61 //! This method must be called only once on an object. This method must be | 62 //! This method must be called only once on an object. This method must be |
62 //! called successfully before any other method in this class may be called. | 63 //! called successfully before any other method in this class may be called. |
63 //! | 64 //! |
64 //! \param[in] process_reader The reader for the remote process. | 65 //! \param[in] process_reader The reader for the remote process. |
65 //! \param[in] address The address, in the remote process' address space, | 66 //! \param[in] address The address, in the remote process' address space, |
66 //! where the `IMAGE_DOS_HEADER` is located. | 67 //! where the `IMAGE_DOS_HEADER` is located. |
67 //! \param[in] size The size of the image. | 68 //! \param[in] size The size of the image. |
68 //! \param[in] name The module's name, a string to be used in logged messages. | 69 //! \param[in] module_name The module's name, a string to be used in logged |
69 //! This string is for diagnostic purposes. | 70 //! messages. This string is for diagnostic purposes. |
70 //! | 71 //! |
71 //! \return `true` if the image was read successfully, `false` otherwise, with | 72 //! \return `true` if the image was read successfully, `false` otherwise, with |
72 //! an appropriate message logged. | 73 //! an appropriate message logged. |
73 bool Initialize(ProcessReaderWin* process_reader, | 74 bool Initialize(ProcessReaderWin* process_reader, |
74 WinVMAddress address, | 75 WinVMAddress address, |
75 WinVMSize size, | 76 WinVMSize size, |
76 const std::string& module_name); | 77 const std::string& module_name); |
77 | 78 |
78 //! \brief Returns the image's load address. | 79 //! \brief Returns the image's load address. |
79 //! | 80 //! |
80 //! This is the value passed as \a address to Initialize(). | 81 //! This is the value passed as \a address to Initialize(). |
81 WinVMAddress Address() const { return module_range_.Base(); } | 82 WinVMAddress Address() const { return module_subrange_reader_.Base(); } |
82 | 83 |
83 //! \brief Returns the image's size. | 84 //! \brief Returns the image's size. |
84 //! | 85 //! |
85 //! This is the value passed as \a size to Initialize(). | 86 //! This is the value passed as \a size to Initialize(). |
86 WinVMSize Size() const { return module_range_.Size(); } | 87 WinVMSize Size() const { return module_subrange_reader_.Size(); } |
87 | 88 |
88 //! \brief Obtains the module's CrashpadInfo structure. | 89 //! \brief Obtains the module's CrashpadInfo structure. |
89 //! | 90 //! |
90 //! \return `true` on success, `false` on failure. If the module does not have | 91 //! \return `true` on success, `false` on failure. If the module does not have |
91 //! a `CPADinfo` section, this will return `false` without logging any | 92 //! a `CPADinfo` section, this will return `false` without logging any |
92 //! messages. Other failures will result in messages being logged. | 93 //! messages. Other failures will result in messages being logged. |
93 template <class Traits> | 94 template <class Traits> |
94 bool GetCrashpadInfo( | 95 bool GetCrashpadInfo( |
95 process_types::CrashpadInfo<Traits>* crashpad_info) const; | 96 process_types::CrashpadInfo<Traits>* crashpad_info) const; |
96 | 97 |
97 //! \brief Obtains information from the module's debug directory, if any. | 98 //! \brief Obtains information from the module's debug directory, if any. |
98 //! | 99 //! |
99 //! \param[out] uuid The unique identifier of the executable/PDB. | 100 //! \param[out] uuid The unique identifier of the executable/PDB. |
100 //! \param[out] age The age field for the pdb (the number of times it's been | 101 //! \param[out] age The age field for the pdb (the number of times it's been |
101 //! relinked). | 102 //! relinked). |
102 //! \param[out] pdbname Name of the pdb file. | 103 //! \param[out] pdbname Name of the pdb file. |
103 //! \return `true` on success, or `false` if the module has no debug directory | |
104 //! entry. | |
105 bool DebugDirectoryInformation(UUID* uuid, | |
106 DWORD* age, | |
107 std::string* pdbname) const; | |
108 | |
109 private: | |
110 //! \brief Implementation helper for DebugDirectoryInformation() templated by | |
111 //! `IMAGE_NT_HEADERS` type for different bitnesses. | |
112 //! | 104 //! |
113 //! \return `true` on success, with the parameters set appropriately. `false` | 105 //! \return `true` on success, with the parameters set appropriately. `false` |
114 //! on failure. This method may return `false` without logging anything in | 106 //! on failure. This method may return `false` without logging anything in |
115 //! the case of a module that does not contain relevant debugging | 107 //! the case of a module that does not contain relevant debugging |
116 //! information but is otherwise properly structured. | 108 //! information but is otherwise properly structured. |
117 template <class NtHeadersType> | 109 bool DebugDirectoryInformation(UUID* uuid, |
118 bool ReadDebugDirectoryInformation(UUID* uuid, | 110 DWORD* age, |
119 DWORD* age, | 111 std::string* pdbname) const; |
120 std::string* pdbname) const; | |
121 | 112 |
| 113 //! \brief Obtains the module’s `VS_FIXEDFILEINFO`, containing its version and |
| 114 //! type information. |
| 115 //! |
| 116 //! The data obtained from this method should be equivalent to what could be |
| 117 //! obtained by calling GetModuleVersionAndType(). Avoiding that function |
| 118 //! ensures that the data in the module loaded into the remote process will be |
| 119 //! used as-is, without the risks associated with loading the module into the |
| 120 //! reading process. |
| 121 //! |
| 122 //! \param[out] vs_fixed_file_info The VS_FIXEDFILEINFO on success. |
| 123 //! VS_FIXEDFILEINFO::dwFileFlags will have been masked with |
| 124 //! VS_FIXEDFILEINFO::dwFileFlagsMask already. |
| 125 //! |
| 126 //! \return `true` on success. `false` if the module does not contain this |
| 127 //! information, without logging any messages. `false` on failure, with |
| 128 //! a message logged. |
| 129 bool VSFixedFileInfo(VS_FIXEDFILEINFO* vs_fixed_file_info) const; |
| 130 |
| 131 private: |
122 //! \brief Reads the `IMAGE_NT_HEADERS` from the beginning of the image. | 132 //! \brief Reads the `IMAGE_NT_HEADERS` from the beginning of the image. |
123 //! | 133 //! |
124 //! \param[out] nt_headers The contents of the templated NtHeadersType | 134 //! \param[out] nt_headers The contents of the templated NtHeadersType |
125 //! structure read from the remote process. | 135 //! structure read from the remote process. |
126 //! \param[out] nt_headers_address The address of the templated NtHeadersType | 136 //! \param[out] nt_headers_address The address of the templated NtHeadersType |
127 //! structure in the remote process’ address space. If this information is | 137 //! structure in the remote process’ address space. If this information is |
128 //! not needed, this parameter may be `nullptr`. | 138 //! not needed, this parameter may be `nullptr`. |
129 //! | 139 //! |
130 //! \return `true` on success, with \a nt_headers and optionally \a | 140 //! \return `true` on success, with \a nt_headers and optionally \a |
131 //! nt_headers_address set appropriately. `false` on failure, with a | 141 //! nt_headers_address set appropriately. `false` on failure, with a |
132 //! message logged. | 142 //! message logged. |
133 template <class NtHeadersType> | 143 template <class NtHeadersType> |
134 bool ReadNtHeaders(NtHeadersType* nt_headers, | 144 bool ReadNtHeaders(NtHeadersType* nt_headers, |
135 WinVMAddress* nt_headers_address) const; | 145 WinVMAddress* nt_headers_address) const; |
136 | 146 |
137 //! \brief Finds a given section by name in the image. | 147 //! \brief Finds a given section by name in the image. |
138 template <class NtHeadersType> | 148 template <class NtHeadersType> |
139 bool GetSectionByName(const std::string& name, | 149 bool GetSectionByName(const std::string& name, |
140 IMAGE_SECTION_HEADER* section) const; | 150 IMAGE_SECTION_HEADER* section) const; |
141 | 151 |
142 //! \brief Reads memory from target process, first checking whether the range | 152 //! \brief Finds the `IMAGE_DATA_DIRECTORY` in |
143 //! requested falls inside module_range_. | 153 //! `IMAGE_OPTIONAL_HEADER::DataDirectory` at the specified \a index. |
144 //! | 154 //! |
145 //! \return `true` on success, with \a into filled out, otherwise `false` and | 155 //! \param[in] index An `IMAGE_DIRECTORY_ENTRY_*` constant specifying the |
146 //! a message will be logged. | 156 //! data to be returned. |
147 bool CheckedReadMemory(WinVMAddress address, | 157 //! \param[out] entry The `IMAGE_DATA_DIRECTORY` found within the module. |
148 WinVMSize size, | 158 //! |
149 void* into) const; | 159 //! \return `true` on success, with \a entry set appropriately. `false` if the |
| 160 //! module does not contain the specified information, without logging a |
| 161 //! message. `false` on failure, with a message logged. |
| 162 bool ImageDataDirectoryEntry(size_t index, IMAGE_DATA_DIRECTORY* entry) const; |
150 | 163 |
151 ProcessReaderWin* process_reader_; // weak | 164 //! \brief A templatized helper for ImageDataDirectoryEntry() to account for |
152 CheckedWinAddressRange module_range_; | 165 //! differences in \a NtHeadersType. |
153 std::string module_name_; | 166 template <class NtHeadersType> |
| 167 bool ImageDataDirectoryEntryT(size_t index, |
| 168 IMAGE_DATA_DIRECTORY* entry) const; |
| 169 |
| 170 ProcessSubrangeReader module_subrange_reader_; |
154 InitializationStateDcheck initialized_; | 171 InitializationStateDcheck initialized_; |
155 | 172 |
156 DISALLOW_COPY_AND_ASSIGN(PEImageReader); | 173 DISALLOW_COPY_AND_ASSIGN(PEImageReader); |
157 }; | 174 }; |
158 | 175 |
159 } // namespace crashpad | 176 } // namespace crashpad |
160 | 177 |
161 #endif // CRASHPAD_SNAPSHOT_WIN_PE_IMAGE_READER_H_ | 178 #endif // CRASHPAD_SNAPSHOT_WIN_PE_IMAGE_READER_H_ |
OLD | NEW |