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
21#include "base/stl_util.h"
22#include "base/unix_file/fd_file.h"
23#include "elf_file.h"
24#include "oat.h"
25#include "mirror/art_method.h"
26#include "mirror/art_method-inl.h"
27#include "mirror/class.h"
28#include "mirror/object-inl.h"
29#include "os.h"
30#include "utils.h"
31#include "vmap_table.h"
32
33namespace art {
34
35std::string OatFile::DexFilenameToOdexFilename(const std::string& location) {
36  CHECK_GE(location.size(), 4U) << location;  // must be at least .123
37  size_t dot_index = location.size() - 3 - 1;  // 3=dex or zip or apk
38  CHECK_EQ('.', location[dot_index]) << location;
39  std::string odex_location(location);
40  odex_location.resize(dot_index + 1);
41  CHECK_EQ('.', odex_location[odex_location.size()-1]) << location << " " << odex_location;
42  odex_location += "odex";
43  return odex_location;
44}
45
46void OatFile::CheckLocation(const std::string& location) {
47  CHECK(!location.empty());
48}
49
50OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
51                             const std::string& location) {
52  CHECK(!oat_contents.empty()) << location;
53  CheckLocation(location);
54  UniquePtr<OatFile> oat_file(new OatFile(location));
55  oat_file->begin_ = &oat_contents[0];
56  oat_file->end_ = &oat_contents[oat_contents.size()];
57  return oat_file->Setup() ? oat_file.release() : NULL;
58}
59
60OatFile* OatFile::Open(const std::string& filename,
61                       const std::string& location,
62                       byte* requested_base,
63                       bool executable) {
64  CHECK(!filename.empty()) << location;
65  CheckLocation(filename);
66#ifdef ART_USE_PORTABLE_COMPILER
67  // If we are using PORTABLE, use dlopen to deal with relocations.
68  //
69  // We use our own ELF loader for Quick to deal with legacy apps that
70  // open a generated dex file by name, remove the file, then open
71  // another generated dex file with the same name. http://b/10614658
72  if (executable) {
73    return OpenDlopen(filename, location, requested_base);
74  }
75#endif
76  // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
77  //
78  // On target, dlopen may fail when compiling due to selinux restrictions on installd.
79  //
80  // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
81  // This won't work for portable runtime execution because it doesn't process relocations.
82  UniquePtr<File> file(OS::OpenFileForReading(filename.c_str()));
83  if (file.get() == NULL) {
84    return NULL;
85  }
86  return OpenElfFile(file.get(), location, requested_base, false, executable);
87}
88
89OatFile* OatFile::OpenWritable(File* file, const std::string& location) {
90  CheckLocation(location);
91  return OpenElfFile(file, location, NULL, true, false);
92}
93
94OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
95                             const std::string& location,
96                             byte* requested_base) {
97  UniquePtr<OatFile> oat_file(new OatFile(location));
98  bool success = oat_file->Dlopen(elf_filename, requested_base);
99  if (!success) {
100    return NULL;
101  }
102  return oat_file.release();
103}
104
105OatFile* OatFile::OpenElfFile(File* file,
106                              const std::string& location,
107                              byte* requested_base,
108                              bool writable,
109                              bool executable) {
110  UniquePtr<OatFile> oat_file(new OatFile(location));
111  bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable);
112  if (!success) {
113    return NULL;
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  STLDeleteValues(&oat_dex_files_);
125  if (dlopen_handle_ != NULL) {
126    dlclose(dlopen_handle_);
127  }
128}
129
130bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base) {
131  char* absolute_path = realpath(elf_filename.c_str(), NULL);
132  if (absolute_path == NULL) {
133    VLOG(class_linker) << "Failed to find absolute path for " << elf_filename;
134    return false;
135  }
136  dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
137  free(absolute_path);
138  if (dlopen_handle_ == NULL) {
139    VLOG(class_linker) << "Failed to dlopen " << elf_filename << ": " << dlerror();
140    return false;
141  }
142  begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
143  if (begin_ == NULL) {
144    LOG(WARNING) << "Failed to find oatdata symbol in " << elf_filename << ": " << dlerror();
145    return false;
146  }
147  if (requested_base != NULL && begin_ != requested_base) {
148    std::string maps;
149    ReadFileToString("/proc/self/maps", &maps);
150    LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
151                 << reinterpret_cast<const void*>(begin_) << " != expected="
152                 << reinterpret_cast<const void*>(requested_base)
153                 << " /proc/self/maps:\n" << maps;
154    return false;
155  }
156  end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
157  if (end_ == NULL) {
158    LOG(WARNING) << "Failed to find oatlastword symbol in " << elf_filename << ": " << dlerror();
159    return false;
160  }
161  // Readjust to be non-inclusive upper bound.
162  end_ += sizeof(uint32_t);
163  return Setup();
164}
165
166bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable) {
167  elf_file_.reset(ElfFile::Open(file, writable, true));
168  if (elf_file_.get() == NULL) {
169    if (writable) {
170      PLOG(WARNING) << "Failed to open ELF file for " << file->GetPath();
171    }
172    return false;
173  }
174  bool loaded = elf_file_->Load(executable);
175  if (!loaded) {
176    LOG(WARNING) << "Failed to load ELF file " << file->GetPath();
177    return false;
178  }
179  begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
180  if (begin_ == NULL) {
181    LOG(WARNING) << "Failed to find oatdata symbol in " << file->GetPath();
182    return false;
183  }
184  if (requested_base != NULL && begin_ != requested_base) {
185    std::string maps;
186    ReadFileToString("/proc/self/maps", &maps);
187    LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
188                 << reinterpret_cast<const void*>(begin_) << " != expected="
189                 << reinterpret_cast<const void*>(requested_base)
190                 << " /proc/self/maps:\n" << maps;
191    return false;
192  }
193  end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
194  if (end_ == NULL) {
195    LOG(WARNING) << "Failed to find oatlastword symbol in " << file->GetPath();
196    return false;
197  }
198  // Readjust to be non-inclusive upper bound.
199  end_ += sizeof(uint32_t);
200  return Setup();
201}
202
203bool OatFile::Setup() {
204  if (!GetOatHeader().IsValid()) {
205    LOG(WARNING) << "Invalid oat magic for " << GetLocation();
206    return false;
207  }
208  const byte* oat = Begin();
209  oat += sizeof(OatHeader);
210  if (oat > End()) {
211    LOG(ERROR) << "In oat file " << GetLocation() << " found truncated OatHeader";
212    return false;
213  }
214
215  oat += GetOatHeader().GetImageFileLocationSize();
216  if (oat > End()) {
217    LOG(ERROR) << "In oat file " << GetLocation() << " found truncated image file location: "
218               << reinterpret_cast<const void*>(Begin())
219               << "+" << sizeof(OatHeader)
220               << "+" << GetOatHeader().GetImageFileLocationSize()
221               << "<=" << reinterpret_cast<const void*>(End());
222    return false;
223  }
224
225  for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
226    size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
227    if (dex_file_location_size == 0U) {
228      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
229                 << " with empty location name";
230      return false;
231    }
232    oat += sizeof(dex_file_location_size);
233    if (oat > End()) {
234      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
235                 << " truncated after dex file location size";
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 (oat > End()) {
242      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
243                 << " with truncated dex file location";
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 (oat > End()) {
252      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
253                 << " for "<< dex_file_location
254                 << " truncated after dex file checksum";
255      return false;
256    }
257
258    uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
259    if (dex_file_offset == 0U) {
260      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
261                 << " for "<< dex_file_location
262                 << " with zero dex file offset";
263      return false;
264    }
265    if (dex_file_offset > Size()) {
266      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
267                 << " for "<< dex_file_location
268                 << " with dex file offset" << dex_file_offset << " > " << Size();
269      return false;
270    }
271    oat += sizeof(dex_file_offset);
272    if (oat > End()) {
273      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
274                 << " for "<< dex_file_location
275                 << " truncated after dex file offset";
276      return false;
277    }
278
279    const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
280    if (!DexFile::IsMagicValid(dex_file_pointer)) {
281      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
282                 << " for "<< dex_file_location
283                 << " with invalid dex file magic: " << dex_file_pointer;
284      return false;
285    }
286    if (!DexFile::IsVersionValid(dex_file_pointer)) {
287      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
288                 << " for "<< dex_file_location
289                 << " with invalid dex file version: " << dex_file_pointer;
290      return false;
291    }
292    const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
293    const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
294
295    oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
296    if (oat > End()) {
297      LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
298                 << " for "<< dex_file_location
299                 << " with truncated method offsets";
300      return false;
301    }
302
303    oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
304                                                         dex_file_location,
305                                                         dex_file_checksum,
306                                                         dex_file_pointer,
307                                                         methods_offsets_pointer));
308  }
309  return true;
310}
311
312const OatHeader& OatFile::GetOatHeader() const {
313  return *reinterpret_cast<const OatHeader*>(Begin());
314}
315
316const byte* OatFile::Begin() const {
317  CHECK(begin_ != NULL);
318  return begin_;
319}
320
321const byte* OatFile::End() const {
322  CHECK(end_ != NULL);
323  return end_;
324}
325
326const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_location,
327                                                  const uint32_t* const dex_location_checksum,
328                                                  bool warn_if_not_found) const {
329  Table::const_iterator it = oat_dex_files_.find(dex_location);
330  if (it != oat_dex_files_.end()) {
331    const OatFile::OatDexFile* oat_dex_file = it->second;
332    if (dex_location_checksum == NULL ||
333        oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) {
334      return oat_dex_file;
335    }
336  }
337
338  if (warn_if_not_found) {
339    LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
340                 << " in OatFile " << GetLocation();
341    if (kIsDebugBuild) {
342      for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
343        LOG(WARNING) << "OatFile " << GetLocation()
344                     << " contains OatDexFile " << it->second->GetDexFileLocation();
345      }
346    }
347  }
348  return NULL;
349}
350
351std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
352  std::vector<const OatFile::OatDexFile*> result;
353  for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
354    result.push_back(it->second);
355  }
356  return result;
357}
358
359OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
360                                const std::string& dex_file_location,
361                                uint32_t dex_file_location_checksum,
362                                const byte* dex_file_pointer,
363                                const uint32_t* oat_class_offsets_pointer)
364    : oat_file_(oat_file),
365      dex_file_location_(dex_file_location),
366      dex_file_location_checksum_(dex_file_location_checksum),
367      dex_file_pointer_(dex_file_pointer),
368      oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
369
370OatFile::OatDexFile::~OatDexFile() {}
371
372size_t OatFile::OatDexFile::FileSize() const {
373  return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
374}
375
376const DexFile* OatFile::OatDexFile::OpenDexFile() const {
377  return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
378                       dex_file_location_checksum_);
379}
380
381const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
382  uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
383
384  const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
385  CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
386  mirror::Class::Status status = *reinterpret_cast<const mirror::Class::Status*>(oat_class_pointer);
387
388  const byte* methods_pointer = oat_class_pointer + sizeof(status);
389  CHECK_LT(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
390
391  return new OatClass(oat_file_,
392                      status,
393                      reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
394}
395
396OatFile::OatClass::OatClass(const OatFile* oat_file,
397                            mirror::Class::Status status,
398                            const OatMethodOffsets* methods_pointer)
399    : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
400
401OatFile::OatClass::~OatClass() {}
402
403mirror::Class::Status OatFile::OatClass::GetStatus() const {
404  return status_;
405}
406
407const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
408  const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
409  return OatMethod(
410      oat_file_->Begin(),
411      oat_method_offsets.code_offset_,
412      oat_method_offsets.frame_size_in_bytes_,
413      oat_method_offsets.core_spill_mask_,
414      oat_method_offsets.fp_spill_mask_,
415      oat_method_offsets.mapping_table_offset_,
416      oat_method_offsets.vmap_table_offset_,
417      oat_method_offsets.gc_map_offset_);
418}
419
420OatFile::OatMethod::OatMethod(const byte* base,
421                              const uint32_t code_offset,
422                              const size_t frame_size_in_bytes,
423                              const uint32_t core_spill_mask,
424                              const uint32_t fp_spill_mask,
425                              const uint32_t mapping_table_offset,
426                              const uint32_t vmap_table_offset,
427                              const uint32_t gc_map_offset)
428  : begin_(base),
429    code_offset_(code_offset),
430    frame_size_in_bytes_(frame_size_in_bytes),
431    core_spill_mask_(core_spill_mask),
432    fp_spill_mask_(fp_spill_mask),
433    mapping_table_offset_(mapping_table_offset),
434    vmap_table_offset_(vmap_table_offset),
435    native_gc_map_offset_(gc_map_offset) {
436#ifndef NDEBUG
437  if (mapping_table_offset_ != 0) {  // implies non-native, non-stub code
438    if (vmap_table_offset_ == 0) {
439      DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) +
440                                          __builtin_popcount(fp_spill_mask_)));
441    } else {
442      VmapTable vmap_table(reinterpret_cast<const uint8_t*>(begin_ + vmap_table_offset_));
443
444      DCHECK_EQ(vmap_table.Size(), static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) +
445                                                         __builtin_popcount(fp_spill_mask_)));
446    }
447  } else {
448    DCHECK_EQ(vmap_table_offset_, 0U);
449  }
450#endif
451}
452
453OatFile::OatMethod::~OatMethod() {}
454
455const void* OatFile::OatMethod::GetCode() const {
456  return GetOatPointer<const void*>(code_offset_);
457}
458
459uint32_t OatFile::OatMethod::GetCodeSize() const {
460#if defined(ART_USE_PORTABLE_COMPILER)
461  // TODO: With Quick, we store the size before the code. With
462  // Portable, the code is in a .o file we don't manage ourselves. ELF
463  // symbols do have a concept of size, so we could capture that and
464  // store it somewhere, such as the OatMethod.
465  return 0;
466#else
467  uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
468
469  if (code == 0) {
470    return 0;
471  }
472  // TODO: make this Thumb2 specific
473  code &= ~0x1;
474  return reinterpret_cast<uint32_t*>(code)[-1];
475#endif
476}
477
478void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
479  CHECK(method != NULL);
480  method->SetEntryPointFromCompiledCode(GetCode());
481  method->SetFrameSizeInBytes(frame_size_in_bytes_);
482  method->SetCoreSpillMask(core_spill_mask_);
483  method->SetFpSpillMask(fp_spill_mask_);
484  method->SetMappingTable(GetMappingTable());
485  method->SetVmapTable(GetVmapTable());
486  method->SetNativeGcMap(GetNativeGcMap());  // Used by native methods in work around JNI mode.
487}
488
489}  // namespace art
490