blob: 9ba5d345761be9fadd859f580f8496f242821b8a [file] [log] [blame]
// Copyright 2011 Google Inc. All Rights Reserved.
#include "dex_verifier.h"
#include <iostream>
#include "class_linker.h"
#include "dex_cache.h"
#include "dex_file.h"
#include "dex_instruction.h"
#include "dex_instruction_visitor.h"
#include "dex_verifier.h"
#include "intern_table.h"
#include "leb128.h"
#include "logging.h"
#include "object_utils.h"
#include "runtime.h"
#include "stringpiece.h"
namespace art {
namespace verifier {
static const bool gDebugVerify = false;
std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
return os << (int)rhs;
}
static const char* type_strings[] = {
"Unknown",
"Conflict",
"Boolean",
"Byte",
"Short",
"Char",
"Integer",
"Float",
"Long (Low Half)",
"Long (High Half)",
"Double (Low Half)",
"Double (High Half)",
"64-bit Constant (Low Half)",
"64-bit Constant (High Half)",
"32-bit Constant",
"Unresolved Reference",
"Uninitialized Reference",
"Uninitialized This Reference",
"Unresolved And Uninitialized Reference",
"Reference",
};
std::string RegType::Dump() const {
DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
std::string result;
if (IsConstant()) {
uint32_t val = ConstantValue();
if (val == 0) {
result = "Zero";
} else {
if(IsConstantShort()) {
result = StringPrintf("32-bit Constant: %d", val);
} else {
result = StringPrintf("32-bit Constant: 0x%x", val);
}
}
} else {
result = type_strings[type_];
if (IsReferenceTypes()) {
result += ": ";
if (IsUnresolvedTypes()) {
result += PrettyDescriptor(GetDescriptor());
} else {
result += PrettyDescriptor(GetClass());
}
}
}
return result;
}
const RegType& RegType::HighHalf(RegTypeCache* cache) const {
CHECK(IsLowHalf());
if (type_ == kRegTypeLongLo) {
return cache->FromType(kRegTypeLongHi);
} else if (type_ == kRegTypeDoubleLo) {
return cache->FromType(kRegTypeDoubleHi);
} else {
return cache->FromType(kRegTypeConstHi);
}
}
/*
* A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
* S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
* 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
* is the deepest (lowest upper bound) parent of S and T).
*
* This operation applies for regular classes and arrays, however, for interface types there needn't
* be a partial ordering on the types. We could solve the problem of a lack of a partial order by
* introducing sets of types, however, the only operation permissible on an interface is
* invoke-interface. In the tradition of Java verifiers we defer the verification of interface
* types until an invoke-interface call on the interface typed reference at runtime and allow
* the perversion of any Object being assignable to an interface type (note, however, that we don't
* allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
* safe; further the Join on a Object cannot result in a sub-class by definition).
*/
Class* RegType::ClassJoin(Class* s, Class* t) {
DCHECK(!s->IsPrimitive()) << PrettyClass(s);
DCHECK(!t->IsPrimitive()) << PrettyClass(t);
if (s == t) {
return s;
} else if (s->IsAssignableFrom(t)) {
return s;
} else if (t->IsAssignableFrom(s)) {
return t;
} else if (s->IsArrayClass() && t->IsArrayClass()) {
Class* s_ct = s->GetComponentType();
Class* t_ct = t->GetComponentType();
if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
// Given the types aren't the same, if either array is of primitive types then the only
// common parent is java.lang.Object
Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
DCHECK(result->IsObjectClass());
return result;
}
Class* common_elem = ClassJoin(s_ct, t_ct);
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
const ClassLoader* class_loader = s->GetClassLoader();
std::string descriptor = "[";
descriptor += ClassHelper(common_elem).GetDescriptor();
Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
DCHECK(array_class != NULL);
return array_class;
} else {
size_t s_depth = s->Depth();
size_t t_depth = t->Depth();
// Get s and t to the same depth in the hierarchy
if (s_depth > t_depth) {
while (s_depth > t_depth) {
s = s->GetSuperClass();
s_depth--;
}
} else {
while (t_depth > s_depth) {
t = t->GetSuperClass();
t_depth--;
}
}
// Go up the hierarchy until we get to the common parent
while (s != t) {
s = s->GetSuperClass();
t = t->GetSuperClass();
}
return s;
}
}
bool RegType::IsAssignableFrom(const RegType& src) const {
if (Equals(src)) {
return true;
} else {
switch (GetType()) {
case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
case RegType::kRegTypeByte: return src.IsByteTypes();
case RegType::kRegTypeShort: return src.IsShortTypes();
case RegType::kRegTypeChar: return src.IsCharTypes();
case RegType::kRegTypeInteger: return src.IsIntegralTypes();
case RegType::kRegTypeFloat: return src.IsFloatTypes();
case RegType::kRegTypeLongLo: return src.IsLongTypes();
case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes();
default:
if (!IsReferenceTypes()) {
LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
}
if (src.IsZero()) {
return true; // all reference types can be assigned null
} else if (!src.IsReferenceTypes()) {
return false; // expect src to be a reference type
} else if (IsJavaLangObject()) {
return true; // all reference types can be assigned to Object
} else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
return true; // We allow assignment to any interface, see comment in ClassJoin
} else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
GetClass()->IsAssignableFrom(src.GetClass())) {
// We're assignable from the Class point-of-view
return true;
} else {
return false;
}
}
}
}
static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
return a.IsConstant() ? b : a;
}
const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
if (IsUnknown() && incoming_type.IsUnknown()) {
return *this; // Unknown MERGE Unknown => Unknown
} else if (IsConflict()) {
return *this; // Conflict MERGE * => Conflict
} else if (incoming_type.IsConflict()) {
return incoming_type; // * MERGE Conflict => Conflict
} else if (IsUnknown() || incoming_type.IsUnknown()) {
return reg_types->Conflict(); // Unknown MERGE * => Conflict
} else if(IsConstant() && incoming_type.IsConstant()) {
int32_t val1 = ConstantValue();
int32_t val2 = incoming_type.ConstantValue();
if (val1 >= 0 && val2 >= 0) {
// +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
if (val1 >= val2) {
return *this;
} else {
return incoming_type;
}
} else if (val1 < 0 && val2 < 0) {
// -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
if (val1 <= val2) {
return *this;
} else {
return incoming_type;
}
} else {
// Values are +ve and -ve, choose smallest signed type in which they both fit
if (IsConstantByte()) {
if (incoming_type.IsConstantByte()) {
return reg_types->ByteConstant();
} else if (incoming_type.IsConstantShort()) {
return reg_types->ShortConstant();
} else {
return reg_types->IntConstant();
}
} else if (IsConstantShort()) {
if (incoming_type.IsConstantShort()) {
return reg_types->ShortConstant();
} else {
return reg_types->IntConstant();
}
} else {
return reg_types->IntConstant();
}
}
} else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
return reg_types->Boolean(); // boolean MERGE boolean => boolean
}
if (IsByteTypes() && incoming_type.IsByteTypes()) {
return reg_types->Byte(); // byte MERGE byte => byte
}
if (IsShortTypes() && incoming_type.IsShortTypes()) {
return reg_types->Short(); // short MERGE short => short
}
if (IsCharTypes() && incoming_type.IsCharTypes()) {
return reg_types->Char(); // char MERGE char => char
}
return reg_types->Integer(); // int MERGE * => int
} else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
(IsLongTypes() && incoming_type.IsLongTypes()) ||
(IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
(IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
(IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
// check constant case was handled prior to entry
DCHECK(!IsConstant() || !incoming_type.IsConstant());
// float/long/double MERGE float/long/double_constant => float/long/double
return SelectNonConstant(*this, incoming_type);
} else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
if (IsZero() || incoming_type.IsZero()) {
return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
} else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
return reg_types->JavaLangObject(); // Object MERGE ref => Object
} else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
// Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
// checked these so => Conflict
return reg_types->Conflict();
} else { // Two reference types, compute Join
Class* c1 = GetClass();
Class* c2 = incoming_type.GetClass();
DCHECK(c1 != NULL && !c1->IsPrimitive());
DCHECK(c2 != NULL && !c2->IsPrimitive());
Class* join_class = ClassJoin(c1, c2);
if (c1 == join_class) {
return *this;
} else if (c2 == join_class) {
return incoming_type;
} else {
return reg_types->FromClass(join_class);
}
}
} else {
return reg_types->Conflict(); // Unexpected types => Conflict
}
}
static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
switch (prim_type) {
case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
case Primitive::kPrimByte: return RegType::kRegTypeByte;
case Primitive::kPrimShort: return RegType::kRegTypeShort;
case Primitive::kPrimChar: return RegType::kRegTypeChar;
case Primitive::kPrimInt: return RegType::kRegTypeInteger;
case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
case Primitive::kPrimVoid:
default: return RegType::kRegTypeUnknown;
}
}
static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
if (descriptor.length() == 1) {
switch (descriptor[0]) {
case 'Z': return RegType::kRegTypeBoolean;
case 'B': return RegType::kRegTypeByte;
case 'S': return RegType::kRegTypeShort;
case 'C': return RegType::kRegTypeChar;
case 'I': return RegType::kRegTypeInteger;
case 'J': return RegType::kRegTypeLongLo;
case 'F': return RegType::kRegTypeFloat;
case 'D': return RegType::kRegTypeDoubleLo;
case 'V':
default: return RegType::kRegTypeUnknown;
}
} else if(descriptor[0] == 'L' || descriptor[0] == '[') {
return RegType::kRegTypeReference;
} else {
return RegType::kRegTypeUnknown;
}
}
std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
os << rhs.Dump();
return os;
}
const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
const std::string& descriptor) {
return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
}
const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
const std::string& descriptor) {
if (type <= RegType::kRegTypeLastFixedLocation) {
// entries should be sized greater than primitive types
DCHECK_GT(entries_.size(), static_cast<size_t>(type));
RegType* entry = entries_[type];
if (entry == NULL) {
Class* klass = NULL;
if (descriptor.size() != 0) {
klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
}
entry = new RegType(type, klass, 0, type);
entries_[type] = entry;
}
return *entry;
} else {
DCHECK (type == RegType::kRegTypeReference);
ClassHelper kh;
for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
RegType* cur_entry = entries_[i];
// check resolved and unresolved references, ignore uninitialized references
if (cur_entry->IsReference()) {
kh.ChangeClass(cur_entry->GetClass());
if (descriptor == kh.GetDescriptor()) {
return *cur_entry;
}
} else if (cur_entry->IsUnresolvedReference() &&
cur_entry->GetDescriptor()->Equals(descriptor)) {
return *cur_entry;
}
}
Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
if (klass != NULL) {
// Able to resolve so create resolved register type
RegType* entry = new RegType(type, klass, 0, entries_.size());
entries_.push_back(entry);
return *entry;
} else {
// TODO: we assume unresolved, but we may be able to do better by validating whether the
// descriptor string is valid
// Unable to resolve so create unresolved register type
DCHECK(Thread::Current()->IsExceptionPending());
Thread::Current()->ClearException();
if (IsValidDescriptor(descriptor.c_str())) {
String* string_descriptor =
Runtime::Current()->GetInternTable()->InternStrong(descriptor.c_str());
RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
entries_.size());
entries_.push_back(entry);
return *entry;
} else {
// The descriptor is broken return the unknown type as there's nothing sensible that
// could be done at runtime
return Unknown();
}
}
}
}
const RegType& RegTypeCache::FromClass(Class* klass) {
if (klass->IsPrimitive()) {
RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
// entries should be sized greater than primitive types
DCHECK_GT(entries_.size(), static_cast<size_t>(type));
RegType* entry = entries_[type];
if (entry == NULL) {
entry = new RegType(type, klass, 0, type);
entries_[type] = entry;
}
return *entry;
} else {
for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
RegType* cur_entry = entries_[i];
if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
return *cur_entry;
}
}
RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
entries_.push_back(entry);
return *entry;
}
}
const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
RegType* entry;
if (type.IsUnresolvedTypes()) {
String* descriptor = type.GetDescriptor();
for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
RegType* cur_entry = entries_[i];
if (cur_entry->IsUnresolvedAndUninitializedReference() &&
cur_entry->GetAllocationPc() == allocation_pc &&
cur_entry->GetDescriptor() == descriptor) {
return *cur_entry;
}
}
entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
descriptor, allocation_pc, entries_.size());
} else {
Class* klass = type.GetClass();
for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
RegType* cur_entry = entries_[i];
if (cur_entry->IsUninitializedReference() &&
cur_entry->GetAllocationPc() == allocation_pc &&
cur_entry->GetClass() == klass) {
return *cur_entry;
}
}
entry = new RegType(RegType::kRegTypeUninitializedReference,
klass, allocation_pc, entries_.size());
}
entries_.push_back(entry);
return *entry;
}
const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
RegType* entry;
if (uninit_type.IsUnresolvedTypes()) {
String* descriptor = uninit_type.GetDescriptor();
for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
RegType* cur_entry = entries_[i];
if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
return *cur_entry;
}
}
entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
} else {
Class* klass = uninit_type.GetClass();
for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
RegType* cur_entry = entries_[i];
if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
return *cur_entry;
}
}
entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
}
entries_.push_back(entry);
return *entry;
}
const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
RegType* cur_entry = entries_[i];
if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
return *cur_entry;
}
}
RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
entries_.size());
entries_.push_back(entry);
return *entry;
}
const RegType& RegTypeCache::FromType(RegType::Type type) {
CHECK(type < RegType::kRegTypeReference);
switch (type) {
case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
case RegType::kRegTypeByte: return From(type, NULL, "B");
case RegType::kRegTypeShort: return From(type, NULL, "S");
case RegType::kRegTypeChar: return From(type, NULL, "C");
case RegType::kRegTypeInteger: return From(type, NULL, "I");
case RegType::kRegTypeFloat: return From(type, NULL, "F");
case RegType::kRegTypeLongLo:
case RegType::kRegTypeLongHi: return From(type, NULL, "J");
case RegType::kRegTypeDoubleLo:
case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
default: return From(type, NULL, "");
}
}
const RegType& RegTypeCache::FromCat1Const(int32_t value) {
for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
RegType* cur_entry = entries_[i];
if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
return *cur_entry;
}
}
RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
entries_.push_back(entry);
return *entry;
}
const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
CHECK(array.IsArrayClass());
if (array.IsUnresolvedTypes()) {
std::string descriptor = array.GetDescriptor()->ToModifiedUtf8();
std::string component = descriptor.substr(1, descriptor.size() - 1);
return FromDescriptor(loader, component);
} else {
return FromClass(array.GetClass()->GetComponentType());
}
}
bool RegisterLine::CheckConstructorReturn() const {
for (size_t i = 0; i < num_regs_; i++) {
if (GetRegisterType(i).IsUninitializedThisReference()) {
verifier_->Fail(VERIFY_ERROR_GENERIC)
<< "Constructor returning without calling superclass constructor";
return false;
}
}
return true;
}
void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
DCHECK(vdst < num_regs_);
if (new_type.IsLowHalf()) {
line_[vdst] = new_type.GetId();
line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
} else if (new_type.IsHighHalf()) {
/* should never set these explicitly */
verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
} else if (new_type.IsConflict()) { // should only be set during a merge
verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
} else {
line_[vdst] = new_type.GetId();
}
// Clear the monitor entry bits for this register.
ClearAllRegToLockDepths(vdst);
}
void RegisterLine::SetResultTypeToUnknown() {
uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
result_[0] = unknown_id;
result_[1] = unknown_id;
}
void RegisterLine::SetResultRegisterType(const RegType& new_type) {
result_[0] = new_type.GetId();
if(new_type.IsLowHalf()) {
DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
result_[1] = new_type.GetId() + 1;
} else {
result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
}
}
const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
// The register index was validated during the static pass, so we don't need to check it here.
DCHECK_LT(vsrc, num_regs_);
return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
}
const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
if (dec_insn.vA_ < 1) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
return verifier_->GetRegTypeCache()->Unknown();
}
/* get the element type of the array held in vsrc */
const RegType& this_type = GetRegisterType(dec_insn.vC_);
if (!this_type.IsReferenceTypes()) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
<< dec_insn.vC_ << " (type=" << this_type << ")";
return verifier_->GetRegTypeCache()->Unknown();
}
return this_type;
}
Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
/* get the element type of the array held in vsrc */
const RegType& type = GetRegisterType(vsrc);
/* if "always zero", we allow it to fail at runtime */
if (type.IsZero()) {
return NULL;
} else if (!type.IsReferenceTypes()) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
<< " (type=" << type << ")";
return NULL;
} else if (type.IsUninitializedReference()) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
return NULL;
} else {
return type.GetClass();
}
}
bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
// Verify the src register type against the check type refining the type of the register
const RegType& src_type = GetRegisterType(vsrc);
if (!check_type.IsAssignableFrom(src_type)) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
<< " but expected " << check_type;
return false;
}
// The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
// precise than the subtype in vsrc so leave it for reference types. For primitive types
// if they are a defined type then they are as precise as we can get, however, for constant
// types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
return true;
}
void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
DCHECK(uninit_type.IsUninitializedTypes());
const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
size_t changed = 0;
for (size_t i = 0; i < num_regs_; i++) {
if (GetRegisterType(i).Equals(uninit_type)) {
line_[i] = init_type.GetId();
changed++;
}
}
DCHECK_GT(changed, 0u);
}
void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
for (size_t i = 0; i < num_regs_; i++) {
if (GetRegisterType(i).Equals(uninit_type)) {
line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
ClearAllRegToLockDepths(i);
}
}
}
void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
const RegType& type = GetRegisterType(vsrc);
SetRegisterType(vdst, type);
if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
(cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
<< " cat=" << static_cast<int>(cat);
} else if (cat == kTypeCategoryRef) {
CopyRegToLockDepth(vdst, vsrc);
}
}
void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
const RegType& type_l = GetRegisterType(vsrc);
const RegType& type_h = GetRegisterType(vsrc + 1);
if (!type_l.CheckWidePair(type_h)) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
<< " type=" << type_l << "/" << type_h;
} else {
SetRegisterType(vdst, type_l); // implicitly sets the second half
}
}
void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
if ((!is_reference && !type.IsCategory1Types()) ||
(is_reference && !type.IsReferenceTypes())) {
verifier_->Fail(VERIFY_ERROR_GENERIC)
<< "copyRes1 v" << vdst << "<- result0" << " type=" << type;
} else {
DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
SetRegisterType(vdst, type);
result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
}
}
/*
* Implement "move-result-wide". Copy the category-2 value from the result
* register to another register, and reset the result register.
*/
void RegisterLine::CopyResultRegister2(uint32_t vdst) {
const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
if (!type_l.IsCategory2Types()) {
verifier_->Fail(VERIFY_ERROR_GENERIC)
<< "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
} else {
DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
SetRegisterType(vdst, type_l); // also sets the high
result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
}
}
void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
const RegType& dst_type, const RegType& src_type) {
if (VerifyRegisterType(dec_insn.vB_, src_type)) {
SetRegisterType(dec_insn.vA_, dst_type);
}
}
void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
const RegType& dst_type,
const RegType& src_type1, const RegType& src_type2,
bool check_boolean_op) {
if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
VerifyRegisterType(dec_insn.vC_, src_type2)) {
if (check_boolean_op) {
DCHECK(dst_type.IsInteger());
if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
return;
}
}
SetRegisterType(dec_insn.vA_, dst_type);
}
}
void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
const RegType& dst_type, const RegType& src_type1,
const RegType& src_type2, bool check_boolean_op) {
if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
VerifyRegisterType(dec_insn.vB_, src_type2)) {
if (check_boolean_op) {
DCHECK(dst_type.IsInteger());
if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
return;
}
}
SetRegisterType(dec_insn.vA_, dst_type);
}
}
void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
const RegType& dst_type, const RegType& src_type,
bool check_boolean_op) {
if (VerifyRegisterType(dec_insn.vB_, src_type)) {
if (check_boolean_op) {
DCHECK(dst_type.IsInteger());
/* check vB with the call, then check the constant manually */
if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
(dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
return;
}
}
SetRegisterType(dec_insn.vA_, dst_type);
}
}
void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
const RegType& reg_type = GetRegisterType(reg_idx);
if (!reg_type.IsReferenceTypes()) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
} else {
SetRegToLockDepth(reg_idx, monitors_.size());
monitors_.push_back(insn_idx);
}
}
void RegisterLine::PopMonitor(uint32_t reg_idx) {
const RegType& reg_type = GetRegisterType(reg_idx);
if (!reg_type.IsReferenceTypes()) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
} else if (monitors_.empty()) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
} else {
monitors_.pop_back();
if(!IsSetLockDepth(reg_idx, monitors_.size())) {
// Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
// format "036" the constant collector may create unlocks on the same object but referenced
// via different registers.
((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
: verifier_->LogVerifyInfo())
<< "monitor-exit not unlocking the top of the monitor stack";
} else {
// Record the register was unlocked
ClearRegToLockDepth(reg_idx, monitors_.size());
}
}
}
bool RegisterLine::VerifyMonitorStackEmpty() {
if (MonitorStackDepth() != 0) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
return false;
} else {
return true;
}
}
bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
bool changed = false;
for (size_t idx = 0; idx < num_regs_; idx++) {
if (line_[idx] != incoming_line->line_[idx]) {
const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
const RegType& cur_type = GetRegisterType(idx);
const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
changed = changed || !cur_type.Equals(new_type);
line_[idx] = new_type.GetId();
}
}
if(monitors_.size() != incoming_line->monitors_.size()) {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
<< MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
} else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
for (uint32_t idx = 0; idx < num_regs_; idx++) {
size_t depths = reg_to_lock_depths_.count(idx);
size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
if (depths != incoming_depths) {
if (depths == 0 || incoming_depths == 0) {
reg_to_lock_depths_.erase(idx);
} else {
verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
<< ": " << depths << " != " << incoming_depths;
break;
}
}
}
}
return changed;
}
void RegisterLine::WriteReferenceBitMap(int8_t* data, size_t max_bytes) {
for (size_t i = 0; i < num_regs_; i += 8) {
uint8_t val = 0;
for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
// Note: we write 1 for a Reference but not for Null
if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
val |= 1 << j;
}
}
if (val != 0) {
DCHECK_LT(i / 8, max_bytes);
data[i / 8] = val;
}
}
}
std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
os << rhs.Dump();
return os;
}
void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
uint32_t insns_size, uint16_t registers_size,
DexVerifier* verifier) {
DCHECK_GT(insns_size, 0U);
for (uint32_t i = 0; i < insns_size; i++) {
bool interesting = false;
switch (mode) {
case kTrackRegsAll:
interesting = flags[i].IsOpcode();
break;
case kTrackRegsGcPoints:
interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
break;
case kTrackRegsBranches:
interesting = flags[i].IsBranchTarget();
break;
default:
break;
}
if (interesting) {
pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
}
}
}
bool DexVerifier::VerifyClass(const Class* klass) {
if (klass->IsVerified()) {
return true;
}
Class* super = klass->GetSuperClass();
if (super == NULL && ClassHelper(klass).GetDescriptor() != "Ljava/lang/Object;") {
LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
return false;
}
if (super != NULL) {
if (!super->IsVerified() && !super->IsErroneous()) {
Runtime::Current()->GetClassLinker()->VerifyClass(super);
}
if (!super->IsVerified()) {
LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
<< " that attempts to sub-class corrupt class " << PrettyClass(super);
return false;
} else if (super->IsFinal()) {
LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
<< " that attempts to sub-class final class " << PrettyClass(super);
return false;
}
}
for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Method* method = klass->GetDirectMethod(i);
if (!VerifyMethod(method)) {
LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
<< PrettyMethod(method, true);
return false;
}
}
for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Method* method = klass->GetVirtualMethod(i);
if (!VerifyMethod(method)) {
LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
<< PrettyMethod(method, true);
return false;
}
}
return true;
}
bool DexVerifier::VerifyMethod(Method* method) {
DexVerifier verifier(method);
bool success = verifier.Verify();
// We expect either success and no verification error, or failure and a generic failure to
// reject the class.
if (success) {
if (verifier.failure_ != VERIFY_ERROR_NONE) {
LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
<< verifier.fail_messages_;
}
} else {
LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
<< verifier.fail_messages_.str();
if (gDebugVerify) {
std::cout << std::endl << verifier.info_messages_.str();
verifier.Dump(std::cout);
}
DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
}
return success;
}
void DexVerifier::VerifyMethodAndDump(Method* method) {
DexVerifier verifier(method);
verifier.Verify();
LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
<< verifier.fail_messages_.str() << std::endl
<< verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
}
DexVerifier::DexVerifier(Method* method) : work_insn_idx_(-1), method_(method),
failure_(VERIFY_ERROR_NONE),
new_instance_count_(0), monitor_enter_count_(0) {
const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
dex_file_ = &class_linker->FindDexFile(dex_cache);
code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
}
bool DexVerifier::Verify() {
// If there aren't any instructions, make sure that's expected, then exit successfully.
if (code_item_ == NULL) {
if (!method_->IsNative() && !method_->IsAbstract()) {
Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
return false;
} else {
return true;
}
}
// Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
if (code_item_->ins_size_ > code_item_->registers_size_) {
Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
<< " regs=" << code_item_->registers_size_;
return false;
}
// Allocate and initialize an array to hold instruction data.
insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
// Run through the instructions and see if the width checks out.
bool result = ComputeWidthsAndCountOps();
// Flag instructions guarded by a "try" block and check exception handlers.
result = result && ScanTryCatchBlocks();
// Perform static instruction verification.
result = result && VerifyInstructions();
// Perform code flow analysis.
result = result && VerifyCodeFlow();
return result;
}
bool DexVerifier::ComputeWidthsAndCountOps() {
const uint16_t* insns = code_item_->insns_;
size_t insns_size = code_item_->insns_size_in_code_units_;
const Instruction* inst = Instruction::At(insns);
size_t new_instance_count = 0;
size_t monitor_enter_count = 0;
size_t dex_pc = 0;
while (dex_pc < insns_size) {
Instruction::Code opcode = inst->Opcode();
if (opcode == Instruction::NEW_INSTANCE) {
new_instance_count++;
} else if (opcode == Instruction::MONITOR_ENTER) {
monitor_enter_count++;
}
size_t inst_size = inst->SizeInCodeUnits();
insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
dex_pc += inst_size;
inst = inst->Next();
}
if (dex_pc != insns_size) {
Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
<< dex_pc << " vs. " << insns_size << ")";
return false;
}
new_instance_count_ = new_instance_count;
monitor_enter_count_ = monitor_enter_count;
return true;
}
bool DexVerifier::ScanTryCatchBlocks() {
uint32_t tries_size = code_item_->tries_size_;
if (tries_size == 0) {
return true;
}
uint32_t insns_size = code_item_->insns_size_in_code_units_;
const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
for (uint32_t idx = 0; idx < tries_size; idx++) {
const DexFile::TryItem* try_item = &tries[idx];
uint32_t start = try_item->start_addr_;
uint32_t end = start + try_item->insn_count_;
if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
<< " endAddr=" << end << " (size=" << insns_size << ")";
return false;
}
if (!insn_flags_[start].IsOpcode()) {
Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
return false;
}
for (uint32_t dex_pc = start; dex_pc < end;
dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
insn_flags_[dex_pc].SetInTry();
}
}
/* Iterate over each of the handlers to verify target addresses. */
const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
ClassLinker* linker = Runtime::Current()->GetClassLinker();
for (uint32_t idx = 0; idx < handlers_size; idx++) {
CatchHandlerIterator iterator(handlers_ptr);
for (; iterator.HasNext(); iterator.Next()) {
uint32_t dex_pc= iterator.GetHandlerAddress();
if (!insn_flags_[dex_pc].IsOpcode()) {
Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
return false;
}
insn_flags_[dex_pc].SetBranchTarget();
// Ensure exception types are resolved so that they don't need resolution to be delivered,
// unresolved exception types will be ignored by exception delivery
if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
if (exception_type == NULL) {
DCHECK(Thread::Current()->IsExceptionPending());
Thread::Current()->ClearException();
}
}
}
handlers_ptr = iterator.EndDataPointer();
}
return true;
}
bool DexVerifier::VerifyInstructions() {
const Instruction* inst = Instruction::At(code_item_->insns_);
/* Flag the start of the method as a branch target. */
insn_flags_[0].SetBranchTarget();
uint32_t insns_size = code_item_->insns_size_in_code_units_;
for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
if (!VerifyInstruction(inst, dex_pc)) {
DCHECK_NE(failure_, VERIFY_ERROR_NONE);
fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
return false;
}
/* Flag instructions that are garbage collection points */
if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
insn_flags_[dex_pc].SetGcPoint();
}
dex_pc += inst->SizeInCodeUnits();
inst = inst->Next();
}
return true;
}
bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Instruction::DecodedInstruction dec_insn(inst);
bool result = true;
switch (inst->GetVerifyTypeArgumentA()) {
case Instruction::kVerifyRegA:
result = result && CheckRegisterIndex(dec_insn.vA_);
break;
case Instruction::kVerifyRegAWide:
result = result && CheckWideRegisterIndex(dec_insn.vA_);
break;
}
switch (inst->GetVerifyTypeArgumentB()) {
case Instruction::kVerifyRegB:
result = result && CheckRegisterIndex(dec_insn.vB_);
break;
case Instruction::kVerifyRegBField:
result = result && CheckFieldIndex(dec_insn.vB_);
break;
case Instruction::kVerifyRegBMethod:
result = result && CheckMethodIndex(dec_insn.vB_);
break;
case Instruction::kVerifyRegBNewInstance:
result = result && CheckNewInstance(dec_insn.vB_);
break;
case Instruction::kVerifyRegBString:
result = result && CheckStringIndex(dec_insn.vB_);
break;
case Instruction::kVerifyRegBType:
result = result && CheckTypeIndex(dec_insn.vB_);
break;
case Instruction::kVerifyRegBWide:
result = result && CheckWideRegisterIndex(dec_insn.vB_);
break;
}
switch (inst->GetVerifyTypeArgumentC()) {
case Instruction::kVerifyRegC:
result = result && CheckRegisterIndex(dec_insn.vC_);
break;
case Instruction::kVerifyRegCField:
result = result && CheckFieldIndex(dec_insn.vC_);
break;
case Instruction::kVerifyRegCNewArray:
result = result && CheckNewArray(dec_insn.vC_);
break;
case Instruction::kVerifyRegCType:
result = result && CheckTypeIndex(dec_insn.vC_);
break;
case Instruction::kVerifyRegCWide:
result = result && CheckWideRegisterIndex(dec_insn.vC_);
break;
}
switch (inst->GetVerifyExtraFlags()) {
case Instruction::kVerifyArrayData:
result = result && CheckArrayData(code_offset);
break;
case Instruction::kVerifyBranchTarget:
result = result && CheckBranchTarget(code_offset);
break;
case Instruction::kVerifySwitchTargets:
result = result && CheckSwitchTargets(code_offset);
break;
case Instruction::kVerifyVarArg:
result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
break;
case Instruction::kVerifyVarArgRange:
result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
break;
case Instruction::kVerifyError:
Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
result = false;
break;
}
return result;
}
bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
if (idx >= code_item_->registers_size_) {
Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
<< code_item_->registers_size_ << ")";
return false;
}
return true;
}
bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
if (idx + 1 >= code_item_->registers_size_) {
Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
<< "+1 >= " << code_item_->registers_size_ << ")";
return false;
}
return true;
}
bool DexVerifier::CheckFieldIndex(uint32_t idx) {
if (idx >= dex_file_->GetHeader().field_ids_size_) {
Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
<< dex_file_->GetHeader().field_ids_size_ << ")";
return false;
}
return true;
}
bool DexVerifier::CheckMethodIndex(uint32_t idx) {
if (idx >= dex_file_->GetHeader().method_ids_size_) {
Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
<< dex_file_->GetHeader().method_ids_size_ << ")";
return false;
}
return true;
}
bool DexVerifier::CheckNewInstance(uint32_t idx) {
if (idx >= dex_file_->GetHeader().type_ids_size_) {
Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
<< dex_file_->GetHeader().type_ids_size_ << ")";
return false;
}
// We don't need the actual class, just a pointer to the class name.
const char* descriptor = dex_file_->StringByTypeIdx(idx);
if (descriptor[0] != 'L') {
Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
return false;
}
return true;
}
bool DexVerifier::CheckStringIndex(uint32_t idx) {
if (idx >= dex_file_->GetHeader().string_ids_size_) {
Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
<< dex_file_->GetHeader().string_ids_size_ << ")";
return false;
}
return true;
}
bool DexVerifier::CheckTypeIndex(uint32_t idx) {
if (idx >= dex_file_->GetHeader().type_ids_size_) {
Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
<< dex_file_->GetHeader().type_ids_size_ << ")";
return false;
}
return true;
}
bool DexVerifier::CheckNewArray(uint32_t idx) {
if (idx >= dex_file_->GetHeader().type_ids_size_) {
Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
<< dex_file_->GetHeader().type_ids_size_ << ")";
return false;
}
int bracket_count = 0;
const char* descriptor = dex_file_->StringByTypeIdx(idx);
const char* cp = descriptor;
while (*cp++ == '[') {
bracket_count++;
}
if (bracket_count == 0) {
/* The given class must be an array type. */
Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
return false;
} else if (bracket_count > 255) {
/* It is illegal to create an array of more than 255 dimensions. */
Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
return false;
}
return true;
}
bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
const uint32_t insn_count = code_item_->insns_size_in_code_units_;
const uint16_t* insns = code_item_->insns_ + cur_offset;
const uint16_t* array_data;
int32_t array_data_offset;
DCHECK_LT(cur_offset, insn_count);
/* make sure the start of the array data table is in range */
array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
if ((int32_t) cur_offset + array_data_offset < 0 ||
cur_offset + array_data_offset + 2 >= insn_count) {
Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
<< ", data offset " << array_data_offset << ", count " << insn_count;
return false;
}
/* offset to array data table is a relative branch-style offset */
array_data = insns + array_data_offset;
/* make sure the table is 32-bit aligned */
if ((((uint32_t) array_data) & 0x03) != 0) {
Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
<< ", data offset " << array_data_offset;
return false;
}
uint32_t value_width = array_data[1];
uint32_t value_count = *(uint32_t*) (&array_data[2]);
uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
/* make sure the end of the switch is in range */
if (cur_offset + array_data_offset + table_size > insn_count) {
Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
<< ", data offset " << array_data_offset << ", end "
<< cur_offset + array_data_offset + table_size
<< ", count " << insn_count;
return false;
}
return true;
}
bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
int32_t offset;
bool isConditional, selfOkay;
if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
return false;
}
if (!selfOkay && offset == 0) {
Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
return false;
}
// Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
// identical "wrap-around" behavior, but it's unwise to depend on that.
if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
return false;
}
const uint32_t insn_count = code_item_->insns_size_in_code_units_;
int32_t abs_offset = cur_offset + offset;
if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
<< (void*) abs_offset << ") at " << (void*) cur_offset;
return false;
}
insn_flags_[abs_offset].SetBranchTarget();
return true;
}
bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
bool* selfOkay) {
const uint16_t* insns = code_item_->insns_ + cur_offset;
*pConditional = false;
*selfOkay = false;
switch (*insns & 0xff) {
case Instruction::GOTO:
*pOffset = ((int16_t) *insns) >> 8;
break;
case Instruction::GOTO_32:
*pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
*selfOkay = true;
break;
case Instruction::GOTO_16:
*pOffset = (int16_t) insns[1];
break;
case Instruction::IF_EQ:
case Instruction::IF_NE:
case Instruction::IF_LT:
case Instruction::IF_GE:
case Instruction::IF_GT:
case Instruction::IF_LE:
case Instruction::IF_EQZ:
case Instruction::IF_NEZ:
case Instruction::IF_LTZ:
case Instruction::IF_GEZ:
case Instruction::IF_GTZ:
case Instruction::IF_LEZ:
*pOffset = (int16_t) insns[1];
*pConditional = true;
break;
default:
return false;
break;
}
return true;
}
bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
const uint32_t insn_count = code_item_->insns_size_in_code_units_;
DCHECK_LT(cur_offset, insn_count);
const uint16_t* insns = code_item_->insns_ + cur_offset;
/* make sure the start of the switch is in range */
int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
<< ", switch offset " << switch_offset << ", count " << insn_count;
return false;
}
/* offset to switch table is a relative branch-style offset */
const uint16_t* switch_insns = insns + switch_offset;
/* make sure the table is 32-bit aligned */
if ((((uint32_t) switch_insns) & 0x03) != 0) {
Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
<< ", switch offset " << switch_offset;
return false;
}
uint32_t switch_count = switch_insns[1];
int32_t keys_offset, targets_offset;
uint16_t expected_signature;
if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
/* 0=sig, 1=count, 2/3=firstKey */
targets_offset = 4;
keys_offset = -1;
expected_signature = Instruction::kPackedSwitchSignature;
} else {
/* 0=sig, 1=count, 2..count*2 = keys */
keys_offset = 2;
targets_offset = 2 + 2 * switch_count;
expected_signature = Instruction::kSparseSwitchSignature;
}
uint32_t table_size = targets_offset + switch_count * 2;
if (switch_insns[0] != expected_signature) {
Fail(VERIFY_ERROR_GENERIC) << "wrong signature for switch table (" << (void*) switch_insns[0]
<< ", wanted " << (void*) expected_signature << ")";
return false;
}
/* make sure the end of the switch is in range */
if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
<< switch_offset << ", end "
<< (cur_offset + switch_offset + table_size)
<< ", count " << insn_count;
return false;
}
/* for a sparse switch, verify the keys are in ascending order */
if (keys_offset > 0 && switch_count > 1) {
int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
for (uint32_t targ = 1; targ < switch_count; targ++) {
int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
(int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
if (key <= last_key) {
Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
<< ", this=" << key;
return false;
}
last_key = key;
}
}
/* verify each switch target */
for (uint32_t targ = 0; targ < switch_count; targ++) {
int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
(int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
int32_t abs_offset = cur_offset + offset;
if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
<< (void*) abs_offset << ") at "
<< (void*) cur_offset << "[" << targ << "]";
return false;
}
insn_flags_[abs_offset].SetBranchTarget();
}
return true;
}
bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
if (vA > 5) {
Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
return false;
}
uint16_t registers_size = code_item_->registers_size_;
for (uint32_t idx = 0; idx < vA; idx++) {
if (arg[idx] > registers_size) {
Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
<< ") in non-range invoke (> " << registers_size << ")";
return false;
}
}
return true;
}
bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
uint16_t registers_size = code_item_->registers_size_;
// vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
// integer overflow when adding them here.
if (vA + vC > registers_size) {
Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
<< registers_size << ")";
return false;
}
return true;
}
bool DexVerifier::VerifyCodeFlow() {
uint16_t registers_size = code_item_->registers_size_;
uint32_t insns_size = code_item_->insns_size_in_code_units_;
if (registers_size * insns_size > 4*1024*1024) {
Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
<< " insns_size=" << insns_size << ")";
}
/* Create and initialize table holding register status */
reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
registers_size, this);
work_line_.reset(new RegisterLine(registers_size, this));
saved_line_.reset(new RegisterLine(registers_size, this));
/* Initialize register types of method arguments. */
if (!SetTypesFromSignature()) {
DCHECK_NE(failure_, VERIFY_ERROR_NONE);
fail_messages_ << "Bad signature in " << PrettyMethod(method_);
return false;
}
/* Perform code flow verification. */
if (!CodeFlowVerifyMethod()) {
return false;
}
/* Generate a register map and add it to the method. */
ByteArray* map = GenerateGcMap();
if (map == NULL) {
return false; // Not a real failure, but a failure to encode
}
method_->SetGcMap(map);
#ifndef NDEBUG
VerifyGcMap();
#endif
return true;
}
void DexVerifier::Dump(std::ostream& os) {
if (method_->IsNative()) {
os << "Native method" << std::endl;
return;
}
DCHECK(code_item_ != NULL);
const Instruction* inst = Instruction::At(code_item_->insns_);
for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
<< " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
if (reg_line != NULL) {
os << reg_line->Dump() << std::endl;
}
inst = inst->Next();
}
}
static bool IsPrimitiveDescriptor(char descriptor) {
switch (descriptor) {
case 'I':
case 'C':
case 'S':
case 'B':
case 'Z':
case 'F':
case 'D':
case 'J':
return true;
default:
return false;
}
}
bool DexVerifier::SetTypesFromSignature() {
RegisterLine* reg_line = reg_table_.GetLine(0);
int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
size_t expected_args = code_item_->ins_size_; /* long/double count as two */
DCHECK_GE(arg_start, 0); /* should have been verified earlier */
//Include the "this" pointer.
size_t cur_arg = 0;
if (!method_->IsStatic()) {
// If this is a constructor for a class other than java.lang.Object, mark the first ("this")
// argument as uninitialized. This restricts field access until the superclass constructor is
// called.
Class* declaring_class = method_->GetDeclaringClass();
if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
reg_line->SetRegisterType(arg_start + cur_arg,
reg_types_.UninitializedThisArgument(declaring_class));
} else {
reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
}
cur_arg++;
}
const DexFile::ProtoId& proto_id =
dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
DexFileParameterIterator iterator(*dex_file_, proto_id);
for (; iterator.HasNext(); iterator.Next()) {
const char* descriptor = iterator.GetDescriptor();
if (descriptor == NULL) {
LOG(FATAL) << "Null descriptor";
}
if (cur_arg >= expected_args) {
Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
<< " args, found more (" << descriptor << ")";
return false;
}
switch (descriptor[0]) {
case 'L':
case '[':
// We assume that reference arguments are initialized. The only way it could be otherwise
// (assuming the caller was verified) is if the current method is <init>, but in that case
// it's effectively considered initialized the instant we reach here (in the sense that we
// can return without doing anything or call virtual methods).
{
const RegType& reg_type =
reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
}
break;
case 'Z':
reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
break;
case 'C':
reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
break;
case 'B':
reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
break;
case 'I':
reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
break;
case 'S':
reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
break;
case 'F':
reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
break;
case 'J':
case 'D': {
const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
cur_arg++;
break;
}
default:
Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
return false;
}
cur_arg++;
}
if (cur_arg != expected_args) {
Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
return false;
}
const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
// Validate return type. We don't do the type lookup; just want to make sure that it has the right
// format. Only major difference from the method argument format is that 'V' is supported.
bool result;
if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
result = descriptor[1] == '\0';
} else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
size_t i = 0;
do {
i++;
} while (descriptor[i] == '['); // process leading [
if (descriptor[i] == 'L') { // object array
do {
i++; // find closing ;
} while (descriptor[i] != ';' && descriptor[i] != '\0');
result = descriptor[i] == ';';
} else { // primitive array
result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
}
} else if (descriptor[0] == 'L') {
// could be more thorough here, but shouldn't be required
size_t i = 0;
do {
i++;
} while (descriptor[i] != ';' && descriptor[i] != '\0');
result = descriptor[i] == ';';
} else {
result = false;
}
if (!result) {
Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
<< descriptor << "'";
}
return result;
}
bool DexVerifier::CodeFlowVerifyMethod() {
const uint16_t* insns = code_item_->insns_;
const uint32_t insns_size = code_item_->insns_size_in_code_units_;
/* Begin by marking the first instruction as "changed". */
insn_flags_[0].SetChanged();
uint32_t start_guess = 0;
/* Continue until no instructions are marked "changed". */
while (true) {
// Find the first marked one. Use "start_guess" as a way to find one quickly.
uint32_t insn_idx = start_guess;
for (; insn_idx < insns_size; insn_idx++) {
if (insn_flags_[insn_idx].IsChanged())
break;
}
if (insn_idx == insns_size) {
if (start_guess != 0) {
/* try again, starting from the top */
start_guess = 0;
continue;
} else {
/* all flags are clear */
break;
}
}
// We carry the working set of registers from instruction to instruction. If this address can
// be the target of a branch (or throw) instruction, or if we're skipping around chasing
// "changed" flags, we need to load the set of registers from the table.
// Because we always prefer to continue on to the next instruction, we should never have a
// situation where we have a stray "changed" flag set on an instruction that isn't a branch
// target.
work_insn_idx_ = insn_idx;
if (insn_flags_[insn_idx].IsBranchTarget()) {
work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
} else {
#ifndef NDEBUG
/*
* Sanity check: retrieve the stored register line (assuming
* a full table) and make sure it actually matches.
*/
RegisterLine* register_line = reg_table_.GetLine(insn_idx);
if (register_line != NULL) {
if (work_line_->CompareLine(register_line) != 0) {
Dump(std::cout);
std::cout << info_messages_.str();
LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
<< "@" << (void*)work_insn_idx_ << std::endl
<< " work_line=" << *work_line_ << std::endl
<< " expected=" << *register_line;
}
}
#endif
}
if (!CodeFlowVerifyInstruction(&start_guess)) {
fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
return false;
}
/* Clear "changed" and mark as visited. */
insn_flags_[insn_idx].SetVisited();
insn_flags_[insn_idx].ClearChanged();
}
if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
/*
* Scan for dead code. There's nothing "evil" about dead code
* (besides the wasted space), but it indicates a flaw somewhere
* down the line, possibly in the verifier.
*
* If we've substituted "always throw" instructions into the stream,
* we are almost certainly going to have some dead code.
*/
int dead_start = -1;
uint32_t insn_idx = 0;
for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
/*
* Switch-statement data doesn't get "visited" by scanner. It
* may or may not be preceded by a padding NOP (for alignment).
*/
if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
insns[insn_idx] == Instruction::kSparseSwitchSignature ||
insns[insn_idx] == Instruction::kArrayDataSignature ||
(insns[insn_idx] == Instruction::NOP &&
(insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
insn_flags_[insn_idx].SetVisited();
}
if (!insn_flags_[insn_idx].IsVisited()) {
if (dead_start < 0)
dead_start = insn_idx;
} else if (dead_start >= 0) {
LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
dead_start = -1;
}
}
if (dead_start >= 0) {
LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
}
}
return true;
}
bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
#ifdef VERIFIER_STATS
if (CurrentInsnFlags().IsVisited()) {
gDvm.verifierStats.instrsReexamined++;
} else {
gDvm.verifierStats.instrsExamined++;
}
#endif
/*
* Once we finish decoding the instruction, we need to figure out where
* we can go from here. There are three possible ways to transfer
* control to another statement:
*
* (1) Continue to the next instruction. Applies to all but
* unconditional branches, method returns, and exception throws.
* (2) Branch to one or more possible locations. Applies to branches
* and switch statements.
* (3) Exception handlers. Applies to any instruction that can
* throw an exception that is handled by an encompassing "try"
* block.
*
* We can also return, in which case there is no successor instruction
* from this point.
*
* The behavior can be determined from the OpcodeFlags.
*/
const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
const Instruction* inst = Instruction::At(insns);
Instruction::DecodedInstruction dec_insn(inst);
int opcode_flag = inst->Flag();
int32_t branch_target = 0;
bool just_set_result = false;
if (gDebugVerify) {
// Generate processing back trace to debug verifier
LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
<< *work_line_.get() << std::endl;
}
/*
* Make a copy of the previous register state. If the instruction
* can throw an exception, we will copy/merge this into the "catch"
* address rather than work_line, because we don't want the result
* from the "successful" code path (e.g. a check-cast that "improves"
* a type) to be visible to the exception handler.
*/
if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
saved_line_->CopyFromLine(work_line_.get());
} else {
#ifndef NDEBUG
saved_line_->FillWithGarbage();
#endif
}
switch (dec_insn.opcode_) {
case Instruction::NOP:
/*
* A "pure" NOP has no effect on anything. Data tables start with
* a signature that looks like a NOP; if we see one of these in
* the course of executing code then we have a problem.
*/
if (dec_insn.vA_ != 0) {
Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
}
break;
case Instruction::MOVE:
case Instruction::MOVE_FROM16:
case Instruction::MOVE_16:
work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
break;
case Instruction::MOVE_WIDE:
case Instruction::MOVE_WIDE_FROM16:
case Instruction::MOVE_WIDE_16:
work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
break;
case Instruction::MOVE_OBJECT:
case Instruction::MOVE_OBJECT_FROM16:
case Instruction::MOVE_OBJECT_16:
work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
break;
/*
* The move-result instructions copy data out of a "pseudo-register"
* with the results from the last method invocation. In practice we
* might want to hold the result in an actual CPU register, so the
* Dalvik spec requires that these only appear immediately after an
* invoke or filled-new-array.
*
* These calls invalidate the "result" register. (This is now
* redundant with the reset done below, but it can make the debug info
* easier to read in some cases.)
*/
case Instruction::MOVE_RESULT:
work_line_->CopyResultRegister1(dec_insn.vA_, false);
break;
case Instruction::MOVE_RESULT_WIDE:
work_line_->CopyResultRegister2(dec_insn.vA_);
break;
case Instruction::MOVE_RESULT_OBJECT:
work_line_->CopyResultRegister1(dec_insn.vA_, true);
break;
case Instruction::MOVE_EXCEPTION: {
/*
* This statement can only appear as the first instruction in an exception handler (though not
* all exception handlers need to have one of these). We verify that as part of extracting the
* exception type from the catch block list.
*/
const RegType& res_type = GetCaughtExceptionType();
work_line_->SetRegisterType(dec_insn.vA_, res_type);
break;
}
case Instruction::RETURN_VOID:
if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
if (!GetMethodReturnType().IsUnknown()) {
Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
}
}
break;
case Instruction::RETURN:
if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
/* check the method signature */
const RegType& return_type = GetMethodReturnType();
if (!return_type.IsCategory1Types()) {
Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
} else {
// Compilers may generate synthetic functions that write byte values into boolean fields.
// Also, it may use integer values for boolean, byte, short, and character return types.
const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
((return_type.IsBoolean() || return_type.IsByte() ||
return_type.IsShort() || return_type.IsChar()) &&
src_type.IsInteger()));
/* check the register contents */
work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
if (failure_ != VERIFY_ERROR_NONE) {
fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
}
}
}
break;
case Instruction::RETURN_WIDE:
if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
/* check the method signature */
const RegType& return_type = GetMethodReturnType();
if (!return_type.IsCategory2Types()) {
Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
} else {
/* check the register contents */
work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
if (failure_ != VERIFY_ERROR_NONE) {
fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
}
}
}
break;
case Instruction::RETURN_OBJECT:
if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
const RegType& return_type = GetMethodReturnType();
if (!return_type.IsReferenceTypes()) {
Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
} else {
/* return_type is the *expected* return type, not register value */
DCHECK(!return_type.IsZero());
DCHECK(!return_type.IsUninitializedReference());
const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
// Disallow returning uninitialized values and verify that the reference in vAA is an
// instance of the "return_type"
if (reg_type.IsUninitializedTypes()) {
Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
} else if (!return_type.IsAssignableFrom(reg_type)) {
Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
<< "', but expected from declaration '" << return_type << "'";
}
}
}
break;
case Instruction::CONST_4:
case Instruction::CONST_16:
case Instruction::CONST:
/* could be boolean, int, float, or a null reference */
work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
break;
case Instruction::CONST_HIGH16:
/* could be boolean, int, float, or a null reference */
work_line_->SetRegisterType(dec_insn.vA_,
reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
break;
case Instruction::CONST_WIDE_16:
case Instruction::CONST_WIDE_32:
case Instruction::CONST_WIDE:
case Instruction::CONST_WIDE_HIGH16:
/* could be long or double; resolved upon use */
work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
break;
case Instruction::CONST_STRING:
case Instruction::CONST_STRING_JUMBO:
work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
break;
case Instruction::CONST_CLASS: {
// Get type from instruction if unresolved then we need an access check
// TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
// Register holds class, ie its type is class, but on error we keep it Unknown
work_line_->SetRegisterType(dec_insn.vA_,
res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
break;
}
case Instruction::MONITOR_ENTER:
work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
break;
case Instruction::MONITOR_EXIT:
/*
* monitor-exit instructions are odd. They can throw exceptions,
* but when they do they act as if they succeeded and the PC is
* pointing to the following instruction. (This behavior goes back
* to the need to handle asynchronous exceptions, a now-deprecated
* feature that Dalvik doesn't support.)
*
* In practice we don't need to worry about this. The only
* exceptions that can be thrown from monitor-exit are for a
* null reference and -exit without a matching -enter. If the
* structured locking checks are working, the former would have
* failed on the -enter instruction, and the latter is impossible.
*
* This is fortunate, because issue 3221411 prevents us from
* chasing the "can throw" path when monitor verification is
* enabled. If we can fully verify the locking we can ignore
* some catch blocks (which will show up as "dead" code when
* we skip them here); if we can't, then the code path could be
* "live" so we still need to check it.
*/
opcode_flag &= ~Instruction::kThrow;
work_line_->PopMonitor(dec_insn.vA_);
break;
case Instruction::CHECK_CAST:
case Instruction::INSTANCE_OF: {
/*
* If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
* could be a "upcast" -- not expected, so we don't try to address it.)
*
* If it fails, an exception is thrown, which we deal with later by ignoring the update to
* dec_insn.vA_ when branching to a handler.
*/
bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
const RegType& res_type =
ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
if (res_type.IsUnknown()) {
CHECK_NE(failure_, VERIFY_ERROR_NONE);
break; // couldn't resolve class
}
// TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
const RegType& orig_type =
work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
if (!res_type.IsNonZeroReferenceTypes()) {
Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
} else if (!orig_type.IsReferenceTypes()) {
Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
} else {
if (is_checkcast) {
work_line_->SetRegisterType(dec_insn.vA_, res_type);
} else {
work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
}
}
break;
}
case Instruction::ARRAY_LENGTH: {
const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
if (res_type.IsReferenceTypes()) {
if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
} else {
work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
}
}
break;
}
case Instruction::NEW_INSTANCE: {
const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
// TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
// can't create an instance of an interface or abstract class */
if (!res_type.IsInstantiableTypes()) {
Fail(VERIFY_ERROR_INSTANTIATION)
<< "new-instance on primitive, interface or abstract class" << res_type;
} else {
const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
// Any registers holding previous allocations from this address that have not yet been
// initialized must be marked invalid.
work_line_->MarkUninitRefsAsInvalid(uninit_type);
// add the new uninitialized reference to the register state
work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
}
break;
}
case Instruction::NEW_ARRAY: {
const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
// TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
if (!res_type.IsArrayClass()) {
Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
} else {
/* make sure "size" register is valid type */
work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
/* set register type to array class */
work_line_->SetRegisterType(dec_insn.vA_, res_type);
}
break;
}
case Instruction::FILLED_NEW_ARRAY:
case Instruction::FILLED_NEW_ARRAY_RANGE: {
const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
// TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
if (!res_type.IsArrayClass()) {
Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
} else {
bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
/* check the arguments to the instruction */
VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
/* filled-array result goes into "result" register */
work_line_->SetResultRegisterType(res_type);
just_set_result = true;
}
break;
}
case Instruction::CMPL_FLOAT:
case Instruction::CMPG_FLOAT:
work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
break;
case Instruction::CMPL_DOUBLE:
case Instruction::CMPG_DOUBLE:
work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
break;
case Instruction::CMP_LONG:
work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
break;
case Instruction::THROW: {
const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
}
break;
}
case Instruction::GOTO:
case Instruction::GOTO_16:
case Instruction::GOTO_32:
/* no effect on or use of registers */
break;
case Instruction::PACKED_SWITCH:
case Instruction::SPARSE_SWITCH:
/* verify that vAA is an integer, or can be converted to one */
work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
break;
case Instruction::FILL_ARRAY_DATA: {
/* Similar to the verification done for APUT */
Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
if (failure_ == VERIFY_ERROR_NONE) {
/* res_class can be null if the reg type is Zero */
if (res_class != NULL) {
Class* component_type = res_class->GetComponentType();
if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
component_type->IsPrimitiveVoid()) {
Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
<< PrettyDescriptor(res_class);
} else {
const RegType& value_type = reg_types_.FromClass(component_type);
DCHECK(!value_type.IsUnknown());
// Now verify if the element width in the table matches the element width declared in
// the array
const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
if (array_data[0] != Instruction::kArrayDataSignature) {
Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
} else {
size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
// Since we don't compress the data in Dex, expect to see equal width of data stored
// in the table and expected from the array class.
if (array_data[1] != elem_width) {
Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
<< " vs " << elem_width << ")";
}
}
}
}
}
break;
}
case Instruction::IF_EQ:
case Instruction::IF_NE: {
const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
bool mismatch = false;
if (reg_type1.IsZero()) { // zero then integral or reference expected
mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
} else if (reg_type1.IsReferenceTypes()) { // both references?
mismatch = !reg_type2.IsReferenceTypes();
} else { // both integral?
mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
}
if (mismatch) {
Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
<< ") must both be references or integral";
}
break;
}
case Instruction::IF_LT:
case Instruction::IF_GE:
case Instruction::IF_GT:
case Instruction::IF_LE: {
const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
<< reg_type2 << ") must be integral";
}
break;
}
case Instruction::IF_EQZ:
case Instruction::IF_NEZ: {
const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
}
break;
}
case Instruction::IF_LTZ:
case Instruction::IF_GEZ:
case Instruction::IF_GTZ:
case Instruction::IF_LEZ: {
const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
if (!reg_type.IsIntegralTypes()) {
Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
<< " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
}
break;
}
case Instruction::AGET_BOOLEAN:
VerifyAGet(dec_insn, reg_types_.Boolean(), true);
break;
case Instruction::AGET_BYTE:
VerifyAGet(dec_insn, reg_types_.Byte(), true);
break;
case Instruction::AGET_CHAR:
VerifyAGet(dec_insn, reg_types_.Char(), true);
break;
case Instruction::AGET_SHORT:
VerifyAGet(dec_insn, reg_types_.Short(), true);
break;
case Instruction::AGET:
VerifyAGet(dec_insn, reg_types_.Integer(), true);
break;
case Instruction::AGET_WIDE:
VerifyAGet(dec_insn, reg_types_.Long(), true);
break;
case Instruction::AGET_OBJECT:
VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
break;
case Instruction::APUT_BOOLEAN:
VerifyAPut(dec_insn, reg_types_.Boolean(), true);
break;
case Instruction::APUT_BYTE:
VerifyAPut(dec_insn, reg_types_.Byte(), true);
break;
case Instruction::APUT_CHAR:
VerifyAPut(dec_insn, reg_types_.Char(), true);
break;
case Instruction::APUT_SHORT:
VerifyAPut(dec_insn, reg_types_.Short(), true);
break;
case Instruction::APUT:
VerifyAPut(dec_insn, reg_types_.Integer(), true);
break;
case Instruction::APUT_WIDE:
VerifyAPut(dec_insn, reg_types_.Long(), true);
break;
case Instruction::APUT_OBJECT:
VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
break;
case Instruction::IGET_BOOLEAN:
VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
break;
case Instruction::IGET_BYTE:
VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
break;
case Instruction::IGET_CHAR:
VerifyISGet(dec_insn, reg_types_.Char(), true, false);
break;
case Instruction::IGET_SHORT:
VerifyISGet(dec_insn, reg_types_.Short(), true, false);
break;
case Instruction::IGET:
VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
break;
case Instruction::IGET_WIDE:
VerifyISGet(dec_insn, reg_types_.Long(), true, false);
break;
case Instruction::IGET_OBJECT:
VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
break;
case Instruction::IPUT_BOOLEAN:
VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
break;
case Instruction::IPUT_BYTE:
VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
break;
case Instruction::IPUT_CHAR:
VerifyISPut(dec_insn, reg_types_.Char(), true, false);
break;
case Instruction::IPUT_SHORT:
VerifyISPut(dec_insn, reg_types_.Short(), true, false);
break;
case Instruction::IPUT:
VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
break;
case Instruction::IPUT_WIDE:
VerifyISPut(dec_insn, reg_types_.Long(), true, false);
break;
case Instruction::IPUT_OBJECT:
VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);