blob: 3698ddfaedf22b15750a625524a32625c527eef6 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat.h"
18
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070019#include <string.h>
Brian Carlstrome24fa612011-09-29 00:53:55 -070020
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Andreas Gampe639b2b12019-01-08 10:32:50 -080023#include "arch/instruction_set.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "arch/instruction_set_features.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/bit_utils.h"
Andreas Gampef45d61c2017-06-07 10:29:33 -070026#include "base/strlcpy.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080027
Brian Carlstrome24fa612011-09-29 00:53:55 -070028namespace art {
29
Andreas Gampe46ee31b2016-12-14 10:11:49 -080030using android::base::StringPrintf;
31
Sebastien Hertz0de11332015-05-13 12:14:05 +020032constexpr const char OatHeader::kTrueValue[];
33constexpr const char OatHeader::kFalseValue[];
Brian Carlstrome24fa612011-09-29 00:53:55 -070034
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070035static 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
48OatHeader* OatHeader::Create(InstructionSet instruction_set,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070049 const InstructionSetFeatures* instruction_set_features,
Vladimir Marko49b0f452015-12-10 13:49:19 +000050 uint32_t dex_file_count,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070051 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 Marko49b0f452015-12-10 13:49:19 +000061 dex_file_count,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070062 variable_data);
Elliott Hughesa72ec822012-03-05 17:12:22 -080063}
64
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070065OatHeader::OatHeader(InstructionSet instruction_set,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070066 const InstructionSetFeatures* instruction_set_features,
Vladimir Marko49b0f452015-12-10 13:49:19 +000067 uint32_t dex_file_count,
68 const SafeMap<std::string, std::string>* variable_data)
Vladimir Markoc10a0c62018-11-16 11:39:22 +000069 : oat_checksum_(0u),
Vladimir Marko49b0f452015-12-10 13:49:19 +000070 instruction_set_(instruction_set),
71 instruction_set_features_bitmap_(instruction_set_features->AsBitmap()),
72 dex_file_count_(dex_file_count),
Vladimir Marko0eb882b2017-05-15 13:39:18 +010073 oat_dex_files_offset_(0),
Vladimir Marko49b0f452015-12-10 13:49:19 +000074 executable_offset_(0),
Vladimir Marko7dac8642019-11-06 17:09:30 +000075 jni_dlsym_lookup_trampoline_offset_(0),
Vladimir Markofa458ac2020-02-12 14:08:07 +000076 jni_dlsym_lookup_critical_trampoline_offset_(0),
Vladimir Marko49b0f452015-12-10 13:49:19 +000077 quick_generic_jni_trampoline_offset_(0),
78 quick_imt_conflict_trampoline_offset_(0),
79 quick_resolution_trampoline_offset_(0),
Nicolas Geoffrayc39af942021-01-25 08:43:57 +000080 quick_to_interpreter_bridge_offset_(0),
81 nterp_trampoline_offset_(0) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -080082 // 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 Wailes72d7e962020-04-15 09:37:38 -070084 static_assert(decltype(magic_)().size() == kOatMagic.size(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -080085 "Oat magic and magic_ have different lengths.");
Chris Wailes72d7e962020-04-15 09:37:38 -070086 static_assert(decltype(version_)().size() == kOatVersion.size(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -080087 "Oat version and version_ have different lengths.");
88
David Srbeckye5d93b52019-02-27 15:10:52 +000089 magic_ = kOatMagic;
90 version_ = kOatVersion;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070091
Vladimir Marko33bff252017-11-01 14:35:42 +000092 CHECK_NE(instruction_set, InstructionSet::kNone);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070093
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070094 // Flatten the map. Will also update variable_size_data_size_.
95 Flatten(variable_data);
Brian Carlstrome24fa612011-09-29 00:53:55 -070096}
97
98bool OatHeader::IsValid() const {
David Srbeckye5d93b52019-02-27 15:10:52 +000099 if (magic_ != kOatMagic) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700100 return false;
101 }
David Srbeckye5d93b52019-02-27 15:10:52 +0000102 if (version_ != kOatVersion) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700103 return false;
104 }
Alex Lighta59dd802014-07-02 16:28:08 -0700105 if (!IsAligned<kPageSize>(executable_offset_)) {
106 return false;
107 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100108 if (!IsValidInstructionSet(instruction_set_)) {
109 return false;
110 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111 return true;
112}
113
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800114std::string OatHeader::GetValidationErrorMessage() const {
David Srbeckye5d93b52019-02-27 15:10:52 +0000115 if (magic_ != kOatMagic) {
Chris Wailes72d7e962020-04-15 09:37:38 -0700116 static_assert(kOatMagic.size() == 4, "kOatMagic has unexpected length");
Vladimir Marko49af4ca2019-11-27 14:35:49 +0000117 return StringPrintf("Invalid oat magic, expected 0x%02x%02x%02x%02x, got 0x%02x%02x%02x%02x.",
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800118 kOatMagic[0], kOatMagic[1], kOatMagic[2], kOatMagic[3],
119 magic_[0], magic_[1], magic_[2], magic_[3]);
120 }
David Srbeckye5d93b52019-02-27 15:10:52 +0000121 if (version_ != kOatVersion) {
Chris Wailes72d7e962020-04-15 09:37:38 -0700122 static_assert(kOatVersion.size() == 4, "kOatVersion has unexpected length");
Vladimir Marko49af4ca2019-11-27 14:35:49 +0000123 return StringPrintf("Invalid oat version, expected 0x%02x%02x%02x%02x, got 0x%02x%02x%02x%02x.",
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800124 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 Marko09d09432015-09-08 13:47:48 +0100130 if (!IsValidInstructionSet(instruction_set_)) {
131 return StringPrintf("Invalid instruction set, %d.", static_cast<int>(instruction_set_));
132 }
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800133 return "";
134}
135
David Srbeckye5d93b52019-02-27 15:10:52 +0000136// 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.
138void 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 Marko49af4ca2019-11-27 14:35:49 +0000141 LOG(FATAL) << StringPrintf("Invalid oat version, expected 0x%02x%02x%02x%02x, "
142 "got 0x%02x%02x%02x%02x.",
David Srbeckye5d93b52019-02-27 15:10:52 +0000143 expected[0], expected[1], expected[2], expected[3],
144 version[0], version[1], version[2], version[3]);
145 }
146}
147
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148const char* OatHeader::GetMagic() const {
149 CHECK(IsValid());
David Srbeckye5d93b52019-02-27 15:10:52 +0000150 return reinterpret_cast<const char*>(magic_.data());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151}
152
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153uint32_t OatHeader::GetChecksum() const {
154 CHECK(IsValid());
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000155 return oat_checksum_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700156}
157
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000158void OatHeader::SetChecksum(uint32_t oat_checksum) {
159 oat_checksum_ = oat_checksum;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700160}
161
Elliott Hughesa72ec822012-03-05 17:12:22 -0800162InstructionSet OatHeader::GetInstructionSet() const {
163 CHECK(IsValid());
164 return instruction_set_;
165}
166
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700167uint32_t OatHeader::GetInstructionSetFeaturesBitmap() const {
Dave Allison70202782013-10-22 17:52:19 -0700168 CHECK(IsValid());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700169 return instruction_set_features_bitmap_;
Dave Allison70202782013-10-22 17:52:19 -0700170}
171
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100172uint32_t OatHeader::GetOatDexFilesOffset() const {
173 DCHECK(IsValid());
174 DCHECK_GT(oat_dex_files_offset_, sizeof(OatHeader));
175 return oat_dex_files_offset_;
176}
177
178void 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 Carlstrome24fa612011-09-29 00:53:55 -0700186uint32_t OatHeader::GetExecutableOffset() const {
187 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700188 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700189 CHECK_GT(executable_offset_, sizeof(OatHeader));
190 return executable_offset_;
191}
192
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700193void 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 Hao0aba0ba2013-06-03 14:49:28 -0700200}
201
Vladimir Marko98fb0832019-03-13 16:52:11 +0000202static const void* GetTrampoline(const OatHeader& header, uint32_t offset) {
203 return (offset != 0u) ? reinterpret_cast<const uint8_t*>(&header) + offset : nullptr;
Ian Rogers468532e2013-08-05 10:56:33 -0700204}
205
Vladimir Marko7dac8642019-11-06 17:09:30 +0000206const void* OatHeader::GetJniDlsymLookupTrampoline() const {
207 return GetTrampoline(*this, GetJniDlsymLookupTrampolineOffset());
Ian Rogers468532e2013-08-05 10:56:33 -0700208}
209
Vladimir Marko7dac8642019-11-06 17:09:30 +0000210uint32_t OatHeader::GetJniDlsymLookupTrampolineOffset() const {
Ian Rogers468532e2013-08-05 10:56:33 -0700211 DCHECK(IsValid());
Vladimir Marko7dac8642019-11-06 17:09:30 +0000212 return jni_dlsym_lookup_trampoline_offset_;
Ian Rogers468532e2013-08-05 10:56:33 -0700213}
214
Vladimir Marko7dac8642019-11-06 17:09:30 +0000215void OatHeader::SetJniDlsymLookupTrampolineOffset(uint32_t offset) {
Ian Rogers468532e2013-08-05 10:56:33 -0700216 DCHECK(IsValid());
Vladimir Marko7dac8642019-11-06 17:09:30 +0000217 DCHECK_EQ(jni_dlsym_lookup_trampoline_offset_, 0U) << offset;
Ian Rogers468532e2013-08-05 10:56:33 -0700218
Vladimir Marko7dac8642019-11-06 17:09:30 +0000219 jni_dlsym_lookup_trampoline_offset_ = offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700220}
221
Vladimir Markofa458ac2020-02-12 14:08:07 +0000222const void* OatHeader::GetJniDlsymLookupCriticalTrampoline() const {
223 return GetTrampoline(*this, GetJniDlsymLookupCriticalTrampolineOffset());
224}
225
226uint32_t OatHeader::GetJniDlsymLookupCriticalTrampolineOffset() const {
227 DCHECK(IsValid());
228 return jni_dlsym_lookup_critical_trampoline_offset_;
229}
230
231void 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 Gampe2da88232014-02-27 12:26:20 -0800238const void* OatHeader::GetQuickGenericJniTrampoline() const {
Vladimir Marko98fb0832019-03-13 16:52:11 +0000239 return GetTrampoline(*this, GetQuickGenericJniTrampolineOffset());
Andreas Gampe2da88232014-02-27 12:26:20 -0800240}
241
242uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const {
243 DCHECK(IsValid());
Vladimir Marko7dac8642019-11-06 17:09:30 +0000244 CHECK_GE(quick_generic_jni_trampoline_offset_, jni_dlsym_lookup_trampoline_offset_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800245 return quick_generic_jni_trampoline_offset_;
246}
247
248void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) {
Vladimir Marko7dac8642019-11-06 17:09:30 +0000249 CHECK(offset == 0 || offset >= jni_dlsym_lookup_trampoline_offset_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800250 DCHECK(IsValid());
251 DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset;
252
253 quick_generic_jni_trampoline_offset_ = offset;
Andreas Gampe2da88232014-02-27 12:26:20 -0800254}
255
Jeff Hao88474b42013-10-23 16:24:40 -0700256const void* OatHeader::GetQuickImtConflictTrampoline() const {
Vladimir Marko98fb0832019-03-13 16:52:11 +0000257 return GetTrampoline(*this, GetQuickImtConflictTrampolineOffset());
Jeff Hao88474b42013-10-23 16:24:40 -0700258}
259
260uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const {
261 DCHECK(IsValid());
Andreas Gampe2da88232014-02-27 12:26:20 -0800262 CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_);
Jeff Hao88474b42013-10-23 16:24:40 -0700263 return quick_imt_conflict_trampoline_offset_;
264}
265
266void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800267 CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_);
Jeff Hao88474b42013-10-23 16:24:40 -0700268 DCHECK(IsValid());
269 DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset;
270
271 quick_imt_conflict_trampoline_offset_ = offset;
Jeff Hao88474b42013-10-23 16:24:40 -0700272}
273
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700274const void* OatHeader::GetQuickResolutionTrampoline() const {
Vladimir Marko98fb0832019-03-13 16:52:11 +0000275 return GetTrampoline(*this, GetQuickResolutionTrampolineOffset());
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700276}
277
278uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const {
279 DCHECK(IsValid());
Jeff Hao88474b42013-10-23 16:24:40 -0700280 CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700281 return quick_resolution_trampoline_offset_;
282}
283
284void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) {
Jeff Hao88474b42013-10-23 16:24:40 -0700285 CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700286 DCHECK(IsValid());
287 DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset;
288
289 quick_resolution_trampoline_offset_ = offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700290}
291
Ian Rogers468532e2013-08-05 10:56:33 -0700292const void* OatHeader::GetQuickToInterpreterBridge() const {
Vladimir Marko98fb0832019-03-13 16:52:11 +0000293 return GetTrampoline(*this, GetQuickToInterpreterBridgeOffset());
Ian Rogers468532e2013-08-05 10:56:33 -0700294}
295
296uint32_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
302void 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 Rogers468532e2013-08-05 10:56:33 -0700308}
309
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000310const void* OatHeader::GetNterpTrampoline() const {
311 return GetTrampoline(*this, GetNterpTrampolineOffset());
312}
313
314uint32_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
320void 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 Gampe22f8e5c2014-07-09 11:38:21 -0700328uint32_t OatHeader::GetKeyValueStoreSize() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700329 CHECK(IsValid());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700330 return key_value_store_size_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700331}
332
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700333const uint8_t* OatHeader::GetKeyValueStore() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700334 CHECK(IsValid());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700335 return key_value_store_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700336}
337
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700338const char* OatHeader::GetStoreValueByKey(const char* key) const {
Vladimir Marko37525782021-03-16 16:09:30 +0000339 std::string_view key_view(key);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700340 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 Marko37525782021-03-16 16:09:30 +0000345 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 Gampe22f8e5c2014-07-09 11:38:21 -0700349 }
Vladimir Marko37525782021-03-16 16:09:30 +0000350 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 Gampe22f8e5c2014-07-09 11:38:21 -0700363 }
364 // Not found.
365 return nullptr;
366}
367
Vladimir Marko37525782021-03-16 16:09:30 +0000368bool OatHeader::GetStoreKeyValuePairByIndex(size_t index,
369 const char** key,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700370 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 Marko37525782021-03-16 16:09:30 +0000373 size_t counter = index;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700374
Vladimir Marko37525782021-03-16 16:09:30 +0000375 while (ptr < end) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700376 // Scan for a closing zero.
Vladimir Marko37525782021-03-16 16:09:30 +0000377 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 Gampe22f8e5c2014-07-09 11:38:21 -0700381 }
Vladimir Marko37525782021-03-16 16:09:30 +0000382 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 Gampe22f8e5c2014-07-09 11:38:21 -0700398 }
399 // Not found.
400 return false;
401}
402
403size_t OatHeader::GetHeaderSize() const {
404 return sizeof(OatHeader) + key_value_store_size_;
405}
406
Sebastien Hertz0de11332015-05-13 12:14:05 +0200407bool OatHeader::IsDebuggable() const {
408 return IsKeyEnabled(OatHeader::kDebuggableKey);
409}
410
Andreas Gampef8cd8902017-01-18 16:05:01 -0800411bool OatHeader::IsConcurrentCopying() const {
412 return IsKeyEnabled(OatHeader::kConcurrentCopying);
413}
414
David Srbecky5d950762016-03-07 20:47:29 +0000415bool OatHeader::IsNativeDebuggable() const {
416 return IsKeyEnabled(OatHeader::kNativeDebuggableKey);
417}
418
Eric Holkbc89ed42020-04-29 19:59:24 +0000419bool OatHeader::RequiresImage() const {
420 return IsKeyEnabled(OatHeader::kRequiresImage);
421}
422
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000423CompilerFilter::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 Juravleb077e152016-02-18 18:47:37 +0000430}
431
432bool 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 Brazdilce4b0ba2016-01-28 15:05:49 +0000435}
436
Sebastien Hertz0de11332015-05-13 12:14:05 +0200437bool OatHeader::IsKeyEnabled(const char* key) const {
Calin Juravleb077e152016-02-18 18:47:37 +0000438 return KeyHasValue(key, kTrueValue, sizeof(kTrueValue));
Igor Murashkin46774762014-10-22 11:37:02 -0700439}
440
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700441void 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 Gampef45d61c2017-06-07 10:29:33 -0700447 strlcpy(data_ptr, it->first.c_str(), it->first.length() + 1);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700448 data_ptr += it->first.length() + 1;
Andreas Gampef45d61c2017-06-07 10:29:33 -0700449 strlcpy(data_ptr, it->second.c_str(), it->second.length() + 1);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700450 data_ptr += it->second.length() + 1;
451 }
452 }
453 key_value_store_size_ = data_ptr - reinterpret_cast<char*>(&key_value_store_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700454}
455
Brian Carlstrome24fa612011-09-29 00:53:55 -0700456} // namespace art