Alex Light | a01de59 | 2016-11-15 10:43:06 -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_redefine.h" |
| 33 | |
| 34 | #include <limits> |
| 35 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 36 | #include "android-base/stringprintf.h" |
| 37 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 38 | #include "art_field-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 39 | #include "art_jvmti.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 40 | #include "art_method-inl.h" |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 41 | #include "base/array_ref.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 42 | #include "base/logging.h" |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 43 | #include "base/stringpiece.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 44 | #include "class_linker-inl.h" |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 45 | #include "debugger.h" |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 46 | #include "dex_file.h" |
| 47 | #include "dex_file_types.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 48 | #include "events-inl.h" |
| 49 | #include "gc/allocation_listener.h" |
Alex Light | 6abd539 | 2017-01-05 17:53:00 -0800 | [diff] [blame] | 50 | #include "gc/heap.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 51 | #include "instrumentation.h" |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 52 | #include "intern_table.h" |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 53 | #include "jdwp/jdwp.h" |
| 54 | #include "jdwp/jdwp_constants.h" |
| 55 | #include "jdwp/jdwp_event.h" |
| 56 | #include "jdwp/object_registry.h" |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 57 | #include "jit/jit.h" |
| 58 | #include "jit/jit_code_cache.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 59 | #include "jni_env_ext-inl.h" |
| 60 | #include "jvmti_allocator.h" |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 61 | #include "mirror/class-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 62 | #include "mirror/class_ext.h" |
| 63 | #include "mirror/object.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 64 | #include "nativehelper/ScopedLocalRef.h" |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 65 | #include "non_debuggable_classes.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 66 | #include "object_lock.h" |
| 67 | #include "runtime.h" |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 68 | #include "ti_breakpoint.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 69 | #include "ti_class_loader.h" |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 70 | #include "transform.h" |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 71 | #include "verifier/method_verifier.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 72 | #include "verifier/verifier_enums.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 73 | |
| 74 | namespace openjdkjvmti { |
| 75 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 76 | using android::base::StringPrintf; |
| 77 | |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 78 | // A helper that fills in a classes obsolete_methods_ and obsolete_dex_caches_ classExt fields as |
| 79 | // they are created. This ensures that we can always call any method of an obsolete ArtMethod object |
| 80 | // almost as soon as they are created since the GetObsoleteDexCache method will succeed. |
| 81 | class ObsoleteMap { |
| 82 | public: |
| 83 | art::ArtMethod* FindObsoleteVersion(art::ArtMethod* original) |
| 84 | REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) { |
| 85 | auto method_pair = id_map_.find(original); |
| 86 | if (method_pair != id_map_.end()) { |
| 87 | art::ArtMethod* res = obsolete_methods_->GetElementPtrSize<art::ArtMethod*>( |
| 88 | method_pair->second, art::kRuntimePointerSize); |
| 89 | DCHECK(res != nullptr); |
| 90 | DCHECK_EQ(original, res->GetNonObsoleteMethod()); |
| 91 | return res; |
| 92 | } else { |
| 93 | return nullptr; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void RecordObsolete(art::ArtMethod* original, art::ArtMethod* obsolete) |
| 98 | REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) { |
| 99 | DCHECK(original != nullptr); |
| 100 | DCHECK(obsolete != nullptr); |
| 101 | int32_t slot = next_free_slot_++; |
| 102 | DCHECK_LT(slot, obsolete_methods_->GetLength()); |
| 103 | DCHECK(nullptr == |
| 104 | obsolete_methods_->GetElementPtrSize<art::ArtMethod*>(slot, art::kRuntimePointerSize)); |
| 105 | DCHECK(nullptr == obsolete_dex_caches_->Get(slot)); |
| 106 | obsolete_methods_->SetElementPtrSize(slot, obsolete, art::kRuntimePointerSize); |
| 107 | obsolete_dex_caches_->Set(slot, original_dex_cache_); |
| 108 | id_map_.insert({original, slot}); |
| 109 | } |
| 110 | |
| 111 | ObsoleteMap(art::ObjPtr<art::mirror::PointerArray> obsolete_methods, |
| 112 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> obsolete_dex_caches, |
| 113 | art::ObjPtr<art::mirror::DexCache> original_dex_cache) |
| 114 | : next_free_slot_(0), |
| 115 | obsolete_methods_(obsolete_methods), |
| 116 | obsolete_dex_caches_(obsolete_dex_caches), |
| 117 | original_dex_cache_(original_dex_cache) { |
| 118 | // Figure out where the first unused slot in the obsolete_methods_ array is. |
| 119 | while (obsolete_methods_->GetElementPtrSize<art::ArtMethod*>( |
| 120 | next_free_slot_, art::kRuntimePointerSize) != nullptr) { |
| 121 | DCHECK(obsolete_dex_caches_->Get(next_free_slot_) != nullptr); |
| 122 | next_free_slot_++; |
| 123 | } |
| 124 | // Sanity check that the same slot in obsolete_dex_caches_ is free. |
| 125 | DCHECK(obsolete_dex_caches_->Get(next_free_slot_) == nullptr); |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | int32_t next_free_slot_; |
| 130 | std::unordered_map<art::ArtMethod*, int32_t> id_map_; |
| 131 | // Pointers to the fields in mirror::ClassExt. These can be held as ObjPtr since this is only used |
| 132 | // when we have an exclusive mutator_lock_ (i.e. all threads are suspended). |
| 133 | art::ObjPtr<art::mirror::PointerArray> obsolete_methods_; |
| 134 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> obsolete_dex_caches_; |
| 135 | art::ObjPtr<art::mirror::DexCache> original_dex_cache_; |
| 136 | }; |
| 137 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 138 | // This visitor walks thread stacks and allocates and sets up the obsolete methods. It also does |
| 139 | // some basic sanity checks that the obsolete method is sane. |
| 140 | class ObsoleteMethodStackVisitor : public art::StackVisitor { |
| 141 | protected: |
| 142 | ObsoleteMethodStackVisitor( |
| 143 | art::Thread* thread, |
| 144 | art::LinearAlloc* allocator, |
| 145 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods, |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 146 | ObsoleteMap* obsolete_maps) |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 147 | : StackVisitor(thread, |
| 148 | /*context*/nullptr, |
| 149 | StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 150 | allocator_(allocator), |
| 151 | obsoleted_methods_(obsoleted_methods), |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 152 | obsolete_maps_(obsolete_maps) { } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 153 | |
| 154 | ~ObsoleteMethodStackVisitor() OVERRIDE {} |
| 155 | |
| 156 | public: |
| 157 | // Returns true if we successfully installed obsolete methods on this thread, filling |
| 158 | // obsolete_maps_ with the translations if needed. Returns false and fills error_msg if we fail. |
| 159 | // The stack is cleaned up when we fail. |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 160 | static void UpdateObsoleteFrames( |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 161 | art::Thread* thread, |
| 162 | art::LinearAlloc* allocator, |
| 163 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods, |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 164 | ObsoleteMap* obsolete_maps) |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 165 | REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 166 | ObsoleteMethodStackVisitor visitor(thread, |
| 167 | allocator, |
| 168 | obsoleted_methods, |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 169 | obsolete_maps); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 170 | visitor.WalkStack(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool VisitFrame() OVERRIDE REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 174 | art::ScopedAssertNoThreadSuspension snts("Fixing up the stack for obsolete methods."); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 175 | art::ArtMethod* old_method = GetMethod(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 176 | if (obsoleted_methods_.find(old_method) != obsoleted_methods_.end()) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 177 | // We cannot ensure that the right dex file is used in inlined frames so we don't support |
| 178 | // redefining them. |
| 179 | DCHECK(!IsInInlinedFrame()) << "Inlined frames are not supported when using redefinition"; |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 180 | art::ArtMethod* new_obsolete_method = obsolete_maps_->FindObsoleteVersion(old_method); |
| 181 | if (new_obsolete_method == nullptr) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 182 | // Create a new Obsolete Method and put it in the list. |
| 183 | art::Runtime* runtime = art::Runtime::Current(); |
| 184 | art::ClassLinker* cl = runtime->GetClassLinker(); |
| 185 | auto ptr_size = cl->GetImagePointerSize(); |
| 186 | const size_t method_size = art::ArtMethod::Size(ptr_size); |
Alex Light | 5c11a79 | 2017-03-10 14:29:22 -0800 | [diff] [blame] | 187 | auto* method_storage = allocator_->Alloc(art::Thread::Current(), method_size); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 188 | CHECK(method_storage != nullptr) << "Unable to allocate storage for obsolete version of '" |
| 189 | << old_method->PrettyMethod() << "'"; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 190 | new_obsolete_method = new (method_storage) art::ArtMethod(); |
| 191 | new_obsolete_method->CopyFrom(old_method, ptr_size); |
| 192 | DCHECK_EQ(new_obsolete_method->GetDeclaringClass(), old_method->GetDeclaringClass()); |
| 193 | new_obsolete_method->SetIsObsolete(); |
Alex Light | fcbafb3 | 2017-02-02 15:09:54 -0800 | [diff] [blame] | 194 | new_obsolete_method->SetDontCompile(); |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 195 | cl->SetEntryPointsForObsoleteMethod(new_obsolete_method); |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 196 | obsolete_maps_->RecordObsolete(old_method, new_obsolete_method); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 197 | // Update JIT Data structures to point to the new method. |
| 198 | art::jit::Jit* jit = art::Runtime::Current()->GetJit(); |
| 199 | if (jit != nullptr) { |
| 200 | // Notify the JIT we are making this obsolete method. It will update the jit's internal |
| 201 | // structures to keep track of the new obsolete method. |
| 202 | jit->GetCodeCache()->MoveObsoleteMethod(old_method, new_obsolete_method); |
| 203 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 204 | } |
| 205 | DCHECK(new_obsolete_method != nullptr); |
| 206 | SetMethod(new_obsolete_method); |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | private: |
| 212 | // The linear allocator we should use to make new methods. |
| 213 | art::LinearAlloc* allocator_; |
| 214 | // The set of all methods which could be obsoleted. |
| 215 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods_; |
| 216 | // A map from the original to the newly allocated obsolete method for frames on this thread. The |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 217 | // values in this map are added to the obsolete_methods_ (and obsolete_dex_caches_) fields of |
| 218 | // the redefined classes ClassExt as it is filled. |
| 219 | ObsoleteMap* obsolete_maps_; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 220 | }; |
| 221 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 222 | jvmtiError Redefiner::IsModifiableClass(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 223 | jclass klass, |
| 224 | jboolean* is_redefinable) { |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 225 | art::Thread* self = art::Thread::Current(); |
| 226 | art::ScopedObjectAccess soa(self); |
| 227 | art::StackHandleScope<1> hs(self); |
| 228 | art::ObjPtr<art::mirror::Object> obj(self->DecodeJObject(klass)); |
| 229 | if (obj.IsNull()) { |
| 230 | return ERR(INVALID_CLASS); |
| 231 | } |
| 232 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(obj->AsClass())); |
| 233 | std::string err_unused; |
| 234 | *is_redefinable = |
| 235 | Redefiner::GetClassRedefinitionError(h_klass, &err_unused) == OK ? JNI_TRUE : JNI_FALSE; |
| 236 | return OK; |
| 237 | } |
| 238 | |
| 239 | jvmtiError Redefiner::GetClassRedefinitionError(art::Handle<art::mirror::Class> klass, |
| 240 | /*out*/std::string* error_msg) { |
| 241 | if (klass->IsPrimitive()) { |
| 242 | *error_msg = "Modification of primitive classes is not supported"; |
| 243 | return ERR(UNMODIFIABLE_CLASS); |
| 244 | } else if (klass->IsInterface()) { |
| 245 | *error_msg = "Modification of Interface classes is currently not supported"; |
| 246 | return ERR(UNMODIFIABLE_CLASS); |
Alex Light | 09f274f | 2017-02-21 15:00:48 -0800 | [diff] [blame] | 247 | } else if (klass->IsStringClass()) { |
| 248 | *error_msg = "Modification of String class is not supported"; |
| 249 | return ERR(UNMODIFIABLE_CLASS); |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 250 | } else if (klass->IsArrayClass()) { |
| 251 | *error_msg = "Modification of Array classes is not supported"; |
| 252 | return ERR(UNMODIFIABLE_CLASS); |
| 253 | } else if (klass->IsProxyClass()) { |
| 254 | *error_msg = "Modification of proxy classes is not supported"; |
| 255 | return ERR(UNMODIFIABLE_CLASS); |
| 256 | } |
| 257 | |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 258 | for (jclass c : art::NonDebuggableClasses::GetNonDebuggableClasses()) { |
| 259 | if (klass.Get() == art::Thread::Current()->DecodeJObject(c)->AsClass()) { |
| 260 | *error_msg = "Class might have stack frames that cannot be made obsolete"; |
| 261 | return ERR(UNMODIFIABLE_CLASS); |
| 262 | } |
| 263 | } |
| 264 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 265 | return OK; |
| 266 | } |
| 267 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 268 | // Moves dex data to an anonymous, read-only mmap'd region. |
| 269 | std::unique_ptr<art::MemMap> Redefiner::MoveDataToMemMap(const std::string& original_location, |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 270 | art::ArrayRef<const unsigned char> data, |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 271 | std::string* error_msg) { |
| 272 | std::unique_ptr<art::MemMap> map(art::MemMap::MapAnonymous( |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 273 | StringPrintf("%s-transformed", original_location.c_str()).c_str(), |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 274 | nullptr, |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 275 | data.size(), |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 276 | PROT_READ|PROT_WRITE, |
| 277 | /*low_4gb*/false, |
| 278 | /*reuse*/false, |
| 279 | error_msg)); |
| 280 | if (map == nullptr) { |
| 281 | return map; |
| 282 | } |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 283 | memcpy(map->Begin(), data.data(), data.size()); |
Alex Light | 0b77257 | 2016-12-02 17:27:31 -0800 | [diff] [blame] | 284 | // Make the dex files mmap read only. This matches how other DexFiles are mmaped and prevents |
| 285 | // programs from corrupting it. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 286 | map->Protect(PROT_READ); |
| 287 | return map; |
| 288 | } |
| 289 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 290 | Redefiner::ClassRedefinition::ClassRedefinition( |
| 291 | Redefiner* driver, |
| 292 | jclass klass, |
| 293 | const art::DexFile* redefined_dex_file, |
| 294 | const char* class_sig, |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 295 | art::ArrayRef<const unsigned char> orig_dex_file) : |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 296 | driver_(driver), |
| 297 | klass_(klass), |
| 298 | dex_file_(redefined_dex_file), |
| 299 | class_sig_(class_sig), |
| 300 | original_dex_file_(orig_dex_file) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 301 | GetMirrorClass()->MonitorEnter(driver_->self_); |
| 302 | } |
| 303 | |
| 304 | Redefiner::ClassRedefinition::~ClassRedefinition() { |
| 305 | if (driver_ != nullptr) { |
| 306 | GetMirrorClass()->MonitorExit(driver_->self_); |
| 307 | } |
| 308 | } |
| 309 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 310 | jvmtiError Redefiner::RedefineClasses(ArtJvmTiEnv* env, |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 311 | EventHandler* event_handler, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 312 | art::Runtime* runtime, |
| 313 | art::Thread* self, |
| 314 | jint class_count, |
| 315 | const jvmtiClassDefinition* definitions, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 316 | /*out*/std::string* error_msg) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 317 | if (env == nullptr) { |
| 318 | *error_msg = "env was null!"; |
| 319 | return ERR(INVALID_ENVIRONMENT); |
| 320 | } else if (class_count < 0) { |
| 321 | *error_msg = "class_count was less then 0"; |
| 322 | return ERR(ILLEGAL_ARGUMENT); |
| 323 | } else if (class_count == 0) { |
| 324 | // We don't actually need to do anything. Just return OK. |
| 325 | return OK; |
| 326 | } else if (definitions == nullptr) { |
| 327 | *error_msg = "null definitions!"; |
| 328 | return ERR(NULL_POINTER); |
| 329 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 330 | std::vector<ArtClassDefinition> def_vector; |
| 331 | def_vector.reserve(class_count); |
| 332 | for (jint i = 0; i < class_count; i++) { |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 333 | jboolean is_modifiable = JNI_FALSE; |
| 334 | jvmtiError res = env->IsModifiableClass(definitions[i].klass, &is_modifiable); |
| 335 | if (res != OK) { |
| 336 | return res; |
| 337 | } else if (!is_modifiable) { |
| 338 | return ERR(UNMODIFIABLE_CLASS); |
| 339 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 340 | // We make a copy of the class_bytes to pass into the retransformation. |
| 341 | // This makes cleanup easier (since we unambiguously own the bytes) and also is useful since we |
| 342 | // will need to keep the original bytes around unaltered for subsequent RetransformClasses calls |
| 343 | // to get the passed in bytes. |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 344 | unsigned char* class_bytes_copy = nullptr; |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 345 | res = env->Allocate(definitions[i].class_byte_count, &class_bytes_copy); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 346 | if (res != OK) { |
| 347 | return res; |
| 348 | } |
| 349 | memcpy(class_bytes_copy, definitions[i].class_bytes, definitions[i].class_byte_count); |
| 350 | |
| 351 | ArtClassDefinition def; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 352 | res = def.Init(env, definitions[i]); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 353 | if (res != OK) { |
| 354 | return res; |
| 355 | } |
| 356 | def_vector.push_back(std::move(def)); |
| 357 | } |
| 358 | // Call all the transformation events. |
| 359 | jvmtiError res = Transformer::RetransformClassesDirect(env, |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 360 | event_handler, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 361 | self, |
| 362 | &def_vector); |
| 363 | if (res != OK) { |
| 364 | // Something went wrong with transformation! |
| 365 | return res; |
| 366 | } |
| 367 | return RedefineClassesDirect(env, runtime, self, def_vector, error_msg); |
| 368 | } |
| 369 | |
| 370 | jvmtiError Redefiner::RedefineClassesDirect(ArtJvmTiEnv* env, |
| 371 | art::Runtime* runtime, |
| 372 | art::Thread* self, |
| 373 | const std::vector<ArtClassDefinition>& definitions, |
| 374 | std::string* error_msg) { |
| 375 | DCHECK(env != nullptr); |
| 376 | if (definitions.size() == 0) { |
| 377 | // We don't actually need to do anything. Just return OK. |
| 378 | return OK; |
| 379 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 380 | // Stop JIT for the duration of this redefine since the JIT might concurrently compile a method we |
| 381 | // are going to redefine. |
| 382 | art::jit::ScopedJitSuspend suspend_jit; |
| 383 | // Get shared mutator lock so we can lock all the classes. |
| 384 | art::ScopedObjectAccess soa(self); |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 385 | Redefiner r(env, runtime, self, error_msg); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 386 | for (const ArtClassDefinition& def : definitions) { |
| 387 | // Only try to transform classes that have been modified. |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 388 | if (def.IsModified()) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 389 | jvmtiError res = r.AddRedefinition(env, def); |
| 390 | if (res != OK) { |
| 391 | return res; |
| 392 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | return r.Run(); |
| 396 | } |
| 397 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 398 | jvmtiError Redefiner::AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 399 | std::string original_dex_location; |
| 400 | jvmtiError ret = OK; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 401 | if ((ret = GetClassLocation(env, def.GetClass(), &original_dex_location))) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 402 | *error_msg_ = "Unable to get original dex file location!"; |
| 403 | return ret; |
| 404 | } |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 405 | char* generic_ptr_unused = nullptr; |
| 406 | char* signature_ptr = nullptr; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 407 | if ((ret = env->GetClassSignature(def.GetClass(), &signature_ptr, &generic_ptr_unused)) != OK) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 408 | *error_msg_ = "Unable to get class signature!"; |
| 409 | return ret; |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 410 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 411 | JvmtiUniquePtr<char> generic_unique_ptr(MakeJvmtiUniquePtr(env, generic_ptr_unused)); |
| 412 | JvmtiUniquePtr<char> signature_unique_ptr(MakeJvmtiUniquePtr(env, signature_ptr)); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 413 | std::unique_ptr<art::MemMap> map(MoveDataToMemMap(original_dex_location, |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 414 | def.GetDexData(), |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 415 | error_msg_)); |
| 416 | std::ostringstream os; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 417 | if (map.get() == nullptr) { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 418 | os << "Failed to create anonymous mmap for modified dex file of class " << def.GetName() |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 419 | << "in dex file " << original_dex_location << " because: " << *error_msg_; |
| 420 | *error_msg_ = os.str(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 421 | return ERR(OUT_OF_MEMORY); |
| 422 | } |
| 423 | if (map->Size() < sizeof(art::DexFile::Header)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 424 | *error_msg_ = "Could not read dex file header because dex_data was too short"; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 425 | return ERR(INVALID_CLASS_FORMAT); |
| 426 | } |
| 427 | uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map->Begin())->checksum_; |
| 428 | std::unique_ptr<const art::DexFile> dex_file(art::DexFile::Open(map->GetName(), |
| 429 | checksum, |
| 430 | std::move(map), |
| 431 | /*verify*/true, |
| 432 | /*verify_checksum*/true, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 433 | error_msg_)); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 434 | if (dex_file.get() == nullptr) { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 435 | os << "Unable to load modified dex file for " << def.GetName() << ": " << *error_msg_; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 436 | *error_msg_ = os.str(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 437 | return ERR(INVALID_CLASS_FORMAT); |
| 438 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 439 | redefinitions_.push_back( |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 440 | Redefiner::ClassRedefinition(this, |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 441 | def.GetClass(), |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 442 | dex_file.release(), |
| 443 | signature_ptr, |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 444 | def.GetNewOriginalDexFile())); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 445 | return OK; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 448 | art::mirror::Class* Redefiner::ClassRedefinition::GetMirrorClass() { |
| 449 | return driver_->self_->DecodeJObject(klass_)->AsClass(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 452 | art::mirror::ClassLoader* Redefiner::ClassRedefinition::GetClassLoader() { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 453 | return GetMirrorClass()->GetClassLoader(); |
| 454 | } |
| 455 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 456 | art::mirror::DexCache* Redefiner::ClassRedefinition::CreateNewDexCache( |
| 457 | art::Handle<art::mirror::ClassLoader> loader) { |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 458 | art::StackHandleScope<2> hs(driver_->self_); |
| 459 | art::ClassLinker* cl = driver_->runtime_->GetClassLinker(); |
| 460 | art::Handle<art::mirror::DexCache> cache(hs.NewHandle( |
| 461 | art::ObjPtr<art::mirror::DexCache>::DownCast( |
| 462 | cl->GetClassRoot(art::ClassLinker::kJavaLangDexCache)->AllocObject(driver_->self_)))); |
| 463 | if (cache.IsNull()) { |
| 464 | driver_->self_->AssertPendingOOMException(); |
| 465 | return nullptr; |
| 466 | } |
| 467 | art::Handle<art::mirror::String> location(hs.NewHandle( |
| 468 | cl->GetInternTable()->InternStrong(dex_file_->GetLocation().c_str()))); |
| 469 | if (location.IsNull()) { |
| 470 | driver_->self_->AssertPendingOOMException(); |
| 471 | return nullptr; |
| 472 | } |
| 473 | art::WriterMutexLock mu(driver_->self_, *art::Locks::dex_lock_); |
| 474 | art::mirror::DexCache::InitializeDexCache(driver_->self_, |
| 475 | cache.Get(), |
| 476 | location.Get(), |
| 477 | dex_file_.get(), |
| 478 | loader.IsNull() ? driver_->runtime_->GetLinearAlloc() |
| 479 | : loader->GetAllocator(), |
| 480 | art::kRuntimePointerSize); |
| 481 | return cache.Get(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 484 | void Redefiner::RecordFailure(jvmtiError result, |
| 485 | const std::string& class_sig, |
| 486 | const std::string& error_msg) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 487 | *error_msg_ = StringPrintf("Unable to perform redefinition of '%s': %s", |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 488 | class_sig.c_str(), |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 489 | error_msg.c_str()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 490 | result_ = result; |
| 491 | } |
| 492 | |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 493 | art::mirror::Object* Redefiner::ClassRedefinition::AllocateOrGetOriginalDexFile() { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 494 | // If we have been specifically given a new set of bytes use that |
| 495 | if (original_dex_file_.size() != 0) { |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 496 | return art::mirror::ByteArray::AllocateAndFill( |
| 497 | driver_->self_, |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 498 | reinterpret_cast<const signed char*>(original_dex_file_.data()), |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 499 | original_dex_file_.size()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 500 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 501 | |
| 502 | // See if we already have one set. |
| 503 | art::ObjPtr<art::mirror::ClassExt> ext(GetMirrorClass()->GetExtData()); |
| 504 | if (!ext.IsNull()) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 505 | art::ObjPtr<art::mirror::Object> old_original_dex_file(ext->GetOriginalDexFile()); |
| 506 | if (!old_original_dex_file.IsNull()) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 507 | // We do. Use it. |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 508 | return old_original_dex_file.Ptr(); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 509 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 510 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 511 | |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 512 | // return the current dex_cache which has the dex file in it. |
| 513 | art::ObjPtr<art::mirror::DexCache> current_dex_cache(GetMirrorClass()->GetDexCache()); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 514 | // TODO Handle this or make it so it cannot happen. |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 515 | if (current_dex_cache->GetDexFile()->NumClassDefs() != 1) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 516 | LOG(WARNING) << "Current dex file has more than one class in it. Calling RetransformClasses " |
| 517 | << "on this class might fail if no transformations are applied to it!"; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 518 | } |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 519 | return current_dex_cache.Ptr(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 522 | struct CallbackCtx { |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 523 | ObsoleteMap* obsolete_map; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 524 | art::LinearAlloc* allocator; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 525 | std::unordered_set<art::ArtMethod*> obsolete_methods; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 526 | |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 527 | explicit CallbackCtx(ObsoleteMap* map, art::LinearAlloc* alloc) |
| 528 | : obsolete_map(map), allocator(alloc) {} |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 529 | }; |
| 530 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 531 | void DoAllocateObsoleteMethodsCallback(art::Thread* t, void* vdata) NO_THREAD_SAFETY_ANALYSIS { |
| 532 | CallbackCtx* data = reinterpret_cast<CallbackCtx*>(vdata); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 533 | ObsoleteMethodStackVisitor::UpdateObsoleteFrames(t, |
| 534 | data->allocator, |
| 535 | data->obsolete_methods, |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 536 | data->obsolete_map); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // This creates any ArtMethod* structures needed for obsolete methods and ensures that the stack is |
| 540 | // updated so they will be run. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 541 | // TODO Rewrite so we can do this only once regardless of how many redefinitions there are. |
| 542 | void Redefiner::ClassRedefinition::FindAndAllocateObsoleteMethods(art::mirror::Class* art_klass) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 543 | art::ScopedAssertNoThreadSuspension ns("No thread suspension during thread stack walking"); |
| 544 | art::mirror::ClassExt* ext = art_klass->GetExtData(); |
| 545 | CHECK(ext->GetObsoleteMethods() != nullptr); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 546 | art::ClassLinker* linker = driver_->runtime_->GetClassLinker(); |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 547 | // This holds pointers to the obsolete methods map fields which are updated as needed. |
| 548 | ObsoleteMap map(ext->GetObsoleteMethods(), ext->GetObsoleteDexCaches(), art_klass->GetDexCache()); |
| 549 | CallbackCtx ctx(&map, linker->GetAllocatorForClassLoader(art_klass->GetClassLoader())); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 550 | // Add all the declared methods to the map |
| 551 | for (auto& m : art_klass->GetDeclaredMethods(art::kRuntimePointerSize)) { |
Alex Light | 7532d58 | 2017-02-13 16:36:06 -0800 | [diff] [blame] | 552 | if (m.IsIntrinsic()) { |
| 553 | LOG(WARNING) << "Redefining intrinsic method " << m.PrettyMethod() << ". This may cause the " |
| 554 | << "unexpected use of the original definition of " << m.PrettyMethod() << "in " |
| 555 | << "methods that have already been compiled."; |
| 556 | } |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 557 | // It is possible to simply filter out some methods where they cannot really become obsolete, |
| 558 | // such as native methods and keep their original (possibly optimized) implementations. We don't |
| 559 | // do this, however, since we would need to mark these functions (still in the classes |
| 560 | // declared_methods array) as obsolete so we will find the correct dex file to get meta-data |
| 561 | // from (for example about stack-frame size). Furthermore we would be unable to get some useful |
| 562 | // error checking from the interpreter which ensure we don't try to start executing obsolete |
| 563 | // methods. |
Nicolas Geoffray | 7558d27 | 2017-02-10 10:01:47 +0000 | [diff] [blame] | 564 | ctx.obsolete_methods.insert(&m); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 565 | } |
| 566 | { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 567 | art::MutexLock mu(driver_->self_, *art::Locks::thread_list_lock_); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 568 | art::ThreadList* list = art::Runtime::Current()->GetThreadList(); |
| 569 | list->ForEach(DoAllocateObsoleteMethodsCallback, static_cast<void*>(&ctx)); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 570 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 573 | // Try and get the declared method. First try to get a virtual method then a direct method if that's |
| 574 | // not found. |
| 575 | static art::ArtMethod* FindMethod(art::Handle<art::mirror::Class> klass, |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 576 | art::StringPiece name, |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 577 | art::Signature sig) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 578 | DCHECK(!klass->IsProxyClass()); |
| 579 | for (art::ArtMethod& m : klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize)) { |
| 580 | if (m.GetName() == name && m.GetSignature() == sig) { |
| 581 | return &m; |
| 582 | } |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 583 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 584 | return nullptr; |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | bool Redefiner::ClassRedefinition::CheckSameMethods() { |
| 588 | art::StackHandleScope<1> hs(driver_->self_); |
| 589 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
| 590 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
| 591 | |
| 592 | art::ClassDataItemIterator new_iter(*dex_file_, |
| 593 | dex_file_->GetClassData(dex_file_->GetClassDef(0))); |
| 594 | |
| 595 | // Make sure we have the same number of methods. |
| 596 | uint32_t num_new_method = new_iter.NumVirtualMethods() + new_iter.NumDirectMethods(); |
| 597 | uint32_t num_old_method = h_klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size(); |
| 598 | if (num_new_method != num_old_method) { |
| 599 | bool bigger = num_new_method > num_old_method; |
| 600 | RecordFailure(bigger ? ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED) |
| 601 | : ERR(UNSUPPORTED_REDEFINITION_METHOD_DELETED), |
| 602 | StringPrintf("Total number of declared methods changed from %d to %d", |
| 603 | num_old_method, num_new_method)); |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | // Skip all of the fields. We should have already checked this. |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 608 | new_iter.SkipAllFields(); |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 609 | // Check each of the methods. NB we don't need to specifically check for removals since the 2 dex |
| 610 | // files have the same number of methods, which means there must be an equal amount of additions |
| 611 | // and removals. |
| 612 | for (; new_iter.HasNextVirtualMethod() || new_iter.HasNextDirectMethod(); new_iter.Next()) { |
| 613 | // Get the data on the method we are searching for |
| 614 | const art::DexFile::MethodId& new_method_id = dex_file_->GetMethodId(new_iter.GetMemberIndex()); |
| 615 | const char* new_method_name = dex_file_->GetMethodName(new_method_id); |
| 616 | art::Signature new_method_signature = dex_file_->GetMethodSignature(new_method_id); |
| 617 | art::ArtMethod* old_method = FindMethod(h_klass, new_method_name, new_method_signature); |
| 618 | // If we got past the check for the same number of methods above that means there must be at |
| 619 | // least one added and one removed method. We will return the ADDED failure message since it is |
| 620 | // easier to get a useful error report for it. |
| 621 | if (old_method == nullptr) { |
| 622 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED), |
| 623 | StringPrintf("Unknown method '%s' (sig: %s) was added!", |
| 624 | new_method_name, |
| 625 | new_method_signature.ToString().c_str())); |
| 626 | return false; |
| 627 | } |
| 628 | // Since direct methods have different flags than virtual ones (specifically direct methods must |
| 629 | // have kAccPrivate or kAccStatic or kAccConstructor flags) we can tell if a method changes from |
| 630 | // virtual to direct. |
Mathieu Chartier | f044c22 | 2017-05-31 15:27:54 -0700 | [diff] [blame] | 631 | uint32_t new_flags = new_iter.GetMethodAccessFlags() & ~art::kAccPreviouslyWarm; |
| 632 | if (new_flags != (old_method->GetAccessFlags() & (art::kAccValidMethodFlags ^ art::kAccPreviouslyWarm))) { |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 633 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED), |
| 634 | StringPrintf("method '%s' (sig: %s) had different access flags", |
| 635 | new_method_name, |
| 636 | new_method_signature.ToString().c_str())); |
| 637 | return false; |
| 638 | } |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
| 643 | bool Redefiner::ClassRedefinition::CheckSameFields() { |
| 644 | art::StackHandleScope<1> hs(driver_->self_); |
| 645 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
| 646 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
| 647 | art::ClassDataItemIterator new_iter(*dex_file_, |
| 648 | dex_file_->GetClassData(dex_file_->GetClassDef(0))); |
| 649 | const art::DexFile& old_dex_file = h_klass->GetDexFile(); |
| 650 | art::ClassDataItemIterator old_iter(old_dex_file, |
| 651 | old_dex_file.GetClassData(*h_klass->GetClassDef())); |
| 652 | // Instance and static fields can be differentiated by their flags so no need to check them |
| 653 | // separately. |
| 654 | while (new_iter.HasNextInstanceField() || new_iter.HasNextStaticField()) { |
| 655 | // Get the data on the method we are searching for |
| 656 | const art::DexFile::FieldId& new_field_id = dex_file_->GetFieldId(new_iter.GetMemberIndex()); |
| 657 | const char* new_field_name = dex_file_->GetFieldName(new_field_id); |
| 658 | const char* new_field_type = dex_file_->GetFieldTypeDescriptor(new_field_id); |
| 659 | |
| 660 | if (!(old_iter.HasNextInstanceField() || old_iter.HasNextStaticField())) { |
| 661 | // We are missing the old version of this method! |
| 662 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 663 | StringPrintf("Unknown field '%s' (type: %s) added!", |
| 664 | new_field_name, |
| 665 | new_field_type)); |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | const art::DexFile::FieldId& old_field_id = old_dex_file.GetFieldId(old_iter.GetMemberIndex()); |
| 670 | const char* old_field_name = old_dex_file.GetFieldName(old_field_id); |
| 671 | const char* old_field_type = old_dex_file.GetFieldTypeDescriptor(old_field_id); |
| 672 | |
| 673 | // Check name and type. |
| 674 | if (strcmp(old_field_name, new_field_name) != 0 || |
| 675 | strcmp(old_field_type, new_field_type) != 0) { |
| 676 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 677 | StringPrintf("Field changed from '%s' (sig: %s) to '%s' (sig: %s)!", |
| 678 | old_field_name, |
| 679 | old_field_type, |
| 680 | new_field_name, |
| 681 | new_field_type)); |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | // Since static fields have different flags than instance ones (specifically static fields must |
| 686 | // have the kAccStatic flag) we can tell if a field changes from static to instance. |
| 687 | if (new_iter.GetFieldAccessFlags() != old_iter.GetFieldAccessFlags()) { |
| 688 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 689 | StringPrintf("Field '%s' (sig: %s) had different access flags", |
| 690 | new_field_name, |
| 691 | new_field_type)); |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | new_iter.Next(); |
| 696 | old_iter.Next(); |
| 697 | } |
| 698 | if (old_iter.HasNextInstanceField() || old_iter.HasNextStaticField()) { |
| 699 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 700 | StringPrintf("field '%s' (sig: %s) is missing!", |
| 701 | old_dex_file.GetFieldName(old_dex_file.GetFieldId( |
| 702 | old_iter.GetMemberIndex())), |
| 703 | old_dex_file.GetFieldTypeDescriptor(old_dex_file.GetFieldId( |
| 704 | old_iter.GetMemberIndex())))); |
| 705 | return false; |
| 706 | } |
| 707 | return true; |
| 708 | } |
| 709 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 710 | bool Redefiner::ClassRedefinition::CheckClass() { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 711 | art::StackHandleScope<1> hs(driver_->self_); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 712 | // Easy check that only 1 class def is present. |
| 713 | if (dex_file_->NumClassDefs() != 1) { |
| 714 | RecordFailure(ERR(ILLEGAL_ARGUMENT), |
| 715 | StringPrintf("Expected 1 class def in dex file but found %d", |
| 716 | dex_file_->NumClassDefs())); |
| 717 | return false; |
| 718 | } |
| 719 | // Get the ClassDef from the new DexFile. |
| 720 | // Since the dex file has only a single class def the index is always 0. |
| 721 | const art::DexFile::ClassDef& def = dex_file_->GetClassDef(0); |
| 722 | // Get the class as it is now. |
| 723 | art::Handle<art::mirror::Class> current_class(hs.NewHandle(GetMirrorClass())); |
| 724 | |
| 725 | // Check the access flags didn't change. |
| 726 | if (def.GetJavaAccessFlags() != (current_class->GetAccessFlags() & art::kAccValidClassFlags)) { |
| 727 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED), |
| 728 | "Cannot change modifiers of class by redefinition"); |
| 729 | return false; |
| 730 | } |
| 731 | |
| 732 | // Check class name. |
| 733 | // These should have been checked by the dexfile verifier on load. |
| 734 | DCHECK_NE(def.class_idx_, art::dex::TypeIndex::Invalid()) << "Invalid type index"; |
| 735 | const char* descriptor = dex_file_->StringByTypeIdx(def.class_idx_); |
| 736 | DCHECK(descriptor != nullptr) << "Invalid dex file structure!"; |
| 737 | if (!current_class->DescriptorEquals(descriptor)) { |
| 738 | std::string storage; |
| 739 | RecordFailure(ERR(NAMES_DONT_MATCH), |
| 740 | StringPrintf("expected file to contain class called '%s' but found '%s'!", |
| 741 | current_class->GetDescriptor(&storage), |
| 742 | descriptor)); |
| 743 | return false; |
| 744 | } |
| 745 | if (current_class->IsObjectClass()) { |
| 746 | if (def.superclass_idx_ != art::dex::TypeIndex::Invalid()) { |
| 747 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass added!"); |
| 748 | return false; |
| 749 | } |
| 750 | } else { |
| 751 | const char* super_descriptor = dex_file_->StringByTypeIdx(def.superclass_idx_); |
| 752 | DCHECK(descriptor != nullptr) << "Invalid dex file structure!"; |
| 753 | if (!current_class->GetSuperClass()->DescriptorEquals(super_descriptor)) { |
| 754 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass changed"); |
| 755 | return false; |
| 756 | } |
| 757 | } |
| 758 | const art::DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(def); |
| 759 | if (interfaces == nullptr) { |
| 760 | if (current_class->NumDirectInterfaces() != 0) { |
| 761 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added"); |
| 762 | return false; |
| 763 | } |
| 764 | } else { |
| 765 | DCHECK(!current_class->IsProxyClass()); |
| 766 | const art::DexFile::TypeList* current_interfaces = current_class->GetInterfaceTypeList(); |
| 767 | if (current_interfaces == nullptr || current_interfaces->Size() != interfaces->Size()) { |
| 768 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added or removed"); |
| 769 | return false; |
| 770 | } |
| 771 | // The order of interfaces is (barely) meaningful so we error if it changes. |
| 772 | const art::DexFile& orig_dex_file = current_class->GetDexFile(); |
| 773 | for (uint32_t i = 0; i < interfaces->Size(); i++) { |
| 774 | if (strcmp( |
| 775 | dex_file_->StringByTypeIdx(interfaces->GetTypeItem(i).type_idx_), |
| 776 | orig_dex_file.StringByTypeIdx(current_interfaces->GetTypeItem(i).type_idx_)) != 0) { |
| 777 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), |
| 778 | "Interfaces changed or re-ordered"); |
| 779 | return false; |
| 780 | } |
| 781 | } |
| 782 | } |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 783 | return true; |
| 784 | } |
| 785 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 786 | bool Redefiner::ClassRedefinition::CheckRedefinable() { |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 787 | std::string err; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 788 | art::StackHandleScope<1> hs(driver_->self_); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 789 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 790 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
| 791 | jvmtiError res = Redefiner::GetClassRedefinitionError(h_klass, &err); |
| 792 | if (res != OK) { |
| 793 | RecordFailure(res, err); |
| 794 | return false; |
| 795 | } else { |
| 796 | return true; |
| 797 | } |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 800 | bool Redefiner::ClassRedefinition::CheckRedefinitionIsValid() { |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 801 | return CheckRedefinable() && |
| 802 | CheckClass() && |
| 803 | CheckSameFields() && |
| 804 | CheckSameMethods(); |
| 805 | } |
| 806 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 807 | class RedefinitionDataIter; |
| 808 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 809 | // A wrapper that lets us hold onto the arbitrary sized data needed for redefinitions in a |
| 810 | // reasonably sane way. This adds no fields to the normal ObjectArray. By doing this we can avoid |
| 811 | // having to deal with the fact that we need to hold an arbitrary number of references live. |
| 812 | class RedefinitionDataHolder { |
| 813 | public: |
| 814 | enum DataSlot : int32_t { |
| 815 | kSlotSourceClassLoader = 0, |
| 816 | kSlotJavaDexFile = 1, |
| 817 | kSlotNewDexFileCookie = 2, |
| 818 | kSlotNewDexCache = 3, |
| 819 | kSlotMirrorClass = 4, |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 820 | kSlotOrigDexFile = 5, |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 821 | kSlotOldObsoleteMethods = 6, |
| 822 | kSlotOldDexCaches = 7, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 823 | |
| 824 | // Must be last one. |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 825 | kNumSlots = 8, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 826 | }; |
| 827 | |
| 828 | // This needs to have a HandleScope passed in that is capable of creating a new Handle without |
| 829 | // overflowing. Only one handle will be created. This object has a lifetime identical to that of |
| 830 | // the passed in handle-scope. |
| 831 | RedefinitionDataHolder(art::StackHandleScope<1>* hs, |
| 832 | art::Runtime* runtime, |
| 833 | art::Thread* self, |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 834 | std::vector<Redefiner::ClassRedefinition>* redefinitions) |
| 835 | REQUIRES_SHARED(art::Locks::mutator_lock_) : |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 836 | arr_( |
| 837 | hs->NewHandle( |
| 838 | art::mirror::ObjectArray<art::mirror::Object>::Alloc( |
| 839 | self, |
| 840 | runtime->GetClassLinker()->GetClassRoot(art::ClassLinker::kObjectArrayClass), |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 841 | redefinitions->size() * kNumSlots))), |
| 842 | redefinitions_(redefinitions) {} |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 843 | |
| 844 | bool IsNull() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 845 | return arr_.IsNull(); |
| 846 | } |
| 847 | |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 848 | art::mirror::ClassLoader* GetSourceClassLoader(jint klass_index) const |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 849 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 850 | return art::down_cast<art::mirror::ClassLoader*>(GetSlot(klass_index, kSlotSourceClassLoader)); |
| 851 | } |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 852 | art::mirror::Object* GetJavaDexFile(jint klass_index) const |
| 853 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 854 | return GetSlot(klass_index, kSlotJavaDexFile); |
| 855 | } |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 856 | art::mirror::LongArray* GetNewDexFileCookie(jint klass_index) const |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 857 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 858 | return art::down_cast<art::mirror::LongArray*>(GetSlot(klass_index, kSlotNewDexFileCookie)); |
| 859 | } |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 860 | art::mirror::DexCache* GetNewDexCache(jint klass_index) const |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 861 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 862 | return art::down_cast<art::mirror::DexCache*>(GetSlot(klass_index, kSlotNewDexCache)); |
| 863 | } |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 864 | art::mirror::Class* GetMirrorClass(jint klass_index) const |
| 865 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 866 | return art::down_cast<art::mirror::Class*>(GetSlot(klass_index, kSlotMirrorClass)); |
| 867 | } |
| 868 | |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 869 | art::mirror::Object* GetOriginalDexFile(jint klass_index) const |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 870 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 871 | return art::down_cast<art::mirror::Object*>(GetSlot(klass_index, kSlotOrigDexFile)); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 874 | art::mirror::PointerArray* GetOldObsoleteMethods(jint klass_index) const |
| 875 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 876 | return art::down_cast<art::mirror::PointerArray*>( |
| 877 | GetSlot(klass_index, kSlotOldObsoleteMethods)); |
| 878 | } |
| 879 | |
| 880 | art::mirror::ObjectArray<art::mirror::DexCache>* GetOldDexCaches(jint klass_index) const |
| 881 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 882 | return art::down_cast<art::mirror::ObjectArray<art::mirror::DexCache>*>( |
| 883 | GetSlot(klass_index, kSlotOldDexCaches)); |
| 884 | } |
| 885 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 886 | void SetSourceClassLoader(jint klass_index, art::mirror::ClassLoader* loader) |
| 887 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 888 | SetSlot(klass_index, kSlotSourceClassLoader, loader); |
| 889 | } |
| 890 | void SetJavaDexFile(jint klass_index, art::mirror::Object* dexfile) |
| 891 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 892 | SetSlot(klass_index, kSlotJavaDexFile, dexfile); |
| 893 | } |
| 894 | void SetNewDexFileCookie(jint klass_index, art::mirror::LongArray* cookie) |
| 895 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 896 | SetSlot(klass_index, kSlotNewDexFileCookie, cookie); |
| 897 | } |
| 898 | void SetNewDexCache(jint klass_index, art::mirror::DexCache* cache) |
| 899 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 900 | SetSlot(klass_index, kSlotNewDexCache, cache); |
| 901 | } |
| 902 | void SetMirrorClass(jint klass_index, art::mirror::Class* klass) |
| 903 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 904 | SetSlot(klass_index, kSlotMirrorClass, klass); |
| 905 | } |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 906 | void SetOriginalDexFile(jint klass_index, art::mirror::Object* bytes) |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 907 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 908 | SetSlot(klass_index, kSlotOrigDexFile, bytes); |
| 909 | } |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 910 | void SetOldObsoleteMethods(jint klass_index, art::mirror::PointerArray* methods) |
| 911 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 912 | SetSlot(klass_index, kSlotOldObsoleteMethods, methods); |
| 913 | } |
| 914 | void SetOldDexCaches(jint klass_index, art::mirror::ObjectArray<art::mirror::DexCache>* caches) |
| 915 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 916 | SetSlot(klass_index, kSlotOldDexCaches, caches); |
| 917 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 918 | |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 919 | int32_t Length() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 920 | return arr_->GetLength() / kNumSlots; |
| 921 | } |
| 922 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 923 | std::vector<Redefiner::ClassRedefinition>* GetRedefinitions() |
| 924 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 925 | return redefinitions_; |
| 926 | } |
| 927 | |
| 928 | bool operator==(const RedefinitionDataHolder& other) const |
| 929 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 930 | return arr_.Get() == other.arr_.Get(); |
| 931 | } |
| 932 | |
| 933 | bool operator!=(const RedefinitionDataHolder& other) const |
| 934 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 935 | return !(*this == other); |
| 936 | } |
| 937 | |
| 938 | RedefinitionDataIter begin() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 939 | RedefinitionDataIter end() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 940 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 941 | private: |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 942 | mutable art::Handle<art::mirror::ObjectArray<art::mirror::Object>> arr_; |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 943 | std::vector<Redefiner::ClassRedefinition>* redefinitions_; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 944 | |
| 945 | art::mirror::Object* GetSlot(jint klass_index, |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 946 | DataSlot slot) const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 947 | DCHECK_LT(klass_index, Length()); |
| 948 | return arr_->Get((kNumSlots * klass_index) + slot); |
| 949 | } |
| 950 | |
| 951 | void SetSlot(jint klass_index, |
| 952 | DataSlot slot, |
| 953 | art::ObjPtr<art::mirror::Object> obj) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 954 | DCHECK(!art::Runtime::Current()->IsActiveTransaction()); |
| 955 | DCHECK_LT(klass_index, Length()); |
| 956 | arr_->Set<false>((kNumSlots * klass_index) + slot, obj); |
| 957 | } |
| 958 | |
| 959 | DISALLOW_COPY_AND_ASSIGN(RedefinitionDataHolder); |
| 960 | }; |
| 961 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 962 | class RedefinitionDataIter { |
| 963 | public: |
| 964 | RedefinitionDataIter(int32_t idx, RedefinitionDataHolder& holder) : idx_(idx), holder_(holder) {} |
| 965 | |
| 966 | RedefinitionDataIter(const RedefinitionDataIter&) = default; |
| 967 | RedefinitionDataIter(RedefinitionDataIter&&) = default; |
| 968 | RedefinitionDataIter& operator=(const RedefinitionDataIter&) = default; |
| 969 | RedefinitionDataIter& operator=(RedefinitionDataIter&&) = default; |
| 970 | |
| 971 | bool operator==(const RedefinitionDataIter& other) const |
| 972 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 973 | return idx_ == other.idx_ && holder_ == other.holder_; |
| 974 | } |
| 975 | |
| 976 | bool operator!=(const RedefinitionDataIter& other) const |
| 977 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 978 | return !(*this == other); |
| 979 | } |
| 980 | |
| 981 | RedefinitionDataIter operator++() { // Value after modification. |
| 982 | idx_++; |
| 983 | return *this; |
| 984 | } |
| 985 | |
| 986 | RedefinitionDataIter operator++(int) { |
| 987 | RedefinitionDataIter temp = *this; |
| 988 | idx_++; |
| 989 | return temp; |
| 990 | } |
| 991 | |
| 992 | RedefinitionDataIter operator+(ssize_t delta) const { |
| 993 | RedefinitionDataIter temp = *this; |
| 994 | temp += delta; |
| 995 | return temp; |
| 996 | } |
| 997 | |
| 998 | RedefinitionDataIter& operator+=(ssize_t delta) { |
| 999 | idx_ += delta; |
| 1000 | return *this; |
| 1001 | } |
| 1002 | |
| 1003 | Redefiner::ClassRedefinition& GetRedefinition() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1004 | return (*holder_.GetRedefinitions())[idx_]; |
| 1005 | } |
| 1006 | |
| 1007 | RedefinitionDataHolder& GetHolder() { |
| 1008 | return holder_; |
| 1009 | } |
| 1010 | |
| 1011 | art::mirror::ClassLoader* GetSourceClassLoader() const |
| 1012 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1013 | return holder_.GetSourceClassLoader(idx_); |
| 1014 | } |
| 1015 | art::mirror::Object* GetJavaDexFile() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1016 | return holder_.GetJavaDexFile(idx_); |
| 1017 | } |
| 1018 | art::mirror::LongArray* GetNewDexFileCookie() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1019 | return holder_.GetNewDexFileCookie(idx_); |
| 1020 | } |
| 1021 | art::mirror::DexCache* GetNewDexCache() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1022 | return holder_.GetNewDexCache(idx_); |
| 1023 | } |
| 1024 | art::mirror::Class* GetMirrorClass() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1025 | return holder_.GetMirrorClass(idx_); |
| 1026 | } |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1027 | art::mirror::Object* GetOriginalDexFile() const |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1028 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1029 | return holder_.GetOriginalDexFile(idx_); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1030 | } |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1031 | art::mirror::PointerArray* GetOldObsoleteMethods() const |
| 1032 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1033 | return holder_.GetOldObsoleteMethods(idx_); |
| 1034 | } |
| 1035 | art::mirror::ObjectArray<art::mirror::DexCache>* GetOldDexCaches() const |
| 1036 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1037 | return holder_.GetOldDexCaches(idx_); |
| 1038 | } |
| 1039 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1040 | int32_t GetIndex() const { |
| 1041 | return idx_; |
| 1042 | } |
| 1043 | |
| 1044 | void SetSourceClassLoader(art::mirror::ClassLoader* loader) |
| 1045 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1046 | holder_.SetSourceClassLoader(idx_, loader); |
| 1047 | } |
| 1048 | void SetJavaDexFile(art::mirror::Object* dexfile) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1049 | holder_.SetJavaDexFile(idx_, dexfile); |
| 1050 | } |
| 1051 | void SetNewDexFileCookie(art::mirror::LongArray* cookie) |
| 1052 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1053 | holder_.SetNewDexFileCookie(idx_, cookie); |
| 1054 | } |
| 1055 | void SetNewDexCache(art::mirror::DexCache* cache) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1056 | holder_.SetNewDexCache(idx_, cache); |
| 1057 | } |
| 1058 | void SetMirrorClass(art::mirror::Class* klass) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1059 | holder_.SetMirrorClass(idx_, klass); |
| 1060 | } |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1061 | void SetOriginalDexFile(art::mirror::Object* bytes) |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1062 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1063 | holder_.SetOriginalDexFile(idx_, bytes); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1064 | } |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1065 | void SetOldObsoleteMethods(art::mirror::PointerArray* methods) |
| 1066 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1067 | holder_.SetOldObsoleteMethods(idx_, methods); |
| 1068 | } |
| 1069 | void SetOldDexCaches(art::mirror::ObjectArray<art::mirror::DexCache>* caches) |
| 1070 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1071 | holder_.SetOldDexCaches(idx_, caches); |
| 1072 | } |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1073 | |
| 1074 | private: |
| 1075 | int32_t idx_; |
| 1076 | RedefinitionDataHolder& holder_; |
| 1077 | }; |
| 1078 | |
| 1079 | RedefinitionDataIter RedefinitionDataHolder::begin() { |
| 1080 | return RedefinitionDataIter(0, *this); |
| 1081 | } |
| 1082 | |
| 1083 | RedefinitionDataIter RedefinitionDataHolder::end() { |
| 1084 | return RedefinitionDataIter(Length(), *this); |
| 1085 | } |
| 1086 | |
| 1087 | bool Redefiner::ClassRedefinition::CheckVerification(const RedefinitionDataIter& iter) { |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1088 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
| 1089 | art::StackHandleScope<2> hs(driver_->self_); |
| 1090 | std::string error; |
| 1091 | // TODO Make verification log level lower |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 1092 | art::verifier::FailureKind failure = |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1093 | art::verifier::MethodVerifier::VerifyClass(driver_->self_, |
| 1094 | dex_file_.get(), |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1095 | hs.NewHandle(iter.GetNewDexCache()), |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1096 | hs.NewHandle(GetClassLoader()), |
| 1097 | dex_file_->GetClassDef(0), /*class_def*/ |
| 1098 | nullptr, /*compiler_callbacks*/ |
| 1099 | false, /*allow_soft_failures*/ |
| 1100 | /*log_level*/ |
| 1101 | art::verifier::HardFailLogMode::kLogWarning, |
| 1102 | &error); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 1103 | bool passes = failure == art::verifier::FailureKind::kNoFailure; |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1104 | if (!passes) { |
| 1105 | RecordFailure(ERR(FAILS_VERIFICATION), "Failed to verify class. Error was: " + error); |
| 1106 | } |
| 1107 | return passes; |
| 1108 | } |
| 1109 | |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1110 | // Looks through the previously allocated cookies to see if we need to update them with another new |
| 1111 | // dexfile. This is so that even if multiple classes with the same classloader are redefined at |
| 1112 | // once they are all added to the classloader. |
| 1113 | bool Redefiner::ClassRedefinition::AllocateAndRememberNewDexFileCookie( |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1114 | art::Handle<art::mirror::ClassLoader> source_class_loader, |
| 1115 | art::Handle<art::mirror::Object> dex_file_obj, |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1116 | /*out*/RedefinitionDataIter* cur_data) { |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1117 | art::StackHandleScope<2> hs(driver_->self_); |
| 1118 | art::MutableHandle<art::mirror::LongArray> old_cookie( |
| 1119 | hs.NewHandle<art::mirror::LongArray>(nullptr)); |
| 1120 | bool has_older_cookie = false; |
| 1121 | // See if we already have a cookie that a previous redefinition got from the same classloader. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1122 | for (auto old_data = cur_data->GetHolder().begin(); old_data != *cur_data; ++old_data) { |
| 1123 | if (old_data.GetSourceClassLoader() == source_class_loader.Get()) { |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1124 | // Since every instance of this classloader should have the same cookie associated with it we |
| 1125 | // can stop looking here. |
| 1126 | has_older_cookie = true; |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1127 | old_cookie.Assign(old_data.GetNewDexFileCookie()); |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | if (old_cookie.IsNull()) { |
| 1132 | // No older cookie. Get it directly from the dex_file_obj |
| 1133 | // We should not have seen this classloader elsewhere. |
| 1134 | CHECK(!has_older_cookie); |
| 1135 | old_cookie.Assign(ClassLoaderHelper::GetDexFileCookie(dex_file_obj)); |
| 1136 | } |
| 1137 | // Use the old cookie to generate the new one with the new DexFile* added in. |
| 1138 | art::Handle<art::mirror::LongArray> |
| 1139 | new_cookie(hs.NewHandle(ClassLoaderHelper::AllocateNewDexFileCookie(driver_->self_, |
| 1140 | old_cookie, |
| 1141 | dex_file_.get()))); |
| 1142 | // Make sure the allocation worked. |
| 1143 | if (new_cookie.IsNull()) { |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
| 1147 | // Save the cookie. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1148 | cur_data->SetNewDexFileCookie(new_cookie.Get()); |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1149 | // If there are other copies of this same classloader we need to make sure that we all have the |
| 1150 | // same cookie. |
| 1151 | if (has_older_cookie) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1152 | for (auto old_data = cur_data->GetHolder().begin(); old_data != *cur_data; ++old_data) { |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1153 | // We will let the GC take care of the cookie we allocated for this one. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1154 | if (old_data.GetSourceClassLoader() == source_class_loader.Get()) { |
| 1155 | old_data.SetNewDexFileCookie(new_cookie.Get()); |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | return true; |
| 1161 | } |
| 1162 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1163 | bool Redefiner::ClassRedefinition::FinishRemainingAllocations( |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1164 | /*out*/RedefinitionDataIter* cur_data) { |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1165 | art::ScopedObjectAccessUnchecked soa(driver_->self_); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1166 | art::StackHandleScope<2> hs(driver_->self_); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1167 | cur_data->SetMirrorClass(GetMirrorClass()); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1168 | // This shouldn't allocate |
| 1169 | art::Handle<art::mirror::ClassLoader> loader(hs.NewHandle(GetClassLoader())); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1170 | // The bootclasspath is handled specially so it doesn't have a j.l.DexFile. |
| 1171 | if (!art::ClassLinker::IsBootClassLoader(soa, loader.Get())) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1172 | cur_data->SetSourceClassLoader(loader.Get()); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1173 | art::Handle<art::mirror::Object> dex_file_obj(hs.NewHandle( |
| 1174 | ClassLoaderHelper::FindSourceDexFileObject(driver_->self_, loader))); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1175 | cur_data->SetJavaDexFile(dex_file_obj.Get()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1176 | if (dex_file_obj == nullptr) { |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1177 | RecordFailure(ERR(INTERNAL), "Unable to find dex file!"); |
| 1178 | return false; |
| 1179 | } |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1180 | // Allocate the new dex file cookie. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1181 | if (!AllocateAndRememberNewDexFileCookie(loader, dex_file_obj, cur_data)) { |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1182 | driver_->self_->AssertPendingOOMException(); |
| 1183 | driver_->self_->ClearException(); |
| 1184 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate dex file array for class loader"); |
| 1185 | return false; |
| 1186 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1187 | } |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1188 | cur_data->SetNewDexCache(CreateNewDexCache(loader)); |
| 1189 | if (cur_data->GetNewDexCache() == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1190 | driver_->self_->AssertPendingException(); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1191 | driver_->self_->ClearException(); |
| 1192 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate DexCache"); |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
| 1196 | // We won't always need to set this field. |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1197 | cur_data->SetOriginalDexFile(AllocateOrGetOriginalDexFile()); |
| 1198 | if (cur_data->GetOriginalDexFile() == nullptr) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1199 | driver_->self_->AssertPendingOOMException(); |
| 1200 | driver_->self_->ClearException(); |
| 1201 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate array for original dex file"); |
| 1202 | return false; |
| 1203 | } |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 1207 | void Redefiner::ClassRedefinition::UnregisterJvmtiBreakpoints() { |
| 1208 | BreakpointUtil::RemoveBreakpointsInClass(driver_->env_, GetMirrorClass()); |
| 1209 | } |
| 1210 | |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 1211 | void Redefiner::ClassRedefinition::UnregisterBreakpoints() { |
| 1212 | DCHECK(art::Dbg::IsDebuggerActive()); |
| 1213 | art::JDWP::JdwpState* state = art::Dbg::GetJdwpState(); |
| 1214 | if (state != nullptr) { |
| 1215 | state->UnregisterLocationEventsOnClass(GetMirrorClass()); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | void Redefiner::UnregisterAllBreakpoints() { |
| 1220 | if (LIKELY(!art::Dbg::IsDebuggerActive())) { |
| 1221 | return; |
| 1222 | } |
| 1223 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 1224 | redef.UnregisterBreakpoints(); |
| 1225 | } |
| 1226 | } |
| 1227 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1228 | bool Redefiner::CheckAllRedefinitionAreValid() { |
| 1229 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 1230 | if (!redef.CheckRedefinitionIsValid()) { |
| 1231 | return false; |
| 1232 | } |
| 1233 | } |
| 1234 | return true; |
| 1235 | } |
| 1236 | |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1237 | void Redefiner::RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder) { |
| 1238 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 1239 | data.GetRedefinition().RestoreObsoleteMethodMapsIfUnneeded(&data); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | bool Redefiner::EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder) { |
| 1244 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 1245 | if (!data.GetRedefinition().EnsureClassAllocationsFinished(&data)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1246 | return false; |
| 1247 | } |
| 1248 | } |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
| 1252 | bool Redefiner::FinishAllRemainingAllocations(RedefinitionDataHolder& holder) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1253 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1254 | // Allocate the data this redefinition requires. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1255 | if (!data.GetRedefinition().FinishRemainingAllocations(&data)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1256 | return false; |
| 1257 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1258 | } |
| 1259 | return true; |
| 1260 | } |
| 1261 | |
| 1262 | void Redefiner::ClassRedefinition::ReleaseDexFile() { |
| 1263 | dex_file_.release(); |
| 1264 | } |
| 1265 | |
| 1266 | void Redefiner::ReleaseAllDexFiles() { |
| 1267 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 1268 | redef.ReleaseDexFile(); |
| 1269 | } |
| 1270 | } |
| 1271 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1272 | bool Redefiner::CheckAllClassesAreVerified(RedefinitionDataHolder& holder) { |
| 1273 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 1274 | if (!data.GetRedefinition().CheckVerification(data)) { |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1275 | return false; |
| 1276 | } |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1277 | } |
| 1278 | return true; |
| 1279 | } |
| 1280 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1281 | class ScopedDisableConcurrentAndMovingGc { |
| 1282 | public: |
| 1283 | ScopedDisableConcurrentAndMovingGc(art::gc::Heap* heap, art::Thread* self) |
| 1284 | : heap_(heap), self_(self) { |
| 1285 | if (heap_->IsGcConcurrentAndMoving()) { |
| 1286 | heap_->IncrementDisableMovingGC(self_); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | ~ScopedDisableConcurrentAndMovingGc() { |
| 1291 | if (heap_->IsGcConcurrentAndMoving()) { |
| 1292 | heap_->DecrementDisableMovingGC(self_); |
| 1293 | } |
| 1294 | } |
| 1295 | private: |
| 1296 | art::gc::Heap* heap_; |
| 1297 | art::Thread* self_; |
| 1298 | }; |
| 1299 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1300 | jvmtiError Redefiner::Run() { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1301 | art::StackHandleScope<1> hs(self_); |
| 1302 | // Allocate an array to hold onto all java temporary objects associated with this redefinition. |
| 1303 | // We will let this be collected after the end of this function. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1304 | RedefinitionDataHolder holder(&hs, runtime_, self_, &redefinitions_); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1305 | if (holder.IsNull()) { |
| 1306 | self_->AssertPendingOOMException(); |
| 1307 | self_->ClearException(); |
| 1308 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate storage for temporaries"); |
| 1309 | return result_; |
| 1310 | } |
| 1311 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1312 | // First we just allocate the ClassExt and its fields that we need. These can be updated |
| 1313 | // atomically without any issues (since we allocate the map arrays as empty) so we don't bother |
| 1314 | // doing a try loop. The other allocations we need to ensure that nothing has changed in the time |
| 1315 | // between allocating them and pausing all threads before we can update them so we need to do a |
| 1316 | // try loop. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1317 | if (!CheckAllRedefinitionAreValid() || |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1318 | !EnsureAllClassAllocationsFinished(holder) || |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1319 | !FinishAllRemainingAllocations(holder) || |
| 1320 | !CheckAllClassesAreVerified(holder)) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1321 | return result_; |
| 1322 | } |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1323 | |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 1324 | // At this point we can no longer fail without corrupting the runtime state. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1325 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 1326 | art::ClassLinker* cl = runtime_->GetClassLinker(); |
| 1327 | cl->RegisterExistingDexCache(data.GetNewDexCache(), data.GetSourceClassLoader()); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1328 | if (data.GetSourceClassLoader() == nullptr) { |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 1329 | cl->AppendToBootClassPath(self_, data.GetRedefinition().GetDexFile()); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1330 | } |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1331 | } |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 1332 | UnregisterAllBreakpoints(); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1333 | |
Alex Light | 6abd539 | 2017-01-05 17:53:00 -0800 | [diff] [blame] | 1334 | // Disable GC and wait for it to be done if we are a moving GC. This is fine since we are done |
| 1335 | // allocating so no deadlocks. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1336 | ScopedDisableConcurrentAndMovingGc sdcamgc(runtime_->GetHeap(), self_); |
| 1337 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1338 | // Do transition to final suspension |
| 1339 | // TODO We might want to give this its own suspended state! |
| 1340 | // TODO This isn't right. We need to change state without any chance of suspend ideally! |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1341 | art::ScopedThreadSuspension sts(self_, art::ThreadState::kNative); |
| 1342 | art::ScopedSuspendAll ssa("Final installation of redefined Classes!", /*long_suspend*/true); |
| 1343 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 1344 | art::ScopedAssertNoThreadSuspension nts("Updating runtime objects for redefinition"); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1345 | ClassRedefinition& redef = data.GetRedefinition(); |
| 1346 | if (data.GetSourceClassLoader() != nullptr) { |
| 1347 | ClassLoaderHelper::UpdateJavaDexFile(data.GetJavaDexFile(), data.GetNewDexFileCookie()); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1348 | } |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1349 | art::mirror::Class* klass = data.GetMirrorClass(); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1350 | // TODO Rewrite so we don't do a stack walk for each and every class. |
| 1351 | redef.FindAndAllocateObsoleteMethods(klass); |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1352 | redef.UpdateClass(klass, data.GetNewDexCache(), data.GetOriginalDexFile()); |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 1353 | redef.UnregisterJvmtiBreakpoints(); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1354 | } |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1355 | RestoreObsoleteMethodMapsIfUnneeded(holder); |
Alex Light | 7532d58 | 2017-02-13 16:36:06 -0800 | [diff] [blame] | 1356 | // TODO We should check for if any of the redefined methods are intrinsic methods here and, if any |
| 1357 | // are, force a full-world deoptimization before finishing redefinition. If we don't do this then |
| 1358 | // methods that have been jitted prior to the current redefinition being applied might continue |
| 1359 | // to use the old versions of the intrinsics! |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1360 | // TODO Do the dex_file release at a more reasonable place. This works but it muddles who really |
| 1361 | // owns the DexFile and when ownership is transferred. |
| 1362 | ReleaseAllDexFiles(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1363 | return OK; |
| 1364 | } |
| 1365 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1366 | void Redefiner::ClassRedefinition::UpdateMethods(art::ObjPtr<art::mirror::Class> mclass, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1367 | const art::DexFile::ClassDef& class_def) { |
| 1368 | art::ClassLinker* linker = driver_->runtime_->GetClassLinker(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1369 | art::PointerSize image_pointer_size = linker->GetImagePointerSize(); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1370 | const art::DexFile::TypeId& declaring_class_id = dex_file_->GetTypeId(class_def.class_idx_); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1371 | const art::DexFile& old_dex_file = mclass->GetDexFile(); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1372 | // Update methods. |
Alex Light | 00e475c | 2017-07-19 16:36:23 -0700 | [diff] [blame] | 1373 | for (art::ArtMethod& method : mclass->GetDeclaredMethods(image_pointer_size)) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1374 | const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(method.GetName()); |
| 1375 | art::dex::TypeIndex method_return_idx = |
| 1376 | dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(method.GetReturnTypeDescriptor())); |
| 1377 | const auto* old_type_list = method.GetParameterTypeList(); |
| 1378 | std::vector<art::dex::TypeIndex> new_type_list; |
| 1379 | for (uint32_t i = 0; old_type_list != nullptr && i < old_type_list->Size(); i++) { |
| 1380 | new_type_list.push_back( |
| 1381 | dex_file_->GetIndexForTypeId( |
| 1382 | *dex_file_->FindTypeId( |
| 1383 | old_dex_file.GetTypeDescriptor( |
| 1384 | old_dex_file.GetTypeId( |
| 1385 | old_type_list->GetTypeItem(i).type_idx_))))); |
| 1386 | } |
| 1387 | const art::DexFile::ProtoId* proto_id = dex_file_->FindProtoId(method_return_idx, |
| 1388 | new_type_list); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1389 | CHECK(proto_id != nullptr || old_type_list == nullptr); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1390 | const art::DexFile::MethodId* method_id = dex_file_->FindMethodId(declaring_class_id, |
| 1391 | *new_name_id, |
| 1392 | *proto_id); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1393 | CHECK(method_id != nullptr); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1394 | uint32_t dex_method_idx = dex_file_->GetIndexForMethodId(*method_id); |
| 1395 | method.SetDexMethodIndex(dex_method_idx); |
| 1396 | linker->SetEntryPointsToInterpreter(&method); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1397 | method.SetCodeItemOffset(dex_file_->FindCodeItemOffset(class_def, dex_method_idx)); |
Alex Light | 7532d58 | 2017-02-13 16:36:06 -0800 | [diff] [blame] | 1398 | // Clear all the intrinsics related flags. |
| 1399 | method.ClearAccessFlags(art::kAccIntrinsic | (~art::kAccFlagsNotUsedByIntrinsic)); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1400 | // Notify the jit that this method is redefined. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1401 | art::jit::Jit* jit = driver_->runtime_->GetJit(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1402 | if (jit != nullptr) { |
| 1403 | jit->GetCodeCache()->NotifyMethodRedefined(&method); |
| 1404 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1405 | } |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1406 | } |
| 1407 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1408 | void Redefiner::ClassRedefinition::UpdateFields(art::ObjPtr<art::mirror::Class> mclass) { |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1409 | // TODO The IFields & SFields pointers should be combined like the methods_ arrays were. |
| 1410 | for (auto fields_iter : {mclass->GetIFields(), mclass->GetSFields()}) { |
| 1411 | for (art::ArtField& field : fields_iter) { |
| 1412 | std::string declaring_class_name; |
| 1413 | const art::DexFile::TypeId* new_declaring_id = |
| 1414 | dex_file_->FindTypeId(field.GetDeclaringClass()->GetDescriptor(&declaring_class_name)); |
| 1415 | const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(field.GetName()); |
| 1416 | const art::DexFile::TypeId* new_type_id = dex_file_->FindTypeId(field.GetTypeDescriptor()); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1417 | CHECK(new_name_id != nullptr && new_type_id != nullptr && new_declaring_id != nullptr); |
| 1418 | const art::DexFile::FieldId* new_field_id = |
| 1419 | dex_file_->FindFieldId(*new_declaring_id, *new_name_id, *new_type_id); |
| 1420 | CHECK(new_field_id != nullptr); |
| 1421 | // We only need to update the index since the other data in the ArtField cannot be updated. |
| 1422 | field.SetDexFieldIndex(dex_file_->GetIndexForFieldId(*new_field_id)); |
| 1423 | } |
| 1424 | } |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | // Performs updates to class that will allow us to verify it. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1428 | void Redefiner::ClassRedefinition::UpdateClass( |
| 1429 | art::ObjPtr<art::mirror::Class> mclass, |
| 1430 | art::ObjPtr<art::mirror::DexCache> new_dex_cache, |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1431 | art::ObjPtr<art::mirror::Object> original_dex_file) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 1432 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
| 1433 | const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(0); |
Vladimir Marko | 5122e6b | 2017-08-17 16:10:09 +0100 | [diff] [blame] | 1434 | UpdateMethods(mclass, class_def); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 1435 | UpdateFields(mclass); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1436 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1437 | // Update the class fields. |
| 1438 | // Need to update class last since the ArtMethod gets its DexFile from the class (which is needed |
| 1439 | // to call GetReturnTypeDescriptor and GetParameterTypeList above). |
| 1440 | mclass->SetDexCache(new_dex_cache.Ptr()); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 1441 | mclass->SetDexClassDefIndex(dex_file_->GetIndexForClassDef(class_def)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1442 | mclass->SetDexTypeIndex(dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(class_sig_.c_str()))); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1443 | art::ObjPtr<art::mirror::ClassExt> ext(mclass->GetExtData()); |
| 1444 | CHECK(!ext.IsNull()); |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1445 | ext->SetOriginalDexFile(original_dex_file); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1448 | // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no new |
| 1449 | // obsolete methods). |
| 1450 | void Redefiner::ClassRedefinition::RestoreObsoleteMethodMapsIfUnneeded( |
| 1451 | const RedefinitionDataIter* cur_data) { |
| 1452 | art::mirror::Class* klass = GetMirrorClass(); |
| 1453 | art::mirror::ClassExt* ext = klass->GetExtData(); |
| 1454 | art::mirror::PointerArray* methods = ext->GetObsoleteMethods(); |
Alex Light | 70713df | 2017-04-18 13:03:31 -0700 | [diff] [blame] | 1455 | art::mirror::PointerArray* old_methods = cur_data->GetOldObsoleteMethods(); |
| 1456 | int32_t old_length = old_methods == nullptr ? 0 : old_methods->GetLength(); |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1457 | int32_t expected_length = |
| 1458 | old_length + klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods(); |
| 1459 | // Check to make sure we are only undoing this one. |
| 1460 | if (expected_length == methods->GetLength()) { |
Alex Light | 70713df | 2017-04-18 13:03:31 -0700 | [diff] [blame] | 1461 | for (int32_t i = 0; i < expected_length; i++) { |
| 1462 | art::ArtMethod* expected = nullptr; |
| 1463 | if (i < old_length) { |
| 1464 | expected = old_methods->GetElementPtrSize<art::ArtMethod*>(i, art::kRuntimePointerSize); |
| 1465 | } |
| 1466 | if (methods->GetElementPtrSize<art::ArtMethod*>(i, art::kRuntimePointerSize) != expected) { |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1467 | // We actually have some new obsolete methods. Just abort since we cannot safely shrink the |
| 1468 | // obsolete methods array. |
| 1469 | return; |
| 1470 | } |
| 1471 | } |
| 1472 | // No new obsolete methods! We can get rid of the maps. |
| 1473 | ext->SetObsoleteArrays(cur_data->GetOldObsoleteMethods(), cur_data->GetOldDexCaches()); |
| 1474 | } |
| 1475 | } |
| 1476 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1477 | // This function does all (java) allocations we need to do for the Class being redefined. |
| 1478 | // TODO Change this name maybe? |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1479 | bool Redefiner::ClassRedefinition::EnsureClassAllocationsFinished( |
| 1480 | /*out*/RedefinitionDataIter* cur_data) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1481 | art::StackHandleScope<2> hs(driver_->self_); |
| 1482 | art::Handle<art::mirror::Class> klass(hs.NewHandle( |
| 1483 | driver_->self_->DecodeJObject(klass_)->AsClass())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1484 | if (klass == nullptr) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1485 | RecordFailure(ERR(INVALID_CLASS), "Unable to decode class argument!"); |
| 1486 | return false; |
| 1487 | } |
| 1488 | // Allocate the classExt |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1489 | art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(driver_->self_))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1490 | if (ext == nullptr) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1491 | // No memory. Clear exception (it's not useful) and return error. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1492 | driver_->self_->AssertPendingOOMException(); |
| 1493 | driver_->self_->ClearException(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1494 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate ClassExt"); |
| 1495 | return false; |
| 1496 | } |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1497 | // First save the old values of the 2 arrays that make up the obsolete methods maps. Then |
| 1498 | // allocate the 2 arrays that make up the obsolete methods map. Since the contents of the arrays |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1499 | // are only modified when all threads (other than the modifying one) are suspended we don't need |
| 1500 | // to worry about missing the unsyncronized writes to the array. We do synchronize when setting it |
| 1501 | // however, since that can happen at any time. |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1502 | cur_data->SetOldObsoleteMethods(ext->GetObsoleteMethods()); |
| 1503 | cur_data->SetOldDexCaches(ext->GetObsoleteDexCaches()); |
| 1504 | if (!ext->ExtendObsoleteArrays( |
| 1505 | driver_->self_, klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size())) { |
| 1506 | // OOM. Clear exception and return error. |
| 1507 | driver_->self_->AssertPendingOOMException(); |
| 1508 | driver_->self_->ClearException(); |
| 1509 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate/extend obsolete methods map"); |
| 1510 | return false; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1511 | } |
| 1512 | return true; |
| 1513 | } |
| 1514 | |
| 1515 | } // namespace openjdkjvmti |