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 "base/leb128.h"
33 #include "fixed_up_dex_file.h"
34 #include "dex/class_accessor-inl.h"
35 #include "dex/dex_file-inl.h"
36 #include "dex/dex_file_loader.h"
37 #include "dex/dex_file_verifier.h"
38 
39 // Runtime includes.
40 #include "dex_container.h"
41 #include "dex/compact_dex_level.h"
42 #include "dexlayout.h"
43 #include "oat_file.h"
44 #include "vdex_file.h"
45 
46 namespace openjdkjvmti {
47 
RecomputeDexChecksum(art::DexFile * dex_file)48 static void RecomputeDexChecksum(art::DexFile* dex_file) {
49   reinterpret_cast<art::DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
50       dex_file->CalculateChecksum();
51 }
52 
DCheckVerifyDexFile(const art::DexFile & dex)53 static void DCheckVerifyDexFile(const art::DexFile& dex) {
54   if (art::kIsDebugBuild) {
55     std::string error;
56     if (!art::dex::Verify(&dex,
57                           dex.Begin(),
58                           dex.Size(),
59                           "FixedUpDexFile_Verification.dex",
60                           /*verify_checksum=*/ true,
61                           &error)) {
62       LOG(FATAL) << "Failed to verify de-quickened dex file: " << error;
63     }
64   }
65 }
66 
Create(const art::DexFile & original,const char * descriptor)67 std::unique_ptr<FixedUpDexFile> FixedUpDexFile::Create(const art::DexFile& original,
68                                                        const char* descriptor) {
69   // Copy the data into mutable memory.
70   std::vector<unsigned char> data;
71   std::unique_ptr<const art::DexFile> new_dex_file;
72   std::string error;
73 
74   // Do not use ArtDexFileLoader here. This code runs in a signal handler and
75   // its stack is too small to invoke the required LocationIsOnSystemFramework
76   // (b/76429651). Instead, we use DexFileLoader and copy the IsPlatformDexFile
77   // property from `original` to `new_dex_file`.
78   const art::DexFileLoader dex_file_loader;
79 
80   if (original.IsCompactDexFile() || original.HasHiddenapiClassData()) {
81     // Since we are supposed to return a standard dex, convert back using dexlayout. It's OK to do
82     // this before unquickening.
83     // We also do dex layout for dex files that have hidden API data, as we want to remove that
84     // data.
85     art::Options options;
86     options.compact_dex_level_ = art::CompactDexLevel::kCompactDexLevelNone;
87     // Add a filter to only include the class that has the matching descriptor.
88     static constexpr bool kFilterByDescriptor = true;
89     if (kFilterByDescriptor) {
90       options.class_filter_.insert(descriptor);
91     }
92     art::DexLayout dex_layout(options,
93                               /*info=*/ nullptr,
94                               /*out_file=*/ nullptr,
95                               /*header=*/ nullptr);
96     std::unique_ptr<art::DexContainer> dex_container;
97     bool result = dex_layout.ProcessDexFile(
98         original.GetLocation().c_str(),
99         &original,
100         0,
101         &dex_container,
102         &error);
103     CHECK(result) << "Failed to generate dex file " << error;
104     art::DexContainer::Section* main_section = dex_container->GetMainSection();
105     CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
106     data.insert(data.end(), main_section->Begin(), main_section->End());
107   } else {
108     data.resize(original.Size());
109     memcpy(data.data(), original.Begin(), original.Size());
110   }
111 
112   // Open the dex file in the buffer.
113   new_dex_file = dex_file_loader.Open(
114       data.data(),
115       data.size(),
116       /*location=*/"Unquickening_dexfile.dex",
117       /*location_checksum=*/0,
118       /*oat_dex_file=*/nullptr,
119       /*verify=*/false,
120       /*verify_checksum=*/false,
121       &error);
122 
123   if (new_dex_file == nullptr) {
124     LOG(ERROR) << "Unable to open dex file from memory for unquickening! error: " << error;
125     return nullptr;
126   }
127 
128   new_dex_file->SetHiddenapiDomain(original.GetHiddenapiDomain());
129 
130   RecomputeDexChecksum(const_cast<art::DexFile*>(new_dex_file.get()));
131   DCheckVerifyDexFile(*new_dex_file);
132   std::unique_ptr<FixedUpDexFile> ret(new FixedUpDexFile(std::move(new_dex_file), std::move(data)));
133   return ret;
134 }
135 
136 }  // namespace openjdkjvmti
137