1*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2016 The Android Open Source Project
2*795d594fSAndroid Build Coastguard Worker * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * This file implements interfaces from the file jvmti.h. This implementation
5*795d594fSAndroid Build Coastguard Worker * is licensed under the same terms as the file jvmti.h. The
6*795d594fSAndroid Build Coastguard Worker * copyright and license information for the file jvmti.h follows.
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9*795d594fSAndroid Build Coastguard Worker * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10*795d594fSAndroid Build Coastguard Worker *
11*795d594fSAndroid Build Coastguard Worker * This code is free software; you can redistribute it and/or modify it
12*795d594fSAndroid Build Coastguard Worker * under the terms of the GNU General Public License version 2 only, as
13*795d594fSAndroid Build Coastguard Worker * published by the Free Software Foundation. Oracle designates this
14*795d594fSAndroid Build Coastguard Worker * particular file as subject to the "Classpath" exception as provided
15*795d594fSAndroid Build Coastguard Worker * by Oracle in the LICENSE file that accompanied this code.
16*795d594fSAndroid Build Coastguard Worker *
17*795d594fSAndroid Build Coastguard Worker * This code is distributed in the hope that it will be useful, but WITHOUT
18*795d594fSAndroid Build Coastguard Worker * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19*795d594fSAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20*795d594fSAndroid Build Coastguard Worker * version 2 for more details (a copy is included in the LICENSE file that
21*795d594fSAndroid Build Coastguard Worker * accompanied this code).
22*795d594fSAndroid Build Coastguard Worker *
23*795d594fSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License version
24*795d594fSAndroid Build Coastguard Worker * 2 along with this work; if not, write to the Free Software Foundation,
25*795d594fSAndroid Build Coastguard Worker * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26*795d594fSAndroid Build Coastguard Worker *
27*795d594fSAndroid Build Coastguard Worker * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28*795d594fSAndroid Build Coastguard Worker * or visit www.oracle.com if you need additional information or have any
29*795d594fSAndroid Build Coastguard Worker * questions.
30*795d594fSAndroid Build Coastguard Worker */
31*795d594fSAndroid Build Coastguard Worker
32*795d594fSAndroid Build Coastguard Worker #include "ti_class_definition.h"
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker #include "base/array_slice.h"
35*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"
36*795d594fSAndroid Build Coastguard Worker #include "class_linker-inl.h"
37*795d594fSAndroid Build Coastguard Worker #include "class_root-inl.h"
38*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
39*795d594fSAndroid Build Coastguard Worker #include "dex/art_dex_file_loader.h"
40*795d594fSAndroid Build Coastguard Worker #include "handle.h"
41*795d594fSAndroid Build Coastguard Worker #include "handle_scope-inl.h"
42*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
43*795d594fSAndroid Build Coastguard Worker #include "mirror/class_ext-inl.h"
44*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
45*795d594fSAndroid Build Coastguard Worker #include "reflection.h"
46*795d594fSAndroid Build Coastguard Worker #include "thread.h"
47*795d594fSAndroid Build Coastguard Worker
48*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
49*795d594fSAndroid Build Coastguard Worker
IsModified() const50*795d594fSAndroid Build Coastguard Worker bool ArtClassDefinition::IsModified() const {
51*795d594fSAndroid Build Coastguard Worker // RedefineClasses calls always are 'modified' since they need to change the current_dex_file of
52*795d594fSAndroid Build Coastguard Worker // the class.
53*795d594fSAndroid Build Coastguard Worker if (redefined_) {
54*795d594fSAndroid Build Coastguard Worker return true;
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker
57*795d594fSAndroid Build Coastguard Worker // Check to see if any change has taken place.
58*795d594fSAndroid Build Coastguard Worker if (current_dex_file_.data() == dex_data_.data()) {
59*795d594fSAndroid Build Coastguard Worker // no change at all.
60*795d594fSAndroid Build Coastguard Worker return false;
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker
63*795d594fSAndroid Build Coastguard Worker // Check if the dex file we want to set is the same as the current one.
64*795d594fSAndroid Build Coastguard Worker // Unfortunately we need to do this check even if no modifications have been done since it could
65*795d594fSAndroid Build Coastguard Worker // be that agents were removed in the mean-time so we still have a different dex file. The dex
66*795d594fSAndroid Build Coastguard Worker // checksum means this is likely to be fairly fast.
67*795d594fSAndroid Build Coastguard Worker return current_dex_file_.size() != dex_data_.size() ||
68*795d594fSAndroid Build Coastguard Worker memcmp(current_dex_file_.data(), dex_data_.data(), current_dex_file_.size()) != 0;
69*795d594fSAndroid Build Coastguard Worker }
70*795d594fSAndroid Build Coastguard Worker
InitCommon(art::Thread * self,jclass klass)71*795d594fSAndroid Build Coastguard Worker jvmtiError ArtClassDefinition::InitCommon(art::Thread* self, jclass klass) {
72*795d594fSAndroid Build Coastguard Worker art::ScopedObjectAccess soa(self);
73*795d594fSAndroid Build Coastguard Worker art::ObjPtr<art::mirror::Class> m_klass(soa.Decode<art::mirror::Class>(klass));
74*795d594fSAndroid Build Coastguard Worker if (m_klass.IsNull()) {
75*795d594fSAndroid Build Coastguard Worker return ERR(INVALID_CLASS);
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker initialized_ = true;
78*795d594fSAndroid Build Coastguard Worker klass_ = klass;
79*795d594fSAndroid Build Coastguard Worker loader_ = soa.AddLocalReference<jobject>(m_klass->GetClassLoader());
80*795d594fSAndroid Build Coastguard Worker std::string descriptor_store;
81*795d594fSAndroid Build Coastguard Worker std::string descriptor(m_klass->GetDescriptor(&descriptor_store));
82*795d594fSAndroid Build Coastguard Worker name_ = descriptor.substr(1, descriptor.size() - 2);
83*795d594fSAndroid Build Coastguard Worker // Android doesn't really have protection domains.
84*795d594fSAndroid Build Coastguard Worker protection_domain_ = nullptr;
85*795d594fSAndroid Build Coastguard Worker return OK;
86*795d594fSAndroid Build Coastguard Worker }
87*795d594fSAndroid Build Coastguard Worker
Init(art::Thread * self,jclass klass)88*795d594fSAndroid Build Coastguard Worker jvmtiError ArtClassDefinition::Init(art::Thread* self, jclass klass) {
89*795d594fSAndroid Build Coastguard Worker jvmtiError res = InitCommon(self, klass);
90*795d594fSAndroid Build Coastguard Worker if (res != OK) {
91*795d594fSAndroid Build Coastguard Worker return res;
92*795d594fSAndroid Build Coastguard Worker }
93*795d594fSAndroid Build Coastguard Worker art::ScopedObjectAccess soa(self);
94*795d594fSAndroid Build Coastguard Worker art::StackHandleScope<1> hs(self);
95*795d594fSAndroid Build Coastguard Worker art::Handle<art::mirror::Class> m_klass(hs.NewHandle(self->DecodeJObject(klass)->AsClass()));
96*795d594fSAndroid Build Coastguard Worker art::ObjPtr<art::mirror::ClassExt> ext(m_klass->GetExtData());
97*795d594fSAndroid Build Coastguard Worker if (!ext.IsNull()) {
98*795d594fSAndroid Build Coastguard Worker art::ObjPtr<art::mirror::Object> orig_dex(ext->GetOriginalDexFile());
99*795d594fSAndroid Build Coastguard Worker if (!orig_dex.IsNull()) {
100*795d594fSAndroid Build Coastguard Worker if (orig_dex->IsArrayInstance()) {
101*795d594fSAndroid Build Coastguard Worker // An array instance means the original-dex-file is from a redefineClasses which cannot have any
102*795d594fSAndroid Build Coastguard Worker // compact dex, so it's fine to use directly.
103*795d594fSAndroid Build Coastguard Worker art::ObjPtr<art::mirror::ByteArray> byte_array(orig_dex->AsByteArray());
104*795d594fSAndroid Build Coastguard Worker dex_data_memory_.resize(byte_array->GetLength());
105*795d594fSAndroid Build Coastguard Worker memcpy(dex_data_memory_.data(), byte_array->GetData(), dex_data_memory_.size());
106*795d594fSAndroid Build Coastguard Worker dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
107*795d594fSAndroid Build Coastguard Worker
108*795d594fSAndroid Build Coastguard Worker const art::DexFile& cur_dex = m_klass->GetDexFile();
109*795d594fSAndroid Build Coastguard Worker current_dex_file_ =
110*795d594fSAndroid Build Coastguard Worker art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.SizeIncludingSharedData());
111*795d594fSAndroid Build Coastguard Worker return OK;
112*795d594fSAndroid Build Coastguard Worker }
113*795d594fSAndroid Build Coastguard Worker
114*795d594fSAndroid Build Coastguard Worker if (orig_dex->IsDexCache()) {
115*795d594fSAndroid Build Coastguard Worker res = Init(*orig_dex->AsDexCache()->GetDexFile());
116*795d594fSAndroid Build Coastguard Worker if (res != OK) {
117*795d594fSAndroid Build Coastguard Worker return res;
118*795d594fSAndroid Build Coastguard Worker }
119*795d594fSAndroid Build Coastguard Worker } else {
120*795d594fSAndroid Build Coastguard Worker DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"))
121*795d594fSAndroid Build Coastguard Worker << "Expected java/lang/Long but found object of type "
122*795d594fSAndroid Build Coastguard Worker << orig_dex->GetClass()->PrettyClass();
123*795d594fSAndroid Build Coastguard Worker art::ObjPtr<art::mirror::Class> prim_long_class(
124*795d594fSAndroid Build Coastguard Worker art::GetClassRoot(art::ClassRoot::kPrimitiveLong));
125*795d594fSAndroid Build Coastguard Worker art::JValue val;
126*795d594fSAndroid Build Coastguard Worker if (!art::UnboxPrimitiveForResult(orig_dex.Ptr(), prim_long_class, &val)) {
127*795d594fSAndroid Build Coastguard Worker // This should never happen.
128*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unable to unbox a primitive long value!";
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker res = Init(*reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ())));
131*795d594fSAndroid Build Coastguard Worker if (res != OK) {
132*795d594fSAndroid Build Coastguard Worker return res;
133*795d594fSAndroid Build Coastguard Worker }
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker const art::DexFile& cur_dex = m_klass->GetDexFile();
136*795d594fSAndroid Build Coastguard Worker current_dex_file_ =
137*795d594fSAndroid Build Coastguard Worker art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.SizeIncludingSharedData());
138*795d594fSAndroid Build Coastguard Worker return OK;
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker // No redefinition must have ever happened so we can use the class's dex file.
142*795d594fSAndroid Build Coastguard Worker return Init(m_klass->GetDexFile());
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker
Init(art::Thread * self,const jvmtiClassDefinition & def)145*795d594fSAndroid Build Coastguard Worker jvmtiError ArtClassDefinition::Init(art::Thread* self, const jvmtiClassDefinition& def) {
146*795d594fSAndroid Build Coastguard Worker jvmtiError res = InitCommon(self, def.klass);
147*795d594fSAndroid Build Coastguard Worker if (res != OK) {
148*795d594fSAndroid Build Coastguard Worker return res;
149*795d594fSAndroid Build Coastguard Worker }
150*795d594fSAndroid Build Coastguard Worker // We are being directly redefined.
151*795d594fSAndroid Build Coastguard Worker redefined_ = true;
152*795d594fSAndroid Build Coastguard Worker current_dex_file_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
153*795d594fSAndroid Build Coastguard Worker dex_data_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
154*795d594fSAndroid Build Coastguard Worker return OK;
155*795d594fSAndroid Build Coastguard Worker }
156*795d594fSAndroid Build Coastguard Worker
InitFirstLoad(const char * descriptor,art::Handle<art::mirror::ClassLoader> klass_loader,const art::DexFile & dex_file)157*795d594fSAndroid Build Coastguard Worker jvmtiError ArtClassDefinition::InitFirstLoad(const char* descriptor,
158*795d594fSAndroid Build Coastguard Worker art::Handle<art::mirror::ClassLoader> klass_loader,
159*795d594fSAndroid Build Coastguard Worker const art::DexFile& dex_file) {
160*795d594fSAndroid Build Coastguard Worker art::Thread* self = art::Thread::Current();
161*795d594fSAndroid Build Coastguard Worker art::ScopedObjectAccess soa(self);
162*795d594fSAndroid Build Coastguard Worker initialized_ = true;
163*795d594fSAndroid Build Coastguard Worker // No Class
164*795d594fSAndroid Build Coastguard Worker klass_ = nullptr;
165*795d594fSAndroid Build Coastguard Worker loader_ = soa.AddLocalReference<jobject>(klass_loader.Get());
166*795d594fSAndroid Build Coastguard Worker std::string descriptor_str(descriptor);
167*795d594fSAndroid Build Coastguard Worker name_ = descriptor_str.substr(1, descriptor_str.size() - 2);
168*795d594fSAndroid Build Coastguard Worker // Android doesn't really have protection domains.
169*795d594fSAndroid Build Coastguard Worker protection_domain_ = nullptr;
170*795d594fSAndroid Build Coastguard Worker return Init(dex_file);
171*795d594fSAndroid Build Coastguard Worker }
172*795d594fSAndroid Build Coastguard Worker
Init(const art::DexFile & dex_file)173*795d594fSAndroid Build Coastguard Worker jvmtiError ArtClassDefinition::Init(const art::DexFile& dex_file) {
174*795d594fSAndroid Build Coastguard Worker if (dex_file.IsCompactDexFile()) {
175*795d594fSAndroid Build Coastguard Worker std::string error_msg;
176*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const art::DexFile>> dex_files;
177*795d594fSAndroid Build Coastguard Worker art::ArtDexFileLoader dex_file_loader(dex_file.GetLocation());
178*795d594fSAndroid Build Coastguard Worker if (!dex_file_loader.Open(/* verify= */ false,
179*795d594fSAndroid Build Coastguard Worker /* verify_checksum= */ false,
180*795d594fSAndroid Build Coastguard Worker &error_msg,
181*795d594fSAndroid Build Coastguard Worker &dex_files)) {
182*795d594fSAndroid Build Coastguard Worker return ERR(INTERNAL);
183*795d594fSAndroid Build Coastguard Worker }
184*795d594fSAndroid Build Coastguard Worker const std::vector<const art::OatDexFile*>& oat_dex_files =
185*795d594fSAndroid Build Coastguard Worker dex_file.GetOatDexFile()->GetOatFile()->GetOatDexFiles();
186*795d594fSAndroid Build Coastguard Worker const art::DexFile* original_dex_file = nullptr;
187*795d594fSAndroid Build Coastguard Worker for (uint32_t i = 0; i < oat_dex_files.size(); ++i) {
188*795d594fSAndroid Build Coastguard Worker if (dex_file.GetOatDexFile() == oat_dex_files[i]) {
189*795d594fSAndroid Build Coastguard Worker original_dex_file = dex_files[i].get();
190*795d594fSAndroid Build Coastguard Worker break;
191*795d594fSAndroid Build Coastguard Worker }
192*795d594fSAndroid Build Coastguard Worker }
193*795d594fSAndroid Build Coastguard Worker // Keep the dex_data alive.
194*795d594fSAndroid Build Coastguard Worker dex_data_memory_.resize(original_dex_file->SizeIncludingSharedData());
195*795d594fSAndroid Build Coastguard Worker memcpy(dex_data_memory_.data(), original_dex_file->Begin(), dex_data_memory_.size());
196*795d594fSAndroid Build Coastguard Worker dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
197*795d594fSAndroid Build Coastguard Worker
198*795d594fSAndroid Build Coastguard Worker // In case dex_data gets re-used for redefinition, keep the dex file live
199*795d594fSAndroid Build Coastguard Worker // with current_dex_memory.
200*795d594fSAndroid Build Coastguard Worker current_dex_memory_.resize(dex_data_.size());
201*795d594fSAndroid Build Coastguard Worker memcpy(current_dex_memory_.data(), dex_data_.data(), current_dex_memory_.size());
202*795d594fSAndroid Build Coastguard Worker current_dex_file_ = art::ArrayRef<const unsigned char>(current_dex_memory_);
203*795d594fSAndroid Build Coastguard Worker } else {
204*795d594fSAndroid Build Coastguard Worker // Dex file will always stay live, use it directly.
205*795d594fSAndroid Build Coastguard Worker dex_data_ =
206*795d594fSAndroid Build Coastguard Worker art::ArrayRef<const unsigned char>(dex_file.Begin(), dex_file.SizeIncludingSharedData());
207*795d594fSAndroid Build Coastguard Worker current_dex_file_ = dex_data_;
208*795d594fSAndroid Build Coastguard Worker }
209*795d594fSAndroid Build Coastguard Worker return OK;
210*795d594fSAndroid Build Coastguard Worker }
211*795d594fSAndroid Build Coastguard Worker
212*795d594fSAndroid Build Coastguard Worker } // namespace openjdkjvmti
213