blob: c4f16f5e2d64589cccc50077212f30c9a5debea0 [file] [log] [blame]
Alex Lighta01de592016-11-15 10:43:06 -08001/* 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 Gampe46ee31b2016-12-14 10:11:49 -080036#include "android-base/stringprintf.h"
37
Andreas Gampea1d2f952017-04-20 22:53:58 -070038#include "art_field-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080039#include "art_jvmti.h"
Steven Morelande431e272017-07-18 16:53:49 -070040#include "art_method-inl.h"
Vladimir Markoe1993c72017-06-14 17:01:38 +010041#include "base/array_ref.h"
Alex Lighta01de592016-11-15 10:43:06 -080042#include "base/logging.h"
Vladimir Markoba118822017-06-12 15:41:56 +010043#include "base/stringpiece.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080044#include "class_linker-inl.h"
Alex Light5643caf2017-02-08 11:39:07 -080045#include "debugger.h"
Alex Light460d1b42017-01-10 15:37:17 +000046#include "dex_file.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070047#include "dex_file_loader.h"
Alex Light460d1b42017-01-10 15:37:17 +000048#include "dex_file_types.h"
Alex Lighta01de592016-11-15 10:43:06 -080049#include "events-inl.h"
50#include "gc/allocation_listener.h"
Alex Light6abd5392017-01-05 17:53:00 -080051#include "gc/heap.h"
Alex Lighta01de592016-11-15 10:43:06 -080052#include "instrumentation.h"
Alex Light07f06212017-06-01 14:01:43 -070053#include "intern_table.h"
Alex Light5643caf2017-02-08 11:39:07 -080054#include "jdwp/jdwp.h"
55#include "jdwp/jdwp_constants.h"
56#include "jdwp/jdwp_event.h"
57#include "jdwp/object_registry.h"
Alex Lightdba61482016-12-21 08:20:29 -080058#include "jit/jit.h"
59#include "jit/jit_code_cache.h"
Alex Lighta01de592016-11-15 10:43:06 -080060#include "jni_env_ext-inl.h"
61#include "jvmti_allocator.h"
Alex Light6161f132017-01-25 10:30:20 -080062#include "mirror/class-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080063#include "mirror/class_ext.h"
64#include "mirror/object.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070065#include "nativehelper/scoped_local_ref.h"
Alex Lighte77b48b2017-02-22 11:08:06 -080066#include "non_debuggable_classes.h"
Alex Lighta01de592016-11-15 10:43:06 -080067#include "object_lock.h"
68#include "runtime.h"
Alex Lighta26e3492017-06-27 17:55:37 -070069#include "ti_breakpoint.h"
Alex Lighteb98b082017-01-25 13:02:32 -080070#include "ti_class_loader.h"
Alex Light0e692732017-01-10 15:00:05 -080071#include "transform.h"
Alex Light8c889d22017-02-06 13:58:27 -080072#include "verifier/method_verifier.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070073#include "verifier/verifier_enums.h"
Alex Lighta01de592016-11-15 10:43:06 -080074
75namespace openjdkjvmti {
76
Andreas Gampe46ee31b2016-12-14 10:11:49 -080077using android::base::StringPrintf;
78
Alex Lighteee0bd42017-02-14 15:31:45 +000079// A helper that fills in a classes obsolete_methods_ and obsolete_dex_caches_ classExt fields as
80// they are created. This ensures that we can always call any method of an obsolete ArtMethod object
81// almost as soon as they are created since the GetObsoleteDexCache method will succeed.
82class ObsoleteMap {
83 public:
84 art::ArtMethod* FindObsoleteVersion(art::ArtMethod* original)
85 REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) {
86 auto method_pair = id_map_.find(original);
87 if (method_pair != id_map_.end()) {
88 art::ArtMethod* res = obsolete_methods_->GetElementPtrSize<art::ArtMethod*>(
89 method_pair->second, art::kRuntimePointerSize);
90 DCHECK(res != nullptr);
91 DCHECK_EQ(original, res->GetNonObsoleteMethod());
92 return res;
93 } else {
94 return nullptr;
95 }
96 }
97
98 void RecordObsolete(art::ArtMethod* original, art::ArtMethod* obsolete)
99 REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) {
100 DCHECK(original != nullptr);
101 DCHECK(obsolete != nullptr);
102 int32_t slot = next_free_slot_++;
103 DCHECK_LT(slot, obsolete_methods_->GetLength());
104 DCHECK(nullptr ==
105 obsolete_methods_->GetElementPtrSize<art::ArtMethod*>(slot, art::kRuntimePointerSize));
106 DCHECK(nullptr == obsolete_dex_caches_->Get(slot));
107 obsolete_methods_->SetElementPtrSize(slot, obsolete, art::kRuntimePointerSize);
108 obsolete_dex_caches_->Set(slot, original_dex_cache_);
109 id_map_.insert({original, slot});
110 }
111
112 ObsoleteMap(art::ObjPtr<art::mirror::PointerArray> obsolete_methods,
113 art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> obsolete_dex_caches,
114 art::ObjPtr<art::mirror::DexCache> original_dex_cache)
115 : next_free_slot_(0),
116 obsolete_methods_(obsolete_methods),
117 obsolete_dex_caches_(obsolete_dex_caches),
118 original_dex_cache_(original_dex_cache) {
119 // Figure out where the first unused slot in the obsolete_methods_ array is.
120 while (obsolete_methods_->GetElementPtrSize<art::ArtMethod*>(
121 next_free_slot_, art::kRuntimePointerSize) != nullptr) {
122 DCHECK(obsolete_dex_caches_->Get(next_free_slot_) != nullptr);
123 next_free_slot_++;
124 }
125 // Sanity check that the same slot in obsolete_dex_caches_ is free.
126 DCHECK(obsolete_dex_caches_->Get(next_free_slot_) == nullptr);
127 }
128
129 private:
130 int32_t next_free_slot_;
131 std::unordered_map<art::ArtMethod*, int32_t> id_map_;
132 // Pointers to the fields in mirror::ClassExt. These can be held as ObjPtr since this is only used
133 // when we have an exclusive mutator_lock_ (i.e. all threads are suspended).
134 art::ObjPtr<art::mirror::PointerArray> obsolete_methods_;
135 art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> obsolete_dex_caches_;
136 art::ObjPtr<art::mirror::DexCache> original_dex_cache_;
137};
138
Alex Lightdba61482016-12-21 08:20:29 -0800139// This visitor walks thread stacks and allocates and sets up the obsolete methods. It also does
140// some basic sanity checks that the obsolete method is sane.
141class ObsoleteMethodStackVisitor : public art::StackVisitor {
142 protected:
143 ObsoleteMethodStackVisitor(
144 art::Thread* thread,
145 art::LinearAlloc* allocator,
146 const std::unordered_set<art::ArtMethod*>& obsoleted_methods,
Alex Lighteee0bd42017-02-14 15:31:45 +0000147 ObsoleteMap* obsolete_maps)
Alex Lightdba61482016-12-21 08:20:29 -0800148 : StackVisitor(thread,
149 /*context*/nullptr,
150 StackVisitor::StackWalkKind::kIncludeInlinedFrames),
151 allocator_(allocator),
152 obsoleted_methods_(obsoleted_methods),
Alex Light4ba388a2017-01-27 10:26:49 -0800153 obsolete_maps_(obsolete_maps) { }
Alex Lightdba61482016-12-21 08:20:29 -0800154
155 ~ObsoleteMethodStackVisitor() OVERRIDE {}
156
157 public:
158 // Returns true if we successfully installed obsolete methods on this thread, filling
159 // obsolete_maps_ with the translations if needed. Returns false and fills error_msg if we fail.
160 // The stack is cleaned up when we fail.
Alex Light007ada22017-01-10 13:33:56 -0800161 static void UpdateObsoleteFrames(
Alex Lightdba61482016-12-21 08:20:29 -0800162 art::Thread* thread,
163 art::LinearAlloc* allocator,
164 const std::unordered_set<art::ArtMethod*>& obsoleted_methods,
Alex Lighteee0bd42017-02-14 15:31:45 +0000165 ObsoleteMap* obsolete_maps)
Alex Light007ada22017-01-10 13:33:56 -0800166 REQUIRES(art::Locks::mutator_lock_) {
Alex Lightdba61482016-12-21 08:20:29 -0800167 ObsoleteMethodStackVisitor visitor(thread,
168 allocator,
169 obsoleted_methods,
Alex Light007ada22017-01-10 13:33:56 -0800170 obsolete_maps);
Alex Lightdba61482016-12-21 08:20:29 -0800171 visitor.WalkStack();
Alex Lightdba61482016-12-21 08:20:29 -0800172 }
173
174 bool VisitFrame() OVERRIDE REQUIRES(art::Locks::mutator_lock_) {
Alex Lighteee0bd42017-02-14 15:31:45 +0000175 art::ScopedAssertNoThreadSuspension snts("Fixing up the stack for obsolete methods.");
Alex Lightdba61482016-12-21 08:20:29 -0800176 art::ArtMethod* old_method = GetMethod();
Alex Lightdba61482016-12-21 08:20:29 -0800177 if (obsoleted_methods_.find(old_method) != obsoleted_methods_.end()) {
Alex Lightdba61482016-12-21 08:20:29 -0800178 // We cannot ensure that the right dex file is used in inlined frames so we don't support
179 // redefining them.
180 DCHECK(!IsInInlinedFrame()) << "Inlined frames are not supported when using redefinition";
Alex Lighteee0bd42017-02-14 15:31:45 +0000181 art::ArtMethod* new_obsolete_method = obsolete_maps_->FindObsoleteVersion(old_method);
182 if (new_obsolete_method == nullptr) {
Alex Lightdba61482016-12-21 08:20:29 -0800183 // Create a new Obsolete Method and put it in the list.
184 art::Runtime* runtime = art::Runtime::Current();
185 art::ClassLinker* cl = runtime->GetClassLinker();
186 auto ptr_size = cl->GetImagePointerSize();
187 const size_t method_size = art::ArtMethod::Size(ptr_size);
Alex Light5c11a792017-03-10 14:29:22 -0800188 auto* method_storage = allocator_->Alloc(art::Thread::Current(), method_size);
Alex Light007ada22017-01-10 13:33:56 -0800189 CHECK(method_storage != nullptr) << "Unable to allocate storage for obsolete version of '"
190 << old_method->PrettyMethod() << "'";
Alex Lightdba61482016-12-21 08:20:29 -0800191 new_obsolete_method = new (method_storage) art::ArtMethod();
192 new_obsolete_method->CopyFrom(old_method, ptr_size);
193 DCHECK_EQ(new_obsolete_method->GetDeclaringClass(), old_method->GetDeclaringClass());
194 new_obsolete_method->SetIsObsolete();
Alex Lightfcbafb32017-02-02 15:09:54 -0800195 new_obsolete_method->SetDontCompile();
Alex Lightdb01a092017-04-03 15:39:55 -0700196 cl->SetEntryPointsForObsoleteMethod(new_obsolete_method);
Alex Lighteee0bd42017-02-14 15:31:45 +0000197 obsolete_maps_->RecordObsolete(old_method, new_obsolete_method);
Alex Lightdba61482016-12-21 08:20:29 -0800198 // Update JIT Data structures to point to the new method.
199 art::jit::Jit* jit = art::Runtime::Current()->GetJit();
200 if (jit != nullptr) {
201 // Notify the JIT we are making this obsolete method. It will update the jit's internal
202 // structures to keep track of the new obsolete method.
203 jit->GetCodeCache()->MoveObsoleteMethod(old_method, new_obsolete_method);
204 }
Alex Lightdba61482016-12-21 08:20:29 -0800205 }
206 DCHECK(new_obsolete_method != nullptr);
207 SetMethod(new_obsolete_method);
208 }
209 return true;
210 }
211
212 private:
213 // The linear allocator we should use to make new methods.
214 art::LinearAlloc* allocator_;
215 // The set of all methods which could be obsoleted.
216 const std::unordered_set<art::ArtMethod*>& obsoleted_methods_;
217 // A map from the original to the newly allocated obsolete method for frames on this thread. The
Alex Lighteee0bd42017-02-14 15:31:45 +0000218 // values in this map are added to the obsolete_methods_ (and obsolete_dex_caches_) fields of
219 // the redefined classes ClassExt as it is filled.
220 ObsoleteMap* obsolete_maps_;
Alex Lightdba61482016-12-21 08:20:29 -0800221};
222
Alex Lighte4a88632017-01-10 07:41:24 -0800223jvmtiError Redefiner::IsModifiableClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
224 jclass klass,
225 jboolean* is_redefinable) {
Alex Lighte4a88632017-01-10 07:41:24 -0800226 art::Thread* self = art::Thread::Current();
227 art::ScopedObjectAccess soa(self);
228 art::StackHandleScope<1> hs(self);
229 art::ObjPtr<art::mirror::Object> obj(self->DecodeJObject(klass));
230 if (obj.IsNull()) {
231 return ERR(INVALID_CLASS);
232 }
233 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(obj->AsClass()));
234 std::string err_unused;
235 *is_redefinable =
236 Redefiner::GetClassRedefinitionError(h_klass, &err_unused) == OK ? JNI_TRUE : JNI_FALSE;
237 return OK;
238}
239
240jvmtiError Redefiner::GetClassRedefinitionError(art::Handle<art::mirror::Class> klass,
241 /*out*/std::string* error_msg) {
242 if (klass->IsPrimitive()) {
243 *error_msg = "Modification of primitive classes is not supported";
244 return ERR(UNMODIFIABLE_CLASS);
245 } else if (klass->IsInterface()) {
246 *error_msg = "Modification of Interface classes is currently not supported";
247 return ERR(UNMODIFIABLE_CLASS);
Alex Light09f274f2017-02-21 15:00:48 -0800248 } else if (klass->IsStringClass()) {
249 *error_msg = "Modification of String class is not supported";
250 return ERR(UNMODIFIABLE_CLASS);
Alex Lighte4a88632017-01-10 07:41:24 -0800251 } else if (klass->IsArrayClass()) {
252 *error_msg = "Modification of Array classes is not supported";
253 return ERR(UNMODIFIABLE_CLASS);
254 } else if (klass->IsProxyClass()) {
255 *error_msg = "Modification of proxy classes is not supported";
256 return ERR(UNMODIFIABLE_CLASS);
257 }
258
Alex Lighte77b48b2017-02-22 11:08:06 -0800259 for (jclass c : art::NonDebuggableClasses::GetNonDebuggableClasses()) {
260 if (klass.Get() == art::Thread::Current()->DecodeJObject(c)->AsClass()) {
261 *error_msg = "Class might have stack frames that cannot be made obsolete";
262 return ERR(UNMODIFIABLE_CLASS);
263 }
264 }
265
Alex Lighte4a88632017-01-10 07:41:24 -0800266 return OK;
267}
268
Alex Lighta01de592016-11-15 10:43:06 -0800269// Moves dex data to an anonymous, read-only mmap'd region.
270std::unique_ptr<art::MemMap> Redefiner::MoveDataToMemMap(const std::string& original_location,
Vladimir Markoe1993c72017-06-14 17:01:38 +0100271 art::ArrayRef<const unsigned char> data,
Alex Lighta01de592016-11-15 10:43:06 -0800272 std::string* error_msg) {
273 std::unique_ptr<art::MemMap> map(art::MemMap::MapAnonymous(
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800274 StringPrintf("%s-transformed", original_location.c_str()).c_str(),
Alex Lighta01de592016-11-15 10:43:06 -0800275 nullptr,
Alex Lightb7354d52017-03-30 15:17:01 -0700276 data.size(),
Alex Lighta01de592016-11-15 10:43:06 -0800277 PROT_READ|PROT_WRITE,
278 /*low_4gb*/false,
279 /*reuse*/false,
280 error_msg));
281 if (map == nullptr) {
282 return map;
283 }
Vladimir Markoe1993c72017-06-14 17:01:38 +0100284 memcpy(map->Begin(), data.data(), data.size());
Alex Light0b772572016-12-02 17:27:31 -0800285 // Make the dex files mmap read only. This matches how other DexFiles are mmaped and prevents
286 // programs from corrupting it.
Alex Lighta01de592016-11-15 10:43:06 -0800287 map->Protect(PROT_READ);
288 return map;
289}
290
Alex Lighta7e38d82017-01-19 14:57:28 -0800291Redefiner::ClassRedefinition::ClassRedefinition(
292 Redefiner* driver,
293 jclass klass,
294 const art::DexFile* redefined_dex_file,
295 const char* class_sig,
Vladimir Markoe1993c72017-06-14 17:01:38 +0100296 art::ArrayRef<const unsigned char> orig_dex_file) :
Alex Lighta7e38d82017-01-19 14:57:28 -0800297 driver_(driver),
298 klass_(klass),
299 dex_file_(redefined_dex_file),
300 class_sig_(class_sig),
301 original_dex_file_(orig_dex_file) {
Alex Light0e692732017-01-10 15:00:05 -0800302 GetMirrorClass()->MonitorEnter(driver_->self_);
303}
304
305Redefiner::ClassRedefinition::~ClassRedefinition() {
306 if (driver_ != nullptr) {
307 GetMirrorClass()->MonitorExit(driver_->self_);
308 }
309}
310
Alex Light0e692732017-01-10 15:00:05 -0800311jvmtiError Redefiner::RedefineClasses(ArtJvmTiEnv* env,
Andreas Gampede19eb92017-02-24 16:21:18 -0800312 EventHandler* event_handler,
Alex Light0e692732017-01-10 15:00:05 -0800313 art::Runtime* runtime,
314 art::Thread* self,
315 jint class_count,
316 const jvmtiClassDefinition* definitions,
Alex Light6ac57502017-01-19 15:05:06 -0800317 /*out*/std::string* error_msg) {
Alex Light0e692732017-01-10 15:00:05 -0800318 if (env == nullptr) {
319 *error_msg = "env was null!";
320 return ERR(INVALID_ENVIRONMENT);
321 } else if (class_count < 0) {
322 *error_msg = "class_count was less then 0";
323 return ERR(ILLEGAL_ARGUMENT);
324 } else if (class_count == 0) {
325 // We don't actually need to do anything. Just return OK.
326 return OK;
327 } else if (definitions == nullptr) {
328 *error_msg = "null definitions!";
329 return ERR(NULL_POINTER);
330 }
Alex Light6ac57502017-01-19 15:05:06 -0800331 std::vector<ArtClassDefinition> def_vector;
332 def_vector.reserve(class_count);
333 for (jint i = 0; i < class_count; i++) {
Alex Lightce6ee702017-03-06 15:46:43 -0800334 jboolean is_modifiable = JNI_FALSE;
335 jvmtiError res = env->IsModifiableClass(definitions[i].klass, &is_modifiable);
336 if (res != OK) {
337 return res;
338 } else if (!is_modifiable) {
339 return ERR(UNMODIFIABLE_CLASS);
340 }
Alex Light6ac57502017-01-19 15:05:06 -0800341 // We make a copy of the class_bytes to pass into the retransformation.
342 // This makes cleanup easier (since we unambiguously own the bytes) and also is useful since we
343 // will need to keep the original bytes around unaltered for subsequent RetransformClasses calls
344 // to get the passed in bytes.
Alex Light6ac57502017-01-19 15:05:06 -0800345 unsigned char* class_bytes_copy = nullptr;
Alex Lightce6ee702017-03-06 15:46:43 -0800346 res = env->Allocate(definitions[i].class_byte_count, &class_bytes_copy);
Alex Light6ac57502017-01-19 15:05:06 -0800347 if (res != OK) {
348 return res;
349 }
350 memcpy(class_bytes_copy, definitions[i].class_bytes, definitions[i].class_byte_count);
351
352 ArtClassDefinition def;
Alex Lightb7354d52017-03-30 15:17:01 -0700353 res = def.Init(env, definitions[i]);
Alex Light6ac57502017-01-19 15:05:06 -0800354 if (res != OK) {
355 return res;
356 }
357 def_vector.push_back(std::move(def));
358 }
359 // Call all the transformation events.
360 jvmtiError res = Transformer::RetransformClassesDirect(env,
Andreas Gampede19eb92017-02-24 16:21:18 -0800361 event_handler,
Alex Light6ac57502017-01-19 15:05:06 -0800362 self,
363 &def_vector);
364 if (res != OK) {
365 // Something went wrong with transformation!
366 return res;
367 }
368 return RedefineClassesDirect(env, runtime, self, def_vector, error_msg);
369}
370
371jvmtiError Redefiner::RedefineClassesDirect(ArtJvmTiEnv* env,
372 art::Runtime* runtime,
373 art::Thread* self,
374 const std::vector<ArtClassDefinition>& definitions,
375 std::string* error_msg) {
376 DCHECK(env != nullptr);
377 if (definitions.size() == 0) {
378 // We don't actually need to do anything. Just return OK.
379 return OK;
380 }
Alex Light0e692732017-01-10 15:00:05 -0800381 // Stop JIT for the duration of this redefine since the JIT might concurrently compile a method we
382 // are going to redefine.
383 art::jit::ScopedJitSuspend suspend_jit;
384 // Get shared mutator lock so we can lock all the classes.
385 art::ScopedObjectAccess soa(self);
Alex Lighta26e3492017-06-27 17:55:37 -0700386 Redefiner r(env, runtime, self, error_msg);
Alex Light6ac57502017-01-19 15:05:06 -0800387 for (const ArtClassDefinition& def : definitions) {
388 // Only try to transform classes that have been modified.
Alex Light40528472017-03-28 09:07:36 -0700389 if (def.IsModified()) {
Alex Light6ac57502017-01-19 15:05:06 -0800390 jvmtiError res = r.AddRedefinition(env, def);
391 if (res != OK) {
392 return res;
393 }
Alex Light0e692732017-01-10 15:00:05 -0800394 }
395 }
396 return r.Run();
397}
398
Alex Light6ac57502017-01-19 15:05:06 -0800399jvmtiError Redefiner::AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def) {
Alex Light0e692732017-01-10 15:00:05 -0800400 std::string original_dex_location;
401 jvmtiError ret = OK;
Alex Lightb7354d52017-03-30 15:17:01 -0700402 if ((ret = GetClassLocation(env, def.GetClass(), &original_dex_location))) {
Alex Light0e692732017-01-10 15:00:05 -0800403 *error_msg_ = "Unable to get original dex file location!";
404 return ret;
405 }
Alex Light52a2db52017-01-19 23:00:21 +0000406 char* generic_ptr_unused = nullptr;
407 char* signature_ptr = nullptr;
Alex Lightb7354d52017-03-30 15:17:01 -0700408 if ((ret = env->GetClassSignature(def.GetClass(), &signature_ptr, &generic_ptr_unused)) != OK) {
Alex Light6ac57502017-01-19 15:05:06 -0800409 *error_msg_ = "Unable to get class signature!";
410 return ret;
Alex Light52a2db52017-01-19 23:00:21 +0000411 }
Andreas Gampe54711412017-02-21 12:41:43 -0800412 JvmtiUniquePtr<char> generic_unique_ptr(MakeJvmtiUniquePtr(env, generic_ptr_unused));
413 JvmtiUniquePtr<char> signature_unique_ptr(MakeJvmtiUniquePtr(env, signature_ptr));
Alex Light6ac57502017-01-19 15:05:06 -0800414 std::unique_ptr<art::MemMap> map(MoveDataToMemMap(original_dex_location,
Alex Lightb7354d52017-03-30 15:17:01 -0700415 def.GetDexData(),
Alex Light6ac57502017-01-19 15:05:06 -0800416 error_msg_));
417 std::ostringstream os;
Alex Lighta01de592016-11-15 10:43:06 -0800418 if (map.get() == nullptr) {
Alex Lightb7354d52017-03-30 15:17:01 -0700419 os << "Failed to create anonymous mmap for modified dex file of class " << def.GetName()
Alex Light0e692732017-01-10 15:00:05 -0800420 << "in dex file " << original_dex_location << " because: " << *error_msg_;
421 *error_msg_ = os.str();
Alex Lighta01de592016-11-15 10:43:06 -0800422 return ERR(OUT_OF_MEMORY);
423 }
424 if (map->Size() < sizeof(art::DexFile::Header)) {
Alex Light0e692732017-01-10 15:00:05 -0800425 *error_msg_ = "Could not read dex file header because dex_data was too short";
Alex Lighta01de592016-11-15 10:43:06 -0800426 return ERR(INVALID_CLASS_FORMAT);
427 }
428 uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map->Begin())->checksum_;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700429 std::unique_ptr<const art::DexFile> dex_file(art::DexFileLoader::Open(map->GetName(),
430 checksum,
431 std::move(map),
432 /*verify*/true,
433 /*verify_checksum*/true,
434 error_msg_));
Alex Lighta01de592016-11-15 10:43:06 -0800435 if (dex_file.get() == nullptr) {
Alex Lightb7354d52017-03-30 15:17:01 -0700436 os << "Unable to load modified dex file for " << def.GetName() << ": " << *error_msg_;
Alex Light0e692732017-01-10 15:00:05 -0800437 *error_msg_ = os.str();
Alex Lighta01de592016-11-15 10:43:06 -0800438 return ERR(INVALID_CLASS_FORMAT);
439 }
Alex Light0e692732017-01-10 15:00:05 -0800440 redefinitions_.push_back(
Alex Lighta7e38d82017-01-19 14:57:28 -0800441 Redefiner::ClassRedefinition(this,
Alex Lightb7354d52017-03-30 15:17:01 -0700442 def.GetClass(),
Alex Lighta7e38d82017-01-19 14:57:28 -0800443 dex_file.release(),
444 signature_ptr,
Alex Light40528472017-03-28 09:07:36 -0700445 def.GetNewOriginalDexFile()));
Alex Light0e692732017-01-10 15:00:05 -0800446 return OK;
Alex Lighta01de592016-11-15 10:43:06 -0800447}
448
Alex Light0e692732017-01-10 15:00:05 -0800449art::mirror::Class* Redefiner::ClassRedefinition::GetMirrorClass() {
450 return driver_->self_->DecodeJObject(klass_)->AsClass();
Alex Lighta01de592016-11-15 10:43:06 -0800451}
452
Alex Light0e692732017-01-10 15:00:05 -0800453art::mirror::ClassLoader* Redefiner::ClassRedefinition::GetClassLoader() {
Alex Lighta01de592016-11-15 10:43:06 -0800454 return GetMirrorClass()->GetClassLoader();
455}
456
Alex Light0e692732017-01-10 15:00:05 -0800457art::mirror::DexCache* Redefiner::ClassRedefinition::CreateNewDexCache(
458 art::Handle<art::mirror::ClassLoader> loader) {
Alex Light07f06212017-06-01 14:01:43 -0700459 art::StackHandleScope<2> hs(driver_->self_);
460 art::ClassLinker* cl = driver_->runtime_->GetClassLinker();
461 art::Handle<art::mirror::DexCache> cache(hs.NewHandle(
462 art::ObjPtr<art::mirror::DexCache>::DownCast(
463 cl->GetClassRoot(art::ClassLinker::kJavaLangDexCache)->AllocObject(driver_->self_))));
464 if (cache.IsNull()) {
465 driver_->self_->AssertPendingOOMException();
466 return nullptr;
467 }
468 art::Handle<art::mirror::String> location(hs.NewHandle(
469 cl->GetInternTable()->InternStrong(dex_file_->GetLocation().c_str())));
470 if (location.IsNull()) {
471 driver_->self_->AssertPendingOOMException();
472 return nullptr;
473 }
474 art::WriterMutexLock mu(driver_->self_, *art::Locks::dex_lock_);
475 art::mirror::DexCache::InitializeDexCache(driver_->self_,
476 cache.Get(),
477 location.Get(),
478 dex_file_.get(),
479 loader.IsNull() ? driver_->runtime_->GetLinearAlloc()
480 : loader->GetAllocator(),
481 art::kRuntimePointerSize);
482 return cache.Get();
Alex Lighta01de592016-11-15 10:43:06 -0800483}
484
Alex Light0e692732017-01-10 15:00:05 -0800485void Redefiner::RecordFailure(jvmtiError result,
486 const std::string& class_sig,
487 const std::string& error_msg) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800488 *error_msg_ = StringPrintf("Unable to perform redefinition of '%s': %s",
Alex Light0e692732017-01-10 15:00:05 -0800489 class_sig.c_str(),
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800490 error_msg.c_str());
Alex Lighta01de592016-11-15 10:43:06 -0800491 result_ = result;
492}
493
Alex Light2f814aa2017-03-24 15:21:34 +0000494art::mirror::Object* Redefiner::ClassRedefinition::AllocateOrGetOriginalDexFile() {
Alex Lighta7e38d82017-01-19 14:57:28 -0800495 // If we have been specifically given a new set of bytes use that
496 if (original_dex_file_.size() != 0) {
Alex Light440b5d92017-01-24 15:32:25 -0800497 return art::mirror::ByteArray::AllocateAndFill(
498 driver_->self_,
Vladimir Markoe1993c72017-06-14 17:01:38 +0100499 reinterpret_cast<const signed char*>(original_dex_file_.data()),
Alex Light440b5d92017-01-24 15:32:25 -0800500 original_dex_file_.size());
Alex Lighta01de592016-11-15 10:43:06 -0800501 }
Alex Lighta7e38d82017-01-19 14:57:28 -0800502
503 // See if we already have one set.
504 art::ObjPtr<art::mirror::ClassExt> ext(GetMirrorClass()->GetExtData());
505 if (!ext.IsNull()) {
Alex Light2f814aa2017-03-24 15:21:34 +0000506 art::ObjPtr<art::mirror::Object> old_original_dex_file(ext->GetOriginalDexFile());
507 if (!old_original_dex_file.IsNull()) {
Alex Lighta7e38d82017-01-19 14:57:28 -0800508 // We do. Use it.
Alex Light2f814aa2017-03-24 15:21:34 +0000509 return old_original_dex_file.Ptr();
Alex Lighta7e38d82017-01-19 14:57:28 -0800510 }
Alex Lighta01de592016-11-15 10:43:06 -0800511 }
Alex Lighta7e38d82017-01-19 14:57:28 -0800512
Alex Light2f814aa2017-03-24 15:21:34 +0000513 // return the current dex_cache which has the dex file in it.
514 art::ObjPtr<art::mirror::DexCache> current_dex_cache(GetMirrorClass()->GetDexCache());
Alex Lighta7e38d82017-01-19 14:57:28 -0800515 // TODO Handle this or make it so it cannot happen.
Alex Light2f814aa2017-03-24 15:21:34 +0000516 if (current_dex_cache->GetDexFile()->NumClassDefs() != 1) {
Alex Lighta7e38d82017-01-19 14:57:28 -0800517 LOG(WARNING) << "Current dex file has more than one class in it. Calling RetransformClasses "
518 << "on this class might fail if no transformations are applied to it!";
Alex Lighta01de592016-11-15 10:43:06 -0800519 }
Alex Light2f814aa2017-03-24 15:21:34 +0000520 return current_dex_cache.Ptr();
Alex Lighta01de592016-11-15 10:43:06 -0800521}
522
Alex Lightdba61482016-12-21 08:20:29 -0800523struct CallbackCtx {
Alex Lighteee0bd42017-02-14 15:31:45 +0000524 ObsoleteMap* obsolete_map;
Alex Lightdba61482016-12-21 08:20:29 -0800525 art::LinearAlloc* allocator;
Alex Lightdba61482016-12-21 08:20:29 -0800526 std::unordered_set<art::ArtMethod*> obsolete_methods;
Alex Lightdba61482016-12-21 08:20:29 -0800527
Alex Lighteee0bd42017-02-14 15:31:45 +0000528 explicit CallbackCtx(ObsoleteMap* map, art::LinearAlloc* alloc)
529 : obsolete_map(map), allocator(alloc) {}
Alex Lightdba61482016-12-21 08:20:29 -0800530};
531
Alex Lightdba61482016-12-21 08:20:29 -0800532void DoAllocateObsoleteMethodsCallback(art::Thread* t, void* vdata) NO_THREAD_SAFETY_ANALYSIS {
533 CallbackCtx* data = reinterpret_cast<CallbackCtx*>(vdata);
Alex Light007ada22017-01-10 13:33:56 -0800534 ObsoleteMethodStackVisitor::UpdateObsoleteFrames(t,
535 data->allocator,
536 data->obsolete_methods,
Alex Lighteee0bd42017-02-14 15:31:45 +0000537 data->obsolete_map);
Alex Lightdba61482016-12-21 08:20:29 -0800538}
539
540// This creates any ArtMethod* structures needed for obsolete methods and ensures that the stack is
541// updated so they will be run.
Alex Light0e692732017-01-10 15:00:05 -0800542// TODO Rewrite so we can do this only once regardless of how many redefinitions there are.
543void Redefiner::ClassRedefinition::FindAndAllocateObsoleteMethods(art::mirror::Class* art_klass) {
Alex Lightdba61482016-12-21 08:20:29 -0800544 art::ScopedAssertNoThreadSuspension ns("No thread suspension during thread stack walking");
545 art::mirror::ClassExt* ext = art_klass->GetExtData();
546 CHECK(ext->GetObsoleteMethods() != nullptr);
Alex Light7916f202017-01-27 09:00:15 -0800547 art::ClassLinker* linker = driver_->runtime_->GetClassLinker();
Alex Lighteee0bd42017-02-14 15:31:45 +0000548 // This holds pointers to the obsolete methods map fields which are updated as needed.
549 ObsoleteMap map(ext->GetObsoleteMethods(), ext->GetObsoleteDexCaches(), art_klass->GetDexCache());
550 CallbackCtx ctx(&map, linker->GetAllocatorForClassLoader(art_klass->GetClassLoader()));
Alex Lightdba61482016-12-21 08:20:29 -0800551 // Add all the declared methods to the map
552 for (auto& m : art_klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
Alex Light7532d582017-02-13 16:36:06 -0800553 if (m.IsIntrinsic()) {
554 LOG(WARNING) << "Redefining intrinsic method " << m.PrettyMethod() << ". This may cause the "
555 << "unexpected use of the original definition of " << m.PrettyMethod() << "in "
556 << "methods that have already been compiled.";
557 }
Alex Lighteee0bd42017-02-14 15:31:45 +0000558 // It is possible to simply filter out some methods where they cannot really become obsolete,
559 // such as native methods and keep their original (possibly optimized) implementations. We don't
560 // do this, however, since we would need to mark these functions (still in the classes
561 // declared_methods array) as obsolete so we will find the correct dex file to get meta-data
562 // from (for example about stack-frame size). Furthermore we would be unable to get some useful
563 // error checking from the interpreter which ensure we don't try to start executing obsolete
564 // methods.
Nicolas Geoffray7558d272017-02-10 10:01:47 +0000565 ctx.obsolete_methods.insert(&m);
Alex Lightdba61482016-12-21 08:20:29 -0800566 }
567 {
Alex Light0e692732017-01-10 15:00:05 -0800568 art::MutexLock mu(driver_->self_, *art::Locks::thread_list_lock_);
Alex Lightdba61482016-12-21 08:20:29 -0800569 art::ThreadList* list = art::Runtime::Current()->GetThreadList();
570 list->ForEach(DoAllocateObsoleteMethodsCallback, static_cast<void*>(&ctx));
Alex Lightdba61482016-12-21 08:20:29 -0800571 }
Alex Lightdba61482016-12-21 08:20:29 -0800572}
573
Alex Light6161f132017-01-25 10:30:20 -0800574// Try and get the declared method. First try to get a virtual method then a direct method if that's
575// not found.
576static art::ArtMethod* FindMethod(art::Handle<art::mirror::Class> klass,
Vladimir Markoba118822017-06-12 15:41:56 +0100577 art::StringPiece name,
Alex Light6161f132017-01-25 10:30:20 -0800578 art::Signature sig) REQUIRES_SHARED(art::Locks::mutator_lock_) {
Vladimir Markoba118822017-06-12 15:41:56 +0100579 DCHECK(!klass->IsProxyClass());
580 for (art::ArtMethod& m : klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize)) {
581 if (m.GetName() == name && m.GetSignature() == sig) {
582 return &m;
583 }
Alex Light6161f132017-01-25 10:30:20 -0800584 }
Vladimir Markoba118822017-06-12 15:41:56 +0100585 return nullptr;
Alex Light6161f132017-01-25 10:30:20 -0800586}
587
588bool Redefiner::ClassRedefinition::CheckSameMethods() {
589 art::StackHandleScope<1> hs(driver_->self_);
590 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass()));
591 DCHECK_EQ(dex_file_->NumClassDefs(), 1u);
592
593 art::ClassDataItemIterator new_iter(*dex_file_,
594 dex_file_->GetClassData(dex_file_->GetClassDef(0)));
595
596 // Make sure we have the same number of methods.
597 uint32_t num_new_method = new_iter.NumVirtualMethods() + new_iter.NumDirectMethods();
598 uint32_t num_old_method = h_klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size();
599 if (num_new_method != num_old_method) {
600 bool bigger = num_new_method > num_old_method;
601 RecordFailure(bigger ? ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED)
602 : ERR(UNSUPPORTED_REDEFINITION_METHOD_DELETED),
603 StringPrintf("Total number of declared methods changed from %d to %d",
604 num_old_method, num_new_method));
605 return false;
606 }
607
608 // Skip all of the fields. We should have already checked this.
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700609 new_iter.SkipAllFields();
Alex Light6161f132017-01-25 10:30:20 -0800610 // Check each of the methods. NB we don't need to specifically check for removals since the 2 dex
611 // files have the same number of methods, which means there must be an equal amount of additions
612 // and removals.
613 for (; new_iter.HasNextVirtualMethod() || new_iter.HasNextDirectMethod(); new_iter.Next()) {
614 // Get the data on the method we are searching for
615 const art::DexFile::MethodId& new_method_id = dex_file_->GetMethodId(new_iter.GetMemberIndex());
616 const char* new_method_name = dex_file_->GetMethodName(new_method_id);
617 art::Signature new_method_signature = dex_file_->GetMethodSignature(new_method_id);
618 art::ArtMethod* old_method = FindMethod(h_klass, new_method_name, new_method_signature);
619 // If we got past the check for the same number of methods above that means there must be at
620 // least one added and one removed method. We will return the ADDED failure message since it is
621 // easier to get a useful error report for it.
622 if (old_method == nullptr) {
623 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED),
624 StringPrintf("Unknown method '%s' (sig: %s) was added!",
625 new_method_name,
626 new_method_signature.ToString().c_str()));
627 return false;
628 }
629 // Since direct methods have different flags than virtual ones (specifically direct methods must
630 // have kAccPrivate or kAccStatic or kAccConstructor flags) we can tell if a method changes from
631 // virtual to direct.
Mathieu Chartierf044c222017-05-31 15:27:54 -0700632 uint32_t new_flags = new_iter.GetMethodAccessFlags() & ~art::kAccPreviouslyWarm;
633 if (new_flags != (old_method->GetAccessFlags() & (art::kAccValidMethodFlags ^ art::kAccPreviouslyWarm))) {
Alex Light6161f132017-01-25 10:30:20 -0800634 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED),
635 StringPrintf("method '%s' (sig: %s) had different access flags",
636 new_method_name,
637 new_method_signature.ToString().c_str()));
638 return false;
639 }
640 }
641 return true;
642}
643
644bool Redefiner::ClassRedefinition::CheckSameFields() {
645 art::StackHandleScope<1> hs(driver_->self_);
646 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass()));
647 DCHECK_EQ(dex_file_->NumClassDefs(), 1u);
648 art::ClassDataItemIterator new_iter(*dex_file_,
649 dex_file_->GetClassData(dex_file_->GetClassDef(0)));
650 const art::DexFile& old_dex_file = h_klass->GetDexFile();
651 art::ClassDataItemIterator old_iter(old_dex_file,
652 old_dex_file.GetClassData(*h_klass->GetClassDef()));
653 // Instance and static fields can be differentiated by their flags so no need to check them
654 // separately.
655 while (new_iter.HasNextInstanceField() || new_iter.HasNextStaticField()) {
656 // Get the data on the method we are searching for
657 const art::DexFile::FieldId& new_field_id = dex_file_->GetFieldId(new_iter.GetMemberIndex());
658 const char* new_field_name = dex_file_->GetFieldName(new_field_id);
659 const char* new_field_type = dex_file_->GetFieldTypeDescriptor(new_field_id);
660
661 if (!(old_iter.HasNextInstanceField() || old_iter.HasNextStaticField())) {
662 // We are missing the old version of this method!
663 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED),
664 StringPrintf("Unknown field '%s' (type: %s) added!",
665 new_field_name,
666 new_field_type));
667 return false;
668 }
669
670 const art::DexFile::FieldId& old_field_id = old_dex_file.GetFieldId(old_iter.GetMemberIndex());
671 const char* old_field_name = old_dex_file.GetFieldName(old_field_id);
672 const char* old_field_type = old_dex_file.GetFieldTypeDescriptor(old_field_id);
673
674 // Check name and type.
675 if (strcmp(old_field_name, new_field_name) != 0 ||
676 strcmp(old_field_type, new_field_type) != 0) {
677 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED),
678 StringPrintf("Field changed from '%s' (sig: %s) to '%s' (sig: %s)!",
679 old_field_name,
680 old_field_type,
681 new_field_name,
682 new_field_type));
683 return false;
684 }
685
686 // Since static fields have different flags than instance ones (specifically static fields must
687 // have the kAccStatic flag) we can tell if a field changes from static to instance.
688 if (new_iter.GetFieldAccessFlags() != old_iter.GetFieldAccessFlags()) {
689 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED),
690 StringPrintf("Field '%s' (sig: %s) had different access flags",
691 new_field_name,
692 new_field_type));
693 return false;
694 }
695
696 new_iter.Next();
697 old_iter.Next();
698 }
699 if (old_iter.HasNextInstanceField() || old_iter.HasNextStaticField()) {
700 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED),
701 StringPrintf("field '%s' (sig: %s) is missing!",
702 old_dex_file.GetFieldName(old_dex_file.GetFieldId(
703 old_iter.GetMemberIndex())),
704 old_dex_file.GetFieldTypeDescriptor(old_dex_file.GetFieldId(
705 old_iter.GetMemberIndex()))));
706 return false;
707 }
708 return true;
709}
710
Alex Light0e692732017-01-10 15:00:05 -0800711bool Redefiner::ClassRedefinition::CheckClass() {
Alex Light0e692732017-01-10 15:00:05 -0800712 art::StackHandleScope<1> hs(driver_->self_);
Alex Light460d1b42017-01-10 15:37:17 +0000713 // Easy check that only 1 class def is present.
714 if (dex_file_->NumClassDefs() != 1) {
715 RecordFailure(ERR(ILLEGAL_ARGUMENT),
716 StringPrintf("Expected 1 class def in dex file but found %d",
717 dex_file_->NumClassDefs()));
718 return false;
719 }
720 // Get the ClassDef from the new DexFile.
721 // Since the dex file has only a single class def the index is always 0.
722 const art::DexFile::ClassDef& def = dex_file_->GetClassDef(0);
723 // Get the class as it is now.
724 art::Handle<art::mirror::Class> current_class(hs.NewHandle(GetMirrorClass()));
725
726 // Check the access flags didn't change.
727 if (def.GetJavaAccessFlags() != (current_class->GetAccessFlags() & art::kAccValidClassFlags)) {
728 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED),
729 "Cannot change modifiers of class by redefinition");
730 return false;
731 }
732
733 // Check class name.
734 // These should have been checked by the dexfile verifier on load.
735 DCHECK_NE(def.class_idx_, art::dex::TypeIndex::Invalid()) << "Invalid type index";
736 const char* descriptor = dex_file_->StringByTypeIdx(def.class_idx_);
737 DCHECK(descriptor != nullptr) << "Invalid dex file structure!";
738 if (!current_class->DescriptorEquals(descriptor)) {
739 std::string storage;
740 RecordFailure(ERR(NAMES_DONT_MATCH),
741 StringPrintf("expected file to contain class called '%s' but found '%s'!",
742 current_class->GetDescriptor(&storage),
743 descriptor));
744 return false;
745 }
746 if (current_class->IsObjectClass()) {
747 if (def.superclass_idx_ != art::dex::TypeIndex::Invalid()) {
748 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass added!");
749 return false;
750 }
751 } else {
752 const char* super_descriptor = dex_file_->StringByTypeIdx(def.superclass_idx_);
753 DCHECK(descriptor != nullptr) << "Invalid dex file structure!";
754 if (!current_class->GetSuperClass()->DescriptorEquals(super_descriptor)) {
755 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass changed");
756 return false;
757 }
758 }
759 const art::DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(def);
760 if (interfaces == nullptr) {
761 if (current_class->NumDirectInterfaces() != 0) {
762 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added");
763 return false;
764 }
765 } else {
766 DCHECK(!current_class->IsProxyClass());
767 const art::DexFile::TypeList* current_interfaces = current_class->GetInterfaceTypeList();
768 if (current_interfaces == nullptr || current_interfaces->Size() != interfaces->Size()) {
769 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added or removed");
770 return false;
771 }
772 // The order of interfaces is (barely) meaningful so we error if it changes.
773 const art::DexFile& orig_dex_file = current_class->GetDexFile();
774 for (uint32_t i = 0; i < interfaces->Size(); i++) {
775 if (strcmp(
776 dex_file_->StringByTypeIdx(interfaces->GetTypeItem(i).type_idx_),
777 orig_dex_file.StringByTypeIdx(current_interfaces->GetTypeItem(i).type_idx_)) != 0) {
778 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED),
779 "Interfaces changed or re-ordered");
780 return false;
781 }
782 }
783 }
Alex Light460d1b42017-01-10 15:37:17 +0000784 return true;
785}
786
Alex Light0e692732017-01-10 15:00:05 -0800787bool Redefiner::ClassRedefinition::CheckRedefinable() {
Alex Lighte4a88632017-01-10 07:41:24 -0800788 std::string err;
Alex Light0e692732017-01-10 15:00:05 -0800789 art::StackHandleScope<1> hs(driver_->self_);
Alex Light460d1b42017-01-10 15:37:17 +0000790
Alex Lighte4a88632017-01-10 07:41:24 -0800791 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass()));
792 jvmtiError res = Redefiner::GetClassRedefinitionError(h_klass, &err);
793 if (res != OK) {
794 RecordFailure(res, err);
795 return false;
796 } else {
797 return true;
798 }
Alex Light460d1b42017-01-10 15:37:17 +0000799}
800
Alex Light0e692732017-01-10 15:00:05 -0800801bool Redefiner::ClassRedefinition::CheckRedefinitionIsValid() {
Alex Light460d1b42017-01-10 15:37:17 +0000802 return CheckRedefinable() &&
803 CheckClass() &&
804 CheckSameFields() &&
805 CheckSameMethods();
806}
807
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800808class RedefinitionDataIter;
809
Alex Light0e692732017-01-10 15:00:05 -0800810// A wrapper that lets us hold onto the arbitrary sized data needed for redefinitions in a
811// reasonably sane way. This adds no fields to the normal ObjectArray. By doing this we can avoid
812// having to deal with the fact that we need to hold an arbitrary number of references live.
813class RedefinitionDataHolder {
814 public:
815 enum DataSlot : int32_t {
816 kSlotSourceClassLoader = 0,
817 kSlotJavaDexFile = 1,
818 kSlotNewDexFileCookie = 2,
819 kSlotNewDexCache = 3,
820 kSlotMirrorClass = 4,
Alex Lighta7e38d82017-01-19 14:57:28 -0800821 kSlotOrigDexFile = 5,
Alex Light1e3926a2017-04-07 10:38:06 -0700822 kSlotOldObsoleteMethods = 6,
823 kSlotOldDexCaches = 7,
Alex Light0e692732017-01-10 15:00:05 -0800824
825 // Must be last one.
Alex Light1e3926a2017-04-07 10:38:06 -0700826 kNumSlots = 8,
Alex Light0e692732017-01-10 15:00:05 -0800827 };
828
829 // This needs to have a HandleScope passed in that is capable of creating a new Handle without
830 // overflowing. Only one handle will be created. This object has a lifetime identical to that of
831 // the passed in handle-scope.
832 RedefinitionDataHolder(art::StackHandleScope<1>* hs,
833 art::Runtime* runtime,
834 art::Thread* self,
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800835 std::vector<Redefiner::ClassRedefinition>* redefinitions)
836 REQUIRES_SHARED(art::Locks::mutator_lock_) :
Alex Light0e692732017-01-10 15:00:05 -0800837 arr_(
838 hs->NewHandle(
839 art::mirror::ObjectArray<art::mirror::Object>::Alloc(
840 self,
841 runtime->GetClassLinker()->GetClassRoot(art::ClassLinker::kObjectArrayClass),
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800842 redefinitions->size() * kNumSlots))),
843 redefinitions_(redefinitions) {}
Alex Light0e692732017-01-10 15:00:05 -0800844
845 bool IsNull() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
846 return arr_.IsNull();
847 }
848
Alex Light8c889d22017-02-06 13:58:27 -0800849 art::mirror::ClassLoader* GetSourceClassLoader(jint klass_index) const
Alex Light0e692732017-01-10 15:00:05 -0800850 REQUIRES_SHARED(art::Locks::mutator_lock_) {
851 return art::down_cast<art::mirror::ClassLoader*>(GetSlot(klass_index, kSlotSourceClassLoader));
852 }
Alex Light8c889d22017-02-06 13:58:27 -0800853 art::mirror::Object* GetJavaDexFile(jint klass_index) const
854 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light0e692732017-01-10 15:00:05 -0800855 return GetSlot(klass_index, kSlotJavaDexFile);
856 }
Alex Light8c889d22017-02-06 13:58:27 -0800857 art::mirror::LongArray* GetNewDexFileCookie(jint klass_index) const
Alex Light0e692732017-01-10 15:00:05 -0800858 REQUIRES_SHARED(art::Locks::mutator_lock_) {
859 return art::down_cast<art::mirror::LongArray*>(GetSlot(klass_index, kSlotNewDexFileCookie));
860 }
Alex Light8c889d22017-02-06 13:58:27 -0800861 art::mirror::DexCache* GetNewDexCache(jint klass_index) const
Alex Light0e692732017-01-10 15:00:05 -0800862 REQUIRES_SHARED(art::Locks::mutator_lock_) {
863 return art::down_cast<art::mirror::DexCache*>(GetSlot(klass_index, kSlotNewDexCache));
864 }
Alex Light8c889d22017-02-06 13:58:27 -0800865 art::mirror::Class* GetMirrorClass(jint klass_index) const
866 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light0e692732017-01-10 15:00:05 -0800867 return art::down_cast<art::mirror::Class*>(GetSlot(klass_index, kSlotMirrorClass));
868 }
869
Alex Light2f814aa2017-03-24 15:21:34 +0000870 art::mirror::Object* GetOriginalDexFile(jint klass_index) const
Alex Lighta7e38d82017-01-19 14:57:28 -0800871 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light2f814aa2017-03-24 15:21:34 +0000872 return art::down_cast<art::mirror::Object*>(GetSlot(klass_index, kSlotOrigDexFile));
Alex Lighta7e38d82017-01-19 14:57:28 -0800873 }
874
Alex Light1e3926a2017-04-07 10:38:06 -0700875 art::mirror::PointerArray* GetOldObsoleteMethods(jint klass_index) const
876 REQUIRES_SHARED(art::Locks::mutator_lock_) {
877 return art::down_cast<art::mirror::PointerArray*>(
878 GetSlot(klass_index, kSlotOldObsoleteMethods));
879 }
880
881 art::mirror::ObjectArray<art::mirror::DexCache>* GetOldDexCaches(jint klass_index) const
882 REQUIRES_SHARED(art::Locks::mutator_lock_) {
883 return art::down_cast<art::mirror::ObjectArray<art::mirror::DexCache>*>(
884 GetSlot(klass_index, kSlotOldDexCaches));
885 }
886
Alex Light0e692732017-01-10 15:00:05 -0800887 void SetSourceClassLoader(jint klass_index, art::mirror::ClassLoader* loader)
888 REQUIRES_SHARED(art::Locks::mutator_lock_) {
889 SetSlot(klass_index, kSlotSourceClassLoader, loader);
890 }
891 void SetJavaDexFile(jint klass_index, art::mirror::Object* dexfile)
892 REQUIRES_SHARED(art::Locks::mutator_lock_) {
893 SetSlot(klass_index, kSlotJavaDexFile, dexfile);
894 }
895 void SetNewDexFileCookie(jint klass_index, art::mirror::LongArray* cookie)
896 REQUIRES_SHARED(art::Locks::mutator_lock_) {
897 SetSlot(klass_index, kSlotNewDexFileCookie, cookie);
898 }
899 void SetNewDexCache(jint klass_index, art::mirror::DexCache* cache)
900 REQUIRES_SHARED(art::Locks::mutator_lock_) {
901 SetSlot(klass_index, kSlotNewDexCache, cache);
902 }
903 void SetMirrorClass(jint klass_index, art::mirror::Class* klass)
904 REQUIRES_SHARED(art::Locks::mutator_lock_) {
905 SetSlot(klass_index, kSlotMirrorClass, klass);
906 }
Alex Light2f814aa2017-03-24 15:21:34 +0000907 void SetOriginalDexFile(jint klass_index, art::mirror::Object* bytes)
Alex Lighta7e38d82017-01-19 14:57:28 -0800908 REQUIRES_SHARED(art::Locks::mutator_lock_) {
909 SetSlot(klass_index, kSlotOrigDexFile, bytes);
910 }
Alex Light1e3926a2017-04-07 10:38:06 -0700911 void SetOldObsoleteMethods(jint klass_index, art::mirror::PointerArray* methods)
912 REQUIRES_SHARED(art::Locks::mutator_lock_) {
913 SetSlot(klass_index, kSlotOldObsoleteMethods, methods);
914 }
915 void SetOldDexCaches(jint klass_index, art::mirror::ObjectArray<art::mirror::DexCache>* caches)
916 REQUIRES_SHARED(art::Locks::mutator_lock_) {
917 SetSlot(klass_index, kSlotOldDexCaches, caches);
918 }
Alex Light0e692732017-01-10 15:00:05 -0800919
Alex Light8c889d22017-02-06 13:58:27 -0800920 int32_t Length() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light0e692732017-01-10 15:00:05 -0800921 return arr_->GetLength() / kNumSlots;
922 }
923
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800924 std::vector<Redefiner::ClassRedefinition>* GetRedefinitions()
925 REQUIRES_SHARED(art::Locks::mutator_lock_) {
926 return redefinitions_;
927 }
928
929 bool operator==(const RedefinitionDataHolder& other) const
930 REQUIRES_SHARED(art::Locks::mutator_lock_) {
931 return arr_.Get() == other.arr_.Get();
932 }
933
934 bool operator!=(const RedefinitionDataHolder& other) const
935 REQUIRES_SHARED(art::Locks::mutator_lock_) {
936 return !(*this == other);
937 }
938
939 RedefinitionDataIter begin() REQUIRES_SHARED(art::Locks::mutator_lock_);
940 RedefinitionDataIter end() REQUIRES_SHARED(art::Locks::mutator_lock_);
941
Alex Light0e692732017-01-10 15:00:05 -0800942 private:
Alex Light8c889d22017-02-06 13:58:27 -0800943 mutable art::Handle<art::mirror::ObjectArray<art::mirror::Object>> arr_;
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800944 std::vector<Redefiner::ClassRedefinition>* redefinitions_;
Alex Light0e692732017-01-10 15:00:05 -0800945
946 art::mirror::Object* GetSlot(jint klass_index,
Alex Light8c889d22017-02-06 13:58:27 -0800947 DataSlot slot) const REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light0e692732017-01-10 15:00:05 -0800948 DCHECK_LT(klass_index, Length());
949 return arr_->Get((kNumSlots * klass_index) + slot);
950 }
951
952 void SetSlot(jint klass_index,
953 DataSlot slot,
954 art::ObjPtr<art::mirror::Object> obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
955 DCHECK(!art::Runtime::Current()->IsActiveTransaction());
956 DCHECK_LT(klass_index, Length());
957 arr_->Set<false>((kNumSlots * klass_index) + slot, obj);
958 }
959
960 DISALLOW_COPY_AND_ASSIGN(RedefinitionDataHolder);
961};
962
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800963class RedefinitionDataIter {
964 public:
965 RedefinitionDataIter(int32_t idx, RedefinitionDataHolder& holder) : idx_(idx), holder_(holder) {}
966
967 RedefinitionDataIter(const RedefinitionDataIter&) = default;
968 RedefinitionDataIter(RedefinitionDataIter&&) = default;
969 RedefinitionDataIter& operator=(const RedefinitionDataIter&) = default;
970 RedefinitionDataIter& operator=(RedefinitionDataIter&&) = default;
971
972 bool operator==(const RedefinitionDataIter& other) const
973 REQUIRES_SHARED(art::Locks::mutator_lock_) {
974 return idx_ == other.idx_ && holder_ == other.holder_;
975 }
976
977 bool operator!=(const RedefinitionDataIter& other) const
978 REQUIRES_SHARED(art::Locks::mutator_lock_) {
979 return !(*this == other);
980 }
981
982 RedefinitionDataIter operator++() { // Value after modification.
983 idx_++;
984 return *this;
985 }
986
987 RedefinitionDataIter operator++(int) {
988 RedefinitionDataIter temp = *this;
989 idx_++;
990 return temp;
991 }
992
993 RedefinitionDataIter operator+(ssize_t delta) const {
994 RedefinitionDataIter temp = *this;
995 temp += delta;
996 return temp;
997 }
998
999 RedefinitionDataIter& operator+=(ssize_t delta) {
1000 idx_ += delta;
1001 return *this;
1002 }
1003
1004 Redefiner::ClassRedefinition& GetRedefinition() REQUIRES_SHARED(art::Locks::mutator_lock_) {
1005 return (*holder_.GetRedefinitions())[idx_];
1006 }
1007
1008 RedefinitionDataHolder& GetHolder() {
1009 return holder_;
1010 }
1011
1012 art::mirror::ClassLoader* GetSourceClassLoader() const
1013 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1014 return holder_.GetSourceClassLoader(idx_);
1015 }
1016 art::mirror::Object* GetJavaDexFile() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
1017 return holder_.GetJavaDexFile(idx_);
1018 }
1019 art::mirror::LongArray* GetNewDexFileCookie() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
1020 return holder_.GetNewDexFileCookie(idx_);
1021 }
1022 art::mirror::DexCache* GetNewDexCache() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
1023 return holder_.GetNewDexCache(idx_);
1024 }
1025 art::mirror::Class* GetMirrorClass() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
1026 return holder_.GetMirrorClass(idx_);
1027 }
Alex Light2f814aa2017-03-24 15:21:34 +00001028 art::mirror::Object* GetOriginalDexFile() const
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001029 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light2f814aa2017-03-24 15:21:34 +00001030 return holder_.GetOriginalDexFile(idx_);
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001031 }
Alex Light1e3926a2017-04-07 10:38:06 -07001032 art::mirror::PointerArray* GetOldObsoleteMethods() const
1033 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1034 return holder_.GetOldObsoleteMethods(idx_);
1035 }
1036 art::mirror::ObjectArray<art::mirror::DexCache>* GetOldDexCaches() const
1037 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1038 return holder_.GetOldDexCaches(idx_);
1039 }
1040
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001041 int32_t GetIndex() const {
1042 return idx_;
1043 }
1044
1045 void SetSourceClassLoader(art::mirror::ClassLoader* loader)
1046 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1047 holder_.SetSourceClassLoader(idx_, loader);
1048 }
1049 void SetJavaDexFile(art::mirror::Object* dexfile) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1050 holder_.SetJavaDexFile(idx_, dexfile);
1051 }
1052 void SetNewDexFileCookie(art::mirror::LongArray* cookie)
1053 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1054 holder_.SetNewDexFileCookie(idx_, cookie);
1055 }
1056 void SetNewDexCache(art::mirror::DexCache* cache) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1057 holder_.SetNewDexCache(idx_, cache);
1058 }
1059 void SetMirrorClass(art::mirror::Class* klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1060 holder_.SetMirrorClass(idx_, klass);
1061 }
Alex Light2f814aa2017-03-24 15:21:34 +00001062 void SetOriginalDexFile(art::mirror::Object* bytes)
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001063 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light2f814aa2017-03-24 15:21:34 +00001064 holder_.SetOriginalDexFile(idx_, bytes);
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001065 }
Alex Light1e3926a2017-04-07 10:38:06 -07001066 void SetOldObsoleteMethods(art::mirror::PointerArray* methods)
1067 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1068 holder_.SetOldObsoleteMethods(idx_, methods);
1069 }
1070 void SetOldDexCaches(art::mirror::ObjectArray<art::mirror::DexCache>* caches)
1071 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1072 holder_.SetOldDexCaches(idx_, caches);
1073 }
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001074
1075 private:
1076 int32_t idx_;
1077 RedefinitionDataHolder& holder_;
1078};
1079
1080RedefinitionDataIter RedefinitionDataHolder::begin() {
1081 return RedefinitionDataIter(0, *this);
1082}
1083
1084RedefinitionDataIter RedefinitionDataHolder::end() {
1085 return RedefinitionDataIter(Length(), *this);
1086}
1087
1088bool Redefiner::ClassRedefinition::CheckVerification(const RedefinitionDataIter& iter) {
Alex Light8c889d22017-02-06 13:58:27 -08001089 DCHECK_EQ(dex_file_->NumClassDefs(), 1u);
1090 art::StackHandleScope<2> hs(driver_->self_);
1091 std::string error;
1092 // TODO Make verification log level lower
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001093 art::verifier::FailureKind failure =
Alex Light8c889d22017-02-06 13:58:27 -08001094 art::verifier::MethodVerifier::VerifyClass(driver_->self_,
1095 dex_file_.get(),
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001096 hs.NewHandle(iter.GetNewDexCache()),
Alex Light8c889d22017-02-06 13:58:27 -08001097 hs.NewHandle(GetClassLoader()),
1098 dex_file_->GetClassDef(0), /*class_def*/
1099 nullptr, /*compiler_callbacks*/
Alex Light53330612017-10-04 15:29:53 -07001100 true, /*allow_soft_failures*/
Alex Light8c889d22017-02-06 13:58:27 -08001101 /*log_level*/
1102 art::verifier::HardFailLogMode::kLogWarning,
1103 &error);
Alex Light53330612017-10-04 15:29:53 -07001104 switch (failure) {
1105 case art::verifier::FailureKind::kNoFailure:
1106 case art::verifier::FailureKind::kSoftFailure:
1107 return true;
1108 case art::verifier::FailureKind::kHardFailure: {
1109 RecordFailure(ERR(FAILS_VERIFICATION), "Failed to verify class. Error was: " + error);
1110 return false;
1111 }
Alex Light8c889d22017-02-06 13:58:27 -08001112 }
Alex Light8c889d22017-02-06 13:58:27 -08001113}
1114
Alex Light1babae02017-02-01 15:35:34 -08001115// Looks through the previously allocated cookies to see if we need to update them with another new
1116// dexfile. This is so that even if multiple classes with the same classloader are redefined at
1117// once they are all added to the classloader.
1118bool Redefiner::ClassRedefinition::AllocateAndRememberNewDexFileCookie(
Alex Light1babae02017-02-01 15:35:34 -08001119 art::Handle<art::mirror::ClassLoader> source_class_loader,
1120 art::Handle<art::mirror::Object> dex_file_obj,
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001121 /*out*/RedefinitionDataIter* cur_data) {
Alex Light1babae02017-02-01 15:35:34 -08001122 art::StackHandleScope<2> hs(driver_->self_);
1123 art::MutableHandle<art::mirror::LongArray> old_cookie(
1124 hs.NewHandle<art::mirror::LongArray>(nullptr));
1125 bool has_older_cookie = false;
1126 // See if we already have a cookie that a previous redefinition got from the same classloader.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001127 for (auto old_data = cur_data->GetHolder().begin(); old_data != *cur_data; ++old_data) {
1128 if (old_data.GetSourceClassLoader() == source_class_loader.Get()) {
Alex Light1babae02017-02-01 15:35:34 -08001129 // Since every instance of this classloader should have the same cookie associated with it we
1130 // can stop looking here.
1131 has_older_cookie = true;
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001132 old_cookie.Assign(old_data.GetNewDexFileCookie());
Alex Light1babae02017-02-01 15:35:34 -08001133 break;
1134 }
1135 }
1136 if (old_cookie.IsNull()) {
1137 // No older cookie. Get it directly from the dex_file_obj
1138 // We should not have seen this classloader elsewhere.
1139 CHECK(!has_older_cookie);
1140 old_cookie.Assign(ClassLoaderHelper::GetDexFileCookie(dex_file_obj));
1141 }
1142 // Use the old cookie to generate the new one with the new DexFile* added in.
1143 art::Handle<art::mirror::LongArray>
1144 new_cookie(hs.NewHandle(ClassLoaderHelper::AllocateNewDexFileCookie(driver_->self_,
1145 old_cookie,
1146 dex_file_.get())));
1147 // Make sure the allocation worked.
1148 if (new_cookie.IsNull()) {
1149 return false;
1150 }
1151
1152 // Save the cookie.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001153 cur_data->SetNewDexFileCookie(new_cookie.Get());
Alex Light1babae02017-02-01 15:35:34 -08001154 // If there are other copies of this same classloader we need to make sure that we all have the
1155 // same cookie.
1156 if (has_older_cookie) {
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001157 for (auto old_data = cur_data->GetHolder().begin(); old_data != *cur_data; ++old_data) {
Alex Light1babae02017-02-01 15:35:34 -08001158 // We will let the GC take care of the cookie we allocated for this one.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001159 if (old_data.GetSourceClassLoader() == source_class_loader.Get()) {
1160 old_data.SetNewDexFileCookie(new_cookie.Get());
Alex Light1babae02017-02-01 15:35:34 -08001161 }
1162 }
1163 }
1164
1165 return true;
1166}
1167
Alex Lighta7e38d82017-01-19 14:57:28 -08001168bool Redefiner::ClassRedefinition::FinishRemainingAllocations(
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001169 /*out*/RedefinitionDataIter* cur_data) {
Alex Light7916f202017-01-27 09:00:15 -08001170 art::ScopedObjectAccessUnchecked soa(driver_->self_);
Alex Lighta7e38d82017-01-19 14:57:28 -08001171 art::StackHandleScope<2> hs(driver_->self_);
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001172 cur_data->SetMirrorClass(GetMirrorClass());
Alex Lighta7e38d82017-01-19 14:57:28 -08001173 // This shouldn't allocate
1174 art::Handle<art::mirror::ClassLoader> loader(hs.NewHandle(GetClassLoader()));
Alex Light7916f202017-01-27 09:00:15 -08001175 // The bootclasspath is handled specially so it doesn't have a j.l.DexFile.
1176 if (!art::ClassLinker::IsBootClassLoader(soa, loader.Get())) {
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001177 cur_data->SetSourceClassLoader(loader.Get());
Alex Light7916f202017-01-27 09:00:15 -08001178 art::Handle<art::mirror::Object> dex_file_obj(hs.NewHandle(
1179 ClassLoaderHelper::FindSourceDexFileObject(driver_->self_, loader)));
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001180 cur_data->SetJavaDexFile(dex_file_obj.Get());
Andreas Gampefa4333d2017-02-14 11:10:34 -08001181 if (dex_file_obj == nullptr) {
Alex Light7916f202017-01-27 09:00:15 -08001182 RecordFailure(ERR(INTERNAL), "Unable to find dex file!");
1183 return false;
1184 }
Alex Light1babae02017-02-01 15:35:34 -08001185 // Allocate the new dex file cookie.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001186 if (!AllocateAndRememberNewDexFileCookie(loader, dex_file_obj, cur_data)) {
Alex Light7916f202017-01-27 09:00:15 -08001187 driver_->self_->AssertPendingOOMException();
1188 driver_->self_->ClearException();
1189 RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate dex file array for class loader");
1190 return false;
1191 }
Alex Lighta7e38d82017-01-19 14:57:28 -08001192 }
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001193 cur_data->SetNewDexCache(CreateNewDexCache(loader));
1194 if (cur_data->GetNewDexCache() == nullptr) {
Vladimir Markocd556b02017-02-03 11:47:34 +00001195 driver_->self_->AssertPendingException();
Alex Lighta7e38d82017-01-19 14:57:28 -08001196 driver_->self_->ClearException();
1197 RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate DexCache");
1198 return false;
1199 }
1200
1201 // We won't always need to set this field.
Alex Light2f814aa2017-03-24 15:21:34 +00001202 cur_data->SetOriginalDexFile(AllocateOrGetOriginalDexFile());
1203 if (cur_data->GetOriginalDexFile() == nullptr) {
Alex Lighta7e38d82017-01-19 14:57:28 -08001204 driver_->self_->AssertPendingOOMException();
1205 driver_->self_->ClearException();
1206 RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate array for original dex file");
1207 return false;
1208 }
1209 return true;
1210}
1211
Alex Lighta26e3492017-06-27 17:55:37 -07001212void Redefiner::ClassRedefinition::UnregisterJvmtiBreakpoints() {
1213 BreakpointUtil::RemoveBreakpointsInClass(driver_->env_, GetMirrorClass());
1214}
1215
Alex Light5643caf2017-02-08 11:39:07 -08001216void Redefiner::ClassRedefinition::UnregisterBreakpoints() {
1217 DCHECK(art::Dbg::IsDebuggerActive());
1218 art::JDWP::JdwpState* state = art::Dbg::GetJdwpState();
1219 if (state != nullptr) {
1220 state->UnregisterLocationEventsOnClass(GetMirrorClass());
1221 }
1222}
1223
1224void Redefiner::UnregisterAllBreakpoints() {
1225 if (LIKELY(!art::Dbg::IsDebuggerActive())) {
1226 return;
1227 }
1228 for (Redefiner::ClassRedefinition& redef : redefinitions_) {
1229 redef.UnregisterBreakpoints();
1230 }
1231}
1232
Alex Light0e692732017-01-10 15:00:05 -08001233bool Redefiner::CheckAllRedefinitionAreValid() {
1234 for (Redefiner::ClassRedefinition& redef : redefinitions_) {
1235 if (!redef.CheckRedefinitionIsValid()) {
1236 return false;
1237 }
1238 }
1239 return true;
1240}
1241
Alex Light1e3926a2017-04-07 10:38:06 -07001242void Redefiner::RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder) {
1243 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
1244 data.GetRedefinition().RestoreObsoleteMethodMapsIfUnneeded(&data);
1245 }
1246}
1247
1248bool Redefiner::EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder) {
1249 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
1250 if (!data.GetRedefinition().EnsureClassAllocationsFinished(&data)) {
Alex Light0e692732017-01-10 15:00:05 -08001251 return false;
1252 }
1253 }
1254 return true;
1255}
1256
1257bool Redefiner::FinishAllRemainingAllocations(RedefinitionDataHolder& holder) {
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001258 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
Alex Light0e692732017-01-10 15:00:05 -08001259 // Allocate the data this redefinition requires.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001260 if (!data.GetRedefinition().FinishRemainingAllocations(&data)) {
Alex Light0e692732017-01-10 15:00:05 -08001261 return false;
1262 }
Alex Light0e692732017-01-10 15:00:05 -08001263 }
1264 return true;
1265}
1266
1267void Redefiner::ClassRedefinition::ReleaseDexFile() {
1268 dex_file_.release();
1269}
1270
1271void Redefiner::ReleaseAllDexFiles() {
1272 for (Redefiner::ClassRedefinition& redef : redefinitions_) {
1273 redef.ReleaseDexFile();
1274 }
1275}
1276
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001277bool Redefiner::CheckAllClassesAreVerified(RedefinitionDataHolder& holder) {
1278 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
1279 if (!data.GetRedefinition().CheckVerification(data)) {
Alex Light8c889d22017-02-06 13:58:27 -08001280 return false;
1281 }
Alex Light8c889d22017-02-06 13:58:27 -08001282 }
1283 return true;
1284}
1285
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001286class ScopedDisableConcurrentAndMovingGc {
1287 public:
1288 ScopedDisableConcurrentAndMovingGc(art::gc::Heap* heap, art::Thread* self)
1289 : heap_(heap), self_(self) {
1290 if (heap_->IsGcConcurrentAndMoving()) {
1291 heap_->IncrementDisableMovingGC(self_);
1292 }
1293 }
1294
1295 ~ScopedDisableConcurrentAndMovingGc() {
1296 if (heap_->IsGcConcurrentAndMoving()) {
1297 heap_->DecrementDisableMovingGC(self_);
1298 }
1299 }
1300 private:
1301 art::gc::Heap* heap_;
1302 art::Thread* self_;
1303};
1304
Alex Lighta01de592016-11-15 10:43:06 -08001305jvmtiError Redefiner::Run() {
Alex Light0e692732017-01-10 15:00:05 -08001306 art::StackHandleScope<1> hs(self_);
1307 // Allocate an array to hold onto all java temporary objects associated with this redefinition.
1308 // We will let this be collected after the end of this function.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001309 RedefinitionDataHolder holder(&hs, runtime_, self_, &redefinitions_);
Alex Light0e692732017-01-10 15:00:05 -08001310 if (holder.IsNull()) {
1311 self_->AssertPendingOOMException();
1312 self_->ClearException();
1313 RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate storage for temporaries");
1314 return result_;
1315 }
1316
Alex Lighta01de592016-11-15 10:43:06 -08001317 // First we just allocate the ClassExt and its fields that we need. These can be updated
1318 // atomically without any issues (since we allocate the map arrays as empty) so we don't bother
1319 // doing a try loop. The other allocations we need to ensure that nothing has changed in the time
1320 // between allocating them and pausing all threads before we can update them so we need to do a
1321 // try loop.
Alex Light0e692732017-01-10 15:00:05 -08001322 if (!CheckAllRedefinitionAreValid() ||
Alex Light1e3926a2017-04-07 10:38:06 -07001323 !EnsureAllClassAllocationsFinished(holder) ||
Alex Light8c889d22017-02-06 13:58:27 -08001324 !FinishAllRemainingAllocations(holder) ||
1325 !CheckAllClassesAreVerified(holder)) {
Alex Lighta01de592016-11-15 10:43:06 -08001326 return result_;
1327 }
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001328
Alex Light5643caf2017-02-08 11:39:07 -08001329 // At this point we can no longer fail without corrupting the runtime state.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001330 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
Alex Light07f06212017-06-01 14:01:43 -07001331 art::ClassLinker* cl = runtime_->GetClassLinker();
1332 cl->RegisterExistingDexCache(data.GetNewDexCache(), data.GetSourceClassLoader());
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001333 if (data.GetSourceClassLoader() == nullptr) {
Alex Light07f06212017-06-01 14:01:43 -07001334 cl->AppendToBootClassPath(self_, data.GetRedefinition().GetDexFile());
Alex Light7916f202017-01-27 09:00:15 -08001335 }
Alex Light7916f202017-01-27 09:00:15 -08001336 }
Alex Light5643caf2017-02-08 11:39:07 -08001337 UnregisterAllBreakpoints();
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001338
Alex Light6abd5392017-01-05 17:53:00 -08001339 // Disable GC and wait for it to be done if we are a moving GC. This is fine since we are done
1340 // allocating so no deadlocks.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001341 ScopedDisableConcurrentAndMovingGc sdcamgc(runtime_->GetHeap(), self_);
1342
Alex Lighta01de592016-11-15 10:43:06 -08001343 // Do transition to final suspension
1344 // TODO We might want to give this its own suspended state!
1345 // TODO This isn't right. We need to change state without any chance of suspend ideally!
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001346 art::ScopedThreadSuspension sts(self_, art::ThreadState::kNative);
1347 art::ScopedSuspendAll ssa("Final installation of redefined Classes!", /*long_suspend*/true);
1348 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
Alex Lighteb98b082017-01-25 13:02:32 -08001349 art::ScopedAssertNoThreadSuspension nts("Updating runtime objects for redefinition");
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001350 ClassRedefinition& redef = data.GetRedefinition();
1351 if (data.GetSourceClassLoader() != nullptr) {
1352 ClassLoaderHelper::UpdateJavaDexFile(data.GetJavaDexFile(), data.GetNewDexFileCookie());
Alex Light7916f202017-01-27 09:00:15 -08001353 }
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001354 art::mirror::Class* klass = data.GetMirrorClass();
Alex Light0e692732017-01-10 15:00:05 -08001355 // TODO Rewrite so we don't do a stack walk for each and every class.
1356 redef.FindAndAllocateObsoleteMethods(klass);
Alex Light2f814aa2017-03-24 15:21:34 +00001357 redef.UpdateClass(klass, data.GetNewDexCache(), data.GetOriginalDexFile());
Alex Lighta26e3492017-06-27 17:55:37 -07001358 redef.UnregisterJvmtiBreakpoints();
Alex Light0e692732017-01-10 15:00:05 -08001359 }
Alex Light1e3926a2017-04-07 10:38:06 -07001360 RestoreObsoleteMethodMapsIfUnneeded(holder);
Alex Light7532d582017-02-13 16:36:06 -08001361 // TODO We should check for if any of the redefined methods are intrinsic methods here and, if any
1362 // are, force a full-world deoptimization before finishing redefinition. If we don't do this then
1363 // methods that have been jitted prior to the current redefinition being applied might continue
1364 // to use the old versions of the intrinsics!
Alex Light0e692732017-01-10 15:00:05 -08001365 // TODO Do the dex_file release at a more reasonable place. This works but it muddles who really
1366 // owns the DexFile and when ownership is transferred.
1367 ReleaseAllDexFiles();
Alex Lighta01de592016-11-15 10:43:06 -08001368 return OK;
1369}
1370
Alex Light0e692732017-01-10 15:00:05 -08001371void Redefiner::ClassRedefinition::UpdateMethods(art::ObjPtr<art::mirror::Class> mclass,
Alex Light0e692732017-01-10 15:00:05 -08001372 const art::DexFile::ClassDef& class_def) {
1373 art::ClassLinker* linker = driver_->runtime_->GetClassLinker();
Alex Lighta01de592016-11-15 10:43:06 -08001374 art::PointerSize image_pointer_size = linker->GetImagePointerSize();
Alex Light200b9d72016-12-15 11:34:13 -08001375 const art::DexFile::TypeId& declaring_class_id = dex_file_->GetTypeId(class_def.class_idx_);
Alex Lighta01de592016-11-15 10:43:06 -08001376 const art::DexFile& old_dex_file = mclass->GetDexFile();
Alex Light200b9d72016-12-15 11:34:13 -08001377 // Update methods.
Alex Light00e475c2017-07-19 16:36:23 -07001378 for (art::ArtMethod& method : mclass->GetDeclaredMethods(image_pointer_size)) {
Alex Lighta01de592016-11-15 10:43:06 -08001379 const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(method.GetName());
1380 art::dex::TypeIndex method_return_idx =
1381 dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(method.GetReturnTypeDescriptor()));
1382 const auto* old_type_list = method.GetParameterTypeList();
1383 std::vector<art::dex::TypeIndex> new_type_list;
1384 for (uint32_t i = 0; old_type_list != nullptr && i < old_type_list->Size(); i++) {
1385 new_type_list.push_back(
1386 dex_file_->GetIndexForTypeId(
1387 *dex_file_->FindTypeId(
1388 old_dex_file.GetTypeDescriptor(
1389 old_dex_file.GetTypeId(
1390 old_type_list->GetTypeItem(i).type_idx_)))));
1391 }
1392 const art::DexFile::ProtoId* proto_id = dex_file_->FindProtoId(method_return_idx,
1393 new_type_list);
Alex Lightdba61482016-12-21 08:20:29 -08001394 CHECK(proto_id != nullptr || old_type_list == nullptr);
Alex Lighta01de592016-11-15 10:43:06 -08001395 const art::DexFile::MethodId* method_id = dex_file_->FindMethodId(declaring_class_id,
1396 *new_name_id,
1397 *proto_id);
Alex Lightdba61482016-12-21 08:20:29 -08001398 CHECK(method_id != nullptr);
Alex Lighta01de592016-11-15 10:43:06 -08001399 uint32_t dex_method_idx = dex_file_->GetIndexForMethodId(*method_id);
1400 method.SetDexMethodIndex(dex_method_idx);
1401 linker->SetEntryPointsToInterpreter(&method);
Alex Light200b9d72016-12-15 11:34:13 -08001402 method.SetCodeItemOffset(dex_file_->FindCodeItemOffset(class_def, dex_method_idx));
Alex Light7532d582017-02-13 16:36:06 -08001403 // Clear all the intrinsics related flags.
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +01001404 method.SetNotIntrinsic();
Alex Lightdba61482016-12-21 08:20:29 -08001405 // Notify the jit that this method is redefined.
Alex Light0e692732017-01-10 15:00:05 -08001406 art::jit::Jit* jit = driver_->runtime_->GetJit();
Alex Lightabbc4bc2017-10-05 17:07:40 -07001407 // Non-invokable methods don't have any JIT data associated with them so we don't need to tell
1408 // the jit about them.
1409 if (jit != nullptr && method.IsInvokable()) {
Alex Lightdba61482016-12-21 08:20:29 -08001410 jit->GetCodeCache()->NotifyMethodRedefined(&method);
1411 }
Alex Lighta01de592016-11-15 10:43:06 -08001412 }
Alex Light200b9d72016-12-15 11:34:13 -08001413}
1414
Alex Light0e692732017-01-10 15:00:05 -08001415void Redefiner::ClassRedefinition::UpdateFields(art::ObjPtr<art::mirror::Class> mclass) {
Alex Light200b9d72016-12-15 11:34:13 -08001416 // TODO The IFields & SFields pointers should be combined like the methods_ arrays were.
1417 for (auto fields_iter : {mclass->GetIFields(), mclass->GetSFields()}) {
1418 for (art::ArtField& field : fields_iter) {
1419 std::string declaring_class_name;
1420 const art::DexFile::TypeId* new_declaring_id =
1421 dex_file_->FindTypeId(field.GetDeclaringClass()->GetDescriptor(&declaring_class_name));
1422 const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(field.GetName());
1423 const art::DexFile::TypeId* new_type_id = dex_file_->FindTypeId(field.GetTypeDescriptor());
Alex Light200b9d72016-12-15 11:34:13 -08001424 CHECK(new_name_id != nullptr && new_type_id != nullptr && new_declaring_id != nullptr);
1425 const art::DexFile::FieldId* new_field_id =
1426 dex_file_->FindFieldId(*new_declaring_id, *new_name_id, *new_type_id);
1427 CHECK(new_field_id != nullptr);
1428 // We only need to update the index since the other data in the ArtField cannot be updated.
1429 field.SetDexFieldIndex(dex_file_->GetIndexForFieldId(*new_field_id));
1430 }
1431 }
Alex Light200b9d72016-12-15 11:34:13 -08001432}
1433
1434// Performs updates to class that will allow us to verify it.
Alex Lighta7e38d82017-01-19 14:57:28 -08001435void Redefiner::ClassRedefinition::UpdateClass(
1436 art::ObjPtr<art::mirror::Class> mclass,
1437 art::ObjPtr<art::mirror::DexCache> new_dex_cache,
Alex Light2f814aa2017-03-24 15:21:34 +00001438 art::ObjPtr<art::mirror::Object> original_dex_file) {
Alex Light6ac57502017-01-19 15:05:06 -08001439 DCHECK_EQ(dex_file_->NumClassDefs(), 1u);
1440 const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(0);
Vladimir Marko5122e6b2017-08-17 16:10:09 +01001441 UpdateMethods(mclass, class_def);
Alex Light007ada22017-01-10 13:33:56 -08001442 UpdateFields(mclass);
Alex Light200b9d72016-12-15 11:34:13 -08001443
Alex Lighta01de592016-11-15 10:43:06 -08001444 // Update the class fields.
1445 // Need to update class last since the ArtMethod gets its DexFile from the class (which is needed
1446 // to call GetReturnTypeDescriptor and GetParameterTypeList above).
1447 mclass->SetDexCache(new_dex_cache.Ptr());
Alex Light6ac57502017-01-19 15:05:06 -08001448 mclass->SetDexClassDefIndex(dex_file_->GetIndexForClassDef(class_def));
Alex Light0e692732017-01-10 15:00:05 -08001449 mclass->SetDexTypeIndex(dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(class_sig_.c_str())));
Alex Lighta7e38d82017-01-19 14:57:28 -08001450 art::ObjPtr<art::mirror::ClassExt> ext(mclass->GetExtData());
1451 CHECK(!ext.IsNull());
Alex Light2f814aa2017-03-24 15:21:34 +00001452 ext->SetOriginalDexFile(original_dex_file);
Alex Lighta01de592016-11-15 10:43:06 -08001453}
1454
Alex Light1e3926a2017-04-07 10:38:06 -07001455// Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no new
1456// obsolete methods).
1457void Redefiner::ClassRedefinition::RestoreObsoleteMethodMapsIfUnneeded(
1458 const RedefinitionDataIter* cur_data) {
1459 art::mirror::Class* klass = GetMirrorClass();
1460 art::mirror::ClassExt* ext = klass->GetExtData();
1461 art::mirror::PointerArray* methods = ext->GetObsoleteMethods();
Alex Light70713df2017-04-18 13:03:31 -07001462 art::mirror::PointerArray* old_methods = cur_data->GetOldObsoleteMethods();
1463 int32_t old_length = old_methods == nullptr ? 0 : old_methods->GetLength();
Alex Light1e3926a2017-04-07 10:38:06 -07001464 int32_t expected_length =
1465 old_length + klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods();
1466 // Check to make sure we are only undoing this one.
1467 if (expected_length == methods->GetLength()) {
Alex Light70713df2017-04-18 13:03:31 -07001468 for (int32_t i = 0; i < expected_length; i++) {
1469 art::ArtMethod* expected = nullptr;
1470 if (i < old_length) {
1471 expected = old_methods->GetElementPtrSize<art::ArtMethod*>(i, art::kRuntimePointerSize);
1472 }
1473 if (methods->GetElementPtrSize<art::ArtMethod*>(i, art::kRuntimePointerSize) != expected) {
Alex Light1e3926a2017-04-07 10:38:06 -07001474 // We actually have some new obsolete methods. Just abort since we cannot safely shrink the
1475 // obsolete methods array.
1476 return;
1477 }
1478 }
1479 // No new obsolete methods! We can get rid of the maps.
1480 ext->SetObsoleteArrays(cur_data->GetOldObsoleteMethods(), cur_data->GetOldDexCaches());
1481 }
1482}
1483
Alex Lighta01de592016-11-15 10:43:06 -08001484// This function does all (java) allocations we need to do for the Class being redefined.
1485// TODO Change this name maybe?
Alex Light1e3926a2017-04-07 10:38:06 -07001486bool Redefiner::ClassRedefinition::EnsureClassAllocationsFinished(
1487 /*out*/RedefinitionDataIter* cur_data) {
Alex Light0e692732017-01-10 15:00:05 -08001488 art::StackHandleScope<2> hs(driver_->self_);
1489 art::Handle<art::mirror::Class> klass(hs.NewHandle(
1490 driver_->self_->DecodeJObject(klass_)->AsClass()));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001491 if (klass == nullptr) {
Alex Lighta01de592016-11-15 10:43:06 -08001492 RecordFailure(ERR(INVALID_CLASS), "Unable to decode class argument!");
1493 return false;
1494 }
1495 // Allocate the classExt
Alex Light0e692732017-01-10 15:00:05 -08001496 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(driver_->self_)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001497 if (ext == nullptr) {
Alex Lighta01de592016-11-15 10:43:06 -08001498 // No memory. Clear exception (it's not useful) and return error.
Alex Light0e692732017-01-10 15:00:05 -08001499 driver_->self_->AssertPendingOOMException();
1500 driver_->self_->ClearException();
Alex Lighta01de592016-11-15 10:43:06 -08001501 RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate ClassExt");
1502 return false;
1503 }
Alex Light1e3926a2017-04-07 10:38:06 -07001504 // First save the old values of the 2 arrays that make up the obsolete methods maps. Then
1505 // allocate the 2 arrays that make up the obsolete methods map. Since the contents of the arrays
Alex Lighta01de592016-11-15 10:43:06 -08001506 // are only modified when all threads (other than the modifying one) are suspended we don't need
1507 // to worry about missing the unsyncronized writes to the array. We do synchronize when setting it
1508 // however, since that can happen at any time.
Alex Light1e3926a2017-04-07 10:38:06 -07001509 cur_data->SetOldObsoleteMethods(ext->GetObsoleteMethods());
1510 cur_data->SetOldDexCaches(ext->GetObsoleteDexCaches());
1511 if (!ext->ExtendObsoleteArrays(
1512 driver_->self_, klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size())) {
1513 // OOM. Clear exception and return error.
1514 driver_->self_->AssertPendingOOMException();
1515 driver_->self_->ClearException();
1516 RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate/extend obsolete methods map");
1517 return false;
Alex Lighta01de592016-11-15 10:43:06 -08001518 }
1519 return true;
1520}
1521
1522} // namespace openjdkjvmti