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