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