Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 1 | /* 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 Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame^] | 33 | #include "dex/dex_file-inl.h" |
| 34 | #include "dex/dex_file_loader.h" |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 35 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 36 | // Runtime includes. |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 37 | #include "dex_to_dex_decompiler.h" |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 38 | #include "oat_file.h" |
| 39 | #include "vdex_file.h" |
| 40 | |
| 41 | namespace openjdkjvmti { |
| 42 | |
| 43 | static 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 Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 49 | static void DoDexUnquicken(const art::DexFile& new_dex_file, const art::DexFile& original_dex_file) |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 50 | 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 Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 60 | if (vdex == nullptr) { |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 61 | return; |
| 62 | } |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 63 | art::VdexFile::UnquickenDexFile( |
| 64 | new_dex_file, vdex->GetQuickeningInfo(), /* decompile_return_instruction */true); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::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 Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 73 | std::unique_ptr<const art::DexFile> new_dex_file(art::DexFileLoader::Open( |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 74 | 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 |