1 /* Copyright (C) 2016 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32 #include "ti_method.h"
33
34 #include <initializer_list>
35 #include <type_traits>
36 #include <variant>
37
38 #include "android-base/macros.h"
39 #include "arch/context.h"
40 #include "art_jvmti.h"
41 #include "art_method-inl.h"
42 #include "base/globals.h"
43 #include "base/macros.h"
44 #include "base/mutex-inl.h"
45 #include "base/pointer_size.h"
46 #include "deopt_manager.h"
47 #include "dex/code_item_accessors-inl.h"
48 #include "dex/code_item_accessors.h"
49 #include "dex/dex_file_annotations.h"
50 #include "dex/dex_file_types.h"
51 #include "dex/dex_instruction.h"
52 #include "dex/dex_instruction_iterator.h"
53 #include "dex/modifiers.h"
54 #include "dex/primitive.h"
55 #include "events-inl.h"
56 #include "gc_root-inl.h"
57 #include "handle.h"
58 #include "jit/jit.h"
59 #include "jni/jni_internal.h"
60 #include "jvmti.h"
61 #include "mirror/class-inl.h"
62 #include "mirror/class_loader.h"
63 #include "mirror/object-inl.h"
64 #include "mirror/object_array-inl.h"
65 #include "nativehelper/scoped_local_ref.h"
66 #include "oat/oat_file.h"
67 #include "obj_ptr.h"
68 #include "runtime.h"
69 #include "runtime_callbacks.h"
70 #include "scoped_thread_state_change-inl.h"
71 #include "scoped_thread_state_change.h"
72 #include "stack.h"
73 #include "thread-current-inl.h"
74 #include "thread.h"
75 #include "thread_list.h"
76 #include "ti_logging.h"
77 #include "ti_stack.h"
78 #include "ti_thread.h"
79 #include "ti_phase.h"
80 #include "verifier/register_line-inl.h"
81 #include "verifier/reg_type-inl.h"
82 #include "verifier/method_verifier-inl.h"
83
84 namespace openjdkjvmti {
85
86 struct TiMethodCallback : public art::MethodCallback {
RegisterNativeMethodopenjdkjvmti::TiMethodCallback87 void RegisterNativeMethod(art::ArtMethod* method,
88 const void* cur_method,
89 /*out*/void** new_method)
90 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
91 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kNativeMethodBind)) {
92 art::Thread* thread = art::Thread::Current();
93 art::JNIEnvExt* jnienv = thread->GetJniEnv();
94 ScopedLocalRef<jthread> thread_jni(
95 jnienv, PhaseUtil::IsLivePhase() ? jnienv->AddLocalReference<jthread>(thread->GetPeer())
96 : nullptr);
97 jmethodID method_id = art::jni::EncodeArtMethod(method);
98 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
99 event_handler->DispatchEvent<ArtJvmtiEvent::kNativeMethodBind>(
100 thread,
101 static_cast<JNIEnv*>(jnienv),
102 thread_jni.get(),
103 method_id,
104 const_cast<void*>(cur_method),
105 new_method);
106 }
107 }
108
109 EventHandler* event_handler = nullptr;
110 };
111
112 TiMethodCallback gMethodCallback;
113
Register(EventHandler * handler)114 void MethodUtil::Register(EventHandler* handler) {
115 gMethodCallback.event_handler = handler;
116 art::ScopedThreadStateChange stsc(art::Thread::Current(),
117 art::ThreadState::kWaitingForDebuggerToAttach);
118 art::ScopedSuspendAll ssa("Add method callback");
119 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
120 callbacks->AddMethodCallback(&gMethodCallback);
121 }
122
Unregister()123 void MethodUtil::Unregister() {
124 art::ScopedThreadStateChange stsc(art::Thread::Current(),
125 art::ThreadState::kWaitingForDebuggerToAttach);
126 art::ScopedSuspendAll ssa("Remove method callback");
127 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
128 callbacks->RemoveMethodCallback(&gMethodCallback);
129 }
130
GetBytecodes(jvmtiEnv * env,jmethodID method,jint * size_ptr,unsigned char ** bytecode_ptr)131 jvmtiError MethodUtil::GetBytecodes(jvmtiEnv* env,
132 jmethodID method,
133 jint* size_ptr,
134 unsigned char** bytecode_ptr) {
135 if (method == nullptr) {
136 return ERR(INVALID_METHODID);
137 }
138 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
139
140 if (art_method->IsNative()) {
141 return ERR(NATIVE_METHOD);
142 }
143
144 if (size_ptr == nullptr || bytecode_ptr == nullptr) {
145 return ERR(NULL_POINTER);
146 }
147
148 art::ScopedObjectAccess soa(art::Thread::Current());
149 art::CodeItemInstructionAccessor accessor(art_method->DexInstructions());
150 if (!accessor.HasCodeItem()) {
151 *size_ptr = 0;
152 *bytecode_ptr = nullptr;
153 return OK;
154 }
155 // 2 bytes per instruction for dex code.
156 *size_ptr = accessor.InsnsSizeInCodeUnits() * 2;
157 jvmtiError err = env->Allocate(*size_ptr, bytecode_ptr);
158 if (err != OK) {
159 return err;
160 }
161 memcpy(*bytecode_ptr, accessor.Insns(), *size_ptr);
162 return OK;
163 }
164
GetArgumentsSize(jvmtiEnv * env,jmethodID method,jint * size_ptr)165 jvmtiError MethodUtil::GetArgumentsSize([[maybe_unused]] jvmtiEnv* env,
166 jmethodID method,
167 jint* size_ptr) {
168 if (method == nullptr) {
169 return ERR(INVALID_METHODID);
170 }
171 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
172
173 if (art_method->IsNative()) {
174 return ERR(NATIVE_METHOD);
175 }
176
177 if (size_ptr == nullptr) {
178 return ERR(NULL_POINTER);
179 }
180
181 art::ScopedObjectAccess soa(art::Thread::Current());
182 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
183 // Use the shorty.
184 art::ArtMethod* base_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
185 size_t arg_count = art::ArtMethod::NumArgRegisters(base_method->GetShortyView());
186 if (!base_method->IsStatic()) {
187 arg_count++;
188 }
189 *size_ptr = static_cast<jint>(arg_count);
190 return ERR(NONE);
191 }
192
193 DCHECK(art_method->HasCodeItem());
194 DCHECK_NE(art_method->GetCodeItem(), nullptr);
195 *size_ptr = art_method->DexInstructionData().InsSize();
196
197 return ERR(NONE);
198 }
199
GetLocalVariableTable(jvmtiEnv * env,jmethodID method,jint * entry_count_ptr,jvmtiLocalVariableEntry ** table_ptr)200 jvmtiError MethodUtil::GetLocalVariableTable(jvmtiEnv* env,
201 jmethodID method,
202 jint* entry_count_ptr,
203 jvmtiLocalVariableEntry** table_ptr) {
204 if (method == nullptr) {
205 return ERR(INVALID_METHODID);
206 }
207 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
208
209 if (art_method->IsNative()) {
210 return ERR(NATIVE_METHOD);
211 }
212
213 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
214 return ERR(NULL_POINTER);
215 }
216
217 art::ScopedObjectAccess soa(art::Thread::Current());
218
219 const art::DexFile* const dex_file = art_method->GetDexFile();
220 if (dex_file == nullptr) {
221 return ERR(ABSENT_INFORMATION);
222 }
223
224 // TODO HasCodeItem == false means that the method is abstract (or native, but we check that
225 // earlier). We should check what is returned by the RI in this situation since it's not clear
226 // what the appropriate return value is from the spec.
227 art::CodeItemDebugInfoAccessor accessor(art_method->DexInstructionDebugInfo());
228 if (!accessor.HasCodeItem()) {
229 return ERR(ABSENT_INFORMATION);
230 }
231
232 std::vector<jvmtiLocalVariableEntry> variables;
233 jvmtiError err = OK;
234
235 auto release = [&](jint* out_entry_count_ptr, jvmtiLocalVariableEntry** out_table_ptr) {
236 jlong table_size = sizeof(jvmtiLocalVariableEntry) * variables.size();
237 if (err != OK ||
238 (err = env->Allocate(table_size,
239 reinterpret_cast<unsigned char**>(out_table_ptr))) != OK) {
240 for (jvmtiLocalVariableEntry& e : variables) {
241 env->Deallocate(reinterpret_cast<unsigned char*>(e.name));
242 env->Deallocate(reinterpret_cast<unsigned char*>(e.signature));
243 env->Deallocate(reinterpret_cast<unsigned char*>(e.generic_signature));
244 }
245 return err;
246 }
247 *out_entry_count_ptr = variables.size();
248 memcpy(*out_table_ptr, variables.data(), table_size);
249 return OK;
250 };
251
252 // To avoid defining visitor in the same line as the `if`. We define the lambda and use std::move.
253 auto visitor = [&](const art::DexFile::LocalInfo& entry) {
254 if (err != OK) {
255 return;
256 }
257 JvmtiUniquePtr<char[]> name_str = CopyString(env, entry.name_, &err);
258 if (err != OK) {
259 return;
260 }
261 JvmtiUniquePtr<char[]> sig_str = CopyString(env, entry.descriptor_, &err);
262 if (err != OK) {
263 return;
264 }
265 JvmtiUniquePtr<char[]> generic_sig_str = CopyString(env, entry.signature_, &err);
266 if (err != OK) {
267 return;
268 }
269 variables.push_back({
270 .start_location = static_cast<jlocation>(entry.start_address_),
271 .length = static_cast<jint>(entry.end_address_ - entry.start_address_),
272 .name = name_str.release(),
273 .signature = sig_str.release(),
274 .generic_signature = generic_sig_str.release(),
275 .slot = entry.reg_,
276 });
277 };
278
279 if (!accessor.DecodeDebugLocalInfo(
280 art_method->IsStatic(), art_method->GetDexMethodIndex(), std::move(visitor))) {
281 // Something went wrong with decoding the debug information. It might as well not be there.
282 return ERR(ABSENT_INFORMATION);
283 }
284 return release(entry_count_ptr, table_ptr);
285 }
286
GetMaxLocals(jvmtiEnv * env,jmethodID method,jint * max_ptr)287 jvmtiError MethodUtil::GetMaxLocals([[maybe_unused]] jvmtiEnv* env,
288 jmethodID method,
289 jint* max_ptr) {
290 if (method == nullptr) {
291 return ERR(INVALID_METHODID);
292 }
293 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
294
295 if (art_method->IsNative()) {
296 return ERR(NATIVE_METHOD);
297 }
298
299 if (max_ptr == nullptr) {
300 return ERR(NULL_POINTER);
301 }
302
303 art::ScopedObjectAccess soa(art::Thread::Current());
304 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
305 // This isn't specified as an error case, so return 0.
306 *max_ptr = 0;
307 return ERR(NONE);
308 }
309
310 DCHECK(art_method->HasCodeItem());
311 DCHECK_NE(art_method->GetCodeItem(), nullptr);
312 *max_ptr = art_method->DexInstructionData().RegistersSize();
313
314 return ERR(NONE);
315 }
316
GetMethodName(jvmtiEnv * env,jmethodID method,char ** name_ptr,char ** signature_ptr,char ** generic_ptr)317 jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
318 jmethodID method,
319 char** name_ptr,
320 char** signature_ptr,
321 char** generic_ptr) {
322 art::ScopedObjectAccess soa(art::Thread::Current());
323 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
324 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
325
326 JvmtiUniquePtr<char[]> name_copy;
327 if (name_ptr != nullptr) {
328 const char* method_name = art_method->GetName();
329 if (method_name == nullptr) {
330 method_name = "<error>";
331 }
332 jvmtiError ret;
333 name_copy = CopyString(env, method_name, &ret);
334 if (name_copy == nullptr) {
335 return ret;
336 }
337 *name_ptr = name_copy.get();
338 }
339
340 JvmtiUniquePtr<char[]> signature_copy;
341 if (signature_ptr != nullptr) {
342 const art::Signature sig = art_method->GetSignature();
343 std::string str = sig.ToString();
344 jvmtiError ret;
345 signature_copy = CopyString(env, str.c_str(), &ret);
346 if (signature_copy == nullptr) {
347 return ret;
348 }
349 *signature_ptr = signature_copy.get();
350 }
351
352 if (generic_ptr != nullptr) {
353 *generic_ptr = nullptr;
354 if (!art_method->GetDeclaringClass()->IsProxyClass()) {
355 art::ObjPtr<art::mirror::ObjectArray<art::mirror::String>> str_array =
356 art::annotations::GetSignatureAnnotationForMethod(art_method);
357 if (str_array != nullptr) {
358 std::ostringstream oss;
359 for (auto str : str_array->Iterate()) {
360 oss << str->ToModifiedUtf8();
361 }
362 std::string output_string = oss.str();
363 jvmtiError ret;
364 JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
365 if (generic_copy == nullptr) {
366 return ret;
367 }
368 *generic_ptr = generic_copy.release();
369 } else if (soa.Self()->IsExceptionPending()) {
370 // TODO: Should we report an error here?
371 soa.Self()->ClearException();
372 }
373 }
374 }
375
376 // Everything is fine, release the buffers.
377 name_copy.release();
378 signature_copy.release();
379
380 return ERR(NONE);
381 }
382
GetMethodDeclaringClass(jvmtiEnv * env,jmethodID method,jclass * declaring_class_ptr)383 jvmtiError MethodUtil::GetMethodDeclaringClass([[maybe_unused]] jvmtiEnv* env,
384 jmethodID method,
385 jclass* declaring_class_ptr) {
386 if (declaring_class_ptr == nullptr) {
387 return ERR(NULL_POINTER);
388 }
389
390 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
391 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
392
393 art::ScopedObjectAccess soa(art::Thread::Current());
394 art::ObjPtr<art::mirror::Class> klass = art_method->GetDeclaringClass();
395 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
396
397 return ERR(NONE);
398 }
399
GetMethodLocation(jvmtiEnv * env,jmethodID method,jlocation * start_location_ptr,jlocation * end_location_ptr)400 jvmtiError MethodUtil::GetMethodLocation([[maybe_unused]] jvmtiEnv* env,
401 jmethodID method,
402 jlocation* start_location_ptr,
403 jlocation* end_location_ptr) {
404 if (method == nullptr) {
405 return ERR(INVALID_METHODID);
406 }
407 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
408
409 if (art_method->IsNative()) {
410 return ERR(NATIVE_METHOD);
411 }
412
413 if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
414 return ERR(NULL_POINTER);
415 }
416
417 art::ScopedObjectAccess soa(art::Thread::Current());
418 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
419 // This isn't specified as an error case, so return -1/-1 as the RI does.
420 *start_location_ptr = -1;
421 *end_location_ptr = -1;
422 return ERR(NONE);
423 }
424
425 DCHECK(art_method->HasCodeItem());
426 DCHECK_NE(art_method->GetCodeItem(), nullptr);
427 *start_location_ptr = 0;
428 *end_location_ptr = art_method->DexInstructions().InsnsSizeInCodeUnits() - 1;
429
430 return ERR(NONE);
431 }
432
GetMethodModifiers(jvmtiEnv * env,jmethodID method,jint * modifiers_ptr)433 jvmtiError MethodUtil::GetMethodModifiers([[maybe_unused]] jvmtiEnv* env,
434 jmethodID method,
435 jint* modifiers_ptr) {
436 if (modifiers_ptr == nullptr) {
437 return ERR(NULL_POINTER);
438 }
439
440 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
441 uint32_t modifiers = art_method->GetAccessFlags();
442
443 // Note: Keep this code in sync with Executable.fixMethodFlags.
444 if ((modifiers & art::kAccAbstract) != 0) {
445 modifiers &= ~art::kAccNative;
446 }
447 modifiers &= ~art::kAccSynchronized;
448 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
449 modifiers |= art::kAccSynchronized;
450 }
451 modifiers &= art::kAccJavaFlagsMask;
452
453 *modifiers_ptr = modifiers;
454 return ERR(NONE);
455 }
456
GetLineNumberTable(jvmtiEnv * env,jmethodID method,jint * entry_count_ptr,jvmtiLineNumberEntry ** table_ptr)457 jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
458 jmethodID method,
459 jint* entry_count_ptr,
460 jvmtiLineNumberEntry** table_ptr) {
461 if (method == nullptr) {
462 return ERR(NULL_POINTER);
463 }
464 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
465 DCHECK(!art_method->IsRuntimeMethod());
466
467 art::CodeItemDebugInfoAccessor accessor;
468 const art::DexFile* dex_file;
469 {
470 art::ScopedObjectAccess soa(art::Thread::Current());
471
472 if (art_method->IsProxyMethod()) {
473 return ERR(ABSENT_INFORMATION);
474 }
475 if (art_method->IsNative()) {
476 return ERR(NATIVE_METHOD);
477 }
478 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
479 return ERR(NULL_POINTER);
480 }
481
482 accessor = art::CodeItemDebugInfoAccessor(art_method->DexInstructionDebugInfo());
483 dex_file = art_method->GetDexFile();
484 DCHECK(accessor.HasCodeItem()) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
485 }
486
487 std::vector<jvmtiLineNumberEntry> context;
488 bool success = accessor.DecodeDebugPositionInfo([&](const art::DexFile::PositionInfo& entry) {
489 context.push_back({static_cast<jlocation>(entry.address_), static_cast<jint>(entry.line_)});
490 return false;
491 });
492 if (!success) {
493 return ERR(ABSENT_INFORMATION);
494 }
495
496 unsigned char* data;
497 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
498 jvmtiError alloc_error = env->Allocate(mem_size, &data);
499 if (alloc_error != ERR(NONE)) {
500 return alloc_error;
501 }
502 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
503 memcpy(*table_ptr, context.data(), mem_size);
504 *entry_count_ptr = static_cast<jint>(context.size());
505
506 return ERR(NONE);
507 }
508
509 template <typename T>
IsMethodT(jvmtiEnv * env,jmethodID method,T test,jboolean * is_t_ptr)510 static jvmtiError IsMethodT([[maybe_unused]] jvmtiEnv* env,
511 jmethodID method,
512 T test,
513 jboolean* is_t_ptr) {
514 if (method == nullptr) {
515 return ERR(INVALID_METHODID);
516 }
517 if (is_t_ptr == nullptr) {
518 return ERR(NULL_POINTER);
519 }
520
521 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
522 *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
523
524 return ERR(NONE);
525 }
526
IsMethodNative(jvmtiEnv * env,jmethodID m,jboolean * is_native_ptr)527 jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
528 auto test = [](art::ArtMethod* method) {
529 return method->IsNative();
530 };
531 return IsMethodT(env, m, test, is_native_ptr);
532 }
533
IsMethodObsolete(jvmtiEnv * env,jmethodID m,jboolean * is_obsolete_ptr)534 jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
535 auto test = [](art::ArtMethod* method) {
536 return method->IsObsolete();
537 };
538 return IsMethodT(env, m, test, is_obsolete_ptr);
539 }
540
IsMethodSynthetic(jvmtiEnv * env,jmethodID m,jboolean * is_synthetic_ptr)541 jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
542 auto test = [](art::ArtMethod* method) {
543 return method->IsSynthetic();
544 };
545 return IsMethodT(env, m, test, is_synthetic_ptr);
546 }
547
548 class CommonLocalVariableClosure : public art::Closure {
549 public:
550 // The verifier isn't always able to be as specific as the local-variable-table. We can only get
551 // 32-bit, 64-bit or reference.
552 enum class VerifierPrimitiveType {
553 k32BitValue, // float, int, short, char, boolean, byte
554 k64BitValue, // double, long
555 kReferenceValue, // Object
556 kZeroValue, // null or zero constant. Might be either k32BitValue or kReferenceValue
557 };
558
559 using SlotType = std::variant<art::Primitive::Type, VerifierPrimitiveType>;
560
CommonLocalVariableClosure(jvmtiEnv * jvmti,jint depth,jint slot)561 CommonLocalVariableClosure(jvmtiEnv* jvmti, jint depth, jint slot)
562 : jvmti_(jvmti), result_(ERR(INTERNAL)), depth_(depth), slot_(slot) {}
563
Run(art::Thread * self)564 void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
565 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
566 bool needs_instrument;
567 {
568 art::ScopedAssertNoThreadSuspension sants("CommonLocalVariableClosure::Run");
569 std::unique_ptr<art::Context> context(art::Context::Create());
570 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
571 visitor.WalkStack();
572 if (!visitor.FoundFrame()) {
573 // Must have been a bad depth.
574 result_ = ERR(NO_MORE_FRAMES);
575 return;
576 }
577 art::ArtMethod* method = visitor.GetMethod();
578 // Native and 'art' proxy methods don't have registers.
579 if (method->IsNative() || method->IsProxyMethod()) {
580 // TODO It might be useful to fake up support for get at least on proxy frames.
581 result_ = ERR(OPAQUE_FRAME);
582 return;
583 } else if (slot_ >= method->DexInstructionData().RegistersSize() || slot_ < 0) {
584 result_ = ERR(INVALID_SLOT);
585 return;
586 }
587 needs_instrument = !visitor.IsShadowFrame();
588 uint32_t pc = visitor.GetDexPc(/*abort_on_failure=*/false);
589 if (pc == art::dex::kDexNoIndex) {
590 // Cannot figure out current PC.
591 result_ = ERR(OPAQUE_FRAME);
592 return;
593 }
594 std::string descriptor;
595 SlotType slot_type{ art::Primitive::kPrimVoid };
596 jvmtiError err = GetSlotType(method, pc, &descriptor, &slot_type);
597 if (err != OK) {
598 result_ = err;
599 return;
600 }
601
602 err = GetTypeError(method, slot_type, descriptor);
603 if (err != OK) {
604 result_ = err;
605 return;
606 }
607 result_ = Execute(method, visitor);
608 }
609 if (needs_instrument) {
610 DeoptManager::Get()->DeoptimizeThread(self);
611 }
612 }
613
GetResult()614 virtual jvmtiError GetResult() {
615 return result_;
616 }
617
618 protected:
619 virtual jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
620 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
621 virtual jvmtiError GetTypeError(art::ArtMethod* method,
622 SlotType type,
623 const std::string& descriptor)
624 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
625
626 jvmtiError GetSlotType(art::ArtMethod* method,
627 uint32_t dex_pc,
628 /*out*/std::string* descriptor,
629 /*out*/SlotType* type)
630 REQUIRES_SHARED(art::Locks::mutator_lock_);
631
InferSlotTypeFromVerifier(art::ArtMethod * method,uint32_t dex_pc,std::string * descriptor,SlotType * type)632 jvmtiError InferSlotTypeFromVerifier(art::ArtMethod* method,
633 uint32_t dex_pc,
634 /*out*/ std::string* descriptor,
635 /*out*/ SlotType* type)
636 REQUIRES_SHARED(art::Locks::mutator_lock_) {
637 art::StackHandleScope<2> hs(art::Thread::Current());
638 art::Handle<art::mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
639 art::Handle<art::mirror::ClassLoader> class_loader(hs.NewHandle(method->GetClassLoader()));
640 std::unique_ptr<art::verifier::MethodVerifier> verifier(
641 art::verifier::MethodVerifier::CalculateVerificationInfo(
642 art::Thread::Current(),
643 method,
644 dex_cache,
645 class_loader,
646 dex_pc));
647 if (verifier == nullptr) {
648 JVMTI_LOG(WARNING, jvmti_) << "Unable to extract verification information from "
649 << method->PrettyMethod() << " due to hard verification failures! "
650 << "How did this method even get loaded!";
651 return ERR(INTERNAL);
652 }
653 art::verifier::RegisterLine* line = verifier->GetRegLine(dex_pc);
654 if (line == nullptr) {
655 JVMTI_LOG(WARNING, jvmti_) << "Unable to determine register line at dex-pc " << dex_pc
656 << " for method " << method->PrettyMethod();
657 return ERR(OPAQUE_FRAME);
658 }
659 const art::verifier::RegType& rt = line->GetRegisterType(verifier.get(), slot_);
660 if (rt.IsUndefined()) {
661 return ERR(INVALID_SLOT);
662 } else if (rt.IsNonZeroReferenceTypes() || rt.IsNull()) {
663 *descriptor = (rt.HasClass() ? rt.GetDescriptor() : "Ljava/lang/Object;");
664 *type = VerifierPrimitiveType::kReferenceValue;
665 return OK;
666 } else if (rt.IsZero()) {
667 *descriptor = "I";
668 *type = VerifierPrimitiveType::kZeroValue;
669 return OK;
670 } else if (rt.IsCategory1Types()) {
671 *descriptor = "I";
672 *type = VerifierPrimitiveType::k32BitValue;
673 return OK;
674 } else if (rt.IsCategory2Types() && rt.IsLowHalf()) {
675 *descriptor = "J";
676 *type = VerifierPrimitiveType::k64BitValue;
677 return OK;
678 } else {
679 // The slot doesn't have a type. Must not be valid here.
680 return ERR(INVALID_SLOT);
681 }
682 }
683
SquashType(SlotType t)684 constexpr VerifierPrimitiveType SquashType(SlotType t) {
685 if (std::holds_alternative<art::Primitive::Type>(t)) {
686 switch (std::get<art::Primitive::Type>(t)) {
687 // 32-bit primitives
688 case art::Primitive::kPrimByte:
689 case art::Primitive::kPrimChar:
690 case art::Primitive::kPrimInt:
691 case art::Primitive::kPrimShort:
692 case art::Primitive::kPrimBoolean:
693 case art::Primitive::kPrimFloat:
694 return VerifierPrimitiveType::k32BitValue;
695 // 64-bit primitives
696 case art::Primitive::kPrimLong:
697 case art::Primitive::kPrimDouble:
698 return VerifierPrimitiveType::k64BitValue;
699 case art::Primitive::kPrimNot:
700 return VerifierPrimitiveType::kReferenceValue;
701 case art::Primitive::kPrimVoid:
702 LOG(FATAL) << "Got kPrimVoid";
703 UNREACHABLE();
704 }
705 } else {
706 return std::get<VerifierPrimitiveType>(t);
707 }
708 }
709
710 jvmtiEnv* jvmti_;
711 jvmtiError result_;
712 jint depth_;
713 jint slot_;
714
715 private:
716 DISALLOW_COPY_AND_ASSIGN(CommonLocalVariableClosure);
717 };
718
operator <<(std::ostream & os,CommonLocalVariableClosure::VerifierPrimitiveType state)719 std::ostream& operator<<(std::ostream& os,
720 CommonLocalVariableClosure::VerifierPrimitiveType state) {
721 switch (state) {
722 case CommonLocalVariableClosure::VerifierPrimitiveType::k32BitValue:
723 return os << "32BitValue";
724 case CommonLocalVariableClosure::VerifierPrimitiveType::k64BitValue:
725 return os << "64BitValue";
726 case CommonLocalVariableClosure::VerifierPrimitiveType::kReferenceValue:
727 return os << "ReferenceValue";
728 case CommonLocalVariableClosure::VerifierPrimitiveType::kZeroValue:
729 return os << "ZeroValue";
730 }
731 }
732
operator <<(std::ostream & os,CommonLocalVariableClosure::SlotType state)733 std::ostream& operator<<(std::ostream& os, CommonLocalVariableClosure::SlotType state) {
734 if (std::holds_alternative<art::Primitive::Type>(state)) {
735 return os << "Primitive::Type[" << std::get<art::Primitive::Type>(state) << "]";
736 } else {
737 return os << "VerifierPrimitiveType["
738 << std::get<CommonLocalVariableClosure::VerifierPrimitiveType>(state) << "]";
739 }
740 }
741
GetSlotType(art::ArtMethod * method,uint32_t dex_pc,std::string * descriptor,SlotType * type)742 jvmtiError CommonLocalVariableClosure::GetSlotType(art::ArtMethod* method,
743 uint32_t dex_pc,
744 /*out*/ std::string* descriptor,
745 /*out*/ SlotType* type) {
746 const art::DexFile* dex_file = method->GetDexFile();
747 if (dex_file == nullptr) {
748 return ERR(OPAQUE_FRAME);
749 }
750 art::CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
751 if (!accessor.HasCodeItem()) {
752 return ERR(OPAQUE_FRAME);
753 }
754 bool found = false;
755 *type = art::Primitive::kPrimVoid;
756 descriptor->clear();
757 // To avoid defining visitor in the same line as the `if`. We define the lambda and use std::move.
758 auto visitor = [&](const art::DexFile::LocalInfo& entry) {
759 if (!found && entry.start_address_ <= dex_pc && entry.end_address_ > dex_pc &&
760 entry.reg_ == slot_) {
761 found = true;
762 *type = art::Primitive::GetType(entry.descriptor_[0]);
763 *descriptor = entry.descriptor_;
764 }
765 };
766 if (!accessor.DecodeDebugLocalInfo(
767 method->IsStatic(), method->GetDexMethodIndex(), std::move(visitor)) ||
768 !found) {
769 // Something went wrong with decoding the debug information. It might as well not be there.
770 // Try to find the type with the verifier.
771 // TODO This is very slow.
772 return InferSlotTypeFromVerifier(method, dex_pc, descriptor, type);
773 } else if (art::kIsDebugBuild) {
774 std::string type_unused;
775 SlotType verifier_type{ art::Primitive::kPrimVoid };
776 DCHECK_EQ(InferSlotTypeFromVerifier(method, dex_pc, &type_unused, &verifier_type), OK)
777 << method->PrettyMethod() << " failed to verify!";
778 if (*type == SlotType{ art::Primitive::kPrimNot }) {
779 // We cannot distinguish between a constant 0 and a null reference so we return that it is a
780 // 32bit value (Due to the way references are read by the interpreter this is safe even if
781 // it's modified, the value will remain null). This is not ideal since it prevents modifying
782 // locals in some circumstances but generally is not a big deal (since one can just modify it
783 // later once it's been determined to be a reference by a later instruction).
784 DCHECK(verifier_type == SlotType { VerifierPrimitiveType::kZeroValue } ||
785 verifier_type == SlotType { VerifierPrimitiveType::kReferenceValue })
786 << "Verifier disagrees on type of slot! debug: " << *type
787 << " verifier: " << verifier_type;
788 } else if (verifier_type == SlotType { VerifierPrimitiveType::kZeroValue }) {
789 DCHECK(VerifierPrimitiveType::k32BitValue == SquashType(*type) ||
790 VerifierPrimitiveType::kReferenceValue == SquashType(*type))
791 << "Verifier disagrees on type of slot! debug: " << *type
792 << " verifier: " << verifier_type;
793 } else {
794 DCHECK_EQ(SquashType(verifier_type), SquashType(*type))
795 << "Verifier disagrees on type of slot! debug: " << *type
796 << " verifier: " << verifier_type;
797 }
798 }
799 return OK;
800 }
801
802 class GetLocalVariableClosure : public CommonLocalVariableClosure {
803 public:
GetLocalVariableClosure(jvmtiEnv * jvmti,jint depth,jint slot,art::Primitive::Type type,jvalue * val)804 GetLocalVariableClosure(jvmtiEnv* jvmti,
805 jint depth,
806 jint slot,
807 art::Primitive::Type type,
808 jvalue* val)
809 : CommonLocalVariableClosure(jvmti, depth, slot),
810 type_(type),
811 val_(val),
812 obj_val_(nullptr) {}
813
GetResult()814 jvmtiError GetResult() override REQUIRES_SHARED(art::Locks::mutator_lock_) {
815 if (result_ == OK && type_ == art::Primitive::kPrimNot) {
816 if (obj_val_ == nullptr) {
817 val_->l = nullptr;
818 } else {
819 art::JNIEnvExt* jni = art::Thread::Current()->GetJniEnv();
820 val_->l = static_cast<JNIEnv*>(jni)->NewLocalRef(obj_val_);
821 jni->DeleteGlobalRef(obj_val_);
822 obj_val_ = nullptr;
823 }
824 }
825 return CommonLocalVariableClosure::GetResult();
826 }
827
828 protected:
829 jvmtiError
GetTypeError(art::ArtMethod * method,SlotType slot_type,const std::string & descriptor)830 GetTypeError(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor) override
831 REQUIRES_SHARED(art::Locks::mutator_lock_) {
832 jvmtiError res = GetTypeErrorInner(method, slot_type, descriptor);
833 if (res == ERR(TYPE_MISMATCH)) {
834 JVMTI_LOG(INFO, jvmti_) << "Unable to Get local variable in slot " << slot_ << ". Expected"
835 << " slot to be of type compatible with " << SlotType { type_ }
836 << " but slot is " << slot_type;
837 } else if (res != OK) {
838 JVMTI_LOG(INFO, jvmti_) << "Unable to get local variable in slot " << slot_ << ".";
839 }
840 return res;
841 }
842
GetTypeErrorInner(art::ArtMethod * method,SlotType slot_type,const std::string & descriptor)843 jvmtiError GetTypeErrorInner([[maybe_unused]] art::ArtMethod* method,
844 SlotType slot_type,
845 [[maybe_unused]] const std::string& descriptor)
846 REQUIRES_SHARED(art::Locks::mutator_lock_) {
847 switch (type_) {
848 case art::Primitive::kPrimFloat:
849 case art::Primitive::kPrimInt: {
850 if (std::holds_alternative<VerifierPrimitiveType>(slot_type)) {
851 return (slot_type == SlotType { VerifierPrimitiveType::k32BitValue } ||
852 slot_type == SlotType { VerifierPrimitiveType::kZeroValue })
853 ? OK
854 : ERR(TYPE_MISMATCH);
855 } else if (type_ == art::Primitive::kPrimFloat ||
856 slot_type == SlotType { art::Primitive::kPrimFloat }) {
857 // Check that we are actually a float.
858 return (SlotType { type_ } == slot_type) ? OK : ERR(TYPE_MISMATCH);
859 } else {
860 // Some smaller int type.
861 return SquashType(slot_type) == SquashType(SlotType { type_ }) ? OK : ERR(TYPE_MISMATCH);
862 }
863 }
864 case art::Primitive::kPrimLong:
865 case art::Primitive::kPrimDouble: {
866 // todo
867 if (std::holds_alternative<VerifierPrimitiveType>(slot_type)) {
868 return (slot_type == SlotType { VerifierPrimitiveType::k64BitValue })
869 ? OK
870 : ERR(TYPE_MISMATCH);
871 } else {
872 return slot_type == SlotType { type_ } ? OK : ERR(TYPE_MISMATCH);
873 }
874 }
875 case art::Primitive::kPrimNot:
876 return (SquashType(slot_type) == VerifierPrimitiveType::kReferenceValue ||
877 SquashType(slot_type) == VerifierPrimitiveType::kZeroValue)
878 ? OK
879 : ERR(TYPE_MISMATCH);
880 case art::Primitive::kPrimShort:
881 case art::Primitive::kPrimChar:
882 case art::Primitive::kPrimByte:
883 case art::Primitive::kPrimBoolean:
884 case art::Primitive::kPrimVoid:
885 LOG(FATAL) << "Unexpected primitive type " << slot_type;
886 UNREACHABLE();
887 }
888 }
889
Execute(art::ArtMethod * method,art::StackVisitor & visitor)890 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
891 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
892 switch (type_) {
893 case art::Primitive::kPrimNot: {
894 uint32_t ptr_val;
895 if (!visitor.GetVReg(method,
896 static_cast<uint16_t>(slot_),
897 art::kReferenceVReg,
898 &ptr_val)) {
899 return ERR(OPAQUE_FRAME);
900 }
901 art::ObjPtr<art::mirror::Object> obj(reinterpret_cast<art::mirror::Object*>(ptr_val));
902 obj_val_ = art::Runtime::Current()->GetJavaVM()->AddGlobalRef(art::Thread::Current(), obj);
903 break;
904 }
905 case art::Primitive::kPrimInt:
906 case art::Primitive::kPrimFloat: {
907 if (!visitor.GetVReg(method,
908 static_cast<uint16_t>(slot_),
909 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg : art::kIntVReg,
910 reinterpret_cast<uint32_t*>(&val_->i))) {
911 return ERR(OPAQUE_FRAME);
912 }
913 break;
914 }
915 case art::Primitive::kPrimDouble:
916 case art::Primitive::kPrimLong: {
917 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
918 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
919 if (!visitor.GetVRegPair(method,
920 static_cast<uint16_t>(slot_),
921 lo_type,
922 high_type,
923 reinterpret_cast<uint64_t*>(&val_->j))) {
924 return ERR(OPAQUE_FRAME);
925 }
926 break;
927 }
928 default: {
929 LOG(FATAL) << "unexpected register type " << type_;
930 UNREACHABLE();
931 }
932 }
933 return OK;
934 }
935
936 private:
937 art::Primitive::Type type_;
938 jvalue* val_;
939 // A global reference to the return value. We use the global reference to safely transfer the
940 // value between threads.
941 jobject obj_val_;
942 };
943
GetLocalVariableGeneric(jvmtiEnv * env,jthread thread,jint depth,jint slot,art::Primitive::Type type,jvalue * val)944 jvmtiError MethodUtil::GetLocalVariableGeneric(jvmtiEnv* env,
945 jthread thread,
946 jint depth,
947 jint slot,
948 art::Primitive::Type type,
949 jvalue* val) {
950 if (depth < 0) {
951 return ERR(ILLEGAL_ARGUMENT);
952 }
953 art::Thread* self = art::Thread::Current();
954 art::ScopedObjectAccess soa(self);
955 art::Locks::thread_list_lock_->ExclusiveLock(self);
956 art::Thread* target = nullptr;
957 jvmtiError err = ERR(INTERNAL);
958 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
959 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
960 return err;
961 }
962 GetLocalVariableClosure c(env, depth, slot, type, val);
963 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
964 if (!target->RequestSynchronousCheckpoint(&c)) {
965 return ERR(THREAD_NOT_ALIVE);
966 } else {
967 return c.GetResult();
968 }
969 }
970
971 class SetLocalVariableClosure : public CommonLocalVariableClosure {
972 public:
SetLocalVariableClosure(jvmtiEnv * jvmti,art::Thread * caller,jint depth,jint slot,art::Primitive::Type type,jvalue val)973 SetLocalVariableClosure(jvmtiEnv* jvmti,
974 art::Thread* caller,
975 jint depth,
976 jint slot,
977 art::Primitive::Type type,
978 jvalue val)
979 : CommonLocalVariableClosure(jvmti, depth, slot), caller_(caller), type_(type), val_(val) {}
980
981 protected:
982 jvmtiError
GetTypeError(art::ArtMethod * method,SlotType slot_type,const std::string & descriptor)983 GetTypeError(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor) override
984 REQUIRES_SHARED(art::Locks::mutator_lock_) {
985 jvmtiError res = GetTypeErrorInner(method, slot_type, descriptor);
986 if (res != OK) {
987 if (res == ERR(TYPE_MISMATCH)) {
988 std::ostringstream desc_exp;
989 std::ostringstream desc_set;
990 if (type_ == art::Primitive::kPrimNot) {
991 desc_exp << " (type: " << descriptor << ")";
992 art::ObjPtr<art::mirror::Object> new_val(art::Thread::Current()->DecodeJObject(val_.l));
993 desc_set << " (type: "
994 << (new_val.IsNull() ? "NULL" : new_val->GetClass()->PrettyDescriptor()) << ")";
995 }
996 JVMTI_LOG(INFO, jvmti_) << "Unable to Set local variable in slot " << slot_ << ". Expected"
997 << " slot to be of type compatible with " << SlotType{ type_ }
998 << desc_set.str() << " but slot is " << slot_type << desc_exp.str();
999 } else {
1000 JVMTI_LOG(INFO, jvmti_) << "Unable to set local variable in slot " << slot_ << ". "
1001 << err_.str();
1002 }
1003 }
1004 return res;
1005 }
1006
1007 jvmtiError
GetTypeErrorInner(art::ArtMethod * method,SlotType slot_type,const std::string & descriptor)1008 GetTypeErrorInner(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor)
1009 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1010 switch (SquashType(SlotType{ type_ })) {
1011 case VerifierPrimitiveType::k32BitValue: {
1012 if (slot_type == SlotType{ VerifierPrimitiveType::kZeroValue }) {
1013 if (val_.i == 0) {
1014 return OK;
1015 } else {
1016 err_ << "Cannot determine if slot " << slot_ << " is a null reference or 32bit "
1017 << "constant. Cannot allow writing to slot.";
1018 return ERR(INTERNAL);
1019 }
1020 } else if (SquashType(slot_type) != VerifierPrimitiveType::k32BitValue) {
1021 return ERR(TYPE_MISMATCH);
1022 } else if (slot_type == SlotType { VerifierPrimitiveType::k32BitValue } ||
1023 slot_type == SlotType { type_ }) {
1024 return OK;
1025 } else if (type_ == art::Primitive::kPrimFloat ||
1026 slot_type == SlotType { art::Primitive::kPrimFloat }) {
1027 // we should have hit the get == type_ above
1028 return ERR(TYPE_MISMATCH);
1029 } else {
1030 // Some smaller type then int.
1031 return OK;
1032 }
1033 }
1034 case VerifierPrimitiveType::k64BitValue: {
1035 if (slot_type == SlotType { VerifierPrimitiveType::k64BitValue } ||
1036 slot_type == SlotType { type_ }) {
1037 return OK;
1038 } else {
1039 return ERR(TYPE_MISMATCH);
1040 }
1041 }
1042 case VerifierPrimitiveType::kReferenceValue: {
1043 if (SquashType(slot_type) != VerifierPrimitiveType::kReferenceValue &&
1044 SquashType(slot_type) != VerifierPrimitiveType::kZeroValue) {
1045 return ERR(TYPE_MISMATCH);
1046 } else if (val_.l == nullptr) {
1047 return OK;
1048 } else if (slot_type == SlotType { VerifierPrimitiveType::kZeroValue }) {
1049 err_ << "Cannot determine if slot " << slot_ << " is a null "
1050 << "reference or 32bit constant. Cannot allow writing to slot.";
1051 return ERR(INTERNAL);
1052 } else {
1053 art::ClassLinker* cl = art::Runtime::Current()->GetClassLinker();
1054 art::ObjPtr<art::mirror::Class> set_class = caller_->DecodeJObject(val_.l)->GetClass();
1055 art::ObjPtr<art::mirror::ClassLoader> loader =
1056 method->GetDeclaringClass()->GetClassLoader();
1057 art::ObjPtr<art::mirror::Class> slot_class =
1058 cl->LookupClass(caller_, descriptor.c_str(), loader);
1059 DCHECK(!slot_class.IsNull()) << descriptor << " slot: " << slot_type;
1060 return slot_class->IsAssignableFrom(set_class) ? OK : ERR(TYPE_MISMATCH);
1061 }
1062 }
1063 case VerifierPrimitiveType::kZeroValue: {
1064 LOG(FATAL) << "Illegal result from SquashType of art::Primitive::Type " << type_;
1065 UNREACHABLE();
1066 }
1067 }
1068 }
1069
Execute(art::ArtMethod * method,art::StackVisitor & visitor)1070 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
1071 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
1072 switch (type_) {
1073 case art::Primitive::kPrimNot: {
1074 if (!visitor.SetVRegReference(method,
1075 static_cast<uint16_t>(slot_),
1076 caller_->DecodeJObject(val_.l))) {
1077 return ERR(OPAQUE_FRAME);
1078 }
1079 break;
1080 }
1081 case art::Primitive::kPrimInt:
1082 case art::Primitive::kPrimFloat: {
1083 if (!visitor.SetVReg(method,
1084 static_cast<uint16_t>(slot_),
1085 static_cast<uint32_t>(val_.i),
1086 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg
1087 : art::kIntVReg)) {
1088 return ERR(OPAQUE_FRAME);
1089 }
1090 break;
1091 }
1092 case art::Primitive::kPrimDouble:
1093 case art::Primitive::kPrimLong: {
1094 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
1095 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
1096 if (!visitor.SetVRegPair(method,
1097 static_cast<uint16_t>(slot_),
1098 static_cast<uint64_t>(val_.j),
1099 lo_type,
1100 high_type)) {
1101 return ERR(OPAQUE_FRAME);
1102 }
1103 break;
1104 }
1105 default: {
1106 LOG(FATAL) << "unexpected register type " << type_;
1107 UNREACHABLE();
1108 }
1109 }
1110 return OK;
1111 }
1112
1113 private:
1114 art::Thread* caller_;
1115 art::Primitive::Type type_;
1116 jvalue val_;
1117 std::ostringstream err_;
1118 };
1119
SetLocalVariableGeneric(jvmtiEnv * env,jthread thread,jint depth,jint slot,art::Primitive::Type type,jvalue val)1120 jvmtiError MethodUtil::SetLocalVariableGeneric(jvmtiEnv* env,
1121 jthread thread,
1122 jint depth,
1123 jint slot,
1124 art::Primitive::Type type,
1125 jvalue val) {
1126 if (depth < 0) {
1127 return ERR(ILLEGAL_ARGUMENT);
1128 }
1129 // Make sure that we know not to do any OSR anymore.
1130 // TODO We should really keep track of this at the Frame granularity.
1131 DeoptManager::Get()->SetLocalsUpdated();
1132 art::Thread* self = art::Thread::Current();
1133 art::ScopedObjectAccess soa(self);
1134 art::Locks::thread_list_lock_->ExclusiveLock(self);
1135 art::Thread* target = nullptr;
1136 jvmtiError err = ERR(INTERNAL);
1137 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
1138 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
1139 return err;
1140 }
1141 SetLocalVariableClosure c(env, self, depth, slot, type, val);
1142 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
1143 if (!target->RequestSynchronousCheckpoint(&c)) {
1144 return ERR(THREAD_NOT_ALIVE);
1145 } else {
1146 return c.GetResult();
1147 }
1148 }
1149
1150 class GetLocalInstanceClosure : public art::Closure {
1151 public:
GetLocalInstanceClosure(jint depth)1152 explicit GetLocalInstanceClosure(jint depth)
1153 : result_(ERR(INTERNAL)),
1154 depth_(depth),
1155 val_(nullptr) {}
1156
Run(art::Thread * self)1157 void Run(art::Thread* self) override REQUIRES(art::Locks::mutator_lock_) {
1158 art::ScopedAssertNoThreadSuspension sants("GetLocalInstanceClosure::Run");
1159 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
1160 std::unique_ptr<art::Context> context(art::Context::Create());
1161 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
1162 visitor.WalkStack();
1163 if (!visitor.FoundFrame()) {
1164 // Must have been a bad depth.
1165 result_ = ERR(NO_MORE_FRAMES);
1166 return;
1167 }
1168 result_ = OK;
1169 val_ = art::GcRoot<art::mirror::Object>(visitor.GetThisObject());
1170 }
1171
GetResult(jobject * data_out)1172 jvmtiError GetResult(jobject* data_out) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1173 if (result_ == OK) {
1174 *data_out = val_.IsNull()
1175 ? nullptr
1176 : art::Thread::Current()->GetJniEnv()->AddLocalReference<jobject>(val_.Read());
1177 }
1178 return result_;
1179 }
1180
1181 private:
1182 jvmtiError result_;
1183 jint depth_;
1184 art::GcRoot<art::mirror::Object> val_;
1185 };
1186
GetLocalInstance(jvmtiEnv * env,jthread thread,jint depth,jobject * data)1187 jvmtiError MethodUtil::GetLocalInstance([[maybe_unused]] jvmtiEnv* env,
1188 jthread thread,
1189 jint depth,
1190 jobject* data) {
1191 if (depth < 0) {
1192 return ERR(ILLEGAL_ARGUMENT);
1193 }
1194 art::Thread* self = art::Thread::Current();
1195 art::ScopedObjectAccess soa(self);
1196 art::Locks::thread_list_lock_->ExclusiveLock(self);
1197 art::Thread* target = nullptr;
1198 jvmtiError err = ERR(INTERNAL);
1199 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
1200 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
1201 return err;
1202 }
1203 art::ScopedAssertNoThreadSuspension sants("Performing GetLocalInstance");
1204 GetLocalInstanceClosure c(depth);
1205 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. We
1206 // need to avoid suspending as we wait for the checkpoint to occur since we are (potentially)
1207 // transfering a GcRoot across threads.
1208 if (!target->RequestSynchronousCheckpoint(&c, art::ThreadState::kRunnable)) {
1209 return ERR(THREAD_NOT_ALIVE);
1210 } else {
1211 return c.GetResult(data);
1212 }
1213 }
1214
1215 #define FOR_JVMTI_JVALUE_TYPES(fn) \
1216 fn(jint, art::Primitive::kPrimInt, i) \
1217 fn(jlong, art::Primitive::kPrimLong, j) \
1218 fn(jfloat, art::Primitive::kPrimFloat, f) \
1219 fn(jdouble, art::Primitive::kPrimDouble, d) \
1220 fn(jobject, art::Primitive::kPrimNot, l)
1221
1222 namespace impl {
1223
1224 template<typename T> void WriteJvalue(T, jvalue*);
1225 template<typename T> void ReadJvalue(jvalue, T*);
1226 template<typename T> art::Primitive::Type GetJNIType();
1227
1228 #define JNI_TYPE_CHAR(type, prim, id) \
1229 template<> art::Primitive::Type GetJNIType<type>() { \
1230 return prim; \
1231 }
1232
1233 FOR_JVMTI_JVALUE_TYPES(JNI_TYPE_CHAR);
1234
1235 #undef JNI_TYPE_CHAR
1236
1237 #define RW_JVALUE(srctype, prim, id) \
1238 template<> void ReadJvalue<srctype>(jvalue in, std::add_pointer<srctype>::type out) { \
1239 *out = in.id; \
1240 } \
1241 template<> void WriteJvalue<srctype>(srctype in, jvalue* out) { \
1242 out->id = in; \
1243 }
1244
1245 FOR_JVMTI_JVALUE_TYPES(RW_JVALUE);
1246
1247 #undef RW_JVALUE
1248
1249 } // namespace impl
1250
1251 template<typename T>
SetLocalVariable(jvmtiEnv * env,jthread thread,jint depth,jint slot,T data)1252 jvmtiError MethodUtil::SetLocalVariable(jvmtiEnv* env,
1253 jthread thread,
1254 jint depth,
1255 jint slot,
1256 T data) {
1257 jvalue v = {.j = 0};
1258 art::Primitive::Type type = impl::GetJNIType<T>();
1259 impl::WriteJvalue(data, &v);
1260 return SetLocalVariableGeneric(env, thread, depth, slot, type, v);
1261 }
1262
1263 template<typename T>
GetLocalVariable(jvmtiEnv * env,jthread thread,jint depth,jint slot,T * data)1264 jvmtiError MethodUtil::GetLocalVariable(jvmtiEnv* env,
1265 jthread thread,
1266 jint depth,
1267 jint slot,
1268 T* data) {
1269 if (data == nullptr) {
1270 return ERR(NULL_POINTER);
1271 }
1272 jvalue v = {.j = 0};
1273 art::Primitive::Type type = impl::GetJNIType<T>();
1274 jvmtiError err = GetLocalVariableGeneric(env, thread, depth, slot, type, &v);
1275 if (err != OK) {
1276 return err;
1277 } else {
1278 impl::ReadJvalue(v, data);
1279 return OK;
1280 }
1281 }
1282
1283 #define GET_SET_LV(srctype, prim, id) \
1284 template jvmtiError MethodUtil::GetLocalVariable<srctype>(jvmtiEnv*, \
1285 jthread, \
1286 jint, \
1287 jint, \
1288 std::add_pointer<srctype>::type); \
1289 template jvmtiError MethodUtil::SetLocalVariable<srctype>(jvmtiEnv*, \
1290 jthread, \
1291 jint, \
1292 jint, \
1293 srctype);
1294
1295 FOR_JVMTI_JVALUE_TYPES(GET_SET_LV);
1296
1297 #undef GET_SET_LV
1298
1299 #undef FOR_JVMTI_JVALUE_TYPES
1300
1301 } // namespace openjdkjvmti
1302