Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 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 | |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 34 | #include "base/array_slice.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 35 | #include "class_linker-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 36 | #include "dex/dex_file.h" |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 37 | #include "fixed_up_dex_file.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 38 | #include "handle.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 39 | #include "handle_scope-inl.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 40 | #include "mirror/class-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 41 | #include "mirror/class_ext.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 42 | #include "mirror/object-inl.h" |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 43 | #include "reflection.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 44 | #include "thread.h" |
| 45 | |
| 46 | namespace openjdkjvmti { |
| 47 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 48 | bool ArtClassDefinition::IsModified() const { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 49 | // RedefineClasses calls always are 'modified' since they need to change the current_dex_file of |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 50 | // the class. |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 51 | if (redefined_) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 52 | return true; |
| 53 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 54 | |
| 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 Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 61 | // Check if the dex file we want to set is the same as the current one. |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 62 | // 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 65 | return current_dex_file_.size() != dex_data_.size() || |
| 66 | memcmp(current_dex_file_.data(), dex_data_.data(), current_dex_file_.size()) != 0; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 69 | jvmtiError ArtClassDefinition::InitCommon(art::Thread* self, jclass klass) { |
| 70 | art::ScopedObjectAccess soa(self); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 71 | 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 75 | initialized_ = true; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 76 | 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 86 | static 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 Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 95 | // Gets the data surrounding the given class. |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 96 | static void GetDexDataForRetransformation(art::Handle<art::mirror::Class> klass, |
| 97 | /*out*/std::vector<unsigned char>* dex_data) |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 98 | 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 109 | dex_data->resize(orig_dex_bytes->GetLength()); |
| 110 | memcpy(dex_data->data(), orig_dex_bytes->GetData(), dex_data->size()); |
| 111 | return; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 112 | } else if (orig_dex->IsDexCache()) { |
| 113 | dex_file = orig_dex->AsDexCache()->GetDexFile(); |
| 114 | } else { |
Alex Light | 0ecb236 | 2017-04-12 09:01:47 -0700 | [diff] [blame] | 115 | DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;")) |
| 116 | << "Expected java/lang/Long but found object of type " |
| 117 | << orig_dex->GetClass()->PrettyClass(); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 118 | 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 124 | LOG(FATAL) << "Unable to unbox a primitive long value!"; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 125 | } |
| 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 Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 133 | std::string storage; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 134 | DequickenDexFile(dex_file, klass->GetDescriptor(&storage), dex_data); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 137 | static 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 | |
| 166 | static 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 | |
| 192 | template<typename GetOriginalDexFile> |
| 193 | void 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 | |
| 216 | jvmtiError ArtClassDefinition::Init(art::Thread* self, jclass klass) { |
| 217 | jvmtiError res = InitCommon(self, klass); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 218 | if (res != OK) { |
| 219 | return res; |
| 220 | } |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 221 | 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 224 | 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 Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 238 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 239 | |
| 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 Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 251 | jvmtiError ArtClassDefinition::Init(art::Thread* self, const jvmtiClassDefinition& def) { |
| 252 | jvmtiError res = InitCommon(self, def.klass); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 253 | if (res != OK) { |
| 254 | return res; |
| 255 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 256 | // We are being directly redefined. |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 257 | redefined_ = true; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame^] | 258 | 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 | |
| 263 | void 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 Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | } // namespace openjdkjvmti |