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