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" |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 36 | #include "class_root.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 37 | #include "dex/dex_file.h" |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 38 | #include "fixed_up_dex_file.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 39 | #include "handle.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 40 | #include "handle_scope-inl.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 41 | #include "mirror/class-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 42 | #include "mirror/class_ext.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 43 | #include "mirror/object-inl.h" |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 44 | #include "reflection.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 45 | #include "thread.h" |
| 46 | |
| 47 | namespace openjdkjvmti { |
| 48 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 49 | void ArtClassDefinition::InitializeMemory() const { |
| 50 | DCHECK(art::MemMap::kCanReplaceMapping); |
| 51 | VLOG(signals) << "Initializing de-quickened memory for dex file of " << name_; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 52 | 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 Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 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); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 61 | CHECK_LE(fixed_dex_file->Size(), temp_mmap_.Size()); |
| 62 | CHECK_EQ(temp_mmap_.Size(), dex_data_mmap_.Size()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 63 | // Copy the data to the temp mmap. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 64 | memcpy(temp_mmap_.Begin(), fixed_dex_file->Begin(), fixed_dex_file->Size()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 65 | |
| 66 | // Move the mmap atomically. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 67 | art::MemMap source; |
| 68 | source.swap(temp_mmap_); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 69 | std::string error; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 70 | 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 Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 75 | bool ArtClassDefinition::IsModified() const { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 76 | // 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] | 77 | // the class. |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 78 | if (redefined_) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 79 | return true; |
| 80 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 81 | |
| 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 Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 88 | // The dex_data_ was never touched by the agents. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 89 | if (dex_data_mmap_.IsValid() && dex_data_mmap_.GetProtect() == PROT_NONE) { |
| 90 | if (current_dex_file_.data() == dex_data_mmap_.Begin()) { |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 91 | // 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 Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 95 | } else if (dex_data_.data() == dex_data_mmap_.Begin()) { |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 96 | // 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 Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 109 | // 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] | 110 | // 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 113 | return current_dex_file_.size() != dex_data_.size() || |
| 114 | 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] | 115 | } |
| 116 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 117 | jvmtiError ArtClassDefinition::InitCommon(art::Thread* self, jclass klass) { |
| 118 | art::ScopedObjectAccess soa(self); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 119 | 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 123 | initialized_ = true; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 124 | 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 134 | static 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 Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 143 | // Gets the data surrounding the given class. |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 144 | static void GetDexDataForRetransformation(art::Handle<art::mirror::Class> klass, |
| 145 | /*out*/std::vector<unsigned char>* dex_data) |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 146 | 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 157 | dex_data->resize(orig_dex_bytes->GetLength()); |
| 158 | memcpy(dex_data->data(), orig_dex_bytes->GetData(), dex_data->size()); |
| 159 | return; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 160 | } else if (orig_dex->IsDexCache()) { |
| 161 | dex_file = orig_dex->AsDexCache()->GetDexFile(); |
| 162 | } else { |
Alex Light | 0ecb236 | 2017-04-12 09:01:47 -0700 | [diff] [blame] | 163 | DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;")) |
| 164 | << "Expected java/lang/Long but found object of type " |
| 165 | << orig_dex->GetClass()->PrettyClass(); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 166 | art::ObjPtr<art::mirror::Class> prim_long_class( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 167 | art::GetClassRoot(art::ClassRoot::kPrimitiveLong)); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 168 | art::JValue val; |
| 169 | if (!art::UnboxPrimitiveForResult(orig_dex.Get(), prim_long_class, &val)) { |
| 170 | // This should never happen. |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 171 | LOG(FATAL) << "Unable to unbox a primitive long value!"; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 172 | } |
| 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 Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 180 | std::string storage; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 181 | DequickenDexFile(dex_file, klass->GetDescriptor(&storage), dex_data); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 184 | static 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 | |
| 213 | static 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 Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 230 | art::GetClassRoot(art::ClassRoot::kPrimitiveLong)); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 231 | 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 | |
| 238 | template<typename GetOriginalDexFile> |
| 239 | void ArtClassDefinition::InitWithDex(GetOriginalDexFile get_original, |
| 240 | const art::DexFile* quick_dex) { |
| 241 | art::Thread* self = art::Thread::Current(); |
| 242 | DCHECK(quick_dex != nullptr); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 243 | 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 Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 248 | dex_data_mmap_ = art::MemMap::MapAnonymous(mmap_name.c_str(), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 249 | /* addr= */ nullptr, |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 250 | dequick_size, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 251 | PROT_NONE, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 252 | /*low_4gb=*/ false, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 253 | &error); |
| 254 | mmap_name += "-TEMP"; |
| 255 | temp_mmap_ = art::MemMap::MapAnonymous(mmap_name.c_str(), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 256 | /* addr= */ nullptr, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 257 | dequick_size, |
| 258 | PROT_READ | PROT_WRITE, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 259 | /*low_4gb=*/ false, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 260 | &error); |
| 261 | if (UNLIKELY(dex_data_mmap_.IsValid() && temp_mmap_.IsValid())) { |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 262 | // 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 Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 264 | dex_data_ = art::ArrayRef<const unsigned char>(dex_data_mmap_.Begin(), |
| 265 | dex_data_mmap_.Size()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 266 | 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 Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 277 | current_dex_file_ = art::ArrayRef<const unsigned char>(dex_data_mmap_.Begin(), |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 278 | dequick_size); |
| 279 | } |
| 280 | return; |
| 281 | } |
| 282 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 283 | dex_data_mmap_.Reset(); |
| 284 | temp_mmap_.Reset(); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 285 | // 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 288 | 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 | |
| 307 | jvmtiError ArtClassDefinition::Init(art::Thread* self, jclass klass) { |
| 308 | jvmtiError res = InitCommon(self, klass); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 309 | if (res != OK) { |
| 310 | return res; |
| 311 | } |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 312 | 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 Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 315 | 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 Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 329 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 330 | |
| 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 Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 342 | jvmtiError ArtClassDefinition::Init(art::Thread* self, const jvmtiClassDefinition& def) { |
| 343 | jvmtiError res = InitCommon(self, def.klass); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 344 | if (res != OK) { |
| 345 | return res; |
| 346 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 347 | // We are being directly redefined. |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 348 | redefined_ = true; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 349 | 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 | |
| 354 | void 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 Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | } // namespace openjdkjvmti |