Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "dex_verifier.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 5 | #include <iostream> |
| 6 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 7 | #include "class_linker.h" |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 8 | #include "compiler.h" |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 9 | #include "dex_cache.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 10 | #include "dex_file.h" |
| 11 | #include "dex_instruction.h" |
| 12 | #include "dex_instruction_visitor.h" |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 13 | #include "dex_verifier.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 14 | #include "intern_table.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 15 | #include "leb128.h" |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 16 | #include "logging.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 17 | #include "object_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 18 | #include "runtime.h" |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 19 | #include "stringpiece.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 20 | |
| 21 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 22 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 23 | |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 24 | static const bool gDebugVerify = false; |
| 25 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 26 | std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) { |
| 27 | return os << (int)rhs; |
| 28 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 29 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 30 | static const char* type_strings[] = { |
| 31 | "Unknown", |
| 32 | "Conflict", |
| 33 | "Boolean", |
| 34 | "Byte", |
| 35 | "Short", |
| 36 | "Char", |
| 37 | "Integer", |
| 38 | "Float", |
| 39 | "Long (Low Half)", |
| 40 | "Long (High Half)", |
| 41 | "Double (Low Half)", |
| 42 | "Double (High Half)", |
| 43 | "64-bit Constant (Low Half)", |
| 44 | "64-bit Constant (High Half)", |
| 45 | "32-bit Constant", |
| 46 | "Unresolved Reference", |
| 47 | "Uninitialized Reference", |
| 48 | "Uninitialized This Reference", |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 49 | "Unresolved And Uninitialized Reference", |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 50 | "Reference", |
| 51 | }; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 52 | |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 53 | std::string RegType::Dump() const { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 54 | DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference); |
| 55 | std::string result; |
| 56 | if (IsConstant()) { |
| 57 | uint32_t val = ConstantValue(); |
| 58 | if (val == 0) { |
| 59 | result = "Zero"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 60 | } else { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 61 | if(IsConstantShort()) { |
| 62 | result = StringPrintf("32-bit Constant: %d", val); |
| 63 | } else { |
| 64 | result = StringPrintf("32-bit Constant: 0x%x", val); |
| 65 | } |
| 66 | } |
| 67 | } else { |
| 68 | result = type_strings[type_]; |
| 69 | if (IsReferenceTypes()) { |
| 70 | result += ": "; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 71 | if (IsUnresolvedTypes()) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 72 | result += PrettyDescriptor(GetDescriptor()); |
| 73 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 74 | result += PrettyDescriptor(GetClass()); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 75 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 78 | return result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | const RegType& RegType::HighHalf(RegTypeCache* cache) const { |
| 82 | CHECK(IsLowHalf()); |
| 83 | if (type_ == kRegTypeLongLo) { |
| 84 | return cache->FromType(kRegTypeLongHi); |
| 85 | } else if (type_ == kRegTypeDoubleLo) { |
| 86 | return cache->FromType(kRegTypeDoubleHi); |
| 87 | } else { |
| 88 | return cache->FromType(kRegTypeConstHi); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is |
| 94 | * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of |
| 95 | * 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 |
| 96 | * is the deepest (lowest upper bound) parent of S and T). |
| 97 | * |
| 98 | * This operation applies for regular classes and arrays, however, for interface types there needn't |
| 99 | * be a partial ordering on the types. We could solve the problem of a lack of a partial order by |
| 100 | * introducing sets of types, however, the only operation permissible on an interface is |
| 101 | * invoke-interface. In the tradition of Java verifiers we defer the verification of interface |
| 102 | * types until an invoke-interface call on the interface typed reference at runtime and allow |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 103 | * the perversion of any Object being assignable to an interface type (note, however, that we don't |
| 104 | * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type |
| 105 | * safe; further the Join on a Object cannot result in a sub-class by definition). |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 106 | */ |
| 107 | Class* RegType::ClassJoin(Class* s, Class* t) { |
| 108 | DCHECK(!s->IsPrimitive()) << PrettyClass(s); |
| 109 | DCHECK(!t->IsPrimitive()) << PrettyClass(t); |
| 110 | if (s == t) { |
| 111 | return s; |
| 112 | } else if (s->IsAssignableFrom(t)) { |
| 113 | return s; |
| 114 | } else if (t->IsAssignableFrom(s)) { |
| 115 | return t; |
| 116 | } else if (s->IsArrayClass() && t->IsArrayClass()) { |
| 117 | Class* s_ct = s->GetComponentType(); |
| 118 | Class* t_ct = t->GetComponentType(); |
| 119 | if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) { |
| 120 | // Given the types aren't the same, if either array is of primitive types then the only |
| 121 | // common parent is java.lang.Object |
| 122 | Class* result = s->GetSuperClass(); // short-cut to java.lang.Object |
| 123 | DCHECK(result->IsObjectClass()); |
| 124 | return result; |
| 125 | } |
| 126 | Class* common_elem = ClassJoin(s_ct, t_ct); |
| 127 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 128 | const ClassLoader* class_loader = s->GetClassLoader(); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 129 | std::string descriptor("["); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 130 | descriptor += ClassHelper(common_elem).GetDescriptor(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 131 | Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader); |
| 132 | DCHECK(array_class != NULL); |
| 133 | return array_class; |
| 134 | } else { |
| 135 | size_t s_depth = s->Depth(); |
| 136 | size_t t_depth = t->Depth(); |
| 137 | // Get s and t to the same depth in the hierarchy |
| 138 | if (s_depth > t_depth) { |
| 139 | while (s_depth > t_depth) { |
| 140 | s = s->GetSuperClass(); |
| 141 | s_depth--; |
| 142 | } |
| 143 | } else { |
| 144 | while (t_depth > s_depth) { |
| 145 | t = t->GetSuperClass(); |
| 146 | t_depth--; |
| 147 | } |
| 148 | } |
| 149 | // Go up the hierarchy until we get to the common parent |
| 150 | while (s != t) { |
| 151 | s = s->GetSuperClass(); |
| 152 | t = t->GetSuperClass(); |
| 153 | } |
| 154 | return s; |
| 155 | } |
| 156 | } |
| 157 | |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 158 | bool RegType::IsAssignableFrom(const RegType& src) const { |
| 159 | if (Equals(src)) { |
| 160 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 161 | } else { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 162 | switch (GetType()) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 163 | case RegType::kRegTypeBoolean: return src.IsBooleanTypes(); |
| 164 | case RegType::kRegTypeByte: return src.IsByteTypes(); |
| 165 | case RegType::kRegTypeShort: return src.IsShortTypes(); |
| 166 | case RegType::kRegTypeChar: return src.IsCharTypes(); |
| 167 | case RegType::kRegTypeInteger: return src.IsIntegralTypes(); |
| 168 | case RegType::kRegTypeFloat: return src.IsFloatTypes(); |
| 169 | case RegType::kRegTypeLongLo: return src.IsLongTypes(); |
| 170 | case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes(); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 171 | default: |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 172 | if (!IsReferenceTypes()) { |
| 173 | LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'"; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 174 | } |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 175 | if (src.IsZero()) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 176 | return true; // all reference types can be assigned null |
| 177 | } else if (!src.IsReferenceTypes()) { |
| 178 | return false; // expect src to be a reference type |
| 179 | } else if (IsJavaLangObject()) { |
| 180 | return true; // all reference types can be assigned to Object |
| 181 | } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) { |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 182 | return true; // We allow assignment to any interface, see comment in ClassJoin |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 183 | } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() && |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 184 | GetClass()->IsAssignableFrom(src.GetClass())) { |
| 185 | // We're assignable from the Class point-of-view |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 186 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 187 | } else { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 188 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 189 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 194 | static const RegType& SelectNonConstant(const RegType& a, const RegType& b) { |
| 195 | return a.IsConstant() ? b : a; |
| 196 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 197 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 198 | const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const { |
| 199 | DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 200 | if (IsUnknown() && incoming_type.IsUnknown()) { |
| 201 | return *this; // Unknown MERGE Unknown => Unknown |
| 202 | } else if (IsConflict()) { |
| 203 | return *this; // Conflict MERGE * => Conflict |
| 204 | } else if (incoming_type.IsConflict()) { |
| 205 | return incoming_type; // * MERGE Conflict => Conflict |
| 206 | } else if (IsUnknown() || incoming_type.IsUnknown()) { |
| 207 | return reg_types->Conflict(); // Unknown MERGE * => Conflict |
| 208 | } else if(IsConstant() && incoming_type.IsConstant()) { |
| 209 | int32_t val1 = ConstantValue(); |
| 210 | int32_t val2 = incoming_type.ConstantValue(); |
| 211 | if (val1 >= 0 && val2 >= 0) { |
| 212 | // +ve1 MERGE +ve2 => MAX(+ve1, +ve2) |
| 213 | if (val1 >= val2) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 214 | return *this; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 215 | } else { |
| 216 | return incoming_type; |
| 217 | } |
| 218 | } else if (val1 < 0 && val2 < 0) { |
| 219 | // -ve1 MERGE -ve2 => MIN(-ve1, -ve2) |
| 220 | if (val1 <= val2) { |
| 221 | return *this; |
| 222 | } else { |
| 223 | return incoming_type; |
| 224 | } |
| 225 | } else { |
| 226 | // Values are +ve and -ve, choose smallest signed type in which they both fit |
| 227 | if (IsConstantByte()) { |
| 228 | if (incoming_type.IsConstantByte()) { |
| 229 | return reg_types->ByteConstant(); |
| 230 | } else if (incoming_type.IsConstantShort()) { |
| 231 | return reg_types->ShortConstant(); |
| 232 | } else { |
| 233 | return reg_types->IntConstant(); |
| 234 | } |
| 235 | } else if (IsConstantShort()) { |
Ian Rogers | 1592bc7 | 2011-10-27 20:08:53 -0700 | [diff] [blame] | 236 | if (incoming_type.IsConstantShort()) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 237 | return reg_types->ShortConstant(); |
| 238 | } else { |
| 239 | return reg_types->IntConstant(); |
| 240 | } |
| 241 | } else { |
| 242 | return reg_types->IntConstant(); |
| 243 | } |
| 244 | } |
| 245 | } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) { |
| 246 | if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) { |
| 247 | return reg_types->Boolean(); // boolean MERGE boolean => boolean |
| 248 | } |
| 249 | if (IsByteTypes() && incoming_type.IsByteTypes()) { |
| 250 | return reg_types->Byte(); // byte MERGE byte => byte |
| 251 | } |
| 252 | if (IsShortTypes() && incoming_type.IsShortTypes()) { |
| 253 | return reg_types->Short(); // short MERGE short => short |
| 254 | } |
| 255 | if (IsCharTypes() && incoming_type.IsCharTypes()) { |
| 256 | return reg_types->Char(); // char MERGE char => char |
| 257 | } |
| 258 | return reg_types->Integer(); // int MERGE * => int |
| 259 | } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) || |
| 260 | (IsLongTypes() && incoming_type.IsLongTypes()) || |
| 261 | (IsLongHighTypes() && incoming_type.IsLongHighTypes()) || |
| 262 | (IsDoubleTypes() && incoming_type.IsDoubleTypes()) || |
| 263 | (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) { |
| 264 | // check constant case was handled prior to entry |
| 265 | DCHECK(!IsConstant() || !incoming_type.IsConstant()); |
| 266 | // float/long/double MERGE float/long/double_constant => float/long/double |
| 267 | return SelectNonConstant(*this, incoming_type); |
| 268 | } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 269 | if (IsZero() || incoming_type.IsZero()) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 270 | return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 271 | } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) { |
| 272 | return reg_types->JavaLangObject(); // Object MERGE ref => Object |
| 273 | } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() || |
| 274 | IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) { |
| 275 | // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already |
| 276 | // checked these so => Conflict |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 277 | return reg_types->Conflict(); |
| 278 | } else { // Two reference types, compute Join |
| 279 | Class* c1 = GetClass(); |
| 280 | Class* c2 = incoming_type.GetClass(); |
| 281 | DCHECK(c1 != NULL && !c1->IsPrimitive()); |
| 282 | DCHECK(c2 != NULL && !c2->IsPrimitive()); |
| 283 | Class* join_class = ClassJoin(c1, c2); |
| 284 | if (c1 == join_class) { |
| 285 | return *this; |
| 286 | } else if (c2 == join_class) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 287 | return incoming_type; |
| 288 | } else { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 289 | return reg_types->FromClass(join_class); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 290 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 291 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 292 | } else { |
| 293 | return reg_types->Conflict(); // Unexpected types => Conflict |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 297 | static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 298 | switch (prim_type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 299 | case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean; |
| 300 | case Primitive::kPrimByte: return RegType::kRegTypeByte; |
| 301 | case Primitive::kPrimShort: return RegType::kRegTypeShort; |
| 302 | case Primitive::kPrimChar: return RegType::kRegTypeChar; |
| 303 | case Primitive::kPrimInt: return RegType::kRegTypeInteger; |
| 304 | case Primitive::kPrimLong: return RegType::kRegTypeLongLo; |
| 305 | case Primitive::kPrimFloat: return RegType::kRegTypeFloat; |
| 306 | case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo; |
| 307 | case Primitive::kPrimVoid: |
| 308 | default: return RegType::kRegTypeUnknown; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) { |
| 313 | if (descriptor.length() == 1) { |
| 314 | switch (descriptor[0]) { |
| 315 | case 'Z': return RegType::kRegTypeBoolean; |
| 316 | case 'B': return RegType::kRegTypeByte; |
| 317 | case 'S': return RegType::kRegTypeShort; |
| 318 | case 'C': return RegType::kRegTypeChar; |
| 319 | case 'I': return RegType::kRegTypeInteger; |
| 320 | case 'J': return RegType::kRegTypeLongLo; |
| 321 | case 'F': return RegType::kRegTypeFloat; |
| 322 | case 'D': return RegType::kRegTypeDoubleLo; |
| 323 | case 'V': |
| 324 | default: return RegType::kRegTypeUnknown; |
| 325 | } |
| 326 | } else if(descriptor[0] == 'L' || descriptor[0] == '[') { |
| 327 | return RegType::kRegTypeReference; |
| 328 | } else { |
| 329 | return RegType::kRegTypeUnknown; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | std::ostream& operator<<(std::ostream& os, const RegType& rhs) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 334 | os << rhs.Dump(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 335 | return os; |
| 336 | } |
| 337 | |
| 338 | const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader, |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 339 | const char* descriptor) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 340 | return From(RegTypeFromDescriptor(descriptor), loader, descriptor); |
| 341 | } |
| 342 | |
| 343 | const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader, |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 344 | const char* descriptor) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 345 | if (type <= RegType::kRegTypeLastFixedLocation) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 346 | // entries should be sized greater than primitive types |
| 347 | DCHECK_GT(entries_.size(), static_cast<size_t>(type)); |
| 348 | RegType* entry = entries_[type]; |
| 349 | if (entry == NULL) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 350 | Class* klass = NULL; |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 351 | if (strlen(descriptor) != 0) { |
| 352 | klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 353 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 354 | entry = new RegType(type, klass, 0, type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 355 | entries_[type] = entry; |
| 356 | } |
| 357 | return *entry; |
| 358 | } else { |
| 359 | DCHECK (type == RegType::kRegTypeReference); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 360 | ClassHelper kh; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 361 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 362 | RegType* cur_entry = entries_[i]; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 363 | // check resolved and unresolved references, ignore uninitialized references |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 364 | if (cur_entry->IsReference()) { |
| 365 | kh.ChangeClass(cur_entry->GetClass()); |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 366 | if (strcmp(descriptor, kh.GetDescriptor()) == 0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 367 | return *cur_entry; |
| 368 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 369 | } else if (cur_entry->IsUnresolvedReference() && |
| 370 | cur_entry->GetDescriptor()->Equals(descriptor)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 371 | return *cur_entry; |
| 372 | } |
| 373 | } |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 374 | Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 375 | if (klass != NULL) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 376 | // Able to resolve so create resolved register type |
| 377 | RegType* entry = new RegType(type, klass, 0, entries_.size()); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 378 | entries_.push_back(entry); |
| 379 | return *entry; |
| 380 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 381 | // TODO: we assume unresolved, but we may be able to do better by validating whether the |
| 382 | // descriptor string is valid |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 383 | // Unable to resolve so create unresolved register type |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 384 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 385 | Thread::Current()->ClearException(); |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 386 | if (IsValidDescriptor(descriptor)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 387 | String* string_descriptor = |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 388 | Runtime::Current()->GetInternTable()->InternStrong(descriptor); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 389 | RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0, |
| 390 | entries_.size()); |
| 391 | entries_.push_back(entry); |
| 392 | return *entry; |
| 393 | } else { |
| 394 | // The descriptor is broken return the unknown type as there's nothing sensible that |
| 395 | // could be done at runtime |
| 396 | return Unknown(); |
| 397 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 398 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | const RegType& RegTypeCache::FromClass(Class* klass) { |
| 403 | if (klass->IsPrimitive()) { |
| 404 | RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType()); |
| 405 | // entries should be sized greater than primitive types |
| 406 | DCHECK_GT(entries_.size(), static_cast<size_t>(type)); |
| 407 | RegType* entry = entries_[type]; |
| 408 | if (entry == NULL) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 409 | entry = new RegType(type, klass, 0, type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 410 | entries_[type] = entry; |
| 411 | } |
| 412 | return *entry; |
| 413 | } else { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 414 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 415 | RegType* cur_entry = entries_[i]; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 416 | if (cur_entry->IsReference() && cur_entry->GetClass() == klass) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 417 | return *cur_entry; |
| 418 | } |
| 419 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 420 | RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 421 | entries_.push_back(entry); |
| 422 | return *entry; |
| 423 | } |
| 424 | } |
| 425 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 426 | const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) { |
| 427 | RegType* entry; |
| 428 | if (type.IsUnresolvedTypes()) { |
| 429 | String* descriptor = type.GetDescriptor(); |
| 430 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 431 | RegType* cur_entry = entries_[i]; |
| 432 | if (cur_entry->IsUnresolvedAndUninitializedReference() && |
| 433 | cur_entry->GetAllocationPc() == allocation_pc && |
| 434 | cur_entry->GetDescriptor() == descriptor) { |
| 435 | return *cur_entry; |
| 436 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 437 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 438 | entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference, |
| 439 | descriptor, allocation_pc, entries_.size()); |
| 440 | } else { |
| 441 | Class* klass = type.GetClass(); |
| 442 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 443 | RegType* cur_entry = entries_[i]; |
| 444 | if (cur_entry->IsUninitializedReference() && |
| 445 | cur_entry->GetAllocationPc() == allocation_pc && |
| 446 | cur_entry->GetClass() == klass) { |
| 447 | return *cur_entry; |
| 448 | } |
| 449 | } |
| 450 | entry = new RegType(RegType::kRegTypeUninitializedReference, |
| 451 | klass, allocation_pc, entries_.size()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 452 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 453 | entries_.push_back(entry); |
| 454 | return *entry; |
| 455 | } |
| 456 | |
| 457 | const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) { |
| 458 | RegType* entry; |
| 459 | if (uninit_type.IsUnresolvedTypes()) { |
| 460 | String* descriptor = uninit_type.GetDescriptor(); |
| 461 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 462 | RegType* cur_entry = entries_[i]; |
| 463 | if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) { |
| 464 | return *cur_entry; |
| 465 | } |
| 466 | } |
| 467 | entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size()); |
| 468 | } else { |
| 469 | Class* klass = uninit_type.GetClass(); |
| 470 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 471 | RegType* cur_entry = entries_[i]; |
| 472 | if (cur_entry->IsReference() && cur_entry->GetClass() == klass) { |
| 473 | return *cur_entry; |
| 474 | } |
| 475 | } |
| 476 | entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size()); |
| 477 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 478 | entries_.push_back(entry); |
| 479 | return *entry; |
| 480 | } |
| 481 | |
| 482 | const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 483 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 484 | RegType* cur_entry = entries_[i]; |
| 485 | if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) { |
| 486 | return *cur_entry; |
| 487 | } |
| 488 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 489 | RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 490 | entries_.size()); |
| 491 | entries_.push_back(entry); |
| 492 | return *entry; |
| 493 | } |
| 494 | |
| 495 | const RegType& RegTypeCache::FromType(RegType::Type type) { |
| 496 | CHECK(type < RegType::kRegTypeReference); |
| 497 | switch (type) { |
| 498 | case RegType::kRegTypeBoolean: return From(type, NULL, "Z"); |
| 499 | case RegType::kRegTypeByte: return From(type, NULL, "B"); |
| 500 | case RegType::kRegTypeShort: return From(type, NULL, "S"); |
| 501 | case RegType::kRegTypeChar: return From(type, NULL, "C"); |
| 502 | case RegType::kRegTypeInteger: return From(type, NULL, "I"); |
| 503 | case RegType::kRegTypeFloat: return From(type, NULL, "F"); |
| 504 | case RegType::kRegTypeLongLo: |
| 505 | case RegType::kRegTypeLongHi: return From(type, NULL, "J"); |
| 506 | case RegType::kRegTypeDoubleLo: |
| 507 | case RegType::kRegTypeDoubleHi: return From(type, NULL, "D"); |
| 508 | default: return From(type, NULL, ""); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | const RegType& RegTypeCache::FromCat1Const(int32_t value) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 513 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 514 | RegType* cur_entry = entries_[i]; |
| 515 | if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) { |
| 516 | return *cur_entry; |
| 517 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 518 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 519 | RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size()); |
| 520 | entries_.push_back(entry); |
| 521 | return *entry; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 524 | const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) { |
| 525 | CHECK(array.IsArrayClass()); |
| 526 | if (array.IsUnresolvedTypes()) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 527 | std::string descriptor(array.GetDescriptor()->ToModifiedUtf8()); |
| 528 | std::string component(descriptor.substr(1, descriptor.size() - 1)); |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 529 | return FromDescriptor(loader, component.c_str()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 530 | } else { |
| 531 | return FromClass(array.GetClass()->GetComponentType()); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 536 | bool RegisterLine::CheckConstructorReturn() const { |
| 537 | for (size_t i = 0; i < num_regs_; i++) { |
| 538 | if (GetRegisterType(i).IsUninitializedThisReference()) { |
| 539 | verifier_->Fail(VERIFY_ERROR_GENERIC) |
| 540 | << "Constructor returning without calling superclass constructor"; |
| 541 | return false; |
| 542 | } |
| 543 | } |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) { |
| 548 | DCHECK(vdst < num_regs_); |
| 549 | if (new_type.IsLowHalf()) { |
| 550 | line_[vdst] = new_type.GetId(); |
| 551 | line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(); |
| 552 | } else if (new_type.IsHighHalf()) { |
| 553 | /* should never set these explicitly */ |
| 554 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type"; |
| 555 | } else if (new_type.IsConflict()) { // should only be set during a merge |
| 556 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type; |
| 557 | } else { |
| 558 | line_[vdst] = new_type.GetId(); |
| 559 | } |
| 560 | // Clear the monitor entry bits for this register. |
| 561 | ClearAllRegToLockDepths(vdst); |
| 562 | } |
| 563 | |
| 564 | void RegisterLine::SetResultTypeToUnknown() { |
| 565 | uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 566 | result_[0] = unknown_id; |
| 567 | result_[1] = unknown_id; |
| 568 | } |
| 569 | |
| 570 | void RegisterLine::SetResultRegisterType(const RegType& new_type) { |
| 571 | result_[0] = new_type.GetId(); |
| 572 | if(new_type.IsLowHalf()) { |
| 573 | DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1); |
| 574 | result_[1] = new_type.GetId() + 1; |
| 575 | } else { |
| 576 | result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const { |
| 581 | // The register index was validated during the static pass, so we don't need to check it here. |
| 582 | DCHECK_LT(vsrc, num_regs_); |
| 583 | return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]); |
| 584 | } |
| 585 | |
| 586 | const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) { |
| 587 | if (dec_insn.vA_ < 1) { |
| 588 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'"; |
| 589 | return verifier_->GetRegTypeCache()->Unknown(); |
| 590 | } |
| 591 | /* get the element type of the array held in vsrc */ |
| 592 | const RegType& this_type = GetRegisterType(dec_insn.vC_); |
| 593 | if (!this_type.IsReferenceTypes()) { |
| 594 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v" |
| 595 | << dec_insn.vC_ << " (type=" << this_type << ")"; |
| 596 | return verifier_->GetRegTypeCache()->Unknown(); |
| 597 | } |
| 598 | return this_type; |
| 599 | } |
| 600 | |
| 601 | Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const { |
| 602 | /* get the element type of the array held in vsrc */ |
| 603 | const RegType& type = GetRegisterType(vsrc); |
| 604 | /* if "always zero", we allow it to fail at runtime */ |
| 605 | if (type.IsZero()) { |
| 606 | return NULL; |
| 607 | } else if (!type.IsReferenceTypes()) { |
| 608 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc |
| 609 | << " (type=" << type << ")"; |
| 610 | return NULL; |
| 611 | } else if (type.IsUninitializedReference()) { |
| 612 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference"; |
| 613 | return NULL; |
| 614 | } else { |
| 615 | return type.GetClass(); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) { |
| 620 | // Verify the src register type against the check type refining the type of the register |
| 621 | const RegType& src_type = GetRegisterType(vsrc); |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 622 | if (!check_type.IsAssignableFrom(src_type)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 623 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type |
| 624 | << " but expected " << check_type; |
| 625 | return false; |
| 626 | } |
| 627 | // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less |
| 628 | // precise than the subtype in vsrc so leave it for reference types. For primitive types |
| 629 | // if they are a defined type then they are as precise as we can get, however, for constant |
| 630 | // types we may wish to refine them. Unfortunately constant propagation has rendered this useless. |
| 631 | return true; |
| 632 | } |
| 633 | |
| 634 | void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 635 | DCHECK(uninit_type.IsUninitializedTypes()); |
| 636 | const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type); |
| 637 | size_t changed = 0; |
| 638 | for (size_t i = 0; i < num_regs_; i++) { |
| 639 | if (GetRegisterType(i).Equals(uninit_type)) { |
| 640 | line_[i] = init_type.GetId(); |
| 641 | changed++; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 642 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 643 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 644 | DCHECK_GT(changed, 0u); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) { |
| 648 | for (size_t i = 0; i < num_regs_; i++) { |
| 649 | if (GetRegisterType(i).Equals(uninit_type)) { |
| 650 | line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId(); |
| 651 | ClearAllRegToLockDepths(i); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) { |
| 657 | DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef); |
| 658 | const RegType& type = GetRegisterType(vsrc); |
| 659 | SetRegisterType(vdst, type); |
| 660 | if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) || |
| 661 | (cat == kTypeCategoryRef && !type.IsReferenceTypes())) { |
| 662 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type |
| 663 | << " cat=" << static_cast<int>(cat); |
| 664 | } else if (cat == kTypeCategoryRef) { |
| 665 | CopyRegToLockDepth(vdst, vsrc); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) { |
| 670 | const RegType& type_l = GetRegisterType(vsrc); |
| 671 | const RegType& type_h = GetRegisterType(vsrc + 1); |
| 672 | |
| 673 | if (!type_l.CheckWidePair(type_h)) { |
| 674 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc |
| 675 | << " type=" << type_l << "/" << type_h; |
| 676 | } else { |
| 677 | SetRegisterType(vdst, type_l); // implicitly sets the second half |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) { |
| 682 | const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]); |
| 683 | if ((!is_reference && !type.IsCategory1Types()) || |
| 684 | (is_reference && !type.IsReferenceTypes())) { |
| 685 | verifier_->Fail(VERIFY_ERROR_GENERIC) |
| 686 | << "copyRes1 v" << vdst << "<- result0" << " type=" << type; |
| 687 | } else { |
| 688 | DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown()); |
| 689 | SetRegisterType(vdst, type); |
| 690 | result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Implement "move-result-wide". Copy the category-2 value from the result |
| 696 | * register to another register, and reset the result register. |
| 697 | */ |
| 698 | void RegisterLine::CopyResultRegister2(uint32_t vdst) { |
| 699 | const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]); |
| 700 | const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]); |
| 701 | if (!type_l.IsCategory2Types()) { |
| 702 | verifier_->Fail(VERIFY_ERROR_GENERIC) |
| 703 | << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l; |
| 704 | } else { |
| 705 | DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case |
| 706 | SetRegisterType(vdst, type_l); // also sets the high |
| 707 | result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 708 | result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn, |
| 713 | const RegType& dst_type, const RegType& src_type) { |
| 714 | if (VerifyRegisterType(dec_insn.vB_, src_type)) { |
| 715 | SetRegisterType(dec_insn.vA_, dst_type); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn, |
| 720 | const RegType& dst_type, |
| 721 | const RegType& src_type1, const RegType& src_type2, |
| 722 | bool check_boolean_op) { |
| 723 | if (VerifyRegisterType(dec_insn.vB_, src_type1) && |
| 724 | VerifyRegisterType(dec_insn.vC_, src_type2)) { |
| 725 | if (check_boolean_op) { |
| 726 | DCHECK(dst_type.IsInteger()); |
| 727 | if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() && |
| 728 | GetRegisterType(dec_insn.vC_).IsBooleanTypes()) { |
| 729 | SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean()); |
| 730 | return; |
| 731 | } |
| 732 | } |
| 733 | SetRegisterType(dec_insn.vA_, dst_type); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn, |
| 738 | const RegType& dst_type, const RegType& src_type1, |
| 739 | const RegType& src_type2, bool check_boolean_op) { |
| 740 | if (VerifyRegisterType(dec_insn.vA_, src_type1) && |
| 741 | VerifyRegisterType(dec_insn.vB_, src_type2)) { |
| 742 | if (check_boolean_op) { |
| 743 | DCHECK(dst_type.IsInteger()); |
| 744 | if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() && |
| 745 | GetRegisterType(dec_insn.vB_).IsBooleanTypes()) { |
| 746 | SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean()); |
| 747 | return; |
| 748 | } |
| 749 | } |
| 750 | SetRegisterType(dec_insn.vA_, dst_type); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn, |
| 755 | const RegType& dst_type, const RegType& src_type, |
| 756 | bool check_boolean_op) { |
| 757 | if (VerifyRegisterType(dec_insn.vB_, src_type)) { |
| 758 | if (check_boolean_op) { |
| 759 | DCHECK(dst_type.IsInteger()); |
| 760 | /* check vB with the call, then check the constant manually */ |
| 761 | if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() && |
| 762 | (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) { |
| 763 | SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean()); |
| 764 | return; |
| 765 | } |
| 766 | } |
| 767 | SetRegisterType(dec_insn.vA_, dst_type); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) { |
| 772 | const RegType& reg_type = GetRegisterType(reg_idx); |
| 773 | if (!reg_type.IsReferenceTypes()) { |
| 774 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")"; |
Elliott Hughes | fbef946 | 2011-12-14 14:24:40 -0800 | [diff] [blame] | 775 | } else if (monitors_.size() >= 32) { |
| 776 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter stack overflow: " << monitors_.size(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 777 | } else { |
| 778 | SetRegToLockDepth(reg_idx, monitors_.size()); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 779 | monitors_.push_back(insn_idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
| 783 | void RegisterLine::PopMonitor(uint32_t reg_idx) { |
| 784 | const RegType& reg_type = GetRegisterType(reg_idx); |
| 785 | if (!reg_type.IsReferenceTypes()) { |
| 786 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")"; |
| 787 | } else if (monitors_.empty()) { |
| 788 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow"; |
| 789 | } else { |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 790 | monitors_.pop_back(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 791 | if(!IsSetLockDepth(reg_idx, monitors_.size())) { |
| 792 | // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before |
| 793 | // format "036" the constant collector may create unlocks on the same object but referenced |
| 794 | // via different registers. |
| 795 | ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC) |
| 796 | : verifier_->LogVerifyInfo()) |
| 797 | << "monitor-exit not unlocking the top of the monitor stack"; |
| 798 | } else { |
| 799 | // Record the register was unlocked |
| 800 | ClearRegToLockDepth(reg_idx, monitors_.size()); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | bool RegisterLine::VerifyMonitorStackEmpty() { |
| 806 | if (MonitorStackDepth() != 0) { |
| 807 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack"; |
| 808 | return false; |
| 809 | } else { |
| 810 | return true; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) { |
| 815 | bool changed = false; |
| 816 | for (size_t idx = 0; idx < num_regs_; idx++) { |
| 817 | if (line_[idx] != incoming_line->line_[idx]) { |
| 818 | const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx); |
| 819 | const RegType& cur_type = GetRegisterType(idx); |
| 820 | const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache()); |
| 821 | changed = changed || !cur_type.Equals(new_type); |
| 822 | line_[idx] = new_type.GetId(); |
| 823 | } |
| 824 | } |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 825 | if(monitors_.size() != incoming_line->monitors_.size()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 826 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth=" |
| 827 | << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")"; |
| 828 | } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) { |
| 829 | for (uint32_t idx = 0; idx < num_regs_; idx++) { |
| 830 | size_t depths = reg_to_lock_depths_.count(idx); |
| 831 | size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx); |
| 832 | if (depths != incoming_depths) { |
| 833 | if (depths == 0 || incoming_depths == 0) { |
| 834 | reg_to_lock_depths_.erase(idx); |
| 835 | } else { |
| 836 | verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx |
| 837 | << ": " << depths << " != " << incoming_depths; |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | return changed; |
| 844 | } |
| 845 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 846 | void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 847 | for (size_t i = 0; i < num_regs_; i += 8) { |
| 848 | uint8_t val = 0; |
| 849 | for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) { |
| 850 | // Note: we write 1 for a Reference but not for Null |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 851 | if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 852 | val |= 1 << j; |
| 853 | } |
| 854 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 855 | if ((i / 8) >= max_bytes) { |
| 856 | DCHECK_EQ(0, val); |
| 857 | continue; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 858 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 859 | DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val); |
| 860 | data.push_back(val); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
| 864 | std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 865 | os << rhs.Dump(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 866 | return os; |
| 867 | } |
| 868 | |
| 869 | |
| 870 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags, |
| 871 | uint32_t insns_size, uint16_t registers_size, |
| 872 | DexVerifier* verifier) { |
| 873 | DCHECK_GT(insns_size, 0U); |
| 874 | |
| 875 | for (uint32_t i = 0; i < insns_size; i++) { |
| 876 | bool interesting = false; |
| 877 | switch (mode) { |
| 878 | case kTrackRegsAll: |
| 879 | interesting = flags[i].IsOpcode(); |
| 880 | break; |
| 881 | case kTrackRegsGcPoints: |
| 882 | interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget(); |
| 883 | break; |
| 884 | case kTrackRegsBranches: |
| 885 | interesting = flags[i].IsBranchTarget(); |
| 886 | break; |
| 887 | default: |
| 888 | break; |
| 889 | } |
| 890 | if (interesting) { |
| 891 | pc_to_register_line_[i] = new RegisterLine(registers_size, verifier); |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | bool DexVerifier::VerifyClass(const Class* klass) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 897 | if (klass->IsVerified()) { |
| 898 | return true; |
| 899 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 900 | Class* super = klass->GetSuperClass(); |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 901 | if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 902 | LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class"; |
| 903 | return false; |
| 904 | } |
| 905 | if (super != NULL) { |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 906 | // Acquire lock to prevent races on verifying the super class |
| 907 | ObjectLock lock(super); |
| 908 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 909 | if (!super->IsVerified() && !super->IsErroneous()) { |
| 910 | Runtime::Current()->GetClassLinker()->VerifyClass(super); |
| 911 | } |
| 912 | if (!super->IsVerified()) { |
| 913 | LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) |
| 914 | << " that attempts to sub-class corrupt class " << PrettyClass(super); |
| 915 | return false; |
| 916 | } else if (super->IsFinal()) { |
| 917 | LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) |
| 918 | << " that attempts to sub-class final class " << PrettyClass(super); |
| 919 | return false; |
| 920 | } |
| 921 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 922 | for (size_t i = 0; i < klass->NumDirectMethods(); ++i) { |
| 923 | Method* method = klass->GetDirectMethod(i); |
| 924 | if (!VerifyMethod(method)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 925 | LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method " |
| 926 | << PrettyMethod(method, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 927 | return false; |
| 928 | } |
| 929 | } |
| 930 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
| 931 | Method* method = klass->GetVirtualMethod(i); |
| 932 | if (!VerifyMethod(method)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 933 | LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method " |
| 934 | << PrettyMethod(method, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 935 | return false; |
| 936 | } |
| 937 | } |
| 938 | return true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 939 | } |
| 940 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 941 | bool DexVerifier::VerifyMethod(Method* method) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 942 | DexVerifier verifier(method); |
| 943 | bool success = verifier.Verify(); |
| 944 | // We expect either success and no verification error, or failure and a generic failure to |
| 945 | // reject the class. |
| 946 | if (success) { |
| 947 | if (verifier.failure_ != VERIFY_ERROR_NONE) { |
| 948 | LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl |
| 949 | << verifier.fail_messages_; |
| 950 | } |
| 951 | } else { |
| 952 | LOG(INFO) << "Verification error in " << PrettyMethod(method) << " " |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 953 | << verifier.fail_messages_.str(); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 954 | if (gDebugVerify) { |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 955 | std::cout << std::endl << verifier.info_messages_.str(); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 956 | verifier.Dump(std::cout); |
| 957 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 958 | DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC); |
| 959 | } |
| 960 | return success; |
| 961 | } |
| 962 | |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 963 | void DexVerifier::VerifyMethodAndDump(Method* method) { |
| 964 | DexVerifier verifier(method); |
| 965 | verifier.Verify(); |
| 966 | |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 967 | LOG(INFO) << "Dump of method " << PrettyMethod(method) << " " |
| 968 | << verifier.fail_messages_.str() << std::endl |
| 969 | << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier); |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 972 | DexVerifier::DexVerifier(Method* method) |
| 973 | : work_insn_idx_(-1), |
| 974 | method_(method), |
| 975 | failure_(VERIFY_ERROR_NONE), |
| 976 | new_instance_count_(0), |
| 977 | monitor_enter_count_(0) { |
| 978 | CHECK(method != NULL); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 979 | const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); |
Brian Carlstrom | c12a17a | 2012-01-17 18:02:32 -0800 | [diff] [blame] | 980 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 981 | dex_file_ = &class_linker->FindDexFile(dex_cache); |
| 982 | code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset()); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 985 | bool DexVerifier::Verify() { |
| 986 | // If there aren't any instructions, make sure that's expected, then exit successfully. |
| 987 | if (code_item_ == NULL) { |
| 988 | if (!method_->IsNative() && !method_->IsAbstract()) { |
| 989 | Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 990 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 991 | } else { |
| 992 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 993 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 994 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 995 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
| 996 | if (code_item_->ins_size_ > code_item_->registers_size_) { |
| 997 | Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_ |
| 998 | << " regs=" << code_item_->registers_size_; |
| 999 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1000 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1001 | // Allocate and initialize an array to hold instruction data. |
| 1002 | insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]()); |
| 1003 | // Run through the instructions and see if the width checks out. |
| 1004 | bool result = ComputeWidthsAndCountOps(); |
| 1005 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 1006 | result = result && ScanTryCatchBlocks(); |
| 1007 | // Perform static instruction verification. |
| 1008 | result = result && VerifyInstructions(); |
| 1009 | // Perform code flow analysis. |
| 1010 | result = result && VerifyCodeFlow(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1011 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1014 | bool DexVerifier::ComputeWidthsAndCountOps() { |
| 1015 | const uint16_t* insns = code_item_->insns_; |
| 1016 | size_t insns_size = code_item_->insns_size_in_code_units_; |
| 1017 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1018 | size_t new_instance_count = 0; |
| 1019 | size_t monitor_enter_count = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1020 | size_t dex_pc = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1021 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1022 | while (dex_pc < insns_size) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1023 | Instruction::Code opcode = inst->Opcode(); |
| 1024 | if (opcode == Instruction::NEW_INSTANCE) { |
| 1025 | new_instance_count++; |
| 1026 | } else if (opcode == Instruction::MONITOR_ENTER) { |
| 1027 | monitor_enter_count++; |
| 1028 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1029 | size_t inst_size = inst->SizeInCodeUnits(); |
| 1030 | insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size); |
| 1031 | dex_pc += inst_size; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1032 | inst = inst->Next(); |
| 1033 | } |
| 1034 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1035 | if (dex_pc != insns_size) { |
| 1036 | Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected (" |
| 1037 | << dex_pc << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1038 | return false; |
| 1039 | } |
| 1040 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1041 | new_instance_count_ = new_instance_count; |
| 1042 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1043 | return true; |
| 1044 | } |
| 1045 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1046 | bool DexVerifier::ScanTryCatchBlocks() { |
| 1047 | uint32_t tries_size = code_item_->tries_size_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1048 | if (tries_size == 0) { |
| 1049 | return true; |
| 1050 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1051 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1052 | const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1053 | |
| 1054 | for (uint32_t idx = 0; idx < tries_size; idx++) { |
| 1055 | const DexFile::TryItem* try_item = &tries[idx]; |
| 1056 | uint32_t start = try_item->start_addr_; |
| 1057 | uint32_t end = start + try_item->insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1058 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1059 | Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start |
| 1060 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1061 | return false; |
| 1062 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1063 | if (!insn_flags_[start].IsOpcode()) { |
| 1064 | Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1065 | return false; |
| 1066 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1067 | for (uint32_t dex_pc = start; dex_pc < end; |
| 1068 | dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { |
| 1069 | insn_flags_[dex_pc].SetInTry(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1072 | // Iterate over each of the handlers to verify target addresses. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1073 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1074 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1075 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1076 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1077 | CatchHandlerIterator iterator(handlers_ptr); |
| 1078 | for (; iterator.HasNext(); iterator.Next()) { |
| 1079 | uint32_t dex_pc= iterator.GetHandlerAddress(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1080 | if (!insn_flags_[dex_pc].IsOpcode()) { |
| 1081 | Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1082 | return false; |
| 1083 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1084 | insn_flags_[dex_pc].SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1085 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 1086 | // unresolved exception types will be ignored by exception delivery |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1087 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
| 1088 | Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1089 | if (exception_type == NULL) { |
| 1090 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 1091 | Thread::Current()->ClearException(); |
| 1092 | } |
| 1093 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1094 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1095 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1096 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1097 | return true; |
| 1098 | } |
| 1099 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1100 | bool DexVerifier::VerifyInstructions() { |
| 1101 | const Instruction* inst = Instruction::At(code_item_->insns_); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1102 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1103 | /* Flag the start of the method as a branch target. */ |
| 1104 | insn_flags_[0].SetBranchTarget(); |
| 1105 | |
| 1106 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
| 1107 | for(uint32_t dex_pc = 0; dex_pc < insns_size;) { |
| 1108 | if (!VerifyInstruction(inst, dex_pc)) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1109 | DCHECK_NE(failure_, VERIFY_ERROR_NONE); |
| 1110 | fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1111 | return false; |
| 1112 | } |
| 1113 | /* Flag instructions that are garbage collection points */ |
| 1114 | if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) { |
| 1115 | insn_flags_[dex_pc].SetGcPoint(); |
| 1116 | } |
| 1117 | dex_pc += inst->SizeInCodeUnits(); |
| 1118 | inst = inst->Next(); |
| 1119 | } |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
| 1123 | bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
| 1124 | Instruction::DecodedInstruction dec_insn(inst); |
| 1125 | bool result = true; |
| 1126 | switch (inst->GetVerifyTypeArgumentA()) { |
| 1127 | case Instruction::kVerifyRegA: |
| 1128 | result = result && CheckRegisterIndex(dec_insn.vA_); |
| 1129 | break; |
| 1130 | case Instruction::kVerifyRegAWide: |
| 1131 | result = result && CheckWideRegisterIndex(dec_insn.vA_); |
| 1132 | break; |
| 1133 | } |
| 1134 | switch (inst->GetVerifyTypeArgumentB()) { |
| 1135 | case Instruction::kVerifyRegB: |
| 1136 | result = result && CheckRegisterIndex(dec_insn.vB_); |
| 1137 | break; |
| 1138 | case Instruction::kVerifyRegBField: |
| 1139 | result = result && CheckFieldIndex(dec_insn.vB_); |
| 1140 | break; |
| 1141 | case Instruction::kVerifyRegBMethod: |
| 1142 | result = result && CheckMethodIndex(dec_insn.vB_); |
| 1143 | break; |
| 1144 | case Instruction::kVerifyRegBNewInstance: |
| 1145 | result = result && CheckNewInstance(dec_insn.vB_); |
| 1146 | break; |
| 1147 | case Instruction::kVerifyRegBString: |
| 1148 | result = result && CheckStringIndex(dec_insn.vB_); |
| 1149 | break; |
| 1150 | case Instruction::kVerifyRegBType: |
| 1151 | result = result && CheckTypeIndex(dec_insn.vB_); |
| 1152 | break; |
| 1153 | case Instruction::kVerifyRegBWide: |
| 1154 | result = result && CheckWideRegisterIndex(dec_insn.vB_); |
| 1155 | break; |
| 1156 | } |
| 1157 | switch (inst->GetVerifyTypeArgumentC()) { |
| 1158 | case Instruction::kVerifyRegC: |
| 1159 | result = result && CheckRegisterIndex(dec_insn.vC_); |
| 1160 | break; |
| 1161 | case Instruction::kVerifyRegCField: |
| 1162 | result = result && CheckFieldIndex(dec_insn.vC_); |
| 1163 | break; |
| 1164 | case Instruction::kVerifyRegCNewArray: |
| 1165 | result = result && CheckNewArray(dec_insn.vC_); |
| 1166 | break; |
| 1167 | case Instruction::kVerifyRegCType: |
| 1168 | result = result && CheckTypeIndex(dec_insn.vC_); |
| 1169 | break; |
| 1170 | case Instruction::kVerifyRegCWide: |
| 1171 | result = result && CheckWideRegisterIndex(dec_insn.vC_); |
| 1172 | break; |
| 1173 | } |
| 1174 | switch (inst->GetVerifyExtraFlags()) { |
| 1175 | case Instruction::kVerifyArrayData: |
| 1176 | result = result && CheckArrayData(code_offset); |
| 1177 | break; |
| 1178 | case Instruction::kVerifyBranchTarget: |
| 1179 | result = result && CheckBranchTarget(code_offset); |
| 1180 | break; |
| 1181 | case Instruction::kVerifySwitchTargets: |
| 1182 | result = result && CheckSwitchTargets(code_offset); |
| 1183 | break; |
| 1184 | case Instruction::kVerifyVarArg: |
| 1185 | result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_); |
| 1186 | break; |
| 1187 | case Instruction::kVerifyVarArgRange: |
| 1188 | result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_); |
| 1189 | break; |
| 1190 | case Instruction::kVerifyError: |
| 1191 | Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name(); |
| 1192 | result = false; |
| 1193 | break; |
| 1194 | } |
| 1195 | return result; |
| 1196 | } |
| 1197 | |
| 1198 | bool DexVerifier::CheckRegisterIndex(uint32_t idx) { |
| 1199 | if (idx >= code_item_->registers_size_) { |
| 1200 | Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= " |
| 1201 | << code_item_->registers_size_ << ")"; |
| 1202 | return false; |
| 1203 | } |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
| 1207 | bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) { |
| 1208 | if (idx + 1 >= code_item_->registers_size_) { |
| 1209 | Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx |
| 1210 | << "+1 >= " << code_item_->registers_size_ << ")"; |
| 1211 | return false; |
| 1212 | } |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
| 1216 | bool DexVerifier::CheckFieldIndex(uint32_t idx) { |
| 1217 | if (idx >= dex_file_->GetHeader().field_ids_size_) { |
| 1218 | Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max " |
| 1219 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
| 1220 | return false; |
| 1221 | } |
| 1222 | return true; |
| 1223 | } |
| 1224 | |
| 1225 | bool DexVerifier::CheckMethodIndex(uint32_t idx) { |
| 1226 | if (idx >= dex_file_->GetHeader().method_ids_size_) { |
| 1227 | Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max " |
| 1228 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
| 1229 | return false; |
| 1230 | } |
| 1231 | return true; |
| 1232 | } |
| 1233 | |
| 1234 | bool DexVerifier::CheckNewInstance(uint32_t idx) { |
| 1235 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
| 1236 | Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max " |
| 1237 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
| 1238 | return false; |
| 1239 | } |
| 1240 | // We don't need the actual class, just a pointer to the class name. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1241 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1242 | if (descriptor[0] != 'L') { |
| 1243 | Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'"; |
| 1244 | return false; |
| 1245 | } |
| 1246 | return true; |
| 1247 | } |
| 1248 | |
| 1249 | bool DexVerifier::CheckStringIndex(uint32_t idx) { |
| 1250 | if (idx >= dex_file_->GetHeader().string_ids_size_) { |
| 1251 | Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max " |
| 1252 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
| 1253 | return false; |
| 1254 | } |
| 1255 | return true; |
| 1256 | } |
| 1257 | |
| 1258 | bool DexVerifier::CheckTypeIndex(uint32_t idx) { |
| 1259 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
| 1260 | Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max " |
| 1261 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
| 1262 | return false; |
| 1263 | } |
| 1264 | return true; |
| 1265 | } |
| 1266 | |
| 1267 | bool DexVerifier::CheckNewArray(uint32_t idx) { |
| 1268 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
| 1269 | Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max " |
| 1270 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
| 1271 | return false; |
| 1272 | } |
| 1273 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1274 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1275 | const char* cp = descriptor; |
| 1276 | while (*cp++ == '[') { |
| 1277 | bracket_count++; |
| 1278 | } |
| 1279 | if (bracket_count == 0) { |
| 1280 | /* The given class must be an array type. */ |
| 1281 | Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)"; |
| 1282 | return false; |
| 1283 | } else if (bracket_count > 255) { |
| 1284 | /* It is illegal to create an array of more than 255 dimensions. */ |
| 1285 | Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
| 1286 | return false; |
| 1287 | } |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
| 1291 | bool DexVerifier::CheckArrayData(uint32_t cur_offset) { |
| 1292 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1293 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1294 | const uint16_t* array_data; |
| 1295 | int32_t array_data_offset; |
| 1296 | |
| 1297 | DCHECK_LT(cur_offset, insn_count); |
| 1298 | /* make sure the start of the array data table is in range */ |
| 1299 | array_data_offset = insns[1] | (((int32_t) insns[2]) << 16); |
| 1300 | if ((int32_t) cur_offset + array_data_offset < 0 || |
| 1301 | cur_offset + array_data_offset + 2 >= insn_count) { |
| 1302 | Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset |
| 1303 | << ", data offset " << array_data_offset << ", count " << insn_count; |
| 1304 | return false; |
| 1305 | } |
| 1306 | /* offset to array data table is a relative branch-style offset */ |
| 1307 | array_data = insns + array_data_offset; |
| 1308 | /* make sure the table is 32-bit aligned */ |
| 1309 | if ((((uint32_t) array_data) & 0x03) != 0) { |
| 1310 | Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset |
| 1311 | << ", data offset " << array_data_offset; |
| 1312 | return false; |
| 1313 | } |
| 1314 | uint32_t value_width = array_data[1]; |
| 1315 | uint32_t value_count = *(uint32_t*) (&array_data[2]); |
| 1316 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1317 | /* make sure the end of the switch is in range */ |
| 1318 | if (cur_offset + array_data_offset + table_size > insn_count) { |
| 1319 | Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset |
| 1320 | << ", data offset " << array_data_offset << ", end " |
| 1321 | << cur_offset + array_data_offset + table_size |
| 1322 | << ", count " << insn_count; |
| 1323 | return false; |
| 1324 | } |
| 1325 | return true; |
| 1326 | } |
| 1327 | |
| 1328 | bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) { |
| 1329 | int32_t offset; |
| 1330 | bool isConditional, selfOkay; |
| 1331 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1332 | return false; |
| 1333 | } |
| 1334 | if (!selfOkay && offset == 0) { |
| 1335 | Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset; |
| 1336 | return false; |
| 1337 | } |
| 1338 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have |
| 1339 | // identical "wrap-around" behavior, but it's unwise to depend on that. |
| 1340 | if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) { |
| 1341 | Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset; |
| 1342 | return false; |
| 1343 | } |
| 1344 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1345 | int32_t abs_offset = cur_offset + offset; |
| 1346 | if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) { |
| 1347 | Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> " |
| 1348 | << (void*) abs_offset << ") at " << (void*) cur_offset; |
| 1349 | return false; |
| 1350 | } |
| 1351 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
| 1355 | bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
| 1356 | bool* selfOkay) { |
| 1357 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1358 | *pConditional = false; |
| 1359 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1360 | switch (*insns & 0xff) { |
| 1361 | case Instruction::GOTO: |
| 1362 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1363 | break; |
| 1364 | case Instruction::GOTO_32: |
| 1365 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1366 | *selfOkay = true; |
| 1367 | break; |
| 1368 | case Instruction::GOTO_16: |
| 1369 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1370 | break; |
| 1371 | case Instruction::IF_EQ: |
| 1372 | case Instruction::IF_NE: |
| 1373 | case Instruction::IF_LT: |
| 1374 | case Instruction::IF_GE: |
| 1375 | case Instruction::IF_GT: |
| 1376 | case Instruction::IF_LE: |
| 1377 | case Instruction::IF_EQZ: |
| 1378 | case Instruction::IF_NEZ: |
| 1379 | case Instruction::IF_LTZ: |
| 1380 | case Instruction::IF_GEZ: |
| 1381 | case Instruction::IF_GTZ: |
| 1382 | case Instruction::IF_LEZ: |
| 1383 | *pOffset = (int16_t) insns[1]; |
| 1384 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1385 | break; |
| 1386 | default: |
| 1387 | return false; |
| 1388 | break; |
| 1389 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1390 | return true; |
| 1391 | } |
| 1392 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1393 | bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
| 1394 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1395 | DCHECK_LT(cur_offset, insn_count); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1396 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1397 | /* make sure the start of the switch is in range */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1398 | int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16; |
| 1399 | if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) { |
| 1400 | Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset |
| 1401 | << ", switch offset " << switch_offset << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1402 | return false; |
| 1403 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1404 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1405 | const uint16_t* switch_insns = insns + switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1406 | /* make sure the table is 32-bit aligned */ |
| 1407 | if ((((uint32_t) switch_insns) & 0x03) != 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1408 | Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset |
| 1409 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1410 | return false; |
| 1411 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1412 | uint32_t switch_count = switch_insns[1]; |
| 1413 | int32_t keys_offset, targets_offset; |
| 1414 | uint16_t expected_signature; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1415 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 1416 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1417 | targets_offset = 4; |
| 1418 | keys_offset = -1; |
| 1419 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1420 | } else { |
| 1421 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| 1422 | keys_offset = 2; |
| 1423 | targets_offset = 2 + 2 * switch_count; |
| 1424 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1425 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1426 | uint32_t table_size = targets_offset + switch_count * 2; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1427 | if (switch_insns[0] != expected_signature) { |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1428 | Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1429 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1430 | return false; |
| 1431 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1432 | /* make sure the end of the switch is in range */ |
| 1433 | if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1434 | Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset " |
| 1435 | << switch_offset << ", end " |
| 1436 | << (cur_offset + switch_offset + table_size) |
| 1437 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1438 | return false; |
| 1439 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1440 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1441 | if (keys_offset > 0 && switch_count > 1) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1442 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1443 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1444 | int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] | |
| 1445 | (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16); |
| 1446 | if (key <= last_key) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1447 | Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key |
| 1448 | << ", this=" << key; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1451 | last_key = key; |
| 1452 | } |
| 1453 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1454 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1455 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
| 1456 | int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] | |
| 1457 | (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16); |
| 1458 | int32_t abs_offset = cur_offset + offset; |
| 1459 | if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) { |
| 1460 | Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> " |
| 1461 | << (void*) abs_offset << ") at " |
| 1462 | << (void*) cur_offset << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1463 | return false; |
| 1464 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1465 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1466 | } |
| 1467 | return true; |
| 1468 | } |
| 1469 | |
| 1470 | bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
| 1471 | if (vA > 5) { |
| 1472 | Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)"; |
| 1473 | return false; |
| 1474 | } |
| 1475 | uint16_t registers_size = code_item_->registers_size_; |
| 1476 | for (uint32_t idx = 0; idx < vA; idx++) { |
| 1477 | if (arg[idx] > registers_size) { |
| 1478 | Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx] |
| 1479 | << ") in non-range invoke (> " << registers_size << ")"; |
| 1480 | return false; |
| 1481 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | return true; |
| 1485 | } |
| 1486 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1487 | bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
| 1488 | uint16_t registers_size = code_item_->registers_size_; |
| 1489 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1490 | // integer overflow when adding them here. |
| 1491 | if (vA + vC > registers_size) { |
| 1492 | Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> " |
| 1493 | << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1494 | return false; |
| 1495 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1496 | return true; |
| 1497 | } |
| 1498 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1499 | bool DexVerifier::VerifyCodeFlow() { |
| 1500 | uint16_t registers_size = code_item_->registers_size_; |
| 1501 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1502 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1503 | if (registers_size * insns_size > 4*1024*1024) { |
| 1504 | Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size |
| 1505 | << " insns_size=" << insns_size << ")"; |
| 1506 | } |
| 1507 | /* Create and initialize table holding register status */ |
| 1508 | reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size, |
| 1509 | registers_size, this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1510 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1511 | work_line_.reset(new RegisterLine(registers_size, this)); |
| 1512 | saved_line_.reset(new RegisterLine(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1513 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1514 | /* Initialize register types of method arguments. */ |
| 1515 | if (!SetTypesFromSignature()) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1516 | DCHECK_NE(failure_, VERIFY_ERROR_NONE); |
| 1517 | fail_messages_ << "Bad signature in " << PrettyMethod(method_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1518 | return false; |
| 1519 | } |
| 1520 | /* Perform code flow verification. */ |
| 1521 | if (!CodeFlowVerifyMethod()) { |
| 1522 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1525 | /* Generate a register map and add it to the method. */ |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1526 | const std::vector<uint8_t>* map = GenerateGcMap(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1527 | if (map == NULL) { |
| 1528 | return false; // Not a real failure, but a failure to encode |
| 1529 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1530 | Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex()); |
| 1531 | verifier::DexVerifier::SetGcMap(ref, *map); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1532 | #ifndef NDEBUG |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1533 | VerifyGcMap(*map); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1534 | #endif |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1535 | return true; |
| 1536 | } |
| 1537 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1538 | void DexVerifier::Dump(std::ostream& os) { |
| 1539 | if (method_->IsNative()) { |
| 1540 | os << "Native method" << std::endl; |
| 1541 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1542 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1543 | DCHECK(code_item_ != NULL); |
| 1544 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1545 | for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_; |
| 1546 | dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1547 | os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump() |
| 1548 | << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1549 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
| 1550 | if (reg_line != NULL) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1551 | os << reg_line->Dump() << std::endl; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1552 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1553 | inst = inst->Next(); |
| 1554 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1557 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1558 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1559 | case 'I': |
| 1560 | case 'C': |
| 1561 | case 'S': |
| 1562 | case 'B': |
| 1563 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1564 | case 'F': |
| 1565 | case 'D': |
| 1566 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1567 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1568 | default: |
| 1569 | return false; |
| 1570 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1573 | bool DexVerifier::SetTypesFromSignature() { |
| 1574 | RegisterLine* reg_line = reg_table_.GetLine(0); |
| 1575 | int arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
| 1576 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1577 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1578 | DCHECK_GE(arg_start, 0); /* should have been verified earlier */ |
| 1579 | //Include the "this" pointer. |
| 1580 | size_t cur_arg = 0; |
| 1581 | if (!method_->IsStatic()) { |
| 1582 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1583 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1584 | // called. |
| 1585 | Class* declaring_class = method_->GetDeclaringClass(); |
| 1586 | if (method_->IsConstructor() && !declaring_class->IsObjectClass()) { |
| 1587 | reg_line->SetRegisterType(arg_start + cur_arg, |
| 1588 | reg_types_.UninitializedThisArgument(declaring_class)); |
| 1589 | } else { |
| 1590 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1591 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1592 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1595 | const DexFile::ProtoId& proto_id = |
| 1596 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex())); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1597 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1598 | |
| 1599 | for (; iterator.HasNext(); iterator.Next()) { |
| 1600 | const char* descriptor = iterator.GetDescriptor(); |
| 1601 | if (descriptor == NULL) { |
| 1602 | LOG(FATAL) << "Null descriptor"; |
| 1603 | } |
| 1604 | if (cur_arg >= expected_args) { |
| 1605 | Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args |
| 1606 | << " args, found more (" << descriptor << ")"; |
| 1607 | return false; |
| 1608 | } |
| 1609 | switch (descriptor[0]) { |
| 1610 | case 'L': |
| 1611 | case '[': |
| 1612 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1613 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1614 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1615 | // can return without doing anything or call virtual methods). |
| 1616 | { |
| 1617 | const RegType& reg_type = |
| 1618 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 1619 | reg_line->SetRegisterType(arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1620 | } |
| 1621 | break; |
| 1622 | case 'Z': |
| 1623 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean()); |
| 1624 | break; |
| 1625 | case 'C': |
| 1626 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char()); |
| 1627 | break; |
| 1628 | case 'B': |
| 1629 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte()); |
| 1630 | break; |
| 1631 | case 'I': |
| 1632 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer()); |
| 1633 | break; |
| 1634 | case 'S': |
| 1635 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short()); |
| 1636 | break; |
| 1637 | case 'F': |
| 1638 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float()); |
| 1639 | break; |
| 1640 | case 'J': |
| 1641 | case 'D': { |
| 1642 | const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double(); |
| 1643 | reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register |
| 1644 | cur_arg++; |
| 1645 | break; |
| 1646 | } |
| 1647 | default: |
| 1648 | Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'"; |
| 1649 | return false; |
| 1650 | } |
| 1651 | cur_arg++; |
| 1652 | } |
| 1653 | if (cur_arg != expected_args) { |
| 1654 | Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg; |
| 1655 | return false; |
| 1656 | } |
| 1657 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1658 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1659 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1660 | bool result; |
| 1661 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1662 | result = descriptor[1] == '\0'; |
| 1663 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
| 1664 | size_t i = 0; |
| 1665 | do { |
| 1666 | i++; |
| 1667 | } while (descriptor[i] == '['); // process leading [ |
| 1668 | if (descriptor[i] == 'L') { // object array |
| 1669 | do { |
| 1670 | i++; // find closing ; |
| 1671 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1672 | result = descriptor[i] == ';'; |
| 1673 | } else { // primitive array |
| 1674 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1675 | } |
| 1676 | } else if (descriptor[0] == 'L') { |
| 1677 | // could be more thorough here, but shouldn't be required |
| 1678 | size_t i = 0; |
| 1679 | do { |
| 1680 | i++; |
| 1681 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1682 | result = descriptor[i] == ';'; |
| 1683 | } else { |
| 1684 | result = false; |
| 1685 | } |
| 1686 | if (!result) { |
| 1687 | Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '" |
| 1688 | << descriptor << "'"; |
| 1689 | } |
| 1690 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1693 | bool DexVerifier::CodeFlowVerifyMethod() { |
| 1694 | const uint16_t* insns = code_item_->insns_; |
| 1695 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1696 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1697 | /* Begin by marking the first instruction as "changed". */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1698 | insn_flags_[0].SetChanged(); |
| 1699 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1700 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1701 | /* Continue until no instructions are marked "changed". */ |
| 1702 | while (true) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1703 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1704 | uint32_t insn_idx = start_guess; |
| 1705 | for (; insn_idx < insns_size; insn_idx++) { |
| 1706 | if (insn_flags_[insn_idx].IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1707 | break; |
| 1708 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1709 | if (insn_idx == insns_size) { |
| 1710 | if (start_guess != 0) { |
| 1711 | /* try again, starting from the top */ |
| 1712 | start_guess = 0; |
| 1713 | continue; |
| 1714 | } else { |
| 1715 | /* all flags are clear */ |
| 1716 | break; |
| 1717 | } |
| 1718 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1719 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1720 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1721 | // "changed" flags, we need to load the set of registers from the table. |
| 1722 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1723 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1724 | // target. |
| 1725 | work_insn_idx_ = insn_idx; |
| 1726 | if (insn_flags_[insn_idx].IsBranchTarget()) { |
| 1727 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1728 | } else { |
| 1729 | #ifndef NDEBUG |
| 1730 | /* |
| 1731 | * Sanity check: retrieve the stored register line (assuming |
| 1732 | * a full table) and make sure it actually matches. |
| 1733 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1734 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
| 1735 | if (register_line != NULL) { |
| 1736 | if (work_line_->CompareLine(register_line) != 0) { |
| 1737 | Dump(std::cout); |
| 1738 | std::cout << info_messages_.str(); |
| 1739 | LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_) |
| 1740 | << "@" << (void*)work_insn_idx_ << std::endl |
| 1741 | << " work_line=" << *work_line_ << std::endl |
| 1742 | << " expected=" << *register_line; |
| 1743 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1744 | } |
| 1745 | #endif |
| 1746 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1747 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
| 1748 | fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1749 | return false; |
| 1750 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1751 | /* Clear "changed" and mark as visited. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1752 | insn_flags_[insn_idx].SetVisited(); |
| 1753 | insn_flags_[insn_idx].ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1754 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1755 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1756 | if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1757 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1758 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1759 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1760 | * down the line, possibly in the verifier. |
| 1761 | * |
| 1762 | * If we've substituted "always throw" instructions into the stream, |
| 1763 | * we are almost certainly going to have some dead code. |
| 1764 | */ |
| 1765 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1766 | uint32_t insn_idx = 0; |
| 1767 | for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1768 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1769 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1770 | * may or may not be preceded by a padding NOP (for alignment). |
| 1771 | */ |
| 1772 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1773 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1774 | insns[insn_idx] == Instruction::kArrayDataSignature || |
| 1775 | (insns[insn_idx] == Instruction::NOP && |
| 1776 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1777 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1778 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1779 | insn_flags_[insn_idx].SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1782 | if (!insn_flags_[insn_idx].IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1783 | if (dead_start < 0) |
| 1784 | dead_start = insn_idx; |
| 1785 | } else if (dead_start >= 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1786 | LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1787 | dead_start = -1; |
| 1788 | } |
| 1789 | } |
| 1790 | if (dead_start >= 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1791 | LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1792 | } |
| 1793 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1794 | return true; |
| 1795 | } |
| 1796 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1797 | bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1798 | #ifdef VERIFIER_STATS |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1799 | if (CurrentInsnFlags().IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1800 | gDvm.verifierStats.instrsReexamined++; |
| 1801 | } else { |
| 1802 | gDvm.verifierStats.instrsExamined++; |
| 1803 | } |
| 1804 | #endif |
| 1805 | |
| 1806 | /* |
| 1807 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1808 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1809 | * control to another statement: |
| 1810 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1811 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1812 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1813 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1814 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1815 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1816 | * throw an exception that is handled by an encompassing "try" |
| 1817 | * block. |
| 1818 | * |
| 1819 | * We can also return, in which case there is no successor instruction |
| 1820 | * from this point. |
| 1821 | * |
| 1822 | * The behavior can be determined from the OpcodeFlags. |
| 1823 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1824 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 1825 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1826 | Instruction::DecodedInstruction dec_insn(inst); |
| 1827 | int opcode_flag = inst->Flag(); |
| 1828 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1829 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1830 | bool just_set_result = false; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1831 | if (gDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1832 | // Generate processing back trace to debug verifier |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 1833 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl |
| 1834 | << *work_line_.get() << std::endl; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1835 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1836 | |
| 1837 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1838 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1839 | * can throw an exception, we will copy/merge this into the "catch" |
| 1840 | * address rather than work_line, because we don't want the result |
| 1841 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 1842 | * a type) to be visible to the exception handler. |
| 1843 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1844 | if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) { |
| 1845 | saved_line_->CopyFromLine(work_line_.get()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1846 | } else { |
| 1847 | #ifndef NDEBUG |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1848 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1849 | #endif |
| 1850 | } |
| 1851 | |
| 1852 | switch (dec_insn.opcode_) { |
| 1853 | case Instruction::NOP: |
| 1854 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1855 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1856 | * a signature that looks like a NOP; if we see one of these in |
| 1857 | * the course of executing code then we have a problem. |
| 1858 | */ |
| 1859 | if (dec_insn.vA_ != 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1860 | Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1861 | } |
| 1862 | break; |
| 1863 | |
| 1864 | case Instruction::MOVE: |
| 1865 | case Instruction::MOVE_FROM16: |
| 1866 | case Instruction::MOVE_16: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1867 | work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1868 | break; |
| 1869 | case Instruction::MOVE_WIDE: |
| 1870 | case Instruction::MOVE_WIDE_FROM16: |
| 1871 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1872 | work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1873 | break; |
| 1874 | case Instruction::MOVE_OBJECT: |
| 1875 | case Instruction::MOVE_OBJECT_FROM16: |
| 1876 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1877 | work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1878 | break; |
| 1879 | |
| 1880 | /* |
| 1881 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1882 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1883 | * might want to hold the result in an actual CPU register, so the |
| 1884 | * Dalvik spec requires that these only appear immediately after an |
| 1885 | * invoke or filled-new-array. |
| 1886 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1887 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1888 | * redundant with the reset done below, but it can make the debug info |
| 1889 | * easier to read in some cases.) |
| 1890 | */ |
| 1891 | case Instruction::MOVE_RESULT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1892 | work_line_->CopyResultRegister1(dec_insn.vA_, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1893 | break; |
| 1894 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1895 | work_line_->CopyResultRegister2(dec_insn.vA_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1896 | break; |
| 1897 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1898 | work_line_->CopyResultRegister1(dec_insn.vA_, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1899 | break; |
| 1900 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1901 | case Instruction::MOVE_EXCEPTION: { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1902 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1903 | * This statement can only appear as the first instruction in an exception handler (though not |
| 1904 | * all exception handlers need to have one of these). We verify that as part of extracting the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1905 | * exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1906 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1907 | const RegType& res_type = GetCaughtExceptionType(); |
| 1908 | work_line_->SetRegisterType(dec_insn.vA_, res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1909 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1910 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1911 | case Instruction::RETURN_VOID: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1912 | if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) { |
| 1913 | if (!GetMethodReturnType().IsUnknown()) { |
| 1914 | Fail(VERIFY_ERROR_GENERIC) << "return-void not expected"; |
| 1915 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1916 | } |
| 1917 | break; |
| 1918 | case Instruction::RETURN: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1919 | if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1920 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1921 | const RegType& return_type = GetMethodReturnType(); |
| 1922 | if (!return_type.IsCategory1Types()) { |
| 1923 | Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type; |
| 1924 | } else { |
| 1925 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 1926 | // Also, it may use integer values for boolean, byte, short, and character return types. |
| 1927 | const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_); |
| 1928 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 1929 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 1930 | return_type.IsShort() || return_type.IsChar()) && |
| 1931 | src_type.IsInteger())); |
| 1932 | /* check the register contents */ |
| 1933 | work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type); |
| 1934 | if (failure_ != VERIFY_ERROR_NONE) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 1935 | fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1936 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | break; |
| 1940 | case Instruction::RETURN_WIDE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1941 | if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1942 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1943 | const RegType& return_type = GetMethodReturnType(); |
| 1944 | if (!return_type.IsCategory2Types()) { |
| 1945 | Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected"; |
| 1946 | } else { |
| 1947 | /* check the register contents */ |
| 1948 | work_line_->VerifyRegisterType(dec_insn.vA_, return_type); |
| 1949 | if (failure_ != VERIFY_ERROR_NONE) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 1950 | fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1951 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | break; |
| 1955 | case Instruction::RETURN_OBJECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1956 | if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) { |
| 1957 | const RegType& return_type = GetMethodReturnType(); |
| 1958 | if (!return_type.IsReferenceTypes()) { |
| 1959 | Fail(VERIFY_ERROR_GENERIC) << "return-object not expected"; |
| 1960 | } else { |
| 1961 | /* return_type is the *expected* return type, not register value */ |
| 1962 | DCHECK(!return_type.IsZero()); |
| 1963 | DCHECK(!return_type.IsUninitializedReference()); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1964 | const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_); |
| 1965 | // Disallow returning uninitialized values and verify that the reference in vAA is an |
| 1966 | // instance of the "return_type" |
| 1967 | if (reg_type.IsUninitializedTypes()) { |
| 1968 | Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'"; |
| 1969 | } else if (!return_type.IsAssignableFrom(reg_type)) { |
| 1970 | Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type |
| 1971 | << "', but expected from declaration '" << return_type << "'"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1972 | } |
| 1973 | } |
| 1974 | } |
| 1975 | break; |
| 1976 | |
| 1977 | case Instruction::CONST_4: |
| 1978 | case Instruction::CONST_16: |
| 1979 | case Instruction::CONST: |
| 1980 | /* could be boolean, int, float, or a null reference */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1981 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1982 | break; |
| 1983 | case Instruction::CONST_HIGH16: |
| 1984 | /* could be boolean, int, float, or a null reference */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1985 | work_line_->SetRegisterType(dec_insn.vA_, |
| 1986 | reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1987 | break; |
| 1988 | case Instruction::CONST_WIDE_16: |
| 1989 | case Instruction::CONST_WIDE_32: |
| 1990 | case Instruction::CONST_WIDE: |
| 1991 | case Instruction::CONST_WIDE_HIGH16: |
| 1992 | /* could be long or double; resolved upon use */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1993 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1994 | break; |
| 1995 | case Instruction::CONST_STRING: |
| 1996 | case Instruction::CONST_STRING_JUMBO: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1997 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1998 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1999 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2000 | // Get type from instruction if unresolved then we need an access check |
| 2001 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2002 | const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_); |
| 2003 | // Register holds class, ie its type is class, but on error we keep it Unknown |
| 2004 | work_line_->SetRegisterType(dec_insn.vA_, |
| 2005 | res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2006 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2007 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2008 | case Instruction::MONITOR_ENTER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2009 | work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2010 | break; |
| 2011 | case Instruction::MONITOR_EXIT: |
| 2012 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2013 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2014 | * but when they do they act as if they succeeded and the PC is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2015 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2016 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 2017 | * feature that Dalvik doesn't support.) |
| 2018 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2019 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2020 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2021 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2022 | * structured locking checks are working, the former would have |
| 2023 | * failed on the -enter instruction, and the latter is impossible. |
| 2024 | * |
| 2025 | * This is fortunate, because issue 3221411 prevents us from |
| 2026 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2027 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2028 | * some catch blocks (which will show up as "dead" code when |
| 2029 | * we skip them here); if we can't, then the code path could be |
| 2030 | * "live" so we still need to check it. |
| 2031 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2032 | opcode_flag &= ~Instruction::kThrow; |
| 2033 | work_line_->PopMonitor(dec_insn.vA_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2034 | break; |
| 2035 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2036 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2037 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2038 | /* |
| 2039 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 2040 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 2041 | * |
| 2042 | * If it fails, an exception is thrown, which we deal with later by ignoring the update to |
| 2043 | * dec_insn.vA_ when branching to a handler. |
| 2044 | */ |
| 2045 | bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST; |
| 2046 | const RegType& res_type = |
| 2047 | ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_); |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2048 | if (res_type.IsUnknown()) { |
| 2049 | CHECK_NE(failure_, VERIFY_ERROR_NONE); |
| 2050 | break; // couldn't resolve class |
| 2051 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2052 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2053 | const RegType& orig_type = |
| 2054 | work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_); |
| 2055 | if (!res_type.IsNonZeroReferenceTypes()) { |
| 2056 | Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type; |
| 2057 | } else if (!orig_type.IsReferenceTypes()) { |
| 2058 | Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2059 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2060 | if (is_checkcast) { |
| 2061 | work_line_->SetRegisterType(dec_insn.vA_, res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2062 | } else { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2063 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2064 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2065 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2066 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2067 | } |
| 2068 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2069 | const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_); |
| 2070 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 90f2b30 | 2011-10-29 15:05:54 -0700 | [diff] [blame] | 2071 | if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2072 | Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2073 | } else { |
| 2074 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer()); |
| 2075 | } |
| 2076 | } |
| 2077 | break; |
| 2078 | } |
| 2079 | case Instruction::NEW_INSTANCE: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2080 | const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_); |
| 2081 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2082 | // can't create an instance of an interface or abstract class */ |
| 2083 | if (!res_type.IsInstantiableTypes()) { |
| 2084 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2085 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2086 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2087 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
| 2088 | // Any registers holding previous allocations from this address that have not yet been |
| 2089 | // initialized must be marked invalid. |
| 2090 | work_line_->MarkUninitRefsAsInvalid(uninit_type); |
| 2091 | // add the new uninitialized reference to the register state |
| 2092 | work_line_->SetRegisterType(dec_insn.vA_, uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2093 | } |
| 2094 | break; |
| 2095 | } |
| 2096 | case Instruction::NEW_ARRAY: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2097 | const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_); |
| 2098 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2099 | if (!res_type.IsArrayClass()) { |
| 2100 | Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2101 | } else { |
| 2102 | /* make sure "size" register is valid type */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2103 | work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2104 | /* set register type to array class */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2105 | work_line_->SetRegisterType(dec_insn.vA_, res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2106 | } |
| 2107 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2108 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2109 | case Instruction::FILLED_NEW_ARRAY: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2110 | case Instruction::FILLED_NEW_ARRAY_RANGE: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2111 | const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_); |
| 2112 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2113 | if (!res_type.IsArrayClass()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2114 | Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2115 | } else { |
jeffhao | e0cfb6f | 2011-09-22 16:42:56 -0700 | [diff] [blame] | 2116 | bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2117 | /* check the arguments to the instruction */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2118 | VerifyFilledNewArrayRegs(dec_insn, res_type, is_range); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2119 | /* filled-array result goes into "result" register */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2120 | work_line_->SetResultRegisterType(res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2121 | just_set_result = true; |
| 2122 | } |
| 2123 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2124 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2125 | case Instruction::CMPL_FLOAT: |
| 2126 | case Instruction::CMPG_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2127 | work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float()); |
| 2128 | work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float()); |
| 2129 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2130 | break; |
| 2131 | case Instruction::CMPL_DOUBLE: |
| 2132 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2133 | work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double()); |
| 2134 | work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double()); |
| 2135 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2136 | break; |
| 2137 | case Instruction::CMP_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2138 | work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long()); |
| 2139 | work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long()); |
| 2140 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2141 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2142 | case Instruction::THROW: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2143 | const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_); |
| 2144 | if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) { |
| 2145 | Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2146 | } |
| 2147 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2148 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2149 | case Instruction::GOTO: |
| 2150 | case Instruction::GOTO_16: |
| 2151 | case Instruction::GOTO_32: |
| 2152 | /* no effect on or use of registers */ |
| 2153 | break; |
| 2154 | |
| 2155 | case Instruction::PACKED_SWITCH: |
| 2156 | case Instruction::SPARSE_SWITCH: |
| 2157 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2158 | work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2159 | break; |
| 2160 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2161 | case Instruction::FILL_ARRAY_DATA: { |
| 2162 | /* Similar to the verification done for APUT */ |
| 2163 | Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_); |
| 2164 | if (failure_ == VERIFY_ERROR_NONE) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2165 | /* res_class can be null if the reg type is Zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2166 | if (res_class != NULL) { |
| 2167 | Class* component_type = res_class->GetComponentType(); |
| 2168 | if (!res_class->IsArrayClass() || !component_type->IsPrimitive() || |
| 2169 | component_type->IsPrimitiveVoid()) { |
| 2170 | Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2171 | << PrettyDescriptor(res_class); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2172 | } else { |
| 2173 | const RegType& value_type = reg_types_.FromClass(component_type); |
| 2174 | DCHECK(!value_type.IsUnknown()); |
| 2175 | // Now verify if the element width in the table matches the element width declared in |
| 2176 | // the array |
| 2177 | const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16)); |
| 2178 | if (array_data[0] != Instruction::kArrayDataSignature) { |
| 2179 | Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data"; |
| 2180 | } else { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2181 | size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2182 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2183 | // in the table and expected from the array class. |
| 2184 | if (array_data[1] != elem_width) { |
| 2185 | Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1] |
| 2186 | << " vs " << elem_width << ")"; |
| 2187 | } |
| 2188 | } |
| 2189 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2190 | } |
| 2191 | } |
| 2192 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2193 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2194 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2195 | case Instruction::IF_NE: { |
| 2196 | const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_); |
| 2197 | const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_); |
| 2198 | bool mismatch = false; |
| 2199 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 2200 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2201 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2202 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2203 | } else { // both integral? |
| 2204 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2205 | } |
| 2206 | if (mismatch) { |
| 2207 | Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2 |
| 2208 | << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2209 | } |
| 2210 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2211 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2212 | case Instruction::IF_LT: |
| 2213 | case Instruction::IF_GE: |
| 2214 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2215 | case Instruction::IF_LE: { |
| 2216 | const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_); |
| 2217 | const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_); |
| 2218 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
| 2219 | Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << "," |
| 2220 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2221 | } |
| 2222 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2223 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2224 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2225 | case Instruction::IF_NEZ: { |
| 2226 | const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_); |
| 2227 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
| 2228 | Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez"; |
| 2229 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2230 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2231 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2232 | case Instruction::IF_LTZ: |
| 2233 | case Instruction::IF_GEZ: |
| 2234 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2235 | case Instruction::IF_LEZ: { |
| 2236 | const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_); |
| 2237 | if (!reg_type.IsIntegralTypes()) { |
| 2238 | Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type |
| 2239 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
| 2240 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2241 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2242 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2243 | case Instruction::AGET_BOOLEAN: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2244 | VerifyAGet(dec_insn, reg_types_.Boolean(), true); |
| 2245 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2246 | case Instruction::AGET_BYTE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2247 | VerifyAGet(dec_insn, reg_types_.Byte(), true); |
| 2248 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2249 | case Instruction::AGET_CHAR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2250 | VerifyAGet(dec_insn, reg_types_.Char(), true); |
| 2251 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2252 | case Instruction::AGET_SHORT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2253 | VerifyAGet(dec_insn, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2254 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2255 | case Instruction::AGET: |
| 2256 | VerifyAGet(dec_insn, reg_types_.Integer(), true); |
| 2257 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2258 | case Instruction::AGET_WIDE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2259 | VerifyAGet(dec_insn, reg_types_.Long(), true); |
| 2260 | break; |
| 2261 | case Instruction::AGET_OBJECT: |
| 2262 | VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2263 | break; |
| 2264 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2265 | case Instruction::APUT_BOOLEAN: |
| 2266 | VerifyAPut(dec_insn, reg_types_.Boolean(), true); |
| 2267 | break; |
| 2268 | case Instruction::APUT_BYTE: |
| 2269 | VerifyAPut(dec_insn, reg_types_.Byte(), true); |
| 2270 | break; |
| 2271 | case Instruction::APUT_CHAR: |
| 2272 | VerifyAPut(dec_insn, reg_types_.Char(), true); |
| 2273 | break; |
| 2274 | case Instruction::APUT_SHORT: |
| 2275 | VerifyAPut(dec_insn, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2276 | break; |
| 2277 | case Instruction::APUT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2278 | VerifyAPut(dec_insn, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2279 | break; |
| 2280 | case Instruction::APUT_WIDE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2281 | VerifyAPut(dec_insn, reg_types_.Long(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2282 | break; |
| 2283 | case Instruction::APUT_OBJECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2284 | VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2285 | break; |
| 2286 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2287 | case Instruction::IGET_BOOLEAN: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2288 | VerifyISGet(dec_insn, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2289 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2290 | case Instruction::IGET_BYTE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2291 | VerifyISGet(dec_insn, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2292 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2293 | case Instruction::IGET_CHAR: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2294 | VerifyISGet(dec_insn, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2295 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2296 | case Instruction::IGET_SHORT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2297 | VerifyISGet(dec_insn, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2298 | break; |
| 2299 | case Instruction::IGET: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2300 | VerifyISGet(dec_insn, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2301 | break; |
| 2302 | case Instruction::IGET_WIDE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2303 | VerifyISGet(dec_insn, reg_types_.Long(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2304 | break; |
| 2305 | case Instruction::IGET_OBJECT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2306 | VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2307 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2308 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2309 | case Instruction::IPUT_BOOLEAN: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2310 | VerifyISPut(dec_insn, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2311 | break; |
| 2312 | case Instruction::IPUT_BYTE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2313 | VerifyISPut(dec_insn, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2314 | break; |
| 2315 | case Instruction::IPUT_CHAR: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2316 | VerifyISPut(dec_insn, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2317 | break; |
| 2318 | case Instruction::IPUT_SHORT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2319 | VerifyISPut(dec_insn, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2320 | break; |
| 2321 | case Instruction::IPUT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2322 | VerifyISPut(dec_insn, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2323 | break; |
| 2324 | case Instruction::IPUT_WIDE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2325 | VerifyISPut(dec_insn, reg_types_.Long(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2326 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2327 | case Instruction::IPUT_OBJECT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2328 | VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2329 | break; |
| 2330 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2331 | case Instruction::SGET_BOOLEAN: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2332 | VerifyISGet(dec_insn, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2333 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2334 | case Instruction::SGET_BYTE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2335 | VerifyISGet(dec_insn, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2336 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2337 | case Instruction::SGET_CHAR: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2338 | VerifyISGet(dec_insn, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2339 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2340 | case Instruction::SGET_SHORT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2341 | VerifyISGet(dec_insn, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2342 | break; |
| 2343 | case Instruction::SGET: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2344 | VerifyISGet(dec_insn, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2345 | break; |
| 2346 | case Instruction::SGET_WIDE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2347 | VerifyISGet(dec_insn, reg_types_.Long(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2348 | break; |
| 2349 | case Instruction::SGET_OBJECT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2350 | VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2351 | break; |
| 2352 | |
| 2353 | case Instruction::SPUT_BOOLEAN: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2354 | VerifyISPut(dec_insn, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2355 | break; |
| 2356 | case Instruction::SPUT_BYTE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2357 | VerifyISPut(dec_insn, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2358 | break; |
| 2359 | case Instruction::SPUT_CHAR: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2360 | VerifyISPut(dec_insn, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2361 | break; |
| 2362 | case Instruction::SPUT_SHORT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2363 | VerifyISPut(dec_insn, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2364 | break; |
| 2365 | case Instruction::SPUT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2366 | VerifyISPut(dec_insn, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2367 | break; |
| 2368 | case Instruction::SPUT_WIDE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2369 | VerifyISPut(dec_insn, reg_types_.Long(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2370 | break; |
| 2371 | case Instruction::SPUT_OBJECT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2372 | VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2373 | break; |
| 2374 | |
| 2375 | case Instruction::INVOKE_VIRTUAL: |
| 2376 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2377 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2378 | case Instruction::INVOKE_SUPER_RANGE: { |
| 2379 | bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2380 | dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE); |
| 2381 | bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER || |
| 2382 | dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE); |
| 2383 | Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super); |
| 2384 | if (failure_ == VERIFY_ERROR_NONE) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2385 | const char* descriptor; |
| 2386 | if (called_method == NULL) { |
| 2387 | uint32_t method_idx = dec_insn.vB_; |
| 2388 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2389 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2390 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2391 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2392 | descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2393 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2394 | const RegType& return_type = |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2395 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2396 | work_line_->SetResultRegisterType(return_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2397 | just_set_result = true; |
| 2398 | } |
| 2399 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2400 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2401 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2402 | case Instruction::INVOKE_DIRECT_RANGE: { |
| 2403 | bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE); |
| 2404 | Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false); |
| 2405 | if (failure_ == VERIFY_ERROR_NONE) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2406 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2407 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2408 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2409 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2410 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2411 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2412 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2413 | bool is_constructor; |
| 2414 | if (called_method != NULL) { |
| 2415 | is_constructor = called_method->IsConstructor(); |
| 2416 | } else { |
| 2417 | uint32_t method_idx = dec_insn.vB_; |
| 2418 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2419 | const char* name = dex_file_->GetMethodName(method_id); |
| 2420 | is_constructor = strcmp(name, "<init>") == 0; |
| 2421 | } |
| 2422 | if (is_constructor) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2423 | const RegType& this_type = work_line_->GetInvocationThis(dec_insn); |
| 2424 | if (failure_ != VERIFY_ERROR_NONE) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2425 | break; |
| 2426 | |
| 2427 | /* no null refs allowed (?) */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2428 | if (this_type.IsZero()) { |
| 2429 | Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2430 | break; |
| 2431 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2432 | if (called_method != NULL) { |
| 2433 | Class* this_class = this_type.GetClass(); |
| 2434 | DCHECK(this_class != NULL); |
| 2435 | /* must be in same class or in superclass */ |
| 2436 | if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) { |
| 2437 | if (this_class != method_->GetDeclaringClass()) { |
| 2438 | Fail(VERIFY_ERROR_GENERIC) |
| 2439 | << "invoke-direct <init> on super only allowed for 'this' in <init>"; |
| 2440 | break; |
| 2441 | } |
| 2442 | } else if (called_method->GetDeclaringClass() != this_class) { |
| 2443 | Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2444 | break; |
| 2445 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | /* arg must be an uninitialized reference */ |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 2449 | if (!this_type.IsUninitializedTypes()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2450 | Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference " |
| 2451 | << this_type; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2452 | break; |
| 2453 | } |
| 2454 | |
| 2455 | /* |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 2456 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2457 | * registers that have the same object instance in them, not just the "this" register. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2458 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2459 | work_line_->MarkRefsAsInitialized(this_type); |
| 2460 | if (failure_ != VERIFY_ERROR_NONE) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2461 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2462 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2463 | const char* descriptor; |
| 2464 | if (called_method == NULL) { |
| 2465 | uint32_t method_idx = dec_insn.vB_; |
| 2466 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2467 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2468 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2469 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2470 | descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2471 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2472 | const RegType& return_type = |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2473 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2474 | work_line_->SetResultRegisterType(return_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2475 | just_set_result = true; |
| 2476 | } |
| 2477 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2478 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2479 | case Instruction::INVOKE_STATIC: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2480 | case Instruction::INVOKE_STATIC_RANGE: { |
| 2481 | bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE); |
| 2482 | Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false); |
| 2483 | if (failure_ == VERIFY_ERROR_NONE) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2484 | const char* descriptor; |
| 2485 | if (called_method == NULL) { |
| 2486 | uint32_t method_idx = dec_insn.vB_; |
| 2487 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2488 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2489 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2490 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2491 | descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2492 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2493 | const RegType& return_type = |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2494 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2495 | work_line_->SetResultRegisterType(return_type); |
| 2496 | just_set_result = true; |
| 2497 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2498 | } |
| 2499 | break; |
| 2500 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2501 | case Instruction::INVOKE_INTERFACE_RANGE: { |
| 2502 | bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE); |
| 2503 | Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false); |
| 2504 | if (failure_ == VERIFY_ERROR_NONE) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2505 | if (abs_method != NULL) { |
| 2506 | Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | f3c1f78 | 2011-11-02 14:12:15 -0700 | [diff] [blame] | 2507 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2508 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| 2509 | << PrettyMethod(abs_method) << "'"; |
| 2510 | break; |
| 2511 | } |
| 2512 | } |
| 2513 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 2514 | * interface or Object (see comments in RegType::JoinClass). |
| 2515 | */ |
| 2516 | const RegType& this_type = work_line_->GetInvocationThis(dec_insn); |
| 2517 | if (failure_ == VERIFY_ERROR_NONE) { |
| 2518 | if (this_type.IsZero()) { |
| 2519 | /* null pointer always passes (and always fails at runtime) */ |
| 2520 | } else { |
| 2521 | if (this_type.IsUninitializedTypes()) { |
| 2522 | Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object " |
| 2523 | << this_type; |
| 2524 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2525 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2526 | // In the past we have tried to assert that "called_interface" is assignable |
| 2527 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 2528 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 2529 | // implemented by "this_type". For example, two classes may implement the same |
| 2530 | // interfaces and have a common parent that doesn't implement the interface. The |
| 2531 | // join will set "this_type" to the parent class and a test that this implements |
| 2532 | // the interface will incorrectly fail. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2533 | } |
| 2534 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2535 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2536 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 2537 | * the type information is in the abstract method, so we're good. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2538 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2539 | const char* descriptor; |
| 2540 | if (abs_method == NULL) { |
| 2541 | uint32_t method_idx = dec_insn.vB_; |
| 2542 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2543 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2544 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2545 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2546 | descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2547 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2548 | const RegType& return_type = |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2549 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
| 2550 | work_line_->SetResultRegisterType(return_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2551 | work_line_->SetResultRegisterType(return_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2552 | just_set_result = true; |
| 2553 | } |
| 2554 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2555 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2556 | case Instruction::NEG_INT: |
| 2557 | case Instruction::NOT_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2558 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2559 | break; |
| 2560 | case Instruction::NEG_LONG: |
| 2561 | case Instruction::NOT_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2562 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2563 | break; |
| 2564 | case Instruction::NEG_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2565 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2566 | break; |
| 2567 | case Instruction::NEG_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2568 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2569 | break; |
| 2570 | case Instruction::INT_TO_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2571 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2572 | break; |
| 2573 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2574 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2575 | break; |
| 2576 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2577 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2578 | break; |
| 2579 | case Instruction::LONG_TO_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2580 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2581 | break; |
| 2582 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2583 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2584 | break; |
| 2585 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2586 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2587 | break; |
| 2588 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2589 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2590 | break; |
| 2591 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2592 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2593 | break; |
| 2594 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2595 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2596 | break; |
| 2597 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2598 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2599 | break; |
| 2600 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2601 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2602 | break; |
| 2603 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2604 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2605 | break; |
| 2606 | case Instruction::INT_TO_BYTE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2607 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2608 | break; |
| 2609 | case Instruction::INT_TO_CHAR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2610 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2611 | break; |
| 2612 | case Instruction::INT_TO_SHORT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2613 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2614 | break; |
| 2615 | |
| 2616 | case Instruction::ADD_INT: |
| 2617 | case Instruction::SUB_INT: |
| 2618 | case Instruction::MUL_INT: |
| 2619 | case Instruction::REM_INT: |
| 2620 | case Instruction::DIV_INT: |
| 2621 | case Instruction::SHL_INT: |
| 2622 | case Instruction::SHR_INT: |
| 2623 | case Instruction::USHR_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2624 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2625 | break; |
| 2626 | case Instruction::AND_INT: |
| 2627 | case Instruction::OR_INT: |
| 2628 | case Instruction::XOR_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2629 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2630 | break; |
| 2631 | case Instruction::ADD_LONG: |
| 2632 | case Instruction::SUB_LONG: |
| 2633 | case Instruction::MUL_LONG: |
| 2634 | case Instruction::DIV_LONG: |
| 2635 | case Instruction::REM_LONG: |
| 2636 | case Instruction::AND_LONG: |
| 2637 | case Instruction::OR_LONG: |
| 2638 | case Instruction::XOR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2639 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2640 | break; |
| 2641 | case Instruction::SHL_LONG: |
| 2642 | case Instruction::SHR_LONG: |
| 2643 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2644 | /* shift distance is Int, making these different from other binary operations */ |
| 2645 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2646 | break; |
| 2647 | case Instruction::ADD_FLOAT: |
| 2648 | case Instruction::SUB_FLOAT: |
| 2649 | case Instruction::MUL_FLOAT: |
| 2650 | case Instruction::DIV_FLOAT: |
| 2651 | case Instruction::REM_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2652 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2653 | break; |
| 2654 | case Instruction::ADD_DOUBLE: |
| 2655 | case Instruction::SUB_DOUBLE: |
| 2656 | case Instruction::MUL_DOUBLE: |
| 2657 | case Instruction::DIV_DOUBLE: |
| 2658 | case Instruction::REM_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2659 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2660 | break; |
| 2661 | case Instruction::ADD_INT_2ADDR: |
| 2662 | case Instruction::SUB_INT_2ADDR: |
| 2663 | case Instruction::MUL_INT_2ADDR: |
| 2664 | case Instruction::REM_INT_2ADDR: |
| 2665 | case Instruction::SHL_INT_2ADDR: |
| 2666 | case Instruction::SHR_INT_2ADDR: |
| 2667 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2668 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2669 | break; |
| 2670 | case Instruction::AND_INT_2ADDR: |
| 2671 | case Instruction::OR_INT_2ADDR: |
| 2672 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2673 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2674 | break; |
| 2675 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2676 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2677 | break; |
| 2678 | case Instruction::ADD_LONG_2ADDR: |
| 2679 | case Instruction::SUB_LONG_2ADDR: |
| 2680 | case Instruction::MUL_LONG_2ADDR: |
| 2681 | case Instruction::DIV_LONG_2ADDR: |
| 2682 | case Instruction::REM_LONG_2ADDR: |
| 2683 | case Instruction::AND_LONG_2ADDR: |
| 2684 | case Instruction::OR_LONG_2ADDR: |
| 2685 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2686 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2687 | break; |
| 2688 | case Instruction::SHL_LONG_2ADDR: |
| 2689 | case Instruction::SHR_LONG_2ADDR: |
| 2690 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2691 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2692 | break; |
| 2693 | case Instruction::ADD_FLOAT_2ADDR: |
| 2694 | case Instruction::SUB_FLOAT_2ADDR: |
| 2695 | case Instruction::MUL_FLOAT_2ADDR: |
| 2696 | case Instruction::DIV_FLOAT_2ADDR: |
| 2697 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2698 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2699 | break; |
| 2700 | case Instruction::ADD_DOUBLE_2ADDR: |
| 2701 | case Instruction::SUB_DOUBLE_2ADDR: |
| 2702 | case Instruction::MUL_DOUBLE_2ADDR: |
| 2703 | case Instruction::DIV_DOUBLE_2ADDR: |
| 2704 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2705 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2706 | break; |
| 2707 | case Instruction::ADD_INT_LIT16: |
| 2708 | case Instruction::RSUB_INT: |
| 2709 | case Instruction::MUL_INT_LIT16: |
| 2710 | case Instruction::DIV_INT_LIT16: |
| 2711 | case Instruction::REM_INT_LIT16: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2712 | work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2713 | break; |
| 2714 | case Instruction::AND_INT_LIT16: |
| 2715 | case Instruction::OR_INT_LIT16: |
| 2716 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2717 | work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2718 | break; |
| 2719 | case Instruction::ADD_INT_LIT8: |
| 2720 | case Instruction::RSUB_INT_LIT8: |
| 2721 | case Instruction::MUL_INT_LIT8: |
| 2722 | case Instruction::DIV_INT_LIT8: |
| 2723 | case Instruction::REM_INT_LIT8: |
| 2724 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2725 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2726 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2727 | work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2728 | break; |
| 2729 | case Instruction::AND_INT_LIT8: |
| 2730 | case Instruction::OR_INT_LIT8: |
| 2731 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2732 | work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2733 | break; |
| 2734 | |
| 2735 | /* |
| 2736 | * This falls into the general category of "optimized" instructions, |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2737 | * which don't generally appear during verification. Because it's |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2738 | * inserted in the course of verification, we can expect to see it here. |
| 2739 | */ |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 2740 | case Instruction::THROW_VERIFICATION_ERROR: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2741 | break; |
| 2742 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2743 | /* These should never appear during verification. */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2744 | case Instruction::UNUSED_EE: |
| 2745 | case Instruction::UNUSED_EF: |
| 2746 | case Instruction::UNUSED_F2: |
| 2747 | case Instruction::UNUSED_F3: |
| 2748 | case Instruction::UNUSED_F4: |
| 2749 | case Instruction::UNUSED_F5: |
| 2750 | case Instruction::UNUSED_F6: |
| 2751 | case Instruction::UNUSED_F7: |
| 2752 | case Instruction::UNUSED_F8: |
| 2753 | case Instruction::UNUSED_F9: |
| 2754 | case Instruction::UNUSED_FA: |
| 2755 | case Instruction::UNUSED_FB: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2756 | case Instruction::UNUSED_F0: |
| 2757 | case Instruction::UNUSED_F1: |
| 2758 | case Instruction::UNUSED_E3: |
| 2759 | case Instruction::UNUSED_E8: |
| 2760 | case Instruction::UNUSED_E7: |
| 2761 | case Instruction::UNUSED_E4: |
| 2762 | case Instruction::UNUSED_E9: |
| 2763 | case Instruction::UNUSED_FC: |
| 2764 | case Instruction::UNUSED_E5: |
| 2765 | case Instruction::UNUSED_EA: |
| 2766 | case Instruction::UNUSED_FD: |
| 2767 | case Instruction::UNUSED_E6: |
| 2768 | case Instruction::UNUSED_EB: |
| 2769 | case Instruction::UNUSED_FE: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2770 | case Instruction::UNUSED_3E: |
| 2771 | case Instruction::UNUSED_3F: |
| 2772 | case Instruction::UNUSED_40: |
| 2773 | case Instruction::UNUSED_41: |
| 2774 | case Instruction::UNUSED_42: |
| 2775 | case Instruction::UNUSED_43: |
| 2776 | case Instruction::UNUSED_73: |
| 2777 | case Instruction::UNUSED_79: |
| 2778 | case Instruction::UNUSED_7A: |
| 2779 | case Instruction::UNUSED_EC: |
| 2780 | case Instruction::UNUSED_FF: |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 2781 | Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2782 | break; |
| 2783 | |
| 2784 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2785 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2786 | * complain if an instruction is missing (which is desirable). |
| 2787 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2788 | } // end - switch (dec_insn.opcode_) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2789 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2790 | if (failure_ != VERIFY_ERROR_NONE) { |
| 2791 | if (failure_ == VERIFY_ERROR_GENERIC) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2792 | /* immediate failure, reject class */ |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 2793 | fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2794 | return false; |
| 2795 | } else { |
| 2796 | /* replace opcode and continue on */ |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 2797 | fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2798 | ReplaceFailingInstruction(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2799 | /* IMPORTANT: method->insns may have been changed */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2800 | insns = code_item_->insns_ + work_insn_idx_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2801 | /* continue on as if we just handled a throw-verification-error */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2802 | failure_ = VERIFY_ERROR_NONE; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2803 | opcode_flag = Instruction::kThrow; |
| 2804 | } |
| 2805 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2806 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2807 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 2808 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 2809 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2810 | */ |
| 2811 | if (!just_set_result) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2812 | work_line_->SetResultTypeToUnknown(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 2815 | /* Handle "continue". Tag the next consecutive instruction. */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2816 | if ((opcode_flag & Instruction::kContinue) != 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2817 | uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits(); |
| 2818 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
| 2819 | Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2820 | return false; |
| 2821 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2822 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 2823 | // next instruction isn't one. |
| 2824 | if (!CheckMoveException(code_item_->insns_, next_insn_idx)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2825 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2826 | } |
| 2827 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
| 2828 | if (next_line != NULL) { |
| 2829 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 2830 | // needed. |
| 2831 | if (!UpdateRegisters(next_insn_idx, work_line_.get())) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2832 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2833 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2834 | } else { |
| 2835 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2836 | * We're not recording register data for the next instruction, so we don't know what the prior |
| 2837 | * state was. We have to assume that something has changed and re-evaluate it. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2838 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2839 | insn_flags_[next_insn_idx].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2844 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2845 | * |
| 2846 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2847 | * state of the register when the branch is taken or not taken. For example, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2848 | * somebody could get a reference field, check it for zero, and if the |
| 2849 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2850 | * since the value is known to be zero. We do not currently account for |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2851 | * that, and will reject the code. |
| 2852 | * |
| 2853 | * TODO: avoid re-fetching the branch target |
| 2854 | */ |
| 2855 | if ((opcode_flag & Instruction::kBranch) != 0) { |
| 2856 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2857 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2858 | /* should never happen after static verification */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2859 | Fail(VERIFY_ERROR_GENERIC) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2860 | return false; |
| 2861 | } |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2862 | DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2863 | if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2864 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2865 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2866 | /* update branch target, set "changed" if appropriate */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2867 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2868 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2869 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2873 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2874 | * |
| 2875 | * We've already verified that the table is structurally sound, so we |
| 2876 | * just need to walk through and tag the targets. |
| 2877 | */ |
| 2878 | if ((opcode_flag & Instruction::kSwitch) != 0) { |
| 2879 | int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16); |
| 2880 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 2881 | int switch_count = switch_insns[1]; |
| 2882 | int offset_to_targets, targ; |
| 2883 | |
| 2884 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 2885 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 2886 | offset_to_targets = 4; |
| 2887 | } else { |
| 2888 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2889 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2890 | offset_to_targets = 2 + 2 * switch_count; |
| 2891 | } |
| 2892 | |
| 2893 | /* verify each switch target */ |
| 2894 | for (targ = 0; targ < switch_count; targ++) { |
| 2895 | int offset; |
| 2896 | uint32_t abs_offset; |
| 2897 | |
| 2898 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 2899 | offset = switch_insns[offset_to_targets + targ * 2] | |
| 2900 | (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2901 | abs_offset = work_insn_idx_ + offset; |
| 2902 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
| 2903 | if (!CheckMoveException(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2904 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2905 | } |
| 2906 | if (!UpdateRegisters(abs_offset, work_line_.get())) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2907 | return false; |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2912 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 2913 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2914 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2915 | if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) { |
| 2916 | bool within_catch_all = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2917 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2918 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2919 | for (; iterator.HasNext(); iterator.Next()) { |
| 2920 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2921 | within_catch_all = true; |
| 2922 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2923 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2924 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 2925 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 2926 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2927 | */ |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2928 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2929 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2930 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2934 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 2935 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2936 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2937 | if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2938 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2939 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 2940 | * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2941 | * it will do so before grabbing the lock). |
| 2942 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2943 | if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
| 2944 | Fail(VERIFY_ERROR_GENERIC) |
| 2945 | << "expected to be within a catch-all for an instruction where a monitor is held"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2946 | return false; |
| 2947 | } |
| 2948 | } |
| 2949 | } |
| 2950 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2951 | /* If we're returning from the method, make sure monitor stack is empty. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2952 | if ((opcode_flag & Instruction::kReturn) != 0) { |
| 2953 | if(!work_line_->VerifyMonitorStackEmpty()) { |
| 2954 | return false; |
| 2955 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2959 | * Update start_guess. Advance to the next instruction of that's |
| 2960 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2961 | * neither of those exists we're in a return or throw; leave start_guess |
| 2962 | * alone and let the caller sort it out. |
| 2963 | */ |
| 2964 | if ((opcode_flag & Instruction::kContinue) != 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2965 | *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2966 | } else if ((opcode_flag & Instruction::kBranch) != 0) { |
| 2967 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2968 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2969 | } |
| 2970 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2971 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
| 2972 | DCHECK(insn_flags_[*start_guess].IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2973 | |
| 2974 | return true; |
| 2975 | } |
| 2976 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2977 | const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2978 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2979 | Class* referrer = method_->GetDeclaringClass(); |
| 2980 | Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx); |
| 2981 | const RegType& result = |
| 2982 | klass != NULL ? reg_types_.FromClass(klass) |
| 2983 | : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor); |
| 2984 | if (klass == NULL && !result.IsUnresolvedTypes()) { |
| 2985 | method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2986 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2987 | // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to |
| 2988 | // check at runtime if access is allowed and so pass here. |
| 2989 | if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) { |
| 2990 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2991 | << PrettyDescriptor(referrer) << "' -> '" |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2992 | << result << "'"; |
| 2993 | return reg_types_.Unknown(); |
| 2994 | } else { |
| 2995 | return result; |
| 2996 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2999 | const RegType& DexVerifier::GetCaughtExceptionType() { |
| 3000 | const RegType* common_super = NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3001 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3002 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3003 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3004 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3005 | CatchHandlerIterator iterator(handlers_ptr); |
| 3006 | for (; iterator.HasNext(); iterator.Next()) { |
| 3007 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 3008 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3009 | common_super = ®_types_.JavaLangThrowable(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3010 | } else { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3011 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3012 | /* TODO: on error do we want to keep going? If we don't fail this we run the risk of |
| 3013 | * having a non-Throwable introduced at runtime. However, that won't pass an instanceof |
| 3014 | * test, so is essentially harmless. |
| 3015 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3016 | if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) { |
| 3017 | Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception; |
| 3018 | return reg_types_.Unknown(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3019 | } else if (common_super == NULL) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3020 | common_super = &exception; |
| 3021 | } else if (common_super->Equals(exception)) { |
| 3022 | // nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3023 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3024 | common_super = &common_super->Merge(exception, ®_types_); |
| 3025 | CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3026 | } |
| 3027 | } |
| 3028 | } |
| 3029 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3030 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | if (common_super == NULL) { |
| 3034 | /* no catch blocks, or no catches with classes we can find */ |
| 3035 | Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler"; |
| 3036 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3037 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3041 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 3042 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
| 3043 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3044 | fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id); |
| 3045 | return NULL; |
| 3046 | } |
| 3047 | if(klass_type.IsUnresolvedTypes()) { |
| 3048 | return NULL; // Can't resolve Class so no more to do here |
| 3049 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3050 | Class* referrer = method_->GetDeclaringClass(); |
| 3051 | DexCache* dex_cache = referrer->GetDexCache(); |
| 3052 | Method* res_method = dex_cache->GetResolvedMethod(method_idx); |
| 3053 | if (res_method == NULL) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3054 | Class* klass = klass_type.GetClass(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3055 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3056 | std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3057 | if (is_direct) { |
| 3058 | res_method = klass->FindDirectMethod(name, signature); |
| 3059 | } else if (klass->IsInterface()) { |
| 3060 | res_method = klass->FindInterfaceMethod(name, signature); |
| 3061 | } else { |
| 3062 | res_method = klass->FindVirtualMethod(name, signature); |
| 3063 | } |
| 3064 | if (res_method != NULL) { |
| 3065 | dex_cache->SetResolvedMethod(method_idx, res_method); |
| 3066 | } else { |
| 3067 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3068 | << PrettyDescriptor(klass) << "." << name |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3069 | << " " << signature; |
| 3070 | return NULL; |
| 3071 | } |
| 3072 | } |
| 3073 | /* Check if access is allowed. */ |
| 3074 | if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
| 3075 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3076 | << " from " << PrettyDescriptor(referrer) << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3077 | return NULL; |
| 3078 | } |
| 3079 | return res_method; |
| 3080 | } |
| 3081 | |
| 3082 | Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn, |
| 3083 | MethodType method_type, bool is_range, bool is_super) { |
| 3084 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 3085 | // we're making. |
| 3086 | Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_, |
| 3087 | (method_type == METHOD_DIRECT || method_type == METHOD_STATIC)); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3088 | if (res_method == NULL) { // error or class is unresolved |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3089 | return NULL; |
| 3090 | } |
| 3091 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3092 | // enforce them here. |
| 3093 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
| 3094 | Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor " |
| 3095 | << PrettyMethod(res_method); |
| 3096 | return NULL; |
| 3097 | } |
| 3098 | // See if the method type implied by the invoke instruction matches the access flags for the |
| 3099 | // target method. |
| 3100 | if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) || |
| 3101 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
| 3102 | ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect()) |
| 3103 | ) { |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 3104 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of " |
| 3105 | << PrettyMethod(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3106 | return NULL; |
| 3107 | } |
| 3108 | // If we're using invoke-super(method), make sure that the executing method's class' superclass |
| 3109 | // has a vtable entry for the target method. |
| 3110 | if (is_super) { |
| 3111 | DCHECK(method_type == METHOD_VIRTUAL); |
| 3112 | Class* super = method_->GetDeclaringClass()->GetSuperClass(); |
| 3113 | if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) { |
| 3114 | if (super == NULL) { // Only Object has no super class |
| 3115 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_) |
| 3116 | << " to super " << PrettyMethod(res_method); |
| 3117 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3118 | MethodHelper mh(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3119 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_) |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3120 | << " to super " << PrettyDescriptor(super) |
| 3121 | << "." << mh.GetName() |
| 3122 | << mh.GetSignature(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3123 | } |
| 3124 | return NULL; |
| 3125 | } |
| 3126 | } |
| 3127 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3128 | // match the call to the signature. Also, we might might be calling through an abstract method |
| 3129 | // definition (which doesn't have register count values). |
| 3130 | int expected_args = dec_insn.vA_; |
| 3131 | /* caught by static verifier */ |
| 3132 | DCHECK(is_range || expected_args <= 5); |
| 3133 | if (expected_args > code_item_->outs_size_) { |
| 3134 | Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args |
| 3135 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3136 | return NULL; |
| 3137 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3138 | std::string sig(MethodHelper(res_method).GetSignature()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3139 | if (sig[0] != '(') { |
| 3140 | Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method |
| 3141 | << " as descriptor doesn't start with '(': " << sig; |
| 3142 | return NULL; |
| 3143 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3144 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3145 | * Check the "this" argument, which must be an instance of the class |
| 3146 | * that declared the method. For an interface class, we don't do the |
| 3147 | * full interface merge, so we can't do a rigorous check here (which |
| 3148 | * is okay since we have to do it at runtime). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3149 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3150 | int actual_args = 0; |
| 3151 | if (!res_method->IsStatic()) { |
| 3152 | const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn); |
| 3153 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3154 | return NULL; |
| 3155 | } |
| 3156 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
| 3157 | Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized"; |
| 3158 | return NULL; |
| 3159 | } |
| 3160 | if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 3161 | const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass()); |
| 3162 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
| 3163 | Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '" |
| 3164 | << res_method_class << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3165 | return NULL; |
| 3166 | } |
| 3167 | } |
| 3168 | actual_args++; |
| 3169 | } |
| 3170 | /* |
| 3171 | * Process the target method's signature. This signature may or may not |
| 3172 | * have been verified, so we can't assume it's properly formed. |
| 3173 | */ |
| 3174 | size_t sig_offset = 0; |
| 3175 | for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) { |
| 3176 | if (actual_args >= expected_args) { |
| 3177 | Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method) |
| 3178 | << "'. Expected " << expected_args << " args, found more (" |
| 3179 | << sig.substr(sig_offset) << ")"; |
| 3180 | return NULL; |
| 3181 | } |
| 3182 | std::string descriptor; |
| 3183 | if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) { |
| 3184 | size_t end; |
| 3185 | if (sig[sig_offset] == 'L') { |
| 3186 | end = sig.find(';', sig_offset); |
| 3187 | } else { |
| 3188 | for(end = sig_offset + 1; sig[end] == '['; end++) ; |
| 3189 | if (sig[end] == 'L') { |
| 3190 | end = sig.find(';', end); |
| 3191 | } |
| 3192 | } |
| 3193 | if (end == std::string::npos) { |
| 3194 | Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method) |
| 3195 | << "bad signature component '" << sig << "' (missing ';')"; |
| 3196 | return NULL; |
| 3197 | } |
| 3198 | descriptor = sig.substr(sig_offset, end - sig_offset + 1); |
| 3199 | sig_offset = end; |
| 3200 | } else { |
| 3201 | descriptor = sig[sig_offset]; |
| 3202 | } |
| 3203 | const RegType& reg_type = |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 3204 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), |
| 3205 | descriptor.c_str()); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 3206 | uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args]; |
| 3207 | if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { |
| 3208 | return NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3209 | } |
| 3210 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3211 | } |
| 3212 | if (sig[sig_offset] != ')') { |
| 3213 | Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method); |
| 3214 | return NULL; |
| 3215 | } |
| 3216 | if (actual_args != expected_args) { |
| 3217 | Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method) |
| 3218 | << " expected " << expected_args << " args, found " << actual_args; |
| 3219 | return NULL; |
| 3220 | } else { |
| 3221 | return res_method; |
| 3222 | } |
| 3223 | } |
| 3224 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3225 | const RegType& DexVerifier::GetMethodReturnType() { |
| 3226 | return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), |
| 3227 | MethodHelper(method_).GetReturnTypeDescriptor()); |
| 3228 | } |
| 3229 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3230 | void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn, |
| 3231 | const RegType& insn_type, bool is_primitive) { |
| 3232 | const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_); |
| 3233 | if (!index_type.IsArrayIndexTypes()) { |
| 3234 | Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")"; |
| 3235 | } else { |
| 3236 | Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_); |
| 3237 | if (failure_ == VERIFY_ERROR_NONE) { |
| 3238 | if (array_class == NULL) { |
| 3239 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 3240 | // instruction type. TODO: have a proper notion of bottom here. |
| 3241 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 3242 | // Reference or category 1 |
| 3243 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero()); |
| 3244 | } else { |
| 3245 | // Category 2 |
| 3246 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo()); |
| 3247 | } |
| 3248 | } else { |
| 3249 | /* verify the class */ |
| 3250 | Class* component_class = array_class->GetComponentType(); |
| 3251 | const RegType& component_type = reg_types_.FromClass(component_class); |
| 3252 | if (!array_class->IsArrayClass()) { |
| 3253 | Fail(VERIFY_ERROR_GENERIC) << "not array type " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3254 | << PrettyDescriptor(array_class) << " with aget"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3255 | } else if (component_class->IsPrimitive() && !is_primitive) { |
| 3256 | Fail(VERIFY_ERROR_GENERIC) << "primitive array type " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3257 | << PrettyDescriptor(array_class) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3258 | << " source for aget-object"; |
| 3259 | } else if (!component_class->IsPrimitive() && is_primitive) { |
| 3260 | Fail(VERIFY_ERROR_GENERIC) << "reference array type " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3261 | << PrettyDescriptor(array_class) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3262 | << " source for category 1 aget"; |
| 3263 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3264 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
| 3265 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 3266 | Fail(VERIFY_ERROR_GENERIC) << "array type " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3267 | << PrettyDescriptor(array_class) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3268 | << " incompatible with aget of type " << insn_type; |
| 3269 | } else { |
| 3270 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 3271 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 3272 | // doubles. |
| 3273 | work_line_->SetRegisterType(dec_insn.vA_, component_type); |
| 3274 | } |
| 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn, |
| 3281 | const RegType& insn_type, bool is_primitive) { |
| 3282 | const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_); |
| 3283 | if (!index_type.IsArrayIndexTypes()) { |
| 3284 | Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")"; |
| 3285 | } else { |
| 3286 | Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_); |
| 3287 | if (failure_ == VERIFY_ERROR_NONE) { |
| 3288 | if (array_class == NULL) { |
| 3289 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 3290 | // instruction type. |
| 3291 | } else { |
| 3292 | /* verify the class */ |
| 3293 | Class* component_class = array_class->GetComponentType(); |
| 3294 | const RegType& component_type = reg_types_.FromClass(component_class); |
| 3295 | if (!array_class->IsArrayClass()) { |
| 3296 | Fail(VERIFY_ERROR_GENERIC) << "not array type " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3297 | << PrettyDescriptor(array_class) << " with aput"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3298 | } else if (component_class->IsPrimitive() && !is_primitive) { |
| 3299 | Fail(VERIFY_ERROR_GENERIC) << "primitive array type " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3300 | << PrettyDescriptor(array_class) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3301 | << " source for aput-object"; |
| 3302 | } else if (!component_class->IsPrimitive() && is_primitive) { |
| 3303 | Fail(VERIFY_ERROR_GENERIC) << "reference array type " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3304 | << PrettyDescriptor(array_class) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3305 | << " source for category 1 aput"; |
| 3306 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3307 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
| 3308 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 3309 | Fail(VERIFY_ERROR_GENERIC) << "array type " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3310 | << PrettyDescriptor(array_class) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3311 | << " incompatible with aput of type " << insn_type; |
| 3312 | } else { |
| 3313 | // The instruction agrees with the type of array, confirm the value to be stored does too |
Ian Rogers | 26fee74 | 2011-12-13 13:28:31 -0800 | [diff] [blame] | 3314 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 3315 | // incompatible classes will be caught at runtime as an array store exception |
| 3316 | work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3317 | } |
| 3318 | } |
| 3319 | } |
| 3320 | } |
| 3321 | } |
| 3322 | |
| 3323 | Field* DexVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3324 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3325 | // Check access to class |
| 3326 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
| 3327 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3328 | fail_messages_ << " in attempt to access static field " << field_idx << " (" |
| 3329 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3330 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| 3331 | return NULL; |
| 3332 | } |
| 3333 | if(klass_type.IsUnresolvedTypes()) { |
| 3334 | return NULL; // Can't resolve Class so no more to do here |
| 3335 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 3336 | Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3337 | if (field == NULL) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3338 | LOG(INFO) << "unable to resolve static field " << field_idx << " (" |
| 3339 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3340 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3341 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3342 | Thread::Current()->ClearException(); |
| 3343 | return NULL; |
| 3344 | } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(), |
| 3345 | field->GetAccessFlags())) { |
| 3346 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) |
| 3347 | << " from " << PrettyClass(method_->GetDeclaringClass()); |
| 3348 | return NULL; |
| 3349 | } else if (!field->IsStatic()) { |
| 3350 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; |
| 3351 | return NULL; |
| 3352 | } else { |
| 3353 | return field; |
| 3354 | } |
| 3355 | } |
| 3356 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3357 | Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3358 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3359 | // Check access to class |
| 3360 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
| 3361 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3362 | fail_messages_ << " in attempt to access instance field " << field_idx << " (" |
| 3363 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3364 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| 3365 | return NULL; |
| 3366 | } |
| 3367 | if(klass_type.IsUnresolvedTypes()) { |
| 3368 | return NULL; // Can't resolve Class so no more to do here |
| 3369 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 3370 | Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3371 | if (field == NULL) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3372 | LOG(INFO) << "unable to resolve instance field " << field_idx << " (" |
| 3373 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3374 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3375 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3376 | Thread::Current()->ClearException(); |
| 3377 | return NULL; |
| 3378 | } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(), |
| 3379 | field->GetAccessFlags())) { |
| 3380 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) |
| 3381 | << " from " << PrettyClass(method_->GetDeclaringClass()); |
| 3382 | return NULL; |
| 3383 | } else if (field->IsStatic()) { |
| 3384 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) |
| 3385 | << " to not be static"; |
| 3386 | return NULL; |
| 3387 | } else if (obj_type.IsZero()) { |
| 3388 | // Cannot infer and check type, however, access will cause null pointer exception |
| 3389 | return field; |
| 3390 | } else if(obj_type.IsUninitializedReference() && |
| 3391 | (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() || |
| 3392 | field->GetDeclaringClass() != method_->GetDeclaringClass())) { |
| 3393 | // Field accesses through uninitialized references are only allowable for constructors where |
| 3394 | // the field is declared in this class |
| 3395 | Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field) |
| 3396 | << " of a not fully initialized object within the context of " |
| 3397 | << PrettyMethod(method_); |
| 3398 | return NULL; |
| 3399 | } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) { |
| 3400 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 3401 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 3402 | // obj_type, we've discovered this wasn't so, so report the field didn't exist. |
| 3403 | Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field) |
| 3404 | << " from object of type " << PrettyClass(obj_type.GetClass()); |
| 3405 | return NULL; |
| 3406 | } else { |
| 3407 | return field; |
| 3408 | } |
| 3409 | } |
| 3410 | |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3411 | void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn, |
| 3412 | const RegType& insn_type, bool is_primitive, bool is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3413 | uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3414 | Field* field; |
| 3415 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3416 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3417 | } else { |
| 3418 | const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3419 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3420 | } |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3421 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3422 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown()); |
| 3423 | } else { |
| 3424 | const char* descriptor; |
| 3425 | const ClassLoader* loader; |
| 3426 | if (field != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3427 | descriptor = FieldHelper(field).GetTypeDescriptor(); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3428 | loader = field->GetDeclaringClass()->GetClassLoader(); |
| 3429 | } else { |
| 3430 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3431 | descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
| 3432 | loader = method_->GetDeclaringClass()->GetClassLoader(); |
| 3433 | } |
| 3434 | const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3435 | if (is_primitive) { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3436 | if (field_type.Equals(insn_type) || |
| 3437 | (field_type.IsFloat() && insn_type.IsIntegralTypes()) || |
| 3438 | (field_type.IsDouble() && insn_type.IsLongTypes())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3439 | // expected that read is of the correct primitive type or that int reads are reading |
| 3440 | // floats or long reads are reading doubles |
| 3441 | } else { |
| 3442 | // This is a global failure rather than a class change failure as the instructions and |
| 3443 | // the descriptors for the type should have been consistent within the same file at |
| 3444 | // compile time |
| 3445 | Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field) |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3446 | << " to be of type '" << insn_type |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3447 | << "' but found type '" << field_type << "' in get"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3448 | return; |
| 3449 | } |
| 3450 | } else { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3451 | if (!insn_type.IsAssignableFrom(field_type)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3452 | Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field) |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3453 | << " to be compatible with type '" << insn_type |
| 3454 | << "' but found type '" << field_type |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3455 | << "' in get-object"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3456 | return; |
| 3457 | } |
| 3458 | } |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3459 | work_line_->SetRegisterType(dec_insn.vA_, field_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3460 | } |
| 3461 | } |
| 3462 | |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3463 | void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn, |
| 3464 | const RegType& insn_type, bool is_primitive, bool is_static) { |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3465 | uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3466 | Field* field; |
| 3467 | if (is_static) { |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3468 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3469 | } else { |
| 3470 | const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3471 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3472 | } |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3473 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3474 | work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown()); |
| 3475 | } else { |
| 3476 | const char* descriptor; |
| 3477 | const ClassLoader* loader; |
| 3478 | if (field != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3479 | descriptor = FieldHelper(field).GetTypeDescriptor(); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3480 | loader = field->GetDeclaringClass()->GetClassLoader(); |
| 3481 | } else { |
| 3482 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3483 | descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
| 3484 | loader = method_->GetDeclaringClass()->GetClassLoader(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3485 | } |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3486 | const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor); |
| 3487 | if (field != NULL) { |
| 3488 | if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) { |
| 3489 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
| 3490 | << " from other class " << PrettyClass(method_->GetDeclaringClass()); |
| 3491 | return; |
| 3492 | } |
| 3493 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3494 | if (is_primitive) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3495 | // Primitive field assignability rules are weaker than regular assignability rules |
| 3496 | bool instruction_compatible; |
| 3497 | bool value_compatible; |
| 3498 | const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_); |
| 3499 | if (field_type.IsIntegralTypes()) { |
| 3500 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 3501 | value_compatible = value_type.IsIntegralTypes(); |
| 3502 | } else if (field_type.IsFloat()) { |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3503 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3504 | value_compatible = value_type.IsFloatTypes(); |
| 3505 | } else if (field_type.IsLong()) { |
| 3506 | instruction_compatible = insn_type.IsLong(); |
| 3507 | value_compatible = value_type.IsLongTypes(); |
| 3508 | } else if (field_type.IsDouble()) { |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3509 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3510 | value_compatible = value_type.IsDoubleTypes(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3511 | } else { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3512 | instruction_compatible = false; // reference field with primitive store |
| 3513 | value_compatible = false; // unused |
| 3514 | } |
| 3515 | if (!instruction_compatible) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3516 | // This is a global failure rather than a class change failure as the instructions and |
| 3517 | // the descriptors for the type should have been consistent within the same file at |
| 3518 | // compile time |
| 3519 | Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field) |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3520 | << " to be of type '" << insn_type |
| 3521 | << "' but found type '" << field_type |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3522 | << "' in put"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3523 | return; |
| 3524 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3525 | if (!value_compatible) { |
| 3526 | Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_ |
| 3527 | << " of type " << value_type |
| 3528 | << " but expected " << field_type |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3529 | << " for store to " << PrettyField(field) << " in put"; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3530 | return; |
| 3531 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3532 | } else { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3533 | if (!insn_type.IsAssignableFrom(field_type)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3534 | Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field) |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3535 | << " to be compatible with type '" << insn_type |
| 3536 | << "' but found type '" << field_type |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3537 | << "' in put-object"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3538 | return; |
| 3539 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3540 | work_line_->VerifyRegisterType(dec_insn.vA_, field_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3541 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) { |
| 3546 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
| 3547 | Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception"; |
| 3548 | return false; |
| 3549 | } |
| 3550 | return true; |
| 3551 | } |
| 3552 | |
| 3553 | void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn, |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3554 | const RegType& res_type, bool is_range) { |
| 3555 | DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3556 | /* |
| 3557 | * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the |
| 3558 | * list and fail. It's legal, if silly, for arg_count to be zero. |
| 3559 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3560 | const RegType& expected_type = reg_types_.GetComponentType(res_type, |
| 3561 | method_->GetDeclaringClass()->GetClassLoader()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3562 | uint32_t arg_count = dec_insn.vA_; |
| 3563 | for (size_t ui = 0; ui < arg_count; ui++) { |
| 3564 | uint32_t get_reg; |
| 3565 | |
| 3566 | if (is_range) |
| 3567 | get_reg = dec_insn.vC_ + ui; |
| 3568 | else |
| 3569 | get_reg = dec_insn.arg_[ui]; |
| 3570 | |
| 3571 | if (!work_line_->VerifyRegisterType(get_reg, expected_type)) { |
| 3572 | Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg |
| 3573 | << ") not valid"; |
| 3574 | return; |
| 3575 | } |
| 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | void DexVerifier::ReplaceFailingInstruction() { |
Ian Rogers | f1864ef | 2011-12-09 12:39:48 -0800 | [diff] [blame] | 3580 | if (Runtime::Current()->IsStarted()) { |
| 3581 | LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_) |
| 3582 | << " " << fail_messages_.str(); |
| 3583 | return; |
| 3584 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3585 | const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_); |
| 3586 | DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name(); |
| 3587 | VerifyErrorRefType ref_type; |
| 3588 | switch (inst->Opcode()) { |
| 3589 | case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3590 | case Instruction::CHECK_CAST: |
| 3591 | case Instruction::INSTANCE_OF: |
| 3592 | case Instruction::NEW_INSTANCE: |
| 3593 | case Instruction::NEW_ARRAY: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3594 | case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3595 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
| 3596 | ref_type = VERIFY_ERROR_REF_CLASS; |
| 3597 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3598 | case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3599 | case Instruction::IGET_BOOLEAN: |
| 3600 | case Instruction::IGET_BYTE: |
| 3601 | case Instruction::IGET_CHAR: |
| 3602 | case Instruction::IGET_SHORT: |
| 3603 | case Instruction::IGET_WIDE: |
| 3604 | case Instruction::IGET_OBJECT: |
| 3605 | case Instruction::IPUT: |
| 3606 | case Instruction::IPUT_BOOLEAN: |
| 3607 | case Instruction::IPUT_BYTE: |
| 3608 | case Instruction::IPUT_CHAR: |
| 3609 | case Instruction::IPUT_SHORT: |
| 3610 | case Instruction::IPUT_WIDE: |
| 3611 | case Instruction::IPUT_OBJECT: |
| 3612 | case Instruction::SGET: |
| 3613 | case Instruction::SGET_BOOLEAN: |
| 3614 | case Instruction::SGET_BYTE: |
| 3615 | case Instruction::SGET_CHAR: |
| 3616 | case Instruction::SGET_SHORT: |
| 3617 | case Instruction::SGET_WIDE: |
| 3618 | case Instruction::SGET_OBJECT: |
| 3619 | case Instruction::SPUT: |
| 3620 | case Instruction::SPUT_BOOLEAN: |
| 3621 | case Instruction::SPUT_BYTE: |
| 3622 | case Instruction::SPUT_CHAR: |
| 3623 | case Instruction::SPUT_SHORT: |
| 3624 | case Instruction::SPUT_WIDE: |
| 3625 | case Instruction::SPUT_OBJECT: |
| 3626 | ref_type = VERIFY_ERROR_REF_FIELD; |
| 3627 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3628 | case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3629 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 3630 | case Instruction::INVOKE_SUPER: |
| 3631 | case Instruction::INVOKE_SUPER_RANGE: |
| 3632 | case Instruction::INVOKE_DIRECT: |
| 3633 | case Instruction::INVOKE_DIRECT_RANGE: |
| 3634 | case Instruction::INVOKE_STATIC: |
| 3635 | case Instruction::INVOKE_STATIC_RANGE: |
| 3636 | case Instruction::INVOKE_INTERFACE: |
| 3637 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 3638 | ref_type = VERIFY_ERROR_REF_METHOD; |
| 3639 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3640 | default: |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3641 | LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3642 | return; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 3643 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3644 | uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_); |
| 3645 | // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit |
| 3646 | // instruction, so assert it. |
| 3647 | size_t width = inst->SizeInCodeUnits(); |
| 3648 | CHECK_GT(width, 1u); |
Ian Rogers | f1864ef | 2011-12-09 12:39:48 -0800 | [diff] [blame] | 3649 | // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3650 | // NOPs |
| 3651 | for (size_t i = 2; i < width; i++) { |
| 3652 | insns[work_insn_idx_ + i] = Instruction::NOP; |
| 3653 | } |
| 3654 | // Encode the opcode, with the failure code in the high byte |
| 3655 | uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR | |
| 3656 | (failure_ << 8) | // AA - component |
| 3657 | (ref_type << (8 + kVerifyErrorRefTypeShift)); |
| 3658 | insns[work_insn_idx_] = new_instruction; |
| 3659 | // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we |
| 3660 | // rewrote, so nothing to do here. |
Ian Rogers | 9fdfc18 | 2011-10-26 23:12:52 -0700 | [diff] [blame] | 3661 | LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " " |
| 3662 | << fail_messages_.str(); |
| 3663 | if (gDebugVerify) { |
| 3664 | std::cout << std::endl << info_messages_.str(); |
| 3665 | Dump(std::cout); |
| 3666 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3667 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 3668 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3669 | bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) { |
| 3670 | const bool merge_debug = true; |
| 3671 | bool changed = true; |
| 3672 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
| 3673 | if (!insn_flags_[next_insn].IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3674 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3675 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 3676 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 3677 | * only way a register can transition out of "unknown", so this is not just an optimization.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3678 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3679 | target_line->CopyFromLine(merge_line); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3680 | } else { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3681 | UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL); |
| 3682 | copy->CopyFromLine(target_line); |
| 3683 | changed = target_line->MergeRegisters(merge_line); |
| 3684 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3685 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3686 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3687 | if (gDebugVerify && changed) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3688 | LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl |
| 3689 | << *copy.get() << " MERGE" << std::endl |
| 3690 | << *merge_line << " ==" << std::endl |
| 3691 | << *target_line << std::endl; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3692 | } |
| 3693 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3694 | if (changed) { |
| 3695 | insn_flags_[next_insn].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3696 | } |
| 3697 | return true; |
| 3698 | } |
| 3699 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3700 | void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, |
| 3701 | size_t* log2_max_gc_pc) { |
| 3702 | size_t local_gc_points = 0; |
| 3703 | size_t max_insn = 0; |
| 3704 | size_t max_ref_reg = -1; |
| 3705 | for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) { |
| 3706 | if (insn_flags_[i].IsGcPoint()) { |
| 3707 | local_gc_points++; |
| 3708 | max_insn = i; |
| 3709 | RegisterLine* line = reg_table_.GetLine(i); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 3710 | max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3711 | } |
| 3712 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3713 | *gc_points = local_gc_points; |
| 3714 | *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1) |
| 3715 | size_t i = 0; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 3716 | while ((1U << i) <= max_insn) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3717 | i++; |
| 3718 | } |
| 3719 | *log2_max_gc_pc = i; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3722 | const std::vector<uint8_t>* DexVerifier::GenerateGcMap() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3723 | size_t num_entries, ref_bitmap_bits, pc_bits; |
| 3724 | ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits); |
| 3725 | // There's a single byte to encode the size of each bitmap |
| 3726 | if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) { |
| 3727 | // TODO: either a better GC map format or per method failures |
| 3728 | Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with " |
| 3729 | << ref_bitmap_bits << " registers"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3730 | return NULL; |
| 3731 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3732 | size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8; |
| 3733 | // There are 2 bytes to encode the number of entries |
| 3734 | if (num_entries >= 65536) { |
| 3735 | // TODO: either a better GC map format or per method failures |
| 3736 | Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with " |
| 3737 | << num_entries << " entries"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3738 | return NULL; |
| 3739 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3740 | size_t pc_bytes; |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3741 | RegisterMapFormat format; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 3742 | if (pc_bits <= 8) { |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3743 | format = kRegMapFormatCompact8; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3744 | pc_bytes = 1; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 3745 | } else if (pc_bits <= 16) { |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3746 | format = kRegMapFormatCompact16; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3747 | pc_bytes = 2; |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3748 | } else { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3749 | // TODO: either a better GC map format or per method failures |
| 3750 | Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with " |
| 3751 | << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)"; |
| 3752 | return NULL; |
| 3753 | } |
| 3754 | size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3755 | std::vector<uint8_t>* table = new std::vector<uint8_t>; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3756 | if (table == NULL) { |
| 3757 | Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")"; |
| 3758 | return NULL; |
| 3759 | } |
| 3760 | // Write table header |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3761 | table->push_back(format); |
| 3762 | table->push_back(ref_bitmap_bytes); |
| 3763 | table->push_back(num_entries & 0xFF); |
| 3764 | table->push_back((num_entries >> 8) & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3765 | // Write table data |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3766 | for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) { |
| 3767 | if (insn_flags_[i].IsGcPoint()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3768 | table->push_back(i & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3769 | if (pc_bytes == 2) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3770 | table->push_back((i >> 8) & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3771 | } |
| 3772 | RegisterLine* line = reg_table_.GetLine(i); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3773 | line->WriteReferenceBitMap(*table, ref_bitmap_bytes); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3774 | } |
| 3775 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3776 | DCHECK_EQ(table->size(), table_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3777 | return table; |
| 3778 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3779 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3780 | void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3781 | // Check that for every GC point there is a map entry, there aren't entries for non-GC points, |
| 3782 | // that the table data is well formed and all references are marked (or not) in the bitmap |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3783 | PcToReferenceMap map(&data[0], data.size()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3784 | size_t map_index = 0; |
| 3785 | for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) { |
| 3786 | const uint8_t* reg_bitmap = map.FindBitMap(i, false); |
| 3787 | if (insn_flags_[i].IsGcPoint()) { |
| 3788 | CHECK_LT(map_index, map.NumEntries()); |
| 3789 | CHECK_EQ(map.GetPC(map_index), i); |
| 3790 | CHECK_EQ(map.GetBitMap(map_index), reg_bitmap); |
| 3791 | map_index++; |
| 3792 | RegisterLine* line = reg_table_.GetLine(i); |
| 3793 | for(size_t j = 0; j < code_item_->registers_size_; j++) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 3794 | if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3795 | CHECK_LT(j / 8, map.RegWidth()); |
| 3796 | CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1); |
| 3797 | } else if ((j / 8) < map.RegWidth()) { |
| 3798 | CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0); |
| 3799 | } else { |
| 3800 | // If a register doesn't contain a reference then the bitmap may be shorter than the line |
| 3801 | } |
| 3802 | } |
| 3803 | } else { |
| 3804 | CHECK(reg_bitmap == NULL); |
| 3805 | } |
| 3806 | } |
| 3807 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3808 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3809 | const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const { |
| 3810 | size_t num_entries = NumEntries(); |
| 3811 | // Do linear or binary search? |
| 3812 | static const size_t kSearchThreshold = 8; |
| 3813 | if (num_entries < kSearchThreshold) { |
| 3814 | for (size_t i = 0; i < num_entries; i++) { |
| 3815 | if (GetPC(i) == dex_pc) { |
| 3816 | return GetBitMap(i); |
| 3817 | } |
| 3818 | } |
| 3819 | } else { |
| 3820 | int lo = 0; |
| 3821 | int hi = num_entries -1; |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3822 | while (hi >= lo) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3823 | int mid = (hi + lo) / 2; |
| 3824 | int mid_pc = GetPC(mid); |
| 3825 | if (dex_pc > mid_pc) { |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3826 | lo = mid + 1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3827 | } else if (dex_pc < mid_pc) { |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3828 | hi = mid - 1; |
| 3829 | } else { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3830 | return GetBitMap(mid); |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3831 | } |
| 3832 | } |
| 3833 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3834 | if (error_if_not_present) { |
| 3835 | LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc; |
| 3836 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3837 | return NULL; |
| 3838 | } |
| 3839 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3840 | DexVerifier::GcMapTable DexVerifier::gc_maps_; |
| 3841 | |
| 3842 | void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) { |
| 3843 | CHECK(GetGcMap(ref) == NULL); |
| 3844 | gc_maps_[ref] = &gc_map; |
| 3845 | CHECK(GetGcMap(ref) != NULL); |
| 3846 | } |
| 3847 | |
| 3848 | const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) { |
| 3849 | GcMapTable::const_iterator it = gc_maps_.find(ref); |
| 3850 | if (it == gc_maps_.end()) { |
| 3851 | return NULL; |
| 3852 | } |
| 3853 | CHECK(it->second != NULL); |
| 3854 | return it->second; |
| 3855 | } |
| 3856 | |
| 3857 | void DexVerifier::DeleteGcMaps() { |
| 3858 | STLDeleteValues(&gc_maps_); |
| 3859 | } |
| 3860 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3861 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3862 | } // namespace art |