blob: cc56a7b714f4d3ced1027ea01d62ed3d849dc191 [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 Light40528472017-03-28 09:07:36 -070036
Alex Light40528472017-03-28 09:07:36 -070037// Runtime includes.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080038#include "dex_container.h"
Mathieu Chartier21cf2582018-01-08 17:09:48 -080039#include "dex/compact_dex_level.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010040#include "dex_to_dex_decompiler.h"
Mathieu Chartier21cf2582018-01-08 17:09:48 -080041#include "dexlayout.h"
Alex Light40528472017-03-28 09:07:36 -070042#include "oat_file.h"
43#include "vdex_file.h"
44
45namespace openjdkjvmti {
46
47static void RecomputeDexChecksum(art::DexFile* dex_file)
48 REQUIRES_SHARED(art::Locks::mutator_lock_) {
49 reinterpret_cast<art::DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
50 dex_file->CalculateChecksum();
51}
52
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010053static void DoDexUnquicken(const art::DexFile& new_dex_file, const art::DexFile& original_dex_file)
Alex Light40528472017-03-28 09:07:36 -070054 REQUIRES_SHARED(art::Locks::mutator_lock_) {
55 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
70std::unique_ptr<FixedUpDexFile> FixedUpDexFile::Create(const art::DexFile& original) {
71 // Copy the data into mutable memory.
72 std::vector<unsigned char> data;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080073 if (original.IsCompactDexFile()) {
74 // Compact dex has a separate data section that is relative from the original dex.
75 // We need to copy the shared data section so that dequickening doesn't change anything.
76 data.resize(original.Size() + original.DataSize());
77 memcpy(data.data(), original.Begin(), original.Size());
78 memcpy(data.data() + original.Size(), original.DataBegin(), original.DataSize());
79 // Go patch up the header to point to the copied data section.
80 art::CompactDexFile::Header* const header =
81 const_cast<art::CompactDexFile::Header*>(art::CompactDexFile::Header::At(data.data()));
82 header->data_off_ = original.Size();
83 header->data_size_ = original.DataSize();
84 } else {
85 data.resize(original.Size());
86 memcpy(data.data(), original.Begin(), original.Size());
87 }
Alex Light40528472017-03-28 09:07:36 -070088 std::string error;
David Sehr013fd802018-01-11 22:55:24 -080089 const art::ArtDexFileLoader dex_file_loader;
90 std::unique_ptr<const art::DexFile> new_dex_file(dex_file_loader.Open(
Alex Light40528472017-03-28 09:07:36 -070091 data.data(),
92 data.size(),
93 /*location*/"Unquickening_dexfile.dex",
94 /*location_checksum*/0,
95 /*oat_dex_file*/nullptr,
96 /*verify*/false,
97 /*verify_checksum*/false,
98 &error));
99 if (new_dex_file.get() == nullptr) {
100 LOG(ERROR) << "Unable to open dex file from memory for unquickening! error: " << error;
101 return nullptr;
102 }
103
104 DoDexUnquicken(*new_dex_file, original);
Mathieu Chartier21cf2582018-01-08 17:09:48 -0800105
106 if (original.IsCompactDexFile()) {
107 // Since we are supposed to return a standard dex, convert back using dexlayout.
108 art::Options options;
Mathieu Chartier21cf2582018-01-08 17:09:48 -0800109 options.compact_dex_level_ = art::CompactDexLevel::kCompactDexLevelNone;
110 options.update_checksum_ = true;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800111 art::DexLayout dex_layout(options,
112 /*info*/ nullptr,
113 /*out_file*/ nullptr,
114 /*header*/ nullptr);
115 std::unique_ptr<art::DexContainer> dex_container;
116 dex_layout.ProcessDexFile(new_dex_file->GetLocation().c_str(),
117 new_dex_file.get(),
118 0,
119 &dex_container);
120 art::DexContainer::Section* main_section = dex_container->GetMainSection();
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800121 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
Mathieu Chartier21cf2582018-01-08 17:09:48 -0800122 // Overwrite the dex file stored in data with the new result.
123 data.clear();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800124 data.insert(data.end(), main_section->Begin(), main_section->End());
David Sehr013fd802018-01-11 22:55:24 -0800125 new_dex_file = dex_file_loader.Open(
Mathieu Chartier21cf2582018-01-08 17:09:48 -0800126 data.data(),
127 data.size(),
128 /*location*/"Unquickening_dexfile.dex",
129 /*location_checksum*/0,
130 /*oat_dex_file*/nullptr,
131 /*verify*/false,
132 /*verify_checksum*/false,
133 &error);
134 }
135
Alex Light40528472017-03-28 09:07:36 -0700136 RecomputeDexChecksum(const_cast<art::DexFile*>(new_dex_file.get()));
137 std::unique_ptr<FixedUpDexFile> ret(new FixedUpDexFile(std::move(new_dex_file), std::move(data)));
138 return ret;
139}
140
141} // namespace openjdkjvmti