oat_file.cc revision 4e1d579d6401fef2dd57b16f8d406e33221a69d9
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::OpenReadable(File* file, const std::string& location, std::string* error_msg) {
92  CheckLocation(location);
93  return OpenElfFile(file, location, NULL, false, false, error_msg);
94}
95
96OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
97                             const std::string& location,
98                             byte* requested_base,
99                             std::string* error_msg) {
100  std::unique_ptr<OatFile> oat_file(new OatFile(location));
101  bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
102  if (!success) {
103    return nullptr;
104  }
105  return oat_file.release();
106}
107
108OatFile* OatFile::OpenElfFile(File* file,
109                              const std::string& location,
110                              byte* requested_base,
111                              bool writable,
112                              bool executable,
113                              std::string* error_msg) {
114  std::unique_ptr<OatFile> oat_file(new OatFile(location));
115  bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg);
116  if (!success) {
117    CHECK(!error_msg->empty());
118    return nullptr;
119  }
120  return oat_file.release();
121}
122
123OatFile::OatFile(const std::string& location)
124    : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
125  CHECK(!location_.empty());
126}
127
128OatFile::~OatFile() {
129  for (auto it : oat_dex_files_) {
130     delete it.first.data();
131  }
132  STLDeleteValues(&oat_dex_files_);
133  if (dlopen_handle_ != NULL) {
134    dlclose(dlopen_handle_);
135  }
136}
137
138bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base,
139                     std::string* error_msg) {
140  char* absolute_path = realpath(elf_filename.c_str(), NULL);
141  if (absolute_path == NULL) {
142    *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
143    return false;
144  }
145  dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
146  free(absolute_path);
147  if (dlopen_handle_ == NULL) {
148    *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
149    return false;
150  }
151  begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
152  if (begin_ == NULL) {
153    *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
154                              dlerror());
155    return false;
156  }
157  if (requested_base != NULL && begin_ != requested_base) {
158    *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
159                              "oatdata=%p != expected=%p /proc/self/maps:\n",
160                              begin_, requested_base);
161    ReadFileToString("/proc/self/maps", error_msg);
162    return false;
163  }
164  end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
165  if (end_ == NULL) {
166    *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
167                              dlerror());
168    return false;
169  }
170  // Readjust to be non-inclusive upper bound.
171  end_ += sizeof(uint32_t);
172  return Setup(error_msg);
173}
174
175bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
176                          std::string* error_msg) {
177  elf_file_.reset(ElfFile::Open(file, writable, true, error_msg));
178  if (elf_file_.get() == nullptr) {
179    DCHECK(!error_msg->empty());
180    return false;
181  }
182  bool loaded = elf_file_->Load(executable, error_msg);
183  if (!loaded) {
184    DCHECK(!error_msg->empty());
185    return false;
186  }
187  begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
188  if (begin_ == NULL) {
189    *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
190    return false;
191  }
192  if (requested_base != NULL && begin_ != requested_base) {
193    *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
194                              "oatdata=%p != expected=%p /proc/self/maps:\n",
195                              begin_, requested_base);
196    ReadFileToString("/proc/self/maps", error_msg);
197    return false;
198  }
199  end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
200  if (end_ == NULL) {
201    *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
202    return false;
203  }
204  // Readjust to be non-inclusive upper bound.
205  end_ += sizeof(uint32_t);
206  return Setup(error_msg);
207}
208
209bool OatFile::Setup(std::string* error_msg) {
210  if (!GetOatHeader().IsValid()) {
211    *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str());
212    return false;
213  }
214  const byte* oat = Begin();
215  oat += sizeof(OatHeader);
216  if (oat > End()) {
217    *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
218    return false;
219  }
220
221  oat += GetOatHeader().GetKeyValueStoreSize();
222  if (oat > End()) {
223    *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
224                              "%p + %zd + %ud <= %p", GetLocation().c_str(),
225                              Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
226                              End());
227    return false;
228  }
229
230  for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
231    uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
232    if (UNLIKELY(dex_file_location_size == 0U)) {
233      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
234                                GetLocation().c_str(), i);
235      return false;
236    }
237    oat += sizeof(dex_file_location_size);
238    if (UNLIKELY(oat > End())) {
239      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
240                                "location size", GetLocation().c_str(), i);
241      return false;
242    }
243
244    const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
245    oat += dex_file_location_size;
246    if (UNLIKELY(oat > End())) {
247      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
248                                "location", GetLocation().c_str(), i);
249      return false;
250    }
251
252    std::string dex_file_location(dex_file_location_data, dex_file_location_size);
253
254    uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
255    oat += sizeof(dex_file_checksum);
256    if (UNLIKELY(oat > End())) {
257      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
258                                "dex file checksum", GetLocation().c_str(), i,
259                                dex_file_location.c_str());
260      return false;
261    }
262
263    uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
264    if (UNLIKELY(dex_file_offset == 0U)) {
265      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
266                                "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
267      return false;
268    }
269    if (UNLIKELY(dex_file_offset > Size())) {
270      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
271                                "offset %ud > %zd", GetLocation().c_str(), i,
272                                dex_file_location.c_str(), dex_file_offset, Size());
273      return false;
274    }
275    oat += sizeof(dex_file_offset);
276    if (UNLIKELY(oat > End())) {
277      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
278                                " after dex file offsets", GetLocation().c_str(), i,
279                                dex_file_location.c_str());
280      return false;
281    }
282
283    const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
284    if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
285      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
286                                " dex file magic '%s'", GetLocation().c_str(), i,
287                                dex_file_location.c_str(), dex_file_pointer);
288      return false;
289    }
290    if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
291      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
292                                " dex file version '%s'", GetLocation().c_str(), i,
293                                dex_file_location.c_str(), dex_file_pointer);
294      return false;
295    }
296    const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
297    const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
298
299    oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
300    if (UNLIKELY(oat > End())) {
301      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
302                                " method offsets", GetLocation().c_str(), i,
303                                dex_file_location.c_str());
304      return false;
305    }
306
307    OatDexFile* oat_dex_file = new OatDexFile(this,
308                                              dex_file_location,
309                                              dex_file_checksum,
310                                              dex_file_pointer,
311                                              methods_offsets_pointer);
312
313    std::string dex_canonical_location_str = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
314    // make a copy since we need to persist it as a key in the object's field.
315    int location_size = dex_canonical_location_str.size() + 1;
316    char* dex_canonical_location = new char[location_size ];
317    strncpy(dex_canonical_location, dex_canonical_location_str.c_str(), location_size);
318
319    StringPiece key(dex_canonical_location);
320    oat_dex_files_.Put(key, oat_dex_file);
321  }
322  return true;
323}
324
325const OatHeader& OatFile::GetOatHeader() const {
326  return *reinterpret_cast<const OatHeader*>(Begin());
327}
328
329const byte* OatFile::Begin() const {
330  CHECK(begin_ != NULL);
331  return begin_;
332}
333
334const byte* OatFile::End() const {
335  CHECK(end_ != NULL);
336  return end_;
337}
338
339const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
340                                                  const uint32_t* dex_location_checksum,
341                                                  bool warn_if_not_found) const {
342  std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
343
344  Table::const_iterator it = oat_dex_files_.find(dex_canonical_location);
345  if (it != oat_dex_files_.end()) {
346    const OatFile::OatDexFile* oat_dex_file = it->second;
347    if (dex_location_checksum == NULL ||
348        oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) {
349      return oat_dex_file;
350    }
351  }
352
353  if (warn_if_not_found) {
354    std::string checksum("<unspecified>");
355    if (dex_location_checksum != NULL) {
356      checksum = StringPrintf("0x%08x", *dex_location_checksum);
357    }
358    LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
359                 << " ( canonical path " << dex_canonical_location << ")"
360                 << " with checksum " << checksum << " in OatFile " << GetLocation();
361    if (kIsDebugBuild) {
362      for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
363        LOG(WARNING) << "OatFile " << GetLocation()
364                     << " contains OatDexFile " << it->second->GetDexFileLocation()
365                     << " (canonical path " << it->first << ")"
366                     << " with checksum 0x" << std::hex << it->second->GetDexFileLocationChecksum();
367      }
368    }
369  }
370
371  return NULL;
372}
373
374std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
375  std::vector<const OatFile::OatDexFile*> result;
376  for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
377    result.push_back(it->second);
378  }
379  return result;
380}
381
382OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
383                                const std::string& dex_file_location,
384                                uint32_t dex_file_location_checksum,
385                                const byte* dex_file_pointer,
386                                const uint32_t* oat_class_offsets_pointer)
387    : oat_file_(oat_file),
388      dex_file_location_(dex_file_location),
389      dex_file_location_checksum_(dex_file_location_checksum),
390      dex_file_pointer_(dex_file_pointer),
391      oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
392
393OatFile::OatDexFile::~OatDexFile() {}
394
395size_t OatFile::OatDexFile::FileSize() const {
396  return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
397}
398
399const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
400  return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
401                       dex_file_location_checksum_, error_msg);
402}
403
404OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
405  uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
406
407  const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
408  CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
409
410  const byte* status_pointer = oat_class_pointer;
411  CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
412  mirror::Class::Status status =
413      static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
414  CHECK_LT(status, mirror::Class::kStatusMax);
415
416  const byte* type_pointer = status_pointer + sizeof(uint16_t);
417  CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
418  OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
419  CHECK_LT(type, kOatClassMax);
420
421  const byte* after_type_pointer = type_pointer + sizeof(int16_t);
422  CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
423
424  uint32_t bitmap_size = 0;
425  const byte* bitmap_pointer = nullptr;
426  const byte* methods_pointer = nullptr;
427  if (type == kOatClassSomeCompiled) {
428    bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
429    bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
430    CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
431    methods_pointer = bitmap_pointer + bitmap_size;
432  } else {
433    methods_pointer = after_type_pointer;
434  }
435  CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
436
437  return OatClass(oat_file_,
438                  status,
439                  type,
440                  bitmap_size,
441                  reinterpret_cast<const uint32_t*>(bitmap_pointer),
442                  reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
443}
444
445OatFile::OatClass::OatClass(const OatFile* oat_file,
446                            mirror::Class::Status status,
447                            OatClassType type,
448                            uint32_t bitmap_size,
449                            const uint32_t* bitmap_pointer,
450                            const OatMethodOffsets* methods_pointer)
451    : oat_file_(oat_file), status_(status), type_(type),
452      bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
453    CHECK(methods_pointer != nullptr);
454    switch (type_) {
455      case kOatClassAllCompiled: {
456        CHECK_EQ(0U, bitmap_size);
457        CHECK(bitmap_pointer == nullptr);
458        break;
459      }
460      case kOatClassSomeCompiled: {
461        CHECK_NE(0U, bitmap_size);
462        CHECK(bitmap_pointer != nullptr);
463        break;
464      }
465      case kOatClassNoneCompiled: {
466        CHECK_EQ(0U, bitmap_size);
467        CHECK(bitmap_pointer == nullptr);
468        methods_pointer_ = nullptr;
469        break;
470      }
471      case kOatClassMax: {
472        LOG(FATAL) << "Invalid OatClassType " << type_;
473        break;
474      }
475    }
476}
477
478const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
479  // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index.
480  if (methods_pointer_ == NULL) {
481    CHECK_EQ(kOatClassNoneCompiled, type_);
482    return OatMethod(NULL, 0, 0);
483  }
484  size_t methods_pointer_index;
485  if (bitmap_ == NULL) {
486    CHECK_EQ(kOatClassAllCompiled, type_);
487    methods_pointer_index = method_index;
488  } else {
489    CHECK_EQ(kOatClassSomeCompiled, type_);
490    if (!BitVector::IsBitSet(bitmap_, method_index)) {
491      return OatMethod(NULL, 0, 0);
492    }
493    size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
494    methods_pointer_index = num_set_bits;
495  }
496  const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
497  return OatMethod(
498      oat_file_->Begin(),
499      oat_method_offsets.code_offset_,
500      oat_method_offsets.gc_map_offset_);
501}
502
503OatFile::OatMethod::OatMethod(const byte* base,
504                              const uint32_t code_offset,
505                              const uint32_t gc_map_offset)
506  : begin_(base),
507    code_offset_(code_offset),
508    native_gc_map_offset_(gc_map_offset) {
509}
510
511OatFile::OatMethod::~OatMethod() {}
512
513
514uint32_t OatFile::OatMethod::GetQuickCodeSize() const {
515  uintptr_t code = reinterpret_cast<uintptr_t>(GetQuickCode());
516  if (code == 0) {
517    return 0;
518  }
519  // TODO: make this Thumb2 specific
520  code &= ~0x1;
521  return reinterpret_cast<uint32_t*>(code)[-1];
522}
523
524void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
525  CHECK(method != NULL);
526  method->SetEntryPointFromPortableCompiledCode(GetPortableCode());
527  method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
528  method->SetNativeGcMap(GetNativeGcMap());  // Used by native methods in work around JNI mode.
529}
530
531}  // namespace art
532