1 // -*- mode: C++ -*-
2
3 // Copyright 2010 Google LLC
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google LLC nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Original author: Jim Blandy <[email protected]> <[email protected]>
32
33 // cfi_frame_info-inl.h: Definitions for cfi_frame_info.h inlined functions.
34
35 #ifndef PROCESSOR_CFI_FRAME_INFO_INL_H_
36 #define PROCESSOR_CFI_FRAME_INFO_INL_H_
37
38 #include <string.h>
39
40 namespace google_breakpad {
41
42 template <typename RegisterType, class RawContextType>
FindCallerRegisters(const MemoryRegion & memory,const CFIFrameInfo & cfi_frame_info,const RawContextType & callee_context,int callee_validity,RawContextType * caller_context,int * caller_validity)43 bool SimpleCFIWalker<RegisterType, RawContextType>::FindCallerRegisters(
44 const MemoryRegion& memory,
45 const CFIFrameInfo& cfi_frame_info,
46 const RawContextType& callee_context,
47 int callee_validity,
48 RawContextType* caller_context,
49 int* caller_validity) const {
50 typedef CFIFrameInfo::RegisterValueMap<RegisterType> ValueMap;
51 ValueMap callee_registers;
52 ValueMap caller_registers;
53 // Just for brevity.
54 typename ValueMap::const_iterator caller_none = caller_registers.end();
55
56 // Populate callee_registers with register values from callee_context.
57 for (size_t i = 0; i < map_size_; i++) {
58 const RegisterSet& r = register_map_[i];
59 if (callee_validity & r.validity_flag)
60 callee_registers[r.name] = callee_context.*r.context_member;
61 }
62
63 // Apply the rules, and see what register values they yield.
64 if (!cfi_frame_info.FindCallerRegs<RegisterType>(callee_registers, memory,
65 &caller_registers))
66 return false;
67
68 // Populate *caller_context with the values the rules placed in
69 // caller_registers.
70 memset(caller_context, 0xda, sizeof(*caller_context));
71 *caller_validity = 0;
72 for (size_t i = 0; i < map_size_; i++) {
73 const RegisterSet& r = register_map_[i];
74 typename ValueMap::const_iterator caller_entry;
75
76 // Did the rules provide a value for this register by its name?
77 caller_entry = caller_registers.find(r.name);
78 if (caller_entry != caller_none) {
79 caller_context->*r.context_member = caller_entry->second;
80 *caller_validity |= r.validity_flag;
81 continue;
82 }
83
84 // Did the rules provide a value for this register under its
85 // alternate name?
86 if (r.alternate_name) {
87 caller_entry = caller_registers.find(r.alternate_name);
88 if (caller_entry != caller_none) {
89 caller_context->*r.context_member = caller_entry->second;
90 *caller_validity |= r.validity_flag;
91 continue;
92 }
93 }
94
95 // Is this a callee-saves register? The walker assumes that these
96 // still hold the caller's value if the CFI doesn't mention them.
97 //
98 // Note that other frame walkers may fail to recover callee-saves
99 // registers; for example, the x86 "traditional" strategy only
100 // recovers %eip, %esp, and %ebp, even though %ebx, %esi, and %edi
101 // are callee-saves, too. It is not correct to blindly set the
102 // valid bit for all callee-saves registers, without first
103 // checking its validity bit in the callee.
104 if (r.callee_saves && (callee_validity & r.validity_flag) != 0) {
105 caller_context->*r.context_member = callee_context.*r.context_member;
106 *caller_validity |= r.validity_flag;
107 continue;
108 }
109
110 // Otherwise, the register's value is unknown.
111 }
112
113 return true;
114 }
115
116 } // namespace google_breakpad
117
118 #endif // PROCESSOR_CFI_FRAME_INFO_INL_H_
119