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.h" |
| 18 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 19 | #include <string.h> |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 20 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
Andreas Gampe | 639b2b1 | 2019-01-08 10:32:50 -0800 | [diff] [blame] | 23 | #include "arch/instruction_set.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 24 | #include "arch/instruction_set_features.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 26 | #include "base/strlcpy.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 27 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 30 | using android::base::StringPrintf; |
| 31 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 32 | constexpr const char OatHeader::kTrueValue[]; |
| 33 | constexpr const char OatHeader::kFalseValue[]; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 34 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 35 | static size_t ComputeOatHeaderSize(const SafeMap<std::string, std::string>* variable_data) { |
| 36 | size_t estimate = 0U; |
| 37 | if (variable_data != nullptr) { |
| 38 | SafeMap<std::string, std::string>::const_iterator it = variable_data->begin(); |
| 39 | SafeMap<std::string, std::string>::const_iterator end = variable_data->end(); |
| 40 | for ( ; it != end; ++it) { |
| 41 | estimate += it->first.length() + 1; |
| 42 | estimate += it->second.length() + 1; |
| 43 | } |
| 44 | } |
| 45 | return sizeof(OatHeader) + estimate; |
| 46 | } |
| 47 | |
| 48 | OatHeader* OatHeader::Create(InstructionSet instruction_set, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 49 | const InstructionSetFeatures* instruction_set_features, |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 50 | uint32_t dex_file_count, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 51 | const SafeMap<std::string, std::string>* variable_data) { |
| 52 | // Estimate size of optional data. |
| 53 | size_t needed_size = ComputeOatHeaderSize(variable_data); |
| 54 | |
| 55 | // Reserve enough memory. |
| 56 | void* memory = operator new (needed_size); |
| 57 | |
| 58 | // Create the OatHeader in-place. |
| 59 | return new (memory) OatHeader(instruction_set, |
| 60 | instruction_set_features, |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 61 | dex_file_count, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 62 | variable_data); |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 65 | OatHeader::OatHeader(InstructionSet instruction_set, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 66 | const InstructionSetFeatures* instruction_set_features, |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 67 | uint32_t dex_file_count, |
| 68 | const SafeMap<std::string, std::string>* variable_data) |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 69 | : oat_checksum_(0u), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 70 | instruction_set_(instruction_set), |
| 71 | instruction_set_features_bitmap_(instruction_set_features->AsBitmap()), |
| 72 | dex_file_count_(dex_file_count), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 73 | oat_dex_files_offset_(0), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 74 | executable_offset_(0), |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 75 | jni_dlsym_lookup_trampoline_offset_(0), |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 76 | jni_dlsym_lookup_critical_trampoline_offset_(0), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 77 | quick_generic_jni_trampoline_offset_(0), |
| 78 | quick_imt_conflict_trampoline_offset_(0), |
| 79 | quick_resolution_trampoline_offset_(0), |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 80 | quick_to_interpreter_bridge_offset_(0), |
| 81 | nterp_trampoline_offset_(0) { |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 82 | // Don't want asserts in header as they would be checked in each file that includes it. But the |
| 83 | // fields are private, so we check inside a method. |
Chris Wailes | 72d7e96 | 2020-04-15 09:37:38 -0700 | [diff] [blame] | 84 | static_assert(decltype(magic_)().size() == kOatMagic.size(), |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 85 | "Oat magic and magic_ have different lengths."); |
Chris Wailes | 72d7e96 | 2020-04-15 09:37:38 -0700 | [diff] [blame] | 86 | static_assert(decltype(version_)().size() == kOatVersion.size(), |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 87 | "Oat version and version_ have different lengths."); |
| 88 | |
David Srbecky | e5d93b5 | 2019-02-27 15:10:52 +0000 | [diff] [blame] | 89 | magic_ = kOatMagic; |
| 90 | version_ = kOatVersion; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 91 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 92 | CHECK_NE(instruction_set, InstructionSet::kNone); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 93 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 94 | // Flatten the map. Will also update variable_size_data_size_. |
| 95 | Flatten(variable_data); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | bool OatHeader::IsValid() const { |
David Srbecky | e5d93b5 | 2019-02-27 15:10:52 +0000 | [diff] [blame] | 99 | if (magic_ != kOatMagic) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
David Srbecky | e5d93b5 | 2019-02-27 15:10:52 +0000 | [diff] [blame] | 102 | if (version_ != kOatVersion) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 103 | return false; |
| 104 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 105 | if (!IsAligned<kPageSize>(executable_offset_)) { |
| 106 | return false; |
| 107 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 108 | if (!IsValidInstructionSet(instruction_set_)) { |
| 109 | return false; |
| 110 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 114 | std::string OatHeader::GetValidationErrorMessage() const { |
David Srbecky | e5d93b5 | 2019-02-27 15:10:52 +0000 | [diff] [blame] | 115 | if (magic_ != kOatMagic) { |
Chris Wailes | 72d7e96 | 2020-04-15 09:37:38 -0700 | [diff] [blame] | 116 | static_assert(kOatMagic.size() == 4, "kOatMagic has unexpected length"); |
Vladimir Marko | 49af4ca | 2019-11-27 14:35:49 +0000 | [diff] [blame] | 117 | return StringPrintf("Invalid oat magic, expected 0x%02x%02x%02x%02x, got 0x%02x%02x%02x%02x.", |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 118 | kOatMagic[0], kOatMagic[1], kOatMagic[2], kOatMagic[3], |
| 119 | magic_[0], magic_[1], magic_[2], magic_[3]); |
| 120 | } |
David Srbecky | e5d93b5 | 2019-02-27 15:10:52 +0000 | [diff] [blame] | 121 | if (version_ != kOatVersion) { |
Chris Wailes | 72d7e96 | 2020-04-15 09:37:38 -0700 | [diff] [blame] | 122 | static_assert(kOatVersion.size() == 4, "kOatVersion has unexpected length"); |
Vladimir Marko | 49af4ca | 2019-11-27 14:35:49 +0000 | [diff] [blame] | 123 | return StringPrintf("Invalid oat version, expected 0x%02x%02x%02x%02x, got 0x%02x%02x%02x%02x.", |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 124 | kOatVersion[0], kOatVersion[1], kOatVersion[2], kOatVersion[3], |
| 125 | version_[0], version_[1], version_[2], version_[3]); |
| 126 | } |
| 127 | if (!IsAligned<kPageSize>(executable_offset_)) { |
| 128 | return "Executable offset not page-aligned."; |
| 129 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 130 | if (!IsValidInstructionSet(instruction_set_)) { |
| 131 | return StringPrintf("Invalid instruction set, %d.", static_cast<int>(instruction_set_)); |
| 132 | } |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 133 | return ""; |
| 134 | } |
| 135 | |
David Srbecky | e5d93b5 | 2019-02-27 15:10:52 +0000 | [diff] [blame] | 136 | // Do not move this into the header. The method must be compiled in the runtime library, |
| 137 | // so that we can check that the compile-time oat version matches the version in the caller. |
| 138 | void OatHeader::CheckOatVersion(std::array<uint8_t, 4> version) { |
| 139 | constexpr std::array<uint8_t, 4> expected = kOatVersion; // Runtime oat version. |
| 140 | if (version != kOatVersion) { |
Vladimir Marko | 49af4ca | 2019-11-27 14:35:49 +0000 | [diff] [blame] | 141 | LOG(FATAL) << StringPrintf("Invalid oat version, expected 0x%02x%02x%02x%02x, " |
| 142 | "got 0x%02x%02x%02x%02x.", |
David Srbecky | e5d93b5 | 2019-02-27 15:10:52 +0000 | [diff] [blame] | 143 | expected[0], expected[1], expected[2], expected[3], |
| 144 | version[0], version[1], version[2], version[3]); |
| 145 | } |
| 146 | } |
| 147 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 148 | const char* OatHeader::GetMagic() const { |
| 149 | CHECK(IsValid()); |
David Srbecky | e5d93b5 | 2019-02-27 15:10:52 +0000 | [diff] [blame] | 150 | return reinterpret_cast<const char*>(magic_.data()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 153 | uint32_t OatHeader::GetChecksum() const { |
| 154 | CHECK(IsValid()); |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 155 | return oat_checksum_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 158 | void OatHeader::SetChecksum(uint32_t oat_checksum) { |
| 159 | oat_checksum_ = oat_checksum; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 162 | InstructionSet OatHeader::GetInstructionSet() const { |
| 163 | CHECK(IsValid()); |
| 164 | return instruction_set_; |
| 165 | } |
| 166 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 167 | uint32_t OatHeader::GetInstructionSetFeaturesBitmap() const { |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 168 | CHECK(IsValid()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 169 | return instruction_set_features_bitmap_; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 172 | uint32_t OatHeader::GetOatDexFilesOffset() const { |
| 173 | DCHECK(IsValid()); |
| 174 | DCHECK_GT(oat_dex_files_offset_, sizeof(OatHeader)); |
| 175 | return oat_dex_files_offset_; |
| 176 | } |
| 177 | |
| 178 | void OatHeader::SetOatDexFilesOffset(uint32_t oat_dex_files_offset) { |
| 179 | DCHECK_GT(oat_dex_files_offset, sizeof(OatHeader)); |
| 180 | DCHECK(IsValid()); |
| 181 | DCHECK_EQ(oat_dex_files_offset_, 0u); |
| 182 | |
| 183 | oat_dex_files_offset_ = oat_dex_files_offset; |
| 184 | } |
| 185 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 186 | uint32_t OatHeader::GetExecutableOffset() const { |
| 187 | DCHECK(IsValid()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 188 | DCHECK_ALIGNED(executable_offset_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 189 | CHECK_GT(executable_offset_, sizeof(OatHeader)); |
| 190 | return executable_offset_; |
| 191 | } |
| 192 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 193 | void OatHeader::SetExecutableOffset(uint32_t executable_offset) { |
| 194 | DCHECK_ALIGNED(executable_offset, kPageSize); |
| 195 | CHECK_GT(executable_offset, sizeof(OatHeader)); |
| 196 | DCHECK(IsValid()); |
| 197 | DCHECK_EQ(executable_offset_, 0U); |
| 198 | |
| 199 | executable_offset_ = executable_offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame] | 202 | static const void* GetTrampoline(const OatHeader& header, uint32_t offset) { |
| 203 | return (offset != 0u) ? reinterpret_cast<const uint8_t*>(&header) + offset : nullptr; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 206 | const void* OatHeader::GetJniDlsymLookupTrampoline() const { |
| 207 | return GetTrampoline(*this, GetJniDlsymLookupTrampolineOffset()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 210 | uint32_t OatHeader::GetJniDlsymLookupTrampolineOffset() const { |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 211 | DCHECK(IsValid()); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 212 | return jni_dlsym_lookup_trampoline_offset_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 215 | void OatHeader::SetJniDlsymLookupTrampolineOffset(uint32_t offset) { |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 216 | DCHECK(IsValid()); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 217 | DCHECK_EQ(jni_dlsym_lookup_trampoline_offset_, 0U) << offset; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 218 | |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 219 | jni_dlsym_lookup_trampoline_offset_ = offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 222 | const void* OatHeader::GetJniDlsymLookupCriticalTrampoline() const { |
| 223 | return GetTrampoline(*this, GetJniDlsymLookupCriticalTrampolineOffset()); |
| 224 | } |
| 225 | |
| 226 | uint32_t OatHeader::GetJniDlsymLookupCriticalTrampolineOffset() const { |
| 227 | DCHECK(IsValid()); |
| 228 | return jni_dlsym_lookup_critical_trampoline_offset_; |
| 229 | } |
| 230 | |
| 231 | void OatHeader::SetJniDlsymLookupCriticalTrampolineOffset(uint32_t offset) { |
| 232 | DCHECK(IsValid()); |
| 233 | DCHECK_EQ(jni_dlsym_lookup_critical_trampoline_offset_, 0U) << offset; |
| 234 | |
| 235 | jni_dlsym_lookup_critical_trampoline_offset_ = offset; |
| 236 | } |
| 237 | |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 238 | const void* OatHeader::GetQuickGenericJniTrampoline() const { |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame] | 239 | return GetTrampoline(*this, GetQuickGenericJniTrampolineOffset()); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const { |
| 243 | DCHECK(IsValid()); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 244 | CHECK_GE(quick_generic_jni_trampoline_offset_, jni_dlsym_lookup_trampoline_offset_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 245 | return quick_generic_jni_trampoline_offset_; |
| 246 | } |
| 247 | |
| 248 | void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) { |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 249 | CHECK(offset == 0 || offset >= jni_dlsym_lookup_trampoline_offset_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 250 | DCHECK(IsValid()); |
| 251 | DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset; |
| 252 | |
| 253 | quick_generic_jni_trampoline_offset_ = offset; |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 256 | const void* OatHeader::GetQuickImtConflictTrampoline() const { |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame] | 257 | return GetTrampoline(*this, GetQuickImtConflictTrampolineOffset()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const { |
| 261 | DCHECK(IsValid()); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 262 | CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 263 | return quick_imt_conflict_trampoline_offset_; |
| 264 | } |
| 265 | |
| 266 | void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) { |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 267 | CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 268 | DCHECK(IsValid()); |
| 269 | DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset; |
| 270 | |
| 271 | quick_imt_conflict_trampoline_offset_ = offset; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 274 | const void* OatHeader::GetQuickResolutionTrampoline() const { |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame] | 275 | return GetTrampoline(*this, GetQuickResolutionTrampolineOffset()); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const { |
| 279 | DCHECK(IsValid()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 280 | CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 281 | return quick_resolution_trampoline_offset_; |
| 282 | } |
| 283 | |
| 284 | void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 285 | CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 286 | DCHECK(IsValid()); |
| 287 | DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset; |
| 288 | |
| 289 | quick_resolution_trampoline_offset_ = offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 292 | const void* OatHeader::GetQuickToInterpreterBridge() const { |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame] | 293 | return GetTrampoline(*this, GetQuickToInterpreterBridgeOffset()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | uint32_t OatHeader::GetQuickToInterpreterBridgeOffset() const { |
| 297 | DCHECK(IsValid()); |
| 298 | CHECK_GE(quick_to_interpreter_bridge_offset_, quick_resolution_trampoline_offset_); |
| 299 | return quick_to_interpreter_bridge_offset_; |
| 300 | } |
| 301 | |
| 302 | void OatHeader::SetQuickToInterpreterBridgeOffset(uint32_t offset) { |
| 303 | CHECK(offset == 0 || offset >= quick_resolution_trampoline_offset_); |
| 304 | DCHECK(IsValid()); |
| 305 | DCHECK_EQ(quick_to_interpreter_bridge_offset_, 0U) << offset; |
| 306 | |
| 307 | quick_to_interpreter_bridge_offset_ = offset; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 310 | const void* OatHeader::GetNterpTrampoline() const { |
| 311 | return GetTrampoline(*this, GetNterpTrampolineOffset()); |
| 312 | } |
| 313 | |
| 314 | uint32_t OatHeader::GetNterpTrampolineOffset() const { |
| 315 | DCHECK(IsValid()); |
| 316 | CHECK_GE(nterp_trampoline_offset_, quick_to_interpreter_bridge_offset_); |
| 317 | return nterp_trampoline_offset_; |
| 318 | } |
| 319 | |
| 320 | void OatHeader::SetNterpTrampolineOffset(uint32_t offset) { |
| 321 | CHECK(offset == 0 || offset >= quick_to_interpreter_bridge_offset_); |
| 322 | DCHECK(IsValid()); |
| 323 | DCHECK_EQ(nterp_trampoline_offset_, 0U) << offset; |
| 324 | |
| 325 | nterp_trampoline_offset_ = offset; |
| 326 | } |
| 327 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 328 | uint32_t OatHeader::GetKeyValueStoreSize() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 329 | CHECK(IsValid()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 330 | return key_value_store_size_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 333 | const uint8_t* OatHeader::GetKeyValueStore() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 334 | CHECK(IsValid()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 335 | return key_value_store_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 338 | const char* OatHeader::GetStoreValueByKey(const char* key) const { |
Vladimir Marko | 3752578 | 2021-03-16 16:09:30 +0000 | [diff] [blame] | 339 | std::string_view key_view(key); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 340 | const char* ptr = reinterpret_cast<const char*>(&key_value_store_); |
| 341 | const char* end = ptr + key_value_store_size_; |
| 342 | |
| 343 | while (ptr < end) { |
| 344 | // Scan for a closing zero. |
Vladimir Marko | 3752578 | 2021-03-16 16:09:30 +0000 | [diff] [blame] | 345 | const char* str_end = reinterpret_cast<const char*>(memchr(ptr, 0, end - ptr)); |
| 346 | if (UNLIKELY(str_end == nullptr)) { |
| 347 | LOG(WARNING) << "OatHeader: Unterminated key in key value store."; |
| 348 | return nullptr; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 349 | } |
Vladimir Marko | 3752578 | 2021-03-16 16:09:30 +0000 | [diff] [blame] | 350 | const char* value_start = str_end + 1; |
| 351 | const char* value_end = |
| 352 | reinterpret_cast<const char*>(memchr(value_start, 0, end - value_start)); |
| 353 | if (UNLIKELY(value_end == nullptr)) { |
| 354 | LOG(WARNING) << "OatHeader: Unterminated value in key value store."; |
| 355 | return nullptr; |
| 356 | } |
| 357 | if (key_view == std::string_view(ptr, str_end - ptr)) { |
| 358 | // Same as key. |
| 359 | return value_start; |
| 360 | } |
| 361 | // Different from key. Advance over the value. |
| 362 | ptr = value_end + 1; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 363 | } |
| 364 | // Not found. |
| 365 | return nullptr; |
| 366 | } |
| 367 | |
Vladimir Marko | 3752578 | 2021-03-16 16:09:30 +0000 | [diff] [blame] | 368 | bool OatHeader::GetStoreKeyValuePairByIndex(size_t index, |
| 369 | const char** key, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 370 | const char** value) const { |
| 371 | const char* ptr = reinterpret_cast<const char*>(&key_value_store_); |
| 372 | const char* end = ptr + key_value_store_size_; |
Vladimir Marko | 3752578 | 2021-03-16 16:09:30 +0000 | [diff] [blame] | 373 | size_t counter = index; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 374 | |
Vladimir Marko | 3752578 | 2021-03-16 16:09:30 +0000 | [diff] [blame] | 375 | while (ptr < end) { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 376 | // Scan for a closing zero. |
Vladimir Marko | 3752578 | 2021-03-16 16:09:30 +0000 | [diff] [blame] | 377 | const char* str_end = reinterpret_cast<const char*>(memchr(ptr, 0, end - ptr)); |
| 378 | if (UNLIKELY(str_end == nullptr)) { |
| 379 | LOG(WARNING) << "OatHeader: Unterminated key in key value store."; |
| 380 | return false; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 381 | } |
Vladimir Marko | 3752578 | 2021-03-16 16:09:30 +0000 | [diff] [blame] | 382 | const char* value_start = str_end + 1; |
| 383 | const char* value_end = |
| 384 | reinterpret_cast<const char*>(memchr(value_start, 0, end - value_start)); |
| 385 | if (UNLIKELY(value_end == nullptr)) { |
| 386 | LOG(WARNING) << "OatHeader: Unterminated value in key value store."; |
| 387 | return false; |
| 388 | } |
| 389 | if (counter == 0) { |
| 390 | *key = ptr; |
| 391 | *value = value_start; |
| 392 | return true; |
| 393 | } else { |
| 394 | --counter; |
| 395 | } |
| 396 | // Advance over the value. |
| 397 | ptr = value_end + 1; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 398 | } |
| 399 | // Not found. |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | size_t OatHeader::GetHeaderSize() const { |
| 404 | return sizeof(OatHeader) + key_value_store_size_; |
| 405 | } |
| 406 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 407 | bool OatHeader::IsDebuggable() const { |
| 408 | return IsKeyEnabled(OatHeader::kDebuggableKey); |
| 409 | } |
| 410 | |
Andreas Gampe | f8cd890 | 2017-01-18 16:05:01 -0800 | [diff] [blame] | 411 | bool OatHeader::IsConcurrentCopying() const { |
| 412 | return IsKeyEnabled(OatHeader::kConcurrentCopying); |
| 413 | } |
| 414 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 415 | bool OatHeader::IsNativeDebuggable() const { |
| 416 | return IsKeyEnabled(OatHeader::kNativeDebuggableKey); |
| 417 | } |
| 418 | |
Eric Holk | bc89ed4 | 2020-04-29 19:59:24 +0000 | [diff] [blame] | 419 | bool OatHeader::RequiresImage() const { |
| 420 | return IsKeyEnabled(OatHeader::kRequiresImage); |
| 421 | } |
| 422 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 423 | CompilerFilter::Filter OatHeader::GetCompilerFilter() const { |
| 424 | CompilerFilter::Filter filter; |
| 425 | const char* key_value = GetStoreValueByKey(kCompilerFilter); |
| 426 | CHECK(key_value != nullptr) << "compiler-filter not found in oat header"; |
| 427 | CHECK(CompilerFilter::ParseCompilerFilter(key_value, &filter)) |
| 428 | << "Invalid compiler-filter in oat header: " << key_value; |
| 429 | return filter; |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | bool OatHeader::KeyHasValue(const char* key, const char* value, size_t value_size) const { |
| 433 | const char* key_value = GetStoreValueByKey(key); |
| 434 | return (key_value != nullptr && strncmp(key_value, value, value_size) == 0); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 437 | bool OatHeader::IsKeyEnabled(const char* key) const { |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 438 | return KeyHasValue(key, kTrueValue, sizeof(kTrueValue)); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 441 | void OatHeader::Flatten(const SafeMap<std::string, std::string>* key_value_store) { |
| 442 | char* data_ptr = reinterpret_cast<char*>(&key_value_store_); |
| 443 | if (key_value_store != nullptr) { |
| 444 | SafeMap<std::string, std::string>::const_iterator it = key_value_store->begin(); |
| 445 | SafeMap<std::string, std::string>::const_iterator end = key_value_store->end(); |
| 446 | for ( ; it != end; ++it) { |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 447 | strlcpy(data_ptr, it->first.c_str(), it->first.length() + 1); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 448 | data_ptr += it->first.length() + 1; |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 449 | strlcpy(data_ptr, it->second.c_str(), it->second.length() + 1); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 450 | data_ptr += it->second.length() + 1; |
| 451 | } |
| 452 | } |
| 453 | key_value_store_size_ = data_ptr - reinterpret_cast<char*>(&key_value_store_); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 456 | } // namespace art |