1 /* 2 * Copyright (C) 2014 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 */ 16 17 #include "verified_method.h" 18 19 #include <algorithm> 20 #include <memory> 21 #include <vector> 22 23 #include "base/logging.h" 24 #include "base/stl_util.h" 25 #include "dex_file.h" 26 #include "dex_instruction.h" 27 #include "dex_instruction-inl.h" 28 #include "base/mutex.h" 29 #include "base/mutex-inl.h" 30 #include "mirror/art_method.h" 31 #include "mirror/art_method-inl.h" 32 #include "mirror/class.h" 33 #include "mirror/class-inl.h" 34 #include "mirror/dex_cache.h" 35 #include "mirror/dex_cache-inl.h" 36 #include "mirror/object.h" 37 #include "mirror/object-inl.h" 38 #include "verifier/dex_gc_map.h" 39 #include "verifier/method_verifier.h" 40 #include "verifier/method_verifier-inl.h" 41 #include "verifier/register_line.h" 42 #include "verifier/register_line-inl.h" 43 44 namespace art { 45 46 const VerifiedMethod* VerifiedMethod::Create(verifier::MethodVerifier* method_verifier, 47 bool compile) { 48 std::unique_ptr<VerifiedMethod> verified_method(new VerifiedMethod); 49 if (compile) { 50 /* Generate a register map. */ 51 if (!verified_method->GenerateGcMap(method_verifier)) { 52 CHECK(method_verifier->HasFailures()); 53 return nullptr; // Not a real failure, but a failure to encode. 54 } 55 if (kIsDebugBuild) { 56 VerifyGcMap(method_verifier, verified_method->dex_gc_map_); 57 } 58 59 // TODO: move this out when DEX-to-DEX supports devirtualization. 60 if (method_verifier->HasVirtualOrInterfaceInvokes()) { 61 verified_method->GenerateDevirtMap(method_verifier); 62 } 63 } 64 65 if (method_verifier->HasCheckCasts()) { 66 verified_method->GenerateSafeCastSet(method_verifier); 67 } 68 return verified_method.release(); 69 } 70 71 const MethodReference* VerifiedMethod::GetDevirtTarget(uint32_t dex_pc) const { 72 auto it = devirt_map_.find(dex_pc); 73 return (it != devirt_map_.end()) ? &it->second : nullptr; 74 } 75 76 bool VerifiedMethod::IsSafeCast(uint32_t pc) const { 77 return std::binary_search(safe_cast_set_.begin(), safe_cast_set_.end(), pc); 78 } 79 80 bool VerifiedMethod::GenerateGcMap(verifier::MethodVerifier* method_verifier) { 81 DCHECK(dex_gc_map_.empty()); 82 size_t num_entries, ref_bitmap_bits, pc_bits; 83 ComputeGcMapSizes(method_verifier, &num_entries, &ref_bitmap_bits, &pc_bits); 84 // There's a single byte to encode the size of each bitmap. 85 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) { 86 // TODO: either a better GC map format or per method failures 87 method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD) 88 << "Cannot encode GC map for method with " << ref_bitmap_bits << " registers"; 89 return false; 90 } 91 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8; 92 // There are 2 bytes to encode the number of entries. 93 if (num_entries >= 65536) { 94 // TODO: Either a better GC map format or per method failures. 95 method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD) 96 << "Cannot encode GC map for method with " << num_entries << " entries"; 97 return false; 98 } 99 size_t pc_bytes; 100 verifier::RegisterMapFormat format; 101 if (pc_bits <= 8) { 102 format = verifier::kRegMapFormatCompact8; 103 pc_bytes = 1; 104 } else if (pc_bits <= 16) { 105 format = verifier::kRegMapFormatCompact16; 106 pc_bytes = 2; 107 } else { 108 // TODO: Either a better GC map format or per method failures. 109 method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD) 110 << "Cannot encode GC map for method with " 111 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)"; 112 return false; 113 } 114 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4; 115 dex_gc_map_.reserve(table_size); 116 // Write table header. 117 dex_gc_map_.push_back(format | ((ref_bitmap_bytes & ~0xFF) >> 5)); 118 dex_gc_map_.push_back(ref_bitmap_bytes & 0xFF); 119 dex_gc_map_.push_back(num_entries & 0xFF); 120 dex_gc_map_.push_back((num_entries >> 8) & 0xFF); 121 // Write table data. 122 const DexFile::CodeItem* code_item = method_verifier->CodeItem(); 123 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) { 124 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) { 125 dex_gc_map_.push_back(i & 0xFF); 126 if (pc_bytes == 2) { 127 dex_gc_map_.push_back((i >> 8) & 0xFF); 128 } 129 verifier::RegisterLine* line = method_verifier->GetRegLine(i); 130 line->WriteReferenceBitMap(dex_gc_map_, ref_bitmap_bytes); 131 } 132 } 133 DCHECK_EQ(dex_gc_map_.size(), table_size); 134 return true; 135 } 136 137 void VerifiedMethod::VerifyGcMap(verifier::MethodVerifier* method_verifier, 138 const std::vector<uint8_t>& data) { 139 // Check that for every GC point there is a map entry, there aren't entries for non-GC points, 140 // that the table data is well formed and all references are marked (or not) in the bitmap. 141 verifier::DexPcToReferenceMap map(&data[0]); 142 DCHECK_EQ(data.size(), map.RawSize()); 143 size_t map_index = 0; 144 const DexFile::CodeItem* code_item = method_verifier->CodeItem(); 145 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) { 146 const uint8_t* reg_bitmap = map.FindBitMap(i, false); 147 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) { 148 DCHECK_LT(map_index, map.NumEntries()); 149 DCHECK_EQ(map.GetDexPc(map_index), i); 150 DCHECK_EQ(map.GetBitMap(map_index), reg_bitmap); 151 map_index++; 152 verifier::RegisterLine* line = method_verifier->GetRegLine(i); 153 for (size_t j = 0; j < code_item->registers_size_; j++) { 154 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) { 155 DCHECK_LT(j / 8, map.RegWidth()); 156 DCHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1); 157 } else if ((j / 8) < map.RegWidth()) { 158 DCHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0); 159 } else { 160 // If a register doesn't contain a reference then the bitmap may be shorter than the line. 161 } 162 } 163 } else { 164 DCHECK(reg_bitmap == NULL); 165 } 166 } 167 } 168 169 void VerifiedMethod::ComputeGcMapSizes(verifier::MethodVerifier* method_verifier, 170 size_t* gc_points, size_t* ref_bitmap_bits, 171 size_t* log2_max_gc_pc) { 172 size_t local_gc_points = 0; 173 size_t max_insn = 0; 174 size_t max_ref_reg = -1; 175 const DexFile::CodeItem* code_item = method_verifier->CodeItem(); 176 for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) { 177 if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) { 178 local_gc_points++; 179 max_insn = i; 180 verifier::RegisterLine* line = method_verifier->GetRegLine(i); 181 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg); 182 } 183 } 184 *gc_points = local_gc_points; 185 *ref_bitmap_bits = max_ref_reg + 1; // If max register is 0 we need 1 bit to encode (ie +1). 186 size_t i = 0; 187 while ((1U << i) <= max_insn) { 188 i++; 189 } 190 *log2_max_gc_pc = i; 191 } 192 193 void VerifiedMethod::GenerateDevirtMap(verifier::MethodVerifier* method_verifier) { 194 // It is risky to rely on reg_types for sharpening in cases of soft 195 // verification, we might end up sharpening to a wrong implementation. Just abort. 196 if (method_verifier->HasFailures()) { 197 return; 198 } 199 200 const DexFile::CodeItem* code_item = method_verifier->CodeItem(); 201 const uint16_t* insns = code_item->insns_; 202 const Instruction* inst = Instruction::At(insns); 203 const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_); 204 205 for (; inst < end; inst = inst->Next()) { 206 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) || 207 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE); 208 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) || 209 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); 210 211 if (!is_interface && !is_virtual) { 212 continue; 213 } 214 // Get reg type for register holding the reference to the object that will be dispatched upon. 215 uint32_t dex_pc = inst->GetDexPc(insns); 216 verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); 217 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) || 218 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); 219 verifier::RegType& 220 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c())); 221 222 if (!reg_type.HasClass()) { 223 // We will compute devirtualization information only when we know the Class of the reg type. 224 continue; 225 } 226 mirror::Class* reg_class = reg_type.GetClass(); 227 if (reg_class->IsInterface()) { 228 // We can't devirtualize when the known type of the register is an interface. 229 continue; 230 } 231 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) { 232 // We can't devirtualize abstract classes except on arrays of abstract classes. 233 continue; 234 } 235 mirror::ArtMethod* abstract_method = method_verifier->GetDexCache()->GetResolvedMethod( 236 is_range ? inst->VRegB_3rc() : inst->VRegB_35c()); 237 if (abstract_method == NULL) { 238 // If the method is not found in the cache this means that it was never found 239 // by ResolveMethodAndCheckAccess() called when verifying invoke_*. 240 continue; 241 } 242 // Find the concrete method. 243 mirror::ArtMethod* concrete_method = NULL; 244 if (is_interface) { 245 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method); 246 } 247 if (is_virtual) { 248 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method); 249 } 250 if (concrete_method == NULL || concrete_method->IsAbstract()) { 251 // In cases where concrete_method is not found, or is abstract, continue to the next invoke. 252 continue; 253 } 254 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() || 255 concrete_method->GetDeclaringClass()->IsFinal()) { 256 // If we knew exactly the class being dispatched upon, or if the target method cannot be 257 // overridden record the target to be used in the compiler driver. 258 MethodReference concrete_ref( 259 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(), 260 concrete_method->GetDexMethodIndex()); 261 devirt_map_.Put(dex_pc, concrete_ref); 262 } 263 } 264 } 265 266 void VerifiedMethod::GenerateSafeCastSet(verifier::MethodVerifier* method_verifier) { 267 /* 268 * Walks over the method code and adds any cast instructions in which 269 * the type cast is implicit to a set, which is used in the code generation 270 * to elide these casts. 271 */ 272 if (method_verifier->HasFailures()) { 273 return; 274 } 275 const DexFile::CodeItem* code_item = method_verifier->CodeItem(); 276 const Instruction* inst = Instruction::At(code_item->insns_); 277 const Instruction* end = Instruction::At(code_item->insns_ + 278 code_item->insns_size_in_code_units_); 279 280 for (; inst < end; inst = inst->Next()) { 281 Instruction::Code code = inst->Opcode(); 282 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) { 283 uint32_t dex_pc = inst->GetDexPc(code_item->insns_); 284 const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc); 285 bool is_safe_cast = false; 286 if (code == Instruction::CHECK_CAST) { 287 verifier::RegType& reg_type(line->GetRegisterType(inst->VRegA_21c())); 288 verifier::RegType& cast_type = 289 method_verifier->ResolveCheckedClass(inst->VRegB_21c()); 290 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type); 291 } else { 292 verifier::RegType& array_type(line->GetRegisterType(inst->VRegB_23x())); 293 // We only know its safe to assign to an array if the array type is precise. For example, 294 // an Object[] can have any type of object stored in it, but it may also be assigned a 295 // String[] in which case the stores need to be of Strings. 296 if (array_type.IsPreciseReference()) { 297 verifier::RegType& value_type(line->GetRegisterType(inst->VRegA_23x())); 298 verifier::RegType& component_type = method_verifier->GetRegTypeCache() 299 ->GetComponentType(array_type, method_verifier->GetClassLoader()); 300 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type); 301 } 302 } 303 if (is_safe_cast) { 304 // Verify ordering for push_back() to the sorted vector. 305 DCHECK(safe_cast_set_.empty() || safe_cast_set_.back() < dex_pc); 306 safe_cast_set_.push_back(dex_pc); 307 } 308 } 309 } 310 } 311 312 } // namespace art 313