blob: 895e73450e1649f6b7c20afe28900757f8fc0804 [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_;
Vladimir Markoc34bebf2018-08-16 16:12:49 +010052 CHECK(dex_data_mmap_.IsValid());
53 CHECK(temp_mmap_.IsValid());
54 CHECK_EQ(dex_data_mmap_.GetProtect(), PROT_NONE);
55 CHECK_EQ(temp_mmap_.GetProtect(), PROT_READ | PROT_WRITE);
Alex Lightca97ada2018-02-02 09:25:31 -080056
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);
Vladimir Markoc34bebf2018-08-16 16:12:49 +010061 CHECK_LE(fixed_dex_file->Size(), temp_mmap_.Size());
62 CHECK_EQ(temp_mmap_.Size(), dex_data_mmap_.Size());
Alex Lightca97ada2018-02-02 09:25:31 -080063 // Copy the data to the temp mmap.
Vladimir Markoc34bebf2018-08-16 16:12:49 +010064 memcpy(temp_mmap_.Begin(), fixed_dex_file->Begin(), fixed_dex_file->Size());
Alex Lightca97ada2018-02-02 09:25:31 -080065
66 // Move the mmap atomically.
Vladimir Markoc34bebf2018-08-16 16:12:49 +010067 art::MemMap source;
68 source.swap(temp_mmap_);
Alex Lightca97ada2018-02-02 09:25:31 -080069 std::string error;
Vladimir Markoc34bebf2018-08-16 16:12:49 +010070 CHECK(dex_data_mmap_.ReplaceWith(&source, &error)) << "Failed to replace mmap for "
71 << name_ << " because " << error;
72 CHECK(dex_data_mmap_.Protect(PROT_READ));
Alex Lightca97ada2018-02-02 09:25:31 -080073}
74
Alex Light40528472017-03-28 09:07:36 -070075bool ArtClassDefinition::IsModified() const {
Alex Light64e4c142018-01-30 13:46:37 -080076 // RedefineClasses calls always are 'modified' since they need to change the current_dex_file of
Alex Light40528472017-03-28 09:07:36 -070077 // the class.
Alex Lightb7354d52017-03-30 15:17:01 -070078 if (redefined_) {
Alex Lighta7e38d82017-01-19 14:57:28 -080079 return true;
80 }
Alex Light64e4c142018-01-30 13:46:37 -080081
82 // Check to see if any change has taken place.
83 if (current_dex_file_.data() == dex_data_.data()) {
84 // no change at all.
85 return false;
86 }
87
Alex Lightca97ada2018-02-02 09:25:31 -080088 // The dex_data_ was never touched by the agents.
Vladimir Markoc34bebf2018-08-16 16:12:49 +010089 if (dex_data_mmap_.IsValid() && dex_data_mmap_.GetProtect() == PROT_NONE) {
90 if (current_dex_file_.data() == dex_data_mmap_.Begin()) {
Alex Lightca97ada2018-02-02 09:25:31 -080091 // the dex_data_ looks like it changed (not equal to current_dex_file_) but we never
92 // initialized the dex_data_mmap_. This means the new_dex_data was filled in without looking
93 // at the initial dex_data_.
94 return true;
Vladimir Markoc34bebf2018-08-16 16:12:49 +010095 } else if (dex_data_.data() == dex_data_mmap_.Begin()) {
Alex Lightca97ada2018-02-02 09:25:31 -080096 // The dex file used to have modifications but they were not added again.
97 return true;
98 } else {
99 // It's not clear what happened. It's possible that the agent got the current dex file data
100 // from some other source so we need to initialize everything to see if it is the same.
101 VLOG(signals) << "Lazy dex file for " << name_ << " was never touched but the dex_data_ is "
102 << "changed! Need to initialize the memory to see if anything changed";
103 InitializeMemory();
104 }
105 }
106
107 // We can definitely read current_dex_file_ and dex_file_ without causing page faults.
108
Alex Lighta7e38d82017-01-19 14:57:28 -0800109 // Check if the dex file we want to set is the same as the current one.
Alex Light40528472017-03-28 09:07:36 -0700110 // Unfortunately we need to do this check even if no modifications have been done since it could
111 // be that agents were removed in the mean-time so we still have a different dex file. The dex
112 // checksum means this is likely to be fairly fast.
Alex Light64e4c142018-01-30 13:46:37 -0800113 return current_dex_file_.size() != dex_data_.size() ||
114 memcmp(current_dex_file_.data(), dex_data_.data(), current_dex_file_.size()) != 0;
Alex Lightb7354d52017-03-30 15:17:01 -0700115}
116
Alex Light64e4c142018-01-30 13:46:37 -0800117jvmtiError ArtClassDefinition::InitCommon(art::Thread* self, jclass klass) {
118 art::ScopedObjectAccess soa(self);
Alex Lightb7354d52017-03-30 15:17:01 -0700119 art::ObjPtr<art::mirror::Class> m_klass(soa.Decode<art::mirror::Class>(klass));
120 if (m_klass.IsNull()) {
121 return ERR(INVALID_CLASS);
122 }
Alex Light64e4c142018-01-30 13:46:37 -0800123 initialized_ = true;
Alex Lightb7354d52017-03-30 15:17:01 -0700124 klass_ = klass;
125 loader_ = soa.AddLocalReference<jobject>(m_klass->GetClassLoader());
126 std::string descriptor_store;
127 std::string descriptor(m_klass->GetDescriptor(&descriptor_store));
128 name_ = descriptor.substr(1, descriptor.size() - 2);
129 // Android doesn't really have protection domains.
130 protection_domain_ = nullptr;
131 return OK;
132}
133
Alex Light64e4c142018-01-30 13:46:37 -0800134static void DequickenDexFile(const art::DexFile* dex_file,
135 const char* descriptor,
136 /*out*/std::vector<unsigned char>* dex_data)
137 REQUIRES_SHARED(art::Locks::mutator_lock_) {
138 std::unique_ptr<FixedUpDexFile> fixed_dex_file(FixedUpDexFile::Create(*dex_file, descriptor));
139 dex_data->resize(fixed_dex_file->Size());
140 memcpy(dex_data->data(), fixed_dex_file->Begin(), fixed_dex_file->Size());
141}
142
Alex Lightb7354d52017-03-30 15:17:01 -0700143// Gets the data surrounding the given class.
Alex Light64e4c142018-01-30 13:46:37 -0800144static void GetDexDataForRetransformation(art::Handle<art::mirror::Class> klass,
145 /*out*/std::vector<unsigned char>* dex_data)
Alex Lightb7354d52017-03-30 15:17:01 -0700146 REQUIRES_SHARED(art::Locks::mutator_lock_) {
147 art::StackHandleScope<3> hs(art::Thread::Current());
148 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->GetExtData()));
149 const art::DexFile* dex_file = nullptr;
150 if (!ext.IsNull()) {
151 art::Handle<art::mirror::Object> orig_dex(hs.NewHandle(ext->GetOriginalDexFile()));
152 if (!orig_dex.IsNull()) {
153 if (orig_dex->IsArrayInstance()) {
154 DCHECK(orig_dex->GetClass()->GetComponentType()->IsPrimitiveByte());
155 art::Handle<art::mirror::ByteArray> orig_dex_bytes(
156 hs.NewHandle(art::down_cast<art::mirror::ByteArray*>(orig_dex->AsArray())));
Alex Light64e4c142018-01-30 13:46:37 -0800157 dex_data->resize(orig_dex_bytes->GetLength());
158 memcpy(dex_data->data(), orig_dex_bytes->GetData(), dex_data->size());
159 return;
Alex Lightb7354d52017-03-30 15:17:01 -0700160 } else if (orig_dex->IsDexCache()) {
161 dex_file = orig_dex->AsDexCache()->GetDexFile();
162 } else {
Alex Light0ecb2362017-04-12 09:01:47 -0700163 DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"))
164 << "Expected java/lang/Long but found object of type "
165 << orig_dex->GetClass()->PrettyClass();
Alex Lightb7354d52017-03-30 15:17:01 -0700166 art::ObjPtr<art::mirror::Class> prim_long_class(
Vladimir Markob4eb1b12018-05-24 11:09:38 +0100167 art::GetClassRoot(art::ClassRoot::kPrimitiveLong));
Alex Lightb7354d52017-03-30 15:17:01 -0700168 art::JValue val;
169 if (!art::UnboxPrimitiveForResult(orig_dex.Get(), prim_long_class, &val)) {
170 // This should never happen.
Alex Light64e4c142018-01-30 13:46:37 -0800171 LOG(FATAL) << "Unable to unbox a primitive long value!";
Alex Lightb7354d52017-03-30 15:17:01 -0700172 }
173 dex_file = reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ()));
174 }
175 }
176 }
177 if (dex_file == nullptr) {
178 dex_file = &klass->GetDexFile();
179 }
Mathieu Chartier75175552018-01-25 11:23:01 -0800180 std::string storage;
Alex Light64e4c142018-01-30 13:46:37 -0800181 DequickenDexFile(dex_file, klass->GetDescriptor(&storage), dex_data);
Alex Lightb7354d52017-03-30 15:17:01 -0700182}
183
Alex Light64e4c142018-01-30 13:46:37 -0800184static bool DexNeedsDequickening(art::Handle<art::mirror::Class> klass,
185 /*out*/ bool* from_class_ext)
186 REQUIRES_SHARED(art::Locks::mutator_lock_) {
187 art::ObjPtr<art::mirror::ClassExt> ext(klass->GetExtData());
188 if (ext.IsNull()) {
189 // We don't seem to have ever been redefined so be conservative and say we need de-quickening.
190 *from_class_ext = false;
191 return true;
192 }
193 art::ObjPtr<art::mirror::Object> orig_dex(ext->GetOriginalDexFile());
194 if (orig_dex.IsNull()) {
195 // We don't seem to have ever been redefined so be conservative and say we need de-quickening.
196 *from_class_ext = false;
197 return true;
198 } else if (!orig_dex->IsArrayInstance()) {
199 // We were redefined but the original is held in a dex-cache or dex file. This means that the
200 // original dex file is the one from the disk, which might be quickened.
201 DCHECK(orig_dex->IsDexCache() || orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"));
202 *from_class_ext = true;
203 return true;
204 } else {
205 // An array instance means the original-dex-file is from a redefineClasses which cannot have any
206 // quickening, so it's fine to use directly.
207 DCHECK(orig_dex->GetClass()->GetComponentType()->IsPrimitiveByte());
208 *from_class_ext = true;
209 return false;
210 }
211}
212
213static const art::DexFile* GetQuickenedDexFile(art::Handle<art::mirror::Class> klass)
214 REQUIRES_SHARED(art::Locks::mutator_lock_) {
215 art::ObjPtr<art::mirror::ClassExt> ext(klass->GetExtData());
216 if (ext.IsNull() || ext->GetOriginalDexFile() == nullptr) {
217 return &klass->GetDexFile();
218 }
219
220 art::ObjPtr<art::mirror::Object> orig_dex(ext->GetOriginalDexFile());
221 DCHECK(!orig_dex->IsArrayInstance());
222 if (orig_dex->IsDexCache()) {
223 return orig_dex->AsDexCache()->GetDexFile();
224 }
225
226 DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"))
227 << "Expected java/lang/Long but found object of type "
228 << orig_dex->GetClass()->PrettyClass();
229 art::ObjPtr<art::mirror::Class> prim_long_class(
Vladimir Markob4eb1b12018-05-24 11:09:38 +0100230 art::GetClassRoot(art::ClassRoot::kPrimitiveLong));
Alex Light64e4c142018-01-30 13:46:37 -0800231 art::JValue val;
232 if (!art::UnboxPrimitiveForResult(orig_dex.Ptr(), prim_long_class, &val)) {
233 LOG(FATAL) << "Unable to unwrap a long value!";
234 }
235 return reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ()));
236}
237
238template<typename GetOriginalDexFile>
239void ArtClassDefinition::InitWithDex(GetOriginalDexFile get_original,
240 const art::DexFile* quick_dex) {
241 art::Thread* self = art::Thread::Current();
242 DCHECK(quick_dex != nullptr);
Alex Lightca97ada2018-02-02 09:25:31 -0800243 if (art::MemMap::kCanReplaceMapping && kEnableOnDemandDexDequicken) {
244 size_t dequick_size = quick_dex->GetDequickenedSize();
245 std::string mmap_name("anon-mmap-for-redefine: ");
246 mmap_name += name_;
247 std::string error;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100248 dex_data_mmap_ = art::MemMap::MapAnonymous(mmap_name.c_str(),
249 /* addr */ nullptr,
Alex Lightca97ada2018-02-02 09:25:31 -0800250 dequick_size,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100251 PROT_NONE,
Alex Lightca97ada2018-02-02 09:25:31 -0800252 /*low_4gb*/ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100253 &error);
254 mmap_name += "-TEMP";
255 temp_mmap_ = art::MemMap::MapAnonymous(mmap_name.c_str(),
256 /* addr */ nullptr,
257 dequick_size,
258 PROT_READ | PROT_WRITE,
259 /*low_4gb*/ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100260 &error);
261 if (UNLIKELY(dex_data_mmap_.IsValid() && temp_mmap_.IsValid())) {
Alex Lightca97ada2018-02-02 09:25:31 -0800262 // Need to save the initial dexfile so we don't need to search for it in the fault-handler.
263 initial_dex_file_unquickened_ = quick_dex;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100264 dex_data_ = art::ArrayRef<const unsigned char>(dex_data_mmap_.Begin(),
265 dex_data_mmap_.Size());
Alex Lightca97ada2018-02-02 09:25:31 -0800266 if (from_class_ext_) {
267 // We got initial from class_ext so the current one must have undergone redefinition so no
268 // cdex or quickening stuff.
269 // We can only do this if it's not a first load.
270 DCHECK(klass_ != nullptr);
271 const art::DexFile& cur_dex = self->DecodeJObject(klass_)->AsClass()->GetDexFile();
272 current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
273 } else {
274 // This class hasn't been redefined before. The dequickened current data is the same as the
275 // dex_data_mmap_ when it's filled it. We don't need to copy anything because the mmap will
276 // not be cleared until after everything is done.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100277 current_dex_file_ = art::ArrayRef<const unsigned char>(dex_data_mmap_.Begin(),
Alex Lightca97ada2018-02-02 09:25:31 -0800278 dequick_size);
279 }
280 return;
281 }
282 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100283 dex_data_mmap_.Reset();
284 temp_mmap_.Reset();
Alex Lightca97ada2018-02-02 09:25:31 -0800285 // Failed to mmap a large enough area (or on-demand dequickening was disabled). This is
286 // unfortunate. Since currently the size is just a guess though we might as well try to do it
287 // manually.
Alex Light64e4c142018-01-30 13:46:37 -0800288 get_original(/*out*/&dex_data_memory_);
289 dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
290 if (from_class_ext_) {
291 // We got initial from class_ext so the current one must have undergone redefinition so no
292 // cdex or quickening stuff.
293 // We can only do this if it's not a first load.
294 DCHECK(klass_ != nullptr);
295 const art::DexFile& cur_dex = self->DecodeJObject(klass_)->AsClass()->GetDexFile();
296 current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
297 } else {
298 // No redefinition must have ever happened so the (dequickened) cur_dex is the same as the
299 // initial dex_data. We need to copy it into another buffer to keep it around if we have a
300 // real redefinition.
301 current_dex_memory_.resize(dex_data_.size());
302 memcpy(current_dex_memory_.data(), dex_data_.data(), current_dex_memory_.size());
303 current_dex_file_ = art::ArrayRef<const unsigned char>(current_dex_memory_);
304 }
305}
306
307jvmtiError ArtClassDefinition::Init(art::Thread* self, jclass klass) {
308 jvmtiError res = InitCommon(self, klass);
Alex Lightb7354d52017-03-30 15:17:01 -0700309 if (res != OK) {
310 return res;
311 }
Alex Lightb7354d52017-03-30 15:17:01 -0700312 art::ScopedObjectAccess soa(self);
313 art::StackHandleScope<1> hs(self);
314 art::Handle<art::mirror::Class> m_klass(hs.NewHandle(self->DecodeJObject(klass)->AsClass()));
Alex Light64e4c142018-01-30 13:46:37 -0800315 if (!DexNeedsDequickening(m_klass, &from_class_ext_)) {
316 // We don't need to do any dequickening. We want to copy the data just so we don't need to deal
317 // with the GC moving it around.
318 art::ObjPtr<art::mirror::ByteArray> orig_dex(
319 m_klass->GetExtData()->GetOriginalDexFile()->AsByteArray());
320 dex_data_memory_.resize(orig_dex->GetLength());
321 memcpy(dex_data_memory_.data(), orig_dex->GetData(), dex_data_memory_.size());
322 dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_);
323
324 // Since we are here we must not have any quickened instructions since we were redefined.
325 const art::DexFile& cur_dex = m_klass->GetDexFile();
326 DCHECK(from_class_ext_);
327 current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size());
328 return OK;
Alex Lightb7354d52017-03-30 15:17:01 -0700329 }
Alex Light64e4c142018-01-30 13:46:37 -0800330
331 // We need to dequicken stuff. This is often super slow (10's of ms). Instead we will do it
332 // dynamically.
333 const art::DexFile* quick_dex = GetQuickenedDexFile(m_klass);
334 auto get_original = [&](/*out*/std::vector<unsigned char>* dex_data)
335 REQUIRES_SHARED(art::Locks::mutator_lock_) {
336 GetDexDataForRetransformation(m_klass, dex_data);
337 };
338 InitWithDex(get_original, quick_dex);
339 return OK;
Alex Lightb7354d52017-03-30 15:17:01 -0700340}
341
Alex Light64e4c142018-01-30 13:46:37 -0800342jvmtiError ArtClassDefinition::Init(art::Thread* self, const jvmtiClassDefinition& def) {
343 jvmtiError res = InitCommon(self, def.klass);
Alex Lightb7354d52017-03-30 15:17:01 -0700344 if (res != OK) {
345 return res;
346 }
Alex Light64e4c142018-01-30 13:46:37 -0800347 // We are being directly redefined.
Alex Lightb7354d52017-03-30 15:17:01 -0700348 redefined_ = true;
Alex Light64e4c142018-01-30 13:46:37 -0800349 current_dex_file_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
350 dex_data_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count);
351 return OK;
352}
353
354void ArtClassDefinition::InitFirstLoad(const char* descriptor,
355 art::Handle<art::mirror::ClassLoader> klass_loader,
356 const art::DexFile& dex_file) {
357 art::Thread* self = art::Thread::Current();
358 art::ScopedObjectAccess soa(self);
359 initialized_ = true;
360 // No Class
361 klass_ = nullptr;
362 loader_ = soa.AddLocalReference<jobject>(klass_loader.Get());
363 std::string descriptor_str(descriptor);
364 name_ = descriptor_str.substr(1, descriptor_str.size() - 2);
365 // Android doesn't really have protection domains.
366 protection_domain_ = nullptr;
367 auto get_original = [&](/*out*/std::vector<unsigned char>* dex_data)
368 REQUIRES_SHARED(art::Locks::mutator_lock_) {
369 DequickenDexFile(&dex_file, descriptor, dex_data);
370 };
371 InitWithDex(get_original, &dex_file);
Alex Lighta7e38d82017-01-19 14:57:28 -0800372}
373
374} // namespace openjdkjvmti