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