blob: 16bbee403786f5fe391513d50f37aff9d8c4e8f5 [file] [log] [blame]
Alex Light40528472017-03-28 09:07:36 -07001/* Copyright (C) 2017 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
David Sehr67bf42e2018-02-26 16:43:04 -080032#include "base/leb128.h"
Alex Light40528472017-03-28 09:07:36 -070033#include "fixed_up_dex_file.h"
David Sehr013fd802018-01-11 22:55:24 -080034#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080035#include "dex/dex_file-inl.h"
36#include "dex/dex_file_loader.h"
Alex Light1a824a52018-01-26 15:45:30 -080037#include "dex/dex_file_verifier.h"
Alex Light40528472017-03-28 09:07:36 -070038
Alex Light40528472017-03-28 09:07:36 -070039// Runtime includes.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080040#include "dex_container.h"
Mathieu Chartier21cf2582018-01-08 17:09:48 -080041#include "dex/compact_dex_level.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010042#include "dex_to_dex_decompiler.h"
Mathieu Chartier21cf2582018-01-08 17:09:48 -080043#include "dexlayout.h"
Alex Light40528472017-03-28 09:07:36 -070044#include "oat_file.h"
45#include "vdex_file.h"
46
47namespace openjdkjvmti {
48
Alex Light2e40c1c2018-02-02 09:24:59 -080049static void RecomputeDexChecksum(art::DexFile* dex_file) {
Alex Light40528472017-03-28 09:07:36 -070050 reinterpret_cast<art::DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
51 dex_file->CalculateChecksum();
52}
53
Alex Lightc88a0082018-02-15 17:08:29 -080054static void UnhideApis(const art::DexFile& target_dex_file) {
55 for (uint32_t i = 0; i < target_dex_file.NumClassDefs(); ++i) {
56 const uint8_t* class_data = target_dex_file.GetClassData(target_dex_file.GetClassDef(i));
57 if (class_data != nullptr) {
58 for (art::ClassDataItemIterator class_it(target_dex_file, class_data);
59 class_it.HasNext();
60 class_it.Next()) {
61 art::DexFile::UnHideAccessFlags(class_it);
62 }
63 }
64 }
65}
66
67static const art::VdexFile* GetVdex(const art::DexFile& original_dex_file) {
Alex Light40528472017-03-28 09:07:36 -070068 const art::OatDexFile* oat_dex = original_dex_file.GetOatDexFile();
69 if (oat_dex == nullptr) {
Alex Lightc88a0082018-02-15 17:08:29 -080070 return nullptr;
Alex Light40528472017-03-28 09:07:36 -070071 }
72 const art::OatFile* oat_file = oat_dex->GetOatFile();
73 if (oat_file == nullptr) {
Alex Lightc88a0082018-02-15 17:08:29 -080074 return nullptr;
Alex Light40528472017-03-28 09:07:36 -070075 }
Alex Lightc88a0082018-02-15 17:08:29 -080076 return oat_file->GetVdexFile();
77}
78
79static void DoDexUnquicken(const art::DexFile& new_dex_file,
80 const art::DexFile& original_dex_file) {
81 const art::VdexFile* vdex = GetVdex(original_dex_file);
82 if (vdex != nullptr) {
83 vdex->UnquickenDexFile(new_dex_file, original_dex_file, /* decompile_return_instruction */true);
84 } else {
85 // The dex file isn't quickened since it is being used directly. We might still have hiddenapis
86 // so we need to get rid of those.
87 UnhideApis(new_dex_file);
Alex Light40528472017-03-28 09:07:36 -070088 }
Alex Light40528472017-03-28 09:07:36 -070089}
90
Alex Light1a824a52018-01-26 15:45:30 -080091static void DCheckVerifyDexFile(const art::DexFile& dex) {
92 if (art::kIsDebugBuild) {
93 std::string error;
94 if (!art::DexFileVerifier::Verify(&dex,
95 dex.Begin(),
96 dex.Size(),
97 "FixedUpDexFile_Verification.dex",
98 /*verify_checksum*/ true,
99 &error)) {
100 LOG(FATAL) << "Failed to verify de-quickened dex file: " << error;
101 }
102 }
103}
104
Mathieu Chartier75175552018-01-25 11:23:01 -0800105std::unique_ptr<FixedUpDexFile> FixedUpDexFile::Create(const art::DexFile& original,
106 const char* descriptor) {
Alex Light40528472017-03-28 09:07:36 -0700107 // Copy the data into mutable memory.
108 std::vector<unsigned char> data;
Mathieu Chartier75175552018-01-25 11:23:01 -0800109 std::unique_ptr<const art::DexFile> new_dex_file;
110 std::string error;
111 const art::ArtDexFileLoader dex_file_loader;
112
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800113 if (original.IsCompactDexFile()) {
Mathieu Chartier75175552018-01-25 11:23:01 -0800114 // Since we are supposed to return a standard dex, convert back using dexlayout. It's OK to do
115 // this before unquickening.
116 art::Options options;
117 options.compact_dex_level_ = art::CompactDexLevel::kCompactDexLevelNone;
118 // Add a filter to only include the class that has the matching descriptor.
119 static constexpr bool kFilterByDescriptor = true;
120 if (kFilterByDescriptor) {
121 options.class_filter_.insert(descriptor);
122 }
123 art::DexLayout dex_layout(options,
124 /*info*/ nullptr,
125 /*out_file*/ nullptr,
126 /*header*/ nullptr);
127 std::unique_ptr<art::DexContainer> dex_container;
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800128 bool result = dex_layout.ProcessDexFile(
129 original.GetLocation().c_str(),
130 &original,
131 0,
132 &dex_container,
133 &error);
134 CHECK(result) << "Failed to generate dex file " << error;
Mathieu Chartier75175552018-01-25 11:23:01 -0800135 art::DexContainer::Section* main_section = dex_container->GetMainSection();
136 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
137 data.insert(data.end(), main_section->Begin(), main_section->End());
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800138 } else {
139 data.resize(original.Size());
140 memcpy(data.data(), original.Begin(), original.Size());
141 }
Mathieu Chartier75175552018-01-25 11:23:01 -0800142
143 // Open the dex file in the buffer.
144 new_dex_file = dex_file_loader.Open(
Alex Light40528472017-03-28 09:07:36 -0700145 data.data(),
146 data.size(),
147 /*location*/"Unquickening_dexfile.dex",
148 /*location_checksum*/0,
149 /*oat_dex_file*/nullptr,
150 /*verify*/false,
151 /*verify_checksum*/false,
Mathieu Chartier75175552018-01-25 11:23:01 -0800152 &error);
153
154 if (new_dex_file == nullptr) {
Alex Light40528472017-03-28 09:07:36 -0700155 LOG(ERROR) << "Unable to open dex file from memory for unquickening! error: " << error;
156 return nullptr;
157 }
158
159 DoDexUnquicken(*new_dex_file, original);
Mathieu Chartier21cf2582018-01-08 17:09:48 -0800160
Alex Light40528472017-03-28 09:07:36 -0700161 RecomputeDexChecksum(const_cast<art::DexFile*>(new_dex_file.get()));
Alex Light1a824a52018-01-26 15:45:30 -0800162 DCheckVerifyDexFile(*new_dex_file);
Alex Light40528472017-03-28 09:07:36 -0700163 std::unique_ptr<FixedUpDexFile> ret(new FixedUpDexFile(std::move(new_dex_file), std::move(data)));
164 return ret;
165}
166
167} // namespace openjdkjvmti