blob: dce2733e7e6ec98dbf280eae2c7a20e733c42497 [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"
Vladimir Markob4eb1b12018-05-24 11:09:38 +010036#include "class_root.h"
David Sehr9e734c72018-01-04 17:56:19 -080037#include "dex/dex_file.h"
Alex Lightb7354d52017-03-30 15:17:01 -070038#include "fixed_up_dex_file.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080039#include "handle.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "handle_scope-inl.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080041#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070042#include "mirror/class_ext.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080043#include "mirror/object-inl.h"
Alex Lightb7354d52017-03-30 15:17:01 -070044#include "reflection.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080045#include "thread.h"
46
47namespace openjdkjvmti {
48
Alex Lightca97ada2018-02-02 09:25:31 -080049void ArtClassDefinition::InitializeMemory() const {
50 DCHECK(art::MemMap::kCanReplaceMapping);
51 VLOG(signals) << "Initializing de-quickened memory for dex file of " << name_;
52 CHECK(dex_data_mmap_ != nullptr);
53 CHECK(temp_mmap_ != nullptr);
54 CHECK_EQ(dex_data_mmap_->GetProtect(), PROT_NONE);
55 CHECK_EQ(temp_mmap_->GetProtect(), PROT_READ | PROT_WRITE);
56
57 std::string desc = std::string("L") + name_ + ";";
58 std::unique_ptr<FixedUpDexFile>
59 fixed_dex_file(FixedUpDexFile::Create(*initial_dex_file_unquickened_, desc.c_str()));
60 CHECK(fixed_dex_file.get() != nullptr);
61 CHECK_LE(fixed_dex_file->Size(), temp_mmap_->Size());
62 CHECK_EQ(temp_mmap_->Size(), dex_data_mmap_->Size());
63 // Copy the data to the temp mmap.
64 memcpy(temp_mmap_->Begin(), fixed_dex_file->Begin(), fixed_dex_file->Size());
65
66 // Move the mmap atomically.
67 art::MemMap* source = temp_mmap_.release();
68 std::string error;
69 CHECK(dex_data_mmap_->ReplaceWith(&source, &error)) << "Failed to replace mmap for "
70 << name_ << " because " << error;
71 CHECK(dex_data_mmap_->Protect(PROT_READ));
72}
73
Alex Light40528472017-03-28 09:07:36 -070074bool ArtClassDefinition::IsModified() const {
Alex Light64e4c142018-01-30 13:46:37 -080075 // RedefineClasses calls always are 'modified' since they need to change the current_dex_file of
Alex Light40528472017-03-28 09:07:36 -070076 // the class.
Alex Lightb7354d52017-03-30 15:17:01 -070077 if (redefined_) {
Alex Lighta7e38d82017-01-19 14:57:28 -080078 return true;
79 }
Alex Light64e4c142018-01-30 13:46:37 -080080
81 // Check to see if any change has taken place.
82 if (current_dex_file_.data() == dex_data_.data()) {
83 // no change at all.
84 return false;
85 }
86
Alex Lightca97ada2018-02-02 09:25:31 -080087 // The dex_data_ was never touched by the agents.
88 if (dex_data_mmap_ != nullptr && dex_data_mmap_->GetProtect() == PROT_NONE) {
89 if (current_dex_file_.data() == dex_data_mmap_->Begin()) {
90 // the dex_data_ looks like it changed (not equal to current_dex_file_) but we never
91 // initialized the dex_data_mmap_. This means the new_dex_data was filled in without looking
92 // at the initial dex_data_.
93 return true;
94 } else if (dex_data_.data() == dex_data_mmap_->Begin()) {
95 // The dex file used to have modifications but they were not added again.
96 return true;
97 } else {
98 // It's not clear what happened. It's possible that the agent got the current dex file data
99 // from some other source so we need to initialize everything to see if it is the same.
100 VLOG(signals) << "Lazy dex file for " << name_ << " was never touched but the dex_data_ is "
101 << "changed! Need to initialize the memory to see if anything changed";
102 InitializeMemory();
103 }
104 }
105
106 // We can definitely read current_dex_file_ and dex_file_ without causing page faults.
107
Alex Lighta7e38d82017-01-19 14:57:28 -0800108 // Check if the dex file we want to set is the same as the current one.
Alex Light40528472017-03-28 09:07:36 -0700109 // Unfortunately we need to do this check even if no modifications have been done since it could
110 // be that agents were removed in the mean-time so we still have a different dex file. The dex
111 // checksum means this is likely to be fairly fast.
Alex Light64e4c142018-01-30 13:46:37 -0800112 return current_dex_file_.size() != dex_data_.size() ||
113 memcmp(current_dex_file_.data(), dex_data_.data(), current_dex_file_.size()) != 0;
Alex Lightb7354d52017-03-30 15:17:01 -0700114}
115
Alex Light64e4c142018-01-30 13:46:37 -0800116jvmtiError ArtClassDefinition::InitCommon(art::Thread* self, jclass klass) {
117 art::ScopedObjectAccess soa(self);
Alex Lightb7354d52017-03-30 15:17:01 -0700118 art::ObjPtr<art::mirror::Class> m_klass(soa.Decode<art::mirror::Class>(klass));
119 if (m_klass.IsNull()) {
120 return ERR(INVALID_CLASS);
121 }
Alex Light64e4c142018-01-30 13:46:37 -0800122 initialized_ = true;
Alex Lightb7354d52017-03-30 15:17:01 -0700123 klass_ = klass;
124 loader_ = soa.AddLocalReference<jobject>(m_klass->GetClassLoader());
125 std::string descriptor_store;
126 std::string descriptor(m_klass->GetDescriptor(&descriptor_store));
127 name_ = descriptor.substr(1, descriptor.size() - 2);
128 // Android doesn't really have protection domains.
129 protection_domain_ = nullptr;
130 return OK;
131}
132
Alex Light64e4c142018-01-30 13:46:37 -0800133static void DequickenDexFile(const art::DexFile* dex_file,
134 const char* descriptor,
135 /*out*/std::vector<unsigned char>* dex_data)
136 REQUIRES_SHARED(art::Locks::mutator_lock_) {
137 std::unique_ptr<FixedUpDexFile> fixed_dex_file(FixedUpDexFile::Create(*dex_file, descriptor));
138 dex_data->resize(fixed_dex_file->Size());
139 memcpy(dex_data->data(), fixed_dex_file->Begin(), fixed_dex_file->Size());
140}
141
Alex Lightb7354d52017-03-30 15:17:01 -0700142// Gets the data surrounding the given class.
Alex Light64e4c142018-01-30 13:46:37 -0800143static void GetDexDataForRetransformation(art::Handle<art::mirror::Class> klass,
144 /*out*/std::vector<unsigned char>* dex_data)
Alex Lightb7354d52017-03-30 15:17:01 -0700145 REQUIRES_SHARED(art::Locks::mutator_lock_) {
146 art::StackHandleScope<3> hs(art::Thread::Current());
147 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->GetExtData()));
148 const art::DexFile* dex_file = nullptr;
149 if (!ext.IsNull()) {
150 art::Handle<art::mirror::Object> orig_dex(hs.NewHandle(ext->GetOriginalDexFile()));
151 if (!orig_dex.IsNull()) {
152 if (orig_dex->IsArrayInstance()) {
153 DCHECK(orig_dex->GetClass()->GetComponentType()->IsPrimitiveByte());
154 art::Handle<art::mirror::ByteArray> orig_dex_bytes(
155 hs.NewHandle(art::down_cast<art::mirror::ByteArray*>(orig_dex->AsArray())));
Alex Light64e4c142018-01-30 13:46:37 -0800156 dex_data->resize(orig_dex_bytes->GetLength());
157 memcpy(dex_data->data(), orig_dex_bytes->GetData(), dex_data->size());
158 return;
Alex Lightb7354d52017-03-30 15:17:01 -0700159 } else if (orig_dex->IsDexCache()) {
160 dex_file = orig_dex->AsDexCache()->GetDexFile();
161 } else {
Alex Light0ecb2362017-04-12 09:01:47 -0700162 DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"))
163 << "Expected java/lang/Long but found object of type "
164 << orig_dex->GetClass()->PrettyClass();
Alex Lightb7354d52017-03-30 15:17:01 -0700165 art::ObjPtr<art::mirror::Class> prim_long_class(
Vladimir Markob4eb1b12018-05-24 11:09:38 +0100166 art::GetClassRoot(art::ClassRoot::kPrimitiveLong));
Alex Lightb7354d52017-03-30 15:17:01 -0700167 art::JValue val;
168 if (!art::UnboxPrimitiveForResult(orig_dex.Get(), prim_long_class, &val)) {
169 // This should never happen.
Alex Light64e4c142018-01-30 13:46:37 -0800170 LOG(FATAL) << "Unable to unbox a primitive long value!";
Alex Lightb7354d52017-03-30 15:17:01 -0700171 }
172 dex_file = reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ()));
173 }
174 }
175 }
176 if (dex_file == nullptr) {
177 dex_file = &klass->GetDexFile();
178 }
Mathieu Chartier75175552018-01-25 11:23:01 -0800179 std::string storage;
Alex Light64e4c142018-01-30 13:46:37 -0800180 DequickenDexFile(dex_file, klass->GetDescriptor(&storage), dex_data);
Alex Lightb7354d52017-03-30 15:17:01 -0700181}
182
Alex Light64e4c142018-01-30 13:46:37 -0800183static bool DexNeedsDequickening(art::Handle<art::mirror::Class> klass,
184 /*out*/ bool* from_class_ext)
185 REQUIRES_SHARED(art::Locks::mutator_lock_) {
186 art::ObjPtr<art::mirror::ClassExt> ext(klass->GetExtData());
187 if (ext.IsNull()) {
188 // We don't seem to have ever been redefined so be conservative and say we need de-quickening.
189 *from_class_ext = false;
190 return true;
191 }
192 art::ObjPtr<art::mirror::Object> orig_dex(ext->GetOriginalDexFile());
193 if (orig_dex.IsNull()) {
194 // We don't seem to have ever been redefined so be conservative and say we need de-quickening.
195 *from_class_ext = false;
196 return true;
197 } else if (!orig_dex->IsArrayInstance()) {
198 // We were redefined but the original is held in a dex-cache or dex file. This means that the
199 // original dex file is the one from the disk, which might be quickened.
200 DCHECK(orig_dex->IsDexCache() || orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"));
201 *from_class_ext = true;
202 return true;
203 } else {
204 // An array instance means the original-dex-file is from a redefineClasses which cannot have any
205 // quickening, so it's fine to use directly.
206 DCHECK(orig_dex->GetClass()->GetComponentType()->IsPrimitiveByte());
207 *from_class_ext = true;
208 return false;
209 }
210}
211
212static const art::DexFile* GetQuickenedDexFile(art::Handle<art::mirror::Class> klass)
213 REQUIRES_SHARED(art::Locks::mutator_lock_) {
214 art::ObjPtr<art::mirror::ClassExt> ext(klass->GetExtData());
215 if (ext.IsNull() || ext->GetOriginalDexFile() == nullptr) {
216 return &klass->GetDexFile();
217 }
218
219 art::ObjPtr<art::mirror::Object> orig_dex(ext->GetOriginalDexFile());
220 DCHECK(!orig_dex->IsArrayInstance());
221 if (orig_dex->IsDexCache()) {
222 return orig_dex->AsDexCache()->GetDexFile();
223 }
224
225 DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"))
226 << "Expected java/lang/Long but found object of type "
227 << orig_dex->GetClass()->PrettyClass();
228 art::ObjPtr<art::mirror::Class> prim_long_class(
Vladimir Markob4eb1b12018-05-24 11:09:38 +0100229 art::GetClassRoot(art::ClassRoot::kPrimitiveLong));
Alex Light64e4c142018-01-30 13:46:37 -0800230 art::JValue val;
231 if (!art::UnboxPrimitiveForResult(orig_dex.Ptr(), prim_long_class, &val)) {
232 LOG(FATAL) << "Unable to unwrap a long value!";
233 }
234 return reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ()));
235}
236
237template<typename GetOriginalDexFile>
238void ArtClassDefinition::InitWithDex(GetOriginalDexFile get_original,
239 const art::DexFile* quick_dex) {
240 art::Thread* self = art::Thread::Current();
241 DCHECK(quick_dex != nullptr);
Alex Lightca97ada2018-02-02 09:25:31 -0800242 if (art::MemMap::kCanReplaceMapping && kEnableOnDemandDexDequicken) {
243 size_t dequick_size = quick_dex->GetDequickenedSize();
244 std::string mmap_name("anon-mmap-for-redefine: ");
245 mmap_name += name_;
246 std::string error;
247 dex_data_mmap_.reset(art::MemMap::MapAnonymous(mmap_name.c_str(),
248 nullptr,
249 dequick_size,
250 PROT_NONE,
251 /*low_4gb*/ false,
252 /*reuse*/ false,
253 &error));
254 mmap_name += "-TEMP";
255 temp_mmap_.reset(art::MemMap::MapAnonymous(mmap_name.c_str(),
256 nullptr,
257 dequick_size,
258 PROT_READ | PROT_WRITE,
259 /*low_4gb*/ false,
260 /*reuse*/ false,
261 &error));
262 if (UNLIKELY(dex_data_mmap_ != nullptr && temp_mmap_ != nullptr)) {
263 // Need to save the initial dexfile so we don't need to search for it in the fault-handler.
264 initial_dex_file_unquickened_ = quick_dex;
265 dex_data_ = art::ArrayRef<const unsigned char>(dex_data_mmap_->Begin(),
266 dex_data_mmap_->Size());
267 if (from_class_ext_) {
268 // We got initial from class_ext so the current one must have undergone redefinition so no
269 // cdex or quickening stuff.
270 // We can only do this if it's not a first load.
271 DCHECK(klass_ != nullptr);
272 const art::DexFile& cur_dex = self->DecodeJObject(klass_)->AsClass()->GetDexFile();
273 current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
274 } else {
275 // This class hasn't been redefined before. The dequickened current data is the same as the
276 // dex_data_mmap_ when it's filled it. We don't need to copy anything because the mmap will
277 // not be cleared until after everything is done.
278 current_dex_file_ = art::ArrayRef<const unsigned char>(dex_data_mmap_->Begin(),
279 dequick_size);
280 }
281 return;
282 }
283 }
284 dex_data_mmap_.reset(nullptr);
285 temp_mmap_.reset(nullptr);
286 // Failed to mmap a large enough area (or on-demand dequickening was disabled). This is
287 // unfortunate. Since currently the size is just a guess though we might as well try to do it
288 // manually.
Alex Light64e4c142018-01-30 13:46:37 -0800289 get_original(/*out*/&dex_data_memory_);
290 dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
291 if (from_class_ext_) {
292 // We got initial from class_ext so the current one must have undergone redefinition so no
293 // cdex or quickening stuff.
294 // We can only do this if it's not a first load.
295 DCHECK(klass_ != nullptr);
296 const art::DexFile& cur_dex = self->DecodeJObject(klass_)->AsClass()->GetDexFile();
297 current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
298 } else {
299 // No redefinition must have ever happened so the (dequickened) cur_dex is the same as the
300 // initial dex_data. We need to copy it into another buffer to keep it around if we have a
301 // real redefinition.
302 current_dex_memory_.resize(dex_data_.size());
303 memcpy(current_dex_memory_.data(), dex_data_.data(), current_dex_memory_.size());
304 current_dex_file_ = art::ArrayRef<const unsigned char>(current_dex_memory_);
305 }
306}
307
308jvmtiError ArtClassDefinition::Init(art::Thread* self, jclass klass) {
309 jvmtiError res = InitCommon(self, klass);
Alex Lightb7354d52017-03-30 15:17:01 -0700310 if (res != OK) {
311 return res;
312 }
Alex Lightb7354d52017-03-30 15:17:01 -0700313 art::ScopedObjectAccess soa(self);
314 art::StackHandleScope<1> hs(self);
315 art::Handle<art::mirror::Class> m_klass(hs.NewHandle(self->DecodeJObject(klass)->AsClass()));
Alex Light64e4c142018-01-30 13:46:37 -0800316 if (!DexNeedsDequickening(m_klass, &from_class_ext_)) {
317 // We don't need to do any dequickening. We want to copy the data just so we don't need to deal
318 // with the GC moving it around.
319 art::ObjPtr<art::mirror::ByteArray> orig_dex(
320 m_klass->GetExtData()->GetOriginalDexFile()->AsByteArray());
321 dex_data_memory_.resize(orig_dex->GetLength());
322 memcpy(dex_data_memory_.data(), orig_dex->GetData(), dex_data_memory_.size());
323 dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
324
325 // Since we are here we must not have any quickened instructions since we were redefined.
326 const art::DexFile& cur_dex = m_klass->GetDexFile();
327 DCHECK(from_class_ext_);
328 current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
329 return OK;
Alex Lightb7354d52017-03-30 15:17:01 -0700330 }
Alex Light64e4c142018-01-30 13:46:37 -0800331
332 // We need to dequicken stuff. This is often super slow (10's of ms). Instead we will do it
333 // dynamically.
334 const art::DexFile* quick_dex = GetQuickenedDexFile(m_klass);
335 auto get_original = [&](/*out*/std::vector<unsigned char>* dex_data)
336 REQUIRES_SHARED(art::Locks::mutator_lock_) {
337 GetDexDataForRetransformation(m_klass, dex_data);
338 };
339 InitWithDex(get_original, quick_dex);
340 return OK;
Alex Lightb7354d52017-03-30 15:17:01 -0700341}
342
Alex Light64e4c142018-01-30 13:46:37 -0800343jvmtiError ArtClassDefinition::Init(art::Thread* self, const jvmtiClassDefinition& def) {
344 jvmtiError res = InitCommon(self, def.klass);
Alex Lightb7354d52017-03-30 15:17:01 -0700345 if (res != OK) {
346 return res;
347 }
Alex Light64e4c142018-01-30 13:46:37 -0800348 // We are being directly redefined.
Alex Lightb7354d52017-03-30 15:17:01 -0700349 redefined_ = true;
Alex Light64e4c142018-01-30 13:46:37 -0800350 current_dex_file_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
351 dex_data_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
352 return OK;
353}
354
355void ArtClassDefinition::InitFirstLoad(const char* descriptor,
356 art::Handle<art::mirror::ClassLoader> klass_loader,
357 const art::DexFile& dex_file) {
358 art::Thread* self = art::Thread::Current();
359 art::ScopedObjectAccess soa(self);
360 initialized_ = true;
361 // No Class
362 klass_ = nullptr;
363 loader_ = soa.AddLocalReference<jobject>(klass_loader.Get());
364 std::string descriptor_str(descriptor);
365 name_ = descriptor_str.substr(1, descriptor_str.size() - 2);
366 // Android doesn't really have protection domains.
367 protection_domain_ = nullptr;
368 auto get_original = [&](/*out*/std::vector<unsigned char>* dex_data)
369 REQUIRES_SHARED(art::Locks::mutator_lock_) {
370 DequickenDexFile(&dex_file, descriptor, dex_data);
371 };
372 InitWithDex(get_original, &dex_file);
Alex Lighta7e38d82017-01-19 14:57:28 -0800373}
374
375} // namespace openjdkjvmti