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 */
16
17 #include "oat.h"
18
19 #include <string.h>
20
21 #include "android-base/stringprintf.h"
22
23 #include "arch/instruction_set.h"
24 #include "arch/instruction_set_features.h"
25 #include "base/bit_utils.h"
26 #include "base/strlcpy.h"
27
28 namespace art {
29
30 using android::base::StringPrintf;
31
32 constexpr const char OatHeader::kTrueValue[];
33 constexpr const char OatHeader::kFalseValue[];
34
ComputeOatHeaderSize(const SafeMap<std::string,std::string> * variable_data)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
Create(InstructionSet instruction_set,const InstructionSetFeatures * instruction_set_features,uint32_t dex_file_count,const SafeMap<std::string,std::string> * variable_data)48 OatHeader* OatHeader::Create(InstructionSet instruction_set,
49 const InstructionSetFeatures* instruction_set_features,
50 uint32_t dex_file_count,
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,
61 dex_file_count,
62 variable_data);
63 }
64
OatHeader(InstructionSet instruction_set,const InstructionSetFeatures * instruction_set_features,uint32_t dex_file_count,const SafeMap<std::string,std::string> * variable_data)65 OatHeader::OatHeader(InstructionSet instruction_set,
66 const InstructionSetFeatures* instruction_set_features,
67 uint32_t dex_file_count,
68 const SafeMap<std::string, std::string>* variable_data)
69 : oat_checksum_(0u),
70 instruction_set_(instruction_set),
71 instruction_set_features_bitmap_(instruction_set_features->AsBitmap()),
72 dex_file_count_(dex_file_count),
73 oat_dex_files_offset_(0),
74 bcp_bss_info_offset_(0),
75 executable_offset_(0),
76 jni_dlsym_lookup_trampoline_offset_(0),
77 jni_dlsym_lookup_critical_trampoline_offset_(0),
78 quick_generic_jni_trampoline_offset_(0),
79 quick_imt_conflict_trampoline_offset_(0),
80 quick_resolution_trampoline_offset_(0),
81 quick_to_interpreter_bridge_offset_(0),
82 nterp_trampoline_offset_(0) {
83 // Don't want asserts in header as they would be checked in each file that includes it. But the
84 // fields are private, so we check inside a method.
85 static_assert(decltype(magic_)().size() == kOatMagic.size(),
86 "Oat magic and magic_ have different lengths.");
87 static_assert(decltype(version_)().size() == kOatVersion.size(),
88 "Oat version and version_ have different lengths.");
89
90 magic_ = kOatMagic;
91 version_ = kOatVersion;
92
93 CHECK_NE(instruction_set, InstructionSet::kNone);
94
95 // Flatten the map. Will also update variable_size_data_size_.
96 Flatten(variable_data);
97 }
98
IsValid() const99 bool OatHeader::IsValid() const {
100 if (magic_ != kOatMagic) {
101 return false;
102 }
103 if (version_ != kOatVersion) {
104 return false;
105 }
106 if (!IsAligned<kPageSize>(executable_offset_)) {
107 return false;
108 }
109 if (!IsValidInstructionSet(instruction_set_)) {
110 return false;
111 }
112 return true;
113 }
114
GetValidationErrorMessage() const115 std::string OatHeader::GetValidationErrorMessage() const {
116 if (magic_ != kOatMagic) {
117 static_assert(kOatMagic.size() == 4, "kOatMagic has unexpected length");
118 return StringPrintf("Invalid oat magic, expected 0x%02x%02x%02x%02x, got 0x%02x%02x%02x%02x.",
119 kOatMagic[0], kOatMagic[1], kOatMagic[2], kOatMagic[3],
120 magic_[0], magic_[1], magic_[2], magic_[3]);
121 }
122 if (version_ != kOatVersion) {
123 static_assert(kOatVersion.size() == 4, "kOatVersion has unexpected length");
124 return StringPrintf("Invalid oat version, expected 0x%02x%02x%02x%02x, got 0x%02x%02x%02x%02x.",
125 kOatVersion[0], kOatVersion[1], kOatVersion[2], kOatVersion[3],
126 version_[0], version_[1], version_[2], version_[3]);
127 }
128 if (!IsAligned<kPageSize>(executable_offset_)) {
129 return "Executable offset not page-aligned.";
130 }
131 if (!IsValidInstructionSet(instruction_set_)) {
132 return StringPrintf("Invalid instruction set, %d.", static_cast<int>(instruction_set_));
133 }
134 return "";
135 }
136
137 // Do not move this into the header. The method must be compiled in the runtime library,
138 // so that we can check that the compile-time oat version matches the version in the caller.
CheckOatVersion(std::array<uint8_t,4> version)139 void OatHeader::CheckOatVersion(std::array<uint8_t, 4> version) {
140 constexpr std::array<uint8_t, 4> expected = kOatVersion; // Runtime oat version.
141 if (version != kOatVersion) {
142 LOG(FATAL) << StringPrintf("Invalid oat version, expected 0x%02x%02x%02x%02x, "
143 "got 0x%02x%02x%02x%02x.",
144 expected[0], expected[1], expected[2], expected[3],
145 version[0], version[1], version[2], version[3]);
146 }
147 }
148
GetMagic() const149 const char* OatHeader::GetMagic() const {
150 CHECK(IsValid());
151 return reinterpret_cast<const char*>(magic_.data());
152 }
153
GetChecksum() const154 uint32_t OatHeader::GetChecksum() const {
155 CHECK(IsValid());
156 return oat_checksum_;
157 }
158
SetChecksum(uint32_t oat_checksum)159 void OatHeader::SetChecksum(uint32_t oat_checksum) {
160 oat_checksum_ = oat_checksum;
161 }
162
GetInstructionSet() const163 InstructionSet OatHeader::GetInstructionSet() const {
164 CHECK(IsValid());
165 return instruction_set_;
166 }
167
GetInstructionSetFeaturesBitmap() const168 uint32_t OatHeader::GetInstructionSetFeaturesBitmap() const {
169 CHECK(IsValid());
170 return instruction_set_features_bitmap_;
171 }
172
GetOatDexFilesOffset() const173 uint32_t OatHeader::GetOatDexFilesOffset() const {
174 DCHECK(IsValid());
175 DCHECK_GT(oat_dex_files_offset_, sizeof(OatHeader));
176 return oat_dex_files_offset_;
177 }
178
SetOatDexFilesOffset(uint32_t oat_dex_files_offset)179 void OatHeader::SetOatDexFilesOffset(uint32_t oat_dex_files_offset) {
180 DCHECK_GT(oat_dex_files_offset, sizeof(OatHeader));
181 DCHECK(IsValid());
182 DCHECK_EQ(oat_dex_files_offset_, 0u);
183
184 oat_dex_files_offset_ = oat_dex_files_offset;
185 }
186
GetBcpBssInfoOffset() const187 uint32_t OatHeader::GetBcpBssInfoOffset() const {
188 DCHECK(IsValid());
189 DCHECK(bcp_bss_info_offset_ == 0u || bcp_bss_info_offset_ > sizeof(OatHeader))
190 << "bcp_bss_info_offset_: " << bcp_bss_info_offset_
191 << "sizeof(OatHeader): " << sizeof(OatHeader);
192 return bcp_bss_info_offset_;
193 }
194
SetBcpBssInfoOffset(uint32_t bcp_info_offset)195 void OatHeader::SetBcpBssInfoOffset(uint32_t bcp_info_offset) {
196 DCHECK_GT(bcp_info_offset, sizeof(OatHeader));
197 DCHECK(IsValid());
198 DCHECK_EQ(bcp_bss_info_offset_, 0u);
199
200 bcp_bss_info_offset_ = bcp_info_offset;
201 }
202
GetExecutableOffset() const203 uint32_t OatHeader::GetExecutableOffset() const {
204 DCHECK(IsValid());
205 DCHECK_ALIGNED(executable_offset_, kPageSize);
206 CHECK_GT(executable_offset_, sizeof(OatHeader));
207 return executable_offset_;
208 }
209
SetExecutableOffset(uint32_t executable_offset)210 void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
211 DCHECK_ALIGNED(executable_offset, kPageSize);
212 CHECK_GT(executable_offset, sizeof(OatHeader));
213 DCHECK(IsValid());
214 DCHECK_EQ(executable_offset_, 0U);
215
216 executable_offset_ = executable_offset;
217 }
218
GetTrampoline(const OatHeader & header,uint32_t offset)219 static const void* GetTrampoline(const OatHeader& header, uint32_t offset) {
220 return (offset != 0u) ? reinterpret_cast<const uint8_t*>(&header) + offset : nullptr;
221 }
222
GetJniDlsymLookupTrampoline() const223 const void* OatHeader::GetJniDlsymLookupTrampoline() const {
224 return GetTrampoline(*this, GetJniDlsymLookupTrampolineOffset());
225 }
226
GetJniDlsymLookupTrampolineOffset() const227 uint32_t OatHeader::GetJniDlsymLookupTrampolineOffset() const {
228 DCHECK(IsValid());
229 return jni_dlsym_lookup_trampoline_offset_;
230 }
231
SetJniDlsymLookupTrampolineOffset(uint32_t offset)232 void OatHeader::SetJniDlsymLookupTrampolineOffset(uint32_t offset) {
233 DCHECK(IsValid());
234 DCHECK_EQ(jni_dlsym_lookup_trampoline_offset_, 0U) << offset;
235
236 jni_dlsym_lookup_trampoline_offset_ = offset;
237 }
238
GetJniDlsymLookupCriticalTrampoline() const239 const void* OatHeader::GetJniDlsymLookupCriticalTrampoline() const {
240 return GetTrampoline(*this, GetJniDlsymLookupCriticalTrampolineOffset());
241 }
242
GetJniDlsymLookupCriticalTrampolineOffset() const243 uint32_t OatHeader::GetJniDlsymLookupCriticalTrampolineOffset() const {
244 DCHECK(IsValid());
245 return jni_dlsym_lookup_critical_trampoline_offset_;
246 }
247
SetJniDlsymLookupCriticalTrampolineOffset(uint32_t offset)248 void OatHeader::SetJniDlsymLookupCriticalTrampolineOffset(uint32_t offset) {
249 DCHECK(IsValid());
250 DCHECK_EQ(jni_dlsym_lookup_critical_trampoline_offset_, 0U) << offset;
251
252 jni_dlsym_lookup_critical_trampoline_offset_ = offset;
253 }
254
GetQuickGenericJniTrampoline() const255 const void* OatHeader::GetQuickGenericJniTrampoline() const {
256 return GetTrampoline(*this, GetQuickGenericJniTrampolineOffset());
257 }
258
GetQuickGenericJniTrampolineOffset() const259 uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const {
260 DCHECK(IsValid());
261 CHECK_GE(quick_generic_jni_trampoline_offset_, jni_dlsym_lookup_trampoline_offset_);
262 return quick_generic_jni_trampoline_offset_;
263 }
264
SetQuickGenericJniTrampolineOffset(uint32_t offset)265 void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) {
266 CHECK(offset == 0 || offset >= jni_dlsym_lookup_trampoline_offset_);
267 DCHECK(IsValid());
268 DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset;
269
270 quick_generic_jni_trampoline_offset_ = offset;
271 }
272
GetQuickImtConflictTrampoline() const273 const void* OatHeader::GetQuickImtConflictTrampoline() const {
274 return GetTrampoline(*this, GetQuickImtConflictTrampolineOffset());
275 }
276
GetQuickImtConflictTrampolineOffset() const277 uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const {
278 DCHECK(IsValid());
279 CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_);
280 return quick_imt_conflict_trampoline_offset_;
281 }
282
SetQuickImtConflictTrampolineOffset(uint32_t offset)283 void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) {
284 CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_);
285 DCHECK(IsValid());
286 DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset;
287
288 quick_imt_conflict_trampoline_offset_ = offset;
289 }
290
GetQuickResolutionTrampoline() const291 const void* OatHeader::GetQuickResolutionTrampoline() const {
292 return GetTrampoline(*this, GetQuickResolutionTrampolineOffset());
293 }
294
GetQuickResolutionTrampolineOffset() const295 uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const {
296 DCHECK(IsValid());
297 CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_);
298 return quick_resolution_trampoline_offset_;
299 }
300
SetQuickResolutionTrampolineOffset(uint32_t offset)301 void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) {
302 CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_);
303 DCHECK(IsValid());
304 DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset;
305
306 quick_resolution_trampoline_offset_ = offset;
307 }
308
GetQuickToInterpreterBridge() const309 const void* OatHeader::GetQuickToInterpreterBridge() const {
310 return GetTrampoline(*this, GetQuickToInterpreterBridgeOffset());
311 }
312
GetQuickToInterpreterBridgeOffset() const313 uint32_t OatHeader::GetQuickToInterpreterBridgeOffset() const {
314 DCHECK(IsValid());
315 CHECK_GE(quick_to_interpreter_bridge_offset_, quick_resolution_trampoline_offset_);
316 return quick_to_interpreter_bridge_offset_;
317 }
318
SetQuickToInterpreterBridgeOffset(uint32_t offset)319 void OatHeader::SetQuickToInterpreterBridgeOffset(uint32_t offset) {
320 CHECK(offset == 0 || offset >= quick_resolution_trampoline_offset_);
321 DCHECK(IsValid());
322 DCHECK_EQ(quick_to_interpreter_bridge_offset_, 0U) << offset;
323
324 quick_to_interpreter_bridge_offset_ = offset;
325 }
326
GetNterpTrampoline() const327 const void* OatHeader::GetNterpTrampoline() const {
328 return GetTrampoline(*this, GetNterpTrampolineOffset());
329 }
330
GetNterpTrampolineOffset() const331 uint32_t OatHeader::GetNterpTrampolineOffset() const {
332 DCHECK(IsValid());
333 CHECK_GE(nterp_trampoline_offset_, quick_to_interpreter_bridge_offset_);
334 return nterp_trampoline_offset_;
335 }
336
SetNterpTrampolineOffset(uint32_t offset)337 void OatHeader::SetNterpTrampolineOffset(uint32_t offset) {
338 CHECK(offset == 0 || offset >= quick_to_interpreter_bridge_offset_);
339 DCHECK(IsValid());
340 DCHECK_EQ(nterp_trampoline_offset_, 0U) << offset;
341
342 nterp_trampoline_offset_ = offset;
343 }
344
GetKeyValueStoreSize() const345 uint32_t OatHeader::GetKeyValueStoreSize() const {
346 CHECK(IsValid());
347 return key_value_store_size_;
348 }
349
GetKeyValueStore() const350 const uint8_t* OatHeader::GetKeyValueStore() const {
351 CHECK(IsValid());
352 return key_value_store_;
353 }
354
GetStoreValueByKey(const char * key) const355 const char* OatHeader::GetStoreValueByKey(const char* key) const {
356 std::string_view key_view(key);
357 const char* ptr = reinterpret_cast<const char*>(&key_value_store_);
358 const char* end = ptr + key_value_store_size_;
359
360 while (ptr < end) {
361 // Scan for a closing zero.
362 const char* str_end = reinterpret_cast<const char*>(memchr(ptr, 0, end - ptr));
363 if (UNLIKELY(str_end == nullptr)) {
364 LOG(WARNING) << "OatHeader: Unterminated key in key value store.";
365 return nullptr;
366 }
367 const char* value_start = str_end + 1;
368 const char* value_end =
369 reinterpret_cast<const char*>(memchr(value_start, 0, end - value_start));
370 if (UNLIKELY(value_end == nullptr)) {
371 LOG(WARNING) << "OatHeader: Unterminated value in key value store.";
372 return nullptr;
373 }
374 if (key_view == std::string_view(ptr, str_end - ptr)) {
375 // Same as key.
376 return value_start;
377 }
378 // Different from key. Advance over the value.
379 ptr = value_end + 1;
380 }
381 // Not found.
382 return nullptr;
383 }
384
GetStoreKeyValuePairByIndex(size_t index,const char ** key,const char ** value) const385 bool OatHeader::GetStoreKeyValuePairByIndex(size_t index,
386 const char** key,
387 const char** value) const {
388 const char* ptr = reinterpret_cast<const char*>(&key_value_store_);
389 const char* end = ptr + key_value_store_size_;
390 size_t counter = index;
391
392 while (ptr < end) {
393 // Scan for a closing zero.
394 const char* str_end = reinterpret_cast<const char*>(memchr(ptr, 0, end - ptr));
395 if (UNLIKELY(str_end == nullptr)) {
396 LOG(WARNING) << "OatHeader: Unterminated key in key value store.";
397 return false;
398 }
399 const char* value_start = str_end + 1;
400 const char* value_end =
401 reinterpret_cast<const char*>(memchr(value_start, 0, end - value_start));
402 if (UNLIKELY(value_end == nullptr)) {
403 LOG(WARNING) << "OatHeader: Unterminated value in key value store.";
404 return false;
405 }
406 if (counter == 0) {
407 *key = ptr;
408 *value = value_start;
409 return true;
410 } else {
411 --counter;
412 }
413 // Advance over the value.
414 ptr = value_end + 1;
415 }
416 // Not found.
417 return false;
418 }
419
GetHeaderSize() const420 size_t OatHeader::GetHeaderSize() const {
421 return sizeof(OatHeader) + key_value_store_size_;
422 }
423
IsDebuggable() const424 bool OatHeader::IsDebuggable() const {
425 return IsKeyEnabled(OatHeader::kDebuggableKey);
426 }
427
IsConcurrentCopying() const428 bool OatHeader::IsConcurrentCopying() const {
429 return IsKeyEnabled(OatHeader::kConcurrentCopying);
430 }
431
IsNativeDebuggable() const432 bool OatHeader::IsNativeDebuggable() const {
433 return IsKeyEnabled(OatHeader::kNativeDebuggableKey);
434 }
435
RequiresImage() const436 bool OatHeader::RequiresImage() const {
437 return IsKeyEnabled(OatHeader::kRequiresImage);
438 }
439
GetCompilerFilter() const440 CompilerFilter::Filter OatHeader::GetCompilerFilter() const {
441 CompilerFilter::Filter filter;
442 const char* key_value = GetStoreValueByKey(kCompilerFilter);
443 CHECK(key_value != nullptr) << "compiler-filter not found in oat header";
444 CHECK(CompilerFilter::ParseCompilerFilter(key_value, &filter))
445 << "Invalid compiler-filter in oat header: " << key_value;
446 return filter;
447 }
448
KeyHasValue(const char * key,const char * value,size_t value_size) const449 bool OatHeader::KeyHasValue(const char* key, const char* value, size_t value_size) const {
450 const char* key_value = GetStoreValueByKey(key);
451 return (key_value != nullptr && strncmp(key_value, value, value_size) == 0);
452 }
453
IsKeyEnabled(const char * key) const454 bool OatHeader::IsKeyEnabled(const char* key) const {
455 return KeyHasValue(key, kTrueValue, sizeof(kTrueValue));
456 }
457
Flatten(const SafeMap<std::string,std::string> * key_value_store)458 void OatHeader::Flatten(const SafeMap<std::string, std::string>* key_value_store) {
459 char* data_ptr = reinterpret_cast<char*>(&key_value_store_);
460 if (key_value_store != nullptr) {
461 SafeMap<std::string, std::string>::const_iterator it = key_value_store->begin();
462 SafeMap<std::string, std::string>::const_iterator end = key_value_store->end();
463 for ( ; it != end; ++it) {
464 strlcpy(data_ptr, it->first.c_str(), it->first.length() + 1);
465 data_ptr += it->first.length() + 1;
466 strlcpy(data_ptr, it->second.c_str(), it->second.length() + 1);
467 data_ptr += it->second.length() + 1;
468 }
469 }
470 key_value_store_size_ = data_ptr - reinterpret_cast<char*>(&key_value_store_);
471 }
472
473 } // namespace art
474