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" |
| 39 | #include "base/logging.h" |
| 40 | #include "events-inl.h" |
| 41 | #include "gc/allocation_listener.h" |
| 42 | #include "instrumentation.h" |
| 43 | #include "jni_env_ext-inl.h" |
| 44 | #include "jvmti_allocator.h" |
| 45 | #include "mirror/class.h" |
| 46 | #include "mirror/class_ext.h" |
| 47 | #include "mirror/object.h" |
| 48 | #include "object_lock.h" |
| 49 | #include "runtime.h" |
| 50 | #include "ScopedLocalRef.h" |
| 51 | |
| 52 | namespace openjdkjvmti { |
| 53 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame^] | 54 | using android::base::StringPrintf; |
| 55 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 56 | // Moves dex data to an anonymous, read-only mmap'd region. |
| 57 | std::unique_ptr<art::MemMap> Redefiner::MoveDataToMemMap(const std::string& original_location, |
| 58 | jint data_len, |
| 59 | unsigned char* dex_data, |
| 60 | std::string* error_msg) { |
| 61 | std::unique_ptr<art::MemMap> map(art::MemMap::MapAnonymous( |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame^] | 62 | StringPrintf("%s-transformed", original_location.c_str()).c_str(), |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 63 | nullptr, |
| 64 | data_len, |
| 65 | PROT_READ|PROT_WRITE, |
| 66 | /*low_4gb*/false, |
| 67 | /*reuse*/false, |
| 68 | error_msg)); |
| 69 | if (map == nullptr) { |
| 70 | return map; |
| 71 | } |
| 72 | memcpy(map->Begin(), dex_data, data_len); |
Alex Light | 0b77257 | 2016-12-02 17:27:31 -0800 | [diff] [blame] | 73 | // Make the dex files mmap read only. This matches how other DexFiles are mmaped and prevents |
| 74 | // programs from corrupting it. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 75 | map->Protect(PROT_READ); |
| 76 | return map; |
| 77 | } |
| 78 | |
| 79 | jvmtiError Redefiner::RedefineClass(ArtJvmTiEnv* env, |
| 80 | art::Runtime* runtime, |
| 81 | art::Thread* self, |
| 82 | jclass klass, |
| 83 | const std::string& original_dex_location, |
| 84 | jint data_len, |
| 85 | unsigned char* dex_data, |
| 86 | std::string* error_msg) { |
| 87 | std::unique_ptr<art::MemMap> map(MoveDataToMemMap(original_dex_location, |
| 88 | data_len, |
| 89 | dex_data, |
| 90 | error_msg)); |
| 91 | std::ostringstream os; |
| 92 | char* generic_ptr_unused = nullptr; |
| 93 | char* signature_ptr = nullptr; |
| 94 | if (env->GetClassSignature(klass, &signature_ptr, &generic_ptr_unused) != OK) { |
| 95 | signature_ptr = const_cast<char*>("<UNKNOWN CLASS>"); |
| 96 | } |
| 97 | if (map.get() == nullptr) { |
| 98 | os << "Failed to create anonymous mmap for modified dex file of class " << signature_ptr |
| 99 | << "in dex file " << original_dex_location << " because: " << *error_msg; |
| 100 | *error_msg = os.str(); |
| 101 | return ERR(OUT_OF_MEMORY); |
| 102 | } |
| 103 | if (map->Size() < sizeof(art::DexFile::Header)) { |
| 104 | *error_msg = "Could not read dex file header because dex_data was too short"; |
| 105 | return ERR(INVALID_CLASS_FORMAT); |
| 106 | } |
| 107 | uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map->Begin())->checksum_; |
| 108 | std::unique_ptr<const art::DexFile> dex_file(art::DexFile::Open(map->GetName(), |
| 109 | checksum, |
| 110 | std::move(map), |
| 111 | /*verify*/true, |
| 112 | /*verify_checksum*/true, |
| 113 | error_msg)); |
| 114 | if (dex_file.get() == nullptr) { |
| 115 | os << "Unable to load modified dex file for " << signature_ptr << ": " << *error_msg; |
| 116 | *error_msg = os.str(); |
| 117 | return ERR(INVALID_CLASS_FORMAT); |
| 118 | } |
| 119 | // Get shared mutator lock. |
| 120 | art::ScopedObjectAccess soa(self); |
| 121 | art::StackHandleScope<1> hs(self); |
| 122 | Redefiner r(runtime, self, klass, signature_ptr, dex_file, error_msg); |
| 123 | // Lock around this class to avoid races. |
| 124 | art::ObjectLock<art::mirror::Class> lock(self, hs.NewHandle(r.GetMirrorClass())); |
| 125 | return r.Run(); |
| 126 | } |
| 127 | |
| 128 | // TODO *MAJOR* This should return the actual source java.lang.DexFile object for the klass. |
| 129 | // TODO Make mirror of DexFile and associated types to make this less hellish. |
| 130 | // TODO Make mirror of BaseDexClassLoader and associated types to make this less hellish. |
| 131 | art::mirror::Object* Redefiner::FindSourceDexFileObject( |
| 132 | art::Handle<art::mirror::ClassLoader> loader) { |
| 133 | const char* dex_path_list_element_array_name = "[Ldalvik/system/DexPathList$Element;"; |
| 134 | const char* dex_path_list_element_name = "Ldalvik/system/DexPathList$Element;"; |
| 135 | const char* dex_file_name = "Ldalvik/system/DexFile;"; |
| 136 | const char* dex_path_list_name = "Ldalvik/system/DexPathList;"; |
| 137 | const char* dex_class_loader_name = "Ldalvik/system/BaseDexClassLoader;"; |
| 138 | |
| 139 | CHECK(!self_->IsExceptionPending()); |
| 140 | art::StackHandleScope<11> hs(self_); |
| 141 | art::ClassLinker* class_linker = runtime_->GetClassLinker(); |
| 142 | |
| 143 | art::Handle<art::mirror::ClassLoader> null_loader(hs.NewHandle<art::mirror::ClassLoader>( |
| 144 | nullptr)); |
| 145 | art::Handle<art::mirror::Class> base_dex_loader_class(hs.NewHandle(class_linker->FindClass( |
| 146 | self_, dex_class_loader_name, null_loader))); |
| 147 | |
| 148 | // Get all the ArtFields so we can look in the BaseDexClassLoader |
| 149 | art::ArtField* path_list_field = base_dex_loader_class->FindDeclaredInstanceField( |
| 150 | "pathList", dex_path_list_name); |
| 151 | CHECK(path_list_field != nullptr); |
| 152 | |
| 153 | art::ArtField* dex_path_list_element_field = |
| 154 | class_linker->FindClass(self_, dex_path_list_name, null_loader) |
| 155 | ->FindDeclaredInstanceField("dexElements", dex_path_list_element_array_name); |
| 156 | CHECK(dex_path_list_element_field != nullptr); |
| 157 | |
| 158 | art::ArtField* element_dex_file_field = |
| 159 | class_linker->FindClass(self_, dex_path_list_element_name, null_loader) |
| 160 | ->FindDeclaredInstanceField("dexFile", dex_file_name); |
| 161 | CHECK(element_dex_file_field != nullptr); |
| 162 | |
| 163 | // Check if loader is a BaseDexClassLoader |
| 164 | art::Handle<art::mirror::Class> loader_class(hs.NewHandle(loader->GetClass())); |
| 165 | if (!loader_class->IsSubClass(base_dex_loader_class.Get())) { |
| 166 | LOG(ERROR) << "The classloader is not a BaseDexClassLoader which is currently the only " |
| 167 | << "supported class loader type!"; |
| 168 | return nullptr; |
| 169 | } |
| 170 | // Start navigating the fields of the loader (now known to be a BaseDexClassLoader derivative) |
| 171 | art::Handle<art::mirror::Object> path_list( |
| 172 | hs.NewHandle(path_list_field->GetObject(loader.Get()))); |
| 173 | CHECK(path_list.Get() != nullptr); |
| 174 | CHECK(!self_->IsExceptionPending()); |
| 175 | art::Handle<art::mirror::ObjectArray<art::mirror::Object>> dex_elements_list(hs.NewHandle( |
| 176 | dex_path_list_element_field->GetObject(path_list.Get())-> |
| 177 | AsObjectArray<art::mirror::Object>())); |
| 178 | CHECK(!self_->IsExceptionPending()); |
| 179 | CHECK(dex_elements_list.Get() != nullptr); |
| 180 | size_t num_elements = dex_elements_list->GetLength(); |
| 181 | art::MutableHandle<art::mirror::Object> current_element( |
| 182 | hs.NewHandle<art::mirror::Object>(nullptr)); |
| 183 | art::MutableHandle<art::mirror::Object> first_dex_file( |
| 184 | hs.NewHandle<art::mirror::Object>(nullptr)); |
| 185 | // Iterate over the DexPathList$Element to find the right one |
| 186 | // TODO Or not ATM just return the first one. |
| 187 | for (size_t i = 0; i < num_elements; i++) { |
| 188 | current_element.Assign(dex_elements_list->Get(i)); |
| 189 | CHECK(current_element.Get() != nullptr); |
| 190 | CHECK(!self_->IsExceptionPending()); |
| 191 | CHECK(dex_elements_list.Get() != nullptr); |
| 192 | CHECK_EQ(current_element->GetClass(), class_linker->FindClass(self_, |
| 193 | dex_path_list_element_name, |
| 194 | null_loader)); |
| 195 | // TODO It would be cleaner to put the art::DexFile into the dalvik.system.DexFile the class |
| 196 | // comes from but it is more annoying because we would need to find this class. It is not |
| 197 | // necessary for proper function since we just need to be in front of the classes old dex file |
| 198 | // in the path. |
| 199 | first_dex_file.Assign(element_dex_file_field->GetObject(current_element.Get())); |
| 200 | if (first_dex_file.Get() != nullptr) { |
| 201 | return first_dex_file.Get(); |
| 202 | } |
| 203 | } |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
| 207 | art::mirror::Class* Redefiner::GetMirrorClass() { |
| 208 | return self_->DecodeJObject(klass_)->AsClass(); |
| 209 | } |
| 210 | |
| 211 | art::mirror::ClassLoader* Redefiner::GetClassLoader() { |
| 212 | return GetMirrorClass()->GetClassLoader(); |
| 213 | } |
| 214 | |
| 215 | art::mirror::DexCache* Redefiner::CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader) { |
| 216 | return runtime_->GetClassLinker()->RegisterDexFile(*dex_file_, loader.Get()); |
| 217 | } |
| 218 | |
| 219 | // TODO Really wishing I had that mirror of java.lang.DexFile now. |
| 220 | art::mirror::LongArray* Redefiner::AllocateDexFileCookie( |
| 221 | art::Handle<art::mirror::Object> java_dex_file_obj) { |
| 222 | art::StackHandleScope<2> hs(self_); |
| 223 | // mCookie is nulled out if the DexFile has been closed but mInternalCookie sticks around until |
| 224 | // the object is finalized. Since they always point to the same array if mCookie is not null we |
| 225 | // just use the mInternalCookie field. We will update one or both of these fields later. |
| 226 | // TODO Should I get the class from the classloader or directly? |
| 227 | art::ArtField* internal_cookie_field = java_dex_file_obj->GetClass()->FindDeclaredInstanceField( |
| 228 | "mInternalCookie", "Ljava/lang/Object;"); |
| 229 | // TODO Add check that mCookie is either null or same as mInternalCookie |
| 230 | CHECK(internal_cookie_field != nullptr); |
| 231 | art::Handle<art::mirror::LongArray> cookie( |
| 232 | hs.NewHandle(internal_cookie_field->GetObject(java_dex_file_obj.Get())->AsLongArray())); |
| 233 | // TODO Maybe make these non-fatal. |
| 234 | CHECK(cookie.Get() != nullptr); |
| 235 | CHECK_GE(cookie->GetLength(), 1); |
| 236 | art::Handle<art::mirror::LongArray> new_cookie( |
| 237 | hs.NewHandle(art::mirror::LongArray::Alloc(self_, cookie->GetLength() + 1))); |
| 238 | if (new_cookie.Get() == nullptr) { |
| 239 | self_->AssertPendingOOMException(); |
| 240 | return nullptr; |
| 241 | } |
| 242 | // Copy the oat-dex field at the start. |
| 243 | // TODO Should I clear this field? |
| 244 | // TODO This is a really crappy thing here with the first element being different. |
| 245 | new_cookie->SetWithoutChecks<false>(0, cookie->GetWithoutChecks(0)); |
| 246 | new_cookie->SetWithoutChecks<false>( |
| 247 | 1, static_cast<int64_t>(reinterpret_cast<intptr_t>(dex_file_.get()))); |
| 248 | new_cookie->Memcpy(2, cookie.Get(), 1, cookie->GetLength() - 1); |
| 249 | return new_cookie.Get(); |
| 250 | } |
| 251 | |
| 252 | void Redefiner::RecordFailure(jvmtiError result, const std::string& error_msg) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame^] | 253 | *error_msg_ = StringPrintf("Unable to perform redefinition of '%s': %s", |
| 254 | class_sig_, |
| 255 | error_msg.c_str()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 256 | result_ = result; |
| 257 | } |
| 258 | |
| 259 | bool Redefiner::FinishRemainingAllocations( |
| 260 | /*out*/art::MutableHandle<art::mirror::ClassLoader>* source_class_loader, |
| 261 | /*out*/art::MutableHandle<art::mirror::Object>* java_dex_file_obj, |
| 262 | /*out*/art::MutableHandle<art::mirror::LongArray>* new_dex_file_cookie, |
| 263 | /*out*/art::MutableHandle<art::mirror::DexCache>* new_dex_cache) { |
| 264 | art::StackHandleScope<4> hs(self_); |
| 265 | // This shouldn't allocate |
| 266 | art::Handle<art::mirror::ClassLoader> loader(hs.NewHandle(GetClassLoader())); |
| 267 | if (loader.Get() == nullptr) { |
| 268 | // TODO Better error msg. |
| 269 | RecordFailure(ERR(INTERNAL), "Unable to find class loader!"); |
| 270 | return false; |
| 271 | } |
| 272 | art::Handle<art::mirror::Object> dex_file_obj(hs.NewHandle(FindSourceDexFileObject(loader))); |
| 273 | if (dex_file_obj.Get() == nullptr) { |
| 274 | // TODO Better error msg. |
| 275 | RecordFailure(ERR(INTERNAL), "Unable to find class loader!"); |
| 276 | return false; |
| 277 | } |
| 278 | art::Handle<art::mirror::LongArray> new_cookie(hs.NewHandle(AllocateDexFileCookie(dex_file_obj))); |
| 279 | if (new_cookie.Get() == nullptr) { |
| 280 | self_->AssertPendingOOMException(); |
| 281 | self_->ClearException(); |
| 282 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate dex file array for class loader"); |
| 283 | return false; |
| 284 | } |
| 285 | art::Handle<art::mirror::DexCache> dex_cache(hs.NewHandle(CreateNewDexCache(loader))); |
| 286 | if (dex_cache.Get() == nullptr) { |
| 287 | self_->AssertPendingOOMException(); |
| 288 | self_->ClearException(); |
| 289 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate DexCache"); |
| 290 | return false; |
| 291 | } |
| 292 | source_class_loader->Assign(loader.Get()); |
| 293 | java_dex_file_obj->Assign(dex_file_obj.Get()); |
| 294 | new_dex_file_cookie->Assign(new_cookie.Get()); |
| 295 | new_dex_cache->Assign(dex_cache.Get()); |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | jvmtiError Redefiner::Run() { |
| 300 | art::StackHandleScope<5> hs(self_); |
| 301 | // TODO We might want to have a global lock (or one based on the class being redefined at least) |
| 302 | // in order to make cleanup easier. Not a huge deal though. |
| 303 | // |
| 304 | // First we just allocate the ClassExt and its fields that we need. These can be updated |
| 305 | // atomically without any issues (since we allocate the map arrays as empty) so we don't bother |
| 306 | // doing a try loop. The other allocations we need to ensure that nothing has changed in the time |
| 307 | // between allocating them and pausing all threads before we can update them so we need to do a |
| 308 | // try loop. |
| 309 | if (!EnsureRedefinitionIsValid() || !EnsureClassAllocationsFinished()) { |
| 310 | return result_; |
| 311 | } |
| 312 | art::MutableHandle<art::mirror::ClassLoader> source_class_loader( |
| 313 | hs.NewHandle<art::mirror::ClassLoader>(nullptr)); |
| 314 | art::MutableHandle<art::mirror::Object> java_dex_file( |
| 315 | hs.NewHandle<art::mirror::Object>(nullptr)); |
| 316 | art::MutableHandle<art::mirror::LongArray> new_dex_file_cookie( |
| 317 | hs.NewHandle<art::mirror::LongArray>(nullptr)); |
| 318 | art::MutableHandle<art::mirror::DexCache> new_dex_cache( |
| 319 | hs.NewHandle<art::mirror::DexCache>(nullptr)); |
| 320 | if (!FinishRemainingAllocations(&source_class_loader, |
| 321 | &java_dex_file, |
| 322 | &new_dex_file_cookie, |
| 323 | &new_dex_cache)) { |
| 324 | // TODO Null out the ClassExt fields we allocated (if possible, might be racing with another |
| 325 | // redefineclass call which made it even bigger. Leak shouldn't be huge (2x array of size |
| 326 | // declared_methods_.length) but would be good to get rid of. |
| 327 | // new_dex_file_cookie & new_dex_cache should be cleaned up by the GC. |
| 328 | return result_; |
| 329 | } |
| 330 | // Get the mirror class now that we aren't allocating anymore. |
| 331 | art::Handle<art::mirror::Class> art_class(hs.NewHandle(GetMirrorClass())); |
| 332 | // Enable assertion that this thread isn't interrupted during this installation. |
| 333 | // After this we will need to do real cleanup in case of failure. Prior to this we could simply |
| 334 | // return and would let everything get cleaned up or harmlessly leaked. |
| 335 | // Do transition to final suspension |
| 336 | // TODO We might want to give this its own suspended state! |
| 337 | // TODO This isn't right. We need to change state without any chance of suspend ideally! |
| 338 | self_->TransitionFromRunnableToSuspended(art::ThreadState::kNative); |
| 339 | runtime_->GetThreadList()->SuspendAll( |
| 340 | "Final installation of redefined Class!", /*long_suspend*/true); |
| 341 | // TODO Might want to move this into a different type. |
| 342 | // Now we reach the part where we must do active cleanup if something fails. |
| 343 | // TODO We should really Retry if this fails instead of simply aborting. |
| 344 | // Set the new DexFileCookie returns the original so we can fix it back up if redefinition fails |
| 345 | art::ObjPtr<art::mirror::LongArray> original_dex_file_cookie(nullptr); |
| 346 | if (!UpdateJavaDexFile(java_dex_file.Get(), |
| 347 | new_dex_file_cookie.Get(), |
Alex Light | b81a984 | 2016-12-15 00:59:05 +0000 | [diff] [blame] | 348 | &original_dex_file_cookie)) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 349 | // Release suspendAll |
| 350 | runtime_->GetThreadList()->ResumeAll(); |
| 351 | // Get back shared mutator lock as expected for return. |
| 352 | self_->TransitionFromSuspendedToRunnable(); |
| 353 | return result_; |
| 354 | } |
| 355 | if (!UpdateClass(art_class.Get(), new_dex_cache.Get())) { |
| 356 | // TODO Should have some form of scope to do this. |
| 357 | RestoreJavaDexFile(java_dex_file.Get(), original_dex_file_cookie); |
| 358 | // Release suspendAll |
| 359 | runtime_->GetThreadList()->ResumeAll(); |
| 360 | // Get back shared mutator lock as expected for return. |
| 361 | self_->TransitionFromSuspendedToRunnable(); |
| 362 | return result_; |
| 363 | } |
Alex Light | b81a984 | 2016-12-15 00:59:05 +0000 | [diff] [blame] | 364 | // Update the ClassObjects Keep the old DexCache (and other stuff) around so we can restore |
| 365 | // functions/fields. |
| 366 | // Verify the new Class. |
| 367 | // Failure then undo updates to class |
| 368 | // Do stack walks and allocate obsolete methods |
| 369 | // Shrink the obsolete method maps if possible? |
| 370 | // TODO find appropriate class loader. Allocate new dex files array. Pause all java treads. |
| 371 | // Replace dex files array. Do stack scan + allocate obsoletes. Remove array if possible. |
| 372 | // TODO We might want to ensure that all threads are stopped for this! |
| 373 | // AddDexToClassPath(); |
| 374 | // TODO |
| 375 | // Release suspendAll |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 376 | // TODO Put this into a scoped thing. |
| 377 | runtime_->GetThreadList()->ResumeAll(); |
| 378 | // Get back shared mutator lock as expected for return. |
| 379 | self_->TransitionFromSuspendedToRunnable(); |
| 380 | // TODO Do this at a more reasonable place. |
| 381 | dex_file_.release(); |
| 382 | return OK; |
| 383 | } |
| 384 | |
| 385 | void Redefiner::RestoreJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file, |
| 386 | art::ObjPtr<art::mirror::LongArray> orig_cookie) { |
| 387 | art::ArtField* internal_cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField( |
| 388 | "mInternalCookie", "Ljava/lang/Object;"); |
| 389 | art::ArtField* cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField( |
| 390 | "mCookie", "Ljava/lang/Object;"); |
| 391 | art::ObjPtr<art::mirror::LongArray> new_cookie( |
| 392 | cookie_field->GetObject(java_dex_file)->AsLongArray()); |
| 393 | internal_cookie_field->SetObject<false>(java_dex_file, orig_cookie); |
| 394 | if (!new_cookie.IsNull()) { |
| 395 | cookie_field->SetObject<false>(java_dex_file, orig_cookie); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Performs updates to class that will allow us to verify it. |
| 400 | bool Redefiner::UpdateClass(art::ObjPtr<art::mirror::Class> mclass, |
| 401 | art::ObjPtr<art::mirror::DexCache> new_dex_cache) { |
| 402 | art::ClassLinker* linker = runtime_->GetClassLinker(); |
| 403 | art::PointerSize image_pointer_size = linker->GetImagePointerSize(); |
| 404 | const art::DexFile::ClassDef* class_def = art::OatFile::OatDexFile::FindClassDef( |
| 405 | *dex_file_, class_sig_, art::ComputeModifiedUtf8Hash(class_sig_)); |
| 406 | if (class_def == nullptr) { |
| 407 | RecordFailure(ERR(INVALID_CLASS_FORMAT), "Unable to find ClassDef!"); |
| 408 | return false; |
| 409 | } |
| 410 | const art::DexFile::TypeId& declaring_class_id = dex_file_->GetTypeId(class_def->class_idx_); |
| 411 | const art::DexFile& old_dex_file = mclass->GetDexFile(); |
| 412 | for (art::ArtMethod& method : mclass->GetMethods(image_pointer_size)) { |
| 413 | const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(method.GetName()); |
| 414 | art::dex::TypeIndex method_return_idx = |
| 415 | dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(method.GetReturnTypeDescriptor())); |
| 416 | const auto* old_type_list = method.GetParameterTypeList(); |
| 417 | std::vector<art::dex::TypeIndex> new_type_list; |
| 418 | for (uint32_t i = 0; old_type_list != nullptr && i < old_type_list->Size(); i++) { |
| 419 | new_type_list.push_back( |
| 420 | dex_file_->GetIndexForTypeId( |
| 421 | *dex_file_->FindTypeId( |
| 422 | old_dex_file.GetTypeDescriptor( |
| 423 | old_dex_file.GetTypeId( |
| 424 | old_type_list->GetTypeItem(i).type_idx_))))); |
| 425 | } |
| 426 | const art::DexFile::ProtoId* proto_id = dex_file_->FindProtoId(method_return_idx, |
| 427 | new_type_list); |
Alex Light | d8936da | 2016-11-28 16:24:32 -0800 | [diff] [blame] | 428 | CHECK(proto_id != nullptr || old_type_list == nullptr); |
Alex Light | b81a984 | 2016-12-15 00:59:05 +0000 | [diff] [blame] | 429 | // TODO Return false, cleanup. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 430 | const art::DexFile::MethodId* method_id = dex_file_->FindMethodId(declaring_class_id, |
| 431 | *new_name_id, |
| 432 | *proto_id); |
Alex Light | d8936da | 2016-11-28 16:24:32 -0800 | [diff] [blame] | 433 | CHECK(method_id != nullptr); |
Alex Light | b81a984 | 2016-12-15 00:59:05 +0000 | [diff] [blame] | 434 | // TODO Return false, cleanup. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 435 | uint32_t dex_method_idx = dex_file_->GetIndexForMethodId(*method_id); |
| 436 | method.SetDexMethodIndex(dex_method_idx); |
| 437 | linker->SetEntryPointsToInterpreter(&method); |
| 438 | method.SetCodeItemOffset(dex_file_->FindCodeItemOffset(*class_def, dex_method_idx)); |
| 439 | method.SetDexCacheResolvedMethods(new_dex_cache->GetResolvedMethods(), image_pointer_size); |
| 440 | method.SetDexCacheResolvedTypes(new_dex_cache->GetResolvedTypes(), image_pointer_size); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 441 | } |
| 442 | // Update the class fields. |
| 443 | // Need to update class last since the ArtMethod gets its DexFile from the class (which is needed |
| 444 | // to call GetReturnTypeDescriptor and GetParameterTypeList above). |
| 445 | mclass->SetDexCache(new_dex_cache.Ptr()); |
| 446 | mclass->SetDexCacheStrings(new_dex_cache->GetStrings()); |
| 447 | mclass->SetDexClassDefIndex(dex_file_->GetIndexForClassDef(*class_def)); |
| 448 | mclass->SetDexTypeIndex(dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(class_sig_))); |
| 449 | return true; |
| 450 | } |
| 451 | |
| 452 | bool Redefiner::UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file, |
| 453 | art::ObjPtr<art::mirror::LongArray> new_cookie, |
| 454 | /*out*/art::ObjPtr<art::mirror::LongArray>* original_cookie) { |
| 455 | art::ArtField* internal_cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField( |
| 456 | "mInternalCookie", "Ljava/lang/Object;"); |
| 457 | art::ArtField* cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField( |
| 458 | "mCookie", "Ljava/lang/Object;"); |
| 459 | CHECK(internal_cookie_field != nullptr); |
| 460 | art::ObjPtr<art::mirror::LongArray> orig_internal_cookie( |
| 461 | internal_cookie_field->GetObject(java_dex_file)->AsLongArray()); |
| 462 | art::ObjPtr<art::mirror::LongArray> orig_cookie( |
| 463 | cookie_field->GetObject(java_dex_file)->AsLongArray()); |
| 464 | internal_cookie_field->SetObject<false>(java_dex_file, new_cookie); |
| 465 | *original_cookie = orig_internal_cookie; |
| 466 | if (!orig_cookie.IsNull()) { |
| 467 | cookie_field->SetObject<false>(java_dex_file, new_cookie); |
| 468 | } |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | // This function does all (java) allocations we need to do for the Class being redefined. |
| 473 | // TODO Change this name maybe? |
| 474 | bool Redefiner::EnsureClassAllocationsFinished() { |
| 475 | art::StackHandleScope<2> hs(self_); |
| 476 | art::Handle<art::mirror::Class> klass(hs.NewHandle(self_->DecodeJObject(klass_)->AsClass())); |
| 477 | if (klass.Get() == nullptr) { |
| 478 | RecordFailure(ERR(INVALID_CLASS), "Unable to decode class argument!"); |
| 479 | return false; |
| 480 | } |
| 481 | // Allocate the classExt |
| 482 | art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(self_))); |
| 483 | if (ext.Get() == nullptr) { |
| 484 | // No memory. Clear exception (it's not useful) and return error. |
| 485 | // TODO This doesn't need to be fatal. We could just not support obsolete methods after hitting |
| 486 | // this case. |
| 487 | self_->AssertPendingOOMException(); |
| 488 | self_->ClearException(); |
| 489 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate ClassExt"); |
| 490 | return false; |
| 491 | } |
| 492 | // Allocate the 2 arrays that make up the obsolete methods map. Since the contents of the arrays |
| 493 | // are only modified when all threads (other than the modifying one) are suspended we don't need |
| 494 | // to worry about missing the unsyncronized writes to the array. We do synchronize when setting it |
| 495 | // however, since that can happen at any time. |
| 496 | // TODO Clear these after we walk the stacks in order to free them in the (likely?) event there |
| 497 | // are no obsolete methods. |
| 498 | { |
| 499 | art::ObjectLock<art::mirror::ClassExt> lock(self_, ext); |
| 500 | if (!ext->ExtendObsoleteArrays( |
| 501 | self_, klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size())) { |
| 502 | // OOM. Clear exception and return error. |
| 503 | self_->AssertPendingOOMException(); |
| 504 | self_->ClearException(); |
| 505 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate/extend obsolete methods map"); |
| 506 | return false; |
| 507 | } |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | } // namespace openjdkjvmti |