blob: 7f2f80009aecda23cc608eb51af35463be922825 [file] [log] [blame]
Alex Lighta7e38d82017-01-19 14:57:28 -08001/* 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
Alex Lightb7354d52017-03-30 15:17:01 -070034#include "base/array_slice.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080035#include "class_linker-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080036#include "dex/dex_file.h"
Alex Lightb7354d52017-03-30 15:17:01 -070037#include "fixed_up_dex_file.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080038#include "handle.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "handle_scope-inl.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080040#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070041#include "mirror/class_ext.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080042#include "mirror/object-inl.h"
Alex Lightb7354d52017-03-30 15:17:01 -070043#include "reflection.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080044#include "thread.h"
45
46namespace openjdkjvmti {
47
Alex Light40528472017-03-28 09:07:36 -070048bool ArtClassDefinition::IsModified() const {
Alex Light64e4c142018-01-30 13:46:37 -080049 // RedefineClasses calls always are 'modified' since they need to change the current_dex_file of
Alex Light40528472017-03-28 09:07:36 -070050 // the class.
Alex Lightb7354d52017-03-30 15:17:01 -070051 if (redefined_) {
Alex Lighta7e38d82017-01-19 14:57:28 -080052 return true;
53 }
Alex Light64e4c142018-01-30 13:46:37 -080054
55 // Check to see if any change has taken place.
56 if (current_dex_file_.data() == dex_data_.data()) {
57 // no change at all.
58 return false;
59 }
60
Alex Lighta7e38d82017-01-19 14:57:28 -080061 // Check if the dex file we want to set is the same as the current one.
Alex Light40528472017-03-28 09:07:36 -070062 // Unfortunately we need to do this check even if no modifications have been done since it could
63 // be that agents were removed in the mean-time so we still have a different dex file. The dex
64 // checksum means this is likely to be fairly fast.
Alex Light64e4c142018-01-30 13:46:37 -080065 return current_dex_file_.size() != dex_data_.size() ||
66 memcmp(current_dex_file_.data(), dex_data_.data(), current_dex_file_.size()) != 0;
Alex Lightb7354d52017-03-30 15:17:01 -070067}
68
Alex Light64e4c142018-01-30 13:46:37 -080069jvmtiError ArtClassDefinition::InitCommon(art::Thread* self, jclass klass) {
70 art::ScopedObjectAccess soa(self);
Alex Lightb7354d52017-03-30 15:17:01 -070071 art::ObjPtr<art::mirror::Class> m_klass(soa.Decode<art::mirror::Class>(klass));
72 if (m_klass.IsNull()) {
73 return ERR(INVALID_CLASS);
74 }
Alex Light64e4c142018-01-30 13:46:37 -080075 initialized_ = true;
Alex Lightb7354d52017-03-30 15:17:01 -070076 klass_ = klass;
77 loader_ = soa.AddLocalReference<jobject>(m_klass->GetClassLoader());
78 std::string descriptor_store;
79 std::string descriptor(m_klass->GetDescriptor(&descriptor_store));
80 name_ = descriptor.substr(1, descriptor.size() - 2);
81 // Android doesn't really have protection domains.
82 protection_domain_ = nullptr;
83 return OK;
84}
85
Alex Light64e4c142018-01-30 13:46:37 -080086static void DequickenDexFile(const art::DexFile* dex_file,
87 const char* descriptor,
88 /*out*/std::vector<unsigned char>* dex_data)
89 REQUIRES_SHARED(art::Locks::mutator_lock_) {
90 std::unique_ptr<FixedUpDexFile> fixed_dex_file(FixedUpDexFile::Create(*dex_file, descriptor));
91 dex_data->resize(fixed_dex_file->Size());
92 memcpy(dex_data->data(), fixed_dex_file->Begin(), fixed_dex_file->Size());
93}
94
Alex Lightb7354d52017-03-30 15:17:01 -070095// Gets the data surrounding the given class.
Alex Light64e4c142018-01-30 13:46:37 -080096static void GetDexDataForRetransformation(art::Handle<art::mirror::Class> klass,
97 /*out*/std::vector<unsigned char>* dex_data)
Alex Lightb7354d52017-03-30 15:17:01 -070098 REQUIRES_SHARED(art::Locks::mutator_lock_) {
99 art::StackHandleScope<3> hs(art::Thread::Current());
100 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->GetExtData()));
101 const art::DexFile* dex_file = nullptr;
102 if (!ext.IsNull()) {
103 art::Handle<art::mirror::Object> orig_dex(hs.NewHandle(ext->GetOriginalDexFile()));
104 if (!orig_dex.IsNull()) {
105 if (orig_dex->IsArrayInstance()) {
106 DCHECK(orig_dex->GetClass()->GetComponentType()->IsPrimitiveByte());
107 art::Handle<art::mirror::ByteArray> orig_dex_bytes(
108 hs.NewHandle(art::down_cast<art::mirror::ByteArray*>(orig_dex->AsArray())));
Alex Light64e4c142018-01-30 13:46:37 -0800109 dex_data->resize(orig_dex_bytes->GetLength());
110 memcpy(dex_data->data(), orig_dex_bytes->GetData(), dex_data->size());
111 return;
Alex Lightb7354d52017-03-30 15:17:01 -0700112 } else if (orig_dex->IsDexCache()) {
113 dex_file = orig_dex->AsDexCache()->GetDexFile();
114 } else {
Alex Light0ecb2362017-04-12 09:01:47 -0700115 DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"))
116 << "Expected java/lang/Long but found object of type "
117 << orig_dex->GetClass()->PrettyClass();
Alex Lightb7354d52017-03-30 15:17:01 -0700118 art::ObjPtr<art::mirror::Class> prim_long_class(
119 art::Runtime::Current()->GetClassLinker()->GetClassRoot(
120 art::ClassLinker::kPrimitiveLong));
121 art::JValue val;
122 if (!art::UnboxPrimitiveForResult(orig_dex.Get(), prim_long_class, &val)) {
123 // This should never happen.
Alex Light64e4c142018-01-30 13:46:37 -0800124 LOG(FATAL) << "Unable to unbox a primitive long value!";
Alex Lightb7354d52017-03-30 15:17:01 -0700125 }
126 dex_file = reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ()));
127 }
128 }
129 }
130 if (dex_file == nullptr) {
131 dex_file = &klass->GetDexFile();
132 }
Mathieu Chartier75175552018-01-25 11:23:01 -0800133 std::string storage;
Alex Light64e4c142018-01-30 13:46:37 -0800134 DequickenDexFile(dex_file, klass->GetDescriptor(&storage), dex_data);
Alex Lightb7354d52017-03-30 15:17:01 -0700135}
136
Alex Light64e4c142018-01-30 13:46:37 -0800137static bool DexNeedsDequickening(art::Handle<art::mirror::Class> klass,
138 /*out*/ bool* from_class_ext)
139 REQUIRES_SHARED(art::Locks::mutator_lock_) {
140 art::ObjPtr<art::mirror::ClassExt> ext(klass->GetExtData());
141 if (ext.IsNull()) {
142 // We don't seem to have ever been redefined so be conservative and say we need de-quickening.
143 *from_class_ext = false;
144 return true;
145 }
146 art::ObjPtr<art::mirror::Object> orig_dex(ext->GetOriginalDexFile());
147 if (orig_dex.IsNull()) {
148 // We don't seem to have ever been redefined so be conservative and say we need de-quickening.
149 *from_class_ext = false;
150 return true;
151 } else if (!orig_dex->IsArrayInstance()) {
152 // We were redefined but the original is held in a dex-cache or dex file. This means that the
153 // original dex file is the one from the disk, which might be quickened.
154 DCHECK(orig_dex->IsDexCache() || orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"));
155 *from_class_ext = true;
156 return true;
157 } else {
158 // An array instance means the original-dex-file is from a redefineClasses which cannot have any
159 // quickening, so it's fine to use directly.
160 DCHECK(orig_dex->GetClass()->GetComponentType()->IsPrimitiveByte());
161 *from_class_ext = true;
162 return false;
163 }
164}
165
166static const art::DexFile* GetQuickenedDexFile(art::Handle<art::mirror::Class> klass)
167 REQUIRES_SHARED(art::Locks::mutator_lock_) {
168 art::ObjPtr<art::mirror::ClassExt> ext(klass->GetExtData());
169 if (ext.IsNull() || ext->GetOriginalDexFile() == nullptr) {
170 return &klass->GetDexFile();
171 }
172
173 art::ObjPtr<art::mirror::Object> orig_dex(ext->GetOriginalDexFile());
174 DCHECK(!orig_dex->IsArrayInstance());
175 if (orig_dex->IsDexCache()) {
176 return orig_dex->AsDexCache()->GetDexFile();
177 }
178
179 DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"))
180 << "Expected java/lang/Long but found object of type "
181 << orig_dex->GetClass()->PrettyClass();
182 art::ObjPtr<art::mirror::Class> prim_long_class(
183 art::Runtime::Current()->GetClassLinker()->GetClassRoot(
184 art::ClassLinker::kPrimitiveLong));
185 art::JValue val;
186 if (!art::UnboxPrimitiveForResult(orig_dex.Ptr(), prim_long_class, &val)) {
187 LOG(FATAL) << "Unable to unwrap a long value!";
188 }
189 return reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ()));
190}
191
192template<typename GetOriginalDexFile>
193void ArtClassDefinition::InitWithDex(GetOriginalDexFile get_original,
194 const art::DexFile* quick_dex) {
195 art::Thread* self = art::Thread::Current();
196 DCHECK(quick_dex != nullptr);
197 get_original(/*out*/&dex_data_memory_);
198 dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
199 if (from_class_ext_) {
200 // We got initial from class_ext so the current one must have undergone redefinition so no
201 // cdex or quickening stuff.
202 // We can only do this if it's not a first load.
203 DCHECK(klass_ != nullptr);
204 const art::DexFile& cur_dex = self->DecodeJObject(klass_)->AsClass()->GetDexFile();
205 current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
206 } else {
207 // No redefinition must have ever happened so the (dequickened) cur_dex is the same as the
208 // initial dex_data. We need to copy it into another buffer to keep it around if we have a
209 // real redefinition.
210 current_dex_memory_.resize(dex_data_.size());
211 memcpy(current_dex_memory_.data(), dex_data_.data(), current_dex_memory_.size());
212 current_dex_file_ = art::ArrayRef<const unsigned char>(current_dex_memory_);
213 }
214}
215
216jvmtiError ArtClassDefinition::Init(art::Thread* self, jclass klass) {
217 jvmtiError res = InitCommon(self, klass);
Alex Lightb7354d52017-03-30 15:17:01 -0700218 if (res != OK) {
219 return res;
220 }
Alex Lightb7354d52017-03-30 15:17:01 -0700221 art::ScopedObjectAccess soa(self);
222 art::StackHandleScope<1> hs(self);
223 art::Handle<art::mirror::Class> m_klass(hs.NewHandle(self->DecodeJObject(klass)->AsClass()));
Alex Light64e4c142018-01-30 13:46:37 -0800224 if (!DexNeedsDequickening(m_klass, &from_class_ext_)) {
225 // We don't need to do any dequickening. We want to copy the data just so we don't need to deal
226 // with the GC moving it around.
227 art::ObjPtr<art::mirror::ByteArray> orig_dex(
228 m_klass->GetExtData()->GetOriginalDexFile()->AsByteArray());
229 dex_data_memory_.resize(orig_dex->GetLength());
230 memcpy(dex_data_memory_.data(), orig_dex->GetData(), dex_data_memory_.size());
231 dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
232
233 // Since we are here we must not have any quickened instructions since we were redefined.
234 const art::DexFile& cur_dex = m_klass->GetDexFile();
235 DCHECK(from_class_ext_);
236 current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
237 return OK;
Alex Lightb7354d52017-03-30 15:17:01 -0700238 }
Alex Light64e4c142018-01-30 13:46:37 -0800239
240 // We need to dequicken stuff. This is often super slow (10's of ms). Instead we will do it
241 // dynamically.
242 const art::DexFile* quick_dex = GetQuickenedDexFile(m_klass);
243 auto get_original = [&](/*out*/std::vector<unsigned char>* dex_data)
244 REQUIRES_SHARED(art::Locks::mutator_lock_) {
245 GetDexDataForRetransformation(m_klass, dex_data);
246 };
247 InitWithDex(get_original, quick_dex);
248 return OK;
Alex Lightb7354d52017-03-30 15:17:01 -0700249}
250
Alex Light64e4c142018-01-30 13:46:37 -0800251jvmtiError ArtClassDefinition::Init(art::Thread* self, const jvmtiClassDefinition& def) {
252 jvmtiError res = InitCommon(self, def.klass);
Alex Lightb7354d52017-03-30 15:17:01 -0700253 if (res != OK) {
254 return res;
255 }
Alex Light64e4c142018-01-30 13:46:37 -0800256 // We are being directly redefined.
Alex Lightb7354d52017-03-30 15:17:01 -0700257 redefined_ = true;
Alex Light64e4c142018-01-30 13:46:37 -0800258 current_dex_file_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
259 dex_data_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
260 return OK;
261}
262
263void ArtClassDefinition::InitFirstLoad(const char* descriptor,
264 art::Handle<art::mirror::ClassLoader> klass_loader,
265 const art::DexFile& dex_file) {
266 art::Thread* self = art::Thread::Current();
267 art::ScopedObjectAccess soa(self);
268 initialized_ = true;
269 // No Class
270 klass_ = nullptr;
271 loader_ = soa.AddLocalReference<jobject>(klass_loader.Get());
272 std::string descriptor_str(descriptor);
273 name_ = descriptor_str.substr(1, descriptor_str.size() - 2);
274 // Android doesn't really have protection domains.
275 protection_domain_ = nullptr;
276 auto get_original = [&](/*out*/std::vector<unsigned char>* dex_data)
277 REQUIRES_SHARED(art::Locks::mutator_lock_) {
278 DequickenDexFile(&dex_file, descriptor, dex_data);
279 };
280 InitWithDex(get_original, &dex_file);
Alex Lighta7e38d82017-01-19 14:57:28 -0800281}
282
283} // namespace openjdkjvmti