blob: cfecee67b998b15f7b2432eddc7190cace8147e6 [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>
Vladimir Marko06d7aaa2015-10-16 11:23:41 +010021#include <type_traits>
Andreas Gampedaab38c2014-09-12 18:38:24 -070022#include <unistd.h>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080023
Andreas Gampe7848da42015-04-09 11:15:04 -070024#include <cstdlib>
David Srbecky1baabf02015-06-16 17:12:34 +000025#ifndef __APPLE__
26#include <link.h> // for dl_iterate_phdr.
27#endif
Richard Uhlere5fed032015-03-18 08:21:11 -070028#include <sstream>
29
Andreas Gampefa8429b2015-04-07 18:34:42 -070030// dlopen_ext support from bionic.
Andreas Gampec60e1b72015-07-30 08:57:50 -070031#ifdef __ANDROID__
Andreas Gampefa8429b2015-04-07 18:34:42 -070032#include "android/dlext.h"
33#endif
34
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "art_method-inl.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070036#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080037#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080038#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080039#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070041#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042#include "oat.h"
David Srbecky1baabf02015-06-16 17:12:34 +000043#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070045#include "mirror/object-inl.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010046#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070047#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070048#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070049#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000050#include "type_lookup_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "utils.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010052#include "utils/dex_cache_arrays_layout-inl.h"
Ian Rogers1809a722013-08-09 22:05:32 -070053#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070054
55namespace art {
56
Andreas Gampe049cff02015-12-01 23:27:12 -080057// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000058static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070059
Andreas Gampe049cff02015-12-01 23:27:12 -080060// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070061// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
62// multiple times). However, if/when we switch the above, we likely want to switch this, too,
63// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000064static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070065
66// For debugging, Open will print DlOpen error message if set to true.
67static constexpr bool kPrintDlOpenErrorMessage = false;
68
Andreas Gampe049cff02015-12-01 23:27:12 -080069// Note for OatFileBase and descendents:
70//
71// These are used in OatFile::Open to try all our loaders.
72//
73// The process is simple:
74//
75// 1) Allocate an instance through the standard constructor (location, executable)
76// 2) Load() to try to open the file.
77// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
78// 4) PreSetup() for any steps that should be done before the final setup.
79// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070080
Andreas Gampe049cff02015-12-01 23:27:12 -080081class OatFileBase : public OatFile {
82 public:
83 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070084
Andreas Gampe049cff02015-12-01 23:27:12 -080085 template <typename kOatFileBaseSubType>
86 static OatFileBase* OpenOatFile(const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070087 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080088 uint8_t* requested_base,
89 uint8_t* oat_file_begin,
90 bool writable,
91 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -070092 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -080093 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080094
Andreas Gampe049cff02015-12-01 23:27:12 -080095 protected:
96 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -070097
Andreas Gampe049cff02015-12-01 23:27:12 -080098 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
99 std::string* error_msg) const = 0;
100
101 virtual bool Load(const std::string& elf_filename,
102 uint8_t* oat_file_begin,
103 bool writable,
104 bool executable,
105 std::string* error_msg) = 0;
106
107 bool ComputeFields(uint8_t* requested_base,
108 const std::string& file_path,
109 std::string* error_msg);
110
111 virtual void PreSetup(const std::string& elf_filename) = 0;
112
113 bool Setup(const char* abs_dex_location, std::string* error_msg);
114
115 // Setters exposed for ElfOatFile.
116
117 void SetBegin(const uint8_t* begin) {
118 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700119 }
120
Andreas Gampe049cff02015-12-01 23:27:12 -0800121 void SetEnd(const uint8_t* end) {
122 end_ = end;
123 }
124
125 private:
126 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
127};
128
129template <typename kOatFileBaseSubType>
130OatFileBase* OatFileBase::OpenOatFile(const std::string& elf_filename,
131 const std::string& location,
132 uint8_t* requested_base,
133 uint8_t* oat_file_begin,
134 bool writable,
135 bool executable,
136 const char* abs_dex_location,
137 std::string* error_msg) {
138 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
139 if (!ret->Load(elf_filename,
140 oat_file_begin,
141 writable,
142 executable,
143 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800144 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800145 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800146
Andreas Gampe049cff02015-12-01 23:27:12 -0800147 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
148 return nullptr;
149 }
150
151 ret->PreSetup(elf_filename);
152
153 if (!ret->Setup(abs_dex_location, error_msg)) {
154 return nullptr;
155 }
156
Dave Allison69dfe512014-07-11 17:11:58 +0000157 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800158}
159
Andreas Gampe049cff02015-12-01 23:27:12 -0800160bool OatFileBase::ComputeFields(uint8_t* requested_base,
161 const std::string& file_path,
162 std::string* error_msg) {
163 std::string symbol_error_msg;
164 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700165 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800166 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
167 file_path.c_str(),
168 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700169 return false;
170 }
171 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000172 // Host can fail this check. Do not dump there to avoid polluting the output.
173 if (kIsTargetBuild) {
174 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
175 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700176 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800177 "oatdata=%p != expected=%p. See process maps in the log.",
178 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700179 return false;
180 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800181 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700182 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800183 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
184 file_path.c_str(),
185 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700186 return false;
187 }
188 // Readjust to be non-inclusive upper bound.
189 end_ += sizeof(uint32_t);
190
Andreas Gampe049cff02015-12-01 23:27:12 -0800191 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700192 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800193 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700194 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700195 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800196 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700197 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800198 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700199 return false;
200 }
201 // Readjust to be non-inclusive upper bound.
202 bss_end_ += sizeof(uint32_t);
203 }
204
Andreas Gampe049cff02015-12-01 23:27:12 -0800205 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800206}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800207
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100208// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
209// position by the number of bytes read, i.e. sizeof(T).
210// Return true on success, false if the read would go beyond the end of the OatFile.
211template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100212inline static bool ReadOatDexFileData(const OatFile& oat_file,
213 /*inout*/const uint8_t** oat,
214 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100215 DCHECK(oat != nullptr);
216 DCHECK(value != nullptr);
217 DCHECK_LE(*oat, oat_file.End());
218 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
219 return false;
220 }
221 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
222 typedef __attribute__((__aligned__(1))) T unaligned_type;
223 *value = *reinterpret_cast<const unaligned_type*>(*oat);
224 *oat += sizeof(T);
225 return true;
226}
227
Andreas Gampe049cff02015-12-01 23:27:12 -0800228bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700229 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800230 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100231 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
232 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800233 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700234 return false;
235 }
Ian Rogers13735952014-10-08 12:43:28 -0700236 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700237 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700238 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700239 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700240 return false;
241 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700242
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700243 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700244 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700245 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100246 "%p + %zu + %u <= %p",
247 GetLocation().c_str(),
248 Begin(),
249 sizeof(OatHeader),
250 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700251 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700252 return false;
253 }
254
Vladimir Marko09d09432015-09-08 13:47:48 +0100255 size_t pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100256 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100257 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
258 oat_dex_files_storage_.reserve(dex_file_count);
259 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100260 uint32_t dex_file_location_size;
261 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
262 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
263 "location size",
264 GetLocation().c_str(),
265 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700266 return false;
267 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100268 if (UNLIKELY(dex_file_location_size == 0U)) {
269 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
270 GetLocation().c_str(),
271 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700272 return false;
273 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000274 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100275 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
276 "location",
277 GetLocation().c_str(),
278 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700279 return false;
280 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000281 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
282 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700283
Richard Uhlere5fed032015-03-18 08:21:11 -0700284 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
285 abs_dex_location,
286 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700287
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100288 uint32_t dex_file_checksum;
289 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
290 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
291 "dex file checksum",
292 GetLocation().c_str(),
293 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700294 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700295 return false;
296 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100298 uint32_t dex_file_offset;
299 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
300 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
301 "after dex file offsets",
302 GetLocation().c_str(),
303 i,
304 dex_file_location.c_str());
305 return false;
306 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700307 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100308 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
309 "file offset",
310 GetLocation().c_str(),
311 i,
312 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700313 return false;
314 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700315 if (UNLIKELY(dex_file_offset > Size())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100316 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
317 "offset %u > %zu",
318 GetLocation().c_str(),
319 i,
320 dex_file_location.c_str(),
321 dex_file_offset,
322 Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700323 return false;
324 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000325 if (UNLIKELY(Size() - dex_file_offset < sizeof(DexFile::Header))) {
326 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
327 "offset %u of %zu but the size of dex file header is %zu",
328 GetLocation().c_str(),
329 i,
330 dex_file_location.c_str(),
331 dex_file_offset,
332 Size(),
333 sizeof(DexFile::Header));
334 return false;
335 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800336
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800337 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700338 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100339 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
340 "dex file magic '%s'",
341 GetLocation().c_str(),
342 i,
343 dex_file_location.c_str(),
344 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700345 return false;
346 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700347 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100348 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
349 "dex file version '%s'",
350 GetLocation().c_str(),
351 i,
352 dex_file_location.c_str(),
353 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700354 return false;
355 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800356 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000357 if (Size() - dex_file_offset < header->file_size_) {
358 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
359 "offset %u and size %u truncated at %zu",
360 GetLocation().c_str(),
361 i,
362 dex_file_location.c_str(),
363 dex_file_offset,
364 header->file_size_,
365 Size());
366 return false;
367 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300368
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000369 uint32_t class_offsets_offset;
370 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
371 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
372 "after class offsets offset",
373 GetLocation().c_str(),
374 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300375 dex_file_location.c_str());
376 return false;
377 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000378 if (UNLIKELY(class_offsets_offset > Size()) ||
379 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
380 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
381 "class offsets, offset %u of %zu, class defs %u",
382 GetLocation().c_str(),
383 i,
384 dex_file_location.c_str(),
385 class_offsets_offset,
386 Size(),
387 header->class_defs_size_);
388 return false;
389 }
390 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
391 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
392 "class offsets, offset %u",
393 GetLocation().c_str(),
394 i,
395 dex_file_location.c_str(),
396 class_offsets_offset);
397 return false;
398 }
399 const uint32_t* class_offsets_pointer =
400 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
401
402 uint32_t lookup_table_offset;
403 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
404 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
405 "after lookup table offset",
406 GetLocation().c_str(),
407 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300408 dex_file_location.c_str());
409 return false;
410 }
411 const uint8_t* lookup_table_data = lookup_table_offset != 0u
412 ? Begin() + lookup_table_offset
413 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000414 if (lookup_table_offset != 0u &&
415 (UNLIKELY(lookup_table_offset > Size()) ||
416 UNLIKELY(Size() - lookup_table_offset <
417 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100418 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000419 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100420 GetLocation().c_str(),
421 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000422 dex_file_location.c_str(),
423 lookup_table_offset,
424 Size(),
425 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700426 return false;
427 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100429 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100430 if (dex_cache_arrays != nullptr) {
431 DexCacheArraysLayout layout(pointer_size, *header);
432 if (layout.Size() != 0u) {
433 if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100434 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
435 "truncated dex cache arrays, %zu < %zu.",
436 GetLocation().c_str(),
437 i,
438 dex_file_location.c_str(),
439 static_cast<size_t>(bss_end_ - dex_cache_arrays),
440 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100441 return false;
442 }
443 current_dex_cache_arrays = dex_cache_arrays;
444 dex_cache_arrays += layout.Size();
445 }
446 }
447
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100448 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
449
450 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100451 OatDexFile* oat_dex_file = new OatDexFile(this,
452 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100453 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100454 dex_file_checksum,
455 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300456 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000457 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100458 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100459 oat_dex_files_storage_.push_back(oat_dex_file);
460
461 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100462 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100463 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100464 if (canonical_location != dex_file_location) {
465 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
466 oat_dex_files_.Put(canonical_key, oat_dex_file);
467 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700468 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100469
470 if (dex_cache_arrays != bss_end_) {
471 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
472 // both null) or contain just the dex cache arrays and nothing else.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100473 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100474 GetLocation().c_str(),
475 static_cast<size_t>(bss_end_ - dex_cache_arrays));
476 return false;
477 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700478 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700479}
480
Andreas Gampe049cff02015-12-01 23:27:12 -0800481////////////////////////
482// OatFile via dlopen //
483////////////////////////
484
485static bool RegisterOatFileLocation(const std::string& location) {
486 if (!kIsTargetBuild) {
487 Runtime* const runtime = Runtime::Current();
488 if (runtime != nullptr && !runtime->IsAotCompiler()) {
489 return runtime->GetOatFileManager().RegisterOatFileLocation(location);
490 }
491 return false;
492 }
493 return true;
494}
495
496static void UnregisterOatFileLocation(const std::string& location) {
497 if (!kIsTargetBuild) {
498 Runtime* const runtime = Runtime::Current();
499 if (runtime != nullptr && !runtime->IsAotCompiler()) {
500 runtime->GetOatFileManager().UnRegisterOatFileLocation(location);
501 }
502 }
503}
504
505class DlOpenOatFile FINAL : public OatFileBase {
506 public:
507 DlOpenOatFile(const std::string& filename, bool executable)
508 : OatFileBase(filename, executable),
509 dlopen_handle_(nullptr),
510 first_oat_(RegisterOatFileLocation(filename)) {
511 }
512
513 ~DlOpenOatFile() {
514 if (dlopen_handle_ != nullptr) {
515 dlclose(dlopen_handle_);
516 }
517 UnregisterOatFileLocation(GetLocation());
518 }
519
520 protected:
521 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
522 std::string* error_msg) const OVERRIDE {
523 const uint8_t* ptr =
524 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
525 if (ptr == nullptr) {
526 *error_msg = dlerror();
527 }
528 return ptr;
529 }
530
531 bool Load(const std::string& elf_filename,
532 uint8_t* oat_file_begin,
533 bool writable,
534 bool executable,
535 std::string* error_msg) OVERRIDE;
536
537 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
538 void PreSetup(const std::string& elf_filename) OVERRIDE;
539
540 private:
541 bool Dlopen(const std::string& elf_filename,
542 uint8_t* oat_file_begin,
543 std::string* error_msg);
544
545 // dlopen handle during runtime.
546 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
547
548 // Dummy memory map objects corresponding to the regions mapped by dlopen.
549 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
550
551 // Track the registration status (= was this the first oat file) for the location.
552 const bool first_oat_;
553
554 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
555};
556
557bool DlOpenOatFile::Load(const std::string& elf_filename,
558 uint8_t* oat_file_begin,
559 bool writable,
560 bool executable,
561 std::string* error_msg) {
562 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
563 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
564 // !executable is a sign that we may want to patch), which may not be allowed for
565 // various reasons.
566 if (!kUseDlopen) {
567 *error_msg = "DlOpen is disabled.";
568 return false;
569 }
570 if (writable) {
571 *error_msg = "DlOpen does not support writable loading.";
572 return false;
573 }
574 if (!executable) {
575 *error_msg = "DlOpen does not support non-executable loading.";
576 return false;
577 }
578
579 // dlopen always returns the same library if it is already opened on the host. For this reason
580 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
581 // the same library loaded multiple times at different addresses is required for class unloading
582 // and for having dex caches arrays in the .bss section.
583 if (!kIsTargetBuild) {
584 if (!kUseDlopenOnHost) {
585 *error_msg = "DlOpen disabled for host.";
586 return false;
587 }
588 // For RAII, tracking multiple loads is done in the constructor and destructor. The result is
589 // stored in the first_oat_ flag.
590 if (!first_oat_) {
591 *error_msg = "Loading oat files multiple times with dlopen not supported on host.";
592 return false;
593 }
594 }
595
596 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
597 DCHECK(dlopen_handle_ != nullptr || !success);
598
599 return success;
600}
601
602bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
603 uint8_t* oat_file_begin,
604 std::string* error_msg) {
605#ifdef __APPLE__
606 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
607 // but let's fallback to the custom loading code for the time being.
608 UNUSED(elf_filename, oat_file_begin);
609 *error_msg = "Dlopen unsupported on Mac.";
610 return false;
611#else
612 {
613 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
614 if (absolute_path == nullptr) {
615 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
616 return false;
617 }
618#ifdef __ANDROID__
619 android_dlextinfo extinfo;
620 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
621 // (open oat files multiple
622 // times).
623 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
624 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800625 if (oat_file_begin != nullptr) { //
626 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
627 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
628 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800629 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
630#else
631 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
632 UNUSED(oat_file_begin);
633#endif
634 }
635 if (dlopen_handle_ == nullptr) {
636 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
637 return false;
638 }
639 return true;
640#endif
641}
642
643void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800644#ifdef __APPLE__
645 UNUSED(elf_filename);
646 LOG(FATAL) << "Should not reach here.";
647 UNREACHABLE();
648#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800649 struct dl_iterate_context {
650 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
651 auto* context = reinterpret_cast<dl_iterate_context*>(data);
652 // See whether this callback corresponds to the file which we have just loaded.
653 bool contains_begin = false;
654 for (int i = 0; i < info->dlpi_phnum; i++) {
655 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
656 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
657 info->dlpi_phdr[i].p_vaddr);
658 size_t memsz = info->dlpi_phdr[i].p_memsz;
659 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
660 contains_begin = true;
661 break;
662 }
663 }
664 }
665 // Add dummy mmaps for this file.
666 if (contains_begin) {
667 for (int i = 0; i < info->dlpi_phnum; i++) {
668 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
669 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
670 info->dlpi_phdr[i].p_vaddr);
671 size_t memsz = info->dlpi_phdr[i].p_memsz;
672 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
673 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
674 }
675 }
676 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
677 }
678 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
679 }
680 const uint8_t* const begin_;
681 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
682 } context = { Begin(), &dlopen_mmaps_ };
683
684 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
685 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
Roland Levillain91d65e02016-01-19 15:59:16 +0000686 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
Andreas Gampe049cff02015-12-01 23:27:12 -0800687 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800688#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800689}
690
691////////////////////////////////////////////////
692// OatFile via our own ElfFile implementation //
693////////////////////////////////////////////////
694
695class ElfOatFile FINAL : public OatFileBase {
696 public:
697 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
698
699 static ElfOatFile* OpenElfFile(File* file,
700 const std::string& location,
701 uint8_t* requested_base,
702 uint8_t* oat_file_begin, // Override base if not null
703 bool writable,
704 bool executable,
705 const char* abs_dex_location,
706 std::string* error_msg);
707
708 bool InitializeFromElfFile(ElfFile* elf_file,
709 const char* abs_dex_location,
710 std::string* error_msg);
711
712 protected:
713 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
714 std::string* error_msg) const OVERRIDE {
715 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
716 if (ptr == nullptr) {
717 *error_msg = "(Internal implementation could not find symbol)";
718 }
719 return ptr;
720 }
721
722 bool Load(const std::string& elf_filename,
723 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
724 bool writable,
725 bool executable,
726 std::string* error_msg) OVERRIDE;
727
728 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
729 }
730
731 private:
732 bool ElfFileOpen(File* file,
733 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
734 bool writable,
735 bool executable,
736 std::string* error_msg);
737
738 private:
739 // Backing memory map for oat file during cross compilation.
740 std::unique_ptr<ElfFile> elf_file_;
741
742 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
743};
744
745ElfOatFile* ElfOatFile::OpenElfFile(File* file,
746 const std::string& location,
747 uint8_t* requested_base,
748 uint8_t* oat_file_begin, // Override base if not null
749 bool writable,
750 bool executable,
751 const char* abs_dex_location,
752 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800753 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800754 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
755 bool success = oat_file->ElfFileOpen(file, oat_file_begin, writable, executable, error_msg);
756 if (!success) {
757 CHECK(!error_msg->empty());
758 return nullptr;
759 }
760
761 // Complete the setup.
762 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
763 return nullptr;
764 }
765
766 if (!oat_file->Setup(abs_dex_location, error_msg)) {
767 return nullptr;
768 }
769
770 return oat_file.release();
771}
772
773bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
774 const char* abs_dex_location,
775 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800776 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800777 if (IsExecutable()) {
778 *error_msg = "Cannot initialize from elf file in executable mode.";
779 return false;
780 }
781 elf_file_.reset(elf_file);
782 uint64_t offset, size;
783 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
784 CHECK(has_section);
785 SetBegin(elf_file->Begin() + offset);
786 SetEnd(elf_file->Begin() + size + offset);
787 // Ignore the optional .bss section when opening non-executable.
788 return Setup(abs_dex_location, error_msg);
789}
790
791bool ElfOatFile::Load(const std::string& elf_filename,
792 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
793 bool writable,
794 bool executable,
795 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800796 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800797 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
798 if (file == nullptr) {
799 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
800 return false;
801 }
802 return ElfOatFile::ElfFileOpen(file.get(),
803 oat_file_begin,
804 writable,
805 executable,
806 error_msg);
807}
808
809bool ElfOatFile::ElfFileOpen(File* file,
810 uint8_t* oat_file_begin,
811 bool writable,
812 bool executable,
813 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800814 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800815 // TODO: rename requested_base to oat_data_begin
816 elf_file_.reset(ElfFile::Open(file,
817 writable,
818 /*program_header_only*/true,
819 error_msg,
820 oat_file_begin));
821 if (elf_file_ == nullptr) {
822 DCHECK(!error_msg->empty());
823 return false;
824 }
825 bool loaded = elf_file_->Load(executable, error_msg);
826 DCHECK(loaded || !error_msg->empty());
827 return loaded;
828}
829
830//////////////////////////
831// General OatFile code //
832//////////////////////////
833
834std::string OatFile::ResolveRelativeEncodedDexLocation(
835 const char* abs_dex_location, const std::string& rel_dex_location) {
836 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
837 // Strip :classes<N>.dex used for secondary multidex files.
838 std::string base = DexFile::GetBaseLocation(rel_dex_location);
839 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
840
841 // Check if the base is a suffix of the provided abs_dex_location.
842 std::string target_suffix = "/" + base;
843 std::string abs_location(abs_dex_location);
844 if (abs_location.size() > target_suffix.size()) {
845 size_t pos = abs_location.size() - target_suffix.size();
846 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
847 return abs_location + multidex_suffix;
848 }
849 }
850 }
851 return rel_dex_location;
852}
853
854static void CheckLocation(const std::string& location) {
855 CHECK(!location.empty());
856}
857
858OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
859 const std::string& location,
860 const char* abs_dex_location,
861 std::string* error_msg) {
862 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
863 return oat_file->InitializeFromElfFile(elf_file, abs_dex_location, error_msg)
864 ? oat_file.release()
865 : nullptr;
866}
867
868OatFile* OatFile::Open(const std::string& filename,
869 const std::string& location,
870 uint8_t* requested_base,
871 uint8_t* oat_file_begin,
872 bool executable,
873 const char* abs_dex_location,
874 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800875 ScopedTrace trace("Open oat file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800876 CHECK(!filename.empty()) << location;
877 CheckLocation(location);
878 std::unique_ptr<OatFile> ret;
879
880 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
881 // disabled.
882 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(filename,
883 location,
884 requested_base,
885 oat_file_begin,
886 false,
887 executable,
888 abs_dex_location,
889 error_msg);
890 if (with_dlopen != nullptr) {
891 return with_dlopen;
892 }
893 if (kPrintDlOpenErrorMessage) {
894 LOG(ERROR) << "Failed to dlopen: " << *error_msg;
895 }
896
897 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
898 //
899 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
900 //
901 // We use our own ELF loader for Quick to deal with legacy apps that
902 // open a generated dex file by name, remove the file, then open
903 // another generated dex file with the same name. http://b/10614658
904 //
905 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
906 //
907 //
908 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
909 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
910 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(filename,
911 location,
912 requested_base,
913 oat_file_begin,
914 false,
915 executable,
916 abs_dex_location,
917 error_msg);
918 return with_internal;
919}
920
921OatFile* OatFile::OpenWritable(File* file,
922 const std::string& location,
923 const char* abs_dex_location,
924 std::string* error_msg) {
925 CheckLocation(location);
926 return ElfOatFile::OpenElfFile(file,
927 location,
928 nullptr,
929 nullptr,
930 true,
931 false,
932 abs_dex_location,
933 error_msg);
934}
935
936OatFile* OatFile::OpenReadable(File* file,
937 const std::string& location,
938 const char* abs_dex_location,
939 std::string* error_msg) {
940 CheckLocation(location);
941 return ElfOatFile::OpenElfFile(file,
942 location,
943 nullptr,
944 nullptr,
945 false,
946 false,
947 abs_dex_location,
948 error_msg);
949}
950
951OatFile::OatFile(const std::string& location, bool is_executable)
952 : location_(location),
953 begin_(nullptr),
954 end_(nullptr),
955 bss_begin_(nullptr),
956 bss_end_(nullptr),
957 is_executable_(is_executable),
958 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
959 CHECK(!location_.empty());
960}
961
962OatFile::~OatFile() {
963 STLDeleteElements(&oat_dex_files_storage_);
964}
965
Brian Carlstrome24fa612011-09-29 00:53:55 -0700966const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800967 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700968}
969
Ian Rogers13735952014-10-08 12:43:28 -0700970const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700971 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800972 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700973}
974
Ian Rogers13735952014-10-08 12:43:28 -0700975const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700976 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800977 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700978}
979
Vladimir Marko5c42c292015-02-25 12:02:49 +0000980const uint8_t* OatFile::BssBegin() const {
981 return bss_begin_;
982}
983
984const uint8_t* OatFile::BssEnd() const {
985 return bss_end_;
986}
987
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700988const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
989 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800990 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100991 // NOTE: We assume here that the canonical location for a given dex_location never
992 // changes. If it does (i.e. some symlink used by the filename changes) we may return
993 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
994 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100995
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100996 // TODO: Additional analysis of usage patterns to see if this can be simplified
997 // without any performance loss, for example by not doing the first lock-free lookup.
998
999 const OatFile::OatDexFile* oat_dex_file = nullptr;
1000 StringPiece key(dex_location);
1001 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1002 // directly mentioned in the oat file and doesn't require locking.
1003 auto primary_it = oat_dex_files_.find(key);
1004 if (primary_it != oat_dex_files_.end()) {
1005 oat_dex_file = primary_it->second;
1006 DCHECK(oat_dex_file != nullptr);
1007 } else {
1008 // This dex_location is not one of the dex locations directly mentioned in the
1009 // oat file. The correct lookup is via the canonical location but first see in
1010 // the secondary_oat_dex_files_ whether we've looked up this location before.
1011 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1012 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1013 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001014 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001015 } else {
1016 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001017 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001018 if (dex_canonical_location != dex_location) {
1019 StringPiece canonical_key(dex_canonical_location);
1020 auto canonical_it = oat_dex_files_.find(canonical_key);
1021 if (canonical_it != oat_dex_files_.end()) {
1022 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001023 } // else keep null.
1024 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001025
1026 // Copy the key to the string_cache_ and store the result in secondary map.
1027 string_cache_.emplace_back(key.data(), key.length());
1028 StringPiece key_copy(string_cache_.back());
1029 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001030 }
1031 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001032 if (oat_dex_file != nullptr &&
1033 (dex_location_checksum == nullptr ||
1034 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
1035 return oat_dex_file;
1036 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001037
1038 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001039 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001040 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -07001041 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001042 checksum = StringPrintf("0x%08x", *dex_location_checksum);
1043 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001044 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +01001045 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001046 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001047 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001048 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001049 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001050 << " contains OatDexFile " << odf->GetDexFileLocation()
1051 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
1052 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001053 }
Ian Rogers7fe2c692011-12-06 16:35:59 -08001054 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001055 }
Calin Juravle4e1d5792014-07-15 23:56:47 +01001056
Andreas Gampefa8429b2015-04-07 18:34:42 -07001057 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001058}
1059
Brian Carlstrome24fa612011-09-29 00:53:55 -07001060OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001061 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001062 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001063 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001064 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001065 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001066 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001067 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001068 : oat_file_(oat_file),
1069 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001070 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001071 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001072 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001073 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001074 oat_class_offsets_pointer_(oat_class_offsets_pointer),
1075 dex_cache_arrays_(dex_cache_arrays) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001076
1077OatFile::OatDexFile::~OatDexFile() {}
1078
Ian Rogers05f28c62012-10-23 18:12:13 -07001079size_t OatFile::OatDexFile::FileSize() const {
1080 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1081}
1082
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001083std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001084 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001085 return DexFile::Open(dex_file_pointer_,
1086 FileSize(),
1087 dex_file_location_,
1088 dex_file_location_checksum_,
1089 this,
1090 false /* verify */,
1091 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001092}
1093
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001094uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1095 return oat_class_offsets_pointer_[class_def_index];
1096}
1097
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001098OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001099 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001100
Ian Rogers13735952014-10-08 12:43:28 -07001101 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001102 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001103
Ian Rogers13735952014-10-08 12:43:28 -07001104 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001105 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1106 mirror::Class::Status status =
1107 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1108 CHECK_LT(status, mirror::Class::kStatusMax);
1109
Ian Rogers13735952014-10-08 12:43:28 -07001110 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001111 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1112 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1113 CHECK_LT(type, kOatClassMax);
1114
Ian Rogers13735952014-10-08 12:43:28 -07001115 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001116 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001117
Brian Carlstromcd937a92014-03-04 22:53:23 -08001118 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001119 const uint8_t* bitmap_pointer = nullptr;
1120 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001121 if (type != kOatClassNoneCompiled) {
1122 if (type == kOatClassSomeCompiled) {
1123 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1124 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1125 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1126 methods_pointer = bitmap_pointer + bitmap_size;
1127 } else {
1128 methods_pointer = after_type_pointer;
1129 }
1130 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001131 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001132
Richard Uhler07b3c232015-03-31 15:57:54 -07001133 return OatFile::OatClass(oat_file_,
1134 status,
1135 type,
1136 bitmap_size,
1137 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1138 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001139}
1140
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001141OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001142 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001143 OatClassType type,
1144 uint32_t bitmap_size,
1145 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001146 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001147 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001148 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001149 switch (type_) {
1150 case kOatClassAllCompiled: {
1151 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001152 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001153 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001154 break;
1155 }
1156 case kOatClassSomeCompiled: {
1157 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001158 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001159 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001160 break;
1161 }
1162 case kOatClassNoneCompiled: {
1163 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001164 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001165 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001166 break;
1167 }
1168 case kOatClassMax: {
1169 LOG(FATAL) << "Invalid OatClassType " << type_;
1170 break;
1171 }
1172 }
1173}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001174
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001175uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1176 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1177 if (oat_method_offsets == nullptr) {
1178 return 0u;
1179 }
1180 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1181}
1182
1183const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001184 // 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 -07001185 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001186 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001187 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001188 }
1189 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001190 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001191 CHECK_EQ(kOatClassAllCompiled, type_);
1192 methods_pointer_index = method_index;
1193 } else {
1194 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001195 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001196 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001197 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001198 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1199 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001200 }
1201 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001202 return &oat_method_offsets;
1203}
1204
1205const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1206 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1207 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001208 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001209 }
1210 if (oat_file_->IsExecutable() ||
1211 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001212 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001213 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001214 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001215 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1216 // version.
1217 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001218}
1219
Mathieu Chartiere401d142015-04-22 13:56:20 -07001220void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001221 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001222 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001223}
1224
Andreas Gampe7ba64962014-10-23 11:37:40 -07001225bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001226 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001227 // TODO: Check against oat_patches. b/18144996
1228}
1229
Sebastien Hertz0de11332015-05-13 12:14:05 +02001230bool OatFile::IsDebuggable() const {
1231 return GetOatHeader().IsDebuggable();
1232}
1233
David Brazdilce4b0ba2016-01-28 15:05:49 +00001234bool OatFile::IsExtractOnly() const {
1235 return GetOatHeader().IsExtractOnly();
1236}
1237
Andreas Gampe7848da42015-04-09 11:15:04 -07001238static constexpr char kDexClassPathEncodingSeparator = '*';
1239
1240std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1241 std::ostringstream out;
1242
1243 for (const DexFile* dex_file : dex_files) {
1244 out << dex_file->GetLocation().c_str();
1245 out << kDexClassPathEncodingSeparator;
1246 out << dex_file->GetLocationChecksum();
1247 out << kDexClassPathEncodingSeparator;
1248 }
1249
1250 return out.str();
1251}
1252
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001253bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001254 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1255 // No dependencies.
1256 return true;
1257 }
1258
1259 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1260 // Split() instead of manual parsing of the combined char*.
1261 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001262 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001263 if (split.size() % 2 != 0) {
1264 // Expected pairs of location and checksum.
1265 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1266 return false;
1267 }
1268
1269 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1270 std::string& location = *it;
1271 std::string& checksum = *(it + 1);
1272 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1273 if (converted == 0) {
1274 // Conversion error.
1275 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1276 return false;
1277 }
1278
1279 uint32_t dex_checksum;
1280 std::string error_msg;
1281 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001282 &dex_checksum,
1283 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001284 if (converted != dex_checksum) {
1285 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1286 location.c_str(), converted, dex_checksum);
1287 return false;
1288 }
1289 } else {
1290 // Problem retrieving checksum.
1291 // TODO: odex files?
1292 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1293 error_msg.c_str());
1294 return false;
1295 }
1296 }
1297
1298 return true;
1299}
1300
1301bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001302 std::vector<std::string>* locations) {
1303 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001304 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1305 return true;
1306 }
1307
1308 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1309 // Split() instead of manual parsing of the combined char*.
1310 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001311 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001312 if (split.size() % 2 != 0) {
1313 // Expected pairs of location and checksum.
1314 return false;
1315 }
1316
1317 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1318 locations->push_back(*it);
1319 }
1320
1321 return true;
1322}
1323
Brian Carlstrome24fa612011-09-29 00:53:55 -07001324} // namespace art