Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_DEX_VERIFY_H_ |
| 4 | #define ART_SRC_DEX_VERIFY_H_ |
| 5 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 6 | #include "casts.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 7 | #include "dex_file.h" |
| 8 | #include "dex_instruction.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "macros.h" |
| 10 | #include "object.h" |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 11 | #include "stl_util.h" |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 12 | #include "UniquePtr.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 13 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 14 | #include <limits> |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 15 | #include <map> |
| 16 | #include <stack> |
| 17 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 19 | namespace art { |
| 20 | namespace verifier { |
| 21 | |
| 22 | class DexVerifier; |
| 23 | class PcToReferenceMap; |
| 24 | class RegTypeCache; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 25 | |
| 26 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 27 | * Set this to enable dead code scanning. This is not required, but it's very useful when testing |
| 28 | * changes to the verifier (to make sure we're not skipping over stuff). The only reason not to do |
| 29 | * it is that it slightly increases the time required to perform verification. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 30 | */ |
| 31 | #ifndef NDEBUG |
| 32 | # define DEAD_CODE_SCAN true |
| 33 | #else |
| 34 | # define DEAD_CODE_SCAN false |
| 35 | #endif |
| 36 | |
| 37 | /* |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 38 | * RegType holds information about the "type" of data held in a register. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 39 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 40 | class RegType { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 41 | public: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 42 | enum Type { |
| 43 | kRegTypeUnknown = 0, /* initial state */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 44 | kRegTypeConflict, /* merge clash makes this reg's type unknowable */ |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 45 | kRegTypeBoolean, /* Z */ |
| 46 | kRegType1nrSTART = kRegTypeBoolean, |
| 47 | kRegTypeIntegralSTART = kRegTypeBoolean, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 48 | kRegTypeByte, /* B */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 49 | kRegTypeShort, /* S */ |
| 50 | kRegTypeChar, /* C */ |
| 51 | kRegTypeInteger, /* I */ |
| 52 | kRegTypeIntegralEND = kRegTypeInteger, |
| 53 | kRegTypeFloat, /* F */ |
| 54 | kRegType1nrEND = kRegTypeFloat, |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 55 | kRegTypeLongLo, /* J - lower-numbered register; endian-independent */ |
| 56 | kRegTypeLongHi, |
| 57 | kRegTypeDoubleLo, /* D */ |
| 58 | kRegTypeDoubleHi, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 59 | kRegTypeConstLo, /* const derived wide, lower half - could be long or double */ |
| 60 | kRegTypeConstHi, /* const derived wide, upper half - could be long or double */ |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 61 | kRegTypeLastFixedLocation = kRegTypeConstHi, |
| 62 | kRegTypeConst, /* 32-bit constant derived value - could be float or int */ |
| 63 | kRegTypeUnresolvedReference, // Reference type that couldn't be resolved |
| 64 | kRegTypeUninitializedReference, // Freshly allocated reference type |
| 65 | kRegTypeUninitializedThisReference, // Freshly allocated reference passed as "this" |
| 66 | kRegTypeUnresolvedAndUninitializedReference, // Freshly allocated unresolved reference type |
| 67 | kRegTypeReference, // Reference type |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 70 | Type GetType() const { |
| 71 | return type_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 74 | bool IsUnknown() const { return type_ == kRegTypeUnknown; } |
| 75 | bool IsConflict() const { return type_ == kRegTypeConflict; } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 76 | bool IsBoolean() const { return type_ == kRegTypeBoolean; } |
| 77 | bool IsByte() const { return type_ == kRegTypeByte; } |
| 78 | bool IsChar() const { return type_ == kRegTypeChar; } |
| 79 | bool IsShort() const { return type_ == kRegTypeShort; } |
| 80 | bool IsInteger() const { return type_ == kRegTypeInteger; } |
| 81 | bool IsLong() const { return type_ == kRegTypeLongLo; } |
| 82 | bool IsFloat() const { return type_ == kRegTypeFloat; } |
| 83 | bool IsDouble() const { return type_ == kRegTypeDoubleLo; } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 84 | bool IsUnresolvedReference() const { return type_ == kRegTypeUnresolvedReference; } |
| 85 | bool IsUninitializedReference() const { return type_ == kRegTypeUninitializedReference; } |
| 86 | bool IsUninitializedThisReference() const { return type_ == kRegTypeUninitializedThisReference; } |
| 87 | bool IsUnresolvedAndUninitializedReference() const { |
| 88 | return type_ == kRegTypeUnresolvedAndUninitializedReference; |
| 89 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 90 | bool IsReference() const { return type_ == kRegTypeReference; } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 91 | bool IsUninitializedTypes() const { |
| 92 | return IsUninitializedReference() || IsUninitializedThisReference() || |
| 93 | IsUnresolvedAndUninitializedReference(); |
| 94 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 95 | |
| 96 | bool IsLowHalf() const { return type_ == kRegTypeLongLo || |
| 97 | type_ == kRegTypeDoubleLo || |
| 98 | type_ == kRegTypeConstLo; } |
| 99 | bool IsHighHalf() const { return type_ == kRegTypeLongHi || |
| 100 | type_ == kRegTypeDoubleHi || |
| 101 | type_ == kRegTypeConstHi; } |
| 102 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 103 | bool IsLongOrDoubleTypes() const { return IsLowHalf(); } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 104 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 105 | // Check this is the low half, and that type_h is its matching high-half |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 106 | bool CheckWidePair(const RegType& type_h) const { |
| 107 | return IsLowHalf() && (type_h.type_ == type_ + 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 110 | // The high half that corresponds to this low half |
| 111 | const RegType& HighHalf(RegTypeCache* cache) const; |
| 112 | |
| 113 | bool IsConstant() const { return type_ == kRegTypeConst; } |
| 114 | bool IsLongConstant() const { return type_ == kRegTypeConstLo; } |
| 115 | bool IsLongConstantHigh() const { return type_ == kRegTypeConstHi; } |
| 116 | |
| 117 | // If this is a 32-bit constant, what is the value? This value may just |
| 118 | // approximate to the actual constant value by virtue of merging. |
| 119 | int32_t ConstantValue() const { |
| 120 | DCHECK(IsConstant()); |
| 121 | return allocation_pc_or_constant_; |
| 122 | } |
| 123 | |
| 124 | bool IsZero() const { return IsConstant() && ConstantValue() == 0; } |
| 125 | bool IsOne() const { return IsConstant() && ConstantValue() == 1; } |
| 126 | bool IsConstantBoolean() const { return IsZero() || IsOne(); } |
| 127 | bool IsConstantByte() const { |
| 128 | return IsConstant() && |
| 129 | ConstantValue() >= std::numeric_limits<jbyte>::min() && |
| 130 | ConstantValue() <= std::numeric_limits<jbyte>::max(); |
| 131 | } |
| 132 | bool IsConstantShort() const { |
| 133 | return IsConstant() && |
| 134 | ConstantValue() >= std::numeric_limits<jshort>::min() && |
| 135 | ConstantValue() <= std::numeric_limits<jshort>::max(); |
| 136 | } |
| 137 | bool IsConstantChar() const { |
| 138 | return IsConstant() && ConstantValue() >= 0 && |
| 139 | ConstantValue() <= std::numeric_limits<jchar>::max(); |
| 140 | } |
| 141 | |
| 142 | bool IsReferenceTypes() const { |
| 143 | return IsReference() || IsUnresolvedReference() || IsUninitializedReference() || |
| 144 | IsUninitializedThisReference() || IsZero(); |
| 145 | } |
| 146 | bool IsNonZeroReferenceTypes() const { |
| 147 | return IsReference() || IsUnresolvedReference() || IsUninitializedReference() || |
| 148 | IsUninitializedThisReference(); |
| 149 | } |
| 150 | bool IsCategory1Types() const { |
| 151 | return (type_ >= kRegType1nrSTART && type_ <= kRegType1nrEND) || IsConstant(); |
| 152 | } |
| 153 | bool IsCategory2Types() const { |
| 154 | return IsLowHalf(); // Don't expect explicit testing of high halves |
| 155 | } |
| 156 | |
| 157 | bool IsBooleanTypes() const { return IsBoolean() || IsConstantBoolean(); } |
| 158 | bool IsByteTypes() const { return IsByte() || IsBoolean() || IsConstantByte(); } |
| 159 | bool IsShortTypes() const { return IsShort() || IsByte() || IsBoolean() || IsConstantShort(); } |
| 160 | bool IsCharTypes() const { return IsChar() || IsBooleanTypes() || IsConstantChar(); } |
| 161 | bool IsIntegralTypes() const { |
| 162 | return (type_ >= kRegTypeIntegralSTART && type_ <= kRegTypeIntegralEND) || IsConstant(); |
| 163 | } |
| 164 | bool IsArrayIndexTypes() const { return IsIntegralTypes(); } |
| 165 | |
| 166 | // Float type may be derived from any constant type |
| 167 | bool IsFloatTypes() const { return IsFloat() || IsConstant(); } |
| 168 | |
| 169 | bool IsLongTypes() const { |
| 170 | return IsLong() || IsLongConstant(); |
| 171 | } |
| 172 | bool IsLongHighTypes() const { |
| 173 | return type_ == kRegTypeLongHi || type_ == kRegTypeConstHi; |
| 174 | } |
| 175 | bool IsDoubleTypes() const { |
| 176 | return IsDouble() || IsLongConstant(); |
| 177 | } |
| 178 | bool IsDoubleHighTypes() const { |
| 179 | return type_ == kRegTypeDoubleHi || type_ == kRegTypeConstHi; |
| 180 | } |
| 181 | |
| 182 | uint32_t GetAllocationPc() const { |
| 183 | DCHECK(IsUninitializedReference()); |
| 184 | return allocation_pc_or_constant_; |
| 185 | } |
| 186 | |
| 187 | Class* GetClass() const { |
| 188 | DCHECK(!IsUnresolvedReference()); |
| 189 | DCHECK(klass_or_descriptor_ != NULL); |
| 190 | DCHECK(klass_or_descriptor_->IsClass()); |
| 191 | return down_cast<Class*>(klass_or_descriptor_); |
| 192 | } |
| 193 | |
| 194 | String* GetDescriptor() const { |
| 195 | DCHECK(IsUnresolvedReference()); |
| 196 | DCHECK(klass_or_descriptor_ != NULL); |
| 197 | DCHECK(klass_or_descriptor_->IsString()); |
| 198 | return down_cast<String*>(klass_or_descriptor_); |
| 199 | } |
| 200 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 201 | uint16_t GetId() const { |
| 202 | return cache_id_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 205 | std::string Dump() const; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 206 | |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 207 | bool IsAssignableFrom(const RegType& src) const; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 208 | |
| 209 | const RegType& Merge(const RegType& incoming_type, RegTypeCache* reg_types) const; |
| 210 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 211 | bool Equals(const RegType& other) const { return GetId() == other.GetId(); } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 212 | |
| 213 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 214 | * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is |
| 215 | * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of |
| 216 | * S and T such that there isn't a parent of both S and T that isn't also the parent of J (ie J |
| 217 | * is the deepest (lowest upper bound) parent of S and T). |
| 218 | * |
| 219 | * This operation applies for regular classes and arrays, however, for interface types there needn't |
| 220 | * be a partial ordering on the types. We could solve the problem of a lack of a partial order by |
| 221 | * introducing sets of types, however, the only operation permissible on an interface is |
| 222 | * invoke-interface. In the tradition of Java verifiers [1] we defer the verification of interface |
| 223 | * types until an invoke-interface call on the interface typed reference at runtime and allow |
| 224 | * the perversion of Object being assignable to an interface type (note, however, that we don't |
| 225 | * allow assignment of Object or Interface to any concrete class and are therefore type safe). |
| 226 | * |
| 227 | * [1] Java bytecode verifcation: algorithms and formalizations, Xavier Leroy |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 228 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 229 | static Class* ClassJoin(Class* s, Class* t); |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 230 | |
| 231 | private: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 232 | friend class RegTypeCache; |
| 233 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 234 | RegType(Type type, Object* klass_or_descriptor, uint32_t allocation_pc_or_constant, uint16_t cache_id) : |
| 235 | type_(type), klass_or_descriptor_(klass_or_descriptor), allocation_pc_or_constant_(allocation_pc_or_constant), |
| 236 | cache_id_(cache_id) { |
| 237 | DCHECK(IsConstant() || IsUninitializedReference() || allocation_pc_or_constant == 0); |
| 238 | if (!IsConstant() && !IsLongConstant() && !IsLongConstantHigh() && !IsUnknown() && |
| 239 | !IsConflict()) { |
| 240 | DCHECK(klass_or_descriptor != NULL); |
| 241 | DCHECK(IsUnresolvedReference() || klass_or_descriptor_->IsClass()); |
| 242 | DCHECK(!IsUnresolvedReference() || klass_or_descriptor_->IsString()); |
| 243 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | const Type type_; // The current type of the register |
| 247 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 248 | // If known the type of the register, else a String for the descriptor |
| 249 | Object* klass_or_descriptor_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 250 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 251 | // Overloaded field that: |
| 252 | // - if IsConstant() holds a 32bit constant value |
| 253 | // - is IsReference() holds the allocation_pc or kInitArgAddr for an initialized reference or |
| 254 | // kUninitThisArgAddr for an uninitialized this ptr |
| 255 | const uint32_t allocation_pc_or_constant_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 256 | |
| 257 | // A RegType cache densely encodes types, this is the location in the cache for this type |
| 258 | const uint16_t cache_id_; |
| 259 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 260 | DISALLOW_COPY_AND_ASSIGN(RegType); |
| 261 | }; |
| 262 | std::ostream& operator<<(std::ostream& os, const RegType& rhs); |
| 263 | |
| 264 | class RegTypeCache { |
| 265 | public: |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 266 | explicit RegTypeCache() : entries_(RegType::kRegTypeLastFixedLocation + 1) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 267 | Unknown(); // ensure Unknown is initialized |
| 268 | } |
| 269 | ~RegTypeCache() { |
| 270 | STLDeleteElements(&entries_); |
| 271 | } |
| 272 | |
| 273 | const RegType& GetFromId(uint16_t id) { |
| 274 | DCHECK_LT(id, entries_.size()); |
| 275 | RegType* result = entries_[id]; |
| 276 | DCHECK(result != NULL); |
| 277 | return *result; |
| 278 | } |
| 279 | |
| 280 | const RegType& From(RegType::Type type, const ClassLoader* loader, const std::string& descriptor); |
| 281 | const RegType& FromClass(Class* klass); |
| 282 | const RegType& FromCat1Const(int32_t value); |
| 283 | const RegType& FromDescriptor(const ClassLoader* loader, const std::string& descriptor); |
| 284 | const RegType& FromType(RegType::Type); |
| 285 | |
| 286 | const RegType& Boolean() { return FromType(RegType::kRegTypeBoolean); } |
| 287 | const RegType& Byte() { return FromType(RegType::kRegTypeByte); } |
| 288 | const RegType& Char() { return FromType(RegType::kRegTypeChar); } |
| 289 | const RegType& Short() { return FromType(RegType::kRegTypeShort); } |
| 290 | const RegType& Integer() { return FromType(RegType::kRegTypeInteger); } |
| 291 | const RegType& Float() { return FromType(RegType::kRegTypeFloat); } |
| 292 | const RegType& Long() { return FromType(RegType::kRegTypeLongLo); } |
| 293 | const RegType& Double() { return FromType(RegType::kRegTypeDoubleLo); } |
| 294 | |
| 295 | const RegType& JavaLangClass() { return From(RegType::kRegTypeReference, NULL, "Ljava/lang/Class;"); } |
| 296 | const RegType& JavaLangObject() { return From(RegType::kRegTypeReference, NULL, "Ljava/lang/Object;"); } |
| 297 | const RegType& JavaLangString() { return From(RegType::kRegTypeReference, NULL, "Ljava/lang/String;"); } |
| 298 | |
| 299 | const RegType& Unknown() { return FromType(RegType::kRegTypeUnknown); } |
| 300 | const RegType& Conflict() { return FromType(RegType::kRegTypeConflict); } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 301 | const RegType& ConstLo() { return FromType(RegType::kRegTypeConstLo); } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 302 | const RegType& Zero() { return FromCat1Const(0); } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 303 | |
| 304 | const RegType& Uninitialized(Class* klass, uint32_t allocation_pc); |
| 305 | const RegType& UninitializedThisArgument(Class* klass); |
| 306 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 307 | // Representatives of various constant types. When merging constants we can't infer a type, |
| 308 | // (an int may later be used as a float) so we select these representative values meaning future |
| 309 | // merges won't know the exact constant value but have some notion of its size. |
| 310 | const RegType& ByteConstant() { return FromCat1Const(std::numeric_limits<jbyte>::min()); } |
| 311 | const RegType& ShortConstant() { return FromCat1Const(std::numeric_limits<jshort>::min()); } |
| 312 | const RegType& IntConstant() { return FromCat1Const(std::numeric_limits<jint>::max()); } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 313 | private: |
| 314 | // The allocated entries |
| 315 | std::vector<RegType*> entries_; |
| 316 | |
| 317 | DISALLOW_COPY_AND_ASSIGN(RegTypeCache); |
| 318 | }; |
| 319 | |
| 320 | class InsnFlags { |
| 321 | public: |
| 322 | InsnFlags() : length_(0), flags_(0) {} |
| 323 | |
| 324 | void SetLengthInCodeUnits(size_t length) { |
| 325 | CHECK_LT(length, 65536u); |
| 326 | length_ = length; |
| 327 | } |
| 328 | size_t GetLengthInCodeUnits() { |
| 329 | return length_; |
| 330 | } |
| 331 | bool IsOpcode() const { |
| 332 | return length_ != 0; |
| 333 | } |
| 334 | |
| 335 | void SetInTry() { |
| 336 | flags_ |= 1 << kInsnFlagInTry; |
| 337 | } |
| 338 | void ClearInTry() { |
| 339 | flags_ &= ~(1 << kInsnFlagInTry); |
| 340 | } |
| 341 | bool IsInTry() const { |
| 342 | return (flags_ & (1 << kInsnFlagInTry)) != 0; |
| 343 | } |
| 344 | |
| 345 | void SetBranchTarget() { |
| 346 | flags_ |= 1 << kInsnFlagBranchTarget; |
| 347 | } |
| 348 | void ClearBranchTarget() { |
| 349 | flags_ &= ~(1 << kInsnFlagBranchTarget); |
| 350 | } |
| 351 | bool IsBranchTarget() const { |
| 352 | return (flags_ & (1 << kInsnFlagBranchTarget)) != 0; |
| 353 | } |
| 354 | |
| 355 | void SetGcPoint() { |
| 356 | flags_ |= 1 << kInsnFlagGcPoint; |
| 357 | } |
| 358 | void ClearGcPoint() { |
| 359 | flags_ &= ~(1 << kInsnFlagGcPoint); |
| 360 | } |
| 361 | bool IsGcPoint() const { |
| 362 | return (flags_ & (1 << kInsnFlagGcPoint)) != 0; |
| 363 | } |
| 364 | |
| 365 | void SetVisited() { |
| 366 | flags_ |= 1 << kInsnFlagVisited; |
| 367 | } |
| 368 | void ClearVisited() { |
| 369 | flags_ &= ~(1 << kInsnFlagVisited); |
| 370 | } |
| 371 | bool IsVisited() const { |
| 372 | return (flags_ & (1 << kInsnFlagVisited)) != 0; |
| 373 | } |
| 374 | |
| 375 | void SetChanged() { |
| 376 | flags_ |= 1 << kInsnFlagChanged; |
| 377 | } |
| 378 | void ClearChanged() { |
| 379 | flags_ &= ~(1 << kInsnFlagChanged); |
| 380 | } |
| 381 | bool IsChanged() const { |
| 382 | return (flags_ & (1 << kInsnFlagChanged)) != 0; |
| 383 | } |
| 384 | |
| 385 | bool IsVisitedOrChanged() const { |
| 386 | return IsVisited() || IsChanged(); |
| 387 | } |
| 388 | |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 389 | std::string Dump() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 390 | char encoding[6]; |
| 391 | if (!IsOpcode()) { |
| 392 | strncpy(encoding, "XXXXX", sizeof(encoding)); |
| 393 | } else { |
| 394 | strncpy(encoding, "-----", sizeof(encoding)); |
| 395 | if (IsInTry()) encoding[kInsnFlagInTry] = 'T'; |
| 396 | if (IsBranchTarget()) encoding[kInsnFlagBranchTarget] = 'B'; |
| 397 | if (IsGcPoint()) encoding[kInsnFlagGcPoint] = 'G'; |
| 398 | if (IsVisited()) encoding[kInsnFlagVisited] = 'V'; |
| 399 | if (IsChanged()) encoding[kInsnFlagChanged] = 'C'; |
| 400 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 401 | return std::string(encoding); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 402 | } |
| 403 | private: |
| 404 | enum InsnFlag { |
| 405 | kInsnFlagInTry, |
| 406 | kInsnFlagBranchTarget, |
| 407 | kInsnFlagGcPoint, |
| 408 | kInsnFlagVisited, |
| 409 | kInsnFlagChanged, |
| 410 | }; |
| 411 | |
| 412 | // Size of instruction in code units |
| 413 | uint16_t length_; |
| 414 | uint8_t flags_; |
| 415 | }; |
| 416 | |
| 417 | /* |
| 418 | * "Direct" and "virtual" methods are stored independently. The type of call used to invoke the |
| 419 | * method determines which list we search, and whether we travel up into superclasses. |
| 420 | * |
| 421 | * (<clinit>, <init>, and methods declared "private" or "static" are stored in the "direct" list. |
| 422 | * All others are stored in the "virtual" list.) |
| 423 | */ |
| 424 | enum MethodType { |
| 425 | METHOD_UNKNOWN = 0, |
| 426 | METHOD_DIRECT, // <init>, private |
| 427 | METHOD_STATIC, // static |
| 428 | METHOD_VIRTUAL, // virtual, super |
| 429 | METHOD_INTERFACE // interface |
| 430 | }; |
| 431 | |
| 432 | const int kRegTypeUninitMask = 0xff; |
| 433 | const int kRegTypeUninitShift = 8; |
| 434 | |
| 435 | /* |
| 436 | * Register type categories, for type checking. |
| 437 | * |
| 438 | * The spec says category 1 includes boolean, byte, char, short, int, float, reference, and |
| 439 | * returnAddress. Category 2 includes long and double. |
| 440 | * |
| 441 | * We treat object references separately, so we have "category1nr". We don't support jsr/ret, so |
| 442 | * there is no "returnAddress" type. |
| 443 | */ |
| 444 | enum TypeCategory { |
| 445 | kTypeCategoryUnknown = 0, |
| 446 | kTypeCategory1nr = 1, // boolean, byte, char, short, int, float |
| 447 | kTypeCategory2 = 2, // long, double |
| 448 | kTypeCategoryRef = 3, // object reference |
| 449 | }; |
| 450 | |
| 451 | /* |
| 452 | * An enumeration of problems that can turn up during verification. |
| 453 | * VERIFY_ERROR_GENERIC denotes a failure that causes the entire class to be rejected. Other errors |
| 454 | * denote verification errors that cause bytecode to be rewritten to fail at runtime. |
| 455 | */ |
| 456 | enum VerifyError { |
| 457 | VERIFY_ERROR_NONE = 0, /* no error; must be zero */ |
| 458 | VERIFY_ERROR_GENERIC, /* VerifyError */ |
| 459 | |
| 460 | VERIFY_ERROR_NO_CLASS, /* NoClassDefFoundError */ |
| 461 | VERIFY_ERROR_NO_FIELD, /* NoSuchFieldError */ |
| 462 | VERIFY_ERROR_NO_METHOD, /* NoSuchMethodError */ |
| 463 | VERIFY_ERROR_ACCESS_CLASS, /* IllegalAccessError */ |
| 464 | VERIFY_ERROR_ACCESS_FIELD, /* IllegalAccessError */ |
| 465 | VERIFY_ERROR_ACCESS_METHOD, /* IllegalAccessError */ |
| 466 | VERIFY_ERROR_CLASS_CHANGE, /* IncompatibleClassChangeError */ |
| 467 | VERIFY_ERROR_INSTANTIATION, /* InstantiationError */ |
| 468 | }; |
| 469 | std::ostream& operator<<(std::ostream& os, const VerifyError& rhs); |
| 470 | |
| 471 | /* |
| 472 | * Identifies the type of reference in the instruction that generated the verify error |
| 473 | * (e.g. VERIFY_ERROR_ACCESS_CLASS could come from a method, field, or class reference). |
| 474 | * |
| 475 | * This must fit in two bits. |
| 476 | */ |
| 477 | enum VerifyErrorRefType { |
| 478 | VERIFY_ERROR_REF_CLASS = 0, |
| 479 | VERIFY_ERROR_REF_FIELD = 1, |
| 480 | VERIFY_ERROR_REF_METHOD = 2, |
| 481 | }; |
| 482 | const int kVerifyErrorRefTypeShift = 6; |
| 483 | |
| 484 | /* |
| 485 | * Format enumeration for RegisterMap data area. |
| 486 | */ |
| 487 | enum RegisterMapFormat { |
| 488 | kRegMapFormatUnknown = 0, |
| 489 | kRegMapFormatNone, /* indicates no map data follows */ |
| 490 | kRegMapFormatCompact8, /* compact layout, 8-bit addresses */ |
| 491 | kRegMapFormatCompact16, /* compact layout, 16-bit addresses */ |
| 492 | }; |
| 493 | |
| 494 | // During verification, we associate one of these with every "interesting" instruction. We track |
| 495 | // the status of all registers, and (if the method has any monitor-enter instructions) maintain a |
| 496 | // stack of entered monitors (identified by code unit offset). |
| 497 | // If live-precise register maps are enabled, the "liveRegs" vector will be populated. Unlike the |
| 498 | // other lists of registers here, we do not track the liveness of the method result register |
| 499 | // (which is not visible to the GC). |
| 500 | class RegisterLine { |
| 501 | public: |
| 502 | RegisterLine(size_t num_regs, DexVerifier* verifier) : |
| 503 | line_(new uint16_t[num_regs]), verifier_(verifier), num_regs_(num_regs) { |
| 504 | memset(line_.get(), 0, num_regs_ * sizeof(uint16_t)); |
| 505 | result_[0] = RegType::kRegTypeUnknown; |
| 506 | result_[1] = RegType::kRegTypeUnknown; |
| 507 | } |
| 508 | |
| 509 | // Implement category-1 "move" instructions. Copy a 32-bit value from "vsrc" to "vdst". |
| 510 | void CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat); |
| 511 | |
| 512 | // Implement category-2 "move" instructions. Copy a 64-bit value from "vsrc" to "vdst". This |
| 513 | // copies both halves of the register. |
| 514 | void CopyRegister2(uint32_t vdst, uint32_t vsrc); |
| 515 | |
| 516 | // Implement "move-result". Copy the category-1 value from the result register to another |
| 517 | // register, and reset the result register. |
| 518 | void CopyResultRegister1(uint32_t vdst, bool is_reference); |
| 519 | |
| 520 | // Implement "move-result-wide". Copy the category-2 value from the result register to another |
| 521 | // register, and reset the result register. |
| 522 | void CopyResultRegister2(uint32_t vdst); |
| 523 | |
| 524 | // Set the invisible result register to unknown |
| 525 | void SetResultTypeToUnknown(); |
| 526 | |
| 527 | // Set the type of register N, verifying that the register is valid. If "newType" is the "Lo" |
| 528 | // part of a 64-bit value, register N+1 will be set to "newType+1". |
| 529 | // The register index was validated during the static pass, so we don't need to check it here. |
| 530 | void SetRegisterType(uint32_t vdst, const RegType& new_type); |
| 531 | |
| 532 | /* Set the type of the "result" register. */ |
| 533 | void SetResultRegisterType(const RegType& new_type); |
| 534 | |
| 535 | // Get the type of register vsrc. |
| 536 | const RegType& GetRegisterType(uint32_t vsrc) const; |
| 537 | |
| 538 | bool VerifyRegisterType(uint32_t vsrc, const RegType& check_type); |
| 539 | |
| 540 | void CopyFromLine(const RegisterLine* src) { |
| 541 | DCHECK_EQ(num_regs_, src->num_regs_); |
| 542 | memcpy(line_.get(), src->line_.get(), num_regs_ * sizeof(uint16_t)); |
| 543 | monitors_ = src->monitors_; |
| 544 | reg_to_lock_depths_ = src->reg_to_lock_depths_; |
| 545 | } |
| 546 | |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 547 | std::string Dump() const { |
| 548 | std::string result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 549 | for (size_t i = 0; i < num_regs_; i++) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 550 | result += StringPrintf("%d:[", i); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 551 | result += GetRegisterType(i).Dump(); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 552 | result += "],"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 553 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 554 | return result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void FillWithGarbage() { |
| 558 | memset(line_.get(), 0xf1, num_regs_ * sizeof(uint16_t)); |
| 559 | while (!monitors_.empty()) { |
| 560 | monitors_.pop(); |
| 561 | } |
| 562 | reg_to_lock_depths_.clear(); |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * We're creating a new instance of class C at address A. Any registers holding instances |
| 567 | * previously created at address A must be initialized by now. If not, we mark them as "conflict" |
| 568 | * to prevent them from being used (otherwise, MarkRefsAsInitialized would mark the old ones and |
| 569 | * the new ones at the same time). |
| 570 | */ |
| 571 | void MarkUninitRefsAsInvalid(const RegType& uninit_type); |
| 572 | |
| 573 | /* |
| 574 | * Update all registers holding "uninit_type" to instead hold the corresponding initialized |
| 575 | * reference type. This is called when an appropriate constructor is invoked -- all copies of |
| 576 | * the reference must be marked as initialized. |
| 577 | */ |
| 578 | void MarkRefsAsInitialized(const RegType& uninit_type); |
| 579 | |
| 580 | /* |
| 581 | * Check constraints on constructor return. Specifically, make sure that the "this" argument got |
| 582 | * initialized. |
| 583 | * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start |
| 584 | * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it |
| 585 | * somehow didn't get initialized. |
| 586 | */ |
| 587 | bool CheckConstructorReturn() const; |
| 588 | |
| 589 | // Compare two register lines. Returns 0 if they match. |
| 590 | // Using this for a sort is unwise, since the value can change based on machine endianness. |
| 591 | int CompareLine(const RegisterLine* line2) const { |
| 592 | DCHECK(monitors_ == line2->monitors_); |
| 593 | // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_); |
| 594 | return memcmp(line_.get(), line2->line_.get(), num_regs_ * sizeof(uint16_t)); |
| 595 | } |
| 596 | |
| 597 | size_t NumRegs() const { |
| 598 | return num_regs_; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * Get the "this" pointer from a non-static method invocation. This returns the RegType so the |
| 603 | * caller can decide whether it needs the reference to be initialized or not. (Can also return |
| 604 | * kRegTypeZero if the reference can only be zero at this point.) |
| 605 | * |
| 606 | * The argument count is in vA, and the first argument is in vC, for both "simple" and "range" |
| 607 | * versions. We just need to make sure vA is >= 1 and then return vC. |
| 608 | */ |
| 609 | const RegType& GetInvocationThis(const Instruction::DecodedInstruction& dec_insn); |
| 610 | |
| 611 | /* |
| 612 | * Get the value from a register, and cast it to a Class. Sets "*failure" if something fails. |
| 613 | * This fails if the register holds an uninitialized class. |
| 614 | * If the register holds kRegTypeZero, this returns a NULL pointer. |
| 615 | */ |
| 616 | Class* GetClassFromRegister(uint32_t vsrc) const; |
| 617 | |
| 618 | /* |
| 619 | * Verify types for a simple two-register instruction (e.g. "neg-int"). |
| 620 | * "dst_type" is stored into vA, and "src_type" is verified against vB. |
| 621 | */ |
| 622 | void CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn, |
| 623 | const RegType& dst_type, const RegType& src_type); |
| 624 | |
| 625 | /* |
| 626 | * Verify types for a simple three-register instruction (e.g. "add-int"). |
| 627 | * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified |
| 628 | * against vB/vC. |
| 629 | */ |
| 630 | void CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn, |
| 631 | const RegType& dst_type, const RegType& src_type1, const RegType& src_type2, |
| 632 | bool check_boolean_op); |
| 633 | |
| 634 | /* |
| 635 | * Verify types for a binary "2addr" operation. "src_type1"/"src_type2" |
| 636 | * are verified against vA/vB, then "dst_type" is stored into vA. |
| 637 | */ |
| 638 | void CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn, |
| 639 | const RegType& dst_type, |
| 640 | const RegType& src_type1, const RegType& src_type2, |
| 641 | bool check_boolean_op); |
| 642 | |
| 643 | /* |
| 644 | * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8"). |
| 645 | * "dst_type" is stored into vA, and "src_type" is verified against vB. |
| 646 | * |
| 647 | * If "check_boolean_op" is set, we use the constant value in vC. |
| 648 | */ |
| 649 | void CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn, |
| 650 | const RegType& dst_type, const RegType& src_type, bool check_boolean_op); |
| 651 | |
| 652 | // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx. |
| 653 | void PushMonitor(uint32_t reg_idx, int32_t insn_idx); |
| 654 | |
| 655 | // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked |
| 656 | void PopMonitor(uint32_t reg_idx); |
| 657 | |
| 658 | // Stack of currently held monitors and where they were locked |
| 659 | size_t MonitorStackDepth() const { |
| 660 | return monitors_.size(); |
| 661 | } |
| 662 | |
| 663 | // We expect no monitors to be held at certain points, such a method returns. Verify the stack |
| 664 | // is empty, failing and returning false if not. |
| 665 | bool VerifyMonitorStackEmpty(); |
| 666 | |
| 667 | bool MergeRegisters(const RegisterLine* incoming_line); |
| 668 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 669 | size_t GetMaxNonZeroReferenceReg(size_t max_ref_reg) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 670 | size_t i = static_cast<int>(max_ref_reg) < 0 ? 0 : max_ref_reg; |
| 671 | for(; i < num_regs_; i++) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 672 | if (GetRegisterType(i).IsNonZeroReferenceTypes()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 673 | max_ref_reg = i; |
| 674 | } |
| 675 | } |
| 676 | return max_ref_reg; |
| 677 | } |
| 678 | |
| 679 | // Write a bit at each register location that holds a reference |
| 680 | void WriteReferenceBitMap(int8_t* data, size_t max_bytes); |
| 681 | private: |
| 682 | |
| 683 | void CopyRegToLockDepth(size_t dst, size_t src) { |
| 684 | if (reg_to_lock_depths_.count(src) > 0) { |
| 685 | uint32_t depths = reg_to_lock_depths_[src]; |
| 686 | reg_to_lock_depths_[dst] = depths; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | bool IsSetLockDepth(size_t reg, size_t depth) { |
| 691 | if (reg_to_lock_depths_.count(reg) > 0) { |
| 692 | uint32_t depths = reg_to_lock_depths_[reg]; |
| 693 | return (depths & (1 << depth)) != 0; |
| 694 | } else { |
| 695 | return false; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | void SetRegToLockDepth(size_t reg, size_t depth) { |
| 700 | CHECK_LT(depth, 32u); |
| 701 | DCHECK(!IsSetLockDepth(reg, depth)); |
| 702 | uint32_t depths; |
| 703 | if (reg_to_lock_depths_.count(reg) > 0) { |
| 704 | depths = reg_to_lock_depths_[reg]; |
| 705 | depths = depths | (1 << depth); |
| 706 | } else { |
| 707 | depths = 1 << depth; |
| 708 | } |
| 709 | reg_to_lock_depths_[reg] = depths; |
| 710 | } |
| 711 | |
| 712 | void ClearRegToLockDepth(size_t reg, size_t depth) { |
| 713 | CHECK_LT(depth, 32u); |
| 714 | DCHECK(IsSetLockDepth(reg, depth)); |
| 715 | uint32_t depths = reg_to_lock_depths_[reg]; |
| 716 | depths = depths ^ (1 << depth); |
| 717 | if (depths != 0) { |
| 718 | reg_to_lock_depths_[reg] = depths; |
| 719 | } else { |
| 720 | reg_to_lock_depths_.erase(reg); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | void ClearAllRegToLockDepths(size_t reg) { |
| 725 | reg_to_lock_depths_.erase(reg); |
| 726 | } |
| 727 | |
| 728 | // Storage for the result register's type, valid after an invocation |
| 729 | uint16_t result_[2]; |
| 730 | |
| 731 | // An array of RegType Ids associated with each dex register |
| 732 | UniquePtr<uint16_t[]> line_; |
| 733 | |
| 734 | // Back link to the verifier |
| 735 | DexVerifier* verifier_; |
| 736 | |
| 737 | // Length of reg_types_ |
| 738 | const size_t num_regs_; |
| 739 | // A stack of monitor enter locations |
| 740 | std::stack<uint32_t> monitors_; |
| 741 | // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor |
| 742 | // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a |
| 743 | // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5 |
| 744 | std::map<uint32_t, uint32_t> reg_to_lock_depths_; |
| 745 | }; |
| 746 | std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs); |
| 747 | |
| 748 | class PcToRegisterLineTable { |
| 749 | public: |
| 750 | // We don't need to store the register data for many instructions, because we either only need |
| 751 | // it at branch points (for verification) or GC points and branches (for verification + |
| 752 | // type-precise register analysis). |
| 753 | enum RegisterTrackingMode { |
| 754 | kTrackRegsBranches, |
| 755 | kTrackRegsGcPoints, |
| 756 | kTrackRegsAll, |
| 757 | }; |
| 758 | PcToRegisterLineTable() {} |
| 759 | ~PcToRegisterLineTable() { |
| 760 | STLDeleteValues(&pc_to_register_line_); |
| 761 | } |
| 762 | |
| 763 | // Initialize the RegisterTable. Every instruction address can have a different set of information |
| 764 | // about what's in which register, but for verification purposes we only need to store it at |
| 765 | // branch target addresses (because we merge into that). |
| 766 | void Init(RegisterTrackingMode mode, InsnFlags* flags, uint32_t insns_size, |
| 767 | uint16_t registers_size, DexVerifier* verifier); |
| 768 | |
| 769 | RegisterLine* GetLine(size_t idx) { |
| 770 | return pc_to_register_line_[idx]; |
| 771 | } |
| 772 | |
| 773 | private: |
| 774 | // Map from a dex pc to the register status associated with it |
| 775 | std::map<int32_t, RegisterLine*> pc_to_register_line_; |
| 776 | |
| 777 | // Number of registers we track for each instruction. This is equal to the method's declared |
| 778 | // "registersSize" plus kExtraRegs (2). |
| 779 | size_t insn_reg_count_plus_; |
| 780 | }; |
| 781 | |
| 782 | |
| 783 | |
| 784 | // The verifier |
| 785 | class DexVerifier { |
| 786 | public: |
| 787 | /* Verify a class. Returns "true" on success. */ |
| 788 | static bool VerifyClass(const Class* klass); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 789 | /* |
| 790 | * Perform verification on a single method. |
| 791 | * |
| 792 | * We do this in three passes: |
| 793 | * (1) Walk through all code units, determining instruction locations, |
| 794 | * widths, and other characteristics. |
| 795 | * (2) Walk through all code units, performing static checks on |
| 796 | * operands. |
| 797 | * (3) Iterate through the method, checking type safety and looking |
| 798 | * for code flow problems. |
| 799 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 800 | * Some checks may be bypassed depending on the verification mode. We can't |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 801 | * turn this stuff off completely if we want to do "exact" GC. |
| 802 | * |
| 803 | * Confirmed here: |
| 804 | * - code array must not be empty |
| 805 | * Confirmed by ComputeWidthsAndCountOps(): |
| 806 | * - opcode of first instruction begins at index 0 |
| 807 | * - only documented instructions may appear |
| 808 | * - each instruction follows the last |
| 809 | * - last byte of last instruction is at (code_length-1) |
| 810 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 811 | static bool VerifyMethod(Method* method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 812 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 813 | uint8_t EncodePcToReferenceMapData() const; |
| 814 | |
| 815 | uint32_t DexFileVersion() const { |
| 816 | return dex_file_->GetVersion(); |
| 817 | } |
| 818 | |
| 819 | RegTypeCache* GetRegTypeCache() { |
| 820 | return ®_types_; |
| 821 | } |
| 822 | |
| 823 | // Verification failed |
| 824 | std::ostream& Fail(VerifyError error) { |
| 825 | CHECK_EQ(failure_, VERIFY_ERROR_NONE); |
| 826 | failure_ = error; |
| 827 | return fail_messages_ << "VFY: " << PrettyMethod(method_) |
| 828 | << '[' << (void*)work_insn_idx_ << "] : "; |
| 829 | } |
| 830 | |
| 831 | // Log for verification information |
| 832 | std::ostream& LogVerifyInfo() { |
| 833 | return info_messages_ << "VFY: " << PrettyMethod(method_) |
| 834 | << '[' << (void*)work_insn_idx_ << "] : "; |
| 835 | } |
| 836 | |
| 837 | // Dump the state of the verifier, namely each instruction, what flags are set on it, register |
| 838 | // information |
| 839 | void Dump(std::ostream& os); |
| 840 | |
| 841 | private: |
| 842 | |
| 843 | explicit DexVerifier(Method* method); |
| 844 | |
| 845 | bool Verify(); |
| 846 | |
| 847 | /* |
| 848 | * Compute the width of the instruction at each address in the instruction stream, and store it in |
| 849 | * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch |
| 850 | * table data, are not touched (so the caller should probably initialize "insn_flags" to zero). |
| 851 | * |
| 852 | * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set. |
| 853 | * |
| 854 | * Performs some static checks, notably: |
| 855 | * - opcode of first instruction begins at index 0 |
| 856 | * - only documented instructions may appear |
| 857 | * - each instruction follows the last |
| 858 | * - last byte of last instruction is at (code_length-1) |
| 859 | * |
| 860 | * Logs an error and returns "false" on failure. |
| 861 | */ |
| 862 | bool ComputeWidthsAndCountOps(); |
| 863 | |
| 864 | /* |
| 865 | * Set the "in try" flags for all instructions protected by "try" statements. Also sets the |
| 866 | * "branch target" flags for exception handlers. |
| 867 | * |
| 868 | * Call this after widths have been set in "insn_flags". |
| 869 | * |
| 870 | * Returns "false" if something in the exception table looks fishy, but we're expecting the |
| 871 | * exception table to be somewhat sane. |
| 872 | */ |
| 873 | bool ScanTryCatchBlocks(); |
| 874 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 875 | /* |
| 876 | * Perform static verification on all instructions in a method. |
| 877 | * |
| 878 | * Walks through instructions in a method calling VerifyInstruction on each. |
| 879 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 880 | bool VerifyInstructions(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 881 | |
| 882 | /* |
| 883 | * Perform static verification on an instruction. |
| 884 | * |
| 885 | * As a side effect, this sets the "branch target" flags in InsnFlags. |
| 886 | * |
| 887 | * "(CF)" items are handled during code-flow analysis. |
| 888 | * |
| 889 | * v3 4.10.1 |
| 890 | * - target of each jump and branch instruction must be valid |
| 891 | * - targets of switch statements must be valid |
| 892 | * - operands referencing constant pool entries must be valid |
| 893 | * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid |
| 894 | * - (CF) operands of method invocation instructions must be valid |
| 895 | * - (CF) only invoke-direct can call a method starting with '<' |
| 896 | * - (CF) <clinit> must never be called explicitly |
| 897 | * - operands of instanceof, checkcast, new (and variants) must be valid |
| 898 | * - new-array[-type] limited to 255 dimensions |
| 899 | * - can't use "new" on an array class |
| 900 | * - (?) limit dimensions in multi-array creation |
| 901 | * - local variable load/store register values must be in valid range |
| 902 | * |
| 903 | * v3 4.11.1.2 |
| 904 | * - branches must be within the bounds of the code array |
| 905 | * - targets of all control-flow instructions are the start of an instruction |
| 906 | * - register accesses fall within range of allocated registers |
| 907 | * - (N/A) access to constant pool must be of appropriate type |
| 908 | * - code does not end in the middle of an instruction |
| 909 | * - execution cannot fall off the end of the code |
| 910 | * - (earlier) for each exception handler, the "try" area must begin and |
| 911 | * end at the start of an instruction (end can be at the end of the code) |
| 912 | * - (earlier) for each exception handler, the handler must start at a valid |
| 913 | * instruction |
| 914 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 915 | bool VerifyInstruction(const Instruction* inst, uint32_t code_offset); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 916 | |
| 917 | /* Ensure that the register index is valid for this code item. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 918 | bool CheckRegisterIndex(uint32_t idx); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 919 | |
| 920 | /* Ensure that the wide register index is valid for this code item. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 921 | bool CheckWideRegisterIndex(uint32_t idx); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 922 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 923 | // Perform static checks on a field get or set instruction. All we do here is ensure that the |
| 924 | // field index is in the valid range. |
| 925 | bool CheckFieldIndex(uint32_t idx); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 926 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 927 | // Perform static checks on a method invocation instruction. All we do here is ensure that the |
| 928 | // method index is in the valid range. |
| 929 | bool CheckMethodIndex(uint32_t idx); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 930 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 931 | // Perform static checks on a "new-instance" instruction. Specifically, make sure the class |
| 932 | // reference isn't for an array class. |
| 933 | bool CheckNewInstance(uint32_t idx); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 934 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 935 | /* Ensure that the string index is in the valid range. */ |
| 936 | bool CheckStringIndex(uint32_t idx); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 937 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 938 | // Perform static checks on an instruction that takes a class constant. Ensure that the class |
| 939 | // index is in the valid range. |
| 940 | bool CheckTypeIndex(uint32_t idx); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 941 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 942 | // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't |
| 943 | // creating an array of arrays that causes the number of dimensions to exceed 255. |
| 944 | bool CheckNewArray(uint32_t idx); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 945 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 946 | // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction. |
| 947 | bool CheckArrayData(uint32_t cur_offset); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 948 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 949 | // Verify that the target of a branch instruction is valid. We don't expect code to jump directly |
| 950 | // into an exception handler, but it's valid to do so as long as the target isn't a |
| 951 | // "move-exception" instruction. We verify that in a later stage. |
| 952 | // The dex format forbids certain instructions from branching to themselves. |
| 953 | // Updates "insnFlags", setting the "branch target" flag. |
| 954 | bool CheckBranchTarget(uint32_t cur_offset); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 955 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 956 | // Verify a switch table. "cur_offset" is the offset of the switch instruction. |
| 957 | // Updates "insnFlags", setting the "branch target" flag. |
| 958 | bool CheckSwitchTargets(uint32_t cur_offset); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 959 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 960 | // Check the register indices used in a "vararg" instruction, such as invoke-virtual or |
| 961 | // filled-new-array. |
| 962 | // - vA holds word count (0-5), args[] have values. |
| 963 | // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that |
| 964 | // takes a double is done with consecutive registers. This requires parsing the target method |
| 965 | // signature, which we will be doing later on during the code flow analysis. |
| 966 | bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 967 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 968 | // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range |
| 969 | // or filled-new-array/range. |
| 970 | // - vA holds word count, vC holds index of first reg. |
| 971 | bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 972 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 973 | // Extract the relative offset from a branch instruction. |
| 974 | // Returns "false" on failure (e.g. this isn't a branch instruction). |
| 975 | bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
| 976 | bool* selfOkay); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 977 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 978 | /* Perform detailed code-flow analysis on a single method. */ |
| 979 | bool VerifyCodeFlow(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 980 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 981 | // Set the register types for the first instruction in the method based on the method signature. |
| 982 | // This has the side-effect of validating the signature. |
| 983 | bool SetTypesFromSignature(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 984 | |
| 985 | /* |
| 986 | * Perform code flow on a method. |
| 987 | * |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 988 | * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first |
| 989 | * instruction, process it (setting additional "changed" bits), and repeat until there are no |
| 990 | * more. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 991 | * |
| 992 | * v3 4.11.1.1 |
| 993 | * - (N/A) operand stack is always the same size |
| 994 | * - operand stack [registers] contain the correct types of values |
| 995 | * - local variables [registers] contain the correct types of values |
| 996 | * - methods are invoked with the appropriate arguments |
| 997 | * - fields are assigned using values of appropriate types |
| 998 | * - opcodes have the correct type values in operand registers |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 999 | * - there is never an uninitialized class instance in a local variable in code protected by an |
| 1000 | * exception handler (operand stack is okay, because the operand stack is discarded when an |
| 1001 | * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1002 | * register typing] |
| 1003 | * |
| 1004 | * v3 4.11.1.2 |
| 1005 | * - execution cannot fall off the end of the code |
| 1006 | * |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1007 | * (We also do many of the items described in the "static checks" sections, because it's easier to |
| 1008 | * do them here.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1009 | * |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1010 | * We need an array of RegType values, one per register, for every instruction. If the method uses |
| 1011 | * monitor-enter, we need extra data for every register, and a stack for every "interesting" |
| 1012 | * instruction. In theory this could become quite large -- up to several megabytes for a monster |
| 1013 | * function. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1014 | * |
| 1015 | * NOTE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1016 | * The spec forbids backward branches when there's an uninitialized reference in a register. The |
| 1017 | * idea is to prevent something like this: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1018 | * loop: |
| 1019 | * move r1, r0 |
| 1020 | * new-instance r0, MyClass |
| 1021 | * ... |
| 1022 | * if-eq rN, loop // once |
| 1023 | * initialize r0 |
| 1024 | * |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1025 | * This leaves us with two different instances, both allocated by the same instruction, but only |
| 1026 | * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around |
| 1027 | * it by preventing backward branches. We achieve identical results without restricting code |
| 1028 | * reordering by specifying that you can't execute the new-instance instruction if a register |
| 1029 | * contains an uninitialized instance created by that same instruction. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1030 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1031 | bool CodeFlowVerifyMethod(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1032 | |
| 1033 | /* |
| 1034 | * Perform verification for a single instruction. |
| 1035 | * |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1036 | * This requires fully decoding the instruction to determine the effect it has on registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1037 | * |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1038 | * Finds zero or more following instructions and sets the "changed" flag if execution at that |
| 1039 | * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target |
| 1040 | * addresses. Does not set or clear any other flags in "insn_flags_". |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1041 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1042 | bool CodeFlowVerifyInstruction(uint32_t* start_guess); |
| 1043 | |
| 1044 | // Perform verification of an aget instruction. The destination register's type will be set to |
| 1045 | // be that of component type of the array unless the array type is unknown, in which case a |
| 1046 | // bottom type inferred from the type of instruction is used. is_primitive is false for an |
| 1047 | // aget-object. |
| 1048 | void VerifyAGet(const Instruction::DecodedInstruction& insn, const RegType& insn_type, |
| 1049 | bool is_primitive); |
| 1050 | |
| 1051 | // Perform verification of an aput instruction. |
| 1052 | void VerifyAPut(const Instruction::DecodedInstruction& insn, const RegType& insn_type, |
| 1053 | bool is_primitive); |
| 1054 | |
| 1055 | // Lookup instance field and fail for resolution violations |
| 1056 | Field* GetInstanceField(const RegType& obj_type, int field_idx); |
| 1057 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1058 | // Lookup static field and fail for resolution violations |
| 1059 | Field* GetStaticField(int field_idx); |
| 1060 | |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame^] | 1061 | // Perform verification of an iget or sget instruction. |
| 1062 | void VerifyISGet(const Instruction::DecodedInstruction& insn, const RegType& insn_type, |
| 1063 | bool is_primitive, bool is_static); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1064 | |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame^] | 1065 | // Perform verification of an iput or sput instruction. |
| 1066 | void VerifyISPut(const Instruction::DecodedInstruction& insn, const RegType& insn_type, |
| 1067 | bool is_primitive, bool is_static); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1068 | |
| 1069 | // Verify that the arguments in a filled-new-array instruction are valid. |
| 1070 | // "res_class" is the class refered to by dec_insn->vB_. |
| 1071 | void VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn, Class* res_class, |
| 1072 | bool is_range); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1073 | |
| 1074 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1075 | * Resolves a class based on an index and performs access checks to ensure the referrer can |
| 1076 | * access the resolved class. |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1077 | * Exceptions caused by failures are cleared before returning. |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1078 | * Sets "*failure" on failure. |
| 1079 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1080 | Class* ResolveClassAndCheckAccess(uint32_t class_idx); |
| 1081 | |
| 1082 | /* |
| 1083 | * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler |
| 1084 | * address, determine the first common superclass of all exceptions that can land here. |
| 1085 | * Returns NULL if no matching exception handler can be found, or if the exception is not a |
| 1086 | * subclass of Throwable. |
| 1087 | */ |
| 1088 | Class* GetCaughtExceptionType(); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1089 | |
| 1090 | /* |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 1091 | * Resolves a method based on an index and performs access checks to ensure |
| 1092 | * the referrer can access the resolved method. |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 1093 | * Does not throw exceptions. |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 1094 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1095 | Method* ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1096 | |
| 1097 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1098 | * Verify the arguments to a method. We're executing in "method", making |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1099 | * a call to the method reference in vB. |
| 1100 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1101 | * If this is a "direct" invoke, we allow calls to <init>. For calls to |
| 1102 | * <init>, the first argument may be an uninitialized reference. Otherwise, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1103 | * calls to anything starting with '<' will be rejected, as will any |
| 1104 | * uninitialized reference arguments. |
| 1105 | * |
| 1106 | * For non-static method calls, this will verify that the method call is |
| 1107 | * appropriate for the "this" argument. |
| 1108 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1109 | * The method reference is in vBBBB. The "is_range" parameter determines |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1110 | * whether we use 0-4 "args" values or a range of registers defined by |
| 1111 | * vAA and vCCCC. |
| 1112 | * |
| 1113 | * Widening conversions on integers and references are allowed, but |
| 1114 | * narrowing conversions are not. |
| 1115 | * |
| 1116 | * Returns the resolved method on success, NULL on failure (with *failure |
| 1117 | * set appropriately). |
| 1118 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1119 | Method* VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn, |
| 1120 | MethodType method_type, bool is_range, bool is_super); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1121 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1122 | /* |
| 1123 | * Return the register type for the method. We can't just use the already-computed |
| 1124 | * DalvikJniReturnType, because if it's a reference type we need to do the class lookup. |
| 1125 | * Returned references are assumed to be initialized. Returns kRegTypeUnknown for "void". |
| 1126 | */ |
| 1127 | const RegType& GetMethodReturnType() { |
| 1128 | return reg_types_.FromClass(method_->GetReturnType()); |
| 1129 | } |
jeffhao | e0cfb6f | 2011-09-22 16:42:56 -0700 | [diff] [blame] | 1130 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1131 | /* |
| 1132 | * Verify that the target instruction is not "move-exception". It's important that the only way |
| 1133 | * to execute a move-exception is as the first instruction of an exception handler. |
| 1134 | * Returns "true" if all is well, "false" if the target instruction is move-exception. |
| 1135 | */ |
| 1136 | bool CheckMoveException(const uint16_t* insns, int insn_idx); |
| 1137 | |
| 1138 | /* |
| 1139 | * Replace an instruction with "throw-verification-error". This allows us to |
| 1140 | * defer error reporting until the code path is first used. |
| 1141 | */ |
| 1142 | void ReplaceFailingInstruction(); |
| 1143 | |
| 1144 | /* |
| 1145 | * Control can transfer to "next_insn". Merge the registers from merge_line into the table at |
| 1146 | * next_insn, and set the changed flag on the target address if any of the registers were changed. |
| 1147 | * Returns "false" if an error is encountered. |
| 1148 | */ |
| 1149 | bool UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line); |
| 1150 | |
| 1151 | /* |
| 1152 | * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of |
| 1153 | * verification). For type-precise determination we have all the data we need, so we just need to |
| 1154 | * encode it in some clever fashion. |
| 1155 | * Returns a pointer to a newly-allocated RegisterMap, or NULL on failure. |
| 1156 | */ |
| 1157 | ByteArray* GenerateGcMap(); |
| 1158 | |
| 1159 | // Verify that the GC map associated with method_ is well formed |
| 1160 | void VerifyGcMap(); |
| 1161 | |
| 1162 | // Compute sizes for GC map data |
| 1163 | void ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc); |
| 1164 | |
| 1165 | Class* JavaLangThrowable(); |
| 1166 | |
| 1167 | InsnFlags CurrentInsnFlags() { |
| 1168 | return insn_flags_[work_insn_idx_]; |
| 1169 | } |
| 1170 | |
| 1171 | RegTypeCache reg_types_; |
| 1172 | |
| 1173 | PcToRegisterLineTable reg_table_; |
| 1174 | |
| 1175 | // Storage for the register status we're currently working on. |
| 1176 | UniquePtr<RegisterLine> work_line_; |
| 1177 | |
| 1178 | // Lazily initialized reference to java.lang.Class<java.lang.Throwable> |
| 1179 | Class* java_lang_throwable_; |
| 1180 | |
| 1181 | // The address of the instruction we're currently working on, note that this is in 2 byte |
| 1182 | // quantities |
| 1183 | uint32_t work_insn_idx_; |
| 1184 | |
| 1185 | // Storage for the register status we're saving for later. |
| 1186 | UniquePtr<RegisterLine> saved_line_; |
| 1187 | |
| 1188 | Method* method_; // The method we're working on. |
| 1189 | const DexFile* dex_file_; // The dex file containing the method. |
| 1190 | const DexFile::CodeItem* code_item_; // The code item containing the code for the method. |
| 1191 | UniquePtr<InsnFlags[]> insn_flags_; // Instruction widths and flags, one entry per code unit. |
| 1192 | |
| 1193 | // The type of any error that occurs |
| 1194 | VerifyError failure_; |
| 1195 | |
| 1196 | // Failure message log |
| 1197 | std::ostringstream fail_messages_; |
| 1198 | // Info message log |
| 1199 | std::ostringstream info_messages_; |
| 1200 | |
| 1201 | // The number of occurrences of specific opcodes. |
| 1202 | size_t new_instance_count_; |
| 1203 | size_t monitor_enter_count_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1204 | }; |
| 1205 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1206 | // Lightweight wrapper for PC to reference bit maps. |
| 1207 | class PcToReferenceMap { |
| 1208 | public: |
| 1209 | PcToReferenceMap(Method* m) { |
| 1210 | data_ = down_cast<ByteArray*>(m->GetGcMap()); |
| 1211 | CHECK(data_ != NULL); |
| 1212 | // Check the size of the table agrees with the number of entries |
| 1213 | size_t data_size = data_->GetLength() - 4; |
| 1214 | DCHECK_EQ(EntryWidth() * NumEntries(), data_size); |
| 1215 | } |
| 1216 | |
| 1217 | // The number of entries in the table |
| 1218 | size_t NumEntries() const { |
| 1219 | return GetData()[2] | (GetData()[3] << 8); |
| 1220 | } |
| 1221 | |
| 1222 | // Get the PC at the given index |
| 1223 | uint16_t GetPC(size_t index) const { |
| 1224 | size_t entry_offset = index * EntryWidth(); |
| 1225 | if (PcWidth() == 1) { |
| 1226 | return Table()[entry_offset]; |
| 1227 | } else { |
| 1228 | return Table()[entry_offset] | (Table()[entry_offset + 1] << 8); |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | // Return address of bitmap encoding what are live references |
| 1233 | const uint8_t* GetBitMap(size_t index) const { |
| 1234 | size_t entry_offset = index * EntryWidth(); |
| 1235 | return &Table()[entry_offset + PcWidth()]; |
| 1236 | } |
| 1237 | |
| 1238 | // Find the bitmap associated with the given dex pc |
| 1239 | const uint8_t* FindBitMap(uint16_t dex_pc, bool error_if_not_present = true) const; |
| 1240 | |
| 1241 | // The number of bytes used to encode registers |
| 1242 | size_t RegWidth() const { |
| 1243 | return GetData()[1]; |
| 1244 | } |
| 1245 | |
| 1246 | private: |
| 1247 | // Table of num_entries * (dex pc, bitmap) |
| 1248 | const uint8_t* Table() const { |
| 1249 | return GetData() + 4; |
| 1250 | } |
| 1251 | |
| 1252 | // The format of the table of the PCs for the table |
| 1253 | RegisterMapFormat Format() const { |
| 1254 | return static_cast<RegisterMapFormat>(GetData()[0]); |
| 1255 | } |
| 1256 | |
| 1257 | // Number of bytes used to encode a dex pc |
| 1258 | size_t PcWidth() const { |
| 1259 | RegisterMapFormat format = Format(); |
| 1260 | switch (format) { |
| 1261 | case kRegMapFormatCompact8: |
| 1262 | return 1; |
| 1263 | case kRegMapFormatCompact16: |
| 1264 | return 2; |
| 1265 | default: |
| 1266 | LOG(FATAL) << "Invalid format " << static_cast<int>(format); |
| 1267 | return -1; |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | // The width of an entry in the table |
| 1272 | size_t EntryWidth() const { |
| 1273 | return PcWidth() + RegWidth(); |
| 1274 | } |
| 1275 | |
| 1276 | const uint8_t* GetData() const { |
| 1277 | return reinterpret_cast<uint8_t*>(data_->GetData()); |
| 1278 | } |
| 1279 | ByteArray* data_; // The header and table data |
| 1280 | }; |
| 1281 | |
| 1282 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1283 | } // namespace art |
| 1284 | |
| 1285 | #endif // ART_SRC_DEX_VERIFY_H_ |