Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1 | /* Copyright (C) 2016 The Android Open Source Project |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This file implements interfaces from the file jvmti.h. This implementation |
| 5 | * is licensed under the same terms as the file jvmti.h. The |
| 6 | * copyright and license information for the file jvmti.h follows. |
| 7 | * |
| 8 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
| 9 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 10 | * |
| 11 | * This code is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 only, as |
| 13 | * published by the Free Software Foundation. Oracle designates this |
| 14 | * particular file as subject to the "Classpath" exception as provided |
| 15 | * by Oracle in the LICENSE file that accompanied this code. |
| 16 | * |
| 17 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * version 2 for more details (a copy is included in the LICENSE file that |
| 21 | * accompanied this code). |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License version |
| 24 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 25 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | * |
| 27 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 28 | * or visit www.oracle.com if you need additional information or have any |
| 29 | * questions. |
| 30 | */ |
| 31 | |
| 32 | #include "ti_redefine.h" |
| 33 | |
| 34 | #include <limits> |
| 35 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 36 | #include "android-base/stringprintf.h" |
| 37 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 38 | #include "art_jvmti.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 39 | #include "base/array_slice.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 40 | #include "base/logging.h" |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 41 | #include "dex_file.h" |
| 42 | #include "dex_file_types.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 43 | #include "events-inl.h" |
| 44 | #include "gc/allocation_listener.h" |
Alex Light | 6abd539 | 2017-01-05 17:53:00 -0800 | [diff] [blame] | 45 | #include "gc/heap.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 46 | #include "instrumentation.h" |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 47 | #include "jit/jit.h" |
| 48 | #include "jit/jit_code_cache.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 49 | #include "jni_env_ext-inl.h" |
| 50 | #include "jvmti_allocator.h" |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame^] | 51 | #include "mirror/class-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 52 | #include "mirror/class_ext.h" |
| 53 | #include "mirror/object.h" |
| 54 | #include "object_lock.h" |
| 55 | #include "runtime.h" |
| 56 | #include "ScopedLocalRef.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 57 | #include "ti_class_loader.h" |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 58 | #include "transform.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 59 | |
| 60 | namespace openjdkjvmti { |
| 61 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 62 | using android::base::StringPrintf; |
| 63 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 64 | // This visitor walks thread stacks and allocates and sets up the obsolete methods. It also does |
| 65 | // some basic sanity checks that the obsolete method is sane. |
| 66 | class ObsoleteMethodStackVisitor : public art::StackVisitor { |
| 67 | protected: |
| 68 | ObsoleteMethodStackVisitor( |
| 69 | art::Thread* thread, |
| 70 | art::LinearAlloc* allocator, |
| 71 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods, |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 72 | /*out*/std::unordered_map<art::ArtMethod*, art::ArtMethod*>* obsolete_maps) |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 73 | : StackVisitor(thread, |
| 74 | /*context*/nullptr, |
| 75 | StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 76 | allocator_(allocator), |
| 77 | obsoleted_methods_(obsoleted_methods), |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 78 | obsolete_maps_(obsolete_maps) { } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 79 | |
| 80 | ~ObsoleteMethodStackVisitor() OVERRIDE {} |
| 81 | |
| 82 | public: |
| 83 | // Returns true if we successfully installed obsolete methods on this thread, filling |
| 84 | // obsolete_maps_ with the translations if needed. Returns false and fills error_msg if we fail. |
| 85 | // The stack is cleaned up when we fail. |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 86 | static void UpdateObsoleteFrames( |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 87 | art::Thread* thread, |
| 88 | art::LinearAlloc* allocator, |
| 89 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods, |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 90 | /*out*/std::unordered_map<art::ArtMethod*, art::ArtMethod*>* obsolete_maps) |
| 91 | REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 92 | ObsoleteMethodStackVisitor visitor(thread, |
| 93 | allocator, |
| 94 | obsoleted_methods, |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 95 | obsolete_maps); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 96 | visitor.WalkStack(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool VisitFrame() OVERRIDE REQUIRES(art::Locks::mutator_lock_) { |
| 100 | art::ArtMethod* old_method = GetMethod(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 101 | if (obsoleted_methods_.find(old_method) != obsoleted_methods_.end()) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 102 | // We cannot ensure that the right dex file is used in inlined frames so we don't support |
| 103 | // redefining them. |
| 104 | DCHECK(!IsInInlinedFrame()) << "Inlined frames are not supported when using redefinition"; |
| 105 | // TODO We should really support intrinsic obsolete methods. |
| 106 | // TODO We should really support redefining intrinsics. |
| 107 | // We don't support intrinsics so check for them here. |
| 108 | DCHECK(!old_method->IsIntrinsic()); |
| 109 | art::ArtMethod* new_obsolete_method = nullptr; |
| 110 | auto obsolete_method_pair = obsolete_maps_->find(old_method); |
| 111 | if (obsolete_method_pair == obsolete_maps_->end()) { |
| 112 | // Create a new Obsolete Method and put it in the list. |
| 113 | art::Runtime* runtime = art::Runtime::Current(); |
| 114 | art::ClassLinker* cl = runtime->GetClassLinker(); |
| 115 | auto ptr_size = cl->GetImagePointerSize(); |
| 116 | const size_t method_size = art::ArtMethod::Size(ptr_size); |
| 117 | auto* method_storage = allocator_->Alloc(GetThread(), method_size); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 118 | CHECK(method_storage != nullptr) << "Unable to allocate storage for obsolete version of '" |
| 119 | << old_method->PrettyMethod() << "'"; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 120 | new_obsolete_method = new (method_storage) art::ArtMethod(); |
| 121 | new_obsolete_method->CopyFrom(old_method, ptr_size); |
| 122 | DCHECK_EQ(new_obsolete_method->GetDeclaringClass(), old_method->GetDeclaringClass()); |
| 123 | new_obsolete_method->SetIsObsolete(); |
| 124 | obsolete_maps_->insert({old_method, new_obsolete_method}); |
| 125 | // Update JIT Data structures to point to the new method. |
| 126 | art::jit::Jit* jit = art::Runtime::Current()->GetJit(); |
| 127 | if (jit != nullptr) { |
| 128 | // Notify the JIT we are making this obsolete method. It will update the jit's internal |
| 129 | // structures to keep track of the new obsolete method. |
| 130 | jit->GetCodeCache()->MoveObsoleteMethod(old_method, new_obsolete_method); |
| 131 | } |
| 132 | } else { |
| 133 | new_obsolete_method = obsolete_method_pair->second; |
| 134 | } |
| 135 | DCHECK(new_obsolete_method != nullptr); |
| 136 | SetMethod(new_obsolete_method); |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | // The linear allocator we should use to make new methods. |
| 143 | art::LinearAlloc* allocator_; |
| 144 | // The set of all methods which could be obsoleted. |
| 145 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods_; |
| 146 | // A map from the original to the newly allocated obsolete method for frames on this thread. The |
| 147 | // values in this map must be added to the obsolete_methods_ (and obsolete_dex_caches_) fields of |
| 148 | // the redefined classes ClassExt by the caller. |
| 149 | std::unordered_map<art::ArtMethod*, art::ArtMethod*>* obsolete_maps_; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 152 | jvmtiError Redefiner::IsModifiableClass(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 153 | jclass klass, |
| 154 | jboolean* is_redefinable) { |
| 155 | // TODO Check for the appropriate feature flags once we have enabled them. |
| 156 | art::Thread* self = art::Thread::Current(); |
| 157 | art::ScopedObjectAccess soa(self); |
| 158 | art::StackHandleScope<1> hs(self); |
| 159 | art::ObjPtr<art::mirror::Object> obj(self->DecodeJObject(klass)); |
| 160 | if (obj.IsNull()) { |
| 161 | return ERR(INVALID_CLASS); |
| 162 | } |
| 163 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(obj->AsClass())); |
| 164 | std::string err_unused; |
| 165 | *is_redefinable = |
| 166 | Redefiner::GetClassRedefinitionError(h_klass, &err_unused) == OK ? JNI_TRUE : JNI_FALSE; |
| 167 | return OK; |
| 168 | } |
| 169 | |
| 170 | jvmtiError Redefiner::GetClassRedefinitionError(art::Handle<art::mirror::Class> klass, |
| 171 | /*out*/std::string* error_msg) { |
| 172 | if (klass->IsPrimitive()) { |
| 173 | *error_msg = "Modification of primitive classes is not supported"; |
| 174 | return ERR(UNMODIFIABLE_CLASS); |
| 175 | } else if (klass->IsInterface()) { |
| 176 | *error_msg = "Modification of Interface classes is currently not supported"; |
| 177 | return ERR(UNMODIFIABLE_CLASS); |
| 178 | } else if (klass->IsArrayClass()) { |
| 179 | *error_msg = "Modification of Array classes is not supported"; |
| 180 | return ERR(UNMODIFIABLE_CLASS); |
| 181 | } else if (klass->IsProxyClass()) { |
| 182 | *error_msg = "Modification of proxy classes is not supported"; |
| 183 | return ERR(UNMODIFIABLE_CLASS); |
| 184 | } |
| 185 | |
| 186 | // TODO We should check if the class has non-obsoletable methods on the stack |
| 187 | LOG(WARNING) << "presence of non-obsoletable methods on stacks is not currently checked"; |
| 188 | return OK; |
| 189 | } |
| 190 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 191 | // Moves dex data to an anonymous, read-only mmap'd region. |
| 192 | std::unique_ptr<art::MemMap> Redefiner::MoveDataToMemMap(const std::string& original_location, |
| 193 | jint data_len, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 194 | const unsigned char* dex_data, |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 195 | std::string* error_msg) { |
| 196 | std::unique_ptr<art::MemMap> map(art::MemMap::MapAnonymous( |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 197 | StringPrintf("%s-transformed", original_location.c_str()).c_str(), |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 198 | nullptr, |
| 199 | data_len, |
| 200 | PROT_READ|PROT_WRITE, |
| 201 | /*low_4gb*/false, |
| 202 | /*reuse*/false, |
| 203 | error_msg)); |
| 204 | if (map == nullptr) { |
| 205 | return map; |
| 206 | } |
| 207 | memcpy(map->Begin(), dex_data, data_len); |
Alex Light | 0b77257 | 2016-12-02 17:27:31 -0800 | [diff] [blame] | 208 | // Make the dex files mmap read only. This matches how other DexFiles are mmaped and prevents |
| 209 | // programs from corrupting it. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 210 | map->Protect(PROT_READ); |
| 211 | return map; |
| 212 | } |
| 213 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 214 | Redefiner::ClassRedefinition::ClassRedefinition( |
| 215 | Redefiner* driver, |
| 216 | jclass klass, |
| 217 | const art::DexFile* redefined_dex_file, |
| 218 | const char* class_sig, |
| 219 | art::ArraySlice<const unsigned char> orig_dex_file) : |
| 220 | driver_(driver), |
| 221 | klass_(klass), |
| 222 | dex_file_(redefined_dex_file), |
| 223 | class_sig_(class_sig), |
| 224 | original_dex_file_(orig_dex_file) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 225 | GetMirrorClass()->MonitorEnter(driver_->self_); |
| 226 | } |
| 227 | |
| 228 | Redefiner::ClassRedefinition::~ClassRedefinition() { |
| 229 | if (driver_ != nullptr) { |
| 230 | GetMirrorClass()->MonitorExit(driver_->self_); |
| 231 | } |
| 232 | } |
| 233 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 234 | jvmtiError Redefiner::RedefineClasses(ArtJvmTiEnv* env, |
| 235 | art::Runtime* runtime, |
| 236 | art::Thread* self, |
| 237 | jint class_count, |
| 238 | const jvmtiClassDefinition* definitions, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 239 | /*out*/std::string* error_msg) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 240 | if (env == nullptr) { |
| 241 | *error_msg = "env was null!"; |
| 242 | return ERR(INVALID_ENVIRONMENT); |
| 243 | } else if (class_count < 0) { |
| 244 | *error_msg = "class_count was less then 0"; |
| 245 | return ERR(ILLEGAL_ARGUMENT); |
| 246 | } else if (class_count == 0) { |
| 247 | // We don't actually need to do anything. Just return OK. |
| 248 | return OK; |
| 249 | } else if (definitions == nullptr) { |
| 250 | *error_msg = "null definitions!"; |
| 251 | return ERR(NULL_POINTER); |
| 252 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 253 | std::vector<ArtClassDefinition> def_vector; |
| 254 | def_vector.reserve(class_count); |
| 255 | for (jint i = 0; i < class_count; i++) { |
| 256 | // We make a copy of the class_bytes to pass into the retransformation. |
| 257 | // This makes cleanup easier (since we unambiguously own the bytes) and also is useful since we |
| 258 | // will need to keep the original bytes around unaltered for subsequent RetransformClasses calls |
| 259 | // to get the passed in bytes. |
| 260 | // TODO Implement saving the original bytes. |
| 261 | unsigned char* class_bytes_copy = nullptr; |
| 262 | jvmtiError res = env->Allocate(definitions[i].class_byte_count, &class_bytes_copy); |
| 263 | if (res != OK) { |
| 264 | return res; |
| 265 | } |
| 266 | memcpy(class_bytes_copy, definitions[i].class_bytes, definitions[i].class_byte_count); |
| 267 | |
| 268 | ArtClassDefinition def; |
| 269 | def.dex_len = definitions[i].class_byte_count; |
| 270 | def.dex_data = MakeJvmtiUniquePtr(env, class_bytes_copy); |
| 271 | // We are definitely modified. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 272 | def.SetModified(); |
| 273 | def.original_dex_file = art::ArraySlice<const unsigned char>(definitions[i].class_bytes, |
| 274 | definitions[i].class_byte_count); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 275 | res = Transformer::FillInTransformationData(env, definitions[i].klass, &def); |
| 276 | if (res != OK) { |
| 277 | return res; |
| 278 | } |
| 279 | def_vector.push_back(std::move(def)); |
| 280 | } |
| 281 | // Call all the transformation events. |
| 282 | jvmtiError res = Transformer::RetransformClassesDirect(env, |
| 283 | self, |
| 284 | &def_vector); |
| 285 | if (res != OK) { |
| 286 | // Something went wrong with transformation! |
| 287 | return res; |
| 288 | } |
| 289 | return RedefineClassesDirect(env, runtime, self, def_vector, error_msg); |
| 290 | } |
| 291 | |
| 292 | jvmtiError Redefiner::RedefineClassesDirect(ArtJvmTiEnv* env, |
| 293 | art::Runtime* runtime, |
| 294 | art::Thread* self, |
| 295 | const std::vector<ArtClassDefinition>& definitions, |
| 296 | std::string* error_msg) { |
| 297 | DCHECK(env != nullptr); |
| 298 | if (definitions.size() == 0) { |
| 299 | // We don't actually need to do anything. Just return OK. |
| 300 | return OK; |
| 301 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 302 | // Stop JIT for the duration of this redefine since the JIT might concurrently compile a method we |
| 303 | // are going to redefine. |
| 304 | art::jit::ScopedJitSuspend suspend_jit; |
| 305 | // Get shared mutator lock so we can lock all the classes. |
| 306 | art::ScopedObjectAccess soa(self); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 307 | Redefiner r(runtime, self, error_msg); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 308 | for (const ArtClassDefinition& def : definitions) { |
| 309 | // Only try to transform classes that have been modified. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 310 | if (def.IsModified(self)) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 311 | jvmtiError res = r.AddRedefinition(env, def); |
| 312 | if (res != OK) { |
| 313 | return res; |
| 314 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | return r.Run(); |
| 318 | } |
| 319 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 320 | jvmtiError Redefiner::AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 321 | std::string original_dex_location; |
| 322 | jvmtiError ret = OK; |
| 323 | if ((ret = GetClassLocation(env, def.klass, &original_dex_location))) { |
| 324 | *error_msg_ = "Unable to get original dex file location!"; |
| 325 | return ret; |
| 326 | } |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 327 | char* generic_ptr_unused = nullptr; |
| 328 | char* signature_ptr = nullptr; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 329 | if ((ret = env->GetClassSignature(def.klass, &signature_ptr, &generic_ptr_unused)) != OK) { |
| 330 | *error_msg_ = "Unable to get class signature!"; |
| 331 | return ret; |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 332 | } |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 333 | JvmtiUniquePtr generic_unique_ptr(MakeJvmtiUniquePtr(env, generic_ptr_unused)); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 334 | JvmtiUniquePtr signature_unique_ptr(MakeJvmtiUniquePtr(env, signature_ptr)); |
| 335 | std::unique_ptr<art::MemMap> map(MoveDataToMemMap(original_dex_location, |
| 336 | def.dex_len, |
| 337 | def.dex_data.get(), |
| 338 | error_msg_)); |
| 339 | std::ostringstream os; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 340 | if (map.get() == nullptr) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 341 | os << "Failed to create anonymous mmap for modified dex file of class " << def.name |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 342 | << "in dex file " << original_dex_location << " because: " << *error_msg_; |
| 343 | *error_msg_ = os.str(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 344 | return ERR(OUT_OF_MEMORY); |
| 345 | } |
| 346 | if (map->Size() < sizeof(art::DexFile::Header)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 347 | *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] | 348 | return ERR(INVALID_CLASS_FORMAT); |
| 349 | } |
| 350 | uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map->Begin())->checksum_; |
| 351 | std::unique_ptr<const art::DexFile> dex_file(art::DexFile::Open(map->GetName(), |
| 352 | checksum, |
| 353 | std::move(map), |
| 354 | /*verify*/true, |
| 355 | /*verify_checksum*/true, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 356 | error_msg_)); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 357 | if (dex_file.get() == nullptr) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 358 | os << "Unable to load modified dex file for " << def.name << ": " << *error_msg_; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 359 | *error_msg_ = os.str(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 360 | return ERR(INVALID_CLASS_FORMAT); |
| 361 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 362 | redefinitions_.push_back( |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 363 | Redefiner::ClassRedefinition(this, |
| 364 | def.klass, |
| 365 | dex_file.release(), |
| 366 | signature_ptr, |
| 367 | def.original_dex_file)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 368 | return OK; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 371 | art::mirror::Class* Redefiner::ClassRedefinition::GetMirrorClass() { |
| 372 | return driver_->self_->DecodeJObject(klass_)->AsClass(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 375 | art::mirror::ClassLoader* Redefiner::ClassRedefinition::GetClassLoader() { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 376 | return GetMirrorClass()->GetClassLoader(); |
| 377 | } |
| 378 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 379 | art::mirror::DexCache* Redefiner::ClassRedefinition::CreateNewDexCache( |
| 380 | art::Handle<art::mirror::ClassLoader> loader) { |
| 381 | return driver_->runtime_->GetClassLinker()->RegisterDexFile(*dex_file_, loader.Get()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 384 | void Redefiner::RecordFailure(jvmtiError result, |
| 385 | const std::string& class_sig, |
| 386 | const std::string& error_msg) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 387 | *error_msg_ = StringPrintf("Unable to perform redefinition of '%s': %s", |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 388 | class_sig.c_str(), |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 389 | error_msg.c_str()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 390 | result_ = result; |
| 391 | } |
| 392 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 393 | art::mirror::ByteArray* Redefiner::ClassRedefinition::AllocateOrGetOriginalDexFileBytes() { |
| 394 | // If we have been specifically given a new set of bytes use that |
| 395 | if (original_dex_file_.size() != 0) { |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 396 | return art::mirror::ByteArray::AllocateAndFill( |
| 397 | driver_->self_, |
| 398 | reinterpret_cast<const signed char*>(&original_dex_file_.At(0)), |
| 399 | original_dex_file_.size()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 400 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 401 | |
| 402 | // See if we already have one set. |
| 403 | art::ObjPtr<art::mirror::ClassExt> ext(GetMirrorClass()->GetExtData()); |
| 404 | if (!ext.IsNull()) { |
| 405 | art::ObjPtr<art::mirror::ByteArray> old_original_bytes(ext->GetOriginalDexFileBytes()); |
| 406 | if (!old_original_bytes.IsNull()) { |
| 407 | // We do. Use it. |
| 408 | return old_original_bytes.Ptr(); |
| 409 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 410 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 411 | |
| 412 | // Copy the current dex_file |
| 413 | const art::DexFile& current_dex_file = GetMirrorClass()->GetDexFile(); |
| 414 | // TODO Handle this or make it so it cannot happen. |
| 415 | if (current_dex_file.NumClassDefs() != 1) { |
| 416 | LOG(WARNING) << "Current dex file has more than one class in it. Calling RetransformClasses " |
| 417 | << "on this class might fail if no transformations are applied to it!"; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 418 | } |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 419 | return art::mirror::ByteArray::AllocateAndFill( |
| 420 | driver_->self_, |
| 421 | reinterpret_cast<const signed char*>(current_dex_file.Begin()), |
| 422 | current_dex_file.Size()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 425 | struct CallbackCtx { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 426 | art::LinearAlloc* allocator; |
| 427 | std::unordered_map<art::ArtMethod*, art::ArtMethod*> obsolete_map; |
| 428 | std::unordered_set<art::ArtMethod*> obsolete_methods; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 429 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 430 | explicit CallbackCtx(art::LinearAlloc* alloc) : allocator(alloc) {} |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 431 | }; |
| 432 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 433 | void DoAllocateObsoleteMethodsCallback(art::Thread* t, void* vdata) NO_THREAD_SAFETY_ANALYSIS { |
| 434 | CallbackCtx* data = reinterpret_cast<CallbackCtx*>(vdata); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 435 | ObsoleteMethodStackVisitor::UpdateObsoleteFrames(t, |
| 436 | data->allocator, |
| 437 | data->obsolete_methods, |
| 438 | &data->obsolete_map); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // This creates any ArtMethod* structures needed for obsolete methods and ensures that the stack is |
| 442 | // updated so they will be run. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 443 | // TODO Rewrite so we can do this only once regardless of how many redefinitions there are. |
| 444 | void Redefiner::ClassRedefinition::FindAndAllocateObsoleteMethods(art::mirror::Class* art_klass) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 445 | art::ScopedAssertNoThreadSuspension ns("No thread suspension during thread stack walking"); |
| 446 | art::mirror::ClassExt* ext = art_klass->GetExtData(); |
| 447 | CHECK(ext->GetObsoleteMethods() != nullptr); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 448 | art::ClassLinker* linker = driver_->runtime_->GetClassLinker(); |
| 449 | CallbackCtx ctx(linker->GetAllocatorForClassLoader(art_klass->GetClassLoader())); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 450 | // Add all the declared methods to the map |
| 451 | for (auto& m : art_klass->GetDeclaredMethods(art::kRuntimePointerSize)) { |
| 452 | ctx.obsolete_methods.insert(&m); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 453 | // TODO Allow this or check in IsModifiableClass. |
| 454 | DCHECK(!m.IsIntrinsic()); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 455 | } |
| 456 | { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 457 | art::MutexLock mu(driver_->self_, *art::Locks::thread_list_lock_); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 458 | art::ThreadList* list = art::Runtime::Current()->GetThreadList(); |
| 459 | list->ForEach(DoAllocateObsoleteMethodsCallback, static_cast<void*>(&ctx)); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 460 | } |
| 461 | FillObsoleteMethodMap(art_klass, ctx.obsolete_map); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | // Fills the obsolete method map in the art_klass's extData. This is so obsolete methods are able to |
| 465 | // figure out their DexCaches. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 466 | void Redefiner::ClassRedefinition::FillObsoleteMethodMap( |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 467 | art::mirror::Class* art_klass, |
| 468 | const std::unordered_map<art::ArtMethod*, art::ArtMethod*>& obsoletes) { |
| 469 | int32_t index = 0; |
| 470 | art::mirror::ClassExt* ext_data = art_klass->GetExtData(); |
| 471 | art::mirror::PointerArray* obsolete_methods = ext_data->GetObsoleteMethods(); |
| 472 | art::mirror::ObjectArray<art::mirror::DexCache>* obsolete_dex_caches = |
| 473 | ext_data->GetObsoleteDexCaches(); |
| 474 | int32_t num_method_slots = obsolete_methods->GetLength(); |
| 475 | // Find the first empty index. |
| 476 | for (; index < num_method_slots; index++) { |
| 477 | if (obsolete_methods->GetElementPtrSize<art::ArtMethod*>( |
| 478 | index, art::kRuntimePointerSize) == nullptr) { |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | // Make sure we have enough space. |
| 483 | CHECK_GT(num_method_slots, static_cast<int32_t>(obsoletes.size() + index)); |
| 484 | CHECK(obsolete_dex_caches->Get(index) == nullptr); |
| 485 | // Fill in the map. |
| 486 | for (auto& obs : obsoletes) { |
| 487 | obsolete_methods->SetElementPtrSize(index, obs.second, art::kRuntimePointerSize); |
| 488 | obsolete_dex_caches->Set(index, art_klass->GetDexCache()); |
| 489 | index++; |
| 490 | } |
| 491 | } |
| 492 | |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame^] | 493 | // Try and get the declared method. First try to get a virtual method then a direct method if that's |
| 494 | // not found. |
| 495 | static art::ArtMethod* FindMethod(art::Handle<art::mirror::Class> klass, |
| 496 | const char* name, |
| 497 | art::Signature sig) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 498 | art::ArtMethod* m = klass->FindDeclaredVirtualMethod(name, sig, art::kRuntimePointerSize); |
| 499 | if (m == nullptr) { |
| 500 | m = klass->FindDeclaredDirectMethod(name, sig, art::kRuntimePointerSize); |
| 501 | } |
| 502 | return m; |
| 503 | } |
| 504 | |
| 505 | bool Redefiner::ClassRedefinition::CheckSameMethods() { |
| 506 | art::StackHandleScope<1> hs(driver_->self_); |
| 507 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
| 508 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
| 509 | |
| 510 | art::ClassDataItemIterator new_iter(*dex_file_, |
| 511 | dex_file_->GetClassData(dex_file_->GetClassDef(0))); |
| 512 | |
| 513 | // Make sure we have the same number of methods. |
| 514 | uint32_t num_new_method = new_iter.NumVirtualMethods() + new_iter.NumDirectMethods(); |
| 515 | uint32_t num_old_method = h_klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size(); |
| 516 | if (num_new_method != num_old_method) { |
| 517 | bool bigger = num_new_method > num_old_method; |
| 518 | RecordFailure(bigger ? ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED) |
| 519 | : ERR(UNSUPPORTED_REDEFINITION_METHOD_DELETED), |
| 520 | StringPrintf("Total number of declared methods changed from %d to %d", |
| 521 | num_old_method, num_new_method)); |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | // Skip all of the fields. We should have already checked this. |
| 526 | while (new_iter.HasNextStaticField() || new_iter.HasNextInstanceField()) { |
| 527 | new_iter.Next(); |
| 528 | } |
| 529 | // Check each of the methods. NB we don't need to specifically check for removals since the 2 dex |
| 530 | // files have the same number of methods, which means there must be an equal amount of additions |
| 531 | // and removals. |
| 532 | for (; new_iter.HasNextVirtualMethod() || new_iter.HasNextDirectMethod(); new_iter.Next()) { |
| 533 | // Get the data on the method we are searching for |
| 534 | const art::DexFile::MethodId& new_method_id = dex_file_->GetMethodId(new_iter.GetMemberIndex()); |
| 535 | const char* new_method_name = dex_file_->GetMethodName(new_method_id); |
| 536 | art::Signature new_method_signature = dex_file_->GetMethodSignature(new_method_id); |
| 537 | art::ArtMethod* old_method = FindMethod(h_klass, new_method_name, new_method_signature); |
| 538 | // If we got past the check for the same number of methods above that means there must be at |
| 539 | // least one added and one removed method. We will return the ADDED failure message since it is |
| 540 | // easier to get a useful error report for it. |
| 541 | if (old_method == nullptr) { |
| 542 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED), |
| 543 | StringPrintf("Unknown method '%s' (sig: %s) was added!", |
| 544 | new_method_name, |
| 545 | new_method_signature.ToString().c_str())); |
| 546 | return false; |
| 547 | } |
| 548 | // Since direct methods have different flags than virtual ones (specifically direct methods must |
| 549 | // have kAccPrivate or kAccStatic or kAccConstructor flags) we can tell if a method changes from |
| 550 | // virtual to direct. |
| 551 | uint32_t new_flags = new_iter.GetMethodAccessFlags(); |
| 552 | if (new_flags != (old_method->GetAccessFlags() & art::kAccValidMethodFlags)) { |
| 553 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED), |
| 554 | StringPrintf("method '%s' (sig: %s) had different access flags", |
| 555 | new_method_name, |
| 556 | new_method_signature.ToString().c_str())); |
| 557 | return false; |
| 558 | } |
| 559 | } |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | bool Redefiner::ClassRedefinition::CheckSameFields() { |
| 564 | art::StackHandleScope<1> hs(driver_->self_); |
| 565 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
| 566 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
| 567 | art::ClassDataItemIterator new_iter(*dex_file_, |
| 568 | dex_file_->GetClassData(dex_file_->GetClassDef(0))); |
| 569 | const art::DexFile& old_dex_file = h_klass->GetDexFile(); |
| 570 | art::ClassDataItemIterator old_iter(old_dex_file, |
| 571 | old_dex_file.GetClassData(*h_klass->GetClassDef())); |
| 572 | // Instance and static fields can be differentiated by their flags so no need to check them |
| 573 | // separately. |
| 574 | while (new_iter.HasNextInstanceField() || new_iter.HasNextStaticField()) { |
| 575 | // Get the data on the method we are searching for |
| 576 | const art::DexFile::FieldId& new_field_id = dex_file_->GetFieldId(new_iter.GetMemberIndex()); |
| 577 | const char* new_field_name = dex_file_->GetFieldName(new_field_id); |
| 578 | const char* new_field_type = dex_file_->GetFieldTypeDescriptor(new_field_id); |
| 579 | |
| 580 | if (!(old_iter.HasNextInstanceField() || old_iter.HasNextStaticField())) { |
| 581 | // We are missing the old version of this method! |
| 582 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 583 | StringPrintf("Unknown field '%s' (type: %s) added!", |
| 584 | new_field_name, |
| 585 | new_field_type)); |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | const art::DexFile::FieldId& old_field_id = old_dex_file.GetFieldId(old_iter.GetMemberIndex()); |
| 590 | const char* old_field_name = old_dex_file.GetFieldName(old_field_id); |
| 591 | const char* old_field_type = old_dex_file.GetFieldTypeDescriptor(old_field_id); |
| 592 | |
| 593 | // Check name and type. |
| 594 | if (strcmp(old_field_name, new_field_name) != 0 || |
| 595 | strcmp(old_field_type, new_field_type) != 0) { |
| 596 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 597 | StringPrintf("Field changed from '%s' (sig: %s) to '%s' (sig: %s)!", |
| 598 | old_field_name, |
| 599 | old_field_type, |
| 600 | new_field_name, |
| 601 | new_field_type)); |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | // Since static fields have different flags than instance ones (specifically static fields must |
| 606 | // have the kAccStatic flag) we can tell if a field changes from static to instance. |
| 607 | if (new_iter.GetFieldAccessFlags() != old_iter.GetFieldAccessFlags()) { |
| 608 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 609 | StringPrintf("Field '%s' (sig: %s) had different access flags", |
| 610 | new_field_name, |
| 611 | new_field_type)); |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | new_iter.Next(); |
| 616 | old_iter.Next(); |
| 617 | } |
| 618 | if (old_iter.HasNextInstanceField() || old_iter.HasNextStaticField()) { |
| 619 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 620 | StringPrintf("field '%s' (sig: %s) is missing!", |
| 621 | old_dex_file.GetFieldName(old_dex_file.GetFieldId( |
| 622 | old_iter.GetMemberIndex())), |
| 623 | old_dex_file.GetFieldTypeDescriptor(old_dex_file.GetFieldId( |
| 624 | old_iter.GetMemberIndex())))); |
| 625 | return false; |
| 626 | } |
| 627 | return true; |
| 628 | } |
| 629 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 630 | bool Redefiner::ClassRedefinition::CheckClass() { |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 631 | // TODO Might just want to put it in a ObjPtr and NoSuspend assert. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 632 | art::StackHandleScope<1> hs(driver_->self_); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 633 | // Easy check that only 1 class def is present. |
| 634 | if (dex_file_->NumClassDefs() != 1) { |
| 635 | RecordFailure(ERR(ILLEGAL_ARGUMENT), |
| 636 | StringPrintf("Expected 1 class def in dex file but found %d", |
| 637 | dex_file_->NumClassDefs())); |
| 638 | return false; |
| 639 | } |
| 640 | // Get the ClassDef from the new DexFile. |
| 641 | // Since the dex file has only a single class def the index is always 0. |
| 642 | const art::DexFile::ClassDef& def = dex_file_->GetClassDef(0); |
| 643 | // Get the class as it is now. |
| 644 | art::Handle<art::mirror::Class> current_class(hs.NewHandle(GetMirrorClass())); |
| 645 | |
| 646 | // Check the access flags didn't change. |
| 647 | if (def.GetJavaAccessFlags() != (current_class->GetAccessFlags() & art::kAccValidClassFlags)) { |
| 648 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED), |
| 649 | "Cannot change modifiers of class by redefinition"); |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | // Check class name. |
| 654 | // These should have been checked by the dexfile verifier on load. |
| 655 | DCHECK_NE(def.class_idx_, art::dex::TypeIndex::Invalid()) << "Invalid type index"; |
| 656 | const char* descriptor = dex_file_->StringByTypeIdx(def.class_idx_); |
| 657 | DCHECK(descriptor != nullptr) << "Invalid dex file structure!"; |
| 658 | if (!current_class->DescriptorEquals(descriptor)) { |
| 659 | std::string storage; |
| 660 | RecordFailure(ERR(NAMES_DONT_MATCH), |
| 661 | StringPrintf("expected file to contain class called '%s' but found '%s'!", |
| 662 | current_class->GetDescriptor(&storage), |
| 663 | descriptor)); |
| 664 | return false; |
| 665 | } |
| 666 | if (current_class->IsObjectClass()) { |
| 667 | if (def.superclass_idx_ != art::dex::TypeIndex::Invalid()) { |
| 668 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass added!"); |
| 669 | return false; |
| 670 | } |
| 671 | } else { |
| 672 | const char* super_descriptor = dex_file_->StringByTypeIdx(def.superclass_idx_); |
| 673 | DCHECK(descriptor != nullptr) << "Invalid dex file structure!"; |
| 674 | if (!current_class->GetSuperClass()->DescriptorEquals(super_descriptor)) { |
| 675 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass changed"); |
| 676 | return false; |
| 677 | } |
| 678 | } |
| 679 | const art::DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(def); |
| 680 | if (interfaces == nullptr) { |
| 681 | if (current_class->NumDirectInterfaces() != 0) { |
| 682 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added"); |
| 683 | return false; |
| 684 | } |
| 685 | } else { |
| 686 | DCHECK(!current_class->IsProxyClass()); |
| 687 | const art::DexFile::TypeList* current_interfaces = current_class->GetInterfaceTypeList(); |
| 688 | if (current_interfaces == nullptr || current_interfaces->Size() != interfaces->Size()) { |
| 689 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added or removed"); |
| 690 | return false; |
| 691 | } |
| 692 | // The order of interfaces is (barely) meaningful so we error if it changes. |
| 693 | const art::DexFile& orig_dex_file = current_class->GetDexFile(); |
| 694 | for (uint32_t i = 0; i < interfaces->Size(); i++) { |
| 695 | if (strcmp( |
| 696 | dex_file_->StringByTypeIdx(interfaces->GetTypeItem(i).type_idx_), |
| 697 | orig_dex_file.StringByTypeIdx(current_interfaces->GetTypeItem(i).type_idx_)) != 0) { |
| 698 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), |
| 699 | "Interfaces changed or re-ordered"); |
| 700 | return false; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | LOG(WARNING) << "No verification is done on annotations of redefined classes."; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 705 | LOG(WARNING) << "Bytecodes of redefinitions are not verified."; |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 706 | |
| 707 | return true; |
| 708 | } |
| 709 | |
| 710 | // TODO Move this to use IsRedefinable when that function is made. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 711 | bool Redefiner::ClassRedefinition::CheckRedefinable() { |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 712 | std::string err; |
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 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 715 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
| 716 | jvmtiError res = Redefiner::GetClassRedefinitionError(h_klass, &err); |
| 717 | if (res != OK) { |
| 718 | RecordFailure(res, err); |
| 719 | return false; |
| 720 | } else { |
| 721 | return true; |
| 722 | } |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 725 | bool Redefiner::ClassRedefinition::CheckRedefinitionIsValid() { |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 726 | return CheckRedefinable() && |
| 727 | CheckClass() && |
| 728 | CheckSameFields() && |
| 729 | CheckSameMethods(); |
| 730 | } |
| 731 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 732 | // A wrapper that lets us hold onto the arbitrary sized data needed for redefinitions in a |
| 733 | // reasonably sane way. This adds no fields to the normal ObjectArray. By doing this we can avoid |
| 734 | // having to deal with the fact that we need to hold an arbitrary number of references live. |
| 735 | class RedefinitionDataHolder { |
| 736 | public: |
| 737 | enum DataSlot : int32_t { |
| 738 | kSlotSourceClassLoader = 0, |
| 739 | kSlotJavaDexFile = 1, |
| 740 | kSlotNewDexFileCookie = 2, |
| 741 | kSlotNewDexCache = 3, |
| 742 | kSlotMirrorClass = 4, |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 743 | kSlotOrigDexFile = 5, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 744 | |
| 745 | // Must be last one. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 746 | kNumSlots = 6, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 747 | }; |
| 748 | |
| 749 | // This needs to have a HandleScope passed in that is capable of creating a new Handle without |
| 750 | // overflowing. Only one handle will be created. This object has a lifetime identical to that of |
| 751 | // the passed in handle-scope. |
| 752 | RedefinitionDataHolder(art::StackHandleScope<1>* hs, |
| 753 | art::Runtime* runtime, |
| 754 | art::Thread* self, |
| 755 | int32_t num_redefinitions) REQUIRES_SHARED(art::Locks::mutator_lock_) : |
| 756 | arr_( |
| 757 | hs->NewHandle( |
| 758 | art::mirror::ObjectArray<art::mirror::Object>::Alloc( |
| 759 | self, |
| 760 | runtime->GetClassLinker()->GetClassRoot(art::ClassLinker::kObjectArrayClass), |
| 761 | num_redefinitions * kNumSlots))) {} |
| 762 | |
| 763 | bool IsNull() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 764 | return arr_.IsNull(); |
| 765 | } |
| 766 | |
| 767 | // TODO Maybe make an iterable view type to simplify using this. |
| 768 | art::mirror::ClassLoader* GetSourceClassLoader(jint klass_index) |
| 769 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 770 | return art::down_cast<art::mirror::ClassLoader*>(GetSlot(klass_index, kSlotSourceClassLoader)); |
| 771 | } |
| 772 | art::mirror::Object* GetJavaDexFile(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 773 | return GetSlot(klass_index, kSlotJavaDexFile); |
| 774 | } |
| 775 | art::mirror::LongArray* GetNewDexFileCookie(jint klass_index) |
| 776 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 777 | return art::down_cast<art::mirror::LongArray*>(GetSlot(klass_index, kSlotNewDexFileCookie)); |
| 778 | } |
| 779 | art::mirror::DexCache* GetNewDexCache(jint klass_index) |
| 780 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 781 | return art::down_cast<art::mirror::DexCache*>(GetSlot(klass_index, kSlotNewDexCache)); |
| 782 | } |
| 783 | art::mirror::Class* GetMirrorClass(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 784 | return art::down_cast<art::mirror::Class*>(GetSlot(klass_index, kSlotMirrorClass)); |
| 785 | } |
| 786 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 787 | art::mirror::ByteArray* GetOriginalDexFileBytes(jint klass_index) |
| 788 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 789 | return art::down_cast<art::mirror::ByteArray*>(GetSlot(klass_index, kSlotOrigDexFile)); |
| 790 | } |
| 791 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 792 | void SetSourceClassLoader(jint klass_index, art::mirror::ClassLoader* loader) |
| 793 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 794 | SetSlot(klass_index, kSlotSourceClassLoader, loader); |
| 795 | } |
| 796 | void SetJavaDexFile(jint klass_index, art::mirror::Object* dexfile) |
| 797 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 798 | SetSlot(klass_index, kSlotJavaDexFile, dexfile); |
| 799 | } |
| 800 | void SetNewDexFileCookie(jint klass_index, art::mirror::LongArray* cookie) |
| 801 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 802 | SetSlot(klass_index, kSlotNewDexFileCookie, cookie); |
| 803 | } |
| 804 | void SetNewDexCache(jint klass_index, art::mirror::DexCache* cache) |
| 805 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 806 | SetSlot(klass_index, kSlotNewDexCache, cache); |
| 807 | } |
| 808 | void SetMirrorClass(jint klass_index, art::mirror::Class* klass) |
| 809 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 810 | SetSlot(klass_index, kSlotMirrorClass, klass); |
| 811 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 812 | void SetOriginalDexFileBytes(jint klass_index, art::mirror::ByteArray* bytes) |
| 813 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 814 | SetSlot(klass_index, kSlotOrigDexFile, bytes); |
| 815 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 816 | |
| 817 | int32_t Length() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 818 | return arr_->GetLength() / kNumSlots; |
| 819 | } |
| 820 | |
| 821 | private: |
| 822 | art::Handle<art::mirror::ObjectArray<art::mirror::Object>> arr_; |
| 823 | |
| 824 | art::mirror::Object* GetSlot(jint klass_index, |
| 825 | DataSlot slot) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 826 | DCHECK_LT(klass_index, Length()); |
| 827 | return arr_->Get((kNumSlots * klass_index) + slot); |
| 828 | } |
| 829 | |
| 830 | void SetSlot(jint klass_index, |
| 831 | DataSlot slot, |
| 832 | art::ObjPtr<art::mirror::Object> obj) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 833 | DCHECK(!art::Runtime::Current()->IsActiveTransaction()); |
| 834 | DCHECK_LT(klass_index, Length()); |
| 835 | arr_->Set<false>((kNumSlots * klass_index) + slot, obj); |
| 836 | } |
| 837 | |
| 838 | DISALLOW_COPY_AND_ASSIGN(RedefinitionDataHolder); |
| 839 | }; |
| 840 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 841 | bool Redefiner::ClassRedefinition::FinishRemainingAllocations( |
| 842 | int32_t klass_index, /*out*/RedefinitionDataHolder* holder) { |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 843 | art::ScopedObjectAccessUnchecked soa(driver_->self_); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 844 | art::StackHandleScope<2> hs(driver_->self_); |
| 845 | holder->SetMirrorClass(klass_index, GetMirrorClass()); |
| 846 | // This shouldn't allocate |
| 847 | art::Handle<art::mirror::ClassLoader> loader(hs.NewHandle(GetClassLoader())); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 848 | // The bootclasspath is handled specially so it doesn't have a j.l.DexFile. |
| 849 | if (!art::ClassLinker::IsBootClassLoader(soa, loader.Get())) { |
| 850 | holder->SetSourceClassLoader(klass_index, loader.Get()); |
| 851 | art::Handle<art::mirror::Object> dex_file_obj(hs.NewHandle( |
| 852 | ClassLoaderHelper::FindSourceDexFileObject(driver_->self_, loader))); |
| 853 | holder->SetJavaDexFile(klass_index, dex_file_obj.Get()); |
| 854 | if (dex_file_obj.Get() == nullptr) { |
| 855 | // TODO Better error msg. |
| 856 | RecordFailure(ERR(INTERNAL), "Unable to find dex file!"); |
| 857 | return false; |
| 858 | } |
| 859 | holder->SetNewDexFileCookie(klass_index, |
| 860 | ClassLoaderHelper::AllocateNewDexFileCookie(driver_->self_, |
| 861 | dex_file_obj, |
| 862 | dex_file_.get()).Ptr()); |
| 863 | if (holder->GetNewDexFileCookie(klass_index) == nullptr) { |
| 864 | driver_->self_->AssertPendingOOMException(); |
| 865 | driver_->self_->ClearException(); |
| 866 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate dex file array for class loader"); |
| 867 | return false; |
| 868 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 869 | } |
| 870 | holder->SetNewDexCache(klass_index, CreateNewDexCache(loader)); |
| 871 | if (holder->GetNewDexCache(klass_index) == nullptr) { |
| 872 | driver_->self_->AssertPendingOOMException(); |
| 873 | driver_->self_->ClearException(); |
| 874 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate DexCache"); |
| 875 | return false; |
| 876 | } |
| 877 | |
| 878 | // We won't always need to set this field. |
| 879 | holder->SetOriginalDexFileBytes(klass_index, AllocateOrGetOriginalDexFileBytes()); |
| 880 | if (holder->GetOriginalDexFileBytes(klass_index) == nullptr) { |
| 881 | driver_->self_->AssertPendingOOMException(); |
| 882 | driver_->self_->ClearException(); |
| 883 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate array for original dex file"); |
| 884 | return false; |
| 885 | } |
| 886 | return true; |
| 887 | } |
| 888 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 889 | bool Redefiner::CheckAllRedefinitionAreValid() { |
| 890 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 891 | if (!redef.CheckRedefinitionIsValid()) { |
| 892 | return false; |
| 893 | } |
| 894 | } |
| 895 | return true; |
| 896 | } |
| 897 | |
| 898 | bool Redefiner::EnsureAllClassAllocationsFinished() { |
| 899 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 900 | if (!redef.EnsureClassAllocationsFinished()) { |
| 901 | return false; |
| 902 | } |
| 903 | } |
| 904 | return true; |
| 905 | } |
| 906 | |
| 907 | bool Redefiner::FinishAllRemainingAllocations(RedefinitionDataHolder& holder) { |
| 908 | int32_t cnt = 0; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 909 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 910 | // Allocate the data this redefinition requires. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 911 | if (!redef.FinishRemainingAllocations(cnt, &holder)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 912 | return false; |
| 913 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 914 | cnt++; |
| 915 | } |
| 916 | return true; |
| 917 | } |
| 918 | |
| 919 | void Redefiner::ClassRedefinition::ReleaseDexFile() { |
| 920 | dex_file_.release(); |
| 921 | } |
| 922 | |
| 923 | void Redefiner::ReleaseAllDexFiles() { |
| 924 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 925 | redef.ReleaseDexFile(); |
| 926 | } |
| 927 | } |
| 928 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 929 | jvmtiError Redefiner::Run() { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 930 | art::StackHandleScope<1> hs(self_); |
| 931 | // Allocate an array to hold onto all java temporary objects associated with this redefinition. |
| 932 | // We will let this be collected after the end of this function. |
| 933 | RedefinitionDataHolder holder(&hs, runtime_, self_, redefinitions_.size()); |
| 934 | if (holder.IsNull()) { |
| 935 | self_->AssertPendingOOMException(); |
| 936 | self_->ClearException(); |
| 937 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate storage for temporaries"); |
| 938 | return result_; |
| 939 | } |
| 940 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 941 | // First we just allocate the ClassExt and its fields that we need. These can be updated |
| 942 | // atomically without any issues (since we allocate the map arrays as empty) so we don't bother |
| 943 | // doing a try loop. The other allocations we need to ensure that nothing has changed in the time |
| 944 | // between allocating them and pausing all threads before we can update them so we need to do a |
| 945 | // try loop. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 946 | if (!CheckAllRedefinitionAreValid() || |
| 947 | !EnsureAllClassAllocationsFinished() || |
| 948 | !FinishAllRemainingAllocations(holder)) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 949 | // TODO Null out the ClassExt fields we allocated (if possible, might be racing with another |
| 950 | // redefineclass call which made it even bigger. Leak shouldn't be huge (2x array of size |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 951 | // declared_methods_.length) but would be good to get rid of. All other allocations should be |
| 952 | // cleaned up by the GC eventually. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 953 | return result_; |
| 954 | } |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 955 | int32_t counter = 0; |
| 956 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 957 | if (holder.GetSourceClassLoader(counter) == nullptr) { |
| 958 | runtime_->GetClassLinker()->AppendToBootClassPath(self_, redef.GetDexFile()); |
| 959 | } |
| 960 | counter++; |
| 961 | } |
Alex Light | 6abd539 | 2017-01-05 17:53:00 -0800 | [diff] [blame] | 962 | // Disable GC and wait for it to be done if we are a moving GC. This is fine since we are done |
| 963 | // allocating so no deadlocks. |
| 964 | art::gc::Heap* heap = runtime_->GetHeap(); |
| 965 | if (heap->IsGcConcurrentAndMoving()) { |
| 966 | // GC moving objects can cause deadlocks as we are deoptimizing the stack. |
| 967 | heap->IncrementDisableMovingGC(self_); |
| 968 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 969 | // Do transition to final suspension |
| 970 | // TODO We might want to give this its own suspended state! |
| 971 | // TODO This isn't right. We need to change state without any chance of suspend ideally! |
| 972 | self_->TransitionFromRunnableToSuspended(art::ThreadState::kNative); |
| 973 | runtime_->GetThreadList()->SuspendAll( |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 974 | "Final installation of redefined Classes!", /*long_suspend*/true); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 975 | // TODO We need to invalidate all breakpoints in the redefined class with the debugger. |
| 976 | // TODO We need to deal with any instrumentation/debugger deoptimized_methods_. |
| 977 | // TODO We need to update all debugger MethodIDs so they note the method they point to is |
| 978 | // obsolete or implement some other well defined semantics. |
| 979 | // TODO We need to decide on & implement semantics for JNI jmethodids when we redefine methods. |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 980 | counter = 0; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 981 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 982 | art::ScopedAssertNoThreadSuspension nts("Updating runtime objects for redefinition"); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 983 | if (holder.GetSourceClassLoader(counter) != nullptr) { |
| 984 | ClassLoaderHelper::UpdateJavaDexFile(holder.GetJavaDexFile(counter), |
| 985 | holder.GetNewDexFileCookie(counter)); |
| 986 | } |
| 987 | art::mirror::Class* klass = holder.GetMirrorClass(counter); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 988 | // TODO Rewrite so we don't do a stack walk for each and every class. |
| 989 | redef.FindAndAllocateObsoleteMethods(klass); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 990 | redef.UpdateClass(klass, holder.GetNewDexCache(counter), |
| 991 | holder.GetOriginalDexFileBytes(counter)); |
| 992 | counter++; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 993 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 994 | // TODO Verify the new Class. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 995 | // TODO Shrink the obsolete method maps if possible? |
| 996 | // TODO find appropriate class loader. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 997 | // TODO Put this into a scoped thing. |
| 998 | runtime_->GetThreadList()->ResumeAll(); |
| 999 | // Get back shared mutator lock as expected for return. |
| 1000 | self_->TransitionFromSuspendedToRunnable(); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1001 | // TODO Do the dex_file release at a more reasonable place. This works but it muddles who really |
| 1002 | // owns the DexFile and when ownership is transferred. |
| 1003 | ReleaseAllDexFiles(); |
Alex Light | 6abd539 | 2017-01-05 17:53:00 -0800 | [diff] [blame] | 1004 | if (heap->IsGcConcurrentAndMoving()) { |
| 1005 | heap->DecrementDisableMovingGC(self_); |
| 1006 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1007 | return OK; |
| 1008 | } |
| 1009 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1010 | void Redefiner::ClassRedefinition::UpdateMethods(art::ObjPtr<art::mirror::Class> mclass, |
| 1011 | art::ObjPtr<art::mirror::DexCache> new_dex_cache, |
| 1012 | const art::DexFile::ClassDef& class_def) { |
| 1013 | art::ClassLinker* linker = driver_->runtime_->GetClassLinker(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1014 | art::PointerSize image_pointer_size = linker->GetImagePointerSize(); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1015 | 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] | 1016 | const art::DexFile& old_dex_file = mclass->GetDexFile(); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1017 | // Update methods. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1018 | for (art::ArtMethod& method : mclass->GetMethods(image_pointer_size)) { |
| 1019 | const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(method.GetName()); |
| 1020 | art::dex::TypeIndex method_return_idx = |
| 1021 | dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(method.GetReturnTypeDescriptor())); |
| 1022 | const auto* old_type_list = method.GetParameterTypeList(); |
| 1023 | std::vector<art::dex::TypeIndex> new_type_list; |
| 1024 | for (uint32_t i = 0; old_type_list != nullptr && i < old_type_list->Size(); i++) { |
| 1025 | new_type_list.push_back( |
| 1026 | dex_file_->GetIndexForTypeId( |
| 1027 | *dex_file_->FindTypeId( |
| 1028 | old_dex_file.GetTypeDescriptor( |
| 1029 | old_dex_file.GetTypeId( |
| 1030 | old_type_list->GetTypeItem(i).type_idx_))))); |
| 1031 | } |
| 1032 | const art::DexFile::ProtoId* proto_id = dex_file_->FindProtoId(method_return_idx, |
| 1033 | new_type_list); |
Nicolas Geoffray | f6abcda | 2016-12-21 09:26:18 +0000 | [diff] [blame] | 1034 | // TODO Return false, cleanup. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1035 | CHECK(proto_id != nullptr || old_type_list == nullptr); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1036 | const art::DexFile::MethodId* method_id = dex_file_->FindMethodId(declaring_class_id, |
| 1037 | *new_name_id, |
| 1038 | *proto_id); |
Nicolas Geoffray | f6abcda | 2016-12-21 09:26:18 +0000 | [diff] [blame] | 1039 | // TODO Return false, cleanup. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1040 | CHECK(method_id != nullptr); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1041 | uint32_t dex_method_idx = dex_file_->GetIndexForMethodId(*method_id); |
| 1042 | method.SetDexMethodIndex(dex_method_idx); |
| 1043 | linker->SetEntryPointsToInterpreter(&method); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1044 | method.SetCodeItemOffset(dex_file_->FindCodeItemOffset(class_def, dex_method_idx)); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1045 | method.SetDexCacheResolvedMethods(new_dex_cache->GetResolvedMethods(), image_pointer_size); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1046 | // Notify the jit that this method is redefined. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1047 | art::jit::Jit* jit = driver_->runtime_->GetJit(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1048 | if (jit != nullptr) { |
| 1049 | jit->GetCodeCache()->NotifyMethodRedefined(&method); |
| 1050 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1051 | } |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1052 | } |
| 1053 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1054 | void Redefiner::ClassRedefinition::UpdateFields(art::ObjPtr<art::mirror::Class> mclass) { |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1055 | // TODO The IFields & SFields pointers should be combined like the methods_ arrays were. |
| 1056 | for (auto fields_iter : {mclass->GetIFields(), mclass->GetSFields()}) { |
| 1057 | for (art::ArtField& field : fields_iter) { |
| 1058 | std::string declaring_class_name; |
| 1059 | const art::DexFile::TypeId* new_declaring_id = |
| 1060 | dex_file_->FindTypeId(field.GetDeclaringClass()->GetDescriptor(&declaring_class_name)); |
| 1061 | const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(field.GetName()); |
| 1062 | const art::DexFile::TypeId* new_type_id = dex_file_->FindTypeId(field.GetTypeDescriptor()); |
| 1063 | // TODO Handle error, cleanup. |
| 1064 | CHECK(new_name_id != nullptr && new_type_id != nullptr && new_declaring_id != nullptr); |
| 1065 | const art::DexFile::FieldId* new_field_id = |
| 1066 | dex_file_->FindFieldId(*new_declaring_id, *new_name_id, *new_type_id); |
| 1067 | CHECK(new_field_id != nullptr); |
| 1068 | // We only need to update the index since the other data in the ArtField cannot be updated. |
| 1069 | field.SetDexFieldIndex(dex_file_->GetIndexForFieldId(*new_field_id)); |
| 1070 | } |
| 1071 | } |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | // Performs updates to class that will allow us to verify it. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1075 | void Redefiner::ClassRedefinition::UpdateClass( |
| 1076 | art::ObjPtr<art::mirror::Class> mclass, |
| 1077 | art::ObjPtr<art::mirror::DexCache> new_dex_cache, |
| 1078 | art::ObjPtr<art::mirror::ByteArray> original_dex_file) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 1079 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
| 1080 | const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(0); |
| 1081 | UpdateMethods(mclass, new_dex_cache, class_def); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 1082 | UpdateFields(mclass); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 1083 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1084 | // Update the class fields. |
| 1085 | // Need to update class last since the ArtMethod gets its DexFile from the class (which is needed |
| 1086 | // to call GetReturnTypeDescriptor and GetParameterTypeList above). |
| 1087 | mclass->SetDexCache(new_dex_cache.Ptr()); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 1088 | mclass->SetDexClassDefIndex(dex_file_->GetIndexForClassDef(class_def)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1089 | mclass->SetDexTypeIndex(dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(class_sig_.c_str()))); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1090 | art::ObjPtr<art::mirror::ClassExt> ext(mclass->GetExtData()); |
| 1091 | CHECK(!ext.IsNull()); |
| 1092 | ext->SetOriginalDexFileBytes(original_dex_file); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1095 | // This function does all (java) allocations we need to do for the Class being redefined. |
| 1096 | // TODO Change this name maybe? |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1097 | bool Redefiner::ClassRedefinition::EnsureClassAllocationsFinished() { |
| 1098 | art::StackHandleScope<2> hs(driver_->self_); |
| 1099 | art::Handle<art::mirror::Class> klass(hs.NewHandle( |
| 1100 | driver_->self_->DecodeJObject(klass_)->AsClass())); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1101 | if (klass.Get() == nullptr) { |
| 1102 | RecordFailure(ERR(INVALID_CLASS), "Unable to decode class argument!"); |
| 1103 | return false; |
| 1104 | } |
| 1105 | // Allocate the classExt |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1106 | art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(driver_->self_))); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1107 | if (ext.Get() == nullptr) { |
| 1108 | // No memory. Clear exception (it's not useful) and return error. |
| 1109 | // TODO This doesn't need to be fatal. We could just not support obsolete methods after hitting |
| 1110 | // this case. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1111 | driver_->self_->AssertPendingOOMException(); |
| 1112 | driver_->self_->ClearException(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1113 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate ClassExt"); |
| 1114 | return false; |
| 1115 | } |
| 1116 | // Allocate the 2 arrays that make up the obsolete methods map. Since the contents of the arrays |
| 1117 | // are only modified when all threads (other than the modifying one) are suspended we don't need |
| 1118 | // to worry about missing the unsyncronized writes to the array. We do synchronize when setting it |
| 1119 | // however, since that can happen at any time. |
| 1120 | // TODO Clear these after we walk the stacks in order to free them in the (likely?) event there |
| 1121 | // are no obsolete methods. |
| 1122 | { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1123 | art::ObjectLock<art::mirror::ClassExt> lock(driver_->self_, ext); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1124 | if (!ext->ExtendObsoleteArrays( |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1125 | driver_->self_, klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size())) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1126 | // OOM. Clear exception and return error. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1127 | driver_->self_->AssertPendingOOMException(); |
| 1128 | driver_->self_->ClearException(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1129 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate/extend obsolete methods map"); |
| 1130 | return false; |
| 1131 | } |
| 1132 | } |
| 1133 | return true; |
| 1134 | } |
| 1135 | |
| 1136 | } // namespace openjdkjvmti |