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 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 32 | #include "base/leb128.h" |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 33 | #include "fixed_up_dex_file.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 34 | #include "dex/class_accessor-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 35 | #include "dex/dex_file-inl.h" |
| 36 | #include "dex/dex_file_loader.h" |
Alex Light | 1a824a5 | 2018-01-26 15:45:30 -0800 | [diff] [blame] | 37 | #include "dex/dex_file_verifier.h" |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 38 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 39 | // Runtime includes. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 40 | #include "dex_container.h" |
Mathieu Chartier | 21cf258 | 2018-01-08 17:09:48 -0800 | [diff] [blame] | 41 | #include "dex/compact_dex_level.h" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 42 | #include "dex_to_dex_decompiler.h" |
Mathieu Chartier | 21cf258 | 2018-01-08 17:09:48 -0800 | [diff] [blame] | 43 | #include "dexlayout.h" |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 44 | #include "oat_file.h" |
| 45 | #include "vdex_file.h" |
| 46 | |
| 47 | namespace openjdkjvmti { |
| 48 | |
Alex Light | 2e40c1c | 2018-02-02 09:24:59 -0800 | [diff] [blame] | 49 | static void RecomputeDexChecksum(art::DexFile* dex_file) { |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 50 | reinterpret_cast<art::DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ = |
| 51 | dex_file->CalculateChecksum(); |
| 52 | } |
| 53 | |
Alex Light | c88a008 | 2018-02-15 17:08:29 -0800 | [diff] [blame] | 54 | static const art::VdexFile* GetVdex(const art::DexFile& original_dex_file) { |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 55 | const art::OatDexFile* oat_dex = original_dex_file.GetOatDexFile(); |
| 56 | if (oat_dex == nullptr) { |
Alex Light | c88a008 | 2018-02-15 17:08:29 -0800 | [diff] [blame] | 57 | return nullptr; |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 58 | } |
| 59 | const art::OatFile* oat_file = oat_dex->GetOatFile(); |
| 60 | if (oat_file == nullptr) { |
Alex Light | c88a008 | 2018-02-15 17:08:29 -0800 | [diff] [blame] | 61 | return nullptr; |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 62 | } |
Alex Light | c88a008 | 2018-02-15 17:08:29 -0800 | [diff] [blame] | 63 | return oat_file->GetVdexFile(); |
| 64 | } |
| 65 | |
| 66 | static void DoDexUnquicken(const art::DexFile& new_dex_file, |
| 67 | const art::DexFile& original_dex_file) { |
| 68 | const art::VdexFile* vdex = GetVdex(original_dex_file); |
| 69 | if (vdex != nullptr) { |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 70 | vdex->UnquickenDexFile(new_dex_file, |
| 71 | original_dex_file, |
| 72 | /* decompile_return_instruction= */ true); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 73 | } |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Alex Light | 1a824a5 | 2018-01-26 15:45:30 -0800 | [diff] [blame] | 76 | static void DCheckVerifyDexFile(const art::DexFile& dex) { |
| 77 | if (art::kIsDebugBuild) { |
| 78 | std::string error; |
Andreas Gampe | 60415ae | 2019-07-02 14:03:13 -0700 | [diff] [blame] | 79 | if (!art::dex::Verify(&dex, |
| 80 | dex.Begin(), |
| 81 | dex.Size(), |
| 82 | "FixedUpDexFile_Verification.dex", |
| 83 | /*verify_checksum=*/ true, |
| 84 | &error)) { |
Alex Light | 1a824a5 | 2018-01-26 15:45:30 -0800 | [diff] [blame] | 85 | LOG(FATAL) << "Failed to verify de-quickened dex file: " << error; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
Nicolas Geoffray | 16fc474 | 2019-01-30 16:53:24 +0000 | [diff] [blame] | 90 | std::unique_ptr<FixedUpDexFile> FixedUpDexFile::Create(const art::DexFile& original, |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 91 | const char* descriptor) { |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 92 | // Copy the data into mutable memory. |
| 93 | std::vector<unsigned char> data; |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 94 | std::unique_ptr<const art::DexFile> new_dex_file; |
| 95 | std::string error; |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 96 | |
| 97 | // Do not use ArtDexFileLoader here. This code runs in a signal handler and |
| 98 | // its stack is too small to invoke the required LocationIsOnSystemFramework |
| 99 | // (b/76429651). Instead, we use DexFileLoader and copy the IsPlatformDexFile |
| 100 | // property from `original` to `new_dex_file`. |
| 101 | const art::DexFileLoader dex_file_loader; |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 102 | |
Nicolas Geoffray | 16fc474 | 2019-01-30 16:53:24 +0000 | [diff] [blame] | 103 | if (original.IsCompactDexFile() || original.HasHiddenapiClassData()) { |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 104 | // Since we are supposed to return a standard dex, convert back using dexlayout. It's OK to do |
| 105 | // this before unquickening. |
Nicolas Geoffray | 16fc474 | 2019-01-30 16:53:24 +0000 | [diff] [blame] | 106 | // We also do dex layout for dex files that have hidden API data, as we want to remove that |
| 107 | // data. |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 108 | art::Options options; |
| 109 | options.compact_dex_level_ = art::CompactDexLevel::kCompactDexLevelNone; |
| 110 | // Add a filter to only include the class that has the matching descriptor. |
| 111 | static constexpr bool kFilterByDescriptor = true; |
| 112 | if (kFilterByDescriptor) { |
| 113 | options.class_filter_.insert(descriptor); |
| 114 | } |
| 115 | art::DexLayout dex_layout(options, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 116 | /*info=*/ nullptr, |
| 117 | /*out_file=*/ nullptr, |
| 118 | /*header=*/ nullptr); |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 119 | std::unique_ptr<art::DexContainer> dex_container; |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 120 | bool result = dex_layout.ProcessDexFile( |
| 121 | original.GetLocation().c_str(), |
| 122 | &original, |
| 123 | 0, |
| 124 | &dex_container, |
| 125 | &error); |
| 126 | CHECK(result) << "Failed to generate dex file " << error; |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 127 | art::DexContainer::Section* main_section = dex_container->GetMainSection(); |
| 128 | CHECK_EQ(dex_container->GetDataSection()->Size(), 0u); |
| 129 | data.insert(data.end(), main_section->Begin(), main_section->End()); |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 130 | } else { |
| 131 | data.resize(original.Size()); |
| 132 | memcpy(data.data(), original.Begin(), original.Size()); |
| 133 | } |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 134 | |
| 135 | // Open the dex file in the buffer. |
| 136 | new_dex_file = dex_file_loader.Open( |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 137 | data.data(), |
| 138 | data.size(), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 139 | /*location=*/"Unquickening_dexfile.dex", |
| 140 | /*location_checksum=*/0, |
| 141 | /*oat_dex_file=*/nullptr, |
| 142 | /*verify=*/false, |
| 143 | /*verify_checksum=*/false, |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 144 | &error); |
| 145 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 146 | if (new_dex_file == nullptr) { |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 147 | LOG(ERROR) << "Unable to open dex file from memory for unquickening! error: " << error; |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
David Brazdil | e7e26d1 | 2019-02-28 15:04:14 +0000 | [diff] [blame] | 151 | new_dex_file->SetHiddenapiDomain(original.GetHiddenapiDomain()); |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 152 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 153 | DoDexUnquicken(*new_dex_file, original); |
Mathieu Chartier | 21cf258 | 2018-01-08 17:09:48 -0800 | [diff] [blame] | 154 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 155 | RecomputeDexChecksum(const_cast<art::DexFile*>(new_dex_file.get())); |
Alex Light | 1a824a5 | 2018-01-26 15:45:30 -0800 | [diff] [blame] | 156 | DCheckVerifyDexFile(*new_dex_file); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 157 | std::unique_ptr<FixedUpDexFile> ret(new FixedUpDexFile(std::move(new_dex_file), std::move(data))); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | } // namespace openjdkjvmti |