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