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