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