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