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