blob: 5c5523d9c8a0b34640ddfc5394dc13c95aba05ae [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>
David Srbecky1baabf02015-06-16 17:12:34 +000020#ifndef __APPLE__
21#include <link.h> // for dl_iterate_phdr.
22#endif
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include <unistd.h>
24
25#include <cstdlib>
26#include <cstring>
Richard Uhlere5fed032015-03-18 08:21:11 -070027#include <sstream>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include <type_traits>
Shubham Ajmerab22dea02017-10-04 18:36:41 -070029#include <sys/stat.h>
Richard Uhlere5fed032015-03-18 08:21:11 -070030
Andreas Gampefa8429b2015-04-07 18:34:42 -070031// dlopen_ext support from bionic.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010032#ifdef ART_TARGET_ANDROID
Andreas Gampefa8429b2015-04-07 18:34:42 -070033#include "android/dlext.h"
34#endif
35
Andreas Gampe875b4f22018-11-19 12:59:15 -080036#include <android-base/logging.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080037#include "android-base/stringprintf.h"
38
Andreas Gampec6ea7d02017-02-01 16:46:28 -080039#include "art_method.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070040#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070041#include "base/enums.h"
David Sehr891a50e2017-10-27 17:01:07 -070042#include "base/file_utils.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080043#include "base/logging.h" // For VLOG_IS_ON.
David Sehr79e26072018-04-06 17:58:50 -070044#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080045#include "base/os.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080046#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080047#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080048#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080049#include "base/utils.h"
David Sehr013fd802018-01-11 22:55:24 -080050#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080051#include "dex/dex_file_loader.h"
52#include "dex/dex_file_types.h"
53#include "dex/standard_dex_file.h"
David Sehr9c4a0152018-04-05 12:23:54 -070054#include "dex/type_lookup_table.h"
David Sehr0225f8e2018-01-31 08:52:24 +000055#include "dex/utf-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070057#include "elf_utils.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000058#include "gc_root.h"
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +010059#include "gc/space/image_space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070061#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070062#include "oat.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010063#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070064#include "oat_file_manager.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070065#include "runtime.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010066#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070067
68namespace art {
69
Andreas Gampe46ee31b2016-12-14 10:11:49 -080070using android::base::StringPrintf;
71
Andreas Gampe049cff02015-12-01 23:27:12 -080072// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000073static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070074
Andreas Gampe049cff02015-12-01 23:27:12 -080075// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070076// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
77// multiple times). However, if/when we switch the above, we likely want to switch this, too,
78// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000079static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070080
81// For debugging, Open will print DlOpen error message if set to true.
82static constexpr bool kPrintDlOpenErrorMessage = false;
83
Andreas Gampe049cff02015-12-01 23:27:12 -080084// Note for OatFileBase and descendents:
85//
86// These are used in OatFile::Open to try all our loaders.
87//
88// The process is simple:
89//
90// 1) Allocate an instance through the standard constructor (location, executable)
91// 2) Load() to try to open the file.
92// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
93// 4) PreSetup() for any steps that should be done before the final setup.
94// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070095
Andreas Gampe049cff02015-12-01 23:27:12 -080096class OatFileBase : public OatFile {
97 public:
98 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070099
Andreas Gampe049cff02015-12-01 23:27:12 -0800100 template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100101 static OatFileBase* OpenOatFile(int zip_fd,
102 const std::string& vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100103 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -0700104 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800105 bool writable,
106 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800107 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -0700108 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100109 /*inout*/MemMap* reservation, // Where to load if not null.
110 /*out*/std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800111
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700112 template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100113 static OatFileBase* OpenOatFile(int zip_fd,
114 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700115 int oat_fd,
116 const std::string& vdex_filename,
117 const std::string& oat_filename,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700118 bool writable,
119 bool executable,
120 bool low_4gb,
121 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100122 /*inout*/MemMap* reservation, // Where to load if not null.
123 /*out*/std::string* error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700124
Andreas Gampe049cff02015-12-01 23:27:12 -0800125 protected:
126 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700127
Andreas Gampe049cff02015-12-01 23:27:12 -0800128 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
129 std::string* error_msg) const = 0;
130
Andreas Gampe4075f832016-05-18 13:09:54 -0700131 virtual void PreLoad() = 0;
132
David Brazdil7b49e6c2016-09-01 11:06:18 +0100133 bool LoadVdex(const std::string& vdex_filename,
134 bool writable,
135 bool low_4gb,
136 std::string* error_msg);
137
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700138 bool LoadVdex(int vdex_fd,
139 const std::string& vdex_filename,
140 bool writable,
141 bool low_4gb,
142 std::string* error_msg);
143
Andreas Gampe049cff02015-12-01 23:27:12 -0800144 virtual bool Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800145 bool writable,
146 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800147 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100148 /*inout*/MemMap* reservation, // Where to load if not null.
149 /*out*/std::string* error_msg) = 0;
Andreas Gampe049cff02015-12-01 23:27:12 -0800150
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700151 virtual bool Load(int oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700152 bool writable,
153 bool executable,
154 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100155 /*inout*/MemMap* reservation, // Where to load if not null.
156 /*out*/std::string* error_msg) = 0;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700157
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100158 bool ComputeFields(const std::string& file_path, std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800159
160 virtual void PreSetup(const std::string& elf_filename) = 0;
161
Nicolas Geoffray30025092018-04-19 14:43:29 +0100162 bool Setup(int zip_fd, const char* abs_dex_location, std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800163
164 // Setters exposed for ElfOatFile.
165
166 void SetBegin(const uint8_t* begin) {
167 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700168 }
169
Andreas Gampe049cff02015-12-01 23:27:12 -0800170 void SetEnd(const uint8_t* end) {
171 end_ = end;
172 }
173
David Brazdilc93b3be2016-09-12 18:49:58 +0100174 void SetVdex(VdexFile* vdex) {
175 vdex_.reset(vdex);
176 }
177
Andreas Gampe049cff02015-12-01 23:27:12 -0800178 private:
179 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
180};
181
182template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100183OatFileBase* OatFileBase::OpenOatFile(int zip_fd,
184 const std::string& vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100185 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800186 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800187 bool writable,
188 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800189 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800190 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100191 /*inout*/MemMap* reservation,
192 /*out*/std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800193 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700194
195 ret->PreLoad();
196
Andreas Gampe049cff02015-12-01 23:27:12 -0800197 if (!ret->Load(elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800198 writable,
199 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800200 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100201 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -0800202 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800203 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800204 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800205
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100206 if (!ret->ComputeFields(elf_filename, error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800207 return nullptr;
208 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700209
David Sehr2300b2d2018-05-10 14:20:10 -0700210 ret->PreSetup(elf_filename);
211
David Srbeckyec2cdf42017-12-08 16:21:25 +0000212 if (!ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
213 return nullptr;
214 }
215
Nicolas Geoffray30025092018-04-19 14:43:29 +0100216 if (!ret->Setup(zip_fd, abs_dex_location, error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800217 return nullptr;
218 }
219
Dave Allison69dfe512014-07-11 17:11:58 +0000220 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800221}
222
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700223template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100224OatFileBase* OatFileBase::OpenOatFile(int zip_fd,
225 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700226 int oat_fd,
227 const std::string& vdex_location,
228 const std::string& oat_location,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700229 bool writable,
230 bool executable,
231 bool low_4gb,
232 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100233 /*inout*/MemMap* reservation,
234 /*out*/std::string* error_msg) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700235 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(oat_location, executable));
236
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700237 if (!ret->Load(oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700238 writable,
239 executable,
240 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100241 reservation,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700242 error_msg)) {
243 return nullptr;
244 }
245
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100246 if (!ret->ComputeFields(oat_location, error_msg)) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700247 return nullptr;
248 }
249
David Sehr2300b2d2018-05-10 14:20:10 -0700250 ret->PreSetup(oat_location);
251
David Srbeckyec2cdf42017-12-08 16:21:25 +0000252 if (!ret->LoadVdex(vdex_fd, vdex_location, writable, low_4gb, error_msg)) {
253 return nullptr;
254 }
255
Nicolas Geoffray30025092018-04-19 14:43:29 +0100256 if (!ret->Setup(zip_fd, abs_dex_location, error_msg)) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700257 return nullptr;
258 }
259
260 return ret.release();
261}
262
David Brazdil7b49e6c2016-09-01 11:06:18 +0100263bool OatFileBase::LoadVdex(const std::string& vdex_filename,
264 bool writable,
265 bool low_4gb,
266 std::string* error_msg) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000267 vdex_ = VdexFile::OpenAtAddress(vdex_begin_,
268 vdex_end_ - vdex_begin_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100269 /*mmap_reuse=*/ vdex_begin_ != nullptr,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000270 vdex_filename,
271 writable,
272 low_4gb,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700273 /* unquicken=*/ false,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000274 error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100275 if (vdex_.get() == nullptr) {
276 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
277 vdex_filename.c_str(),
278 error_msg->c_str());
279 return false;
280 }
281 return true;
282}
283
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700284bool OatFileBase::LoadVdex(int vdex_fd,
285 const std::string& vdex_filename,
286 bool writable,
287 bool low_4gb,
288 std::string* error_msg) {
289 if (vdex_fd != -1) {
290 struct stat s;
291 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd, &s));
292 if (rc == -1) {
293 PLOG(WARNING) << "Failed getting length of vdex file";
294 } else {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000295 vdex_ = VdexFile::OpenAtAddress(vdex_begin_,
296 vdex_end_ - vdex_begin_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100297 /*mmap_reuse=*/ vdex_begin_ != nullptr,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000298 vdex_fd,
299 s.st_size,
300 vdex_filename,
301 writable,
302 low_4gb,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100303 /*unquicken=*/ false,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000304 error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700305 if (vdex_.get() == nullptr) {
306 *error_msg = "Failed opening vdex file.";
307 return false;
308 }
309 }
310 }
311 return true;
312}
313
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100314bool OatFileBase::ComputeFields(const std::string& file_path, std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800315 std::string symbol_error_msg;
316 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700317 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800318 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
319 file_path.c_str(),
320 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700321 return false;
322 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800323 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700324 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800325 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
326 file_path.c_str(),
327 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700328 return false;
329 }
330 // Readjust to be non-inclusive upper bound.
331 end_ += sizeof(uint32_t);
332
Vladimir Markob066d432018-01-03 13:14:37 +0000333 data_bimg_rel_ro_begin_ = FindDynamicSymbolAddress("oatdatabimgrelro", &symbol_error_msg);
334 if (data_bimg_rel_ro_begin_ != nullptr) {
335 data_bimg_rel_ro_end_ =
336 FindDynamicSymbolAddress("oatdatabimgrelrolastword", &symbol_error_msg);
337 if (data_bimg_rel_ro_end_ == nullptr) {
338 *error_msg =
339 StringPrintf("Failed to find oatdatabimgrelrolastword symbol in '%s'", file_path.c_str());
340 return false;
341 }
342 // Readjust to be non-inclusive upper bound.
343 data_bimg_rel_ro_end_ += sizeof(uint32_t);
344 }
345
Andreas Gampe049cff02015-12-01 23:27:12 -0800346 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700347 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800348 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700349 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700350 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800351 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700352 if (bss_end_ == nullptr) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000353 *error_msg = StringPrintf("Failed to find oatbsslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700354 return false;
355 }
356 // Readjust to be non-inclusive upper bound.
357 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100358 // Find bss methods if present.
359 bss_methods_ =
360 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000361 // Find bss roots if present.
362 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700363 }
364
David Srbeckyec2cdf42017-12-08 16:21:25 +0000365 vdex_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdex", &symbol_error_msg));
366 if (vdex_begin_ == nullptr) {
367 // No .vdex section.
368 vdex_end_ = nullptr;
369 } else {
370 vdex_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdexlastword", &symbol_error_msg));
371 if (vdex_end_ == nullptr) {
372 *error_msg = StringPrintf("Failed to find oatdexlastword symbol in '%s'", file_path.c_str());
373 return false;
374 }
375 // Readjust to be non-inclusive upper bound.
376 vdex_end_ += sizeof(uint32_t);
377 }
378
Andreas Gampe049cff02015-12-01 23:27:12 -0800379 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800380}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800381
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100382// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
383// position by the number of bytes read, i.e. sizeof(T).
384// Return true on success, false if the read would go beyond the end of the OatFile.
385template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100386inline static bool ReadOatDexFileData(const OatFile& oat_file,
387 /*inout*/const uint8_t** oat,
388 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100389 DCHECK(oat != nullptr);
390 DCHECK(value != nullptr);
391 DCHECK_LE(*oat, oat_file.End());
392 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
393 return false;
394 }
395 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
Andreas Gampec55bb392018-09-21 00:02:02 +0000396 using unaligned_type __attribute__((__aligned__(1))) = T;
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100397 *value = *reinterpret_cast<const unaligned_type*>(*oat);
398 *oat += sizeof(T);
399 return true;
400}
401
Vladimir Markof3c52b42017-11-17 17:32:12 +0000402static bool ReadIndexBssMapping(OatFile* oat_file,
403 /*inout*/const uint8_t** oat,
404 size_t dex_file_index,
405 const std::string& dex_file_location,
406 const char* tag,
407 /*out*/const IndexBssMapping** mapping,
408 std::string* error_msg) {
409 uint32_t index_bss_mapping_offset;
410 if (UNLIKELY(!ReadOatDexFileData(*oat_file, oat, &index_bss_mapping_offset))) {
411 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
412 "after %s bss mapping offset",
413 oat_file->GetLocation().c_str(),
414 dex_file_index,
415 dex_file_location.c_str(),
416 tag);
417 return false;
418 }
419 const bool readable_index_bss_mapping_size =
420 index_bss_mapping_offset != 0u &&
421 index_bss_mapping_offset <= oat_file->Size() &&
422 IsAligned<alignof(IndexBssMapping)>(index_bss_mapping_offset) &&
423 oat_file->Size() - index_bss_mapping_offset >= IndexBssMapping::ComputeSize(0);
424 const IndexBssMapping* index_bss_mapping = readable_index_bss_mapping_size
425 ? reinterpret_cast<const IndexBssMapping*>(oat_file->Begin() + index_bss_mapping_offset)
426 : nullptr;
427 if (index_bss_mapping_offset != 0u &&
428 (UNLIKELY(index_bss_mapping == nullptr) ||
429 UNLIKELY(index_bss_mapping->size() == 0u) ||
430 UNLIKELY(oat_file->Size() - index_bss_mapping_offset <
431 IndexBssMapping::ComputeSize(index_bss_mapping->size())))) {
432 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
433 " truncated %s bss mapping, offset %u of %zu, length %zu",
434 oat_file->GetLocation().c_str(),
435 dex_file_index,
436 dex_file_location.c_str(),
437 tag,
438 index_bss_mapping_offset,
439 oat_file->Size(),
440 index_bss_mapping != nullptr ? index_bss_mapping->size() : 0u);
441 return false;
442 }
443
444 *mapping = index_bss_mapping;
445 return true;
446}
447
448static void DCheckIndexToBssMapping(OatFile* oat_file,
449 uint32_t number_of_indexes,
450 size_t slot_size,
451 const IndexBssMapping* index_bss_mapping) {
452 if (kIsDebugBuild && index_bss_mapping != nullptr) {
453 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
454 const IndexBssMappingEntry* prev_entry = nullptr;
455 for (const IndexBssMappingEntry& entry : *index_bss_mapping) {
456 CHECK_ALIGNED_PARAM(entry.bss_offset, slot_size);
457 // When loading a non-executable ElfOatFile, .bss symbols are not even
458 // looked up, so we cannot verify the offset against BssSize().
459 if (oat_file->IsExecutable()) {
460 CHECK_LT(entry.bss_offset, oat_file->BssSize());
461 }
462 uint32_t mask = entry.GetMask(index_bits);
463 CHECK_LE(POPCOUNT(mask) * slot_size, entry.bss_offset);
464 size_t index_mask_span = (mask != 0u) ? 32u - index_bits - CTZ(mask) : 0u;
465 CHECK_LE(index_mask_span, entry.GetIndex(index_bits));
466 if (prev_entry != nullptr) {
467 CHECK_LT(prev_entry->GetIndex(index_bits), entry.GetIndex(index_bits) - index_mask_span);
468 }
469 prev_entry = &entry;
470 }
Andreas Gampe875b4f22018-11-19 12:59:15 -0800471 CHECK(prev_entry != nullptr);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000472 CHECK_LT(prev_entry->GetIndex(index_bits), number_of_indexes);
473 }
474}
475
Nicolas Geoffray30025092018-04-19 14:43:29 +0100476bool OatFileBase::Setup(int zip_fd, const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700477 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800478 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100479 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
480 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800481 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700482 return false;
483 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100484 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
485 size_t key_value_store_size =
486 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
487 if (Size() < sizeof(OatHeader) + key_value_store_size) {
488 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
489 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100490 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100491 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100492 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100493 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700494 return false;
495 }
496
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100497 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
498 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
499 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
500 "%zu is not in [%zu, %zu]",
501 GetLocation().c_str(),
502 oat_dex_files_offset,
503 GetOatHeader().GetHeaderSize(),
504 Size());
505 return false;
506 }
507 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
508
Vladimir Markob066d432018-01-03 13:14:37 +0000509 if (!IsAligned<sizeof(uint32_t)>(data_bimg_rel_ro_begin_) ||
510 !IsAligned<sizeof(uint32_t)>(data_bimg_rel_ro_end_) ||
511 data_bimg_rel_ro_begin_ > data_bimg_rel_ro_end_) {
512 *error_msg = StringPrintf("In oat file '%s' found unaligned or unordered databimgrelro "
513 "symbol(s): begin = %p, end = %p",
514 GetLocation().c_str(),
515 data_bimg_rel_ro_begin_,
516 data_bimg_rel_ro_end_);
517 return false;
518 }
519
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100520 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
521 if (!IsAligned<kPageSize>(bss_begin_) ||
522 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
523 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000524 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
525 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100526 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000527 GetLocation().c_str(),
528 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100529 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000530 bss_roots_,
531 bss_end_);
532 return false;
533 }
534
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100535 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
536 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
537 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
538 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
Vladimir Marko0f3c7002017-09-07 14:15:56 +0100539 "begin = %p, methods = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000540 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000541 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100542 bss_methods_,
543 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000544 bss_end_);
545 return false;
546 }
547
Vladimir Markoe47f60c2018-02-21 13:43:28 +0000548 if (bss_methods_ != nullptr && bss_methods_ != bss_begin_) {
549 *error_msg = StringPrintf("In oat file '%s' found unexpected .bss gap before 'oatbssmethods': "
550 "begin = %p, methods = %p",
551 GetLocation().c_str(),
552 bss_begin_,
553 bss_methods_);
554 return false;
555 }
556
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100557 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
558 oat_dex_files_storage_.reserve(dex_file_count);
559 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100560 uint32_t dex_file_location_size;
561 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
562 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
563 "location size",
564 GetLocation().c_str(),
565 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700566 return false;
567 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100568 if (UNLIKELY(dex_file_location_size == 0U)) {
569 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
570 GetLocation().c_str(),
571 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700572 return false;
573 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000574 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100575 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
576 "location",
577 GetLocation().c_str(),
578 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700579 return false;
580 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000581 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
582 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700583
Richard Uhlere5fed032015-03-18 08:21:11 -0700584 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
585 abs_dex_location,
586 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700587
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100588 uint32_t dex_file_checksum;
589 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
590 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
591 "dex file checksum",
592 GetLocation().c_str(),
593 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700594 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700595 return false;
596 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700597
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100598 uint32_t dex_file_offset;
599 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
600 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
601 "after dex file offsets",
602 GetLocation().c_str(),
603 i,
604 dex_file_location.c_str());
605 return false;
606 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100607 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100608 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
609 "offset %u > %zu",
610 GetLocation().c_str(),
611 i,
612 dex_file_location.c_str(),
613 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100614 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700615 return false;
616 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000617 const uint8_t* dex_file_pointer = nullptr;
618 if (UNLIKELY(dex_file_offset == 0U)) {
619 if (uncompressed_dex_files_ == nullptr) {
Andreas Gampefc604a72018-02-08 15:43:37 -0800620 // Do not support mixed-mode oat files.
621 if (i > 0) {
622 *error_msg = StringPrintf("In oat file '%s', unsupported uncompressed-dex-file for dex "
623 "file %zu (%s)",
624 GetLocation().c_str(),
625 i,
626 dex_file_location.c_str());
627 return false;
628 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000629 uncompressed_dex_files_.reset(new std::vector<std::unique_ptr<const DexFile>>());
630 // No dex files, load it from location.
631 const ArtDexFileLoader dex_file_loader;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100632 bool loaded = false;
633 if (zip_fd != -1) {
634 loaded = dex_file_loader.OpenZip(zip_fd,
635 dex_file_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100636 /*verify=*/ false,
637 /*verify_checksum=*/ false,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100638 error_msg,
639 uncompressed_dex_files_.get());
640 } else {
641 loaded = dex_file_loader.Open(dex_file_location.c_str(),
642 dex_file_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100643 /*verify=*/ false,
644 /*verify_checksum=*/ false,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100645 error_msg,
646 uncompressed_dex_files_.get());
647 }
648 if (!loaded) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000649 if (Runtime::Current() == nullptr) {
650 // If there's no runtime, we're running oatdump, so return
651 // a half constructed oat file that oatdump knows how to deal with.
652 LOG(WARNING) << "Could not find associated dex files of oat file. "
653 << "Oatdump will only dump the header.";
654 return true;
655 } else {
656 return false;
657 }
658 }
Andreas Gampefc604a72018-02-08 15:43:37 -0800659 // The oat file may be out of date wrt/ the dex-file location. We need to be defensive
660 // here and ensure that at least the number of dex files still matches.
661 // Note: actual checksum comparisons are the duty of the OatFileAssistant and will be
662 // done after loading the OatFile.
663 if (uncompressed_dex_files_->size() != dex_file_count) {
664 *error_msg = StringPrintf("In oat file '%s', expected %u uncompressed dex files, but "
665 "found %zu in '%s'",
666 GetLocation().c_str(),
667 dex_file_count,
668 uncompressed_dex_files_->size(),
669 dex_file_location.c_str());
670 return false;
671 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000672 }
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100673 dex_file_pointer = (*uncompressed_dex_files_)[i]->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000674 } else {
Andreas Gampefc604a72018-02-08 15:43:37 -0800675 // Do not support mixed-mode oat files.
676 if (uncompressed_dex_files_ != nullptr) {
677 *error_msg = StringPrintf("In oat file '%s', unsupported embedded dex-file for dex file "
678 "%zu (%s)",
679 GetLocation().c_str(),
680 i,
681 dex_file_location.c_str());
682 return false;
683 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000684 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
685 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
686 "offset %u of %zu but the size of dex file header is %zu",
687 GetLocation().c_str(),
688 i,
689 dex_file_location.c_str(),
690 dex_file_offset,
691 DexSize(),
692 sizeof(DexFile::Header));
693 return false;
694 }
695 dex_file_pointer = DexBegin() + dex_file_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000696 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800697
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700698 const bool valid_magic = DexFileLoader::IsMagicValid(dex_file_pointer);
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700699 if (UNLIKELY(!valid_magic)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100700 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
701 "dex file magic '%s'",
702 GetLocation().c_str(),
703 i,
704 dex_file_location.c_str(),
705 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700706 return false;
707 }
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700708 if (UNLIKELY(!DexFileLoader::IsVersionAndMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100709 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
710 "dex file version '%s'",
711 GetLocation().c_str(),
712 i,
713 dex_file_location.c_str(),
714 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700715 return false;
716 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800717 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000718 if (dex_file_offset != 0 && (DexSize() - dex_file_offset < header->file_size_)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000719 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
720 "offset %u and size %u truncated at %zu",
721 GetLocation().c_str(),
722 i,
723 dex_file_location.c_str(),
724 dex_file_offset,
725 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100726 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000727 return false;
728 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300729
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000730 uint32_t class_offsets_offset;
731 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
732 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
733 "after class offsets offset",
734 GetLocation().c_str(),
735 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300736 dex_file_location.c_str());
737 return false;
738 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000739 if (UNLIKELY(class_offsets_offset > Size()) ||
740 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
741 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
742 "class offsets, offset %u of %zu, class defs %u",
743 GetLocation().c_str(),
744 i,
745 dex_file_location.c_str(),
746 class_offsets_offset,
747 Size(),
748 header->class_defs_size_);
749 return false;
750 }
751 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
752 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
753 "class offsets, offset %u",
754 GetLocation().c_str(),
755 i,
756 dex_file_location.c_str(),
757 class_offsets_offset);
758 return false;
759 }
760 const uint32_t* class_offsets_pointer =
761 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
762
763 uint32_t lookup_table_offset;
764 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
765 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
766 "after lookup table offset",
767 GetLocation().c_str(),
768 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300769 dex_file_location.c_str());
770 return false;
771 }
772 const uint8_t* lookup_table_data = lookup_table_offset != 0u
773 ? Begin() + lookup_table_offset
774 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000775 if (lookup_table_offset != 0u &&
776 (UNLIKELY(lookup_table_offset > Size()) ||
777 UNLIKELY(Size() - lookup_table_offset <
778 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100779 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000780 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100781 GetLocation().c_str(),
782 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000783 dex_file_location.c_str(),
784 lookup_table_offset,
785 Size(),
786 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700787 return false;
788 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700789
Mathieu Chartier120aa282017-08-05 16:03:03 -0700790 uint32_t dex_layout_sections_offset;
791 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) {
792 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
793 "after dex layout sections offset",
794 GetLocation().c_str(),
795 i,
796 dex_file_location.c_str());
797 return false;
798 }
799 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0
800 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset)
801 : nullptr;
802
Vladimir Markof3c52b42017-11-17 17:32:12 +0000803 const IndexBssMapping* method_bss_mapping;
804 const IndexBssMapping* type_bss_mapping;
805 const IndexBssMapping* string_bss_mapping;
806 if (!ReadIndexBssMapping(
807 this, &oat, i, dex_file_location, "method", &method_bss_mapping, error_msg) ||
808 !ReadIndexBssMapping(
809 this, &oat, i, dex_file_location, "type", &type_bss_mapping, error_msg) ||
810 !ReadIndexBssMapping(
811 this, &oat, i, dex_file_location, "string", &string_bss_mapping, error_msg)) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100812 return false;
813 }
Vladimir Markof3c52b42017-11-17 17:32:12 +0000814 DCheckIndexToBssMapping(
815 this, header->method_ids_size_, static_cast<size_t>(pointer_size), method_bss_mapping);
816 DCheckIndexToBssMapping(
817 this, header->type_ids_size_, sizeof(GcRoot<mirror::Class>), type_bss_mapping);
818 DCheckIndexToBssMapping(
819 this, header->string_ids_size_, sizeof(GcRoot<mirror::String>), string_bss_mapping);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100820
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700821 std::string canonical_location =
822 DexFileLoader::GetDexCanonicalLocation(dex_file_location.c_str());
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100823
824 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100825 OatDexFile* oat_dex_file = new OatDexFile(this,
826 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100827 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100828 dex_file_checksum,
829 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300830 lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100831 method_bss_mapping,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000832 type_bss_mapping,
833 string_bss_mapping,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000834 class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700835 dex_layout_sections);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100836 oat_dex_files_storage_.push_back(oat_dex_file);
837
838 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100839 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100840 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100841 if (canonical_location != dex_file_location) {
842 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
843 oat_dex_files_.Put(canonical_key, oat_dex_file);
844 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700845 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100846
Vladimir Markob066d432018-01-03 13:14:37 +0000847 Runtime* runtime = Runtime::Current();
848
849 if (DataBimgRelRoBegin() != nullptr) {
850 // Make .data.bimg.rel.ro read only. ClassLinker shall make it writable for relocation.
851 uint8_t* reloc_begin = const_cast<uint8_t*>(DataBimgRelRoBegin());
852 CheckedCall(mprotect, "protect relocations", reloc_begin, DataBimgRelRoSize(), PROT_READ);
853 if (UNLIKELY(runtime == nullptr)) {
854 // This must be oatdump without boot image.
855 } else if (!IsExecutable()) {
856 // Do not check whether we have a boot image if the oat file is not executable.
857 } else if (UNLIKELY(runtime->GetHeap()->GetBootImageSpaces().empty())) {
858 *error_msg = StringPrintf("Cannot load oat file '%s' with .data.bimg.rel.ro as executable "
859 "without boot image.",
860 GetLocation().c_str());
861 return false;
862 } else {
863 // ClassLinker shall perform the relocation when we register a dex file from
864 // this oat file. We do not do the relocation here to avoid dirtying the pages
865 // if the code is never actually ready to be executed.
866 }
867 }
868
Brian Carlstromf1b30302013-03-28 10:35:32 -0700869 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700870}
871
Andreas Gampe049cff02015-12-01 23:27:12 -0800872////////////////////////
873// OatFile via dlopen //
874////////////////////////
875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100876class DlOpenOatFile final : public OatFileBase {
Andreas Gampe049cff02015-12-01 23:27:12 -0800877 public:
878 DlOpenOatFile(const std::string& filename, bool executable)
879 : OatFileBase(filename, executable),
880 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700881 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800882 }
883
884 ~DlOpenOatFile() {
885 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700886 if (!kIsTargetBuild) {
887 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
888 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700889 dlclose(dlopen_handle_);
890 } else {
891 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700892 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800893 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800894 }
895
896 protected:
897 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100898 std::string* error_msg) const override {
Andreas Gampe049cff02015-12-01 23:27:12 -0800899 const uint8_t* ptr =
900 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
901 if (ptr == nullptr) {
902 *error_msg = dlerror();
903 }
904 return ptr;
905 }
906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100907 void PreLoad() override;
Andreas Gampe4075f832016-05-18 13:09:54 -0700908
Andreas Gampe049cff02015-12-01 23:27:12 -0800909 bool Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800910 bool writable,
911 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800912 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100913 /*inout*/MemMap* reservation, // Where to load if not null.
914 /*out*/std::string* error_msg) override;
Andreas Gampe049cff02015-12-01 23:27:12 -0800915
Vladimir Markoc09cd052018-08-23 16:36:36 +0100916 bool Load(int oat_fd ATTRIBUTE_UNUSED,
917 bool writable ATTRIBUTE_UNUSED,
918 bool executable ATTRIBUTE_UNUSED,
919 bool low_4gb ATTRIBUTE_UNUSED,
920 /*inout*/MemMap* reservation ATTRIBUTE_UNUSED,
921 /*out*/std::string* error_msg ATTRIBUTE_UNUSED) override {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700922 return false;
923 }
924
Andreas Gampe049cff02015-12-01 23:27:12 -0800925 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100926 void PreSetup(const std::string& elf_filename) override;
Andreas Gampe049cff02015-12-01 23:27:12 -0800927
928 private:
929 bool Dlopen(const std::string& elf_filename,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100930 /*inout*/MemMap* reservation, // Where to load if not null.
931 /*out*/std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800932
Richard Uhlera206c742016-05-24 15:04:22 -0700933 // On the host, if the same library is loaded again with dlopen the same
934 // file handle is returned. This differs from the behavior of dlopen on the
935 // target, where dlopen reloads the library at a different address every
936 // time you load it. The runtime relies on the target behavior to ensure
937 // each instance of the loaded library has a unique dex cache. To avoid
938 // problems, we fall back to our own linker in the case when the same
939 // library is opened multiple times on host. dlopen_handles_ is used to
940 // detect that case.
941 // Guarded by host_dlopen_handles_lock_;
942 static std::unordered_set<void*> host_dlopen_handles_;
943
Vladimir Markoc09cd052018-08-23 16:36:36 +0100944 // Reservation and dummy memory map objects corresponding to the regions mapped by dlopen.
945 // Note: Must be destroyed after dlclose() as it can hold the owning reservation.
946 std::vector<MemMap> dlopen_mmaps_;
947
Andreas Gampe049cff02015-12-01 23:27:12 -0800948 // dlopen handle during runtime.
949 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
950
Andreas Gampe4075f832016-05-18 13:09:54 -0700951 // The number of shared objects the linker told us about before loading. Used to
952 // (optimistically) optimize the PreSetup stage (see comment there).
953 size_t shared_objects_before_;
954
Andreas Gampe049cff02015-12-01 23:27:12 -0800955 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
956};
957
Richard Uhlera206c742016-05-24 15:04:22 -0700958std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
959
Andreas Gampe4075f832016-05-18 13:09:54 -0700960void DlOpenOatFile::PreLoad() {
961#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700962 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700963 LOG(FATAL) << "Should not reach here.";
964 UNREACHABLE();
965#else
966 // Count the entries in dl_iterate_phdr we get at this point in time.
967 struct dl_iterate_context {
Vladimir Markoc09cd052018-08-23 16:36:36 +0100968 static int callback(dl_phdr_info* info ATTRIBUTE_UNUSED,
Andreas Gampe4075f832016-05-18 13:09:54 -0700969 size_t size ATTRIBUTE_UNUSED,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100970 void* data) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700971 reinterpret_cast<dl_iterate_context*>(data)->count++;
972 return 0; // Continue iteration.
973 }
974 size_t count = 0;
975 } context;
976
977 dl_iterate_phdr(dl_iterate_context::callback, &context);
978 shared_objects_before_ = context.count;
979#endif
980}
981
Andreas Gampe049cff02015-12-01 23:27:12 -0800982bool DlOpenOatFile::Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800983 bool writable,
984 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800985 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100986 /*inout*/MemMap* reservation, // Where to load if not null.
987 /*out*/std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800988 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
989 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
990 // !executable is a sign that we may want to patch), which may not be allowed for
991 // various reasons.
992 if (!kUseDlopen) {
993 *error_msg = "DlOpen is disabled.";
994 return false;
995 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800996 if (low_4gb) {
997 *error_msg = "DlOpen does not support low 4gb loading.";
998 return false;
999 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001000 if (writable) {
1001 *error_msg = "DlOpen does not support writable loading.";
1002 return false;
1003 }
1004 if (!executable) {
1005 *error_msg = "DlOpen does not support non-executable loading.";
1006 return false;
1007 }
1008
1009 // dlopen always returns the same library if it is already opened on the host. For this reason
1010 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
1011 // the same library loaded multiple times at different addresses is required for class unloading
1012 // and for having dex caches arrays in the .bss section.
1013 if (!kIsTargetBuild) {
1014 if (!kUseDlopenOnHost) {
1015 *error_msg = "DlOpen disabled for host.";
1016 return false;
1017 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001018 }
1019
Vladimir Markoc09cd052018-08-23 16:36:36 +01001020 bool success = Dlopen(elf_filename, reservation, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001021 DCHECK(dlopen_handle_ != nullptr || !success);
1022
1023 return success;
1024}
1025
1026bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001027 /*inout*/MemMap* reservation,
1028 /*out*/std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001029#ifdef __APPLE__
1030 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
1031 // but let's fallback to the custom loading code for the time being.
Vladimir Marko3ec8fb62018-08-31 17:47:38 +01001032 UNUSED(elf_filename, reservation);
Andreas Gampe049cff02015-12-01 23:27:12 -08001033 *error_msg = "Dlopen unsupported on Mac.";
1034 return false;
1035#else
1036 {
1037 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
1038 if (absolute_path == nullptr) {
1039 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
1040 return false;
1041 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +01001042#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +00001043 android_dlextinfo extinfo = {};
Vladimir Markof6cfd002018-11-01 16:53:31 +00001044 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; // Force-load, don't reuse handle
1045 // (open oat files multiple times).
Vladimir Markoc09cd052018-08-23 16:36:36 +01001046 if (reservation != nullptr) {
1047 if (!reservation->IsValid()) {
1048 *error_msg = StringPrintf("Invalid reservation for %s", elf_filename.c_str());
1049 return false;
1050 }
1051 extinfo.flags |= ANDROID_DLEXT_RESERVED_ADDRESS; // Use the reserved memory range.
1052 extinfo.reserved_addr = reservation->Begin();
1053 extinfo.reserved_size = reservation->Size();
1054 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001055 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
Vladimir Markoc09cd052018-08-23 16:36:36 +01001056 if (reservation != nullptr && dlopen_handle_ != nullptr) {
1057 // Find used pages from the reservation.
1058 struct dl_iterate_context {
1059 static int callback(dl_phdr_info* info, size_t size ATTRIBUTE_UNUSED, void* data) {
1060 auto* context = reinterpret_cast<dl_iterate_context*>(data);
1061 static_assert(std::is_same<Elf32_Half, Elf64_Half>::value, "Half must match");
1062 using Elf_Half = Elf64_Half;
1063
1064 // See whether this callback corresponds to the file which we have just loaded.
1065 uint8_t* reservation_begin = context->reservation->Begin();
1066 bool contained_in_reservation = false;
1067 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
1068 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1069 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1070 info->dlpi_phdr[i].p_vaddr);
1071 size_t memsz = info->dlpi_phdr[i].p_memsz;
1072 size_t offset = static_cast<size_t>(vaddr - reservation_begin);
1073 if (offset < context->reservation->Size()) {
1074 contained_in_reservation = true;
1075 DCHECK_LE(memsz, context->reservation->Size() - offset);
1076 } else if (vaddr < reservation_begin) {
1077 // Check that there's no overlap with the reservation.
1078 DCHECK_LE(memsz, static_cast<size_t>(reservation_begin - vaddr));
1079 }
1080 break; // It is sufficient to check the first PT_LOAD header.
1081 }
1082 }
1083
1084 if (contained_in_reservation) {
1085 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
1086 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1087 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1088 info->dlpi_phdr[i].p_vaddr);
1089 size_t memsz = info->dlpi_phdr[i].p_memsz;
1090 size_t offset = static_cast<size_t>(vaddr - reservation_begin);
1091 DCHECK_LT(offset, context->reservation->Size());
1092 DCHECK_LE(memsz, context->reservation->Size() - offset);
1093 context->max_size = std::max(context->max_size, offset + memsz);
1094 }
1095 }
1096
1097 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
1098 }
1099 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
1100 }
1101
1102 const MemMap* const reservation;
1103 size_t max_size = 0u;
1104 };
1105 dl_iterate_context context = { reservation };
1106
1107 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
1108 LOG(FATAL) << "Could not find the shared object mmapped to the reservation.";
1109 UNREACHABLE();
1110 }
1111
1112 // Take ownership of the memory used by the shared object. dlopen() does not assume
1113 // full ownership of this memory and dlclose() shall just remap it as zero pages with
1114 // PROT_NONE. We need to unmap the memory when destroying this oat file.
1115 dlopen_mmaps_.push_back(reservation->TakeReservedMemory(context.max_size));
1116 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001117#else
Steve Austin882ed6b2018-06-08 11:40:38 -07001118 static_assert(!kIsTargetBuild || kIsTargetLinux || kIsTargetFuchsia,
1119 "host_dlopen_handles_ will leak handles");
Vladimir Markoc09cd052018-08-23 16:36:36 +01001120 if (reservation != nullptr) {
1121 *error_msg = StringPrintf("dlopen() into reserved memory is unsupported on host for '%s'.",
1122 elf_filename.c_str());
1123 return false;
1124 }
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -07001125 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -07001126 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
1127 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -07001128 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
1129 dlclose(dlopen_handle_);
1130 dlopen_handle_ = nullptr;
1131 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
1132 return false;
1133 }
1134 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +01001135#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -08001136 }
1137 if (dlopen_handle_ == nullptr) {
1138 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
1139 return false;
1140 }
1141 return true;
1142#endif
1143}
1144
1145void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -08001146#ifdef __APPLE__
1147 UNUSED(elf_filename);
1148 LOG(FATAL) << "Should not reach here.";
1149 UNREACHABLE();
1150#else
Andreas Gampe049cff02015-12-01 23:27:12 -08001151 struct dl_iterate_context {
Vladimir Markoc09cd052018-08-23 16:36:36 +01001152 static int callback(dl_phdr_info* info, size_t size ATTRIBUTE_UNUSED, void* data) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001153 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Vladimir Markoc09cd052018-08-23 16:36:36 +01001154 static_assert(std::is_same<Elf32_Half, Elf64_Half>::value, "Half must match");
1155 using Elf_Half = Elf64_Half;
1156
Andreas Gampe4075f832016-05-18 13:09:54 -07001157 context->shared_objects_seen++;
1158 if (context->shared_objects_seen < context->shared_objects_before) {
1159 // We haven't been called yet for anything we haven't seen before. Just continue.
1160 // Note: this is aggressively optimistic. If another thread was unloading a library,
1161 // we may miss out here. However, this does not happen often in practice.
1162 return 0;
1163 }
1164
Andreas Gampe049cff02015-12-01 23:27:12 -08001165 // See whether this callback corresponds to the file which we have just loaded.
1166 bool contains_begin = false;
Vladimir Markoc09cd052018-08-23 16:36:36 +01001167 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001168 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1169 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1170 info->dlpi_phdr[i].p_vaddr);
1171 size_t memsz = info->dlpi_phdr[i].p_memsz;
1172 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
1173 contains_begin = true;
1174 break;
1175 }
1176 }
1177 }
1178 // Add dummy mmaps for this file.
1179 if (contains_begin) {
Vladimir Markoc09cd052018-08-23 16:36:36 +01001180 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001181 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1182 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1183 info->dlpi_phdr[i].p_vaddr);
1184 size_t memsz = info->dlpi_phdr[i].p_memsz;
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001185 MemMap mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
1186 context->dlopen_mmaps_->push_back(std::move(mmap));
Andreas Gampe049cff02015-12-01 23:27:12 -08001187 }
1188 }
1189 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
1190 }
1191 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
1192 }
1193 const uint8_t* const begin_;
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001194 std::vector<MemMap>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -07001195 const size_t shared_objects_before;
1196 size_t shared_objects_seen;
1197 };
1198 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -08001199
1200 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -07001201 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
1202 // before giving up. This should be unusual.
1203 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
1204 << shared_objects_before_;
1205 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
1206 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
1207 // OK, give up and print an error.
Andreas Gampe170331f2017-12-07 18:41:03 -08001208 PrintFileToLog("/proc/self/maps", android::base::LogSeverity::WARNING);
Andreas Gampe4075f832016-05-18 13:09:54 -07001209 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
1210 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001211 }
Andreas Gampe74f07b52015-12-02 11:53:26 -08001212#endif
Andreas Gampe049cff02015-12-01 23:27:12 -08001213}
1214
1215////////////////////////////////////////////////
1216// OatFile via our own ElfFile implementation //
1217////////////////////////////////////////////////
1218
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001219class ElfOatFile final : public OatFileBase {
Andreas Gampe049cff02015-12-01 23:27:12 -08001220 public:
1221 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
1222
Nicolas Geoffray30025092018-04-19 14:43:29 +01001223 static ElfOatFile* OpenElfFile(int zip_fd,
1224 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001225 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001226 bool writable,
1227 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001228 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001229 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001230 /*inout*/MemMap* reservation, // Where to load if not null.
1231 /*out*/std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001232
Nicolas Geoffray30025092018-04-19 14:43:29 +01001233 bool InitializeFromElfFile(int zip_fd,
1234 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001235 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001236 const char* abs_dex_location,
1237 std::string* error_msg);
1238
1239 protected:
1240 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001241 std::string* error_msg) const override {
Andreas Gampe049cff02015-12-01 23:27:12 -08001242 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
1243 if (ptr == nullptr) {
1244 *error_msg = "(Internal implementation could not find symbol)";
1245 }
1246 return ptr;
1247 }
1248
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001249 void PreLoad() override {
Andreas Gampe4075f832016-05-18 13:09:54 -07001250 }
1251
Andreas Gampe049cff02015-12-01 23:27:12 -08001252 bool Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -08001253 bool writable,
1254 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001255 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001256 /*inout*/MemMap* reservation, // Where to load if not null.
1257 /*out*/std::string* error_msg) override;
Andreas Gampe049cff02015-12-01 23:27:12 -08001258
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001259 bool Load(int oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001260 bool writable,
1261 bool executable,
1262 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001263 /*inout*/MemMap* reservation, // Where to load if not null.
1264 /*out*/std::string* error_msg) override;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001265
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001266 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) override {
Andreas Gampe049cff02015-12-01 23:27:12 -08001267 }
1268
1269 private:
1270 bool ElfFileOpen(File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001271 bool writable,
1272 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001273 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001274 /*inout*/MemMap* reservation, // Where to load if not null.
1275 /*out*/std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001276
1277 private:
1278 // Backing memory map for oat file during cross compilation.
1279 std::unique_ptr<ElfFile> elf_file_;
1280
1281 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
1282};
1283
Nicolas Geoffray30025092018-04-19 14:43:29 +01001284ElfOatFile* ElfOatFile::OpenElfFile(int zip_fd,
1285 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001286 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001287 bool writable,
1288 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001289 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001290 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001291 /*inout*/MemMap* reservation, // Where to load if not null.
1292 /*out*/std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001293 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001294 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001295 bool success = oat_file->ElfFileOpen(file,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001296 writable,
1297 low_4gb,
1298 executable,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001299 reservation,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001300 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001301 if (!success) {
1302 CHECK(!error_msg->empty());
1303 return nullptr;
1304 }
1305
1306 // Complete the setup.
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001307 if (!oat_file->ComputeFields(file->GetPath(), error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001308 return nullptr;
1309 }
1310
Nicolas Geoffray30025092018-04-19 14:43:29 +01001311 if (!oat_file->Setup(zip_fd, abs_dex_location, error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001312 return nullptr;
1313 }
1314
1315 return oat_file.release();
1316}
1317
Nicolas Geoffray30025092018-04-19 14:43:29 +01001318bool ElfOatFile::InitializeFromElfFile(int zip_fd,
1319 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001320 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001321 const char* abs_dex_location,
1322 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001323 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001324 if (IsExecutable()) {
1325 *error_msg = "Cannot initialize from elf file in executable mode.";
1326 return false;
1327 }
1328 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001329 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001330 uint64_t offset, size;
1331 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1332 CHECK(has_section);
1333 SetBegin(elf_file->Begin() + offset);
1334 SetEnd(elf_file->Begin() + size + offset);
1335 // Ignore the optional .bss section when opening non-executable.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001336 return Setup(zip_fd, abs_dex_location, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001337}
1338
1339bool ElfOatFile::Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -08001340 bool writable,
1341 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001342 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001343 /*inout*/MemMap* reservation,
1344 /*out*/std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001345 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001346 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1347 if (file == nullptr) {
1348 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1349 return false;
1350 }
1351 return ElfOatFile::ElfFileOpen(file.get(),
Andreas Gampe049cff02015-12-01 23:27:12 -08001352 writable,
1353 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001354 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001355 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -08001356 error_msg);
1357}
1358
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001359bool ElfOatFile::Load(int oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001360 bool writable,
1361 bool executable,
1362 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001363 /*inout*/MemMap* reservation,
1364 /*out*/std::string* error_msg) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001365 ScopedTrace trace(__PRETTY_FUNCTION__);
1366 if (oat_fd != -1) {
Josh Gaoafeec9f2018-08-30 14:05:56 -07001367 int duped_fd = DupCloexec(oat_fd);
1368 std::unique_ptr<File> file = std::make_unique<File>(duped_fd, false);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001369 if (file == nullptr) {
1370 *error_msg = StringPrintf("Failed to open oat filename for reading: %s",
1371 strerror(errno));
1372 return false;
1373 }
1374 return ElfOatFile::ElfFileOpen(file.get(),
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001375 writable,
1376 executable,
1377 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001378 reservation,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001379 error_msg);
1380 }
1381 return false;
1382}
1383
Andreas Gampe049cff02015-12-01 23:27:12 -08001384bool ElfOatFile::ElfFileOpen(File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001385 bool writable,
1386 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001387 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001388 /*inout*/MemMap* reservation,
1389 /*out*/std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001390 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001391 elf_file_.reset(ElfFile::Open(file,
1392 writable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001393 /*program_header_only=*/ true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001394 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001395 error_msg));
Andreas Gampe049cff02015-12-01 23:27:12 -08001396 if (elf_file_ == nullptr) {
1397 DCHECK(!error_msg->empty());
1398 return false;
1399 }
Vladimir Markoc09cd052018-08-23 16:36:36 +01001400 bool loaded = elf_file_->Load(file, executable, low_4gb, reservation, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001401 DCHECK(loaded || !error_msg->empty());
1402 return loaded;
1403}
1404
1405//////////////////////////
1406// General OatFile code //
1407//////////////////////////
1408
1409std::string OatFile::ResolveRelativeEncodedDexLocation(
1410 const char* abs_dex_location, const std::string& rel_dex_location) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001411 // For host, we still do resolution as the rel_dex_location might be absolute
1412 // for a target dex (for example /system/foo/foo.apk).
1413 if (abs_dex_location != nullptr && (rel_dex_location[0] != '/' || !kIsTargetBuild)) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001414 // Strip :classes<N>.dex used for secondary multidex files.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001415 std::string base = DexFileLoader::GetBaseLocation(rel_dex_location);
1416 std::string multidex_suffix = DexFileLoader::GetMultiDexSuffix(rel_dex_location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001417
1418 // Check if the base is a suffix of the provided abs_dex_location.
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001419 std::string target_suffix = ((rel_dex_location[0] != '/') ? "/" : "") + base;
Andreas Gampe049cff02015-12-01 23:27:12 -08001420 std::string abs_location(abs_dex_location);
1421 if (abs_location.size() > target_suffix.size()) {
1422 size_t pos = abs_location.size() - target_suffix.size();
1423 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
1424 return abs_location + multidex_suffix;
1425 }
1426 }
1427 }
1428 return rel_dex_location;
1429}
1430
1431static void CheckLocation(const std::string& location) {
1432 CHECK(!location.empty());
1433}
1434
Nicolas Geoffray30025092018-04-19 14:43:29 +01001435OatFile* OatFile::OpenWithElfFile(int zip_fd,
1436 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001437 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001438 const std::string& location,
1439 const char* abs_dex_location,
1440 std::string* error_msg) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001441 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, /*executable=*/ false));
Nicolas Geoffray30025092018-04-19 14:43:29 +01001442 return oat_file->InitializeFromElfFile(zip_fd, elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001443 ? oat_file.release()
1444 : nullptr;
1445}
1446
Nicolas Geoffray30025092018-04-19 14:43:29 +01001447OatFile* OatFile::Open(int zip_fd,
1448 const std::string& oat_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001449 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001450 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001451 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001452 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001453 /*inout*/MemMap* reservation,
1454 /*out*/std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001455 ScopedTrace trace("Open oat file " + oat_location);
1456 CHECK(!oat_filename.empty()) << oat_location;
1457 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001458
Calin Juravle367b9d82017-05-15 18:18:39 -07001459 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001460
1461 // Check that the files even exist, fast-fail.
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001462 if (!OS::FileExists(vdex_filename.c_str())) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001463 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1464 return nullptr;
1465 } else if (!OS::FileExists(oat_filename.c_str())) {
1466 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001467 return nullptr;
1468 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001469
1470 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1471 // disabled.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001472 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(zip_fd,
1473 vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001474 oat_filename,
1475 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001476 /*writable=*/ false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001477 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001478 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001479 abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001480 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -08001481 error_msg);
1482 if (with_dlopen != nullptr) {
1483 return with_dlopen;
1484 }
1485 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001486 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001487 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001488 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1489 //
1490 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1491 //
1492 // We use our own ELF loader for Quick to deal with legacy apps that
1493 // open a generated dex file by name, remove the file, then open
1494 // another generated dex file with the same name. http://b/10614658
1495 //
1496 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1497 //
1498 //
1499 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1500 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001501 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(zip_fd,
1502 vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001503 oat_filename,
1504 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001505 /*writable=*/ false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001506 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001507 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001508 abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001509 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -08001510 error_msg);
1511 return with_internal;
1512}
1513
Nicolas Geoffray30025092018-04-19 14:43:29 +01001514OatFile* OatFile::Open(int zip_fd,
1515 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001516 int oat_fd,
1517 const std::string& oat_location,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001518 bool executable,
1519 bool low_4gb,
1520 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001521 /*inout*/MemMap* reservation,
1522 /*out*/std::string* error_msg) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001523 CHECK(!oat_location.empty()) << oat_location;
1524
1525 std::string vdex_location = GetVdexFilename(oat_location);
1526
Nicolas Geoffray30025092018-04-19 14:43:29 +01001527 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(zip_fd,
1528 vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001529 oat_fd,
1530 vdex_location,
1531 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001532 /*writable=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001533 executable,
1534 low_4gb,
1535 abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001536 reservation,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001537 error_msg);
1538 return with_internal;
1539}
1540
Nicolas Geoffray30025092018-04-19 14:43:29 +01001541OatFile* OatFile::OpenWritable(int zip_fd,
1542 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001543 const std::string& location,
1544 const char* abs_dex_location,
1545 std::string* error_msg) {
1546 CheckLocation(location);
Nicolas Geoffray30025092018-04-19 14:43:29 +01001547 return ElfOatFile::OpenElfFile(zip_fd,
1548 file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001549 location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001550 /*writable=*/ true,
1551 /*executable=*/ false,
1552 /*low_4gb=*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001553 abs_dex_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001554 /*reservation=*/ nullptr,
Andreas Gampe049cff02015-12-01 23:27:12 -08001555 error_msg);
1556}
1557
Nicolas Geoffray30025092018-04-19 14:43:29 +01001558OatFile* OatFile::OpenReadable(int zip_fd,
1559 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001560 const std::string& location,
1561 const char* abs_dex_location,
1562 std::string* error_msg) {
1563 CheckLocation(location);
Nicolas Geoffray30025092018-04-19 14:43:29 +01001564 return ElfOatFile::OpenElfFile(zip_fd,
1565 file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001566 location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001567 /*writable=*/ false,
1568 /*executable=*/ false,
1569 /*low_4gb=*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001570 abs_dex_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001571 /*reservation=*/ nullptr,
Andreas Gampe049cff02015-12-01 23:27:12 -08001572 error_msg);
1573}
1574
1575OatFile::OatFile(const std::string& location, bool is_executable)
1576 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001577 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001578 begin_(nullptr),
1579 end_(nullptr),
Vladimir Markob066d432018-01-03 13:14:37 +00001580 data_bimg_rel_ro_begin_(nullptr),
1581 data_bimg_rel_ro_end_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001582 bss_begin_(nullptr),
1583 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001584 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001585 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001586 is_executable_(is_executable),
David Srbeckyec2cdf42017-12-08 16:21:25 +00001587 vdex_begin_(nullptr),
1588 vdex_end_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001589 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1590 CHECK(!location_.empty());
1591}
1592
1593OatFile::~OatFile() {
1594 STLDeleteElements(&oat_dex_files_storage_);
1595}
1596
Brian Carlstrome24fa612011-09-29 00:53:55 -07001597const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001598 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001599}
1600
Ian Rogers13735952014-10-08 12:43:28 -07001601const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001602 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001603 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001604}
1605
Ian Rogers13735952014-10-08 12:43:28 -07001606const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001607 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001608 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001609}
1610
David Brazdil7b49e6c2016-09-01 11:06:18 +01001611const uint8_t* OatFile::DexBegin() const {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001612 return vdex_->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001613}
1614
1615const uint8_t* OatFile::DexEnd() const {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001616 return vdex_->End();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001617}
1618
Vladimir Markob066d432018-01-03 13:14:37 +00001619ArrayRef<const uint32_t> OatFile::GetBootImageRelocations() const {
1620 if (data_bimg_rel_ro_begin_ != nullptr) {
1621 const uint32_t* relocations = reinterpret_cast<const uint32_t*>(data_bimg_rel_ro_begin_);
1622 const uint32_t* relocations_end = reinterpret_cast<const uint32_t*>(data_bimg_rel_ro_end_);
1623 return ArrayRef<const uint32_t>(relocations, relocations_end - relocations);
1624 } else {
1625 return ArrayRef<const uint32_t>();
1626 }
1627}
1628
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001629ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1630 if (bss_methods_ != nullptr) {
1631 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1632 ArtMethod** methods_end =
1633 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1634 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1635 } else {
1636 return ArrayRef<ArtMethod*>();
1637 }
1638}
1639
Vladimir Markoaad75c62016-10-03 08:46:48 +00001640ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1641 if (bss_roots_ != nullptr) {
1642 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1643 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1644 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1645 } else {
1646 return ArrayRef<GcRoot<mirror::Object>>();
1647 }
1648}
1649
Andreas Gampeb40d3612018-06-26 15:49:42 -07001650const OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1651 const uint32_t* dex_location_checksum,
1652 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001653 // NOTE: We assume here that the canonical location for a given dex_location never
1654 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1655 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1656 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001657
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001658 // TODO: Additional analysis of usage patterns to see if this can be simplified
1659 // without any performance loss, for example by not doing the first lock-free lookup.
1660
Andreas Gampeb40d3612018-06-26 15:49:42 -07001661 const OatDexFile* oat_dex_file = nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001662 StringPiece key(dex_location);
1663 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1664 // directly mentioned in the oat file and doesn't require locking.
1665 auto primary_it = oat_dex_files_.find(key);
1666 if (primary_it != oat_dex_files_.end()) {
1667 oat_dex_file = primary_it->second;
1668 DCHECK(oat_dex_file != nullptr);
1669 } else {
1670 // This dex_location is not one of the dex locations directly mentioned in the
1671 // oat file. The correct lookup is via the canonical location but first see in
1672 // the secondary_oat_dex_files_ whether we've looked up this location before.
1673 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1674 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1675 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001676 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001677 } else {
1678 // We haven't seen this dex_location before, we must check the canonical location.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001679 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001680 if (dex_canonical_location != dex_location) {
1681 StringPiece canonical_key(dex_canonical_location);
1682 auto canonical_it = oat_dex_files_.find(canonical_key);
1683 if (canonical_it != oat_dex_files_.end()) {
1684 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001685 } // else keep null.
1686 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001687
1688 // Copy the key to the string_cache_ and store the result in secondary map.
1689 string_cache_.emplace_back(key.data(), key.length());
1690 StringPiece key_copy(string_cache_.back());
1691 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001692 }
1693 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001694
1695 if (oat_dex_file == nullptr) {
1696 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001697 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001698 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1699 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1700 }
1701 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001702 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001703
Richard Uhler9a37efc2016-08-05 16:32:55 -07001704 if (dex_location_checksum != nullptr &&
1705 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1706 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001707 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001708 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1709 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1710 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1711 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1712 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001713 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001714 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001715 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001716 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001717}
1718
Andreas Gampeb40d3612018-06-26 15:49:42 -07001719OatDexFile::OatDexFile(const OatFile* oat_file,
1720 const std::string& dex_file_location,
1721 const std::string& canonical_dex_file_location,
1722 uint32_t dex_file_location_checksum,
1723 const uint8_t* dex_file_pointer,
1724 const uint8_t* lookup_table_data,
1725 const IndexBssMapping* method_bss_mapping_data,
1726 const IndexBssMapping* type_bss_mapping_data,
1727 const IndexBssMapping* string_bss_mapping_data,
1728 const uint32_t* oat_class_offsets_pointer,
1729 const DexLayoutSections* dex_layout_sections)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001730 : oat_file_(oat_file),
1731 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001732 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001733 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001734 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001735 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001736 method_bss_mapping_(method_bss_mapping_data),
Vladimir Markof3c52b42017-11-17 17:32:12 +00001737 type_bss_mapping_(type_bss_mapping_data),
1738 string_bss_mapping_(string_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001739 oat_class_offsets_pointer_(oat_class_offsets_pointer),
Vladimir Markoea341d22018-05-11 10:33:37 +01001740 lookup_table_(),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001741 dex_layout_sections_(dex_layout_sections) {
David Sehr9aa352e2016-09-15 18:13:52 -07001742 // Initialize TypeLookupTable.
1743 if (lookup_table_data_ != nullptr) {
1744 // Peek the number of classes from the DexFile.
1745 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1746 const uint32_t num_class_defs = dex_header->class_defs_size_;
1747 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1748 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1749 } else {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001750 const uint8_t* dex_data = dex_file_pointer_;
1751 // TODO: Clean this up to create the type lookup table after the dex file has been created?
1752 if (CompactDexFile::IsMagicValid(dex_header->magic_)) {
1753 dex_data += dex_header->data_off_;
1754 }
1755 lookup_table_ = TypeLookupTable::Open(dex_data, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001756 }
1757 }
1758}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001759
Andreas Gampe875b4f22018-11-19 12:59:15 -08001760OatDexFile::OatDexFile(TypeLookupTable&& lookup_table) : lookup_table_(std::move(lookup_table)) {
1761 // Stripped-down OatDexFile only allowed in the compiler.
1762 CHECK(Runtime::Current() == nullptr || Runtime::Current()->IsAotCompiler());
1763}
Mathieu Chartier1b868492016-11-16 16:22:37 -08001764
Andreas Gampeb40d3612018-06-26 15:49:42 -07001765OatDexFile::~OatDexFile() {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001766
Andreas Gampeb40d3612018-06-26 15:49:42 -07001767size_t OatDexFile::FileSize() const {
Andreas Gampe875b4f22018-11-19 12:59:15 -08001768 DCHECK(dex_file_pointer_ != nullptr);
Ian Rogers05f28c62012-10-23 18:12:13 -07001769 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1770}
1771
Andreas Gampeb40d3612018-06-26 15:49:42 -07001772std::unique_ptr<const DexFile> OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001773 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001774 static constexpr bool kVerify = false;
1775 static constexpr bool kVerifyChecksum = false;
David Sehr013fd802018-01-11 22:55:24 -08001776 const ArtDexFileLoader dex_file_loader;
1777 return dex_file_loader.Open(dex_file_pointer_,
1778 FileSize(),
1779 dex_file_location_,
1780 dex_file_location_checksum_,
1781 this,
1782 kVerify,
1783 kVerifyChecksum,
1784 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001785}
1786
Andreas Gampeb40d3612018-06-26 15:49:42 -07001787uint32_t OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
Andreas Gampe875b4f22018-11-19 12:59:15 -08001788 DCHECK(oat_class_offsets_pointer_ != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001789 return oat_class_offsets_pointer_[class_def_index];
1790}
1791
Andreas Gampeb40d3612018-06-26 15:49:42 -07001792OatFile::OatClass OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001793 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001794
Ian Rogers13735952014-10-08 12:43:28 -07001795 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001796 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001797
Ian Rogers13735952014-10-08 12:43:28 -07001798 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001799 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
Vladimir Marko2c64a832018-01-04 11:31:56 +00001800 ClassStatus status = enum_cast<ClassStatus>(*reinterpret_cast<const int16_t*>(status_pointer));
1801 CHECK_LE(status, ClassStatus::kLast);
Brian Carlstromba150c32013-08-27 17:31:03 -07001802
Ian Rogers13735952014-10-08 12:43:28 -07001803 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001804 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1805 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1806 CHECK_LT(type, kOatClassMax);
1807
Ian Rogers13735952014-10-08 12:43:28 -07001808 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001809 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001810
Brian Carlstromcd937a92014-03-04 22:53:23 -08001811 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001812 const uint8_t* bitmap_pointer = nullptr;
1813 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001814 if (type != kOatClassNoneCompiled) {
1815 if (type == kOatClassSomeCompiled) {
1816 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1817 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1818 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1819 methods_pointer = bitmap_pointer + bitmap_size;
1820 } else {
1821 methods_pointer = after_type_pointer;
1822 }
1823 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001824 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001825
Richard Uhler07b3c232015-03-31 15:57:54 -07001826 return OatFile::OatClass(oat_file_,
1827 status,
1828 type,
1829 bitmap_size,
1830 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1831 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001832}
1833
Andreas Gampeb40d3612018-06-26 15:49:42 -07001834const DexFile::ClassDef* OatDexFile::FindClassDef(const DexFile& dex_file,
1835 const char* descriptor,
1836 size_t hash) {
1837 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
David Sehr9aa352e2016-09-15 18:13:52 -07001838 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001839 bool used_lookup_table = false;
1840 const DexFile::ClassDef* lookup_table_classdef = nullptr;
Vladimir Markoea341d22018-05-11 10:33:37 +01001841 if (LIKELY((oat_dex_file != nullptr) && oat_dex_file->GetTypeLookupTable().Valid())) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001842 used_lookup_table = true;
Vladimir Markoea341d22018-05-11 10:33:37 +01001843 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable().Lookup(descriptor, hash);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001844 lookup_table_classdef = (class_def_idx != dex::kDexNoIndex)
1845 ? &dex_file.GetClassDef(class_def_idx)
1846 : nullptr;
1847 if (!kIsDebugBuild) {
1848 return lookup_table_classdef;
1849 }
David Sehr9aa352e2016-09-15 18:13:52 -07001850 }
1851 // Fast path for rare no class defs case.
1852 const uint32_t num_class_defs = dex_file.NumClassDefs();
1853 if (num_class_defs == 0) {
Vladimir Markoea341d22018-05-11 10:33:37 +01001854 DCHECK(!used_lookup_table);
David Sehr9aa352e2016-09-15 18:13:52 -07001855 return nullptr;
1856 }
1857 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1858 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001859 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001860 const DexFile::ClassDef* found_class_def = dex_file.FindClassDef(type_idx);
1861 if (kIsDebugBuild && used_lookup_table) {
1862 DCHECK_EQ(found_class_def, lookup_table_classdef);
1863 }
1864 return found_class_def;
David Sehr9aa352e2016-09-15 18:13:52 -07001865 }
1866 return nullptr;
1867}
1868
Mathieu Chartier120aa282017-08-05 16:03:03 -07001869// Madvise the dex file based on the state we are moving to.
1870void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001871 Runtime* const runtime = Runtime::Current();
1872 const bool low_ram = runtime->GetHeap()->IsLowMemoryMode();
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001873 // TODO: Also do madvise hints for non low ram devices.
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001874 if (!low_ram) {
Mathieu Chartierbe8303d2017-08-17 17:39:39 -07001875 return;
1876 }
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001877 if (state == MadviseState::kMadviseStateAtLoad && runtime->MAdviseRandomAccess()) {
1878 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices.
1879 // Other devices have enough page cache to get performance benefits from loading more pages
1880 // into the page cache.
David Sehrc431b9d2018-03-02 12:01:51 -08001881 DexLayoutSection::MadviseLargestPageAlignedRegion(dex_file.Begin(),
1882 dex_file.Begin() + dex_file.Size(),
1883 MADV_RANDOM);
Mathieu Chartier120aa282017-08-05 16:03:03 -07001884 }
Andreas Gampeb40d3612018-06-26 15:49:42 -07001885 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier120aa282017-08-05 16:03:03 -07001886 if (oat_dex_file != nullptr) {
1887 // Should always be there.
1888 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
1889 CHECK(sections != nullptr);
1890 sections->Madvise(&dex_file, state);
1891 }
1892}
1893
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001894OatFile::OatClass::OatClass(const OatFile* oat_file,
Vladimir Marko2c64a832018-01-04 11:31:56 +00001895 ClassStatus status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001896 OatClassType type,
1897 uint32_t bitmap_size,
1898 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001899 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001900 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001901 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001902 switch (type_) {
1903 case kOatClassAllCompiled: {
1904 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001905 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001906 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001907 break;
1908 }
1909 case kOatClassSomeCompiled: {
1910 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001911 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001912 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001913 break;
1914 }
1915 case kOatClassNoneCompiled: {
1916 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001917 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001918 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001919 break;
1920 }
1921 case kOatClassMax: {
1922 LOG(FATAL) << "Invalid OatClassType " << type_;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001923 UNREACHABLE();
Brian Carlstromba150c32013-08-27 17:31:03 -07001924 }
1925 }
1926}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001927
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001928uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1929 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1930 if (oat_method_offsets == nullptr) {
1931 return 0u;
1932 }
1933 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1934}
1935
1936const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001937 // 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 -07001938 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001939 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001940 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001941 }
1942 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001943 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001944 CHECK_EQ(kOatClassAllCompiled, type_);
1945 methods_pointer_index = method_index;
1946 } else {
1947 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001948 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001949 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001950 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001951 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1952 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001953 }
1954 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001955 return &oat_method_offsets;
1956}
1957
1958const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1959 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1960 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001961 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001962 }
1963 if (oat_file_->IsExecutable() ||
1964 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001965 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001966 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001967 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001968 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1969 // version.
1970 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001971}
1972
Mathieu Chartiere401d142015-04-22 13:56:20 -07001973void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001974 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001975 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001976}
1977
Sebastien Hertz0de11332015-05-13 12:14:05 +02001978bool OatFile::IsDebuggable() const {
1979 return GetOatHeader().IsDebuggable();
1980}
1981
Andreas Gampe29d38e72016-03-23 15:31:51 +00001982CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1983 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001984}
1985
Calin Juravle44e5efa2017-09-12 00:54:26 -07001986std::string OatFile::GetClassLoaderContext() const {
1987 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001988}
Calin Juravle44e5efa2017-09-12 00:54:26 -07001989
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001990const char* OatFile::GetCompilationReason() const {
1991 return GetOatHeader().GetStoreValueByKey(OatHeader::kCompilationReasonKey);
1992}
1993
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001994OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1995 uint16_t class_def_idx,
1996 bool* found) {
1997 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
Andreas Gampeb40d3612018-06-26 15:49:42 -07001998 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001999 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01002000 *found = false;
2001 return OatFile::OatClass::Invalid();
2002 }
2003 *found = true;
2004 return oat_dex_file->GetOatClass(class_def_idx);
2005}
2006
Andreas Gampeb40d3612018-06-26 15:49:42 -07002007void OatDexFile::AssertAotCompiler() {
Mathieu Chartier1b868492016-11-16 16:22:37 -08002008 CHECK(Runtime::Current()->IsAotCompiler());
2009}
2010
Brian Carlstrome24fa612011-09-29 00:53:55 -07002011} // namespace art