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