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