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