blob: c3fb946b9acdc69635a2f971402155cc18c4811d [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 Gampe57943812017-12-06 21:39:13 -080036#include <android-base/logging.h>
37#include <android-base/stringprintf.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080038
Andreas Gampea1d2f952017-04-20 22:53:58 -070039#include "art_field-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080040#include "art_jvmti.h"
Steven Morelande431e272017-07-18 16:53:49 -070041#include "art_method-inl.h"
Vladimir Markoe1993c72017-06-14 17:01:38 +010042#include "base/array_ref.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"
David Sehr013fd802018-01-11 22:55:24 -080046#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080047#include "dex/dex_file.h"
48#include "dex/dex_file_loader.h"
49#include "dex/dex_file_types.h"
Alex Lighta01de592016-11-15 10:43:06 -080050#include "events-inl.h"
51#include "gc/allocation_listener.h"
Alex Light6abd5392017-01-05 17:53:00 -080052#include "gc/heap.h"
Alex Lighta01de592016-11-15 10:43:06 -080053#include "instrumentation.h"
Alex Light07f06212017-06-01 14:01:43 -070054#include "intern_table.h"
Alex Light5643caf2017-02-08 11:39:07 -080055#include "jdwp/jdwp.h"
56#include "jdwp/jdwp_constants.h"
57#include "jdwp/jdwp_event.h"
58#include "jdwp/object_registry.h"
Alex Lightdba61482016-12-21 08:20:29 -080059#include "jit/jit.h"
60#include "jit/jit_code_cache.h"
Alex Lighta01de592016-11-15 10:43:06 -080061#include "jni_env_ext-inl.h"
62#include "jvmti_allocator.h"
Alex Light6161f132017-01-25 10:30:20 -080063#include "mirror/class-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080064#include "mirror/class_ext.h"
65#include "mirror/object.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070066#include "nativehelper/scoped_local_ref.h"
Alex Lighte77b48b2017-02-22 11:08:06 -080067#include "non_debuggable_classes.h"
Alex Lighta01de592016-11-15 10:43:06 -080068#include "object_lock.h"
69#include "runtime.h"
Alex Lighta26e3492017-06-27 17:55:37 -070070#include "ti_breakpoint.h"
Alex Lighteb98b082017-01-25 13:02:32 -080071#include "ti_class_loader.h"
Alex Light0e692732017-01-10 15:00:05 -080072#include "transform.h"
Alex Light8c889d22017-02-06 13:58:27 -080073#include "verifier/method_verifier.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070074#include "verifier/verifier_enums.h"
Alex Lighta01de592016-11-15 10:43:06 -080075
76namespace openjdkjvmti {
77
Andreas Gampe46ee31b2016-12-14 10:11:49 -080078using android::base::StringPrintf;
79
Alex Lighteee0bd42017-02-14 15:31:45 +000080// A helper that fills in a classes obsolete_methods_ and obsolete_dex_caches_ classExt fields as
81// they are created. This ensures that we can always call any method of an obsolete ArtMethod object
82// almost as soon as they are created since the GetObsoleteDexCache method will succeed.
83class ObsoleteMap {
84 public:
85 art::ArtMethod* FindObsoleteVersion(art::ArtMethod* original)
86 REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) {
87 auto method_pair = id_map_.find(original);
88 if (method_pair != id_map_.end()) {
89 art::ArtMethod* res = obsolete_methods_->GetElementPtrSize<art::ArtMethod*>(
90 method_pair->second, art::kRuntimePointerSize);
91 DCHECK(res != nullptr);
92 DCHECK_EQ(original, res->GetNonObsoleteMethod());
93 return res;
94 } else {
95 return nullptr;
96 }
97 }
98
99 void RecordObsolete(art::ArtMethod* original, art::ArtMethod* obsolete)
100 REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) {
101 DCHECK(original != nullptr);
102 DCHECK(obsolete != nullptr);
103 int32_t slot = next_free_slot_++;
104 DCHECK_LT(slot, obsolete_methods_->GetLength());
105 DCHECK(nullptr ==
106 obsolete_methods_->GetElementPtrSize<art::ArtMethod*>(slot, art::kRuntimePointerSize));
107 DCHECK(nullptr == obsolete_dex_caches_->Get(slot));
108 obsolete_methods_->SetElementPtrSize(slot, obsolete, art::kRuntimePointerSize);
109 obsolete_dex_caches_->Set(slot, original_dex_cache_);
110 id_map_.insert({original, slot});
111 }
112
113 ObsoleteMap(art::ObjPtr<art::mirror::PointerArray> obsolete_methods,
114 art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> obsolete_dex_caches,
115 art::ObjPtr<art::mirror::DexCache> original_dex_cache)
116 : next_free_slot_(0),
117 obsolete_methods_(obsolete_methods),
118 obsolete_dex_caches_(obsolete_dex_caches),
119 original_dex_cache_(original_dex_cache) {
120 // Figure out where the first unused slot in the obsolete_methods_ array is.
121 while (obsolete_methods_->GetElementPtrSize<art::ArtMethod*>(
122 next_free_slot_, art::kRuntimePointerSize) != nullptr) {
123 DCHECK(obsolete_dex_caches_->Get(next_free_slot_) != nullptr);
124 next_free_slot_++;
125 }
126 // Sanity check that the same slot in obsolete_dex_caches_ is free.
127 DCHECK(obsolete_dex_caches_->Get(next_free_slot_) == nullptr);
128 }
129
130 private:
131 int32_t next_free_slot_;
132 std::unordered_map<art::ArtMethod*, int32_t> id_map_;
133 // Pointers to the fields in mirror::ClassExt. These can be held as ObjPtr since this is only used
134 // when we have an exclusive mutator_lock_ (i.e. all threads are suspended).
135 art::ObjPtr<art::mirror::PointerArray> obsolete_methods_;
136 art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> obsolete_dex_caches_;
137 art::ObjPtr<art::mirror::DexCache> original_dex_cache_;
138};
139
Alex Lightdba61482016-12-21 08:20:29 -0800140// This visitor walks thread stacks and allocates and sets up the obsolete methods. It also does
141// some basic sanity checks that the obsolete method is sane.
142class ObsoleteMethodStackVisitor : public art::StackVisitor {
143 protected:
144 ObsoleteMethodStackVisitor(
145 art::Thread* thread,
146 art::LinearAlloc* allocator,
147 const std::unordered_set<art::ArtMethod*>& obsoleted_methods,
Alex Lighteee0bd42017-02-14 15:31:45 +0000148 ObsoleteMap* obsolete_maps)
Alex Lightdba61482016-12-21 08:20:29 -0800149 : StackVisitor(thread,
150 /*context*/nullptr,
151 StackVisitor::StackWalkKind::kIncludeInlinedFrames),
152 allocator_(allocator),
153 obsoleted_methods_(obsoleted_methods),
Alex Light4ba388a2017-01-27 10:26:49 -0800154 obsolete_maps_(obsolete_maps) { }
Alex Lightdba61482016-12-21 08:20:29 -0800155
156 ~ObsoleteMethodStackVisitor() OVERRIDE {}
157
158 public:
159 // Returns true if we successfully installed obsolete methods on this thread, filling
160 // obsolete_maps_ with the translations if needed. Returns false and fills error_msg if we fail.
161 // The stack is cleaned up when we fail.
Alex Light007ada22017-01-10 13:33:56 -0800162 static void UpdateObsoleteFrames(
Alex Lightdba61482016-12-21 08:20:29 -0800163 art::Thread* thread,
164 art::LinearAlloc* allocator,
165 const std::unordered_set<art::ArtMethod*>& obsoleted_methods,
Alex Lighteee0bd42017-02-14 15:31:45 +0000166 ObsoleteMap* obsolete_maps)
Alex Light007ada22017-01-10 13:33:56 -0800167 REQUIRES(art::Locks::mutator_lock_) {
Alex Lightdba61482016-12-21 08:20:29 -0800168 ObsoleteMethodStackVisitor visitor(thread,
169 allocator,
170 obsoleted_methods,
Alex Light007ada22017-01-10 13:33:56 -0800171 obsolete_maps);
Alex Lightdba61482016-12-21 08:20:29 -0800172 visitor.WalkStack();
Alex Lightdba61482016-12-21 08:20:29 -0800173 }
174
175 bool VisitFrame() OVERRIDE REQUIRES(art::Locks::mutator_lock_) {
Alex Lighteee0bd42017-02-14 15:31:45 +0000176 art::ScopedAssertNoThreadSuspension snts("Fixing up the stack for obsolete methods.");
Alex Lightdba61482016-12-21 08:20:29 -0800177 art::ArtMethod* old_method = GetMethod();
Alex Lightdba61482016-12-21 08:20:29 -0800178 if (obsoleted_methods_.find(old_method) != obsoleted_methods_.end()) {
Alex Lightdba61482016-12-21 08:20:29 -0800179 // We cannot ensure that the right dex file is used in inlined frames so we don't support
180 // redefining them.
181 DCHECK(!IsInInlinedFrame()) << "Inlined frames are not supported when using redefinition";
Alex Lighteee0bd42017-02-14 15:31:45 +0000182 art::ArtMethod* new_obsolete_method = obsolete_maps_->FindObsoleteVersion(old_method);
183 if (new_obsolete_method == nullptr) {
Alex Lightdba61482016-12-21 08:20:29 -0800184 // Create a new Obsolete Method and put it in the list.
185 art::Runtime* runtime = art::Runtime::Current();
186 art::ClassLinker* cl = runtime->GetClassLinker();
187 auto ptr_size = cl->GetImagePointerSize();
188 const size_t method_size = art::ArtMethod::Size(ptr_size);
Alex Light5c11a792017-03-10 14:29:22 -0800189 auto* method_storage = allocator_->Alloc(art::Thread::Current(), method_size);
Alex Light007ada22017-01-10 13:33:56 -0800190 CHECK(method_storage != nullptr) << "Unable to allocate storage for obsolete version of '"
191 << old_method->PrettyMethod() << "'";
Alex Lightdba61482016-12-21 08:20:29 -0800192 new_obsolete_method = new (method_storage) art::ArtMethod();
193 new_obsolete_method->CopyFrom(old_method, ptr_size);
194 DCHECK_EQ(new_obsolete_method->GetDeclaringClass(), old_method->GetDeclaringClass());
195 new_obsolete_method->SetIsObsolete();
Alex Lightfcbafb32017-02-02 15:09:54 -0800196 new_obsolete_method->SetDontCompile();
Alex Lightdb01a092017-04-03 15:39:55 -0700197 cl->SetEntryPointsForObsoleteMethod(new_obsolete_method);
Alex Lighteee0bd42017-02-14 15:31:45 +0000198 obsolete_maps_->RecordObsolete(old_method, new_obsolete_method);
Alex Lightdba61482016-12-21 08:20:29 -0800199 // Update JIT Data structures to point to the new method.
200 art::jit::Jit* jit = art::Runtime::Current()->GetJit();
201 if (jit != nullptr) {
202 // Notify the JIT we are making this obsolete method. It will update the jit's internal
203 // structures to keep track of the new obsolete method.
204 jit->GetCodeCache()->MoveObsoleteMethod(old_method, new_obsolete_method);
205 }
Alex Lightdba61482016-12-21 08:20:29 -0800206 }
207 DCHECK(new_obsolete_method != nullptr);
208 SetMethod(new_obsolete_method);
209 }
210 return true;
211 }
212
213 private:
214 // The linear allocator we should use to make new methods.
215 art::LinearAlloc* allocator_;
216 // The set of all methods which could be obsoleted.
217 const std::unordered_set<art::ArtMethod*>& obsoleted_methods_;
218 // A map from the original to the newly allocated obsolete method for frames on this thread. The
Alex Lighteee0bd42017-02-14 15:31:45 +0000219 // values in this map are added to the obsolete_methods_ (and obsolete_dex_caches_) fields of
220 // the redefined classes ClassExt as it is filled.
221 ObsoleteMap* obsolete_maps_;
Alex Lightdba61482016-12-21 08:20:29 -0800222};
223
Alex Lighte4a88632017-01-10 07:41:24 -0800224jvmtiError Redefiner::IsModifiableClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
225 jclass klass,
226 jboolean* is_redefinable) {
Alex Lighte4a88632017-01-10 07:41:24 -0800227 art::Thread* self = art::Thread::Current();
228 art::ScopedObjectAccess soa(self);
229 art::StackHandleScope<1> hs(self);
230 art::ObjPtr<art::mirror::Object> obj(self->DecodeJObject(klass));
231 if (obj.IsNull()) {
232 return ERR(INVALID_CLASS);
233 }
234 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(obj->AsClass()));
235 std::string err_unused;
236 *is_redefinable =
237 Redefiner::GetClassRedefinitionError(h_klass, &err_unused) == OK ? JNI_TRUE : JNI_FALSE;
238 return OK;
239}
240
241jvmtiError Redefiner::GetClassRedefinitionError(art::Handle<art::mirror::Class> klass,
242 /*out*/std::string* error_msg) {
243 if (klass->IsPrimitive()) {
244 *error_msg = "Modification of primitive classes is not supported";
245 return ERR(UNMODIFIABLE_CLASS);
246 } else if (klass->IsInterface()) {
247 *error_msg = "Modification of Interface classes is currently not supported";
248 return ERR(UNMODIFIABLE_CLASS);
Alex Light09f274f2017-02-21 15:00:48 -0800249 } else if (klass->IsStringClass()) {
250 *error_msg = "Modification of String class is not supported";
251 return ERR(UNMODIFIABLE_CLASS);
Alex Lighte4a88632017-01-10 07:41:24 -0800252 } else if (klass->IsArrayClass()) {
253 *error_msg = "Modification of Array classes is not supported";
254 return ERR(UNMODIFIABLE_CLASS);
255 } else if (klass->IsProxyClass()) {
256 *error_msg = "Modification of proxy classes is not supported";
257 return ERR(UNMODIFIABLE_CLASS);
258 }
259
Alex Lighte77b48b2017-02-22 11:08:06 -0800260 for (jclass c : art::NonDebuggableClasses::GetNonDebuggableClasses()) {
261 if (klass.Get() == art::Thread::Current()->DecodeJObject(c)->AsClass()) {
262 *error_msg = "Class might have stack frames that cannot be made obsolete";
263 return ERR(UNMODIFIABLE_CLASS);
264 }
265 }
266
Alex Lighte4a88632017-01-10 07:41:24 -0800267 return OK;
268}
269
Alex Lighta01de592016-11-15 10:43:06 -0800270// Moves dex data to an anonymous, read-only mmap'd region.
271std::unique_ptr<art::MemMap> Redefiner::MoveDataToMemMap(const std::string& original_location,
Vladimir Markoe1993c72017-06-14 17:01:38 +0100272 art::ArrayRef<const unsigned char> data,
Alex Lighta01de592016-11-15 10:43:06 -0800273 std::string* error_msg) {
274 std::unique_ptr<art::MemMap> map(art::MemMap::MapAnonymous(
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800275 StringPrintf("%s-transformed", original_location.c_str()).c_str(),
Alex Lighta01de592016-11-15 10:43:06 -0800276 nullptr,
Alex Lightb7354d52017-03-30 15:17:01 -0700277 data.size(),
Alex Lighta01de592016-11-15 10:43:06 -0800278 PROT_READ|PROT_WRITE,
279 /*low_4gb*/false,
280 /*reuse*/false,
281 error_msg));
282 if (map == nullptr) {
283 return map;
284 }
Vladimir Markoe1993c72017-06-14 17:01:38 +0100285 memcpy(map->Begin(), data.data(), data.size());
Alex Light0b772572016-12-02 17:27:31 -0800286 // Make the dex files mmap read only. This matches how other DexFiles are mmaped and prevents
287 // programs from corrupting it.
Alex Lighta01de592016-11-15 10:43:06 -0800288 map->Protect(PROT_READ);
289 return map;
290}
291
Alex Lighta7e38d82017-01-19 14:57:28 -0800292Redefiner::ClassRedefinition::ClassRedefinition(
293 Redefiner* driver,
294 jclass klass,
295 const art::DexFile* redefined_dex_file,
296 const char* class_sig,
Vladimir Markoe1993c72017-06-14 17:01:38 +0100297 art::ArrayRef<const unsigned char> orig_dex_file) :
Alex Lighta7e38d82017-01-19 14:57:28 -0800298 driver_(driver),
299 klass_(klass),
300 dex_file_(redefined_dex_file),
301 class_sig_(class_sig),
302 original_dex_file_(orig_dex_file) {
Alex Light0e692732017-01-10 15:00:05 -0800303 GetMirrorClass()->MonitorEnter(driver_->self_);
304}
305
306Redefiner::ClassRedefinition::~ClassRedefinition() {
307 if (driver_ != nullptr) {
308 GetMirrorClass()->MonitorExit(driver_->self_);
309 }
310}
311
Alex Light0e692732017-01-10 15:00:05 -0800312jvmtiError Redefiner::RedefineClasses(ArtJvmTiEnv* env,
Andreas Gampede19eb92017-02-24 16:21:18 -0800313 EventHandler* event_handler,
Alex Light0e692732017-01-10 15:00:05 -0800314 art::Runtime* runtime,
315 art::Thread* self,
316 jint class_count,
317 const jvmtiClassDefinition* definitions,
Alex Light6ac57502017-01-19 15:05:06 -0800318 /*out*/std::string* error_msg) {
Alex Light0e692732017-01-10 15:00:05 -0800319 if (env == nullptr) {
320 *error_msg = "env was null!";
321 return ERR(INVALID_ENVIRONMENT);
322 } else if (class_count < 0) {
323 *error_msg = "class_count was less then 0";
324 return ERR(ILLEGAL_ARGUMENT);
325 } else if (class_count == 0) {
326 // We don't actually need to do anything. Just return OK.
327 return OK;
328 } else if (definitions == nullptr) {
329 *error_msg = "null definitions!";
330 return ERR(NULL_POINTER);
331 }
Alex Light6ac57502017-01-19 15:05:06 -0800332 std::vector<ArtClassDefinition> def_vector;
333 def_vector.reserve(class_count);
334 for (jint i = 0; i < class_count; i++) {
Alex Lightce6ee702017-03-06 15:46:43 -0800335 jboolean is_modifiable = JNI_FALSE;
336 jvmtiError res = env->IsModifiableClass(definitions[i].klass, &is_modifiable);
337 if (res != OK) {
338 return res;
339 } else if (!is_modifiable) {
340 return ERR(UNMODIFIABLE_CLASS);
341 }
Alex Light6ac57502017-01-19 15:05:06 -0800342 // We make a copy of the class_bytes to pass into the retransformation.
343 // This makes cleanup easier (since we unambiguously own the bytes) and also is useful since we
344 // will need to keep the original bytes around unaltered for subsequent RetransformClasses calls
345 // to get the passed in bytes.
Alex Light6ac57502017-01-19 15:05:06 -0800346 unsigned char* class_bytes_copy = nullptr;
Alex Lightce6ee702017-03-06 15:46:43 -0800347 res = env->Allocate(definitions[i].class_byte_count, &class_bytes_copy);
Alex Light6ac57502017-01-19 15:05:06 -0800348 if (res != OK) {
349 return res;
350 }
351 memcpy(class_bytes_copy, definitions[i].class_bytes, definitions[i].class_byte_count);
352
353 ArtClassDefinition def;
Alex Light64e4c142018-01-30 13:46:37 -0800354 res = def.Init(self, definitions[i]);
Alex Light6ac57502017-01-19 15:05:06 -0800355 if (res != OK) {
356 return res;
357 }
358 def_vector.push_back(std::move(def));
359 }
360 // Call all the transformation events.
Alex Light64e4c142018-01-30 13:46:37 -0800361 jvmtiError res = Transformer::RetransformClassesDirect(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_;
David Sehr013fd802018-01-11 22:55:24 -0800429 const art::ArtDexFileLoader dex_file_loader;
430 std::unique_ptr<const art::DexFile> dex_file(dex_file_loader.Open(map->GetName(),
431 checksum,
432 std::move(map),
433 /*verify*/true,
434 /*verify_checksum*/true,
435 error_msg_));
Alex Lighta01de592016-11-15 10:43:06 -0800436 if (dex_file.get() == nullptr) {
Alex Lightb7354d52017-03-30 15:17:01 -0700437 os << "Unable to load modified dex file for " << def.GetName() << ": " << *error_msg_;
Alex Light0e692732017-01-10 15:00:05 -0800438 *error_msg_ = os.str();
Alex Lighta01de592016-11-15 10:43:06 -0800439 return ERR(INVALID_CLASS_FORMAT);
440 }
Alex Light0e692732017-01-10 15:00:05 -0800441 redefinitions_.push_back(
Alex Lighta7e38d82017-01-19 14:57:28 -0800442 Redefiner::ClassRedefinition(this,
Alex Lightb7354d52017-03-30 15:17:01 -0700443 def.GetClass(),
Alex Lighta7e38d82017-01-19 14:57:28 -0800444 dex_file.release(),
445 signature_ptr,
Alex Light40528472017-03-28 09:07:36 -0700446 def.GetNewOriginalDexFile()));
Alex Light0e692732017-01-10 15:00:05 -0800447 return OK;
Alex Lighta01de592016-11-15 10:43:06 -0800448}
449
Alex Light0e692732017-01-10 15:00:05 -0800450art::mirror::Class* Redefiner::ClassRedefinition::GetMirrorClass() {
451 return driver_->self_->DecodeJObject(klass_)->AsClass();
Alex Lighta01de592016-11-15 10:43:06 -0800452}
453
Alex Light0e692732017-01-10 15:00:05 -0800454art::mirror::ClassLoader* Redefiner::ClassRedefinition::GetClassLoader() {
Alex Lighta01de592016-11-15 10:43:06 -0800455 return GetMirrorClass()->GetClassLoader();
456}
457
Alex Light0e692732017-01-10 15:00:05 -0800458art::mirror::DexCache* Redefiner::ClassRedefinition::CreateNewDexCache(
459 art::Handle<art::mirror::ClassLoader> loader) {
Alex Light07f06212017-06-01 14:01:43 -0700460 art::StackHandleScope<2> hs(driver_->self_);
461 art::ClassLinker* cl = driver_->runtime_->GetClassLinker();
462 art::Handle<art::mirror::DexCache> cache(hs.NewHandle(
463 art::ObjPtr<art::mirror::DexCache>::DownCast(
464 cl->GetClassRoot(art::ClassLinker::kJavaLangDexCache)->AllocObject(driver_->self_))));
465 if (cache.IsNull()) {
466 driver_->self_->AssertPendingOOMException();
467 return nullptr;
468 }
469 art::Handle<art::mirror::String> location(hs.NewHandle(
470 cl->GetInternTable()->InternStrong(dex_file_->GetLocation().c_str())));
471 if (location.IsNull()) {
472 driver_->self_->AssertPendingOOMException();
473 return nullptr;
474 }
475 art::WriterMutexLock mu(driver_->self_, *art::Locks::dex_lock_);
476 art::mirror::DexCache::InitializeDexCache(driver_->self_,
477 cache.Get(),
478 location.Get(),
479 dex_file_.get(),
480 loader.IsNull() ? driver_->runtime_->GetLinearAlloc()
481 : loader->GetAllocator(),
482 art::kRuntimePointerSize);
483 return cache.Get();
Alex Lighta01de592016-11-15 10:43:06 -0800484}
485
Alex Light0e692732017-01-10 15:00:05 -0800486void Redefiner::RecordFailure(jvmtiError result,
487 const std::string& class_sig,
488 const std::string& error_msg) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800489 *error_msg_ = StringPrintf("Unable to perform redefinition of '%s': %s",
Alex Light0e692732017-01-10 15:00:05 -0800490 class_sig.c_str(),
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800491 error_msg.c_str());
Alex Lighta01de592016-11-15 10:43:06 -0800492 result_ = result;
493}
494
Alex Light2f814aa2017-03-24 15:21:34 +0000495art::mirror::Object* Redefiner::ClassRedefinition::AllocateOrGetOriginalDexFile() {
Alex Lighta7e38d82017-01-19 14:57:28 -0800496 // If we have been specifically given a new set of bytes use that
497 if (original_dex_file_.size() != 0) {
Alex Light440b5d92017-01-24 15:32:25 -0800498 return art::mirror::ByteArray::AllocateAndFill(
499 driver_->self_,
Vladimir Markoe1993c72017-06-14 17:01:38 +0100500 reinterpret_cast<const signed char*>(original_dex_file_.data()),
Alex Light440b5d92017-01-24 15:32:25 -0800501 original_dex_file_.size());
Alex Lighta01de592016-11-15 10:43:06 -0800502 }
Alex Lighta7e38d82017-01-19 14:57:28 -0800503
504 // See if we already have one set.
505 art::ObjPtr<art::mirror::ClassExt> ext(GetMirrorClass()->GetExtData());
506 if (!ext.IsNull()) {
Alex Light2f814aa2017-03-24 15:21:34 +0000507 art::ObjPtr<art::mirror::Object> old_original_dex_file(ext->GetOriginalDexFile());
508 if (!old_original_dex_file.IsNull()) {
Alex Lighta7e38d82017-01-19 14:57:28 -0800509 // We do. Use it.
Alex Light2f814aa2017-03-24 15:21:34 +0000510 return old_original_dex_file.Ptr();
Alex Lighta7e38d82017-01-19 14:57:28 -0800511 }
Alex Lighta01de592016-11-15 10:43:06 -0800512 }
Alex Lighta7e38d82017-01-19 14:57:28 -0800513
Alex Light2f814aa2017-03-24 15:21:34 +0000514 // return the current dex_cache which has the dex file in it.
515 art::ObjPtr<art::mirror::DexCache> current_dex_cache(GetMirrorClass()->GetDexCache());
Alex Lighta7e38d82017-01-19 14:57:28 -0800516 // TODO Handle this or make it so it cannot happen.
Alex Light2f814aa2017-03-24 15:21:34 +0000517 if (current_dex_cache->GetDexFile()->NumClassDefs() != 1) {
Alex Lighta7e38d82017-01-19 14:57:28 -0800518 LOG(WARNING) << "Current dex file has more than one class in it. Calling RetransformClasses "
519 << "on this class might fail if no transformations are applied to it!";
Alex Lighta01de592016-11-15 10:43:06 -0800520 }
Alex Light2f814aa2017-03-24 15:21:34 +0000521 return current_dex_cache.Ptr();
Alex Lighta01de592016-11-15 10:43:06 -0800522}
523
Alex Lightdba61482016-12-21 08:20:29 -0800524struct CallbackCtx {
Alex Lighteee0bd42017-02-14 15:31:45 +0000525 ObsoleteMap* obsolete_map;
Alex Lightdba61482016-12-21 08:20:29 -0800526 art::LinearAlloc* allocator;
Alex Lightdba61482016-12-21 08:20:29 -0800527 std::unordered_set<art::ArtMethod*> obsolete_methods;
Alex Lightdba61482016-12-21 08:20:29 -0800528
Alex Lighteee0bd42017-02-14 15:31:45 +0000529 explicit CallbackCtx(ObsoleteMap* map, art::LinearAlloc* alloc)
530 : obsolete_map(map), allocator(alloc) {}
Alex Lightdba61482016-12-21 08:20:29 -0800531};
532
Alex Lightdba61482016-12-21 08:20:29 -0800533void DoAllocateObsoleteMethodsCallback(art::Thread* t, void* vdata) NO_THREAD_SAFETY_ANALYSIS {
534 CallbackCtx* data = reinterpret_cast<CallbackCtx*>(vdata);
Alex Light007ada22017-01-10 13:33:56 -0800535 ObsoleteMethodStackVisitor::UpdateObsoleteFrames(t,
536 data->allocator,
537 data->obsolete_methods,
Alex Lighteee0bd42017-02-14 15:31:45 +0000538 data->obsolete_map);
Alex Lightdba61482016-12-21 08:20:29 -0800539}
540
541// This creates any ArtMethod* structures needed for obsolete methods and ensures that the stack is
542// updated so they will be run.
Alex Light0e692732017-01-10 15:00:05 -0800543// TODO Rewrite so we can do this only once regardless of how many redefinitions there are.
544void Redefiner::ClassRedefinition::FindAndAllocateObsoleteMethods(art::mirror::Class* art_klass) {
Alex Lightdba61482016-12-21 08:20:29 -0800545 art::ScopedAssertNoThreadSuspension ns("No thread suspension during thread stack walking");
546 art::mirror::ClassExt* ext = art_klass->GetExtData();
547 CHECK(ext->GetObsoleteMethods() != nullptr);
Alex Light7916f202017-01-27 09:00:15 -0800548 art::ClassLinker* linker = driver_->runtime_->GetClassLinker();
Alex Lighteee0bd42017-02-14 15:31:45 +0000549 // This holds pointers to the obsolete methods map fields which are updated as needed.
550 ObsoleteMap map(ext->GetObsoleteMethods(), ext->GetObsoleteDexCaches(), art_klass->GetDexCache());
551 CallbackCtx ctx(&map, linker->GetAllocatorForClassLoader(art_klass->GetClassLoader()));
Alex Lightdba61482016-12-21 08:20:29 -0800552 // Add all the declared methods to the map
553 for (auto& m : art_klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
Alex Light7532d582017-02-13 16:36:06 -0800554 if (m.IsIntrinsic()) {
555 LOG(WARNING) << "Redefining intrinsic method " << m.PrettyMethod() << ". This may cause the "
556 << "unexpected use of the original definition of " << m.PrettyMethod() << "in "
557 << "methods that have already been compiled.";
558 }
Alex Lighteee0bd42017-02-14 15:31:45 +0000559 // It is possible to simply filter out some methods where they cannot really become obsolete,
560 // such as native methods and keep their original (possibly optimized) implementations. We don't
561 // do this, however, since we would need to mark these functions (still in the classes
562 // declared_methods array) as obsolete so we will find the correct dex file to get meta-data
563 // from (for example about stack-frame size). Furthermore we would be unable to get some useful
564 // error checking from the interpreter which ensure we don't try to start executing obsolete
565 // methods.
Nicolas Geoffray7558d272017-02-10 10:01:47 +0000566 ctx.obsolete_methods.insert(&m);
Alex Lightdba61482016-12-21 08:20:29 -0800567 }
568 {
Alex Light0e692732017-01-10 15:00:05 -0800569 art::MutexLock mu(driver_->self_, *art::Locks::thread_list_lock_);
Alex Lightdba61482016-12-21 08:20:29 -0800570 art::ThreadList* list = art::Runtime::Current()->GetThreadList();
571 list->ForEach(DoAllocateObsoleteMethodsCallback, static_cast<void*>(&ctx));
Alex Lightdba61482016-12-21 08:20:29 -0800572 }
Alex Lightdba61482016-12-21 08:20:29 -0800573}
574
Alex Light6161f132017-01-25 10:30:20 -0800575// Try and get the declared method. First try to get a virtual method then a direct method if that's
576// not found.
577static art::ArtMethod* FindMethod(art::Handle<art::mirror::Class> klass,
Vladimir Markoba118822017-06-12 15:41:56 +0100578 art::StringPiece name,
Alex Light6161f132017-01-25 10:30:20 -0800579 art::Signature sig) REQUIRES_SHARED(art::Locks::mutator_lock_) {
Vladimir Markoba118822017-06-12 15:41:56 +0100580 DCHECK(!klass->IsProxyClass());
581 for (art::ArtMethod& m : klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize)) {
582 if (m.GetName() == name && m.GetSignature() == sig) {
583 return &m;
584 }
Alex Light6161f132017-01-25 10:30:20 -0800585 }
Vladimir Markoba118822017-06-12 15:41:56 +0100586 return nullptr;
Alex Light6161f132017-01-25 10:30:20 -0800587}
588
589bool Redefiner::ClassRedefinition::CheckSameMethods() {
590 art::StackHandleScope<1> hs(driver_->self_);
591 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass()));
592 DCHECK_EQ(dex_file_->NumClassDefs(), 1u);
593
594 art::ClassDataItemIterator new_iter(*dex_file_,
595 dex_file_->GetClassData(dex_file_->GetClassDef(0)));
596
597 // Make sure we have the same number of methods.
598 uint32_t num_new_method = new_iter.NumVirtualMethods() + new_iter.NumDirectMethods();
599 uint32_t num_old_method = h_klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size();
600 if (num_new_method != num_old_method) {
601 bool bigger = num_new_method > num_old_method;
602 RecordFailure(bigger ? ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED)
603 : ERR(UNSUPPORTED_REDEFINITION_METHOD_DELETED),
604 StringPrintf("Total number of declared methods changed from %d to %d",
605 num_old_method, num_new_method));
606 return false;
607 }
608
609 // Skip all of the fields. We should have already checked this.
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700610 new_iter.SkipAllFields();
Alex Light6161f132017-01-25 10:30:20 -0800611 // Check each of the methods. NB we don't need to specifically check for removals since the 2 dex
612 // files have the same number of methods, which means there must be an equal amount of additions
613 // and removals.
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800614 for (; new_iter.HasNextMethod(); new_iter.Next()) {
Alex Light6161f132017-01-25 10:30:20 -0800615 // Get the data on the method we are searching for
616 const art::DexFile::MethodId& new_method_id = dex_file_->GetMethodId(new_iter.GetMemberIndex());
617 const char* new_method_name = dex_file_->GetMethodName(new_method_id);
618 art::Signature new_method_signature = dex_file_->GetMethodSignature(new_method_id);
619 art::ArtMethod* old_method = FindMethod(h_klass, new_method_name, new_method_signature);
620 // If we got past the check for the same number of methods above that means there must be at
621 // least one added and one removed method. We will return the ADDED failure message since it is
622 // easier to get a useful error report for it.
623 if (old_method == nullptr) {
624 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED),
625 StringPrintf("Unknown method '%s' (sig: %s) was added!",
626 new_method_name,
627 new_method_signature.ToString().c_str()));
628 return false;
629 }
630 // Since direct methods have different flags than virtual ones (specifically direct methods must
631 // have kAccPrivate or kAccStatic or kAccConstructor flags) we can tell if a method changes from
632 // virtual to direct.
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100633 uint32_t new_flags = new_iter.GetMethodAccessFlags();
634 if (new_flags != (old_method->GetAccessFlags() & art::kAccValidMethodFlags)) {
Alex Light6161f132017-01-25 10:30:20 -0800635 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED),
636 StringPrintf("method '%s' (sig: %s) had different access flags",
637 new_method_name,
638 new_method_signature.ToString().c_str()));
639 return false;
640 }
641 }
642 return true;
643}
644
645bool Redefiner::ClassRedefinition::CheckSameFields() {
646 art::StackHandleScope<1> hs(driver_->self_);
647 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass()));
648 DCHECK_EQ(dex_file_->NumClassDefs(), 1u);
649 art::ClassDataItemIterator new_iter(*dex_file_,
650 dex_file_->GetClassData(dex_file_->GetClassDef(0)));
651 const art::DexFile& old_dex_file = h_klass->GetDexFile();
652 art::ClassDataItemIterator old_iter(old_dex_file,
653 old_dex_file.GetClassData(*h_klass->GetClassDef()));
654 // Instance and static fields can be differentiated by their flags so no need to check them
655 // separately.
656 while (new_iter.HasNextInstanceField() || new_iter.HasNextStaticField()) {
657 // Get the data on the method we are searching for
658 const art::DexFile::FieldId& new_field_id = dex_file_->GetFieldId(new_iter.GetMemberIndex());
659 const char* new_field_name = dex_file_->GetFieldName(new_field_id);
660 const char* new_field_type = dex_file_->GetFieldTypeDescriptor(new_field_id);
661
662 if (!(old_iter.HasNextInstanceField() || old_iter.HasNextStaticField())) {
663 // We are missing the old version of this method!
664 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED),
665 StringPrintf("Unknown field '%s' (type: %s) added!",
666 new_field_name,
667 new_field_type));
668 return false;
669 }
670
671 const art::DexFile::FieldId& old_field_id = old_dex_file.GetFieldId(old_iter.GetMemberIndex());
672 const char* old_field_name = old_dex_file.GetFieldName(old_field_id);
673 const char* old_field_type = old_dex_file.GetFieldTypeDescriptor(old_field_id);
674
675 // Check name and type.
676 if (strcmp(old_field_name, new_field_name) != 0 ||
677 strcmp(old_field_type, new_field_type) != 0) {
678 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED),
679 StringPrintf("Field changed from '%s' (sig: %s) to '%s' (sig: %s)!",
680 old_field_name,
681 old_field_type,
682 new_field_name,
683 new_field_type));
684 return false;
685 }
686
687 // Since static fields have different flags than instance ones (specifically static fields must
688 // have the kAccStatic flag) we can tell if a field changes from static to instance.
689 if (new_iter.GetFieldAccessFlags() != old_iter.GetFieldAccessFlags()) {
690 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED),
691 StringPrintf("Field '%s' (sig: %s) had different access flags",
692 new_field_name,
693 new_field_type));
694 return false;
695 }
696
697 new_iter.Next();
698 old_iter.Next();
699 }
700 if (old_iter.HasNextInstanceField() || old_iter.HasNextStaticField()) {
701 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED),
702 StringPrintf("field '%s' (sig: %s) is missing!",
703 old_dex_file.GetFieldName(old_dex_file.GetFieldId(
704 old_iter.GetMemberIndex())),
705 old_dex_file.GetFieldTypeDescriptor(old_dex_file.GetFieldId(
706 old_iter.GetMemberIndex()))));
707 return false;
708 }
709 return true;
710}
711
Alex Light0e692732017-01-10 15:00:05 -0800712bool Redefiner::ClassRedefinition::CheckClass() {
Alex Light0e692732017-01-10 15:00:05 -0800713 art::StackHandleScope<1> hs(driver_->self_);
Alex Light460d1b42017-01-10 15:37:17 +0000714 // Easy check that only 1 class def is present.
715 if (dex_file_->NumClassDefs() != 1) {
716 RecordFailure(ERR(ILLEGAL_ARGUMENT),
717 StringPrintf("Expected 1 class def in dex file but found %d",
718 dex_file_->NumClassDefs()));
719 return false;
720 }
721 // Get the ClassDef from the new DexFile.
722 // Since the dex file has only a single class def the index is always 0.
723 const art::DexFile::ClassDef& def = dex_file_->GetClassDef(0);
724 // Get the class as it is now.
725 art::Handle<art::mirror::Class> current_class(hs.NewHandle(GetMirrorClass()));
726
727 // Check the access flags didn't change.
728 if (def.GetJavaAccessFlags() != (current_class->GetAccessFlags() & art::kAccValidClassFlags)) {
729 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED),
730 "Cannot change modifiers of class by redefinition");
731 return false;
732 }
733
734 // Check class name.
735 // These should have been checked by the dexfile verifier on load.
736 DCHECK_NE(def.class_idx_, art::dex::TypeIndex::Invalid()) << "Invalid type index";
737 const char* descriptor = dex_file_->StringByTypeIdx(def.class_idx_);
738 DCHECK(descriptor != nullptr) << "Invalid dex file structure!";
739 if (!current_class->DescriptorEquals(descriptor)) {
740 std::string storage;
741 RecordFailure(ERR(NAMES_DONT_MATCH),
742 StringPrintf("expected file to contain class called '%s' but found '%s'!",
743 current_class->GetDescriptor(&storage),
744 descriptor));
745 return false;
746 }
747 if (current_class->IsObjectClass()) {
748 if (def.superclass_idx_ != art::dex::TypeIndex::Invalid()) {
749 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass added!");
750 return false;
751 }
752 } else {
753 const char* super_descriptor = dex_file_->StringByTypeIdx(def.superclass_idx_);
754 DCHECK(descriptor != nullptr) << "Invalid dex file structure!";
755 if (!current_class->GetSuperClass()->DescriptorEquals(super_descriptor)) {
756 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass changed");
757 return false;
758 }
759 }
760 const art::DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(def);
761 if (interfaces == nullptr) {
762 if (current_class->NumDirectInterfaces() != 0) {
763 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added");
764 return false;
765 }
766 } else {
767 DCHECK(!current_class->IsProxyClass());
768 const art::DexFile::TypeList* current_interfaces = current_class->GetInterfaceTypeList();
769 if (current_interfaces == nullptr || current_interfaces->Size() != interfaces->Size()) {
770 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added or removed");
771 return false;
772 }
773 // The order of interfaces is (barely) meaningful so we error if it changes.
774 const art::DexFile& orig_dex_file = current_class->GetDexFile();
775 for (uint32_t i = 0; i < interfaces->Size(); i++) {
776 if (strcmp(
777 dex_file_->StringByTypeIdx(interfaces->GetTypeItem(i).type_idx_),
778 orig_dex_file.StringByTypeIdx(current_interfaces->GetTypeItem(i).type_idx_)) != 0) {
779 RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED),
780 "Interfaces changed or re-ordered");
781 return false;
782 }
783 }
784 }
Alex Light460d1b42017-01-10 15:37:17 +0000785 return true;
786}
787
Alex Light0e692732017-01-10 15:00:05 -0800788bool Redefiner::ClassRedefinition::CheckRedefinable() {
Alex Lighte4a88632017-01-10 07:41:24 -0800789 std::string err;
Alex Light0e692732017-01-10 15:00:05 -0800790 art::StackHandleScope<1> hs(driver_->self_);
Alex Light460d1b42017-01-10 15:37:17 +0000791
Alex Lighte4a88632017-01-10 07:41:24 -0800792 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass()));
793 jvmtiError res = Redefiner::GetClassRedefinitionError(h_klass, &err);
794 if (res != OK) {
795 RecordFailure(res, err);
796 return false;
797 } else {
798 return true;
799 }
Alex Light460d1b42017-01-10 15:37:17 +0000800}
801
Alex Light0e692732017-01-10 15:00:05 -0800802bool Redefiner::ClassRedefinition::CheckRedefinitionIsValid() {
Alex Light460d1b42017-01-10 15:37:17 +0000803 return CheckRedefinable() &&
804 CheckClass() &&
805 CheckSameFields() &&
806 CheckSameMethods();
807}
808
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800809class RedefinitionDataIter;
810
Alex Light0e692732017-01-10 15:00:05 -0800811// A wrapper that lets us hold onto the arbitrary sized data needed for redefinitions in a
812// reasonably sane way. This adds no fields to the normal ObjectArray. By doing this we can avoid
813// having to deal with the fact that we need to hold an arbitrary number of references live.
814class RedefinitionDataHolder {
815 public:
816 enum DataSlot : int32_t {
817 kSlotSourceClassLoader = 0,
818 kSlotJavaDexFile = 1,
819 kSlotNewDexFileCookie = 2,
820 kSlotNewDexCache = 3,
821 kSlotMirrorClass = 4,
Alex Lighta7e38d82017-01-19 14:57:28 -0800822 kSlotOrigDexFile = 5,
Alex Light1e3926a2017-04-07 10:38:06 -0700823 kSlotOldObsoleteMethods = 6,
824 kSlotOldDexCaches = 7,
Alex Light0e692732017-01-10 15:00:05 -0800825
826 // Must be last one.
Alex Light1e3926a2017-04-07 10:38:06 -0700827 kNumSlots = 8,
Alex Light0e692732017-01-10 15:00:05 -0800828 };
829
830 // This needs to have a HandleScope passed in that is capable of creating a new Handle without
831 // overflowing. Only one handle will be created. This object has a lifetime identical to that of
832 // the passed in handle-scope.
833 RedefinitionDataHolder(art::StackHandleScope<1>* hs,
834 art::Runtime* runtime,
835 art::Thread* self,
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800836 std::vector<Redefiner::ClassRedefinition>* redefinitions)
837 REQUIRES_SHARED(art::Locks::mutator_lock_) :
Alex Light0e692732017-01-10 15:00:05 -0800838 arr_(
839 hs->NewHandle(
840 art::mirror::ObjectArray<art::mirror::Object>::Alloc(
841 self,
842 runtime->GetClassLinker()->GetClassRoot(art::ClassLinker::kObjectArrayClass),
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800843 redefinitions->size() * kNumSlots))),
844 redefinitions_(redefinitions) {}
Alex Light0e692732017-01-10 15:00:05 -0800845
846 bool IsNull() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
847 return arr_.IsNull();
848 }
849
Alex Light8c889d22017-02-06 13:58:27 -0800850 art::mirror::ClassLoader* GetSourceClassLoader(jint klass_index) const
Alex Light0e692732017-01-10 15:00:05 -0800851 REQUIRES_SHARED(art::Locks::mutator_lock_) {
852 return art::down_cast<art::mirror::ClassLoader*>(GetSlot(klass_index, kSlotSourceClassLoader));
853 }
Alex Light8c889d22017-02-06 13:58:27 -0800854 art::mirror::Object* GetJavaDexFile(jint klass_index) const
855 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light0e692732017-01-10 15:00:05 -0800856 return GetSlot(klass_index, kSlotJavaDexFile);
857 }
Alex Light8c889d22017-02-06 13:58:27 -0800858 art::mirror::LongArray* GetNewDexFileCookie(jint klass_index) const
Alex Light0e692732017-01-10 15:00:05 -0800859 REQUIRES_SHARED(art::Locks::mutator_lock_) {
860 return art::down_cast<art::mirror::LongArray*>(GetSlot(klass_index, kSlotNewDexFileCookie));
861 }
Alex Light8c889d22017-02-06 13:58:27 -0800862 art::mirror::DexCache* GetNewDexCache(jint klass_index) const
Alex Light0e692732017-01-10 15:00:05 -0800863 REQUIRES_SHARED(art::Locks::mutator_lock_) {
864 return art::down_cast<art::mirror::DexCache*>(GetSlot(klass_index, kSlotNewDexCache));
865 }
Alex Light8c889d22017-02-06 13:58:27 -0800866 art::mirror::Class* GetMirrorClass(jint klass_index) const
867 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light0e692732017-01-10 15:00:05 -0800868 return art::down_cast<art::mirror::Class*>(GetSlot(klass_index, kSlotMirrorClass));
869 }
870
Alex Light2f814aa2017-03-24 15:21:34 +0000871 art::mirror::Object* GetOriginalDexFile(jint klass_index) const
Alex Lighta7e38d82017-01-19 14:57:28 -0800872 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light2f814aa2017-03-24 15:21:34 +0000873 return art::down_cast<art::mirror::Object*>(GetSlot(klass_index, kSlotOrigDexFile));
Alex Lighta7e38d82017-01-19 14:57:28 -0800874 }
875
Alex Light1e3926a2017-04-07 10:38:06 -0700876 art::mirror::PointerArray* GetOldObsoleteMethods(jint klass_index) const
877 REQUIRES_SHARED(art::Locks::mutator_lock_) {
878 return art::down_cast<art::mirror::PointerArray*>(
879 GetSlot(klass_index, kSlotOldObsoleteMethods));
880 }
881
882 art::mirror::ObjectArray<art::mirror::DexCache>* GetOldDexCaches(jint klass_index) const
883 REQUIRES_SHARED(art::Locks::mutator_lock_) {
884 return art::down_cast<art::mirror::ObjectArray<art::mirror::DexCache>*>(
885 GetSlot(klass_index, kSlotOldDexCaches));
886 }
887
Alex Light0e692732017-01-10 15:00:05 -0800888 void SetSourceClassLoader(jint klass_index, art::mirror::ClassLoader* loader)
889 REQUIRES_SHARED(art::Locks::mutator_lock_) {
890 SetSlot(klass_index, kSlotSourceClassLoader, loader);
891 }
892 void SetJavaDexFile(jint klass_index, art::mirror::Object* dexfile)
893 REQUIRES_SHARED(art::Locks::mutator_lock_) {
894 SetSlot(klass_index, kSlotJavaDexFile, dexfile);
895 }
896 void SetNewDexFileCookie(jint klass_index, art::mirror::LongArray* cookie)
897 REQUIRES_SHARED(art::Locks::mutator_lock_) {
898 SetSlot(klass_index, kSlotNewDexFileCookie, cookie);
899 }
900 void SetNewDexCache(jint klass_index, art::mirror::DexCache* cache)
901 REQUIRES_SHARED(art::Locks::mutator_lock_) {
902 SetSlot(klass_index, kSlotNewDexCache, cache);
903 }
904 void SetMirrorClass(jint klass_index, art::mirror::Class* klass)
905 REQUIRES_SHARED(art::Locks::mutator_lock_) {
906 SetSlot(klass_index, kSlotMirrorClass, klass);
907 }
Alex Light2f814aa2017-03-24 15:21:34 +0000908 void SetOriginalDexFile(jint klass_index, art::mirror::Object* bytes)
Alex Lighta7e38d82017-01-19 14:57:28 -0800909 REQUIRES_SHARED(art::Locks::mutator_lock_) {
910 SetSlot(klass_index, kSlotOrigDexFile, bytes);
911 }
Alex Light1e3926a2017-04-07 10:38:06 -0700912 void SetOldObsoleteMethods(jint klass_index, art::mirror::PointerArray* methods)
913 REQUIRES_SHARED(art::Locks::mutator_lock_) {
914 SetSlot(klass_index, kSlotOldObsoleteMethods, methods);
915 }
916 void SetOldDexCaches(jint klass_index, art::mirror::ObjectArray<art::mirror::DexCache>* caches)
917 REQUIRES_SHARED(art::Locks::mutator_lock_) {
918 SetSlot(klass_index, kSlotOldDexCaches, caches);
919 }
Alex Light0e692732017-01-10 15:00:05 -0800920
Alex Light8c889d22017-02-06 13:58:27 -0800921 int32_t Length() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light0e692732017-01-10 15:00:05 -0800922 return arr_->GetLength() / kNumSlots;
923 }
924
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800925 std::vector<Redefiner::ClassRedefinition>* GetRedefinitions()
926 REQUIRES_SHARED(art::Locks::mutator_lock_) {
927 return redefinitions_;
928 }
929
930 bool operator==(const RedefinitionDataHolder& other) const
931 REQUIRES_SHARED(art::Locks::mutator_lock_) {
932 return arr_.Get() == other.arr_.Get();
933 }
934
935 bool operator!=(const RedefinitionDataHolder& other) const
936 REQUIRES_SHARED(art::Locks::mutator_lock_) {
937 return !(*this == other);
938 }
939
940 RedefinitionDataIter begin() REQUIRES_SHARED(art::Locks::mutator_lock_);
941 RedefinitionDataIter end() REQUIRES_SHARED(art::Locks::mutator_lock_);
942
Alex Light0e692732017-01-10 15:00:05 -0800943 private:
Alex Light8c889d22017-02-06 13:58:27 -0800944 mutable art::Handle<art::mirror::ObjectArray<art::mirror::Object>> arr_;
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800945 std::vector<Redefiner::ClassRedefinition>* redefinitions_;
Alex Light0e692732017-01-10 15:00:05 -0800946
947 art::mirror::Object* GetSlot(jint klass_index,
Alex Light8c889d22017-02-06 13:58:27 -0800948 DataSlot slot) const REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light0e692732017-01-10 15:00:05 -0800949 DCHECK_LT(klass_index, Length());
950 return arr_->Get((kNumSlots * klass_index) + slot);
951 }
952
953 void SetSlot(jint klass_index,
954 DataSlot slot,
955 art::ObjPtr<art::mirror::Object> obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
956 DCHECK(!art::Runtime::Current()->IsActiveTransaction());
957 DCHECK_LT(klass_index, Length());
958 arr_->Set<false>((kNumSlots * klass_index) + slot, obj);
959 }
960
961 DISALLOW_COPY_AND_ASSIGN(RedefinitionDataHolder);
962};
963
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800964class RedefinitionDataIter {
965 public:
966 RedefinitionDataIter(int32_t idx, RedefinitionDataHolder& holder) : idx_(idx), holder_(holder) {}
967
968 RedefinitionDataIter(const RedefinitionDataIter&) = default;
969 RedefinitionDataIter(RedefinitionDataIter&&) = default;
970 RedefinitionDataIter& operator=(const RedefinitionDataIter&) = default;
971 RedefinitionDataIter& operator=(RedefinitionDataIter&&) = default;
972
973 bool operator==(const RedefinitionDataIter& other) const
974 REQUIRES_SHARED(art::Locks::mutator_lock_) {
975 return idx_ == other.idx_ && holder_ == other.holder_;
976 }
977
978 bool operator!=(const RedefinitionDataIter& other) const
979 REQUIRES_SHARED(art::Locks::mutator_lock_) {
980 return !(*this == other);
981 }
982
983 RedefinitionDataIter operator++() { // Value after modification.
984 idx_++;
985 return *this;
986 }
987
988 RedefinitionDataIter operator++(int) {
989 RedefinitionDataIter temp = *this;
990 idx_++;
991 return temp;
992 }
993
994 RedefinitionDataIter operator+(ssize_t delta) const {
995 RedefinitionDataIter temp = *this;
996 temp += delta;
997 return temp;
998 }
999
1000 RedefinitionDataIter& operator+=(ssize_t delta) {
1001 idx_ += delta;
1002 return *this;
1003 }
1004
1005 Redefiner::ClassRedefinition& GetRedefinition() REQUIRES_SHARED(art::Locks::mutator_lock_) {
1006 return (*holder_.GetRedefinitions())[idx_];
1007 }
1008
1009 RedefinitionDataHolder& GetHolder() {
1010 return holder_;
1011 }
1012
1013 art::mirror::ClassLoader* GetSourceClassLoader() const
1014 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1015 return holder_.GetSourceClassLoader(idx_);
1016 }
1017 art::mirror::Object* GetJavaDexFile() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
1018 return holder_.GetJavaDexFile(idx_);
1019 }
1020 art::mirror::LongArray* GetNewDexFileCookie() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
1021 return holder_.GetNewDexFileCookie(idx_);
1022 }
1023 art::mirror::DexCache* GetNewDexCache() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
1024 return holder_.GetNewDexCache(idx_);
1025 }
1026 art::mirror::Class* GetMirrorClass() const REQUIRES_SHARED(art::Locks::mutator_lock_) {
1027 return holder_.GetMirrorClass(idx_);
1028 }
Alex Light2f814aa2017-03-24 15:21:34 +00001029 art::mirror::Object* GetOriginalDexFile() const
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001030 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light2f814aa2017-03-24 15:21:34 +00001031 return holder_.GetOriginalDexFile(idx_);
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001032 }
Alex Light1e3926a2017-04-07 10:38:06 -07001033 art::mirror::PointerArray* GetOldObsoleteMethods() const
1034 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1035 return holder_.GetOldObsoleteMethods(idx_);
1036 }
1037 art::mirror::ObjectArray<art::mirror::DexCache>* GetOldDexCaches() const
1038 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1039 return holder_.GetOldDexCaches(idx_);
1040 }
1041
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001042 int32_t GetIndex() const {
1043 return idx_;
1044 }
1045
1046 void SetSourceClassLoader(art::mirror::ClassLoader* loader)
1047 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1048 holder_.SetSourceClassLoader(idx_, loader);
1049 }
1050 void SetJavaDexFile(art::mirror::Object* dexfile) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1051 holder_.SetJavaDexFile(idx_, dexfile);
1052 }
1053 void SetNewDexFileCookie(art::mirror::LongArray* cookie)
1054 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1055 holder_.SetNewDexFileCookie(idx_, cookie);
1056 }
1057 void SetNewDexCache(art::mirror::DexCache* cache) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1058 holder_.SetNewDexCache(idx_, cache);
1059 }
1060 void SetMirrorClass(art::mirror::Class* klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1061 holder_.SetMirrorClass(idx_, klass);
1062 }
Alex Light2f814aa2017-03-24 15:21:34 +00001063 void SetOriginalDexFile(art::mirror::Object* bytes)
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001064 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light2f814aa2017-03-24 15:21:34 +00001065 holder_.SetOriginalDexFile(idx_, bytes);
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001066 }
Alex Light1e3926a2017-04-07 10:38:06 -07001067 void SetOldObsoleteMethods(art::mirror::PointerArray* methods)
1068 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1069 holder_.SetOldObsoleteMethods(idx_, methods);
1070 }
1071 void SetOldDexCaches(art::mirror::ObjectArray<art::mirror::DexCache>* caches)
1072 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1073 holder_.SetOldDexCaches(idx_, caches);
1074 }
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001075
1076 private:
1077 int32_t idx_;
1078 RedefinitionDataHolder& holder_;
1079};
1080
1081RedefinitionDataIter RedefinitionDataHolder::begin() {
1082 return RedefinitionDataIter(0, *this);
1083}
1084
1085RedefinitionDataIter RedefinitionDataHolder::end() {
1086 return RedefinitionDataIter(Length(), *this);
1087}
1088
1089bool Redefiner::ClassRedefinition::CheckVerification(const RedefinitionDataIter& iter) {
Alex Light8c889d22017-02-06 13:58:27 -08001090 DCHECK_EQ(dex_file_->NumClassDefs(), 1u);
1091 art::StackHandleScope<2> hs(driver_->self_);
1092 std::string error;
1093 // TODO Make verification log level lower
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001094 art::verifier::FailureKind failure =
Alex Light8c889d22017-02-06 13:58:27 -08001095 art::verifier::MethodVerifier::VerifyClass(driver_->self_,
1096 dex_file_.get(),
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001097 hs.NewHandle(iter.GetNewDexCache()),
Alex Light8c889d22017-02-06 13:58:27 -08001098 hs.NewHandle(GetClassLoader()),
1099 dex_file_->GetClassDef(0), /*class_def*/
1100 nullptr, /*compiler_callbacks*/
Alex Light53330612017-10-04 15:29:53 -07001101 true, /*allow_soft_failures*/
Alex Light8c889d22017-02-06 13:58:27 -08001102 /*log_level*/
1103 art::verifier::HardFailLogMode::kLogWarning,
1104 &error);
Alex Light53330612017-10-04 15:29:53 -07001105 switch (failure) {
1106 case art::verifier::FailureKind::kNoFailure:
1107 case art::verifier::FailureKind::kSoftFailure:
1108 return true;
1109 case art::verifier::FailureKind::kHardFailure: {
1110 RecordFailure(ERR(FAILS_VERIFICATION), "Failed to verify class. Error was: " + error);
1111 return false;
1112 }
Alex Light8c889d22017-02-06 13:58:27 -08001113 }
Alex Light8c889d22017-02-06 13:58:27 -08001114}
1115
Alex Light1babae02017-02-01 15:35:34 -08001116// Looks through the previously allocated cookies to see if we need to update them with another new
1117// dexfile. This is so that even if multiple classes with the same classloader are redefined at
1118// once they are all added to the classloader.
1119bool Redefiner::ClassRedefinition::AllocateAndRememberNewDexFileCookie(
Alex Light1babae02017-02-01 15:35:34 -08001120 art::Handle<art::mirror::ClassLoader> source_class_loader,
1121 art::Handle<art::mirror::Object> dex_file_obj,
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001122 /*out*/RedefinitionDataIter* cur_data) {
Alex Light1babae02017-02-01 15:35:34 -08001123 art::StackHandleScope<2> hs(driver_->self_);
1124 art::MutableHandle<art::mirror::LongArray> old_cookie(
1125 hs.NewHandle<art::mirror::LongArray>(nullptr));
1126 bool has_older_cookie = false;
1127 // See if we already have a cookie that a previous redefinition got from the same classloader.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001128 for (auto old_data = cur_data->GetHolder().begin(); old_data != *cur_data; ++old_data) {
1129 if (old_data.GetSourceClassLoader() == source_class_loader.Get()) {
Alex Light1babae02017-02-01 15:35:34 -08001130 // Since every instance of this classloader should have the same cookie associated with it we
1131 // can stop looking here.
1132 has_older_cookie = true;
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001133 old_cookie.Assign(old_data.GetNewDexFileCookie());
Alex Light1babae02017-02-01 15:35:34 -08001134 break;
1135 }
1136 }
1137 if (old_cookie.IsNull()) {
1138 // No older cookie. Get it directly from the dex_file_obj
1139 // We should not have seen this classloader elsewhere.
1140 CHECK(!has_older_cookie);
1141 old_cookie.Assign(ClassLoaderHelper::GetDexFileCookie(dex_file_obj));
1142 }
1143 // Use the old cookie to generate the new one with the new DexFile* added in.
1144 art::Handle<art::mirror::LongArray>
1145 new_cookie(hs.NewHandle(ClassLoaderHelper::AllocateNewDexFileCookie(driver_->self_,
1146 old_cookie,
1147 dex_file_.get())));
1148 // Make sure the allocation worked.
1149 if (new_cookie.IsNull()) {
1150 return false;
1151 }
1152
1153 // Save the cookie.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001154 cur_data->SetNewDexFileCookie(new_cookie.Get());
Alex Light1babae02017-02-01 15:35:34 -08001155 // If there are other copies of this same classloader we need to make sure that we all have the
1156 // same cookie.
1157 if (has_older_cookie) {
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001158 for (auto old_data = cur_data->GetHolder().begin(); old_data != *cur_data; ++old_data) {
Alex Light1babae02017-02-01 15:35:34 -08001159 // We will let the GC take care of the cookie we allocated for this one.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001160 if (old_data.GetSourceClassLoader() == source_class_loader.Get()) {
1161 old_data.SetNewDexFileCookie(new_cookie.Get());
Alex Light1babae02017-02-01 15:35:34 -08001162 }
1163 }
1164 }
1165
1166 return true;
1167}
1168
Alex Lighta7e38d82017-01-19 14:57:28 -08001169bool Redefiner::ClassRedefinition::FinishRemainingAllocations(
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001170 /*out*/RedefinitionDataIter* cur_data) {
Alex Light7916f202017-01-27 09:00:15 -08001171 art::ScopedObjectAccessUnchecked soa(driver_->self_);
Alex Lighta7e38d82017-01-19 14:57:28 -08001172 art::StackHandleScope<2> hs(driver_->self_);
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001173 cur_data->SetMirrorClass(GetMirrorClass());
Alex Lighta7e38d82017-01-19 14:57:28 -08001174 // This shouldn't allocate
1175 art::Handle<art::mirror::ClassLoader> loader(hs.NewHandle(GetClassLoader()));
Alex Light7916f202017-01-27 09:00:15 -08001176 // The bootclasspath is handled specially so it doesn't have a j.l.DexFile.
1177 if (!art::ClassLinker::IsBootClassLoader(soa, loader.Get())) {
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001178 cur_data->SetSourceClassLoader(loader.Get());
Alex Light7916f202017-01-27 09:00:15 -08001179 art::Handle<art::mirror::Object> dex_file_obj(hs.NewHandle(
1180 ClassLoaderHelper::FindSourceDexFileObject(driver_->self_, loader)));
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001181 cur_data->SetJavaDexFile(dex_file_obj.Get());
Andreas Gampefa4333d2017-02-14 11:10:34 -08001182 if (dex_file_obj == nullptr) {
Alex Light7916f202017-01-27 09:00:15 -08001183 RecordFailure(ERR(INTERNAL), "Unable to find dex file!");
1184 return false;
1185 }
Alex Light1babae02017-02-01 15:35:34 -08001186 // Allocate the new dex file cookie.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001187 if (!AllocateAndRememberNewDexFileCookie(loader, dex_file_obj, cur_data)) {
Alex Light7916f202017-01-27 09:00:15 -08001188 driver_->self_->AssertPendingOOMException();
1189 driver_->self_->ClearException();
1190 RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate dex file array for class loader");
1191 return false;
1192 }
Alex Lighta7e38d82017-01-19 14:57:28 -08001193 }
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001194 cur_data->SetNewDexCache(CreateNewDexCache(loader));
1195 if (cur_data->GetNewDexCache() == nullptr) {
Vladimir Markocd556b02017-02-03 11:47:34 +00001196 driver_->self_->AssertPendingException();
Alex Lighta7e38d82017-01-19 14:57:28 -08001197 driver_->self_->ClearException();
1198 RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate DexCache");
1199 return false;
1200 }
1201
1202 // We won't always need to set this field.
Alex Light2f814aa2017-03-24 15:21:34 +00001203 cur_data->SetOriginalDexFile(AllocateOrGetOriginalDexFile());
1204 if (cur_data->GetOriginalDexFile() == nullptr) {
Alex Lighta7e38d82017-01-19 14:57:28 -08001205 driver_->self_->AssertPendingOOMException();
1206 driver_->self_->ClearException();
1207 RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate array for original dex file");
1208 return false;
1209 }
1210 return true;
1211}
1212
Alex Lighta26e3492017-06-27 17:55:37 -07001213void Redefiner::ClassRedefinition::UnregisterJvmtiBreakpoints() {
1214 BreakpointUtil::RemoveBreakpointsInClass(driver_->env_, GetMirrorClass());
1215}
1216
Alex Light5643caf2017-02-08 11:39:07 -08001217void Redefiner::ClassRedefinition::UnregisterBreakpoints() {
1218 DCHECK(art::Dbg::IsDebuggerActive());
1219 art::JDWP::JdwpState* state = art::Dbg::GetJdwpState();
1220 if (state != nullptr) {
1221 state->UnregisterLocationEventsOnClass(GetMirrorClass());
1222 }
1223}
1224
1225void Redefiner::UnregisterAllBreakpoints() {
1226 if (LIKELY(!art::Dbg::IsDebuggerActive())) {
1227 return;
1228 }
1229 for (Redefiner::ClassRedefinition& redef : redefinitions_) {
1230 redef.UnregisterBreakpoints();
1231 }
1232}
1233
Alex Light0e692732017-01-10 15:00:05 -08001234bool Redefiner::CheckAllRedefinitionAreValid() {
1235 for (Redefiner::ClassRedefinition& redef : redefinitions_) {
1236 if (!redef.CheckRedefinitionIsValid()) {
1237 return false;
1238 }
1239 }
1240 return true;
1241}
1242
Alex Light1e3926a2017-04-07 10:38:06 -07001243void Redefiner::RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder) {
1244 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
1245 data.GetRedefinition().RestoreObsoleteMethodMapsIfUnneeded(&data);
1246 }
1247}
1248
1249bool Redefiner::EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder) {
1250 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
1251 if (!data.GetRedefinition().EnsureClassAllocationsFinished(&data)) {
Alex Light0e692732017-01-10 15:00:05 -08001252 return false;
1253 }
1254 }
1255 return true;
1256}
1257
1258bool Redefiner::FinishAllRemainingAllocations(RedefinitionDataHolder& holder) {
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001259 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
Alex Light0e692732017-01-10 15:00:05 -08001260 // Allocate the data this redefinition requires.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001261 if (!data.GetRedefinition().FinishRemainingAllocations(&data)) {
Alex Light0e692732017-01-10 15:00:05 -08001262 return false;
1263 }
Alex Light0e692732017-01-10 15:00:05 -08001264 }
1265 return true;
1266}
1267
1268void Redefiner::ClassRedefinition::ReleaseDexFile() {
1269 dex_file_.release();
1270}
1271
1272void Redefiner::ReleaseAllDexFiles() {
1273 for (Redefiner::ClassRedefinition& redef : redefinitions_) {
1274 redef.ReleaseDexFile();
1275 }
1276}
1277
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001278bool Redefiner::CheckAllClassesAreVerified(RedefinitionDataHolder& holder) {
1279 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
1280 if (!data.GetRedefinition().CheckVerification(data)) {
Alex Light8c889d22017-02-06 13:58:27 -08001281 return false;
1282 }
Alex Light8c889d22017-02-06 13:58:27 -08001283 }
1284 return true;
1285}
1286
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001287class ScopedDisableConcurrentAndMovingGc {
1288 public:
1289 ScopedDisableConcurrentAndMovingGc(art::gc::Heap* heap, art::Thread* self)
1290 : heap_(heap), self_(self) {
1291 if (heap_->IsGcConcurrentAndMoving()) {
1292 heap_->IncrementDisableMovingGC(self_);
1293 }
1294 }
1295
1296 ~ScopedDisableConcurrentAndMovingGc() {
1297 if (heap_->IsGcConcurrentAndMoving()) {
1298 heap_->DecrementDisableMovingGC(self_);
1299 }
1300 }
1301 private:
1302 art::gc::Heap* heap_;
1303 art::Thread* self_;
1304};
1305
Alex Lighta01de592016-11-15 10:43:06 -08001306jvmtiError Redefiner::Run() {
Alex Light0e692732017-01-10 15:00:05 -08001307 art::StackHandleScope<1> hs(self_);
1308 // Allocate an array to hold onto all java temporary objects associated with this redefinition.
1309 // We will let this be collected after the end of this function.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001310 RedefinitionDataHolder holder(&hs, runtime_, self_, &redefinitions_);
Alex Light0e692732017-01-10 15:00:05 -08001311 if (holder.IsNull()) {
1312 self_->AssertPendingOOMException();
1313 self_->ClearException();
1314 RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate storage for temporaries");
1315 return result_;
1316 }
1317
Alex Lighta01de592016-11-15 10:43:06 -08001318 // First we just allocate the ClassExt and its fields that we need. These can be updated
1319 // atomically without any issues (since we allocate the map arrays as empty) so we don't bother
1320 // doing a try loop. The other allocations we need to ensure that nothing has changed in the time
1321 // between allocating them and pausing all threads before we can update them so we need to do a
1322 // try loop.
Alex Light0e692732017-01-10 15:00:05 -08001323 if (!CheckAllRedefinitionAreValid() ||
Alex Light1e3926a2017-04-07 10:38:06 -07001324 !EnsureAllClassAllocationsFinished(holder) ||
Alex Light8c889d22017-02-06 13:58:27 -08001325 !FinishAllRemainingAllocations(holder) ||
1326 !CheckAllClassesAreVerified(holder)) {
Alex Lighta01de592016-11-15 10:43:06 -08001327 return result_;
1328 }
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001329
Alex Light5643caf2017-02-08 11:39:07 -08001330 // At this point we can no longer fail without corrupting the runtime state.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001331 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
Alex Light07f06212017-06-01 14:01:43 -07001332 art::ClassLinker* cl = runtime_->GetClassLinker();
1333 cl->RegisterExistingDexCache(data.GetNewDexCache(), data.GetSourceClassLoader());
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001334 if (data.GetSourceClassLoader() == nullptr) {
Alex Light07f06212017-06-01 14:01:43 -07001335 cl->AppendToBootClassPath(self_, data.GetRedefinition().GetDexFile());
Alex Light7916f202017-01-27 09:00:15 -08001336 }
Alex Light7916f202017-01-27 09:00:15 -08001337 }
Alex Light5643caf2017-02-08 11:39:07 -08001338 UnregisterAllBreakpoints();
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001339
Alex Light6abd5392017-01-05 17:53:00 -08001340 // Disable GC and wait for it to be done if we are a moving GC. This is fine since we are done
1341 // allocating so no deadlocks.
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001342 ScopedDisableConcurrentAndMovingGc sdcamgc(runtime_->GetHeap(), self_);
1343
Alex Lighta01de592016-11-15 10:43:06 -08001344 // Do transition to final suspension
1345 // TODO We might want to give this its own suspended state!
1346 // TODO This isn't right. We need to change state without any chance of suspend ideally!
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001347 art::ScopedThreadSuspension sts(self_, art::ThreadState::kNative);
1348 art::ScopedSuspendAll ssa("Final installation of redefined Classes!", /*long_suspend*/true);
1349 for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) {
Alex Lighteb98b082017-01-25 13:02:32 -08001350 art::ScopedAssertNoThreadSuspension nts("Updating runtime objects for redefinition");
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001351 ClassRedefinition& redef = data.GetRedefinition();
1352 if (data.GetSourceClassLoader() != nullptr) {
1353 ClassLoaderHelper::UpdateJavaDexFile(data.GetJavaDexFile(), data.GetNewDexFileCookie());
Alex Light7916f202017-01-27 09:00:15 -08001354 }
Alex Lightc5f5a6e2017-03-01 16:57:08 -08001355 art::mirror::Class* klass = data.GetMirrorClass();
Alex Light0e692732017-01-10 15:00:05 -08001356 // TODO Rewrite so we don't do a stack walk for each and every class.
1357 redef.FindAndAllocateObsoleteMethods(klass);
Alex Light2f814aa2017-03-24 15:21:34 +00001358 redef.UpdateClass(klass, data.GetNewDexCache(), data.GetOriginalDexFile());
Alex Lighta26e3492017-06-27 17:55:37 -07001359 redef.UnregisterJvmtiBreakpoints();
Alex Light0e692732017-01-10 15:00:05 -08001360 }
Alex Light1e3926a2017-04-07 10:38:06 -07001361 RestoreObsoleteMethodMapsIfUnneeded(holder);
Alex Light7532d582017-02-13 16:36:06 -08001362 // TODO We should check for if any of the redefined methods are intrinsic methods here and, if any
1363 // are, force a full-world deoptimization before finishing redefinition. If we don't do this then
1364 // methods that have been jitted prior to the current redefinition being applied might continue
1365 // to use the old versions of the intrinsics!
Alex Light0e692732017-01-10 15:00:05 -08001366 // TODO Do the dex_file release at a more reasonable place. This works but it muddles who really
1367 // owns the DexFile and when ownership is transferred.
1368 ReleaseAllDexFiles();
Alex Lighta01de592016-11-15 10:43:06 -08001369 return OK;
1370}
1371
Alex Light0e692732017-01-10 15:00:05 -08001372void Redefiner::ClassRedefinition::UpdateMethods(art::ObjPtr<art::mirror::Class> mclass,
Alex Light0e692732017-01-10 15:00:05 -08001373 const art::DexFile::ClassDef& class_def) {
1374 art::ClassLinker* linker = driver_->runtime_->GetClassLinker();
Alex Lighta01de592016-11-15 10:43:06 -08001375 art::PointerSize image_pointer_size = linker->GetImagePointerSize();
Alex Light200b9d72016-12-15 11:34:13 -08001376 const art::DexFile::TypeId& declaring_class_id = dex_file_->GetTypeId(class_def.class_idx_);
Alex Lighta01de592016-11-15 10:43:06 -08001377 const art::DexFile& old_dex_file = mclass->GetDexFile();
Alex Light200b9d72016-12-15 11:34:13 -08001378 // Update methods.
Alex Light00e475c2017-07-19 16:36:23 -07001379 for (art::ArtMethod& method : mclass->GetDeclaredMethods(image_pointer_size)) {
Alex Lighta01de592016-11-15 10:43:06 -08001380 const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(method.GetName());
1381 art::dex::TypeIndex method_return_idx =
1382 dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(method.GetReturnTypeDescriptor()));
1383 const auto* old_type_list = method.GetParameterTypeList();
1384 std::vector<art::dex::TypeIndex> new_type_list;
1385 for (uint32_t i = 0; old_type_list != nullptr && i < old_type_list->Size(); i++) {
1386 new_type_list.push_back(
1387 dex_file_->GetIndexForTypeId(
1388 *dex_file_->FindTypeId(
1389 old_dex_file.GetTypeDescriptor(
1390 old_dex_file.GetTypeId(
1391 old_type_list->GetTypeItem(i).type_idx_)))));
1392 }
1393 const art::DexFile::ProtoId* proto_id = dex_file_->FindProtoId(method_return_idx,
1394 new_type_list);
Alex Lightdba61482016-12-21 08:20:29 -08001395 CHECK(proto_id != nullptr || old_type_list == nullptr);
Alex Lighta01de592016-11-15 10:43:06 -08001396 const art::DexFile::MethodId* method_id = dex_file_->FindMethodId(declaring_class_id,
1397 *new_name_id,
1398 *proto_id);
Alex Lightdba61482016-12-21 08:20:29 -08001399 CHECK(method_id != nullptr);
Alex Lighta01de592016-11-15 10:43:06 -08001400 uint32_t dex_method_idx = dex_file_->GetIndexForMethodId(*method_id);
1401 method.SetDexMethodIndex(dex_method_idx);
1402 linker->SetEntryPointsToInterpreter(&method);
Alex Light200b9d72016-12-15 11:34:13 -08001403 method.SetCodeItemOffset(dex_file_->FindCodeItemOffset(class_def, dex_method_idx));
Alex Light7532d582017-02-13 16:36:06 -08001404 // Clear all the intrinsics related flags.
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +01001405 method.SetNotIntrinsic();
Alex Lightdba61482016-12-21 08:20:29 -08001406 // Notify the jit that this method is redefined.
Alex Light0e692732017-01-10 15:00:05 -08001407 art::jit::Jit* jit = driver_->runtime_->GetJit();
Alex Lightabbc4bc2017-10-05 17:07:40 -07001408 // Non-invokable methods don't have any JIT data associated with them so we don't need to tell
1409 // the jit about them.
1410 if (jit != nullptr && method.IsInvokable()) {
Alex Lightdba61482016-12-21 08:20:29 -08001411 jit->GetCodeCache()->NotifyMethodRedefined(&method);
1412 }
Alex Lighta01de592016-11-15 10:43:06 -08001413 }
Alex Light200b9d72016-12-15 11:34:13 -08001414}
1415
Alex Light0e692732017-01-10 15:00:05 -08001416void Redefiner::ClassRedefinition::UpdateFields(art::ObjPtr<art::mirror::Class> mclass) {
Alex Light200b9d72016-12-15 11:34:13 -08001417 // TODO The IFields & SFields pointers should be combined like the methods_ arrays were.
1418 for (auto fields_iter : {mclass->GetIFields(), mclass->GetSFields()}) {
1419 for (art::ArtField& field : fields_iter) {
1420 std::string declaring_class_name;
1421 const art::DexFile::TypeId* new_declaring_id =
1422 dex_file_->FindTypeId(field.GetDeclaringClass()->GetDescriptor(&declaring_class_name));
1423 const art::DexFile::StringId* new_name_id = dex_file_->FindStringId(field.GetName());
1424 const art::DexFile::TypeId* new_type_id = dex_file_->FindTypeId(field.GetTypeDescriptor());
Alex Light200b9d72016-12-15 11:34:13 -08001425 CHECK(new_name_id != nullptr && new_type_id != nullptr && new_declaring_id != nullptr);
1426 const art::DexFile::FieldId* new_field_id =
1427 dex_file_->FindFieldId(*new_declaring_id, *new_name_id, *new_type_id);
1428 CHECK(new_field_id != nullptr);
1429 // We only need to update the index since the other data in the ArtField cannot be updated.
1430 field.SetDexFieldIndex(dex_file_->GetIndexForFieldId(*new_field_id));
1431 }
1432 }
Alex Light200b9d72016-12-15 11:34:13 -08001433}
1434
1435// Performs updates to class that will allow us to verify it.
Alex Lighta7e38d82017-01-19 14:57:28 -08001436void Redefiner::ClassRedefinition::UpdateClass(
1437 art::ObjPtr<art::mirror::Class> mclass,
1438 art::ObjPtr<art::mirror::DexCache> new_dex_cache,
Alex Light2f814aa2017-03-24 15:21:34 +00001439 art::ObjPtr<art::mirror::Object> original_dex_file) {
Alex Light6ac57502017-01-19 15:05:06 -08001440 DCHECK_EQ(dex_file_->NumClassDefs(), 1u);
1441 const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(0);
Vladimir Marko5122e6b2017-08-17 16:10:09 +01001442 UpdateMethods(mclass, class_def);
Alex Light007ada22017-01-10 13:33:56 -08001443 UpdateFields(mclass);
Alex Light200b9d72016-12-15 11:34:13 -08001444
Alex Lighta01de592016-11-15 10:43:06 -08001445 // Update the class fields.
1446 // Need to update class last since the ArtMethod gets its DexFile from the class (which is needed
1447 // to call GetReturnTypeDescriptor and GetParameterTypeList above).
1448 mclass->SetDexCache(new_dex_cache.Ptr());
Alex Light6ac57502017-01-19 15:05:06 -08001449 mclass->SetDexClassDefIndex(dex_file_->GetIndexForClassDef(class_def));
Alex Light0e692732017-01-10 15:00:05 -08001450 mclass->SetDexTypeIndex(dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(class_sig_.c_str())));
Alex Lighta7e38d82017-01-19 14:57:28 -08001451 art::ObjPtr<art::mirror::ClassExt> ext(mclass->GetExtData());
1452 CHECK(!ext.IsNull());
Alex Light2f814aa2017-03-24 15:21:34 +00001453 ext->SetOriginalDexFile(original_dex_file);
Alex Lighta01de592016-11-15 10:43:06 -08001454}
1455
Alex Light1e3926a2017-04-07 10:38:06 -07001456// Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no new
1457// obsolete methods).
1458void Redefiner::ClassRedefinition::RestoreObsoleteMethodMapsIfUnneeded(
1459 const RedefinitionDataIter* cur_data) {
1460 art::mirror::Class* klass = GetMirrorClass();
1461 art::mirror::ClassExt* ext = klass->GetExtData();
1462 art::mirror::PointerArray* methods = ext->GetObsoleteMethods();
Alex Light70713df2017-04-18 13:03:31 -07001463 art::mirror::PointerArray* old_methods = cur_data->GetOldObsoleteMethods();
1464 int32_t old_length = old_methods == nullptr ? 0 : old_methods->GetLength();
Alex Light1e3926a2017-04-07 10:38:06 -07001465 int32_t expected_length =
1466 old_length + klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods();
1467 // Check to make sure we are only undoing this one.
1468 if (expected_length == methods->GetLength()) {
Alex Light70713df2017-04-18 13:03:31 -07001469 for (int32_t i = 0; i < expected_length; i++) {
1470 art::ArtMethod* expected = nullptr;
1471 if (i < old_length) {
1472 expected = old_methods->GetElementPtrSize<art::ArtMethod*>(i, art::kRuntimePointerSize);
1473 }
1474 if (methods->GetElementPtrSize<art::ArtMethod*>(i, art::kRuntimePointerSize) != expected) {
Alex Light1e3926a2017-04-07 10:38:06 -07001475 // We actually have some new obsolete methods. Just abort since we cannot safely shrink the
1476 // obsolete methods array.
1477 return;
1478 }
1479 }
1480 // No new obsolete methods! We can get rid of the maps.
1481 ext->SetObsoleteArrays(cur_data->GetOldObsoleteMethods(), cur_data->GetOldDexCaches());
1482 }
1483}
1484
Alex Lighta01de592016-11-15 10:43:06 -08001485// This function does all (java) allocations we need to do for the Class being redefined.
1486// TODO Change this name maybe?
Alex Light1e3926a2017-04-07 10:38:06 -07001487bool Redefiner::ClassRedefinition::EnsureClassAllocationsFinished(
1488 /*out*/RedefinitionDataIter* cur_data) {
Alex Light0e692732017-01-10 15:00:05 -08001489 art::StackHandleScope<2> hs(driver_->self_);
1490 art::Handle<art::mirror::Class> klass(hs.NewHandle(
1491 driver_->self_->DecodeJObject(klass_)->AsClass()));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001492 if (klass == nullptr) {
Alex Lighta01de592016-11-15 10:43:06 -08001493 RecordFailure(ERR(INVALID_CLASS), "Unable to decode class argument!");
1494 return false;
1495 }
1496 // Allocate the classExt
Alex Light0e692732017-01-10 15:00:05 -08001497 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(driver_->self_)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001498 if (ext == nullptr) {
Alex Lighta01de592016-11-15 10:43:06 -08001499 // No memory. Clear exception (it's not useful) and return error.
Alex Light0e692732017-01-10 15:00:05 -08001500 driver_->self_->AssertPendingOOMException();
1501 driver_->self_->ClearException();
Alex Lighta01de592016-11-15 10:43:06 -08001502 RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate ClassExt");
1503 return false;
1504 }
Alex Light1e3926a2017-04-07 10:38:06 -07001505 // First save the old values of the 2 arrays that make up the obsolete methods maps. Then
1506 // allocate the 2 arrays that make up the obsolete methods map. Since the contents of the arrays
Alex Lighta01de592016-11-15 10:43:06 -08001507 // are only modified when all threads (other than the modifying one) are suspended we don't need
1508 // to worry about missing the unsyncronized writes to the array. We do synchronize when setting it
1509 // however, since that can happen at any time.
Alex Light1e3926a2017-04-07 10:38:06 -07001510 cur_data->SetOldObsoleteMethods(ext->GetObsoleteMethods());
1511 cur_data->SetOldDexCaches(ext->GetObsoleteDexCaches());
1512 if (!ext->ExtendObsoleteArrays(
1513 driver_->self_, klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size())) {
1514 // OOM. Clear exception and return error.
1515 driver_->self_->AssertPendingOOMException();
1516 driver_->self_->ClearException();
1517 RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate/extend obsolete methods map");
1518 return false;
Alex Lighta01de592016-11-15 10:43:06 -08001519 }
1520 return true;
1521}
1522
1523} // namespace openjdkjvmti