blob: d3c4b491461e451dd9a8ca31b553734e41a43b69 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_file.h"
18
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019#include <dlfcn.h>
Calin Juravle4e1d5792014-07-15 23:56:47 +010020#include <string.h>
Andreas Gampedaab38c2014-09-12 18:38:24 -070021#include <unistd.h>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080022
Andreas Gampe7848da42015-04-09 11:15:04 -070023#include <cstdlib>
Richard Uhlere5fed032015-03-18 08:21:11 -070024#include <sstream>
25
Brian Carlstromba150c32013-08-27 17:31:03 -070026#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070030#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031#include "oat.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070032#include "mirror/art_method.h"
33#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070036#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070037#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070039#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070040
41namespace art {
42
Richard Uhlere5fed032015-03-18 08:21:11 -070043std::string OatFile::ResolveRelativeEncodedDexLocation(
44 const char* abs_dex_location, const std::string& rel_dex_location) {
45 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
46 // Strip :classes<N>.dex used for secondary multidex files.
47 std::string base = DexFile::GetBaseLocation(rel_dex_location);
48 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
49
50 // Check if the base is a suffix of the provided abs_dex_location.
51 std::string target_suffix = "/" + base;
52 std::string abs_location(abs_dex_location);
53 if (abs_location.size() > target_suffix.size()) {
54 size_t pos = abs_location.size() - target_suffix.size();
55 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
56 return abs_location + multidex_suffix;
57 }
58 }
59 }
60 return rel_dex_location;
61}
62
Brian Carlstrom700c8d32012-11-05 10:42:02 -080063void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070064 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080065}
66
Alex Light84d76052014-08-22 17:49:35 -070067OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
68 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070069 const char* abs_dex_location,
Alex Light84d76052014-08-22 17:49:35 -070070 std::string* error_msg) {
71 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
72 oat_file->elf_file_.reset(elf_file);
Tong Shen62d1ca32014-09-03 17:24:56 -070073 uint64_t offset, size;
74 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
75 CHECK(has_section);
76 oat_file->begin_ = elf_file->Begin() + offset;
77 oat_file->end_ = elf_file->Begin() + size + offset;
Vladimir Marko5c42c292015-02-25 12:02:49 +000078 // Ignore the optional .bss section when opening non-executable.
Richard Uhlere5fed032015-03-18 08:21:11 -070079 return oat_file->Setup(abs_dex_location, error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080080}
81
82OatFile* OatFile::Open(const std::string& filename,
83 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -070084 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -070085 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070086 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -070087 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070088 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080089 CHECK(!filename.empty()) << location;
Andreas Gampe7ba64962014-10-23 11:37:40 -070090 CheckLocation(location);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070091 std::unique_ptr<OatFile> ret;
Elliott Hughes956af0f2014-12-11 14:34:28 -080092 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
93 //
94 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
95 //
96 // We use our own ELF loader for Quick to deal with legacy apps that
97 // open a generated dex file by name, remove the file, then open
98 // another generated dex file with the same name. http://b/10614658
99 //
100 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
101 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
102 if (file.get() == NULL) {
103 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
104 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800105 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800106 ret.reset(OpenElfFile(file.get(), location, requested_base, oat_file_begin, false, executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700107 abs_dex_location, error_msg));
Elliott Hughes956af0f2014-12-11 14:34:28 -0800108
109 // It would be nice to unlink here. But we might have opened the file created by the
110 // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API
111 // to allow removal when we know the ELF must be borked.
Dave Allison69dfe512014-07-11 17:11:58 +0000112 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800113}
114
Richard Uhlere5fed032015-03-18 08:21:11 -0700115OatFile* OatFile::OpenWritable(File* file, const std::string& location,
116 const char* abs_dex_location,
117 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800118 CheckLocation(location);
Richard Uhlere5fed032015-03-18 08:21:11 -0700119 return OpenElfFile(file, location, nullptr, nullptr, true, false, abs_dex_location, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800120}
121
Richard Uhlere5fed032015-03-18 08:21:11 -0700122OatFile* OatFile::OpenReadable(File* file, const std::string& location,
123 const char* abs_dex_location,
124 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700125 CheckLocation(location);
Richard Uhlere5fed032015-03-18 08:21:11 -0700126 return OpenElfFile(file, location, nullptr, nullptr, false, false, abs_dex_location, error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700127}
128
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800129OatFile* OatFile::OpenElfFile(File* file,
130 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700131 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700132 uint8_t* oat_file_begin,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700133 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700134 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700135 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700136 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700137 std::unique_ptr<OatFile> oat_file(new OatFile(location, executable));
Igor Murashkin46774762014-10-22 11:37:02 -0700138 bool success = oat_file->ElfFileOpen(file, requested_base, oat_file_begin, writable, executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700139 abs_dex_location, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700141 CHECK(!error_msg->empty());
142 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143 }
144 return oat_file.release();
145}
146
Alex Light9dcc4572014-08-14 14:16:26 -0700147OatFile::OatFile(const std::string& location, bool is_executable)
Vladimir Marko5c42c292015-02-25 12:02:49 +0000148 : location_(location), begin_(NULL), end_(NULL), bss_begin_(nullptr), bss_end_(nullptr),
149 is_executable_(is_executable), dlopen_handle_(NULL),
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100150 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800151 CHECK(!location_.empty());
152}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153
154OatFile::~OatFile() {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100155 STLDeleteElements(&oat_dex_files_storage_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 if (dlopen_handle_ != NULL) {
157 dlclose(dlopen_handle_);
158 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700159}
160
Igor Murashkin46774762014-10-22 11:37:02 -0700161bool OatFile::ElfFileOpen(File* file, uint8_t* requested_base, uint8_t* oat_file_begin,
162 bool writable, bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700163 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700164 std::string* error_msg) {
Igor Murashkin46774762014-10-22 11:37:02 -0700165 // TODO: rename requested_base to oat_data_begin
166 elf_file_.reset(ElfFile::Open(file, writable, /*program_header_only*/true, error_msg,
167 oat_file_begin));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700168 if (elf_file_.get() == nullptr) {
169 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800170 return false;
171 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700172 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800173 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700174 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800175 return false;
176 }
177 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
178 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700179 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800180 return false;
181 }
182 if (requested_base != NULL && begin_ != requested_base) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800183 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700184 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800185 "oatdata=%p != expected=%p. See process maps in the log.",
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700186 begin_, requested_base);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800187 return false;
188 }
189 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
190 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700191 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800192 return false;
193 }
194 // Readjust to be non-inclusive upper bound.
195 end_ += sizeof(uint32_t);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000196
197 bss_begin_ = elf_file_->FindDynamicSymbolAddress("oatbss");
198 if (bss_begin_ == nullptr) {
199 // No .bss section. Clear dlerror().
200 bss_end_ = nullptr;
201 dlerror();
202 } else {
203 bss_end_ = elf_file_->FindDynamicSymbolAddress("oatbsslastword");
204 if (bss_end_ == nullptr) {
205 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'",
206 file->GetPath().c_str());
207 return false;
208 }
209 // Readjust to be non-inclusive upper bound.
210 bss_end_ += sizeof(uint32_t);
211 }
212
Richard Uhlere5fed032015-03-18 08:21:11 -0700213 return Setup(abs_dex_location, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800214}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800215
Richard Uhlere5fed032015-03-18 08:21:11 -0700216bool OatFile::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700217 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800218 std::string cause = GetOatHeader().GetValidationErrorMessage();
219 *error_msg = StringPrintf("Invalid oat header for '%s': %s", GetLocation().c_str(),
220 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700221 return false;
222 }
Ian Rogers13735952014-10-08 12:43:28 -0700223 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700224 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700225 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700226 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700227 return false;
228 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700229
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700230 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700231 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700232 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700233 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700234 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700235 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700236 return false;
237 }
238
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100239 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
240 oat_dex_files_storage_.reserve(dex_file_count);
241 for (size_t i = 0; i < dex_file_count; i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700242 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700243 if (UNLIKELY(dex_file_location_size == 0U)) {
244 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
245 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700246 return false;
247 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700249 if (UNLIKELY(oat > End())) {
250 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
251 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700252 return false;
253 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254
255 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
256 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700257 if (UNLIKELY(oat > End())) {
258 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
259 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700260 return false;
261 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700262
Richard Uhlere5fed032015-03-18 08:21:11 -0700263 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
264 abs_dex_location,
265 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700266
267 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
268 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700269 if (UNLIKELY(oat > End())) {
270 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
271 "dex file checksum", GetLocation().c_str(), i,
272 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700273 return false;
274 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700275
Brian Carlstrom89521892011-12-07 22:05:07 -0800276 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700277 if (UNLIKELY(dex_file_offset == 0U)) {
278 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
279 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700280 return false;
281 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700282 if (UNLIKELY(dex_file_offset > Size())) {
283 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
284 "offset %ud > %zd", GetLocation().c_str(), i,
285 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700286 return false;
287 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800288 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700289 if (UNLIKELY(oat > End())) {
290 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700291 "after dex file offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700292 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700293 return false;
294 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800295
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800296 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700297 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
298 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700299 "dex file magic '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700300 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700301 return false;
302 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700303 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
304 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700305 "dex file version '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700306 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700307 return false;
308 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800309 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
310 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800311
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800312 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700313 if (UNLIKELY(oat > End())) {
314 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700315 "method offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700316 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700317 return false;
318 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700319
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100320 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
321
322 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100323 OatDexFile* oat_dex_file = new OatDexFile(this,
324 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100325 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100326 dex_file_checksum,
327 dex_file_pointer,
328 methods_offsets_pointer);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100329 oat_dex_files_storage_.push_back(oat_dex_file);
330
331 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100332 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100333 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100334 if (canonical_location != dex_file_location) {
335 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
336 oat_dex_files_.Put(canonical_key, oat_dex_file);
337 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700338 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700339 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700340}
341
342const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800343 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700344}
345
Ian Rogers13735952014-10-08 12:43:28 -0700346const uint8_t* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800347 CHECK(begin_ != NULL);
348 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700349}
350
Ian Rogers13735952014-10-08 12:43:28 -0700351const uint8_t* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800352 CHECK(end_ != NULL);
353 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354}
355
Vladimir Marko5c42c292015-02-25 12:02:49 +0000356const uint8_t* OatFile::BssBegin() const {
357 return bss_begin_;
358}
359
360const uint8_t* OatFile::BssEnd() const {
361 return bss_end_;
362}
363
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700364const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
365 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800366 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100367 // NOTE: We assume here that the canonical location for a given dex_location never
368 // changes. If it does (i.e. some symlink used by the filename changes) we may return
369 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
370 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100371
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100372 // TODO: Additional analysis of usage patterns to see if this can be simplified
373 // without any performance loss, for example by not doing the first lock-free lookup.
374
375 const OatFile::OatDexFile* oat_dex_file = nullptr;
376 StringPiece key(dex_location);
377 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
378 // directly mentioned in the oat file and doesn't require locking.
379 auto primary_it = oat_dex_files_.find(key);
380 if (primary_it != oat_dex_files_.end()) {
381 oat_dex_file = primary_it->second;
382 DCHECK(oat_dex_file != nullptr);
383 } else {
384 // This dex_location is not one of the dex locations directly mentioned in the
385 // oat file. The correct lookup is via the canonical location but first see in
386 // the secondary_oat_dex_files_ whether we've looked up this location before.
387 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
388 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
389 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
390 oat_dex_file = secondary_lb->second; // May be nullptr.
391 } else {
392 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100393 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100394 if (dex_canonical_location != dex_location) {
395 StringPiece canonical_key(dex_canonical_location);
396 auto canonical_it = oat_dex_files_.find(canonical_key);
397 if (canonical_it != oat_dex_files_.end()) {
398 oat_dex_file = canonical_it->second;
399 } // else keep nullptr.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100400 } // else keep nullptr.
401
402 // Copy the key to the string_cache_ and store the result in secondary map.
403 string_cache_.emplace_back(key.data(), key.length());
404 StringPiece key_copy(string_cache_.back());
405 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700406 }
407 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100408 if (oat_dex_file != nullptr &&
409 (dex_location_checksum == nullptr ||
410 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
411 return oat_dex_file;
412 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700413
414 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100415 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800416 std::string checksum("<unspecified>");
417 if (dex_location_checksum != NULL) {
418 checksum = StringPrintf("0x%08x", *dex_location_checksum);
419 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700420 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100421 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800422 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700423 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100424 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700425 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100426 << " contains OatDexFile " << odf->GetDexFileLocation()
427 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
428 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700429 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800430 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700431 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100432
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700433 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700434}
435
Brian Carlstrome24fa612011-09-29 00:53:55 -0700436OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800437 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100438 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800439 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -0700440 const uint8_t* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800441 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700442 : oat_file_(oat_file),
443 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100444 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800445 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800446 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800447 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700448
449OatFile::OatDexFile::~OatDexFile() {}
450
Ian Rogers05f28c62012-10-23 18:12:13 -0700451size_t OatFile::OatDexFile::FileSize() const {
452 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
453}
454
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800455std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700456 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Richard Uhler07b3c232015-03-31 15:57:54 -0700457 dex_file_location_checksum_, this, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800458}
459
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700460uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
461 return oat_class_offsets_pointer_[class_def_index];
462}
463
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100464OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700465 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800466
Ian Rogers13735952014-10-08 12:43:28 -0700467 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700468 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800469
Ian Rogers13735952014-10-08 12:43:28 -0700470 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -0700471 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
472 mirror::Class::Status status =
473 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
474 CHECK_LT(status, mirror::Class::kStatusMax);
475
Ian Rogers13735952014-10-08 12:43:28 -0700476 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -0700477 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
478 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
479 CHECK_LT(type, kOatClassMax);
480
Ian Rogers13735952014-10-08 12:43:28 -0700481 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800482 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700483
Brian Carlstromcd937a92014-03-04 22:53:23 -0800484 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700485 const uint8_t* bitmap_pointer = nullptr;
486 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -0700487 if (type != kOatClassNoneCompiled) {
488 if (type == kOatClassSomeCompiled) {
489 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
490 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
491 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
492 methods_pointer = bitmap_pointer + bitmap_size;
493 } else {
494 methods_pointer = after_type_pointer;
495 }
496 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -0800497 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800498
Richard Uhler07b3c232015-03-31 15:57:54 -0700499 return OatFile::OatClass(oat_file_,
500 status,
501 type,
502 bitmap_size,
503 reinterpret_cast<const uint32_t*>(bitmap_pointer),
504 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700505}
506
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800507OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700509 OatClassType type,
510 uint32_t bitmap_size,
511 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800512 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700513 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100514 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700515 switch (type_) {
516 case kOatClassAllCompiled: {
517 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800518 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700519 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700520 break;
521 }
522 case kOatClassSomeCompiled: {
523 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800524 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700525 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700526 break;
527 }
528 case kOatClassNoneCompiled: {
529 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800530 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700531 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700532 break;
533 }
534 case kOatClassMax: {
535 LOG(FATAL) << "Invalid OatClassType " << type_;
536 break;
537 }
538 }
539}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700540
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700541uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
542 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
543 if (oat_method_offsets == nullptr) {
544 return 0u;
545 }
546 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
547}
548
549const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100550 // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700551 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700552 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700553 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700554 }
555 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700556 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700557 CHECK_EQ(kOatClassAllCompiled, type_);
558 methods_pointer_index = method_index;
559 } else {
560 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100561 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700562 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700563 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100564 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
565 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700566 }
567 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700568 return &oat_method_offsets;
569}
570
571const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
572 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
573 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800574 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700575 }
576 if (oat_file_->IsExecutable() ||
577 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800578 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800579 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -0700580 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800581 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
582 // version.
583 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700584}
585
Brian Carlstromea46f952013-07-30 01:26:50 -0700586void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700587 CHECK(method != NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800588 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -0800589}
590
Andreas Gampe7ba64962014-10-23 11:37:40 -0700591bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -0700592 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -0700593 // TODO: Check against oat_patches. b/18144996
594}
595
Andreas Gampe7848da42015-04-09 11:15:04 -0700596static constexpr char kDexClassPathEncodingSeparator = '*';
597
598std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
599 std::ostringstream out;
600
601 for (const DexFile* dex_file : dex_files) {
602 out << dex_file->GetLocation().c_str();
603 out << kDexClassPathEncodingSeparator;
604 out << dex_file->GetLocationChecksum();
605 out << kDexClassPathEncodingSeparator;
606 }
607
608 return out.str();
609}
610
611bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
612 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
613 // No dependencies.
614 return true;
615 }
616
617 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
618 // Split() instead of manual parsing of the combined char*.
619 std::vector<std::string> split;
620 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
621 if (split.size() % 2 != 0) {
622 // Expected pairs of location and checksum.
623 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
624 return false;
625 }
626
627 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
628 std::string& location = *it;
629 std::string& checksum = *(it + 1);
630 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
631 if (converted == 0) {
632 // Conversion error.
633 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
634 return false;
635 }
636
637 uint32_t dex_checksum;
638 std::string error_msg;
639 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
640 &dex_checksum,
641 &error_msg)) {
642 if (converted != dex_checksum) {
643 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
644 location.c_str(), converted, dex_checksum);
645 return false;
646 }
647 } else {
648 // Problem retrieving checksum.
649 // TODO: odex files?
650 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
651 error_msg.c_str());
652 return false;
653 }
654 }
655
656 return true;
657}
658
659bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
660 std::vector<std::string>* locations) {
661 DCHECK(locations != nullptr);
662 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
663 return true;
664 }
665
666 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
667 // Split() instead of manual parsing of the combined char*.
668 std::vector<std::string> split;
669 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
670 if (split.size() % 2 != 0) {
671 // Expected pairs of location and checksum.
672 return false;
673 }
674
675 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
676 locations->push_back(*it);
677 }
678
679 return true;
680}
681
Brian Carlstrome24fa612011-09-29 00:53:55 -0700682} // namespace art