blob: 427d87eedf95476db7fbd94c949dad0049e260ba [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
Alex Light2e40c1c2018-02-02 09:24:59 -080048static void RecomputeDexChecksum(art::DexFile* dex_file) {
Alex Light40528472017-03-28 09:07:36 -070049 reinterpret_cast<art::DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
50 dex_file->CalculateChecksum();
51}
52
Alex Light2e40c1c2018-02-02 09:24:59 -080053static void DoDexUnquicken(const art::DexFile& new_dex_file,
54 const art::DexFile& original_dex_file) {
Alex Light40528472017-03-28 09:07:36 -070055 const art::OatDexFile* oat_dex = original_dex_file.GetOatDexFile();
56 if (oat_dex == nullptr) {
57 return;
58 }
59 const art::OatFile* oat_file = oat_dex->GetOatFile();
60 if (oat_file == nullptr) {
61 return;
62 }
63 const art::VdexFile* vdex = oat_file->GetVdexFile();
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010064 if (vdex == nullptr) {
Alex Light40528472017-03-28 09:07:36 -070065 return;
66 }
Mathieu Chartier210531f2018-01-12 10:15:51 -080067 vdex->UnquickenDexFile(new_dex_file, original_dex_file, /* decompile_return_instruction */true);
Alex Light40528472017-03-28 09:07:36 -070068}
69
Alex Light1a824a52018-01-26 15:45:30 -080070static void DCheckVerifyDexFile(const art::DexFile& dex) {
71 if (art::kIsDebugBuild) {
72 std::string error;
73 if (!art::DexFileVerifier::Verify(&dex,
74 dex.Begin(),
75 dex.Size(),
76 "FixedUpDexFile_Verification.dex",
77 /*verify_checksum*/ true,
78 &error)) {
79 LOG(FATAL) << "Failed to verify de-quickened dex file: " << error;
80 }
81 }
82}
83
Mathieu Chartier75175552018-01-25 11:23:01 -080084std::unique_ptr<FixedUpDexFile> FixedUpDexFile::Create(const art::DexFile& original,
85 const char* descriptor) {
Alex Light40528472017-03-28 09:07:36 -070086 // Copy the data into mutable memory.
87 std::vector<unsigned char> data;
Mathieu Chartier75175552018-01-25 11:23:01 -080088 std::unique_ptr<const art::DexFile> new_dex_file;
89 std::string error;
90 const art::ArtDexFileLoader dex_file_loader;
91
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080092 if (original.IsCompactDexFile()) {
Mathieu Chartier75175552018-01-25 11:23:01 -080093 // Since we are supposed to return a standard dex, convert back using dexlayout. It's OK to do
94 // this before unquickening.
95 art::Options options;
96 options.compact_dex_level_ = art::CompactDexLevel::kCompactDexLevelNone;
97 // Add a filter to only include the class that has the matching descriptor.
98 static constexpr bool kFilterByDescriptor = true;
99 if (kFilterByDescriptor) {
100 options.class_filter_.insert(descriptor);
101 }
102 art::DexLayout dex_layout(options,
103 /*info*/ nullptr,
104 /*out_file*/ nullptr,
105 /*header*/ nullptr);
106 std::unique_ptr<art::DexContainer> dex_container;
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800107 bool result = dex_layout.ProcessDexFile(
108 original.GetLocation().c_str(),
109 &original,
110 0,
111 &dex_container,
112 &error);
113 CHECK(result) << "Failed to generate dex file " << error;
Mathieu Chartier75175552018-01-25 11:23:01 -0800114 art::DexContainer::Section* main_section = dex_container->GetMainSection();
115 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
116 data.insert(data.end(), main_section->Begin(), main_section->End());
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800117 } else {
118 data.resize(original.Size());
119 memcpy(data.data(), original.Begin(), original.Size());
120 }
Mathieu Chartier75175552018-01-25 11:23:01 -0800121
122 // Open the dex file in the buffer.
123 new_dex_file = dex_file_loader.Open(
Alex Light40528472017-03-28 09:07:36 -0700124 data.data(),
125 data.size(),
126 /*location*/"Unquickening_dexfile.dex",
127 /*location_checksum*/0,
128 /*oat_dex_file*/nullptr,
129 /*verify*/false,
130 /*verify_checksum*/false,
Mathieu Chartier75175552018-01-25 11:23:01 -0800131 &error);
132
133 if (new_dex_file == nullptr) {
Alex Light40528472017-03-28 09:07:36 -0700134 LOG(ERROR) << "Unable to open dex file from memory for unquickening! error: " << error;
135 return nullptr;
136 }
137
138 DoDexUnquicken(*new_dex_file, original);
Mathieu Chartier21cf2582018-01-08 17:09:48 -0800139
Alex Light40528472017-03-28 09:07:36 -0700140 RecomputeDexChecksum(const_cast<art::DexFile*>(new_dex_file.get()));
Alex Light1a824a52018-01-26 15:45:30 -0800141 DCheckVerifyDexFile(*new_dex_file);
Alex Light40528472017-03-28 09:07:36 -0700142 std::unique_ptr<FixedUpDexFile> ret(new FixedUpDexFile(std::move(new_dex_file), std::move(data)));
143 return ret;
144}
145
146} // namespace openjdkjvmti