blob: ad928d9b37b1d694aac607a7d4e9f6f2c7790a82 [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 Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file-inl.h"
34#include "dex/dex_file_loader.h"
Alex Light40528472017-03-28 09:07:36 -070035
Alex Light40528472017-03-28 09:07:36 -070036// Runtime includes.
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010037#include "dex_to_dex_decompiler.h"
Alex Light40528472017-03-28 09:07:36 -070038#include "oat_file.h"
39#include "vdex_file.h"
40
41namespace openjdkjvmti {
42
43static void RecomputeDexChecksum(art::DexFile* dex_file)
44 REQUIRES_SHARED(art::Locks::mutator_lock_) {
45 reinterpret_cast<art::DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
46 dex_file->CalculateChecksum();
47}
48
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010049static void DoDexUnquicken(const art::DexFile& new_dex_file, const art::DexFile& original_dex_file)
Alex Light40528472017-03-28 09:07:36 -070050 REQUIRES_SHARED(art::Locks::mutator_lock_) {
51 const art::OatDexFile* oat_dex = original_dex_file.GetOatDexFile();
52 if (oat_dex == nullptr) {
53 return;
54 }
55 const art::OatFile* oat_file = oat_dex->GetOatFile();
56 if (oat_file == nullptr) {
57 return;
58 }
59 const art::VdexFile* vdex = oat_file->GetVdexFile();
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010060 if (vdex == nullptr) {
Alex Light40528472017-03-28 09:07:36 -070061 return;
62 }
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +000063 art::VdexFile::UnquickenDexFile(
64 new_dex_file, vdex->GetQuickeningInfo(), /* decompile_return_instruction */true);
Alex Light40528472017-03-28 09:07:36 -070065}
66
67std::unique_ptr<FixedUpDexFile> FixedUpDexFile::Create(const art::DexFile& original) {
68 // Copy the data into mutable memory.
69 std::vector<unsigned char> data;
70 data.resize(original.Size());
71 memcpy(data.data(), original.Begin(), original.Size());
72 std::string error;
Mathieu Chartier79c87da2017-10-10 11:54:29 -070073 std::unique_ptr<const art::DexFile> new_dex_file(art::DexFileLoader::Open(
Alex Light40528472017-03-28 09:07:36 -070074 data.data(),
75 data.size(),
76 /*location*/"Unquickening_dexfile.dex",
77 /*location_checksum*/0,
78 /*oat_dex_file*/nullptr,
79 /*verify*/false,
80 /*verify_checksum*/false,
81 &error));
82 if (new_dex_file.get() == nullptr) {
83 LOG(ERROR) << "Unable to open dex file from memory for unquickening! error: " << error;
84 return nullptr;
85 }
86
87 DoDexUnquicken(*new_dex_file, original);
88 RecomputeDexChecksum(const_cast<art::DexFile*>(new_dex_file.get()));
89 std::unique_ptr<FixedUpDexFile> ret(new FixedUpDexFile(std::move(new_dex_file), std::move(data)));
90 return ret;
91}
92
93} // namespace openjdkjvmti