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" |
| 51 | #include "mirror/class.h" |
| 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(); |
Alex Light | fcbafb3 | 2017-02-02 15:09:54 -0800 | [diff] [blame^] | 124 | new_obsolete_method->SetDontCompile(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 125 | obsolete_maps_->insert({old_method, new_obsolete_method}); |
| 126 | // Update JIT Data structures to point to the new method. |
| 127 | art::jit::Jit* jit = art::Runtime::Current()->GetJit(); |
| 128 | if (jit != nullptr) { |
| 129 | // Notify the JIT we are making this obsolete method. It will update the jit's internal |
| 130 | // structures to keep track of the new obsolete method. |
| 131 | jit->GetCodeCache()->MoveObsoleteMethod(old_method, new_obsolete_method); |
| 132 | } |
| 133 | } else { |
| 134 | new_obsolete_method = obsolete_method_pair->second; |
| 135 | } |
| 136 | DCHECK(new_obsolete_method != nullptr); |
| 137 | SetMethod(new_obsolete_method); |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | // The linear allocator we should use to make new methods. |
| 144 | art::LinearAlloc* allocator_; |
| 145 | // The set of all methods which could be obsoleted. |
| 146 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods_; |
| 147 | // A map from the original to the newly allocated obsolete method for frames on this thread. The |
| 148 | // values in this map must be added to the obsolete_methods_ (and obsolete_dex_caches_) fields of |
| 149 | // the redefined classes ClassExt by the caller. |
| 150 | std::unordered_map<art::ArtMethod*, art::ArtMethod*>* obsolete_maps_; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 153 | jvmtiError Redefiner::IsModifiableClass(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 154 | jclass klass, |
| 155 | jboolean* is_redefinable) { |
| 156 | // TODO Check for the appropriate feature flags once we have enabled them. |
| 157 | art::Thread* self = art::Thread::Current(); |
| 158 | art::ScopedObjectAccess soa(self); |
| 159 | art::StackHandleScope<1> hs(self); |
| 160 | art::ObjPtr<art::mirror::Object> obj(self->DecodeJObject(klass)); |
| 161 | if (obj.IsNull()) { |
| 162 | return ERR(INVALID_CLASS); |
| 163 | } |
| 164 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(obj->AsClass())); |
| 165 | std::string err_unused; |
| 166 | *is_redefinable = |
| 167 | Redefiner::GetClassRedefinitionError(h_klass, &err_unused) == OK ? JNI_TRUE : JNI_FALSE; |
| 168 | return OK; |
| 169 | } |
| 170 | |
| 171 | jvmtiError Redefiner::GetClassRedefinitionError(art::Handle<art::mirror::Class> klass, |
| 172 | /*out*/std::string* error_msg) { |
| 173 | if (klass->IsPrimitive()) { |
| 174 | *error_msg = "Modification of primitive classes is not supported"; |
| 175 | return ERR(UNMODIFIABLE_CLASS); |
| 176 | } else if (klass->IsInterface()) { |
| 177 | *error_msg = "Modification of Interface classes is currently not supported"; |
| 178 | return ERR(UNMODIFIABLE_CLASS); |
| 179 | } else if (klass->IsArrayClass()) { |
| 180 | *error_msg = "Modification of Array classes is not supported"; |
| 181 | return ERR(UNMODIFIABLE_CLASS); |
| 182 | } else if (klass->IsProxyClass()) { |
| 183 | *error_msg = "Modification of proxy classes is not supported"; |
| 184 | return ERR(UNMODIFIABLE_CLASS); |
| 185 | } |
| 186 | |
| 187 | // TODO We should check if the class has non-obsoletable methods on the stack |
| 188 | LOG(WARNING) << "presence of non-obsoletable methods on stacks is not currently checked"; |
| 189 | return OK; |
| 190 | } |
| 191 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 192 | // Moves dex data to an anonymous, read-only mmap'd region. |
| 193 | std::unique_ptr<art::MemMap> Redefiner::MoveDataToMemMap(const std::string& original_location, |
| 194 | jint data_len, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 195 | const unsigned char* dex_data, |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 196 | std::string* error_msg) { |
| 197 | std::unique_ptr<art::MemMap> map(art::MemMap::MapAnonymous( |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 198 | StringPrintf("%s-transformed", original_location.c_str()).c_str(), |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 199 | nullptr, |
| 200 | data_len, |
| 201 | PROT_READ|PROT_WRITE, |
| 202 | /*low_4gb*/false, |
| 203 | /*reuse*/false, |
| 204 | error_msg)); |
| 205 | if (map == nullptr) { |
| 206 | return map; |
| 207 | } |
| 208 | memcpy(map->Begin(), dex_data, data_len); |
Alex Light | 0b77257 | 2016-12-02 17:27:31 -0800 | [diff] [blame] | 209 | // Make the dex files mmap read only. This matches how other DexFiles are mmaped and prevents |
| 210 | // programs from corrupting it. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 211 | map->Protect(PROT_READ); |
| 212 | return map; |
| 213 | } |
| 214 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 215 | Redefiner::ClassRedefinition::ClassRedefinition( |
| 216 | Redefiner* driver, |
| 217 | jclass klass, |
| 218 | const art::DexFile* redefined_dex_file, |
| 219 | const char* class_sig, |
| 220 | art::ArraySlice<const unsigned char> orig_dex_file) : |
| 221 | driver_(driver), |
| 222 | klass_(klass), |
| 223 | dex_file_(redefined_dex_file), |
| 224 | class_sig_(class_sig), |
| 225 | original_dex_file_(orig_dex_file) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 226 | GetMirrorClass()->MonitorEnter(driver_->self_); |
| 227 | } |
| 228 | |
| 229 | Redefiner::ClassRedefinition::~ClassRedefinition() { |
| 230 | if (driver_ != nullptr) { |
| 231 | GetMirrorClass()->MonitorExit(driver_->self_); |
| 232 | } |
| 233 | } |
| 234 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 235 | jvmtiError Redefiner::RedefineClasses(ArtJvmTiEnv* env, |
| 236 | art::Runtime* runtime, |
| 237 | art::Thread* self, |
| 238 | jint class_count, |
| 239 | const jvmtiClassDefinition* definitions, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 240 | /*out*/std::string* error_msg) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 241 | if (env == nullptr) { |
| 242 | *error_msg = "env was null!"; |
| 243 | return ERR(INVALID_ENVIRONMENT); |
| 244 | } else if (class_count < 0) { |
| 245 | *error_msg = "class_count was less then 0"; |
| 246 | return ERR(ILLEGAL_ARGUMENT); |
| 247 | } else if (class_count == 0) { |
| 248 | // We don't actually need to do anything. Just return OK. |
| 249 | return OK; |
| 250 | } else if (definitions == nullptr) { |
| 251 | *error_msg = "null definitions!"; |
| 252 | return ERR(NULL_POINTER); |
| 253 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 254 | std::vector<ArtClassDefinition> def_vector; |
| 255 | def_vector.reserve(class_count); |
| 256 | for (jint i = 0; i < class_count; i++) { |
| 257 | // We make a copy of the class_bytes to pass into the retransformation. |
| 258 | // This makes cleanup easier (since we unambiguously own the bytes) and also is useful since we |
| 259 | // will need to keep the original bytes around unaltered for subsequent RetransformClasses calls |
| 260 | // to get the passed in bytes. |
| 261 | // TODO Implement saving the original bytes. |
| 262 | unsigned char* class_bytes_copy = nullptr; |
| 263 | jvmtiError res = env->Allocate(definitions[i].class_byte_count, &class_bytes_copy); |
| 264 | if (res != OK) { |
| 265 | return res; |
| 266 | } |
| 267 | memcpy(class_bytes_copy, definitions[i].class_bytes, definitions[i].class_byte_count); |
| 268 | |
| 269 | ArtClassDefinition def; |
| 270 | def.dex_len = definitions[i].class_byte_count; |
| 271 | def.dex_data = MakeJvmtiUniquePtr(env, class_bytes_copy); |
| 272 | // We are definitely modified. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 273 | def.SetModified(); |
| 274 | def.original_dex_file = art::ArraySlice<const unsigned char>(definitions[i].class_bytes, |
| 275 | definitions[i].class_byte_count); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 276 | res = Transformer::FillInTransformationData(env, definitions[i].klass, &def); |
| 277 | if (res != OK) { |
| 278 | return res; |
| 279 | } |
| 280 | def_vector.push_back(std::move(def)); |
| 281 | } |
| 282 | // Call all the transformation events. |
| 283 | jvmtiError res = Transformer::RetransformClassesDirect(env, |
| 284 | self, |
| 285 | &def_vector); |
| 286 | if (res != OK) { |
| 287 | // Something went wrong with transformation! |
| 288 | return res; |
| 289 | } |
| 290 | return RedefineClassesDirect(env, runtime, self, def_vector, error_msg); |
| 291 | } |
| 292 | |
| 293 | jvmtiError Redefiner::RedefineClassesDirect(ArtJvmTiEnv* env, |
| 294 | art::Runtime* runtime, |
| 295 | art::Thread* self, |
| 296 | const std::vector<ArtClassDefinition>& definitions, |
| 297 | std::string* error_msg) { |
| 298 | DCHECK(env != nullptr); |
| 299 | if (definitions.size() == 0) { |
| 300 | // We don't actually need to do anything. Just return OK. |
| 301 | return OK; |
| 302 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 303 | // Stop JIT for the duration of this redefine since the JIT might concurrently compile a method we |
| 304 | // are going to redefine. |
| 305 | art::jit::ScopedJitSuspend suspend_jit; |
| 306 | // Get shared mutator lock so we can lock all the classes. |
| 307 | art::ScopedObjectAccess soa(self); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 308 | Redefiner r(runtime, self, error_msg); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 309 | for (const ArtClassDefinition& def : definitions) { |
| 310 | // Only try to transform classes that have been modified. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 311 | if (def.IsModified(self)) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 312 | jvmtiError res = r.AddRedefinition(env, def); |
| 313 | if (res != OK) { |
| 314 | return res; |
| 315 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | return r.Run(); |
| 319 | } |
| 320 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 321 | jvmtiError Redefiner::AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 322 | std::string original_dex_location; |
| 323 | jvmtiError ret = OK; |
| 324 | if ((ret = GetClassLocation(env, def.klass, &original_dex_location))) { |
| 325 | *error_msg_ = "Unable to get original dex file location!"; |
| 326 | return ret; |
| 327 | } |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 328 | char* generic_ptr_unused = nullptr; |
| 329 | char* signature_ptr = nullptr; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 330 | if ((ret = env->GetClassSignature(def.klass, &signature_ptr, &generic_ptr_unused)) != OK) { |
| 331 | *error_msg_ = "Unable to get class signature!"; |
| 332 | return ret; |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 333 | } |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 334 | JvmtiUniquePtr generic_unique_ptr(MakeJvmtiUniquePtr(env, generic_ptr_unused)); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 335 | JvmtiUniquePtr signature_unique_ptr(MakeJvmtiUniquePtr(env, signature_ptr)); |
| 336 | std::unique_ptr<art::MemMap> map(MoveDataToMemMap(original_dex_location, |
| 337 | def.dex_len, |
| 338 | def.dex_data.get(), |
| 339 | error_msg_)); |
| 340 | std::ostringstream os; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 341 | if (map.get() == nullptr) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 342 | 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] | 343 | << "in dex file " << original_dex_location << " because: " << *error_msg_; |
| 344 | *error_msg_ = os.str(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 345 | return ERR(OUT_OF_MEMORY); |
| 346 | } |
| 347 | if (map->Size() < sizeof(art::DexFile::Header)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 348 | *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] | 349 | return ERR(INVALID_CLASS_FORMAT); |
| 350 | } |
| 351 | uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map->Begin())->checksum_; |
| 352 | std::unique_ptr<const art::DexFile> dex_file(art::DexFile::Open(map->GetName(), |
| 353 | checksum, |
| 354 | std::move(map), |
| 355 | /*verify*/true, |
| 356 | /*verify_checksum*/true, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 357 | error_msg_)); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 358 | if (dex_file.get() == nullptr) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 359 | os << "Unable to load modified dex file for " << def.name << ": " << *error_msg_; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 360 | *error_msg_ = os.str(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 361 | return ERR(INVALID_CLASS_FORMAT); |
| 362 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 363 | redefinitions_.push_back( |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 364 | Redefiner::ClassRedefinition(this, |
| 365 | def.klass, |
| 366 | dex_file.release(), |
| 367 | signature_ptr, |
| 368 | def.original_dex_file)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 369 | return OK; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 372 | art::mirror::Class* Redefiner::ClassRedefinition::GetMirrorClass() { |
| 373 | return driver_->self_->DecodeJObject(klass_)->AsClass(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 376 | art::mirror::ClassLoader* Redefiner::ClassRedefinition::GetClassLoader() { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 377 | return GetMirrorClass()->GetClassLoader(); |
| 378 | } |
| 379 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 380 | art::mirror::DexCache* Redefiner::ClassRedefinition::CreateNewDexCache( |
| 381 | art::Handle<art::mirror::ClassLoader> loader) { |
| 382 | return driver_->runtime_->GetClassLinker()->RegisterDexFile(*dex_file_, loader.Get()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 385 | void Redefiner::RecordFailure(jvmtiError result, |
| 386 | const std::string& class_sig, |
| 387 | const std::string& error_msg) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 388 | *error_msg_ = StringPrintf("Unable to perform redefinition of '%s': %s", |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 389 | class_sig.c_str(), |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 390 | error_msg.c_str()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 391 | result_ = result; |
| 392 | } |
| 393 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 394 | art::mirror::ByteArray* Redefiner::ClassRedefinition::AllocateOrGetOriginalDexFileBytes() { |
| 395 | // If we have been specifically given a new set of bytes use that |
| 396 | if (original_dex_file_.size() != 0) { |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 397 | return art::mirror::ByteArray::AllocateAndFill( |
| 398 | driver_->self_, |
| 399 | reinterpret_cast<const signed char*>(&original_dex_file_.At(0)), |
| 400 | original_dex_file_.size()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 401 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 402 | |
| 403 | // See if we already have one set. |
| 404 | art::ObjPtr<art::mirror::ClassExt> ext(GetMirrorClass()->GetExtData()); |
| 405 | if (!ext.IsNull()) { |
| 406 | art::ObjPtr<art::mirror::ByteArray> old_original_bytes(ext->GetOriginalDexFileBytes()); |
| 407 | if (!old_original_bytes.IsNull()) { |
| 408 | // We do. Use it. |
| 409 | return old_original_bytes.Ptr(); |
| 410 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 411 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 412 | |
| 413 | // Copy the current dex_file |
| 414 | const art::DexFile& current_dex_file = GetMirrorClass()->GetDexFile(); |
| 415 | // TODO Handle this or make it so it cannot happen. |
| 416 | if (current_dex_file.NumClassDefs() != 1) { |
| 417 | LOG(WARNING) << "Current dex file has more than one class in it. Calling RetransformClasses " |
| 418 | << "on this class might fail if no transformations are applied to it!"; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 419 | } |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 420 | return art::mirror::ByteArray::AllocateAndFill( |
| 421 | driver_->self_, |
| 422 | reinterpret_cast<const signed char*>(current_dex_file.Begin()), |
| 423 | current_dex_file.Size()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 426 | struct CallbackCtx { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 427 | art::LinearAlloc* allocator; |
| 428 | std::unordered_map<art::ArtMethod*, art::ArtMethod*> obsolete_map; |
| 429 | std::unordered_set<art::ArtMethod*> obsolete_methods; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 430 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 431 | explicit CallbackCtx(art::LinearAlloc* alloc) : allocator(alloc) {} |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 432 | }; |
| 433 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 434 | void DoAllocateObsoleteMethodsCallback(art::Thread* t, void* vdata) NO_THREAD_SAFETY_ANALYSIS { |
| 435 | CallbackCtx* data = reinterpret_cast<CallbackCtx*>(vdata); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 436 | ObsoleteMethodStackVisitor::UpdateObsoleteFrames(t, |
| 437 | data->allocator, |
| 438 | data->obsolete_methods, |
| 439 | &data->obsolete_map); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // This creates any ArtMethod* structures needed for obsolete methods and ensures that the stack is |
| 443 | // updated so they will be run. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 444 | // TODO Rewrite so we can do this only once regardless of how many redefinitions there are. |
| 445 | void Redefiner::ClassRedefinition::FindAndAllocateObsoleteMethods(art::mirror::Class* art_klass) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 446 | art::ScopedAssertNoThreadSuspension ns("No thread suspension during thread stack walking"); |
| 447 | art::mirror::ClassExt* ext = art_klass->GetExtData(); |
| 448 | CHECK(ext->GetObsoleteMethods() != nullptr); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 449 | art::ClassLinker* linker = driver_->runtime_->GetClassLinker(); |
| 450 | CallbackCtx ctx(linker->GetAllocatorForClassLoader(art_klass->GetClassLoader())); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 451 | // Add all the declared methods to the map |
| 452 | for (auto& m : art_klass->GetDeclaredMethods(art::kRuntimePointerSize)) { |
| 453 | ctx.obsolete_methods.insert(&m); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 454 | // TODO Allow this or check in IsModifiableClass. |
| 455 | DCHECK(!m.IsIntrinsic()); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 456 | } |
| 457 | { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 458 | art::MutexLock mu(driver_->self_, *art::Locks::thread_list_lock_); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 459 | art::ThreadList* list = art::Runtime::Current()->GetThreadList(); |
| 460 | list->ForEach(DoAllocateObsoleteMethodsCallback, static_cast<void*>(&ctx)); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 461 | } |
| 462 | FillObsoleteMethodMap(art_klass, ctx.obsolete_map); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // Fills the obsolete method map in the art_klass's extData. This is so obsolete methods are able to |
| 466 | // figure out their DexCaches. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 467 | void Redefiner::ClassRedefinition::FillObsoleteMethodMap( |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 468 | art::mirror::Class* art_klass, |
| 469 | const std::unordered_map<art::ArtMethod*, art::ArtMethod*>& obsoletes) { |
| 470 | int32_t index = 0; |
| 471 | art::mirror::ClassExt* ext_data = art_klass->GetExtData(); |
| 472 | art::mirror::PointerArray* obsolete_methods = ext_data->GetObsoleteMethods(); |
| 473 | art::mirror::ObjectArray<art::mirror::DexCache>* obsolete_dex_caches = |
| 474 | ext_data->GetObsoleteDexCaches(); |
| 475 | int32_t num_method_slots = obsolete_methods->GetLength(); |
| 476 | // Find the first empty index. |
| 477 | for (; index < num_method_slots; index++) { |
| 478 | if (obsolete_methods->GetElementPtrSize<art::ArtMethod*>( |
| 479 | index, art::kRuntimePointerSize) == nullptr) { |
| 480 | break; |
| 481 | } |
| 482 | } |
| 483 | // Make sure we have enough space. |
| 484 | CHECK_GT(num_method_slots, static_cast<int32_t>(obsoletes.size() + index)); |
| 485 | CHECK(obsolete_dex_caches->Get(index) == nullptr); |
| 486 | // Fill in the map. |
| 487 | for (auto& obs : obsoletes) { |
| 488 | obsolete_methods->SetElementPtrSize(index, obs.second, art::kRuntimePointerSize); |
| 489 | obsolete_dex_caches->Set(index, art_klass->GetDexCache()); |
| 490 | index++; |
| 491 | } |
| 492 | } |
| 493 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 494 | bool Redefiner::ClassRedefinition::CheckClass() { |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 495 | // 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] | 496 | art::StackHandleScope<1> hs(driver_->self_); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 497 | // Easy check that only 1 class def is present. |
| 498 | if (dex_file_->NumClassDefs() != 1) { |
| 499 | RecordFailure(ERR(ILLEGAL_ARGUMENT), |
| 500 | StringPrintf("Expected 1 class def in dex file but found %d", |
| 501 | dex_file_->NumClassDefs())); |
| 502 | return false; |
| 503 | } |
| 504 | // Get the ClassDef from the new DexFile. |
| 505 | // Since the dex file has only a single class def the index is always 0. |
| 506 | const art::DexFile::ClassDef& def = dex_file_->GetClassDef(0); |
| 507 | // Get the class as it is now. |
| 508 | art::Handle<art::mirror::Class> current_class(hs.NewHandle(GetMirrorClass())); |
| 509 | |
| 510 | // Check the access flags didn't change. |
| 511 | if (def.GetJavaAccessFlags() != (current_class->GetAccessFlags() & art::kAccValidClassFlags)) { |
| 512 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED), |
| 513 | "Cannot change modifiers of class by redefinition"); |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | // Check class name. |
| 518 | // These should have been checked by the dexfile verifier on load. |
| 519 | DCHECK_NE(def.class_idx_, art::dex::TypeIndex::Invalid()) << "Invalid type index"; |
| 520 | const char* descriptor = dex_file_->StringByTypeIdx(def.class_idx_); |
| 521 | DCHECK(descriptor != nullptr) << "Invalid dex file structure!"; |
| 522 | if (!current_class->DescriptorEquals(descriptor)) { |
| 523 | std::string storage; |
| 524 | RecordFailure(ERR(NAMES_DONT_MATCH), |
| 525 | StringPrintf("expected file to contain class called '%s' but found '%s'!", |
| 526 | current_class->GetDescriptor(&storage), |
| 527 | descriptor)); |
| 528 | return false; |
| 529 | } |
| 530 | if (current_class->IsObjectClass()) { |
| 531 | if (def.superclass_idx_ != art::dex::TypeIndex::Invalid()) { |
| 532 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass added!"); |
| 533 | return false; |
| 534 | } |
| 535 | } else { |
| 536 | const char* super_descriptor = dex_file_->StringByTypeIdx(def.superclass_idx_); |
| 537 | DCHECK(descriptor != nullptr) << "Invalid dex file structure!"; |
| 538 | if (!current_class->GetSuperClass()->DescriptorEquals(super_descriptor)) { |
| 539 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass changed"); |
| 540 | return false; |
| 541 | } |
| 542 | } |
| 543 | const art::DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(def); |
| 544 | if (interfaces == nullptr) { |
| 545 | if (current_class->NumDirectInterfaces() != 0) { |
| 546 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added"); |
| 547 | return false; |
| 548 | } |
| 549 | } else { |
| 550 | DCHECK(!current_class->IsProxyClass()); |
| 551 | const art::DexFile::TypeList* current_interfaces = current_class->GetInterfaceTypeList(); |
| 552 | if (current_interfaces == nullptr || current_interfaces->Size() != interfaces->Size()) { |
| 553 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added or removed"); |
| 554 | return false; |
| 555 | } |
| 556 | // The order of interfaces is (barely) meaningful so we error if it changes. |
| 557 | const art::DexFile& orig_dex_file = current_class->GetDexFile(); |
| 558 | for (uint32_t i = 0; i < interfaces->Size(); i++) { |
| 559 | if (strcmp( |
| 560 | dex_file_->StringByTypeIdx(interfaces->GetTypeItem(i).type_idx_), |
| 561 | orig_dex_file.StringByTypeIdx(current_interfaces->GetTypeItem(i).type_idx_)) != 0) { |
| 562 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), |
| 563 | "Interfaces changed or re-ordered"); |
| 564 | return false; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | LOG(WARNING) << "No verification is done on annotations of redefined classes."; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 569 | LOG(WARNING) << "Bytecodes of redefinitions are not verified."; |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 570 | |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | // TODO Move this to use IsRedefinable when that function is made. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 575 | bool Redefiner::ClassRedefinition::CheckRedefinable() { |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 576 | std::string err; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 577 | art::StackHandleScope<1> hs(driver_->self_); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 578 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 579 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
| 580 | jvmtiError res = Redefiner::GetClassRedefinitionError(h_klass, &err); |
| 581 | if (res != OK) { |
| 582 | RecordFailure(res, err); |
| 583 | return false; |
| 584 | } else { |
| 585 | return true; |
| 586 | } |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 589 | bool Redefiner::ClassRedefinition::CheckRedefinitionIsValid() { |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 590 | return CheckRedefinable() && |
| 591 | CheckClass() && |
| 592 | CheckSameFields() && |
| 593 | CheckSameMethods(); |
| 594 | } |
| 595 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 596 | // A wrapper that lets us hold onto the arbitrary sized data needed for redefinitions in a |
| 597 | // reasonably sane way. This adds no fields to the normal ObjectArray. By doing this we can avoid |
| 598 | // having to deal with the fact that we need to hold an arbitrary number of references live. |
| 599 | class RedefinitionDataHolder { |
| 600 | public: |
| 601 | enum DataSlot : int32_t { |
| 602 | kSlotSourceClassLoader = 0, |
| 603 | kSlotJavaDexFile = 1, |
| 604 | kSlotNewDexFileCookie = 2, |
| 605 | kSlotNewDexCache = 3, |
| 606 | kSlotMirrorClass = 4, |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 607 | kSlotOrigDexFile = 5, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 608 | |
| 609 | // Must be last one. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 610 | kNumSlots = 6, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 611 | }; |
| 612 | |
| 613 | // This needs to have a HandleScope passed in that is capable of creating a new Handle without |
| 614 | // overflowing. Only one handle will be created. This object has a lifetime identical to that of |
| 615 | // the passed in handle-scope. |
| 616 | RedefinitionDataHolder(art::StackHandleScope<1>* hs, |
| 617 | art::Runtime* runtime, |
| 618 | art::Thread* self, |
| 619 | int32_t num_redefinitions) REQUIRES_SHARED(art::Locks::mutator_lock_) : |
| 620 | arr_( |
| 621 | hs->NewHandle( |
| 622 | art::mirror::ObjectArray<art::mirror::Object>::Alloc( |
| 623 | self, |
| 624 | runtime->GetClassLinker()->GetClassRoot(art::ClassLinker::kObjectArrayClass), |
| 625 | num_redefinitions * kNumSlots))) {} |
| 626 | |
| 627 | bool IsNull() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 628 | return arr_.IsNull(); |
| 629 | } |
| 630 | |
| 631 | // TODO Maybe make an iterable view type to simplify using this. |
| 632 | art::mirror::ClassLoader* GetSourceClassLoader(jint klass_index) |
| 633 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 634 | return art::down_cast<art::mirror::ClassLoader*>(GetSlot(klass_index, kSlotSourceClassLoader)); |
| 635 | } |
| 636 | art::mirror::Object* GetJavaDexFile(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 637 | return GetSlot(klass_index, kSlotJavaDexFile); |
| 638 | } |
| 639 | art::mirror::LongArray* GetNewDexFileCookie(jint klass_index) |
| 640 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 641 | return art::down_cast<art::mirror::LongArray*>(GetSlot(klass_index, kSlotNewDexFileCookie)); |
| 642 | } |
| 643 | art::mirror::DexCache* GetNewDexCache(jint klass_index) |
| 644 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 645 | return art::down_cast<art::mirror::DexCache*>(GetSlot(klass_index, kSlotNewDexCache)); |
| 646 | } |
| 647 | art::mirror::Class* GetMirrorClass(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 648 | return art::down_cast<art::mirror::Class*>(GetSlot(klass_index, kSlotMirrorClass)); |
| 649 | } |
| 650 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 651 | art::mirror::ByteArray* GetOriginalDexFileBytes(jint klass_index) |
| 652 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 653 | return art::down_cast<art::mirror::ByteArray*>(GetSlot(klass_index, kSlotOrigDexFile)); |
| 654 | } |
| 655 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 656 | void SetSourceClassLoader(jint klass_index, art::mirror::ClassLoader* loader) |
| 657 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 658 | SetSlot(klass_index, kSlotSourceClassLoader, loader); |
| 659 | } |
| 660 | void SetJavaDexFile(jint klass_index, art::mirror::Object* dexfile) |
| 661 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 662 | SetSlot(klass_index, kSlotJavaDexFile, dexfile); |
| 663 | } |
| 664 | void SetNewDexFileCookie(jint klass_index, art::mirror::LongArray* cookie) |
| 665 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 666 | SetSlot(klass_index, kSlotNewDexFileCookie, cookie); |
| 667 | } |
| 668 | void SetNewDexCache(jint klass_index, art::mirror::DexCache* cache) |
| 669 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 670 | SetSlot(klass_index, kSlotNewDexCache, cache); |
| 671 | } |
| 672 | void SetMirrorClass(jint klass_index, art::mirror::Class* klass) |
| 673 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 674 | SetSlot(klass_index, kSlotMirrorClass, klass); |
| 675 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 676 | void SetOriginalDexFileBytes(jint klass_index, art::mirror::ByteArray* bytes) |
| 677 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 678 | SetSlot(klass_index, kSlotOrigDexFile, bytes); |
| 679 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 680 | |
| 681 | int32_t Length() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 682 | return arr_->GetLength() / kNumSlots; |
| 683 | } |
| 684 | |
| 685 | private: |
| 686 | art::Handle<art::mirror::ObjectArray<art::mirror::Object>> arr_; |
| 687 | |
| 688 | art::mirror::Object* GetSlot(jint klass_index, |
| 689 | DataSlot slot) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 690 | DCHECK_LT(klass_index, Length()); |
| 691 | return arr_->Get((kNumSlots * klass_index) + slot); |
| 692 | } |
| 693 | |
| 694 | void SetSlot(jint klass_index, |
| 695 | DataSlot slot, |
| 696 | art::ObjPtr<art::mirror::Object> obj) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 697 | DCHECK(!art::Runtime::Current()->IsActiveTransaction()); |
| 698 | DCHECK_LT(klass_index, Length()); |
| 699 | arr_->Set<false>((kNumSlots * klass_index) + slot, obj); |
| 700 | } |
| 701 | |
| 702 | DISALLOW_COPY_AND_ASSIGN(RedefinitionDataHolder); |
| 703 | }; |
| 704 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 705 | bool Redefiner::ClassRedefinition::FinishRemainingAllocations( |
| 706 | int32_t klass_index, /*out*/RedefinitionDataHolder* holder) { |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 707 | art::ScopedObjectAccessUnchecked soa(driver_->self_); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 708 | art::StackHandleScope<2> hs(driver_->self_); |
| 709 | holder->SetMirrorClass(klass_index, GetMirrorClass()); |
| 710 | // This shouldn't allocate |
| 711 | art::Handle<art::mirror::ClassLoader> loader(hs.NewHandle(GetClassLoader())); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 712 | // The bootclasspath is handled specially so it doesn't have a j.l.DexFile. |
| 713 | if (!art::ClassLinker::IsBootClassLoader(soa, loader.Get())) { |
| 714 | holder->SetSourceClassLoader(klass_index, loader.Get()); |
| 715 | art::Handle<art::mirror::Object> dex_file_obj(hs.NewHandle( |
| 716 | ClassLoaderHelper::FindSourceDexFileObject(driver_->self_, loader))); |
| 717 | holder->SetJavaDexFile(klass_index, dex_file_obj.Get()); |
| 718 | if (dex_file_obj.Get() == nullptr) { |
| 719 | // TODO Better error msg. |
| 720 | RecordFailure(ERR(INTERNAL), "Unable to find dex file!"); |
| 721 | return false; |
| 722 | } |
| 723 | holder->SetNewDexFileCookie(klass_index, |
| 724 | ClassLoaderHelper::AllocateNewDexFileCookie(driver_->self_, |
| 725 | dex_file_obj, |
| 726 | dex_file_.get()).Ptr()); |
| 727 | if (holder->GetNewDexFileCookie(klass_index) == nullptr) { |
| 728 | driver_->self_->AssertPendingOOMException(); |
| 729 | driver_->self_->ClearException(); |
| 730 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate dex file array for class loader"); |
| 731 | return false; |
| 732 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 733 | } |
| 734 | holder->SetNewDexCache(klass_index, CreateNewDexCache(loader)); |
| 735 | if (holder->GetNewDexCache(klass_index) == nullptr) { |
| 736 | driver_->self_->AssertPendingOOMException(); |
| 737 | driver_->self_->ClearException(); |
| 738 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate DexCache"); |
| 739 | return false; |
| 740 | } |
| 741 | |
| 742 | // We won't always need to set this field. |
| 743 | holder->SetOriginalDexFileBytes(klass_index, AllocateOrGetOriginalDexFileBytes()); |
| 744 | if (holder->GetOriginalDexFileBytes(klass_index) == nullptr) { |
| 745 | driver_->self_->AssertPendingOOMException(); |
| 746 | driver_->self_->ClearException(); |
| 747 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate array for original dex file"); |
| 748 | return false; |
| 749 | } |
| 750 | return true; |
| 751 | } |
| 752 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 753 | bool Redefiner::CheckAllRedefinitionAreValid() { |
| 754 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 755 | if (!redef.CheckRedefinitionIsValid()) { |
| 756 | return false; |
| 757 | } |
| 758 | } |
| 759 | return true; |
| 760 | } |
| 761 | |
| 762 | bool Redefiner::EnsureAllClassAllocationsFinished() { |
| 763 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 764 | if (!redef.EnsureClassAllocationsFinished()) { |
| 765 | return false; |
| 766 | } |
| 767 | } |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | bool Redefiner::FinishAllRemainingAllocations(RedefinitionDataHolder& holder) { |
| 772 | int32_t cnt = 0; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 773 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 774 | // Allocate the data this redefinition requires. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 775 | if (!redef.FinishRemainingAllocations(cnt, &holder)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 776 | return false; |
| 777 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 778 | cnt++; |
| 779 | } |
| 780 | return true; |
| 781 | } |
| 782 | |
| 783 | void Redefiner::ClassRedefinition::ReleaseDexFile() { |
| 784 | dex_file_.release(); |
| 785 | } |
| 786 | |
| 787 | void Redefiner::ReleaseAllDexFiles() { |
| 788 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 789 | redef.ReleaseDexFile(); |
| 790 | } |
| 791 | } |
| 792 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 793 | jvmtiError Redefiner::Run() { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 794 | art::StackHandleScope<1> hs(self_); |
| 795 | // Allocate an array to hold onto all java temporary objects associated with this redefinition. |
| 796 | // We will let this be collected after the end of this function. |
| 797 | RedefinitionDataHolder holder(&hs, runtime_, self_, redefinitions_.size()); |
| 798 | if (holder.IsNull()) { |
| 799 | self_->AssertPendingOOMException(); |
| 800 | self_->ClearException(); |
| 801 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate storage for temporaries"); |
| 802 | return result_; |
| 803 | } |
| 804 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 805 | // First we just allocate the ClassExt and its fields that we need. These can be updated |
| 806 | // atomically without any issues (since we allocate the map arrays as empty) so we don't bother |
| 807 | // doing a try loop. The other allocations we need to ensure that nothing has changed in the time |
| 808 | // between allocating them and pausing all threads before we can update them so we need to do a |
| 809 | // try loop. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 810 | if (!CheckAllRedefinitionAreValid() || |
| 811 | !EnsureAllClassAllocationsFinished() || |
| 812 | !FinishAllRemainingAllocations(holder)) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 813 | // TODO Null out the ClassExt fields we allocated (if possible, might be racing with another |
| 814 | // 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] | 815 | // declared_methods_.length) but would be good to get rid of. All other allocations should be |
| 816 | // cleaned up by the GC eventually. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 817 | return result_; |
| 818 | } |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 819 | int32_t counter = 0; |
| 820 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 821 | if (holder.GetSourceClassLoader(counter) == nullptr) { |
| 822 | runtime_->GetClassLinker()->AppendToBootClassPath(self_, redef.GetDexFile()); |
| 823 | } |
| 824 | counter++; |
| 825 | } |
Alex Light | 6abd539 | 2017-01-05 17:53:00 -0800 | [diff] [blame] | 826 | // Disable GC and wait for it to be done if we are a moving GC. This is fine since we are done |
| 827 | // allocating so no deadlocks. |
| 828 | art::gc::Heap* heap = runtime_->GetHeap(); |
| 829 | if (heap->IsGcConcurrentAndMoving()) { |
| 830 | // GC moving objects can cause deadlocks as we are deoptimizing the stack. |
| 831 | heap->IncrementDisableMovingGC(self_); |
| 832 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 833 | // Do transition to final suspension |
| 834 | // TODO We might want to give this its own suspended state! |
| 835 | // TODO This isn't right. We need to change state without any chance of suspend ideally! |
| 836 | self_->TransitionFromRunnableToSuspended(art::ThreadState::kNative); |
| 837 | runtime_->GetThreadList()->SuspendAll( |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 838 | "Final installation of redefined Classes!", /*long_suspend*/true); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 839 | // TODO We need to invalidate all breakpoints in the redefined class with the debugger. |
| 840 | // TODO We need to deal with any instrumentation/debugger deoptimized_methods_. |
| 841 | // TODO We need to update all debugger MethodIDs so they note the method they point to is |
| 842 | // obsolete or implement some other well defined semantics. |
| 843 | // 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] | 844 | counter = 0; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 845 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 846 | art::ScopedAssertNoThreadSuspension nts("Updating runtime objects for redefinition"); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 847 | if (holder.GetSourceClassLoader(counter) != nullptr) { |
| 848 | ClassLoaderHelper::UpdateJavaDexFile(holder.GetJavaDexFile(counter), |
| 849 | holder.GetNewDexFileCookie(counter)); |
| 850 | } |
| 851 | art::mirror::Class* klass = holder.GetMirrorClass(counter); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 852 | // TODO Rewrite so we don't do a stack walk for each and every class. |
| 853 | redef.FindAndAllocateObsoleteMethods(klass); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 854 | redef.UpdateClass(klass, holder.GetNewDexCache(counter), |
| 855 | holder.GetOriginalDexFileBytes(counter)); |
| 856 | counter++; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 857 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 858 | // TODO Verify the new Class. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 859 | // TODO Shrink the obsolete method maps if possible? |
| 860 | // TODO find appropriate class loader. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 861 | // TODO Put this into a scoped thing. |
| 862 | runtime_->GetThreadList()->ResumeAll(); |
| 863 | // Get back shared mutator lock as expected for return. |
| 864 | self_->TransitionFromSuspendedToRunnable(); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 865 | // TODO Do the dex_file release at a more reasonable place. This works but it muddles who really |
| 866 | // owns the DexFile and when ownership is transferred. |
| 867 | ReleaseAllDexFiles(); |
Alex Light | 6abd539 | 2017-01-05 17:53:00 -0800 | [diff] [blame] | 868 | if (heap->IsGcConcurrentAndMoving()) { |
| 869 | heap->DecrementDisableMovingGC(self_); |
| 870 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 871 | return OK; |
| 872 | } |
| 873 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 874 | void Redefiner::ClassRedefinition::UpdateMethods(art::ObjPtr<art::mirror::Class> mclass, |
| 875 | art::ObjPtr<art::mirror::DexCache> new_dex_cache, |
| 876 | const art::DexFile::ClassDef& class_def) { |
| 877 | art::ClassLinker* linker = driver_->runtime_->GetClassLinker(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 878 | art::PointerSize image_pointer_size = linker->GetImagePointerSize(); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 879 | 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] | 880 | const art::DexFile& old_dex_file = mclass->GetDexFile(); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 881 | // Update methods. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 882 | for (art::ArtMethod& method : mclass->GetMethods(image_pointer_size)) { |
| 883 | const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(method.GetName()); |
| 884 | art::dex::TypeIndex method_return_idx = |
| 885 | dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(method.GetReturnTypeDescriptor())); |
| 886 | const auto* old_type_list = method.GetParameterTypeList(); |
| 887 | std::vector<art::dex::TypeIndex> new_type_list; |
| 888 | for (uint32_t i = 0; old_type_list != nullptr && i < old_type_list->Size(); i++) { |
| 889 | new_type_list.push_back( |
| 890 | dex_file_->GetIndexForTypeId( |
| 891 | *dex_file_->FindTypeId( |
| 892 | old_dex_file.GetTypeDescriptor( |
| 893 | old_dex_file.GetTypeId( |
| 894 | old_type_list->GetTypeItem(i).type_idx_))))); |
| 895 | } |
| 896 | const art::DexFile::ProtoId* proto_id = dex_file_->FindProtoId(method_return_idx, |
| 897 | new_type_list); |
Nicolas Geoffray | f6abcda | 2016-12-21 09:26:18 +0000 | [diff] [blame] | 898 | // TODO Return false, cleanup. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 899 | CHECK(proto_id != nullptr || old_type_list == nullptr); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 900 | const art::DexFile::MethodId* method_id = dex_file_->FindMethodId(declaring_class_id, |
| 901 | *new_name_id, |
| 902 | *proto_id); |
Nicolas Geoffray | f6abcda | 2016-12-21 09:26:18 +0000 | [diff] [blame] | 903 | // TODO Return false, cleanup. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 904 | CHECK(method_id != nullptr); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 905 | uint32_t dex_method_idx = dex_file_->GetIndexForMethodId(*method_id); |
| 906 | method.SetDexMethodIndex(dex_method_idx); |
| 907 | linker->SetEntryPointsToInterpreter(&method); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 908 | method.SetCodeItemOffset(dex_file_->FindCodeItemOffset(class_def, dex_method_idx)); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 909 | method.SetDexCacheResolvedMethods(new_dex_cache->GetResolvedMethods(), image_pointer_size); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 910 | // Notify the jit that this method is redefined. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 911 | art::jit::Jit* jit = driver_->runtime_->GetJit(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 912 | if (jit != nullptr) { |
| 913 | jit->GetCodeCache()->NotifyMethodRedefined(&method); |
| 914 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 915 | } |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 916 | } |
| 917 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 918 | void Redefiner::ClassRedefinition::UpdateFields(art::ObjPtr<art::mirror::Class> mclass) { |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 919 | // TODO The IFields & SFields pointers should be combined like the methods_ arrays were. |
| 920 | for (auto fields_iter : {mclass->GetIFields(), mclass->GetSFields()}) { |
| 921 | for (art::ArtField& field : fields_iter) { |
| 922 | std::string declaring_class_name; |
| 923 | const art::DexFile::TypeId* new_declaring_id = |
| 924 | dex_file_->FindTypeId(field.GetDeclaringClass()->GetDescriptor(&declaring_class_name)); |
| 925 | const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(field.GetName()); |
| 926 | const art::DexFile::TypeId* new_type_id = dex_file_->FindTypeId(field.GetTypeDescriptor()); |
| 927 | // TODO Handle error, cleanup. |
| 928 | CHECK(new_name_id != nullptr && new_type_id != nullptr && new_declaring_id != nullptr); |
| 929 | const art::DexFile::FieldId* new_field_id = |
| 930 | dex_file_->FindFieldId(*new_declaring_id, *new_name_id, *new_type_id); |
| 931 | CHECK(new_field_id != nullptr); |
| 932 | // We only need to update the index since the other data in the ArtField cannot be updated. |
| 933 | field.SetDexFieldIndex(dex_file_->GetIndexForFieldId(*new_field_id)); |
| 934 | } |
| 935 | } |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | // Performs updates to class that will allow us to verify it. |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 939 | void Redefiner::ClassRedefinition::UpdateClass( |
| 940 | art::ObjPtr<art::mirror::Class> mclass, |
| 941 | art::ObjPtr<art::mirror::DexCache> new_dex_cache, |
| 942 | art::ObjPtr<art::mirror::ByteArray> original_dex_file) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 943 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
| 944 | const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(0); |
| 945 | UpdateMethods(mclass, new_dex_cache, class_def); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 946 | UpdateFields(mclass); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 947 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 948 | // Update the class fields. |
| 949 | // Need to update class last since the ArtMethod gets its DexFile from the class (which is needed |
| 950 | // to call GetReturnTypeDescriptor and GetParameterTypeList above). |
| 951 | mclass->SetDexCache(new_dex_cache.Ptr()); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 952 | mclass->SetDexClassDefIndex(dex_file_->GetIndexForClassDef(class_def)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 953 | mclass->SetDexTypeIndex(dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(class_sig_.c_str()))); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 954 | art::ObjPtr<art::mirror::ClassExt> ext(mclass->GetExtData()); |
| 955 | CHECK(!ext.IsNull()); |
| 956 | ext->SetOriginalDexFileBytes(original_dex_file); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 959 | // This function does all (java) allocations we need to do for the Class being redefined. |
| 960 | // TODO Change this name maybe? |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 961 | bool Redefiner::ClassRedefinition::EnsureClassAllocationsFinished() { |
| 962 | art::StackHandleScope<2> hs(driver_->self_); |
| 963 | art::Handle<art::mirror::Class> klass(hs.NewHandle( |
| 964 | driver_->self_->DecodeJObject(klass_)->AsClass())); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 965 | if (klass.Get() == nullptr) { |
| 966 | RecordFailure(ERR(INVALID_CLASS), "Unable to decode class argument!"); |
| 967 | return false; |
| 968 | } |
| 969 | // Allocate the classExt |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 970 | art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(driver_->self_))); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 971 | if (ext.Get() == nullptr) { |
| 972 | // No memory. Clear exception (it's not useful) and return error. |
| 973 | // TODO This doesn't need to be fatal. We could just not support obsolete methods after hitting |
| 974 | // this case. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 975 | driver_->self_->AssertPendingOOMException(); |
| 976 | driver_->self_->ClearException(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 977 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate ClassExt"); |
| 978 | return false; |
| 979 | } |
| 980 | // Allocate the 2 arrays that make up the obsolete methods map. Since the contents of the arrays |
| 981 | // are only modified when all threads (other than the modifying one) are suspended we don't need |
| 982 | // to worry about missing the unsyncronized writes to the array. We do synchronize when setting it |
| 983 | // however, since that can happen at any time. |
| 984 | // TODO Clear these after we walk the stacks in order to free them in the (likely?) event there |
| 985 | // are no obsolete methods. |
| 986 | { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 987 | art::ObjectLock<art::mirror::ClassExt> lock(driver_->self_, ext); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 988 | if (!ext->ExtendObsoleteArrays( |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 989 | driver_->self_, klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size())) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 990 | // OOM. Clear exception and return error. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 991 | driver_->self_->AssertPendingOOMException(); |
| 992 | driver_->self_->ClearException(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 993 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate/extend obsolete methods map"); |
| 994 | return false; |
| 995 | } |
| 996 | } |
| 997 | return true; |
| 998 | } |
| 999 | |
| 1000 | } // namespace openjdkjvmti |