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