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 | 85f1c57 | 2018-11-21 13:52:48 -0800 | [diff] [blame] | 35 | #include "base/logging.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 36 | #include "class_linker-inl.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 37 | #include "class_root-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 38 | #include "dex/dex_file.h" |
Nicolas Geoffray | ad67bbb | 2022-02-04 17:26:40 +0000 | [diff] [blame] | 39 | #include "dex/art_dex_file_loader.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 40 | #include "handle.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 41 | #include "handle_scope-inl.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 42 | #include "mirror/class-inl.h" |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 43 | #include "mirror/class_ext-inl.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 44 | #include "mirror/object-inl.h" |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 45 | #include "reflection.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 46 | #include "thread.h" |
| 47 | |
| 48 | namespace openjdkjvmti { |
| 49 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 50 | bool ArtClassDefinition::IsModified() const { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 51 | // 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] | 52 | // the class. |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 53 | if (redefined_) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 54 | return true; |
| 55 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 56 | |
| 57 | // Check to see if any change has taken place. |
| 58 | if (current_dex_file_.data() == dex_data_.data()) { |
| 59 | // no change at all. |
| 60 | return false; |
| 61 | } |
| 62 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 63 | // 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] | 64 | // Unfortunately we need to do this check even if no modifications have been done since it could |
| 65 | // be that agents were removed in the mean-time so we still have a different dex file. The dex |
| 66 | // checksum means this is likely to be fairly fast. |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 67 | return current_dex_file_.size() != dex_data_.size() || |
| 68 | 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] | 69 | } |
| 70 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 71 | jvmtiError ArtClassDefinition::InitCommon(art::Thread* self, jclass klass) { |
| 72 | art::ScopedObjectAccess soa(self); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 73 | art::ObjPtr<art::mirror::Class> m_klass(soa.Decode<art::mirror::Class>(klass)); |
| 74 | if (m_klass.IsNull()) { |
| 75 | return ERR(INVALID_CLASS); |
| 76 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 77 | initialized_ = true; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 78 | klass_ = klass; |
| 79 | loader_ = soa.AddLocalReference<jobject>(m_klass->GetClassLoader()); |
| 80 | std::string descriptor_store; |
| 81 | std::string descriptor(m_klass->GetDescriptor(&descriptor_store)); |
| 82 | name_ = descriptor.substr(1, descriptor.size() - 2); |
| 83 | // Android doesn't really have protection domains. |
| 84 | protection_domain_ = nullptr; |
| 85 | return OK; |
| 86 | } |
| 87 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 88 | jvmtiError ArtClassDefinition::Init(art::Thread* self, jclass klass) { |
| 89 | jvmtiError res = InitCommon(self, klass); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 90 | if (res != OK) { |
| 91 | return res; |
| 92 | } |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 93 | art::ScopedObjectAccess soa(self); |
| 94 | art::StackHandleScope<1> hs(self); |
| 95 | art::Handle<art::mirror::Class> m_klass(hs.NewHandle(self->DecodeJObject(klass)->AsClass())); |
Nicolas Geoffray | ad67bbb | 2022-02-04 17:26:40 +0000 | [diff] [blame] | 96 | art::ObjPtr<art::mirror::ClassExt> ext(m_klass->GetExtData()); |
| 97 | if (!ext.IsNull()) { |
| 98 | art::ObjPtr<art::mirror::Object> orig_dex(ext->GetOriginalDexFile()); |
| 99 | if (!orig_dex.IsNull()) { |
| 100 | if (orig_dex->IsArrayInstance()) { |
| 101 | // An array instance means the original-dex-file is from a redefineClasses which cannot have any |
| 102 | // compact dex, so it's fine to use directly. |
| 103 | art::ObjPtr<art::mirror::ByteArray> byte_array(orig_dex->AsByteArray()); |
| 104 | dex_data_memory_.resize(byte_array->GetLength()); |
| 105 | memcpy(dex_data_memory_.data(), byte_array->GetData(), dex_data_memory_.size()); |
| 106 | dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 107 | |
Nicolas Geoffray | ad67bbb | 2022-02-04 17:26:40 +0000 | [diff] [blame] | 108 | const art::DexFile& cur_dex = m_klass->GetDexFile(); |
| 109 | current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size()); |
| 110 | return OK; |
| 111 | } |
| 112 | |
| 113 | if (orig_dex->IsDexCache()) { |
| 114 | res = Init(*orig_dex->AsDexCache()->GetDexFile()); |
| 115 | if (res != OK) { |
| 116 | return res; |
| 117 | } |
| 118 | } else { |
| 119 | DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;")) |
| 120 | << "Expected java/lang/Long but found object of type " |
| 121 | << orig_dex->GetClass()->PrettyClass(); |
| 122 | art::ObjPtr<art::mirror::Class> prim_long_class( |
| 123 | art::GetClassRoot(art::ClassRoot::kPrimitiveLong)); |
| 124 | art::JValue val; |
| 125 | if (!art::UnboxPrimitiveForResult(orig_dex.Ptr(), prim_long_class, &val)) { |
| 126 | // This should never happen. |
| 127 | LOG(FATAL) << "Unable to unbox a primitive long value!"; |
| 128 | } |
| 129 | res = Init(*reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ()))); |
| 130 | if (res != OK) { |
| 131 | return res; |
| 132 | } |
| 133 | } |
| 134 | const art::DexFile& cur_dex = m_klass->GetDexFile(); |
| 135 | current_dex_file_ = art::ArrayRef<const unsigned char>(cur_dex.Begin(), cur_dex.Size()); |
| 136 | return OK; |
| 137 | } |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 138 | } |
Nicolas Geoffray | ad67bbb | 2022-02-04 17:26:40 +0000 | [diff] [blame] | 139 | // No redefinition must have ever happened so we can use the class's dex file. |
| 140 | return Init(m_klass->GetDexFile()); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 143 | jvmtiError ArtClassDefinition::Init(art::Thread* self, const jvmtiClassDefinition& def) { |
| 144 | jvmtiError res = InitCommon(self, def.klass); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 145 | if (res != OK) { |
| 146 | return res; |
| 147 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 148 | // We are being directly redefined. |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 149 | redefined_ = true; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 150 | current_dex_file_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count); |
| 151 | dex_data_ = art::ArrayRef<const unsigned char>(def.class_bytes, def.class_byte_count); |
| 152 | return OK; |
| 153 | } |
| 154 | |
Nicolas Geoffray | ad67bbb | 2022-02-04 17:26:40 +0000 | [diff] [blame] | 155 | jvmtiError ArtClassDefinition::InitFirstLoad(const char* descriptor, |
| 156 | art::Handle<art::mirror::ClassLoader> klass_loader, |
| 157 | const art::DexFile& dex_file) { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 158 | art::Thread* self = art::Thread::Current(); |
| 159 | art::ScopedObjectAccess soa(self); |
| 160 | initialized_ = true; |
| 161 | // No Class |
| 162 | klass_ = nullptr; |
| 163 | loader_ = soa.AddLocalReference<jobject>(klass_loader.Get()); |
| 164 | std::string descriptor_str(descriptor); |
| 165 | name_ = descriptor_str.substr(1, descriptor_str.size() - 2); |
| 166 | // Android doesn't really have protection domains. |
| 167 | protection_domain_ = nullptr; |
Nicolas Geoffray | ad67bbb | 2022-02-04 17:26:40 +0000 | [diff] [blame] | 168 | return Init(dex_file); |
| 169 | } |
| 170 | |
| 171 | jvmtiError ArtClassDefinition::Init(const art::DexFile& dex_file) { |
| 172 | if (dex_file.IsCompactDexFile()) { |
| 173 | std::string error_msg; |
| 174 | std::vector<std::unique_ptr<const art::DexFile>> dex_files; |
| 175 | const art::ArtDexFileLoader dex_file_loader; |
| 176 | if (!dex_file_loader.Open(dex_file.GetLocation().c_str(), |
| 177 | dex_file.GetLocation().c_str(), |
| 178 | /* verify= */ false, |
| 179 | /* verify_checksum= */ false, |
| 180 | &error_msg, |
| 181 | &dex_files)) { |
| 182 | return ERR(INTERNAL); |
| 183 | } |
| 184 | const std::vector<const art::OatDexFile*>& oat_dex_files = |
| 185 | dex_file.GetOatDexFile()->GetOatFile()->GetOatDexFiles(); |
| 186 | const art::DexFile* original_dex_file = nullptr; |
| 187 | for (uint32_t i = 0; i < oat_dex_files.size(); ++i) { |
| 188 | if (dex_file.GetOatDexFile() == oat_dex_files[i]) { |
| 189 | original_dex_file = dex_files[i].get(); |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | // Keep the dex_data alive. |
| 194 | dex_data_memory_.resize(original_dex_file->Size()); |
| 195 | memcpy(dex_data_memory_.data(), original_dex_file->Begin(), original_dex_file->Size()); |
| 196 | dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_); |
| 197 | |
| 198 | // In case dex_data gets re-used for redefinition, keep the dex file live |
| 199 | // with current_dex_memory. |
| 200 | current_dex_memory_.resize(dex_data_.size()); |
| 201 | memcpy(current_dex_memory_.data(), dex_data_.data(), current_dex_memory_.size()); |
| 202 | current_dex_file_ = art::ArrayRef<const unsigned char>(current_dex_memory_); |
| 203 | } else { |
| 204 | // Dex file will always stay live, use it directly. |
| 205 | dex_data_ = art::ArrayRef<const unsigned char>(dex_file.Begin(), dex_file.Size()); |
| 206 | current_dex_file_ = dex_data_; |
| 207 | } |
| 208 | return OK; |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | } // namespace openjdkjvmti |