|
1 // -*- mode: C++ -*- |
|
2 |
|
3 // Copyright (c) 2010, Google Inc. |
|
4 // All rights reserved. |
|
5 // |
|
6 // Redistribution and use in source and binary forms, with or without |
|
7 // modification, are permitted provided that the following conditions are |
|
8 // met: |
|
9 // |
|
10 // * Redistributions of source code must retain the above copyright |
|
11 // notice, this list of conditions and the following disclaimer. |
|
12 // * Redistributions in binary form must reproduce the above |
|
13 // copyright notice, this list of conditions and the following disclaimer |
|
14 // in the documentation and/or other materials provided with the |
|
15 // distribution. |
|
16 // * Neither the name of Google Inc. nor the names of its |
|
17 // contributors may be used to endorse or promote products derived from |
|
18 // this software without specific prior written permission. |
|
19 // |
|
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 |
|
32 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
|
33 |
|
34 // cfi_frame_info.h: Define the CFIFrameInfo class, which holds the |
|
35 // set of 'STACK CFI'-derived register recovery rules that apply at a |
|
36 // given instruction. |
|
37 |
|
38 #ifndef PROCESSOR_CFI_FRAME_INFO_H_ |
|
39 #define PROCESSOR_CFI_FRAME_INFO_H_ |
|
40 |
|
41 #include <map> |
|
42 #include <string> |
|
43 |
|
44 #include "common/using_std_string.h" |
|
45 #include "common/unique_string.h" |
|
46 #include "google_breakpad/common/breakpad_types.h" |
|
47 #include "common/module.h" |
|
48 |
|
49 namespace google_breakpad { |
|
50 |
|
51 using std::map; |
|
52 |
|
53 class MemoryRegion; |
|
54 |
|
55 // A set of rules for recovering the calling frame's registers' |
|
56 // values, when the PC is at a given address in the current frame's |
|
57 // function. See the description of 'STACK CFI' records at: |
|
58 // |
|
59 // http://code.google.com/p/google-breakpad/wiki/SymbolFiles |
|
60 // |
|
61 // To prepare an instance of CFIFrameInfo for use at a given |
|
62 // instruction, first populate it with the rules from the 'STACK CFI |
|
63 // INIT' record that covers that instruction, and then apply the |
|
64 // changes given by the 'STACK CFI' records up to our instruction's |
|
65 // address. Then, use the FindCallerRegs member function to apply the |
|
66 // rules to the callee frame's register values, yielding the caller |
|
67 // frame's register values. |
|
68 class CFIFrameInfo { |
|
69 public: |
|
70 // A map from register names onto values. |
|
71 template<typename ValueType> class RegisterValueMap: |
|
72 public UniqueStringMap<ValueType> { }; |
|
73 |
|
74 // Set the expression for computing a call frame address, return |
|
75 // address, or register's value. At least the CFA rule and the RA |
|
76 // rule must be set before calling FindCallerRegs. |
|
77 void SetCFARule(const Module::Expr& rule) { cfa_rule_ = rule; } |
|
78 void SetRARule(const Module::Expr& rule) { ra_rule_ = rule; } |
|
79 void SetRegisterRule(const UniqueString* register_name, |
|
80 const Module::Expr& rule) { |
|
81 register_rules_[register_name] = rule; |
|
82 } |
|
83 |
|
84 // Compute the values of the calling frame's registers, according to |
|
85 // this rule set. Use ValueType in expression evaluation; this |
|
86 // should be uint32_t on machines with 32-bit addresses, or |
|
87 // uint64_t on machines with 64-bit addresses. |
|
88 // |
|
89 // Return true on success, false otherwise. |
|
90 // |
|
91 // MEMORY provides access to the contents of the stack. REGISTERS is |
|
92 // a dictionary mapping the names of registers whose values are |
|
93 // known in the current frame to their values. CALLER_REGISTERS is |
|
94 // populated with the values of the recoverable registers in the |
|
95 // frame that called the current frame. |
|
96 // |
|
97 // In addition, CALLER_REGISTERS[".ra"] will be the return address, |
|
98 // and CALLER_REGISTERS[".cfa"] will be the call frame address. |
|
99 // These may be helpful in computing the caller's PC and stack |
|
100 // pointer, if their values are not explicitly specified. |
|
101 template<typename ValueType> |
|
102 bool FindCallerRegs(const RegisterValueMap<ValueType> ®isters, |
|
103 const MemoryRegion &memory, |
|
104 RegisterValueMap<ValueType> *caller_registers) const; |
|
105 |
|
106 // Serialize the rules in this object into a string in the format |
|
107 // of STACK CFI records. |
|
108 string Serialize() const; |
|
109 |
|
110 private: |
|
111 |
|
112 // A map from register names onto evaluation rules. |
|
113 typedef map<const UniqueString*, Module::Expr> RuleMap; |
|
114 |
|
115 // An expression for computing the current frame's CFA (call |
|
116 // frame address). The CFA is a reference address for the frame that |
|
117 // remains unchanged throughout the frame's lifetime. You should |
|
118 // evaluate this expression with a dictionary initially populated |
|
119 // with the values of the current frame's known registers. |
|
120 Module::Expr cfa_rule_; |
|
121 |
|
122 // The following expressions should be evaluated with a dictionary |
|
123 // initially populated with the values of the current frame's known |
|
124 // registers, and with ".cfa" set to the result of evaluating the |
|
125 // cfa_rule expression, above. |
|
126 |
|
127 // An expression for computing the current frame's return address. |
|
128 Module::Expr ra_rule_; |
|
129 |
|
130 // For a register named REG, rules[REG] is a postfix expression |
|
131 // which leaves the value of REG in the calling frame on the top of |
|
132 // the stack. You should evaluate this expression |
|
133 RuleMap register_rules_; |
|
134 }; |
|
135 |
|
136 // A parser for STACK CFI-style rule sets. |
|
137 // This may seem bureaucratic: there's no legitimate run-time reason |
|
138 // to use a parser/handler pattern for this, as it's not a likely |
|
139 // reuse boundary. But doing so makes finer-grained unit testing |
|
140 // possible. |
|
141 class CFIRuleParser { |
|
142 public: |
|
143 |
|
144 class Handler { |
|
145 public: |
|
146 Handler() { } |
|
147 virtual ~Handler() { } |
|
148 |
|
149 // The input specifies EXPRESSION as the CFA/RA computation rule. |
|
150 virtual void CFARule(const string &expression) = 0; |
|
151 virtual void RARule(const string &expression) = 0; |
|
152 |
|
153 // The input specifies EXPRESSION as the recovery rule for register NAME. |
|
154 virtual void RegisterRule(const UniqueString* name, |
|
155 const string &expression) = 0; |
|
156 }; |
|
157 |
|
158 // Construct a parser which feeds its results to HANDLER. |
|
159 CFIRuleParser(Handler *handler) : handler_(handler) { } |
|
160 |
|
161 // Parse RULE_SET as a set of CFA computation and RA/register |
|
162 // recovery rules, as appearing in STACK CFI records. Report the |
|
163 // results of parsing by making the appropriate calls to handler_. |
|
164 // Return true if parsing was successful, false otherwise. |
|
165 bool Parse(const string &rule_set); |
|
166 |
|
167 private: |
|
168 // Report any accumulated rule to handler_ |
|
169 bool Report(); |
|
170 |
|
171 // The handler to which the parser reports its findings. |
|
172 Handler *handler_; |
|
173 |
|
174 // Working data. |
|
175 const UniqueString* name_; |
|
176 string expression_; |
|
177 }; |
|
178 |
|
179 // A handler for rule set parsing that populates a CFIFrameInfo with |
|
180 // the results. |
|
181 class CFIFrameInfoParseHandler: public CFIRuleParser::Handler { |
|
182 public: |
|
183 // Populate FRAME_INFO with the results of parsing. |
|
184 CFIFrameInfoParseHandler(CFIFrameInfo *frame_info) |
|
185 : frame_info_(frame_info) { } |
|
186 |
|
187 void CFARule(const string &expression); |
|
188 void RARule(const string &expression); |
|
189 void RegisterRule(const UniqueString* name, const string &expression); |
|
190 |
|
191 private: |
|
192 CFIFrameInfo *frame_info_; |
|
193 }; |
|
194 |
|
195 // A utility class template for simple 'STACK CFI'-driven stack walkers. |
|
196 // Given a CFIFrameInfo instance, a table describing the architecture's |
|
197 // register set, and a context holding the last frame's registers, an |
|
198 // instance of this class can populate a new context with the caller's |
|
199 // registers. |
|
200 // |
|
201 // This class template doesn't use any internal knowledge of CFIFrameInfo |
|
202 // or the other stack walking structures; it just uses the public interface |
|
203 // of CFIFrameInfo to do the usual things. But the logic it handles should |
|
204 // be common to many different architectures' stack walkers, so wrapping it |
|
205 // up in a class should allow the walkers to share code. |
|
206 // |
|
207 // RegisterType should be the type of this architecture's registers, either |
|
208 // uint32_t or uint64_t. RawContextType should be the raw context |
|
209 // structure type for this architecture. |
|
210 template <typename RegisterType, class RawContextType> |
|
211 class SimpleCFIWalker { |
|
212 public: |
|
213 // A structure describing one architecture register. |
|
214 struct RegisterSet { |
|
215 // The register name, as it appears in STACK CFI rules. |
|
216 const UniqueString* name; |
|
217 |
|
218 // An alternate name that the register's value might be found |
|
219 // under in a register value dictionary, or NULL. When generating |
|
220 // names, prefer NAME to this value. It's common to list ".cfa" as |
|
221 // an alternative name for the stack pointer, and ".ra" as an |
|
222 // alternative name for the instruction pointer. |
|
223 const UniqueString* alternate_name; |
|
224 |
|
225 // True if the callee is expected to preserve the value of this |
|
226 // register. If this flag is true for some register R, and the STACK |
|
227 // CFI records provide no rule to recover R, then SimpleCFIWalker |
|
228 // assumes that the callee has not changed R's value, and the caller's |
|
229 // value for R is that currently in the callee's context. |
|
230 bool callee_saves; |
|
231 |
|
232 // The ContextValidity flag representing the register's presence. |
|
233 int validity_flag; |
|
234 |
|
235 // A pointer to the RawContextType member that holds the |
|
236 // register's value. |
|
237 RegisterType RawContextType::*context_member; |
|
238 }; |
|
239 |
|
240 // Create a simple CFI-based frame walker, given a description of the |
|
241 // architecture's register set. REGISTER_MAP is an array of |
|
242 // RegisterSet structures; MAP_SIZE is the number of elements in the |
|
243 // array. |
|
244 SimpleCFIWalker(const RegisterSet *register_map, size_t map_size) |
|
245 : register_map_(register_map), map_size_(map_size) { } |
|
246 |
|
247 // Compute the calling frame's raw context given the callee's raw |
|
248 // context. |
|
249 // |
|
250 // Given: |
|
251 // |
|
252 // - MEMORY, holding the stack's contents, |
|
253 // - CFI_FRAME_INFO, describing the called function, |
|
254 // - CALLEE_CONTEXT, holding the called frame's registers, and |
|
255 // - CALLEE_VALIDITY, indicating which registers in CALLEE_CONTEXT are valid, |
|
256 // |
|
257 // fill in CALLER_CONTEXT with the caller's register values, and set |
|
258 // CALLER_VALIDITY to indicate which registers are valid in |
|
259 // CALLER_CONTEXT. Return true on success, or false on failure. |
|
260 bool FindCallerRegisters(const MemoryRegion &memory, |
|
261 const CFIFrameInfo &cfi_frame_info, |
|
262 const RawContextType &callee_context, |
|
263 int callee_validity, |
|
264 RawContextType *caller_context, |
|
265 int *caller_validity) const; |
|
266 |
|
267 private: |
|
268 const RegisterSet *register_map_; |
|
269 size_t map_size_; |
|
270 }; |
|
271 |
|
272 } // namespace google_breakpad |
|
273 |
|
274 #include "cfi_frame_info-inl.h" |
|
275 |
|
276 #endif // PROCESSOR_CFI_FRAME_INFO_H_ |