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