Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 16 | |
| 17 | #include "oat_file.h" |
| 18 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 19 | #include <dlfcn.h> |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 20 | #ifndef __APPLE__ |
| 21 | #include <link.h> // for dl_iterate_phdr. |
| 22 | #endif |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <cstdlib> |
| 26 | #include <cstring> |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 27 | #include <sstream> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include <type_traits> |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 29 | #include <sys/stat.h> |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 30 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 31 | // dlopen_ext support from bionic. |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 32 | #ifdef ART_TARGET_ANDROID |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 33 | #include "android/dlext.h" |
| 34 | #endif |
| 35 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 36 | #include "android-base/stringprintf.h" |
| 37 | |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 38 | #include "art_method.h" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 39 | #include "base/bit_vector.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 40 | #include "base/enums.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 41 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 42 | #include "base/systrace.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 43 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 44 | #include "dex_file_types.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 45 | #include "elf_file.h" |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 46 | #include "elf_utils.h" |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 47 | #include "gc_root.h" |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 48 | #include "gc/space/image_space.h" |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 49 | #include "mem_map.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 50 | #include "mirror/class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 51 | #include "mirror/object-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 52 | #include "oat.h" |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 53 | #include "oat_file-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 54 | #include "oat_file_manager.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 55 | #include "os.h" |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 56 | #include "runtime.h" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 57 | #include "type_lookup_table.h" |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 58 | #include "utf-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | #include "utils.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 60 | #include "vdex_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 61 | |
| 62 | namespace art { |
| 63 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 64 | using android::base::StringPrintf; |
| 65 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 66 | // Whether OatFile::Open will try dlopen. Fallback is our own ELF loader. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 67 | static constexpr bool kUseDlopen = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 68 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 69 | // Whether OatFile::Open will try dlopen on the host. On the host we're not linking against |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 70 | // bionic, so cannot take advantage of the support for changed semantics (loading the same soname |
| 71 | // multiple times). However, if/when we switch the above, we likely want to switch this, too, |
| 72 | // to get test coverage of the code paths. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 73 | static constexpr bool kUseDlopenOnHost = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 74 | |
| 75 | // For debugging, Open will print DlOpen error message if set to true. |
| 76 | static constexpr bool kPrintDlOpenErrorMessage = false; |
| 77 | |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 78 | // If true, we advise the kernel about dex file mem map accesses. |
Mathieu Chartier | 150d25d | 2017-08-28 09:52:55 -0700 | [diff] [blame] | 79 | static constexpr bool kMadviseDexFileAccesses = true; |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 80 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 81 | // Note for OatFileBase and descendents: |
| 82 | // |
| 83 | // These are used in OatFile::Open to try all our loaders. |
| 84 | // |
| 85 | // The process is simple: |
| 86 | // |
| 87 | // 1) Allocate an instance through the standard constructor (location, executable) |
| 88 | // 2) Load() to try to open the file. |
| 89 | // 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress. |
| 90 | // 4) PreSetup() for any steps that should be done before the final setup. |
| 91 | // 5) Setup() to complete the procedure. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 92 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 93 | class OatFileBase : public OatFile { |
| 94 | public: |
| 95 | virtual ~OatFileBase() {} |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 96 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 97 | template <typename kOatFileBaseSubType> |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 98 | static OatFileBase* OpenOatFile(const std::string& vdex_filename, |
| 99 | const std::string& elf_filename, |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 100 | const std::string& location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 101 | uint8_t* requested_base, |
| 102 | uint8_t* oat_file_begin, |
| 103 | bool writable, |
| 104 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 105 | bool low_4gb, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 106 | const char* abs_dex_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 107 | std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 108 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 109 | template <typename kOatFileBaseSubType> |
| 110 | static OatFileBase* OpenOatFile(int vdex_fd, |
| 111 | int oat_fd, |
| 112 | const std::string& vdex_filename, |
| 113 | const std::string& oat_filename, |
| 114 | uint8_t* requested_base, |
| 115 | uint8_t* oat_file_begin, |
| 116 | bool writable, |
| 117 | bool executable, |
| 118 | bool low_4gb, |
| 119 | const char* abs_dex_location, |
| 120 | std::string* error_msg); |
| 121 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 122 | protected: |
| 123 | OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {} |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 124 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 125 | virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, |
| 126 | std::string* error_msg) const = 0; |
| 127 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 128 | virtual void PreLoad() = 0; |
| 129 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 130 | bool LoadVdex(const std::string& vdex_filename, |
| 131 | bool writable, |
| 132 | bool low_4gb, |
| 133 | std::string* error_msg); |
| 134 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 135 | bool LoadVdex(int vdex_fd, |
| 136 | const std::string& vdex_filename, |
| 137 | bool writable, |
| 138 | bool low_4gb, |
| 139 | std::string* error_msg); |
| 140 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 141 | virtual bool Load(const std::string& elf_filename, |
| 142 | uint8_t* oat_file_begin, |
| 143 | bool writable, |
| 144 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 145 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 146 | std::string* error_msg) = 0; |
| 147 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 148 | virtual bool Load(int oat_fd, |
| 149 | uint8_t* oat_file_begin, |
| 150 | bool writable, |
| 151 | bool executable, |
| 152 | bool low_4gb, |
| 153 | std::string* error_msg) = 0; |
| 154 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 155 | bool ComputeFields(uint8_t* requested_base, |
| 156 | const std::string& file_path, |
| 157 | std::string* error_msg); |
| 158 | |
| 159 | virtual void PreSetup(const std::string& elf_filename) = 0; |
| 160 | |
| 161 | bool Setup(const char* abs_dex_location, std::string* error_msg); |
| 162 | |
| 163 | // Setters exposed for ElfOatFile. |
| 164 | |
| 165 | void SetBegin(const uint8_t* begin) { |
| 166 | begin_ = begin; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 169 | void SetEnd(const uint8_t* end) { |
| 170 | end_ = end; |
| 171 | } |
| 172 | |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 173 | void SetVdex(VdexFile* vdex) { |
| 174 | vdex_.reset(vdex); |
| 175 | } |
| 176 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 177 | private: |
| 178 | DISALLOW_COPY_AND_ASSIGN(OatFileBase); |
| 179 | }; |
| 180 | |
| 181 | template <typename kOatFileBaseSubType> |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 182 | OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename, |
| 183 | const std::string& elf_filename, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 184 | const std::string& location, |
| 185 | uint8_t* requested_base, |
| 186 | uint8_t* oat_file_begin, |
| 187 | bool writable, |
| 188 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 189 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 190 | const char* abs_dex_location, |
| 191 | std::string* error_msg) { |
| 192 | std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable)); |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 193 | |
| 194 | ret->PreLoad(); |
| 195 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 196 | if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) { |
| 197 | return nullptr; |
| 198 | } |
| 199 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 200 | if (!ret->Load(elf_filename, |
| 201 | oat_file_begin, |
| 202 | writable, |
| 203 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 204 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 205 | error_msg)) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 206 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 207 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 208 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 209 | if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) { |
| 210 | return nullptr; |
| 211 | } |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 212 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 213 | ret->PreSetup(elf_filename); |
| 214 | |
| 215 | if (!ret->Setup(abs_dex_location, error_msg)) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 219 | return ret.release(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 222 | template <typename kOatFileBaseSubType> |
| 223 | OatFileBase* OatFileBase::OpenOatFile(int vdex_fd, |
| 224 | int oat_fd, |
| 225 | const std::string& vdex_location, |
| 226 | const std::string& oat_location, |
| 227 | uint8_t* requested_base, |
| 228 | uint8_t* oat_file_begin, |
| 229 | bool writable, |
| 230 | bool executable, |
| 231 | bool low_4gb, |
| 232 | const char* abs_dex_location, |
| 233 | std::string* error_msg) { |
| 234 | std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(oat_location, executable)); |
| 235 | |
| 236 | if (kIsVdexEnabled && !ret->LoadVdex(vdex_fd, vdex_location, writable, low_4gb, error_msg)) { |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
| 240 | if (!ret->Load(oat_fd, |
| 241 | oat_file_begin, |
| 242 | writable, |
| 243 | executable, |
| 244 | low_4gb, |
| 245 | error_msg)) { |
| 246 | return nullptr; |
| 247 | } |
| 248 | |
| 249 | if (!ret->ComputeFields(requested_base, oat_location, error_msg)) { |
| 250 | return nullptr; |
| 251 | } |
| 252 | |
| 253 | ret->PreSetup(oat_location); |
| 254 | |
| 255 | if (!ret->Setup(abs_dex_location, error_msg)) { |
| 256 | return nullptr; |
| 257 | } |
| 258 | |
| 259 | return ret.release(); |
| 260 | } |
| 261 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 262 | bool OatFileBase::LoadVdex(const std::string& vdex_filename, |
| 263 | bool writable, |
| 264 | bool low_4gb, |
| 265 | std::string* error_msg) { |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 266 | vdex_ = VdexFile::Open(vdex_filename, writable, low_4gb, /* unquicken*/ false, error_msg); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 267 | if (vdex_.get() == nullptr) { |
| 268 | *error_msg = StringPrintf("Failed to load vdex file '%s' %s", |
| 269 | vdex_filename.c_str(), |
| 270 | error_msg->c_str()); |
| 271 | return false; |
| 272 | } |
| 273 | return true; |
| 274 | } |
| 275 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 276 | bool OatFileBase::LoadVdex(int vdex_fd, |
| 277 | const std::string& vdex_filename, |
| 278 | bool writable, |
| 279 | bool low_4gb, |
| 280 | std::string* error_msg) { |
| 281 | if (vdex_fd != -1) { |
| 282 | struct stat s; |
| 283 | int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd, &s)); |
| 284 | if (rc == -1) { |
| 285 | PLOG(WARNING) << "Failed getting length of vdex file"; |
| 286 | } else { |
| 287 | vdex_ = VdexFile::Open(vdex_fd, |
| 288 | s.st_size, |
| 289 | vdex_filename, |
| 290 | writable, |
| 291 | low_4gb, |
| 292 | false /* unquicken */, |
| 293 | error_msg); |
| 294 | if (vdex_.get() == nullptr) { |
| 295 | *error_msg = "Failed opening vdex file."; |
| 296 | return false; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | return true; |
| 301 | } |
| 302 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 303 | bool OatFileBase::ComputeFields(uint8_t* requested_base, |
| 304 | const std::string& file_path, |
| 305 | std::string* error_msg) { |
| 306 | std::string symbol_error_msg; |
| 307 | begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 308 | if (begin_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 309 | *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s", |
| 310 | file_path.c_str(), |
| 311 | symbol_error_msg.c_str()); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 312 | return false; |
| 313 | } |
| 314 | if (requested_base != nullptr && begin_ != requested_base) { |
Nicolas Geoffray | f97cf2a | 2016-03-09 15:36:23 +0000 | [diff] [blame] | 315 | // Host can fail this check. Do not dump there to avoid polluting the output. |
Andreas Gampe | 7ec0904 | 2016-04-01 17:20:49 -0700 | [diff] [blame] | 316 | if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) { |
Nicolas Geoffray | f97cf2a | 2016-03-09 15:36:23 +0000 | [diff] [blame] | 317 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 318 | } |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 319 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 320 | "oatdata=%p != expected=%p. See process maps in the log.", |
| 321 | begin_, requested_base); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 322 | return false; |
| 323 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 324 | end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 325 | if (end_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 326 | *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s", |
| 327 | file_path.c_str(), |
| 328 | symbol_error_msg.c_str()); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 329 | return false; |
| 330 | } |
| 331 | // Readjust to be non-inclusive upper bound. |
| 332 | end_ += sizeof(uint32_t); |
| 333 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 334 | bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 335 | if (bss_begin_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 336 | // No .bss section. |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 337 | bss_end_ = nullptr; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 338 | } else { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 339 | bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 340 | if (bss_end_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 341 | *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str()); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 342 | return false; |
| 343 | } |
| 344 | // Readjust to be non-inclusive upper bound. |
| 345 | bss_end_ += sizeof(uint32_t); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 346 | // Find bss methods if present. |
| 347 | bss_methods_ = |
| 348 | const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg)); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 349 | // Find bss roots if present. |
| 350 | bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 353 | return true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 354 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 355 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 356 | // Read an unaligned entry from the OatDexFile data in OatFile and advance the read |
| 357 | // position by the number of bytes read, i.e. sizeof(T). |
| 358 | // Return true on success, false if the read would go beyond the end of the OatFile. |
| 359 | template <typename T> |
Vladimir Marko | 722fa98 | 2015-10-19 18:18:27 +0100 | [diff] [blame] | 360 | inline static bool ReadOatDexFileData(const OatFile& oat_file, |
| 361 | /*inout*/const uint8_t** oat, |
| 362 | /*out*/T* value) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 363 | DCHECK(oat != nullptr); |
| 364 | DCHECK(value != nullptr); |
| 365 | DCHECK_LE(*oat, oat_file.End()); |
| 366 | if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) { |
| 367 | return false; |
| 368 | } |
| 369 | static_assert(std::is_trivial<T>::value, "T must be a trivial type"); |
| 370 | typedef __attribute__((__aligned__(1))) T unaligned_type; |
| 371 | *value = *reinterpret_cast<const unaligned_type*>(*oat); |
| 372 | *oat += sizeof(T); |
| 373 | return true; |
| 374 | } |
| 375 | |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 376 | static inline bool MapConstantTables(const gc::space::ImageSpace* space, |
| 377 | uint8_t* address) { |
| 378 | // If MREMAP_DUP is ever merged to Linux kernel, use it to avoid the unnecessary open()/close(). |
| 379 | // Note: The current approach relies on the filename still referencing the same inode. |
| 380 | |
| 381 | File file(space->GetImageFilename(), O_RDONLY, /* checkUsage */ false); |
| 382 | if (!file.IsOpened()) { |
| 383 | LOG(ERROR) << "Failed to open boot image file " << space->GetImageFilename(); |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | uint32_t offset = space->GetImageHeader().GetBootImageConstantTablesOffset(); |
| 388 | uint32_t size = space->GetImageHeader().GetBootImageConstantTablesSize(); |
| 389 | std::string error_msg; |
| 390 | std::unique_ptr<MemMap> mem_map(MemMap::MapFileAtAddress(address, |
| 391 | size, |
| 392 | PROT_READ, |
| 393 | MAP_PRIVATE, |
| 394 | file.Fd(), |
| 395 | offset, |
| 396 | /* low_4gb */ false, |
| 397 | /* reuse */ true, |
| 398 | file.GetPath().c_str(), |
| 399 | &error_msg)); |
| 400 | if (mem_map == nullptr) { |
| 401 | LOG(ERROR) << "Failed to mmap boot image tables from file " << space->GetImageFilename(); |
| 402 | return false; |
| 403 | } |
| 404 | return true; |
| 405 | } |
| 406 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 407 | bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) { |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 408 | if (!GetOatHeader().IsValid()) { |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 409 | std::string cause = GetOatHeader().GetValidationErrorMessage(); |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 410 | *error_msg = StringPrintf("Invalid oat header for '%s': %s", |
| 411 | GetLocation().c_str(), |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 412 | cause.c_str()); |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 413 | return false; |
| 414 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 415 | PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet()); |
| 416 | size_t key_value_store_size = |
| 417 | (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u; |
| 418 | if (Size() < sizeof(OatHeader) + key_value_store_size) { |
| 419 | *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, " |
| 420 | "size = %zu < %zu + %zu", |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 421 | GetLocation().c_str(), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 422 | Size(), |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 423 | sizeof(OatHeader), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 424 | key_value_store_size); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 428 | size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset(); |
| 429 | if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) { |
| 430 | *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: " |
| 431 | "%zu is not in [%zu, %zu]", |
| 432 | GetLocation().c_str(), |
| 433 | oat_dex_files_offset, |
| 434 | GetOatHeader().GetHeaderSize(), |
| 435 | Size()); |
| 436 | return false; |
| 437 | } |
| 438 | const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records. |
| 439 | |
| 440 | DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>)); |
| 441 | if (!IsAligned<kPageSize>(bss_begin_) || |
| 442 | !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) || |
| 443 | !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) || |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 444 | !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) { |
| 445 | *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): " |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 446 | "begin = %p, methods_ = %p, roots = %p, end = %p", |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 447 | GetLocation().c_str(), |
| 448 | bss_begin_, |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 449 | bss_methods_, |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 450 | bss_roots_, |
| 451 | bss_end_); |
| 452 | return false; |
| 453 | } |
| 454 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 455 | if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) || |
| 456 | (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) || |
| 457 | (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) { |
| 458 | *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: " |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 459 | "begin = %p, methods = %p, roots = %p, end = %p", |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 460 | GetLocation().c_str(), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 461 | bss_begin_, |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 462 | bss_methods_, |
| 463 | bss_roots_, |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 464 | bss_end_); |
| 465 | return false; |
| 466 | } |
| 467 | |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 468 | uint8_t* after_tables = |
| 469 | (bss_methods_ != nullptr) ? bss_methods_ : bss_roots_; // May be null. |
| 470 | uint8_t* boot_image_tables = (bss_begin_ == after_tables) ? nullptr : bss_begin_; |
| 471 | uint8_t* boot_image_tables_end = |
| 472 | (bss_begin_ == after_tables) ? nullptr : (after_tables != nullptr) ? after_tables : bss_end_; |
| 473 | DCHECK_EQ(boot_image_tables != nullptr, boot_image_tables_end != nullptr); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 474 | uint32_t dex_file_count = GetOatHeader().GetDexFileCount(); |
| 475 | oat_dex_files_storage_.reserve(dex_file_count); |
| 476 | for (size_t i = 0; i < dex_file_count; i++) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 477 | uint32_t dex_file_location_size; |
| 478 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) { |
| 479 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file " |
| 480 | "location size", |
| 481 | GetLocation().c_str(), |
| 482 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 483 | return false; |
| 484 | } |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 485 | if (UNLIKELY(dex_file_location_size == 0U)) { |
| 486 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name", |
| 487 | GetLocation().c_str(), |
| 488 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 489 | return false; |
| 490 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 491 | if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 492 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file " |
| 493 | "location", |
| 494 | GetLocation().c_str(), |
| 495 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 496 | return false; |
| 497 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 498 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 499 | oat += dex_file_location_size; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 500 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 501 | std::string dex_file_location = ResolveRelativeEncodedDexLocation( |
| 502 | abs_dex_location, |
| 503 | std::string(dex_file_location_data, dex_file_location_size)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 504 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 505 | uint32_t dex_file_checksum; |
| 506 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) { |
| 507 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after " |
| 508 | "dex file checksum", |
| 509 | GetLocation().c_str(), |
| 510 | i, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 511 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 512 | return false; |
| 513 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 514 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 515 | uint32_t dex_file_offset; |
| 516 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) { |
| 517 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated " |
| 518 | "after dex file offsets", |
| 519 | GetLocation().c_str(), |
| 520 | i, |
| 521 | dex_file_location.c_str()); |
| 522 | return false; |
| 523 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 524 | if (UNLIKELY(dex_file_offset == 0U)) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 525 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex " |
| 526 | "file offset", |
| 527 | GetLocation().c_str(), |
| 528 | i, |
| 529 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 530 | return false; |
| 531 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 532 | if (UNLIKELY(dex_file_offset > DexSize())) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 533 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 534 | "offset %u > %zu", |
| 535 | GetLocation().c_str(), |
| 536 | i, |
| 537 | dex_file_location.c_str(), |
| 538 | dex_file_offset, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 539 | DexSize()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 540 | return false; |
| 541 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 542 | if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 543 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 544 | "offset %u of %zu but the size of dex file header is %zu", |
| 545 | GetLocation().c_str(), |
| 546 | i, |
| 547 | dex_file_location.c_str(), |
| 548 | dex_file_offset, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 549 | DexSize(), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 550 | sizeof(DexFile::Header)); |
| 551 | return false; |
| 552 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 553 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 554 | const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 555 | if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 556 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " |
| 557 | "dex file magic '%s'", |
| 558 | GetLocation().c_str(), |
| 559 | i, |
| 560 | dex_file_location.c_str(), |
| 561 | dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 562 | return false; |
| 563 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 564 | if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 565 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " |
| 566 | "dex file version '%s'", |
| 567 | GetLocation().c_str(), |
| 568 | i, |
| 569 | dex_file_location.c_str(), |
| 570 | dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 571 | return false; |
| 572 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 573 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 574 | if (DexSize() - dex_file_offset < header->file_size_) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 575 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 576 | "offset %u and size %u truncated at %zu", |
| 577 | GetLocation().c_str(), |
| 578 | i, |
| 579 | dex_file_location.c_str(), |
| 580 | dex_file_offset, |
| 581 | header->file_size_, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 582 | DexSize()); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 583 | return false; |
| 584 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 585 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 586 | uint32_t class_offsets_offset; |
| 587 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) { |
| 588 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated " |
| 589 | "after class offsets offset", |
| 590 | GetLocation().c_str(), |
| 591 | i, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 592 | dex_file_location.c_str()); |
| 593 | return false; |
| 594 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 595 | if (UNLIKELY(class_offsets_offset > Size()) || |
| 596 | UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) { |
| 597 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated " |
| 598 | "class offsets, offset %u of %zu, class defs %u", |
| 599 | GetLocation().c_str(), |
| 600 | i, |
| 601 | dex_file_location.c_str(), |
| 602 | class_offsets_offset, |
| 603 | Size(), |
| 604 | header->class_defs_size_); |
| 605 | return false; |
| 606 | } |
| 607 | if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) { |
| 608 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned " |
| 609 | "class offsets, offset %u", |
| 610 | GetLocation().c_str(), |
| 611 | i, |
| 612 | dex_file_location.c_str(), |
| 613 | class_offsets_offset); |
| 614 | return false; |
| 615 | } |
| 616 | const uint32_t* class_offsets_pointer = |
| 617 | reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset); |
| 618 | |
| 619 | uint32_t lookup_table_offset; |
| 620 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) { |
| 621 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " |
| 622 | "after lookup table offset", |
| 623 | GetLocation().c_str(), |
| 624 | i, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 625 | dex_file_location.c_str()); |
| 626 | return false; |
| 627 | } |
| 628 | const uint8_t* lookup_table_data = lookup_table_offset != 0u |
| 629 | ? Begin() + lookup_table_offset |
| 630 | : nullptr; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 631 | if (lookup_table_offset != 0u && |
| 632 | (UNLIKELY(lookup_table_offset > Size()) || |
| 633 | UNLIKELY(Size() - lookup_table_offset < |
| 634 | TypeLookupTable::RawDataLength(header->class_defs_size_)))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 635 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated " |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 636 | "type lookup table, offset %u of %zu, class defs %u", |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 637 | GetLocation().c_str(), |
| 638 | i, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 639 | dex_file_location.c_str(), |
| 640 | lookup_table_offset, |
| 641 | Size(), |
| 642 | header->class_defs_size_); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 643 | return false; |
| 644 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 645 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 646 | uint32_t dex_layout_sections_offset; |
| 647 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) { |
| 648 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " |
| 649 | "after dex layout sections offset", |
| 650 | GetLocation().c_str(), |
| 651 | i, |
| 652 | dex_file_location.c_str()); |
| 653 | return false; |
| 654 | } |
| 655 | const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0 |
| 656 | ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset) |
| 657 | : nullptr; |
| 658 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 659 | uint32_t method_bss_mapping_offset; |
| 660 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &method_bss_mapping_offset))) { |
| 661 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " |
| 662 | "after method bss mapping offset", |
| 663 | GetLocation().c_str(), |
| 664 | i, |
| 665 | dex_file_location.c_str()); |
| 666 | return false; |
| 667 | } |
| 668 | const bool readable_method_bss_mapping_size = |
| 669 | method_bss_mapping_offset != 0u && |
| 670 | method_bss_mapping_offset <= Size() && |
| 671 | IsAligned<alignof(MethodBssMapping)>(method_bss_mapping_offset) && |
| 672 | Size() - method_bss_mapping_offset >= MethodBssMapping::ComputeSize(0); |
| 673 | const MethodBssMapping* method_bss_mapping = readable_method_bss_mapping_size |
| 674 | ? reinterpret_cast<const MethodBssMapping*>(Begin() + method_bss_mapping_offset) |
| 675 | : nullptr; |
| 676 | if (method_bss_mapping_offset != 0u && |
| 677 | (UNLIKELY(method_bss_mapping == nullptr) || |
| 678 | UNLIKELY(method_bss_mapping->size() == 0u) || |
| 679 | UNLIKELY(Size() - method_bss_mapping_offset < |
| 680 | MethodBssMapping::ComputeSize(method_bss_mapping->size())))) { |
| 681 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or " |
| 682 | " truncated method bss mapping, offset %u of %zu, length %zu", |
| 683 | GetLocation().c_str(), |
| 684 | i, |
| 685 | dex_file_location.c_str(), |
| 686 | method_bss_mapping_offset, |
| 687 | Size(), |
| 688 | method_bss_mapping != nullptr ? method_bss_mapping->size() : 0u); |
| 689 | return false; |
| 690 | } |
| 691 | if (kIsDebugBuild && method_bss_mapping != nullptr) { |
| 692 | const MethodBssMappingEntry* prev_entry = nullptr; |
| 693 | for (const MethodBssMappingEntry& entry : *method_bss_mapping) { |
| 694 | CHECK_ALIGNED_PARAM(entry.bss_offset, static_cast<size_t>(pointer_size)); |
| 695 | CHECK_LT(entry.bss_offset, BssSize()); |
| 696 | CHECK_LE(POPCOUNT(entry.index_mask) * static_cast<size_t>(pointer_size), entry.bss_offset); |
| 697 | size_t index_mask_span = (entry.index_mask != 0u) ? 16u - CTZ(entry.index_mask) : 0u; |
| 698 | CHECK_LE(index_mask_span, entry.method_index); |
| 699 | if (prev_entry != nullptr) { |
| 700 | CHECK_LT(prev_entry->method_index, entry.method_index - index_mask_span); |
| 701 | } |
| 702 | prev_entry = &entry; |
| 703 | } |
| 704 | CHECK_LT(prev_entry->method_index, |
| 705 | reinterpret_cast<const DexFile::Header*>(dex_file_pointer)->method_ids_size_); |
| 706 | } |
| 707 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 708 | std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str()); |
| 709 | |
| 710 | // Create the OatDexFile and add it to the owning container. |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 711 | OatDexFile* oat_dex_file = new OatDexFile(this, |
| 712 | dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 713 | canonical_location, |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 714 | dex_file_checksum, |
| 715 | dex_file_pointer, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 716 | lookup_table_data, |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 717 | method_bss_mapping, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 718 | class_offsets_pointer, |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 719 | dex_layout_sections); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 720 | oat_dex_files_storage_.push_back(oat_dex_file); |
| 721 | |
| 722 | // Add the location and canonical location (if different) to the oat_dex_files_ table. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 723 | StringPiece key(oat_dex_file->GetDexFileLocation()); |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 724 | oat_dex_files_.Put(key, oat_dex_file); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 725 | if (canonical_location != dex_file_location) { |
| 726 | StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation()); |
| 727 | oat_dex_files_.Put(canonical_key, oat_dex_file); |
| 728 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 729 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 730 | |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 731 | if (boot_image_tables != nullptr) { |
Vladimir Marko | 667585a | 2017-09-29 10:42:31 +0100 | [diff] [blame] | 732 | Runtime* runtime = Runtime::Current(); |
| 733 | if (UNLIKELY(runtime == nullptr)) { |
| 734 | // This must be oatdump without boot image. Make sure the .bss is inaccessible. |
Mathieu Chartier | 8d8de0c | 2017-10-04 09:35:30 -0700 | [diff] [blame] | 735 | CheckedCall(mprotect, "protect bss", const_cast<uint8_t*>(BssBegin()), BssSize(), PROT_NONE); |
Vladimir Marko | 667585a | 2017-09-29 10:42:31 +0100 | [diff] [blame] | 736 | } else { |
| 737 | // Map boot image tables into the .bss. The reserved size must match size of the tables. |
| 738 | size_t reserved_size = static_cast<size_t>(boot_image_tables_end - boot_image_tables); |
| 739 | size_t tables_size = 0u; |
| 740 | for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) { |
| 741 | tables_size += space->GetImageHeader().GetBootImageConstantTablesSize(); |
| 742 | DCHECK_ALIGNED(tables_size, kPageSize); |
| 743 | } |
| 744 | if (tables_size != reserved_size) { |
| 745 | *error_msg = StringPrintf("In oat file '%s' found unexpected boot image table sizes, " |
| 746 | " %zu bytes, should be %zu.", |
| 747 | GetLocation().c_str(), |
| 748 | reserved_size, |
| 749 | tables_size); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 750 | return false; |
| 751 | } |
Vladimir Marko | 667585a | 2017-09-29 10:42:31 +0100 | [diff] [blame] | 752 | for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) { |
| 753 | uint32_t current_tables_size = space->GetImageHeader().GetBootImageConstantTablesSize(); |
| 754 | if (current_tables_size != 0u && !MapConstantTables(space, boot_image_tables)) { |
| 755 | return false; |
| 756 | } |
| 757 | boot_image_tables += current_tables_size; |
| 758 | } |
| 759 | DCHECK(boot_image_tables == boot_image_tables_end); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 760 | } |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 761 | } |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 762 | return true; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 765 | //////////////////////// |
| 766 | // OatFile via dlopen // |
| 767 | //////////////////////// |
| 768 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 769 | class DlOpenOatFile FINAL : public OatFileBase { |
| 770 | public: |
| 771 | DlOpenOatFile(const std::string& filename, bool executable) |
| 772 | : OatFileBase(filename, executable), |
| 773 | dlopen_handle_(nullptr), |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 774 | shared_objects_before_(0) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | ~DlOpenOatFile() { |
| 778 | if (dlopen_handle_ != nullptr) { |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 779 | if (!kIsTargetBuild) { |
| 780 | MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_); |
| 781 | host_dlopen_handles_.erase(dlopen_handle_); |
Mathieu Chartier | c7d3f4b | 2016-06-01 10:48:19 -0700 | [diff] [blame] | 782 | dlclose(dlopen_handle_); |
| 783 | } else { |
| 784 | dlclose(dlopen_handle_); |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 785 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 786 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | protected: |
| 790 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, |
| 791 | std::string* error_msg) const OVERRIDE { |
| 792 | const uint8_t* ptr = |
| 793 | reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str())); |
| 794 | if (ptr == nullptr) { |
| 795 | *error_msg = dlerror(); |
| 796 | } |
| 797 | return ptr; |
| 798 | } |
| 799 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 800 | void PreLoad() OVERRIDE; |
| 801 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 802 | bool Load(const std::string& elf_filename, |
| 803 | uint8_t* oat_file_begin, |
| 804 | bool writable, |
| 805 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 806 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 807 | std::string* error_msg) OVERRIDE; |
| 808 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 809 | bool Load(int, uint8_t*, bool, bool, bool, std::string*) { |
| 810 | return false; |
| 811 | } |
| 812 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 813 | // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions. |
| 814 | void PreSetup(const std::string& elf_filename) OVERRIDE; |
| 815 | |
| 816 | private: |
| 817 | bool Dlopen(const std::string& elf_filename, |
| 818 | uint8_t* oat_file_begin, |
| 819 | std::string* error_msg); |
| 820 | |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 821 | // On the host, if the same library is loaded again with dlopen the same |
| 822 | // file handle is returned. This differs from the behavior of dlopen on the |
| 823 | // target, where dlopen reloads the library at a different address every |
| 824 | // time you load it. The runtime relies on the target behavior to ensure |
| 825 | // each instance of the loaded library has a unique dex cache. To avoid |
| 826 | // problems, we fall back to our own linker in the case when the same |
| 827 | // library is opened multiple times on host. dlopen_handles_ is used to |
| 828 | // detect that case. |
| 829 | // Guarded by host_dlopen_handles_lock_; |
| 830 | static std::unordered_set<void*> host_dlopen_handles_; |
| 831 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 832 | // dlopen handle during runtime. |
| 833 | void* dlopen_handle_; // TODO: Unique_ptr with custom deleter. |
| 834 | |
| 835 | // Dummy memory map objects corresponding to the regions mapped by dlopen. |
| 836 | std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_; |
| 837 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 838 | // The number of shared objects the linker told us about before loading. Used to |
| 839 | // (optimistically) optimize the PreSetup stage (see comment there). |
| 840 | size_t shared_objects_before_; |
| 841 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 842 | DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile); |
| 843 | }; |
| 844 | |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 845 | std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_; |
| 846 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 847 | void DlOpenOatFile::PreLoad() { |
| 848 | #ifdef __APPLE__ |
Andreas Gampe | 39004a6 | 2016-05-18 21:27:00 -0700 | [diff] [blame] | 849 | UNUSED(shared_objects_before_); |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 850 | LOG(FATAL) << "Should not reach here."; |
| 851 | UNREACHABLE(); |
| 852 | #else |
| 853 | // Count the entries in dl_iterate_phdr we get at this point in time. |
| 854 | struct dl_iterate_context { |
| 855 | static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED, |
| 856 | size_t size ATTRIBUTE_UNUSED, |
| 857 | void *data) { |
| 858 | reinterpret_cast<dl_iterate_context*>(data)->count++; |
| 859 | return 0; // Continue iteration. |
| 860 | } |
| 861 | size_t count = 0; |
| 862 | } context; |
| 863 | |
| 864 | dl_iterate_phdr(dl_iterate_context::callback, &context); |
| 865 | shared_objects_before_ = context.count; |
| 866 | #endif |
| 867 | } |
| 868 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 869 | bool DlOpenOatFile::Load(const std::string& elf_filename, |
| 870 | uint8_t* oat_file_begin, |
| 871 | bool writable, |
| 872 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 873 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 874 | std::string* error_msg) { |
| 875 | // Use dlopen only when flagged to do so, and when it's OK to load things executable. |
| 876 | // TODO: Also try when not executable? The issue here could be re-mapping as writable (as |
| 877 | // !executable is a sign that we may want to patch), which may not be allowed for |
| 878 | // various reasons. |
| 879 | if (!kUseDlopen) { |
| 880 | *error_msg = "DlOpen is disabled."; |
| 881 | return false; |
| 882 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 883 | if (low_4gb) { |
| 884 | *error_msg = "DlOpen does not support low 4gb loading."; |
| 885 | return false; |
| 886 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 887 | if (writable) { |
| 888 | *error_msg = "DlOpen does not support writable loading."; |
| 889 | return false; |
| 890 | } |
| 891 | if (!executable) { |
| 892 | *error_msg = "DlOpen does not support non-executable loading."; |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | // dlopen always returns the same library if it is already opened on the host. For this reason |
| 897 | // we only use dlopen if we are the target or we do not already have the dex file opened. Having |
| 898 | // the same library loaded multiple times at different addresses is required for class unloading |
| 899 | // and for having dex caches arrays in the .bss section. |
| 900 | if (!kIsTargetBuild) { |
| 901 | if (!kUseDlopenOnHost) { |
| 902 | *error_msg = "DlOpen disabled for host."; |
| 903 | return false; |
| 904 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | bool success = Dlopen(elf_filename, oat_file_begin, error_msg); |
| 908 | DCHECK(dlopen_handle_ != nullptr || !success); |
| 909 | |
| 910 | return success; |
| 911 | } |
| 912 | |
| 913 | bool DlOpenOatFile::Dlopen(const std::string& elf_filename, |
| 914 | uint8_t* oat_file_begin, |
| 915 | std::string* error_msg) { |
| 916 | #ifdef __APPLE__ |
| 917 | // The dl_iterate_phdr syscall is missing. There is similar API on OSX, |
| 918 | // but let's fallback to the custom loading code for the time being. |
| 919 | UNUSED(elf_filename, oat_file_begin); |
| 920 | *error_msg = "Dlopen unsupported on Mac."; |
| 921 | return false; |
| 922 | #else |
| 923 | { |
| 924 | UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr)); |
| 925 | if (absolute_path == nullptr) { |
| 926 | *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str()); |
| 927 | return false; |
| 928 | } |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 929 | #ifdef ART_TARGET_ANDROID |
Anton Kirilov | 3a2e78e | 2017-01-06 13:33:42 +0000 | [diff] [blame] | 930 | android_dlextinfo extinfo = {}; |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 931 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle |
| 932 | // (open oat files multiple |
| 933 | // times). |
| 934 | ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute |
| 935 | // (non-pic boot image). |
Andreas Gampe | 226b91e | 2015-12-02 10:29:33 -0800 | [diff] [blame] | 936 | if (oat_file_begin != nullptr) { // |
| 937 | extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if |
| 938 | extinfo.reserved_addr = oat_file_begin; // vaddr = 0. |
| 939 | } // (pic boot image). |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 940 | dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo); |
| 941 | #else |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 942 | UNUSED(oat_file_begin); |
Julien Duraj | 1af0c4f | 2016-11-16 14:05:48 +0000 | [diff] [blame] | 943 | static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles"); |
Mathieu Chartier | c7d3f4b | 2016-06-01 10:48:19 -0700 | [diff] [blame] | 944 | MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_); |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 945 | dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW); |
| 946 | if (dlopen_handle_ != nullptr) { |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 947 | if (!host_dlopen_handles_.insert(dlopen_handle_).second) { |
| 948 | dlclose(dlopen_handle_); |
| 949 | dlopen_handle_ = nullptr; |
| 950 | *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str()); |
| 951 | return false; |
| 952 | } |
| 953 | } |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 954 | #endif // ART_TARGET_ANDROID |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 955 | } |
| 956 | if (dlopen_handle_ == nullptr) { |
| 957 | *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror()); |
| 958 | return false; |
| 959 | } |
| 960 | return true; |
| 961 | #endif |
| 962 | } |
| 963 | |
| 964 | void DlOpenOatFile::PreSetup(const std::string& elf_filename) { |
Andreas Gampe | 74f07b5 | 2015-12-02 11:53:26 -0800 | [diff] [blame] | 965 | #ifdef __APPLE__ |
| 966 | UNUSED(elf_filename); |
| 967 | LOG(FATAL) << "Should not reach here."; |
| 968 | UNREACHABLE(); |
| 969 | #else |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 970 | struct dl_iterate_context { |
| 971 | static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) { |
| 972 | auto* context = reinterpret_cast<dl_iterate_context*>(data); |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 973 | context->shared_objects_seen++; |
| 974 | if (context->shared_objects_seen < context->shared_objects_before) { |
| 975 | // We haven't been called yet for anything we haven't seen before. Just continue. |
| 976 | // Note: this is aggressively optimistic. If another thread was unloading a library, |
| 977 | // we may miss out here. However, this does not happen often in practice. |
| 978 | return 0; |
| 979 | } |
| 980 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 981 | // See whether this callback corresponds to the file which we have just loaded. |
| 982 | bool contains_begin = false; |
| 983 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 984 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 985 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 986 | info->dlpi_phdr[i].p_vaddr); |
| 987 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
| 988 | if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) { |
| 989 | contains_begin = true; |
| 990 | break; |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | // Add dummy mmaps for this file. |
| 995 | if (contains_begin) { |
| 996 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 997 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 998 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 999 | info->dlpi_phdr[i].p_vaddr); |
| 1000 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
| 1001 | MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz); |
| 1002 | context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap)); |
| 1003 | } |
| 1004 | } |
| 1005 | return 1; // Stop iteration and return 1 from dl_iterate_phdr. |
| 1006 | } |
| 1007 | return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished. |
| 1008 | } |
| 1009 | const uint8_t* const begin_; |
| 1010 | std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_; |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 1011 | const size_t shared_objects_before; |
| 1012 | size_t shared_objects_seen; |
| 1013 | }; |
| 1014 | dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0}; |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1015 | |
| 1016 | if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) { |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 1017 | // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0 |
| 1018 | // before giving up. This should be unusual. |
| 1019 | VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before=" |
| 1020 | << shared_objects_before_; |
| 1021 | dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0}; |
| 1022 | if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) { |
| 1023 | // OK, give up and print an error. |
| 1024 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 1025 | LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps."; |
| 1026 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1027 | } |
Andreas Gampe | 74f07b5 | 2015-12-02 11:53:26 -0800 | [diff] [blame] | 1028 | #endif |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | //////////////////////////////////////////////// |
| 1032 | // OatFile via our own ElfFile implementation // |
| 1033 | //////////////////////////////////////////////// |
| 1034 | |
| 1035 | class ElfOatFile FINAL : public OatFileBase { |
| 1036 | public: |
| 1037 | ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {} |
| 1038 | |
| 1039 | static ElfOatFile* OpenElfFile(File* file, |
| 1040 | const std::string& location, |
| 1041 | uint8_t* requested_base, |
| 1042 | uint8_t* oat_file_begin, // Override base if not null |
| 1043 | bool writable, |
| 1044 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1045 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1046 | const char* abs_dex_location, |
| 1047 | std::string* error_msg); |
| 1048 | |
| 1049 | bool InitializeFromElfFile(ElfFile* elf_file, |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 1050 | VdexFile* vdex_file, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1051 | const char* abs_dex_location, |
| 1052 | std::string* error_msg); |
| 1053 | |
| 1054 | protected: |
| 1055 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, |
| 1056 | std::string* error_msg) const OVERRIDE { |
| 1057 | const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name); |
| 1058 | if (ptr == nullptr) { |
| 1059 | *error_msg = "(Internal implementation could not find symbol)"; |
| 1060 | } |
| 1061 | return ptr; |
| 1062 | } |
| 1063 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 1064 | void PreLoad() OVERRIDE { |
| 1065 | } |
| 1066 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1067 | bool Load(const std::string& elf_filename, |
| 1068 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 1069 | bool writable, |
| 1070 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1071 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1072 | std::string* error_msg) OVERRIDE; |
| 1073 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 1074 | bool Load(int oat_fd, |
| 1075 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 1076 | bool writable, |
| 1077 | bool executable, |
| 1078 | bool low_4gb, |
| 1079 | std::string* error_msg) OVERRIDE; |
| 1080 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1081 | void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE { |
| 1082 | } |
| 1083 | |
| 1084 | private: |
| 1085 | bool ElfFileOpen(File* file, |
| 1086 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 1087 | bool writable, |
| 1088 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1089 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1090 | std::string* error_msg); |
| 1091 | |
| 1092 | private: |
| 1093 | // Backing memory map for oat file during cross compilation. |
| 1094 | std::unique_ptr<ElfFile> elf_file_; |
| 1095 | |
| 1096 | DISALLOW_COPY_AND_ASSIGN(ElfOatFile); |
| 1097 | }; |
| 1098 | |
| 1099 | ElfOatFile* ElfOatFile::OpenElfFile(File* file, |
| 1100 | const std::string& location, |
| 1101 | uint8_t* requested_base, |
| 1102 | uint8_t* oat_file_begin, // Override base if not null |
| 1103 | bool writable, |
| 1104 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1105 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1106 | const char* abs_dex_location, |
| 1107 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1108 | ScopedTrace trace("Open elf file " + location); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1109 | std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable)); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1110 | bool success = oat_file->ElfFileOpen(file, |
| 1111 | oat_file_begin, |
| 1112 | writable, |
| 1113 | low_4gb, |
| 1114 | executable, |
| 1115 | error_msg); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1116 | if (!success) { |
| 1117 | CHECK(!error_msg->empty()); |
| 1118 | return nullptr; |
| 1119 | } |
| 1120 | |
| 1121 | // Complete the setup. |
| 1122 | if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) { |
| 1123 | return nullptr; |
| 1124 | } |
| 1125 | |
| 1126 | if (!oat_file->Setup(abs_dex_location, error_msg)) { |
| 1127 | return nullptr; |
| 1128 | } |
| 1129 | |
| 1130 | return oat_file.release(); |
| 1131 | } |
| 1132 | |
| 1133 | bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file, |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 1134 | VdexFile* vdex_file, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1135 | const char* abs_dex_location, |
| 1136 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1137 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1138 | if (IsExecutable()) { |
| 1139 | *error_msg = "Cannot initialize from elf file in executable mode."; |
| 1140 | return false; |
| 1141 | } |
| 1142 | elf_file_.reset(elf_file); |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 1143 | SetVdex(vdex_file); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1144 | uint64_t offset, size; |
| 1145 | bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size); |
| 1146 | CHECK(has_section); |
| 1147 | SetBegin(elf_file->Begin() + offset); |
| 1148 | SetEnd(elf_file->Begin() + size + offset); |
| 1149 | // Ignore the optional .bss section when opening non-executable. |
| 1150 | return Setup(abs_dex_location, error_msg); |
| 1151 | } |
| 1152 | |
| 1153 | bool ElfOatFile::Load(const std::string& elf_filename, |
| 1154 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 1155 | bool writable, |
| 1156 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1157 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1158 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1159 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1160 | std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str())); |
| 1161 | if (file == nullptr) { |
| 1162 | *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno)); |
| 1163 | return false; |
| 1164 | } |
| 1165 | return ElfOatFile::ElfFileOpen(file.get(), |
| 1166 | oat_file_begin, |
| 1167 | writable, |
| 1168 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1169 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1170 | error_msg); |
| 1171 | } |
| 1172 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 1173 | bool ElfOatFile::Load(int oat_fd, |
| 1174 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 1175 | bool writable, |
| 1176 | bool executable, |
| 1177 | bool low_4gb, |
| 1178 | std::string* error_msg) { |
| 1179 | ScopedTrace trace(__PRETTY_FUNCTION__); |
| 1180 | if (oat_fd != -1) { |
| 1181 | std::unique_ptr<File> file = std::make_unique<File>(oat_fd, false); |
| 1182 | file->DisableAutoClose(); |
| 1183 | if (file == nullptr) { |
| 1184 | *error_msg = StringPrintf("Failed to open oat filename for reading: %s", |
| 1185 | strerror(errno)); |
| 1186 | return false; |
| 1187 | } |
| 1188 | return ElfOatFile::ElfFileOpen(file.get(), |
| 1189 | oat_file_begin, |
| 1190 | writable, |
| 1191 | executable, |
| 1192 | low_4gb, |
| 1193 | error_msg); |
| 1194 | } |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1198 | bool ElfOatFile::ElfFileOpen(File* file, |
| 1199 | uint8_t* oat_file_begin, |
| 1200 | bool writable, |
| 1201 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1202 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1203 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1204 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1205 | // TODO: rename requested_base to oat_data_begin |
| 1206 | elf_file_.reset(ElfFile::Open(file, |
| 1207 | writable, |
| 1208 | /*program_header_only*/true, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1209 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1210 | error_msg, |
| 1211 | oat_file_begin)); |
| 1212 | if (elf_file_ == nullptr) { |
| 1213 | DCHECK(!error_msg->empty()); |
| 1214 | return false; |
| 1215 | } |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1216 | bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1217 | DCHECK(loaded || !error_msg->empty()); |
| 1218 | return loaded; |
| 1219 | } |
| 1220 | |
| 1221 | ////////////////////////// |
| 1222 | // General OatFile code // |
| 1223 | ////////////////////////// |
| 1224 | |
| 1225 | std::string OatFile::ResolveRelativeEncodedDexLocation( |
| 1226 | const char* abs_dex_location, const std::string& rel_dex_location) { |
| 1227 | if (abs_dex_location != nullptr && rel_dex_location[0] != '/') { |
| 1228 | // Strip :classes<N>.dex used for secondary multidex files. |
| 1229 | std::string base = DexFile::GetBaseLocation(rel_dex_location); |
| 1230 | std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location); |
| 1231 | |
| 1232 | // Check if the base is a suffix of the provided abs_dex_location. |
| 1233 | std::string target_suffix = "/" + base; |
| 1234 | std::string abs_location(abs_dex_location); |
| 1235 | if (abs_location.size() > target_suffix.size()) { |
| 1236 | size_t pos = abs_location.size() - target_suffix.size(); |
| 1237 | if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) { |
| 1238 | return abs_location + multidex_suffix; |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | return rel_dex_location; |
| 1243 | } |
| 1244 | |
| 1245 | static void CheckLocation(const std::string& location) { |
| 1246 | CHECK(!location.empty()); |
| 1247 | } |
| 1248 | |
| 1249 | OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file, |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 1250 | VdexFile* vdex_file, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1251 | const std::string& location, |
| 1252 | const char* abs_dex_location, |
| 1253 | std::string* error_msg) { |
| 1254 | std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */)); |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 1255 | return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg) |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1256 | ? oat_file.release() |
| 1257 | : nullptr; |
| 1258 | } |
| 1259 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1260 | OatFile* OatFile::Open(const std::string& oat_filename, |
| 1261 | const std::string& oat_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1262 | uint8_t* requested_base, |
| 1263 | uint8_t* oat_file_begin, |
| 1264 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1265 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1266 | const char* abs_dex_location, |
| 1267 | std::string* error_msg) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1268 | ScopedTrace trace("Open oat file " + oat_location); |
| 1269 | CHECK(!oat_filename.empty()) << oat_location; |
| 1270 | CheckLocation(oat_location); |
Andreas Gampe | 54315c7 | 2016-05-18 21:10:42 -0700 | [diff] [blame] | 1271 | |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 1272 | std::string vdex_filename = GetVdexFilename(oat_filename); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1273 | |
| 1274 | // Check that the files even exist, fast-fail. |
| 1275 | if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) { |
| 1276 | *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str()); |
| 1277 | return nullptr; |
| 1278 | } else if (!OS::FileExists(oat_filename.c_str())) { |
| 1279 | *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str()); |
Andreas Gampe | 54315c7 | 2016-05-18 21:10:42 -0700 | [diff] [blame] | 1280 | return nullptr; |
| 1281 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1282 | |
| 1283 | // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is |
| 1284 | // disabled. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1285 | OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename, |
| 1286 | oat_filename, |
| 1287 | oat_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1288 | requested_base, |
| 1289 | oat_file_begin, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1290 | false /* writable */, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1291 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1292 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1293 | abs_dex_location, |
| 1294 | error_msg); |
| 1295 | if (with_dlopen != nullptr) { |
| 1296 | return with_dlopen; |
| 1297 | } |
| 1298 | if (kPrintDlOpenErrorMessage) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1299 | LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg; |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1300 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1301 | // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: |
| 1302 | // |
| 1303 | // On target, dlopen may fail when compiling due to selinux restrictions on installd. |
| 1304 | // |
| 1305 | // We use our own ELF loader for Quick to deal with legacy apps that |
| 1306 | // open a generated dex file by name, remove the file, then open |
| 1307 | // another generated dex file with the same name. http://b/10614658 |
| 1308 | // |
| 1309 | // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. |
| 1310 | // |
| 1311 | // |
| 1312 | // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually |
| 1313 | // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1314 | OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename, |
| 1315 | oat_filename, |
| 1316 | oat_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1317 | requested_base, |
| 1318 | oat_file_begin, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1319 | false /* writable */, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1320 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1321 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1322 | abs_dex_location, |
| 1323 | error_msg); |
| 1324 | return with_internal; |
| 1325 | } |
| 1326 | |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame^] | 1327 | OatFile* OatFile::Open(int vdex_fd, |
| 1328 | int oat_fd, |
| 1329 | const std::string& oat_location, |
| 1330 | uint8_t* requested_base, |
| 1331 | uint8_t* oat_file_begin, |
| 1332 | bool executable, |
| 1333 | bool low_4gb, |
| 1334 | const char* abs_dex_location, |
| 1335 | std::string* error_msg) { |
| 1336 | CHECK(!oat_location.empty()) << oat_location; |
| 1337 | |
| 1338 | std::string vdex_location = GetVdexFilename(oat_location); |
| 1339 | |
| 1340 | OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_fd, |
| 1341 | oat_fd, |
| 1342 | vdex_location, |
| 1343 | oat_location, |
| 1344 | requested_base, |
| 1345 | oat_file_begin, |
| 1346 | false /* writable */, |
| 1347 | executable, |
| 1348 | low_4gb, |
| 1349 | abs_dex_location, |
| 1350 | error_msg); |
| 1351 | return with_internal; |
| 1352 | } |
| 1353 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1354 | OatFile* OatFile::OpenWritable(File* file, |
| 1355 | const std::string& location, |
| 1356 | const char* abs_dex_location, |
| 1357 | std::string* error_msg) { |
| 1358 | CheckLocation(location); |
| 1359 | return ElfOatFile::OpenElfFile(file, |
| 1360 | location, |
| 1361 | nullptr, |
| 1362 | nullptr, |
| 1363 | true, |
| 1364 | false, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1365 | /*low_4gb*/false, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1366 | abs_dex_location, |
| 1367 | error_msg); |
| 1368 | } |
| 1369 | |
| 1370 | OatFile* OatFile::OpenReadable(File* file, |
| 1371 | const std::string& location, |
| 1372 | const char* abs_dex_location, |
| 1373 | std::string* error_msg) { |
| 1374 | CheckLocation(location); |
| 1375 | return ElfOatFile::OpenElfFile(file, |
| 1376 | location, |
| 1377 | nullptr, |
| 1378 | nullptr, |
| 1379 | false, |
| 1380 | false, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1381 | /*low_4gb*/false, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1382 | abs_dex_location, |
| 1383 | error_msg); |
| 1384 | } |
| 1385 | |
| 1386 | OatFile::OatFile(const std::string& location, bool is_executable) |
| 1387 | : location_(location), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1388 | vdex_(nullptr), |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1389 | begin_(nullptr), |
| 1390 | end_(nullptr), |
| 1391 | bss_begin_(nullptr), |
| 1392 | bss_end_(nullptr), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1393 | bss_methods_(nullptr), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1394 | bss_roots_(nullptr), |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1395 | is_executable_(is_executable), |
| 1396 | secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) { |
| 1397 | CHECK(!location_.empty()); |
| 1398 | } |
| 1399 | |
| 1400 | OatFile::~OatFile() { |
| 1401 | STLDeleteElements(&oat_dex_files_storage_); |
| 1402 | } |
| 1403 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1404 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1405 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1408 | const uint8_t* OatFile::Begin() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 1409 | CHECK(begin_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1410 | return begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1413 | const uint8_t* OatFile::End() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 1414 | CHECK(end_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1415 | return end_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1418 | const uint8_t* OatFile::BssBegin() const { |
| 1419 | return bss_begin_; |
| 1420 | } |
| 1421 | |
| 1422 | const uint8_t* OatFile::BssEnd() const { |
| 1423 | return bss_end_; |
| 1424 | } |
| 1425 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1426 | const uint8_t* OatFile::DexBegin() const { |
| 1427 | return kIsVdexEnabled ? vdex_->Begin() : Begin(); |
| 1428 | } |
| 1429 | |
| 1430 | const uint8_t* OatFile::DexEnd() const { |
| 1431 | return kIsVdexEnabled ? vdex_->End() : End(); |
| 1432 | } |
| 1433 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1434 | ArrayRef<ArtMethod*> OatFile::GetBssMethods() const { |
| 1435 | if (bss_methods_ != nullptr) { |
| 1436 | ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_); |
| 1437 | ArtMethod** methods_end = |
| 1438 | reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_); |
| 1439 | return ArrayRef<ArtMethod*>(methods, methods_end - methods); |
| 1440 | } else { |
| 1441 | return ArrayRef<ArtMethod*>(); |
| 1442 | } |
| 1443 | } |
| 1444 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1445 | ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const { |
| 1446 | if (bss_roots_ != nullptr) { |
| 1447 | auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_); |
| 1448 | auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_); |
| 1449 | return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots); |
| 1450 | } else { |
| 1451 | return ArrayRef<GcRoot<mirror::Object>>(); |
| 1452 | } |
| 1453 | } |
| 1454 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1455 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location, |
| 1456 | const uint32_t* dex_location_checksum, |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1457 | std::string* error_msg) const { |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1458 | // NOTE: We assume here that the canonical location for a given dex_location never |
| 1459 | // changes. If it does (i.e. some symlink used by the filename changes) we may return |
| 1460 | // an incorrect OatDexFile. As long as we have a checksum to check, we shall return |
| 1461 | // an identical file or fail; otherwise we may see some unpredictable failures. |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1462 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1463 | // TODO: Additional analysis of usage patterns to see if this can be simplified |
| 1464 | // without any performance loss, for example by not doing the first lock-free lookup. |
| 1465 | |
| 1466 | const OatFile::OatDexFile* oat_dex_file = nullptr; |
| 1467 | StringPiece key(dex_location); |
| 1468 | // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations |
| 1469 | // directly mentioned in the oat file and doesn't require locking. |
| 1470 | auto primary_it = oat_dex_files_.find(key); |
| 1471 | if (primary_it != oat_dex_files_.end()) { |
| 1472 | oat_dex_file = primary_it->second; |
| 1473 | DCHECK(oat_dex_file != nullptr); |
| 1474 | } else { |
| 1475 | // This dex_location is not one of the dex locations directly mentioned in the |
| 1476 | // oat file. The correct lookup is via the canonical location but first see in |
| 1477 | // the secondary_oat_dex_files_ whether we've looked up this location before. |
| 1478 | MutexLock mu(Thread::Current(), secondary_lookup_lock_); |
| 1479 | auto secondary_lb = secondary_oat_dex_files_.lower_bound(key); |
| 1480 | if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1481 | oat_dex_file = secondary_lb->second; // May be null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1482 | } else { |
| 1483 | // We haven't seen this dex_location before, we must check the canonical location. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1484 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1485 | if (dex_canonical_location != dex_location) { |
| 1486 | StringPiece canonical_key(dex_canonical_location); |
| 1487 | auto canonical_it = oat_dex_files_.find(canonical_key); |
| 1488 | if (canonical_it != oat_dex_files_.end()) { |
| 1489 | oat_dex_file = canonical_it->second; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1490 | } // else keep null. |
| 1491 | } // else keep null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1492 | |
| 1493 | // Copy the key to the string_cache_ and store the result in secondary map. |
| 1494 | string_cache_.emplace_back(key.data(), key.length()); |
| 1495 | StringPiece key_copy(string_cache_.back()); |
| 1496 | secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 1497 | } |
| 1498 | } |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1499 | |
| 1500 | if (oat_dex_file == nullptr) { |
| 1501 | if (error_msg != nullptr) { |
| 1502 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
| 1503 | *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location) |
| 1504 | + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation(); |
| 1505 | } |
| 1506 | return nullptr; |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1507 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 1508 | |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1509 | if (dex_location_checksum != nullptr && |
| 1510 | oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) { |
| 1511 | if (error_msg != nullptr) { |
| 1512 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
| 1513 | std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum()); |
| 1514 | std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum); |
| 1515 | *error_msg = "OatDexFile for DexFile " + std::string(dex_location) |
| 1516 | + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation() |
| 1517 | + " has checksum " + checksum + " but " + required_checksum + " was required"; |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1518 | } |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1519 | return nullptr; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1520 | } |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1521 | return oat_dex_file; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1524 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 1525 | const std::string& dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1526 | const std::string& canonical_dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1527 | uint32_t dex_file_location_checksum, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1528 | const uint8_t* dex_file_pointer, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1529 | const uint8_t* lookup_table_data, |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1530 | const MethodBssMapping* method_bss_mapping_data, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1531 | const uint32_t* oat_class_offsets_pointer, |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1532 | const DexLayoutSections* dex_layout_sections) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1533 | : oat_file_(oat_file), |
| 1534 | dex_file_location_(dex_file_location), |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1535 | canonical_dex_file_location_(canonical_dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1536 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1537 | dex_file_pointer_(dex_file_pointer), |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1538 | lookup_table_data_(lookup_table_data), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1539 | method_bss_mapping_(method_bss_mapping_data), |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1540 | oat_class_offsets_pointer_(oat_class_offsets_pointer), |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1541 | dex_layout_sections_(dex_layout_sections) { |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1542 | // Initialize TypeLookupTable. |
| 1543 | if (lookup_table_data_ != nullptr) { |
| 1544 | // Peek the number of classes from the DexFile. |
| 1545 | const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_); |
| 1546 | const uint32_t num_class_defs = dex_header->class_defs_size_; |
| 1547 | if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) { |
| 1548 | LOG(WARNING) << "found truncated lookup table in " << dex_file_location_; |
| 1549 | } else { |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1550 | lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1554 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1555 | OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table) |
| 1556 | : lookup_table_(std::move(lookup_table)) {} |
| 1557 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1558 | OatFile::OatDexFile::~OatDexFile() {} |
| 1559 | |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 1560 | size_t OatFile::OatDexFile::FileSize() const { |
| 1561 | return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 1562 | } |
| 1563 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1564 | std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1565 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1566 | static constexpr bool kVerify = false; |
| 1567 | static constexpr bool kVerifyChecksum = false; |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 1568 | return DexFile::Open(dex_file_pointer_, |
| 1569 | FileSize(), |
| 1570 | dex_file_location_, |
| 1571 | dex_file_location_checksum_, |
| 1572 | this, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1573 | kVerify, |
| 1574 | kVerifyChecksum, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 1575 | error_msg); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1576 | } |
| 1577 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1578 | uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const { |
| 1579 | return oat_class_offsets_pointer_[class_def_index]; |
| 1580 | } |
| 1581 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1582 | OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1583 | uint32_t oat_class_offset = GetOatClassOffset(class_def_index); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1584 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1585 | const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1586 | CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1587 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1588 | const uint8_t* status_pointer = oat_class_pointer; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1589 | CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 1590 | mirror::Class::Status status = |
| 1591 | static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer)); |
| 1592 | CHECK_LT(status, mirror::Class::kStatusMax); |
| 1593 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1594 | const uint8_t* type_pointer = status_pointer + sizeof(uint16_t); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1595 | CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 1596 | OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer)); |
| 1597 | CHECK_LT(type, kOatClassMax); |
| 1598 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1599 | const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1600 | CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1601 | |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1602 | uint32_t bitmap_size = 0; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1603 | const uint8_t* bitmap_pointer = nullptr; |
| 1604 | const uint8_t* methods_pointer = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1605 | if (type != kOatClassNoneCompiled) { |
| 1606 | if (type == kOatClassSomeCompiled) { |
| 1607 | bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer)); |
| 1608 | bitmap_pointer = after_type_pointer + sizeof(bitmap_size); |
| 1609 | CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 1610 | methods_pointer = bitmap_pointer + bitmap_size; |
| 1611 | } else { |
| 1612 | methods_pointer = after_type_pointer; |
| 1613 | } |
| 1614 | CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1615 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1616 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1617 | return OatFile::OatClass(oat_file_, |
| 1618 | status, |
| 1619 | type, |
| 1620 | bitmap_size, |
| 1621 | reinterpret_cast<const uint32_t*>(bitmap_pointer), |
| 1622 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1625 | const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file, |
| 1626 | const char* descriptor, |
| 1627 | size_t hash) { |
| 1628 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 1629 | DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash); |
| 1630 | if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) { |
| 1631 | const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 1632 | return (class_def_idx != dex::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr; |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1633 | } |
| 1634 | // Fast path for rare no class defs case. |
| 1635 | const uint32_t num_class_defs = dex_file.NumClassDefs(); |
| 1636 | if (num_class_defs == 0) { |
| 1637 | return nullptr; |
| 1638 | } |
| 1639 | const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor); |
| 1640 | if (type_id != nullptr) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1641 | dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1642 | return dex_file.FindClassDef(type_idx); |
| 1643 | } |
| 1644 | return nullptr; |
| 1645 | } |
| 1646 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1647 | // Madvise the dex file based on the state we are moving to. |
| 1648 | void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) { |
Mathieu Chartier | 150d25d | 2017-08-28 09:52:55 -0700 | [diff] [blame] | 1649 | const bool low_ram = Runtime::Current()->GetHeap()->IsLowMemoryMode(); |
| 1650 | // TODO: Also do madvise hints for non low ram devices. |
| 1651 | if (!kMadviseDexFileAccesses || !low_ram) { |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1652 | return; |
| 1653 | } |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1654 | if (state == MadviseState::kMadviseStateAtLoad) { |
Mathieu Chartier | 150d25d | 2017-08-28 09:52:55 -0700 | [diff] [blame] | 1655 | if (low_ram) { |
Mathieu Chartier | 4ec507d | 2017-08-15 15:21:40 -0700 | [diff] [blame] | 1656 | // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices. |
| 1657 | // Other devices have enough page cache to get performance benefits from loading more pages |
| 1658 | // into the page cache. |
| 1659 | MadviseLargestPageAlignedRegion(dex_file.Begin(), |
| 1660 | dex_file.Begin() + dex_file.Size(), |
| 1661 | MADV_RANDOM); |
| 1662 | } |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1663 | } |
| 1664 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 1665 | if (oat_dex_file != nullptr) { |
| 1666 | // Should always be there. |
| 1667 | const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections(); |
| 1668 | CHECK(sections != nullptr); |
| 1669 | sections->Madvise(&dex_file, state); |
| 1670 | } |
| 1671 | } |
| 1672 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1673 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1674 | mirror::Class::Status status, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1675 | OatClassType type, |
| 1676 | uint32_t bitmap_size, |
| 1677 | const uint32_t* bitmap_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1678 | const OatMethodOffsets* methods_pointer) |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1679 | : oat_file_(oat_file), status_(status), type_(type), |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1680 | bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1681 | switch (type_) { |
| 1682 | case kOatClassAllCompiled: { |
| 1683 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1684 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1685 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1686 | break; |
| 1687 | } |
| 1688 | case kOatClassSomeCompiled: { |
| 1689 | CHECK_NE(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1690 | CHECK(bitmap_pointer != nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1691 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1692 | break; |
| 1693 | } |
| 1694 | case kOatClassNoneCompiled: { |
| 1695 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1696 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1697 | CHECK(methods_pointer_ == nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1698 | break; |
| 1699 | } |
| 1700 | case kOatClassMax: { |
| 1701 | LOG(FATAL) << "Invalid OatClassType " << type_; |
| 1702 | break; |
| 1703 | } |
| 1704 | } |
| 1705 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1706 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1707 | uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const { |
| 1708 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 1709 | if (oat_method_offsets == nullptr) { |
| 1710 | return 0u; |
| 1711 | } |
| 1712 | return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin(); |
| 1713 | } |
| 1714 | |
| 1715 | const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1716 | // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index. |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1717 | if (methods_pointer_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1718 | CHECK_EQ(kOatClassNoneCompiled, type_); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1719 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1720 | } |
| 1721 | size_t methods_pointer_index; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1722 | if (bitmap_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1723 | CHECK_EQ(kOatClassAllCompiled, type_); |
| 1724 | methods_pointer_index = method_index; |
| 1725 | } else { |
| 1726 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1727 | if (!BitVector::IsBitSet(bitmap_, method_index)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1728 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1729 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1730 | size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index); |
| 1731 | methods_pointer_index = num_set_bits; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1732 | } |
| 1733 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index]; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1734 | return &oat_method_offsets; |
| 1735 | } |
| 1736 | |
| 1737 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 1738 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 1739 | if (oat_method_offsets == nullptr) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1740 | return OatMethod(nullptr, 0); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1741 | } |
| 1742 | if (oat_file_->IsExecutable() || |
| 1743 | Runtime::Current() == nullptr || // This case applies for oatdump. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1744 | Runtime::Current()->IsAotCompiler()) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1745 | return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_); |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1746 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1747 | // We aren't allowed to use the compiled code. We just force it down the interpreted / jit |
| 1748 | // version. |
| 1749 | return OatMethod(oat_file_->Begin(), 0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1752 | void OatFile::OatMethod::LinkMethod(ArtMethod* method) const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 1753 | CHECK(method != nullptr); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1754 | method->SetEntryPointFromQuickCompiledCode(GetQuickCode()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1757 | bool OatFile::IsPic() const { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1758 | return GetOatHeader().IsPic(); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1759 | // TODO: Check against oat_patches. b/18144996 |
| 1760 | } |
| 1761 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 1762 | bool OatFile::IsDebuggable() const { |
| 1763 | return GetOatHeader().IsDebuggable(); |
| 1764 | } |
| 1765 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1766 | CompilerFilter::Filter OatFile::GetCompilerFilter() const { |
| 1767 | return GetOatHeader().GetCompilerFilter(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1770 | std::string OatFile::GetClassLoaderContext() const { |
| 1771 | return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey); |
| 1772 | }; |
| 1773 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 1774 | OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file, |
| 1775 | uint16_t class_def_idx, |
| 1776 | bool* found) { |
| 1777 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
| 1778 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1779 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 1780 | *found = false; |
| 1781 | return OatFile::OatClass::Invalid(); |
| 1782 | } |
| 1783 | *found = true; |
| 1784 | return oat_dex_file->GetOatClass(class_def_idx); |
| 1785 | } |
| 1786 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1787 | void OatFile::OatDexFile::AssertAotCompiler() { |
| 1788 | CHECK(Runtime::Current()->IsAotCompiler()); |
| 1789 | } |
| 1790 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1791 | } // namespace art |