oat_file.cc revision 66d1caf42c20efba8305efb3a819993126e8abbf
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "oat_file.h" 18 19#include <dlfcn.h> 20#include <sstream> 21#include <string.h> 22 23#include "base/bit_vector.h" 24#include "base/stl_util.h" 25#include "base/unix_file/fd_file.h" 26#include "elf_file.h" 27#include "oat.h" 28#include "mirror/art_method.h" 29#include "mirror/art_method-inl.h" 30#include "mirror/class.h" 31#include "mirror/object-inl.h" 32#include "os.h" 33#include "runtime.h" 34#include "utils.h" 35#include "vmap_table.h" 36 37namespace art { 38 39void OatFile::CheckLocation(const std::string& location) { 40 CHECK(!location.empty()); 41} 42 43OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents, 44 const std::string& location, 45 std::string* error_msg) { 46 CHECK(!oat_contents.empty()) << location; 47 CheckLocation(location); 48 std::unique_ptr<OatFile> oat_file(new OatFile(location)); 49 oat_file->begin_ = &oat_contents[0]; 50 oat_file->end_ = &oat_contents[oat_contents.size()]; 51 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr; 52} 53 54OatFile* OatFile::Open(const std::string& filename, 55 const std::string& location, 56 byte* requested_base, 57 bool executable, 58 std::string* error_msg) { 59 CHECK(!filename.empty()) << location; 60 CheckLocation(filename); 61 std::unique_ptr<OatFile> ret; 62 if (kUsePortableCompiler && executable) { 63 // If we are using PORTABLE, use dlopen to deal with relocations. 64 // 65 // We use our own ELF loader for Quick to deal with legacy apps that 66 // open a generated dex file by name, remove the file, then open 67 // another generated dex file with the same name. http://b/10614658 68 ret.reset(OpenDlopen(filename, location, requested_base, error_msg)); 69 } else { 70 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: 71 // 72 // On target, dlopen may fail when compiling due to selinux restrictions on installd. 73 // 74 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. 75 // This won't work for portable runtime execution because it doesn't process relocations. 76 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str())); 77 if (file.get() == NULL) { 78 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno)); 79 return nullptr; 80 } 81 ret.reset(OpenElfFile(file.get(), location, requested_base, false, executable, error_msg)); 82 } 83 return ret.release(); 84} 85 86OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) { 87 CheckLocation(location); 88 return OpenElfFile(file, location, NULL, true, false, error_msg); 89} 90 91OatFile* OatFile::OpenDlopen(const std::string& elf_filename, 92 const std::string& location, 93 byte* requested_base, 94 std::string* error_msg) { 95 std::unique_ptr<OatFile> oat_file(new OatFile(location)); 96 bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg); 97 if (!success) { 98 return nullptr; 99 } 100 return oat_file.release(); 101} 102 103OatFile* OatFile::OpenElfFile(File* file, 104 const std::string& location, 105 byte* requested_base, 106 bool writable, 107 bool executable, 108 std::string* error_msg) { 109 std::unique_ptr<OatFile> oat_file(new OatFile(location)); 110 bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg); 111 if (!success) { 112 CHECK(!error_msg->empty()); 113 return nullptr; 114 } 115 return oat_file.release(); 116} 117 118OatFile::OatFile(const std::string& location) 119 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) { 120 CHECK(!location_.empty()); 121} 122 123OatFile::~OatFile() { 124 for (auto it : oat_dex_files_) { 125 delete it.first.data(); 126 } 127 STLDeleteValues(&oat_dex_files_); 128 if (dlopen_handle_ != NULL) { 129 dlclose(dlopen_handle_); 130 } 131} 132 133bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base, 134 std::string* error_msg) { 135 char* absolute_path = realpath(elf_filename.c_str(), NULL); 136 if (absolute_path == NULL) { 137 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str()); 138 return false; 139 } 140 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW); 141 free(absolute_path); 142 if (dlopen_handle_ == NULL) { 143 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror()); 144 return false; 145 } 146 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata")); 147 if (begin_ == NULL) { 148 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(), 149 dlerror()); 150 return false; 151 } 152 if (requested_base != NULL && begin_ != requested_base) { 153 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " 154 "oatdata=%p != expected=%p /proc/self/maps:\n", 155 begin_, requested_base); 156 ReadFileToString("/proc/self/maps", error_msg); 157 return false; 158 } 159 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword")); 160 if (end_ == NULL) { 161 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(), 162 dlerror()); 163 return false; 164 } 165 // Readjust to be non-inclusive upper bound. 166 end_ += sizeof(uint32_t); 167 return Setup(error_msg); 168} 169 170bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable, 171 std::string* error_msg) { 172 elf_file_.reset(ElfFile::Open(file, writable, true, error_msg)); 173 if (elf_file_.get() == nullptr) { 174 DCHECK(!error_msg->empty()); 175 return false; 176 } 177 bool loaded = elf_file_->Load(executable, error_msg); 178 if (!loaded) { 179 DCHECK(!error_msg->empty()); 180 return false; 181 } 182 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata"); 183 if (begin_ == NULL) { 184 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str()); 185 return false; 186 } 187 if (requested_base != NULL && begin_ != requested_base) { 188 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " 189 "oatdata=%p != expected=%p /proc/self/maps:\n", 190 begin_, requested_base); 191 ReadFileToString("/proc/self/maps", error_msg); 192 return false; 193 } 194 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword"); 195 if (end_ == NULL) { 196 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str()); 197 return false; 198 } 199 // Readjust to be non-inclusive upper bound. 200 end_ += sizeof(uint32_t); 201 return Setup(error_msg); 202} 203 204bool OatFile::Setup(std::string* error_msg) { 205 if (!GetOatHeader().IsValid()) { 206 *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str()); 207 return false; 208 } 209 const byte* oat = Begin(); 210 oat += sizeof(OatHeader); 211 if (oat > End()) { 212 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str()); 213 return false; 214 } 215 216 oat += GetOatHeader().GetKeyValueStoreSize(); 217 if (oat > End()) { 218 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: " 219 "%p + %zd + %ud <= %p", GetLocation().c_str(), 220 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(), 221 End()); 222 return false; 223 } 224 225 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) { 226 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); 227 if (UNLIKELY(dex_file_location_size == 0U)) { 228 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name", 229 GetLocation().c_str(), i); 230 return false; 231 } 232 oat += sizeof(dex_file_location_size); 233 if (UNLIKELY(oat > End())) { 234 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file " 235 "location size", GetLocation().c_str(), i); 236 return false; 237 } 238 239 const char* dex_file_location_data = reinterpret_cast<const char*>(oat); 240 oat += dex_file_location_size; 241 if (UNLIKELY(oat > End())) { 242 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file " 243 "location", GetLocation().c_str(), i); 244 return false; 245 } 246 247 std::string dex_file_location(dex_file_location_data, dex_file_location_size); 248 249 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); 250 oat += sizeof(dex_file_checksum); 251 if (UNLIKELY(oat > End())) { 252 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after " 253 "dex file checksum", GetLocation().c_str(), i, 254 dex_file_location.c_str()); 255 return false; 256 } 257 258 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); 259 if (UNLIKELY(dex_file_offset == 0U)) { 260 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex " 261 "file offset", GetLocation().c_str(), i, dex_file_location.c_str()); 262 return false; 263 } 264 if (UNLIKELY(dex_file_offset > Size())) { 265 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file " 266 "offset %ud > %zd", GetLocation().c_str(), i, 267 dex_file_location.c_str(), dex_file_offset, Size()); 268 return false; 269 } 270 oat += sizeof(dex_file_offset); 271 if (UNLIKELY(oat > End())) { 272 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " 273 " after dex file offsets", GetLocation().c_str(), i, 274 dex_file_location.c_str()); 275 return false; 276 } 277 278 const uint8_t* dex_file_pointer = Begin() + dex_file_offset; 279 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) { 280 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid " 281 " dex file magic '%s'", GetLocation().c_str(), i, 282 dex_file_location.c_str(), dex_file_pointer); 283 return false; 284 } 285 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) { 286 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid " 287 " dex file version '%s'", GetLocation().c_str(), i, 288 dex_file_location.c_str(), dex_file_pointer); 289 return false; 290 } 291 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); 292 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); 293 294 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); 295 if (UNLIKELY(oat > End())) { 296 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated " 297 " method offsets", GetLocation().c_str(), i, 298 dex_file_location.c_str()); 299 return false; 300 } 301 302 OatDexFile* oat_dex_file = new OatDexFile(this, 303 dex_file_location, 304 dex_file_checksum, 305 dex_file_pointer, 306 methods_offsets_pointer); 307 308 std::string dex_canonical_location_str = DexFile::GetDexCanonicalLocation(dex_file_location.c_str()); 309 // make a copy since we need to persist it as a key in the object's field. 310 int location_size = dex_canonical_location_str.size() + 1; 311 char* dex_canonical_location = new char[location_size ]; 312 strncpy(dex_canonical_location, dex_canonical_location_str.c_str(), location_size); 313 314 StringPiece key(dex_canonical_location); 315 oat_dex_files_.Put(key, oat_dex_file); 316 } 317 return true; 318} 319 320const OatHeader& OatFile::GetOatHeader() const { 321 return *reinterpret_cast<const OatHeader*>(Begin()); 322} 323 324const byte* OatFile::Begin() const { 325 CHECK(begin_ != NULL); 326 return begin_; 327} 328 329const byte* OatFile::End() const { 330 CHECK(end_ != NULL); 331 return end_; 332} 333 334const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location, 335 const uint32_t* dex_location_checksum, 336 bool warn_if_not_found) const { 337 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); 338 339 Table::const_iterator it = oat_dex_files_.find(dex_canonical_location); 340 if (it != oat_dex_files_.end()) { 341 const OatFile::OatDexFile* oat_dex_file = it->second; 342 if (dex_location_checksum == NULL || 343 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) { 344 return oat_dex_file; 345 } 346 } 347 348 if (warn_if_not_found) { 349 std::string checksum("<unspecified>"); 350 if (dex_location_checksum != NULL) { 351 checksum = StringPrintf("0x%08x", *dex_location_checksum); 352 } 353 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location 354 << " ( canonical path " << dex_canonical_location << ")" 355 << " with checksum " << checksum << " in OatFile " << GetLocation(); 356 if (kIsDebugBuild) { 357 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) { 358 LOG(WARNING) << "OatFile " << GetLocation() 359 << " contains OatDexFile " << it->second->GetDexFileLocation() 360 << " (canonical path " << it->first << ")" 361 << " with checksum 0x" << std::hex << it->second->GetDexFileLocationChecksum(); 362 } 363 } 364 } 365 366 return NULL; 367} 368 369std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const { 370 std::vector<const OatFile::OatDexFile*> result; 371 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) { 372 result.push_back(it->second); 373 } 374 return result; 375} 376 377OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, 378 const std::string& dex_file_location, 379 uint32_t dex_file_location_checksum, 380 const byte* dex_file_pointer, 381 const uint32_t* oat_class_offsets_pointer) 382 : oat_file_(oat_file), 383 dex_file_location_(dex_file_location), 384 dex_file_location_checksum_(dex_file_location_checksum), 385 dex_file_pointer_(dex_file_pointer), 386 oat_class_offsets_pointer_(oat_class_offsets_pointer) {} 387 388OatFile::OatDexFile::~OatDexFile() {} 389 390size_t OatFile::OatDexFile::FileSize() const { 391 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; 392} 393 394const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const { 395 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_, 396 dex_file_location_checksum_, error_msg); 397} 398 399OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const { 400 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index]; 401 402 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset; 403 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation(); 404 405 const byte* status_pointer = oat_class_pointer; 406 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation(); 407 mirror::Class::Status status = 408 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer)); 409 CHECK_LT(status, mirror::Class::kStatusMax); 410 411 const byte* type_pointer = status_pointer + sizeof(uint16_t); 412 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation(); 413 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer)); 414 CHECK_LT(type, kOatClassMax); 415 416 const byte* after_type_pointer = type_pointer + sizeof(int16_t); 417 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation(); 418 419 uint32_t bitmap_size = 0; 420 const byte* bitmap_pointer = nullptr; 421 const byte* methods_pointer = nullptr; 422 if (type == kOatClassSomeCompiled) { 423 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer)); 424 bitmap_pointer = after_type_pointer + sizeof(bitmap_size); 425 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation(); 426 methods_pointer = bitmap_pointer + bitmap_size; 427 } else { 428 methods_pointer = after_type_pointer; 429 } 430 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); 431 432 return OatClass(oat_file_, 433 status, 434 type, 435 bitmap_size, 436 reinterpret_cast<const uint32_t*>(bitmap_pointer), 437 reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); 438} 439 440OatFile::OatClass::OatClass(const OatFile* oat_file, 441 mirror::Class::Status status, 442 OatClassType type, 443 uint32_t bitmap_size, 444 const uint32_t* bitmap_pointer, 445 const OatMethodOffsets* methods_pointer) 446 : oat_file_(oat_file), status_(status), type_(type), 447 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) { 448 CHECK(methods_pointer != nullptr); 449 switch (type_) { 450 case kOatClassAllCompiled: { 451 CHECK_EQ(0U, bitmap_size); 452 CHECK(bitmap_pointer == nullptr); 453 break; 454 } 455 case kOatClassSomeCompiled: { 456 CHECK_NE(0U, bitmap_size); 457 CHECK(bitmap_pointer != nullptr); 458 break; 459 } 460 case kOatClassNoneCompiled: { 461 CHECK_EQ(0U, bitmap_size); 462 CHECK(bitmap_pointer == nullptr); 463 methods_pointer_ = nullptr; 464 break; 465 } 466 case kOatClassMax: { 467 LOG(FATAL) << "Invalid OatClassType " << type_; 468 break; 469 } 470 } 471} 472 473const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { 474 // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index. 475 if (methods_pointer_ == NULL) { 476 CHECK_EQ(kOatClassNoneCompiled, type_); 477 return OatMethod(NULL, 0, 0); 478 } 479 size_t methods_pointer_index; 480 if (bitmap_ == NULL) { 481 CHECK_EQ(kOatClassAllCompiled, type_); 482 methods_pointer_index = method_index; 483 } else { 484 CHECK_EQ(kOatClassSomeCompiled, type_); 485 if (!BitVector::IsBitSet(bitmap_, method_index)) { 486 return OatMethod(NULL, 0, 0); 487 } 488 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index); 489 methods_pointer_index = num_set_bits; 490 } 491 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index]; 492 return OatMethod( 493 oat_file_->Begin(), 494 oat_method_offsets.code_offset_, 495 oat_method_offsets.gc_map_offset_); 496} 497 498OatFile::OatMethod::OatMethod(const byte* base, 499 const uint32_t code_offset, 500 const uint32_t gc_map_offset) 501 : begin_(base), 502 code_offset_(code_offset), 503 native_gc_map_offset_(gc_map_offset) { 504} 505 506OatFile::OatMethod::~OatMethod() {} 507 508 509uint32_t OatFile::OatMethod::GetQuickCodeSize() const { 510 uintptr_t code = reinterpret_cast<uintptr_t>(GetQuickCode()); 511 if (code == 0) { 512 return 0; 513 } 514 // TODO: make this Thumb2 specific 515 code &= ~0x1; 516 return reinterpret_cast<uint32_t*>(code)[-1]; 517} 518 519void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const { 520 CHECK(method != NULL); 521 method->SetEntryPointFromPortableCompiledCode(GetPortableCode()); 522 method->SetEntryPointFromQuickCompiledCode(GetQuickCode()); 523 method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode. 524} 525 526} // namespace art 527