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[StandardDexFile::kNumDexVersions]
28                                                 [StandardDexFile::kDexVersionLen] = {
29   {'0', '3', '5', '\0'},
30   // Dex version 036 skipped because of an old dalvik bug on some versions of android where dex
31   // 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: beyond Android "10" (previously known as Android "Q").
38   {'0', '4', '0', '\0'},
39 };
40 
WriteMagic(uint8_t * magic)41 void StandardDexFile::WriteMagic(uint8_t* magic) {
42   std::copy_n(kDexMagic, kDexMagicSize, magic);
43 }
44 
WriteCurrentVersion(uint8_t * magic)45 void StandardDexFile::WriteCurrentVersion(uint8_t* magic) {
46   std::copy_n(kDexMagicVersions[StandardDexFile::kDexVersionLen - 1],
47               kDexVersionLen,
48               magic + kDexMagicSize);
49 }
50 
51 
WriteVersionBeforeDefaultMethods(uint8_t * magic)52 void StandardDexFile::WriteVersionBeforeDefaultMethods(uint8_t* magic) {
53   std::copy_n(kDexMagicVersions[0u], kDexVersionLen, magic + kDexMagicSize);
54 }
55 
IsMagicValid(const uint8_t * magic)56 bool StandardDexFile::IsMagicValid(const uint8_t* magic) {
57   return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
58 }
59 
IsVersionValid(const uint8_t * magic)60 bool StandardDexFile::IsVersionValid(const uint8_t* magic) {
61   const uint8_t* version = &magic[sizeof(kDexMagic)];
62   for (uint32_t i = 0; i < kNumDexVersions; i++) {
63     if (memcmp(version, kDexMagicVersions[i], kDexVersionLen) == 0) {
64       return true;
65     }
66   }
67   return false;
68 }
69 
IsMagicValid() const70 bool StandardDexFile::IsMagicValid() const {
71   return IsMagicValid(header_->magic_);
72 }
73 
IsVersionValid() const74 bool StandardDexFile::IsVersionValid() const {
75   return IsVersionValid(header_->magic_);
76 }
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