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