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