1 /*
2 * Copyright (C) 2017 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 "standard_dex_file.h"
18
19 #include "base/casts.h"
20 #include "base/leb128.h"
21 #include "code_item_accessors-inl.h"
22 #include "dex_file-inl.h"
23
24 namespace art {
25
26 const uint8_t StandardDexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
27 const uint8_t StandardDexFile::kDexMagicVersions
28 [StandardDexFile::kNumDexVersions][StandardDexFile::kDexVersionLen] = {
29 {'0', '3', '5', '\0'},
30 // Dex version 036 skipped because of an old dalvik bug on some versions of android where
31 // dex files with that version number would erroneously be accepted and run.
32 {'0', '3', '7', '\0'},
33 // Dex version 038: Android "O" and beyond.
34 {'0', '3', '8', '\0'},
35 // Dex version 039: Android "P" and beyond.
36 {'0', '3', '9', '\0'},
37 // Dex version 040: Android "Q" and beyond (aka Android 10).
38 {'0', '4', '0', '\0'},
39 // Dex version 041: Android "V" and beyond (aka Android 15).
40 {'0', '4', '1', '\0'},
41 };
42
WriteMagic(uint8_t * magic)43 void StandardDexFile::WriteMagic(uint8_t* magic) {
44 std::copy_n(kDexMagic, kDexMagicSize, magic);
45 }
46
WriteCurrentVersion(uint8_t * magic)47 void StandardDexFile::WriteCurrentVersion(uint8_t* magic) {
48 std::copy_n(kDexMagicVersions[StandardDexFile::kDexVersionLen - 1],
49 kDexVersionLen,
50 magic + kDexMagicSize);
51 }
52
53
WriteVersionBeforeDefaultMethods(uint8_t * magic)54 void StandardDexFile::WriteVersionBeforeDefaultMethods(uint8_t* magic) {
55 std::copy_n(kDexMagicVersions[0u], kDexVersionLen, magic + kDexMagicSize);
56 }
57
IsMagicValid(const uint8_t * magic)58 bool StandardDexFile::IsMagicValid(const uint8_t* magic) {
59 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
60 }
61
IsVersionValid(const uint8_t * magic)62 bool StandardDexFile::IsVersionValid(const uint8_t* magic) {
63 const uint8_t* version = &magic[sizeof(kDexMagic)];
64 for (uint32_t i = 0; i < kNumDexVersions; i++) {
65 if (memcmp(version, kDexMagicVersions[i], kDexVersionLen) == 0) {
66 return true;
67 }
68 }
69 return false;
70 }
71
IsMagicValid() const72 bool StandardDexFile::IsMagicValid() const {
73 return IsMagicValid(header_->magic_);
74 }
75
IsVersionValid() const76 bool StandardDexFile::IsVersionValid() const { return IsVersionValid(header_->magic_.data()); }
77
SupportsDefaultMethods() const78 bool StandardDexFile::SupportsDefaultMethods() const {
79 return GetDexVersion() >= DexFile::kDefaultMethodsVersion;
80 }
81
GetCodeItemSize(const dex::CodeItem & item) const82 uint32_t StandardDexFile::GetCodeItemSize(const dex::CodeItem& item) const {
83 DCHECK(IsInDataSection(&item));
84 return reinterpret_cast<uintptr_t>(CodeItemDataAccessor(*this, &item).CodeItemDataEnd()) -
85 reinterpret_cast<uintptr_t>(&item);
86 }
87
88 } // namespace art
89