Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2010, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
michael@0 | 31 | |
michael@0 | 32 | // cfi_frame_info.cc: Implementation of CFIFrameInfo class. |
michael@0 | 33 | // See cfi_frame_info.h for details. |
michael@0 | 34 | |
michael@0 | 35 | #include "processor/cfi_frame_info.h" |
michael@0 | 36 | |
michael@0 | 37 | #include <string.h> |
michael@0 | 38 | |
michael@0 | 39 | #include <algorithm> |
michael@0 | 40 | #include <sstream> |
michael@0 | 41 | |
michael@0 | 42 | #include "common/scoped_ptr.h" |
michael@0 | 43 | #include "processor/postfix_evaluator-inl.h" |
michael@0 | 44 | |
michael@0 | 45 | namespace google_breakpad { |
michael@0 | 46 | |
michael@0 | 47 | #ifdef _WIN32 |
michael@0 | 48 | #define strtok_r strtok_s |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | template<typename V> |
michael@0 | 52 | bool CFIFrameInfo::FindCallerRegs(const RegisterValueMap<V> ®isters, |
michael@0 | 53 | const MemoryRegion &memory, |
michael@0 | 54 | RegisterValueMap<V> *caller_registers) const { |
michael@0 | 55 | // If there are not rules for both .ra and .cfa in effect at this address, |
michael@0 | 56 | // don't use this CFI data for stack walking. |
michael@0 | 57 | if (cfa_rule_.isExprInvalid() || ra_rule_.isExprInvalid()) |
michael@0 | 58 | return false; |
michael@0 | 59 | |
michael@0 | 60 | RegisterValueMap<V> working; |
michael@0 | 61 | PostfixEvaluator<V> evaluator(&working, &memory); |
michael@0 | 62 | |
michael@0 | 63 | caller_registers->clear(); |
michael@0 | 64 | |
michael@0 | 65 | // First, compute the CFA. |
michael@0 | 66 | V cfa; |
michael@0 | 67 | working = registers; |
michael@0 | 68 | if (!evaluator.EvaluateForValue(cfa_rule_, &cfa)) |
michael@0 | 69 | return false; |
michael@0 | 70 | |
michael@0 | 71 | // Then, compute the return address. |
michael@0 | 72 | V ra; |
michael@0 | 73 | working = registers; |
michael@0 | 74 | working.set(ustr__ZDcfa(), cfa); |
michael@0 | 75 | if (!evaluator.EvaluateForValue(ra_rule_, &ra)) |
michael@0 | 76 | return false; |
michael@0 | 77 | |
michael@0 | 78 | // Now, compute values for all the registers register_rules_ mentions. |
michael@0 | 79 | for (RuleMap::const_iterator it = register_rules_.begin(); |
michael@0 | 80 | it != register_rules_.end(); it++) { |
michael@0 | 81 | V value; |
michael@0 | 82 | working = registers; |
michael@0 | 83 | working.set(ustr__ZDcfa(), cfa); |
michael@0 | 84 | if (!evaluator.EvaluateForValue(it->second, &value)) |
michael@0 | 85 | return false; |
michael@0 | 86 | caller_registers->set(it->first, value); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | caller_registers->set(ustr__ZDra(), ra); |
michael@0 | 90 | caller_registers->set(ustr__ZDcfa(), cfa); |
michael@0 | 91 | |
michael@0 | 92 | return true; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // Explicit instantiations for 32-bit and 64-bit architectures. |
michael@0 | 96 | template bool CFIFrameInfo::FindCallerRegs<uint32_t>( |
michael@0 | 97 | const RegisterValueMap<uint32_t> ®isters, |
michael@0 | 98 | const MemoryRegion &memory, |
michael@0 | 99 | RegisterValueMap<uint32_t> *caller_registers) const; |
michael@0 | 100 | template bool CFIFrameInfo::FindCallerRegs<uint64_t>( |
michael@0 | 101 | const RegisterValueMap<uint64_t> ®isters, |
michael@0 | 102 | const MemoryRegion &memory, |
michael@0 | 103 | RegisterValueMap<uint64_t> *caller_registers) const; |
michael@0 | 104 | |
michael@0 | 105 | string CFIFrameInfo::Serialize() const { |
michael@0 | 106 | std::ostringstream stream; |
michael@0 | 107 | |
michael@0 | 108 | if (!cfa_rule_.isExprInvalid()) { |
michael@0 | 109 | stream << ".cfa: " << cfa_rule_; |
michael@0 | 110 | } |
michael@0 | 111 | if (!ra_rule_.isExprInvalid()) { |
michael@0 | 112 | if (static_cast<std::streamoff>(stream.tellp()) != 0) |
michael@0 | 113 | stream << " "; |
michael@0 | 114 | stream << ".ra: " << ra_rule_; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | // Visit the register rules in alphabetical order. Because |
michael@0 | 118 | // register_rules_ has the elements in some arbitrary order, |
michael@0 | 119 | // get the names out into a vector, sort them, and visit in |
michael@0 | 120 | // sorted order. |
michael@0 | 121 | std::vector<const UniqueString*> rr_names; |
michael@0 | 122 | for (RuleMap::const_iterator iter = register_rules_.begin(); |
michael@0 | 123 | iter != register_rules_.end(); |
michael@0 | 124 | ++iter) { |
michael@0 | 125 | rr_names.push_back(iter->first); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | std::sort(rr_names.begin(), rr_names.end(), LessThan_UniqueString); |
michael@0 | 129 | |
michael@0 | 130 | // Now visit the register rules in alphabetical order. |
michael@0 | 131 | for (std::vector<const UniqueString*>::const_iterator name = rr_names.begin(); |
michael@0 | 132 | name != rr_names.end(); |
michael@0 | 133 | ++name) { |
michael@0 | 134 | const UniqueString* nm = *name; |
michael@0 | 135 | Module::Expr rule = register_rules_.find(nm)->second; |
michael@0 | 136 | if (static_cast<std::streamoff>(stream.tellp()) != 0) |
michael@0 | 137 | stream << " "; |
michael@0 | 138 | stream << FromUniqueString(nm) << ": " << rule; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | return stream.str(); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | bool CFIRuleParser::Parse(const string &rule_set) { |
michael@0 | 145 | size_t rule_set_len = rule_set.size(); |
michael@0 | 146 | scoped_array<char> working_copy(new char[rule_set_len + 1]); |
michael@0 | 147 | memcpy(working_copy.get(), rule_set.data(), rule_set_len); |
michael@0 | 148 | working_copy[rule_set_len] = '\0'; |
michael@0 | 149 | |
michael@0 | 150 | name_ = ustr__empty(); |
michael@0 | 151 | expression_.clear(); |
michael@0 | 152 | |
michael@0 | 153 | char *cursor; |
michael@0 | 154 | static const char token_breaks[] = " \t\r\n"; |
michael@0 | 155 | char *token = strtok_r(working_copy.get(), token_breaks, &cursor); |
michael@0 | 156 | |
michael@0 | 157 | for (;;) { |
michael@0 | 158 | // End of rule set? |
michael@0 | 159 | if (!token) return Report(); |
michael@0 | 160 | |
michael@0 | 161 | // Register/pseudoregister name? |
michael@0 | 162 | size_t token_len = strlen(token); |
michael@0 | 163 | if (token_len >= 1 && token[token_len - 1] == ':') { |
michael@0 | 164 | // Names can't be empty. |
michael@0 | 165 | if (token_len < 2) return false; |
michael@0 | 166 | // If there is any pending content, report it. |
michael@0 | 167 | if (name_ != ustr__empty() || !expression_.empty()) { |
michael@0 | 168 | if (!Report()) return false; |
michael@0 | 169 | } |
michael@0 | 170 | name_ = ToUniqueString_n(token, token_len - 1); |
michael@0 | 171 | expression_.clear(); |
michael@0 | 172 | } else { |
michael@0 | 173 | // Another expression component. |
michael@0 | 174 | assert(token_len > 0); // strtok_r guarantees this, I think. |
michael@0 | 175 | if (!expression_.empty()) |
michael@0 | 176 | expression_ += ' '; |
michael@0 | 177 | expression_ += token; |
michael@0 | 178 | } |
michael@0 | 179 | token = strtok_r(NULL, token_breaks, &cursor); |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | bool CFIRuleParser::Report() { |
michael@0 | 184 | if (name_ == ustr__empty() || expression_.empty()) return false; |
michael@0 | 185 | if (name_ == ustr__ZDcfa()) handler_->CFARule(expression_); |
michael@0 | 186 | else if (name_ == ustr__ZDra()) handler_->RARule(expression_); |
michael@0 | 187 | else handler_->RegisterRule(name_, expression_); |
michael@0 | 188 | return true; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | void CFIFrameInfoParseHandler::CFARule(const string &expression) { |
michael@0 | 192 | // 'expression' is a postfix expression string. |
michael@0 | 193 | frame_info_->SetCFARule(Module::Expr(expression)); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | void CFIFrameInfoParseHandler::RARule(const string &expression) { |
michael@0 | 197 | frame_info_->SetRARule(Module::Expr(expression)); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | void CFIFrameInfoParseHandler::RegisterRule(const UniqueString* name, |
michael@0 | 201 | const string &expression) { |
michael@0 | 202 | frame_info_->SetRegisterRule(name, Module::Expr(expression)); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | } // namespace google_breakpad |