Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 17 | #include "standard_dex_file.h" |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 18 | |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 19 | #include "base/casts.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 20 | #include "base/leb128.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 21 | #include "code_item_accessors-inl.h" |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 22 | #include "dex_file-inl.h" |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 23 | |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 26 | const uint8_t StandardDexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' }; |
| 27 | const uint8_t StandardDexFile::kDexMagicVersions[StandardDexFile::kNumDexVersions] |
| 28 | [StandardDexFile::kDexVersionLen] = { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 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 verion 039: Beyond Android "O". |
| 36 | {'0', '3', '9', '\0'}, |
| 37 | }; |
| 38 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 39 | void StandardDexFile::WriteMagic(uint8_t* magic) { |
| 40 | std::copy_n(kDexMagic, kDexMagicSize, magic); |
| 41 | } |
| 42 | |
| 43 | void StandardDexFile::WriteCurrentVersion(uint8_t* magic) { |
| 44 | std::copy_n(kDexMagicVersions[StandardDexFile::kDexVersionLen - 1], |
| 45 | kDexVersionLen, |
| 46 | magic + kDexMagicSize); |
| 47 | } |
| 48 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 49 | bool StandardDexFile::IsMagicValid(const uint8_t* magic) { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 50 | return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0); |
| 51 | } |
| 52 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 53 | bool StandardDexFile::IsVersionValid(const uint8_t* magic) { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 54 | const uint8_t* version = &magic[sizeof(kDexMagic)]; |
| 55 | for (uint32_t i = 0; i < kNumDexVersions; i++) { |
| 56 | if (memcmp(version, kDexMagicVersions[i], kDexVersionLen) == 0) { |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 63 | bool StandardDexFile::IsMagicValid() const { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 64 | return IsMagicValid(header_->magic_); |
| 65 | } |
| 66 | |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 67 | bool StandardDexFile::IsVersionValid() const { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 68 | return IsVersionValid(header_->magic_); |
| 69 | } |
| 70 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 71 | bool StandardDexFile::SupportsDefaultMethods() const { |
| 72 | return GetDexVersion() >= DexFile::kDefaultMethodsVersion; |
| 73 | } |
| 74 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 75 | uint32_t StandardDexFile::GetCodeItemSize(const dex::CodeItem& item) const { |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 76 | DCHECK(IsInDataSection(&item)); |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 77 | return reinterpret_cast<uintptr_t>(CodeItemDataAccessor(*this, &item).CodeItemDataEnd()) - |
| 78 | reinterpret_cast<uintptr_t>(&item); |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 81 | } // namespace art |