blob: 8bac44e02eeff9e77682f27e90c69e5782ef943c [file] [log] [blame]
Mathieu Chartier7b074bf2017-09-25 16:22:36 -07001/*
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 Chartier292567e2017-10-12 13:24:38 -070017#include "standard_dex_file.h"
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070018
Mathieu Chartier6238c832018-01-04 09:55:13 -080019#include "base/casts.h"
David Sehr67bf42e2018-02-26 16:43:04 -080020#include "base/leb128.h"
David Sehr0225f8e2018-01-31 08:52:24 +000021#include "code_item_accessors-inl.h"
Mathieu Chartier6238c832018-01-04 09:55:13 -080022#include "dex_file-inl.h"
Mathieu Chartier6238c832018-01-04 09:55:13 -080023
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070024namespace art {
25
Mathieu Chartier292567e2017-10-12 13:24:38 -070026const uint8_t StandardDexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
27const uint8_t StandardDexFile::kDexMagicVersions[StandardDexFile::kNumDexVersions]
28 [StandardDexFile::kDexVersionLen] = {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070029 {'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 Chartier69147f12017-11-06 20:02:24 -080039void StandardDexFile::WriteMagic(uint8_t* magic) {
40 std::copy_n(kDexMagic, kDexMagicSize, magic);
41}
42
43void StandardDexFile::WriteCurrentVersion(uint8_t* magic) {
44 std::copy_n(kDexMagicVersions[StandardDexFile::kDexVersionLen - 1],
45 kDexVersionLen,
46 magic + kDexMagicSize);
47}
48
Mathieu Chartier292567e2017-10-12 13:24:38 -070049bool StandardDexFile::IsMagicValid(const uint8_t* magic) {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070050 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
51}
52
Mathieu Chartier292567e2017-10-12 13:24:38 -070053bool StandardDexFile::IsVersionValid(const uint8_t* magic) {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070054 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 Chartier292567e2017-10-12 13:24:38 -070063bool StandardDexFile::IsMagicValid() const {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070064 return IsMagicValid(header_->magic_);
65}
66
Mathieu Chartier292567e2017-10-12 13:24:38 -070067bool StandardDexFile::IsVersionValid() const {
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070068 return IsVersionValid(header_->magic_);
69}
70
Mathieu Chartierf6e31472017-12-28 13:32:08 -080071bool StandardDexFile::SupportsDefaultMethods() const {
72 return GetDexVersion() >= DexFile::kDefaultMethodsVersion;
73}
74
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080075uint32_t StandardDexFile::GetCodeItemSize(const dex::CodeItem& item) const {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080076 DCHECK(IsInDataSection(&item));
Mathieu Chartier698ebbc2018-01-05 11:00:42 -080077 return reinterpret_cast<uintptr_t>(CodeItemDataAccessor(*this, &item).CodeItemDataEnd()) -
78 reinterpret_cast<uintptr_t>(&item);
Mathieu Chartier6238c832018-01-04 09:55:13 -080079}
80
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070081} // namespace art