• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2016 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 "ti_class_definition.h"
33 
34 #include "base/array_slice.h"
35 #include "base/logging.h"
36 #include "class_linker-inl.h"
37 #include "class_root-inl.h"
38 #include "dex/dex_file.h"
39 #include "dex/art_dex_file_loader.h"
40 #include "handle.h"
41 #include "handle_scope-inl.h"
42 #include "mirror/class-inl.h"
43 #include "mirror/class_ext-inl.h"
44 #include "mirror/object-inl.h"
45 #include "reflection.h"
46 #include "thread.h"
47 
48 namespace openjdkjvmti {
49 
IsModified() const50 bool ArtClassDefinition::IsModified() const {
51   // RedefineClasses calls always are 'modified' since they need to change the current_dex_file of
52   // the class.
53   if (redefined_) {
54     return true;
55   }
56 
57   // Check to see if any change has taken place.
58   if (current_dex_file_.data() == dex_data_.data()) {
59     // no change at all.
60     return false;
61   }
62 
63   // Check if the dex file we want to set is the same as the current one.
64   // Unfortunately we need to do this check even if no modifications have been done since it could
65   // be that agents were removed in the mean-time so we still have a different dex file. The dex
66   // checksum means this is likely to be fairly fast.
67   return current_dex_file_.size() != dex_data_.size() ||
68       memcmp(current_dex_file_.data(), dex_data_.data(), current_dex_file_.size()) != 0;
69 }
70 
InitCommon(art::Thread * self,jclass klass)71 jvmtiError ArtClassDefinition::InitCommon(art::Thread* self, jclass klass) {
72   art::ScopedObjectAccess soa(self);
73   art::ObjPtr<art::mirror::Class> m_klass(soa.Decode<art::mirror::Class>(klass));
74   if (m_klass.IsNull()) {
75     return ERR(INVALID_CLASS);
76   }
77   initialized_ = true;
78   klass_ = klass;
79   loader_ = soa.AddLocalReference<jobject>(m_klass->GetClassLoader());
80   std::string descriptor_store;
81   std::string descriptor(m_klass->GetDescriptor(&descriptor_store));
82   name_ = descriptor.substr(1, descriptor.size() - 2);
83   // Android doesn't really have protection domains.
84   protection_domain_ = nullptr;
85   return OK;
86 }
87 
Init(art::Thread * self,jclass klass)88 jvmtiError ArtClassDefinition::Init(art::Thread* self, jclass klass) {
89   jvmtiError res = InitCommon(self, klass);
90   if (res != OK) {
91     return res;
92   }
93   art::ScopedObjectAccess soa(self);
94   art::StackHandleScope<1> hs(self);
95   art::Handle<art::mirror::Class> m_klass(hs.NewHandle(self->DecodeJObject(klass)->AsClass()));
96   art::ObjPtr<art::mirror::ClassExt> ext(m_klass->GetExtData());
97   if (!ext.IsNull()) {
98     art::ObjPtr<art::mirror::Object> orig_dex(ext->GetOriginalDexFile());
99     if (!orig_dex.IsNull()) {
100       if (orig_dex->IsArrayInstance()) {
101         // An array instance means the original-dex-file is from a redefineClasses which cannot have any
102         // compact dex, so it's fine to use directly.
103         art::ObjPtr<art::mirror::ByteArray> byte_array(orig_dex->AsByteArray());
104         dex_data_memory_.resize(byte_array->GetLength());
105         memcpy(dex_data_memory_.data(), byte_array->GetData(), dex_data_memory_.size());
106         dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
107 
108         const art::DexFile& cur_dex = m_klass->GetDexFile();
109         current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
110         return OK;
111       }
112 
113       if (orig_dex->IsDexCache()) {
114         res = Init(*orig_dex->AsDexCache()->GetDexFile());
115         if (res != OK) {
116           return res;
117         }
118       } else {
119         DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"))
120             << "Expected java/lang/Long but found object of type "
121             << orig_dex->GetClass()->PrettyClass();
122         art::ObjPtr<art::mirror::Class> prim_long_class(
123             art::GetClassRoot(art::ClassRoot::kPrimitiveLong));
124         art::JValue val;
125         if (!art::UnboxPrimitiveForResult(orig_dex.Ptr(), prim_long_class, &val)) {
126           // This should never happen.
127           LOG(FATAL) << "Unable to unbox a primitive long value!";
128         }
129         res = Init(*reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ())));
130         if (res != OK) {
131           return res;
132         }
133       }
134       const art::DexFile& cur_dex = m_klass->GetDexFile();
135       current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
136       return OK;
137     }
138   }
139   // No redefinition must have ever happened so we can use the class's dex file.
140   return Init(m_klass->GetDexFile());
141 }
142 
Init(art::Thread * self,const jvmtiClassDefinition & def)143 jvmtiError ArtClassDefinition::Init(art::Thread* self, const jvmtiClassDefinition& def) {
144   jvmtiError res = InitCommon(self, def.klass);
145   if (res != OK) {
146     return res;
147   }
148   // We are being directly redefined.
149   redefined_ = true;
150   current_dex_file_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
151   dex_data_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
152   return OK;
153 }
154 
InitFirstLoad(const char * descriptor,art::Handle<art::mirror::ClassLoader> klass_loader,const art::DexFile & dex_file)155 jvmtiError ArtClassDefinition::InitFirstLoad(const char* descriptor,
156                                              art::Handle<art::mirror::ClassLoader> klass_loader,
157                                              const art::DexFile& dex_file) {
158   art::Thread* self = art::Thread::Current();
159   art::ScopedObjectAccess soa(self);
160   initialized_ = true;
161   // No Class
162   klass_ = nullptr;
163   loader_ = soa.AddLocalReference<jobject>(klass_loader.Get());
164   std::string descriptor_str(descriptor);
165   name_ = descriptor_str.substr(1, descriptor_str.size() - 2);
166   // Android doesn't really have protection domains.
167   protection_domain_ = nullptr;
168   return Init(dex_file);
169 }
170 
Init(const art::DexFile & dex_file)171 jvmtiError ArtClassDefinition::Init(const art::DexFile& dex_file) {
172   if (dex_file.IsCompactDexFile()) {
173     std::string error_msg;
174     std::vector<std::unique_ptr<const art::DexFile>> dex_files;
175     art::ArtDexFileLoader dex_file_loader(dex_file.GetLocation());
176     if (!dex_file_loader.Open(/* verify= */ false,
177                               /* verify_checksum= */ false,
178                               &error_msg,
179                               &dex_files)) {
180       return ERR(INTERNAL);
181     }
182     const std::vector<const art::OatDexFile*>& oat_dex_files =
183         dex_file.GetOatDexFile()->GetOatFile()->GetOatDexFiles();
184     const art::DexFile* original_dex_file = nullptr;
185     for (uint32_t i = 0; i < oat_dex_files.size(); ++i) {
186       if (dex_file.GetOatDexFile() == oat_dex_files[i]) {
187         original_dex_file = dex_files[i].get();
188         break;
189       }
190     }
191     // Keep the dex_data alive.
192     dex_data_memory_.resize(original_dex_file->Size());
193     memcpy(dex_data_memory_.data(), original_dex_file->Begin(), original_dex_file->Size());
194     dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
195 
196     // In case dex_data gets re-used for redefinition, keep the dex file live
197     // with current_dex_memory.
198     current_dex_memory_.resize(dex_data_.size());
199     memcpy(current_dex_memory_.data(), dex_data_.data(), current_dex_memory_.size());
200     current_dex_file_ = art::ArrayRef<const unsigned char>(current_dex_memory_);
201   } else {
202     // Dex file will always stay live, use it directly.
203     dex_data_ = art::ArrayRef<const unsigned char>(dex_file.Begin(), dex_file.Size());
204     current_dex_file_ = dex_data_;
205   }
206   return OK;
207 }
208 
209 }  // namespace openjdkjvmti
210