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