blob: a8d2a37fa671bf4f9e42fc1c3f0a0c140c896d64 [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
32#include "fixed_up_dex_file.h"
David Sehr013fd802018-01-11 22:55:24 -080033#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file-inl.h"
35#include "dex/dex_file_loader.h"
Alex Light1a824a52018-01-26 15:45:30 -080036#include "dex/dex_file_verifier.h"
Alex Light40528472017-03-28 09:07:36 -070037
Alex Light40528472017-03-28 09:07:36 -070038// Runtime includes.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080039#include "dex_container.h"
Mathieu Chartier21cf2582018-01-08 17:09:48 -080040#include "dex/compact_dex_level.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010041#include "dex_to_dex_decompiler.h"
Mathieu Chartier21cf2582018-01-08 17:09:48 -080042#include "dexlayout.h"
Alex Light40528472017-03-28 09:07:36 -070043#include "oat_file.h"
44#include "vdex_file.h"
45
46namespace openjdkjvmti {
47
48static void RecomputeDexChecksum(art::DexFile* dex_file)
49 REQUIRES_SHARED(art::Locks::mutator_lock_) {
50 reinterpret_cast<art::DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
51 dex_file->CalculateChecksum();
52}
53
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010054static void DoDexUnquicken(const art::DexFile& new_dex_file, const art::DexFile& original_dex_file)
Alex Light40528472017-03-28 09:07:36 -070055 REQUIRES_SHARED(art::Locks::mutator_lock_) {
56 const art::OatDexFile* oat_dex = original_dex_file.GetOatDexFile();
57 if (oat_dex == nullptr) {
58 return;
59 }
60 const art::OatFile* oat_file = oat_dex->GetOatFile();
61 if (oat_file == nullptr) {
62 return;
63 }
64 const art::VdexFile* vdex = oat_file->GetVdexFile();
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010065 if (vdex == nullptr) {
Alex Light40528472017-03-28 09:07:36 -070066 return;
67 }
Mathieu Chartier210531f2018-01-12 10:15:51 -080068 vdex->UnquickenDexFile(new_dex_file, original_dex_file, /* decompile_return_instruction */true);
Alex Light40528472017-03-28 09:07:36 -070069}
70
Alex Light1a824a52018-01-26 15:45:30 -080071static void DCheckVerifyDexFile(const art::DexFile& dex) {
72 if (art::kIsDebugBuild) {
73 std::string error;
74 if (!art::DexFileVerifier::Verify(&dex,
75 dex.Begin(),
76 dex.Size(),
77 "FixedUpDexFile_Verification.dex",
78 /*verify_checksum*/ true,
79 &error)) {
80 LOG(FATAL) << "Failed to verify de-quickened dex file: " << error;
81 }
82 }
83}
84
Mathieu Chartier75175552018-01-25 11:23:01 -080085std::unique_ptr<FixedUpDexFile> FixedUpDexFile::Create(const art::DexFile& original,
86 const char* descriptor) {
Alex Light40528472017-03-28 09:07:36 -070087 // Copy the data into mutable memory.
88 std::vector<unsigned char> data;
Mathieu Chartier75175552018-01-25 11:23:01 -080089 std::unique_ptr<const art::DexFile> new_dex_file;
90 std::string error;
91 const art::ArtDexFileLoader dex_file_loader;
92
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080093 if (original.IsCompactDexFile()) {
Mathieu Chartier75175552018-01-25 11:23:01 -080094 // Since we are supposed to return a standard dex, convert back using dexlayout. It's OK to do
95 // this before unquickening.
96 art::Options options;
97 options.compact_dex_level_ = art::CompactDexLevel::kCompactDexLevelNone;
98 // Add a filter to only include the class that has the matching descriptor.
99 static constexpr bool kFilterByDescriptor = true;
100 if (kFilterByDescriptor) {
101 options.class_filter_.insert(descriptor);
102 }
103 art::DexLayout dex_layout(options,
104 /*info*/ nullptr,
105 /*out_file*/ nullptr,
106 /*header*/ nullptr);
107 std::unique_ptr<art::DexContainer> dex_container;
108 dex_layout.ProcessDexFile(original.GetLocation().c_str(),
109 &original,
110 0,
111 &dex_container);
112 art::DexContainer::Section* main_section = dex_container->GetMainSection();
113 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
114 data.insert(data.end(), main_section->Begin(), main_section->End());
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800115 } else {
116 data.resize(original.Size());
117 memcpy(data.data(), original.Begin(), original.Size());
118 }
Mathieu Chartier75175552018-01-25 11:23:01 -0800119
120 // Open the dex file in the buffer.
121 new_dex_file = dex_file_loader.Open(
Alex Light40528472017-03-28 09:07:36 -0700122 data.data(),
123 data.size(),
124 /*location*/"Unquickening_dexfile.dex",
125 /*location_checksum*/0,
126 /*oat_dex_file*/nullptr,
127 /*verify*/false,
128 /*verify_checksum*/false,
Mathieu Chartier75175552018-01-25 11:23:01 -0800129 &error);
130
131 if (new_dex_file == nullptr) {
Alex Light40528472017-03-28 09:07:36 -0700132 LOG(ERROR) << "Unable to open dex file from memory for unquickening! error: " << error;
133 return nullptr;
134 }
135
136 DoDexUnquicken(*new_dex_file, original);
Mathieu Chartier21cf2582018-01-08 17:09:48 -0800137
Alex Light40528472017-03-28 09:07:36 -0700138 RecomputeDexChecksum(const_cast<art::DexFile*>(new_dex_file.get()));
Alex Light1a824a52018-01-26 15:45:30 -0800139 DCheckVerifyDexFile(*new_dex_file);
Alex Light40528472017-03-28 09:07:36 -0700140 std::unique_ptr<FixedUpDexFile> ret(new FixedUpDexFile(std::move(new_dex_file), std::move(data)));
141 return ret;
142}
143
144} // namespace openjdkjvmti