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