blob: defcd54082e9b2e6503075e23bbb327c8dbc9653 [file] [log] [blame]
Andreas Gampe3c252f02016-10-27 18:25:17 -07001/* 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
Alex Lighte2ddce32019-05-22 17:08:35 +000034#include <initializer_list>
Andreas Gampe49fc60e2017-08-24 13:19:59 -070035#include <type_traits>
Alex Lighte2ddce32019-05-22 17:08:35 +000036#include <variant>
Andreas Gampe49fc60e2017-08-24 13:19:59 -070037
Alex Lighte2ddce32019-05-22 17:08:35 +000038#include "android-base/macros.h"
Andreas Gampee5d23982019-01-08 10:34:26 -080039#include "arch/context.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070040#include "art_jvmti.h"
41#include "art_method-inl.h"
42#include "base/enums.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000043#include "base/globals.h"
44#include "base/macros.h"
Alex Lightbebd7bd2017-07-25 14:05:52 -070045#include "base/mutex-inl.h"
Alex Lighta4cdd362019-04-18 09:17:10 -070046#include "deopt_manager.h"
David Sehr9e734c72018-01-04 17:56:19 -080047#include "dex/code_item_accessors-inl.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000048#include "dex/code_item_accessors.h"
David Sehr9e734c72018-01-04 17:56:19 -080049#include "dex/dex_file_annotations.h"
50#include "dex/dex_file_types.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000051#include "dex/dex_instruction.h"
52#include "dex/dex_instruction_iterator.h"
David Sehr8c0961f2018-01-23 16:11:38 -080053#include "dex/modifiers.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000054#include "dex/primitive.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070055#include "events-inl.h"
Alex Light318afe62018-03-22 16:50:10 -070056#include "gc_root-inl.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000057#include "handle.h"
Alex Light0a5ec3d2017-07-25 16:50:26 -070058#include "jit/jit.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010059#include "jni/jni_internal.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000060#include "jvmti.h"
Alex Lightbebd7bd2017-07-25 14:05:52 -070061#include "mirror/class-inl.h"
62#include "mirror/class_loader.h"
63#include "mirror/object-inl.h"
Andreas Gampe27dfa052017-02-16 15:04:36 -080064#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070065#include "nativehelper/scoped_local_ref.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000066#include "oat_file.h"
Alex Lighta4cdd362019-04-18 09:17:10 -070067#include "obj_ptr.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070068#include "runtime_callbacks.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070069#include "scoped_thread_state_change-inl.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000070#include "scoped_thread_state_change.h"
Alex Lightbebd7bd2017-07-25 14:05:52 -070071#include "stack.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070072#include "thread-current-inl.h"
Alex Lighta4cdd362019-04-18 09:17:10 -070073#include "thread.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070074#include "thread_list.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000075#include "ti_logging.h"
Alex Lighte814f9d2017-07-31 16:14:39 -070076#include "ti_stack.h"
Alex Lightbebd7bd2017-07-25 14:05:52 -070077#include "ti_thread.h"
Alex Light0af8cde2017-04-20 13:35:05 -070078#include "ti_phase.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000079#include "verifier/register_line-inl.h"
80#include "verifier/reg_type-inl.h"
81#include "verifier/method_verifier-inl.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070082
83namespace openjdkjvmti {
84
Alex Lightd78ddec2017-04-18 15:20:38 -070085struct TiMethodCallback : public art::MethodCallback {
86 void RegisterNativeMethod(art::ArtMethod* method,
87 const void* cur_method,
88 /*out*/void** new_method)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010089 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightd78ddec2017-04-18 15:20:38 -070090 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kNativeMethodBind)) {
91 art::Thread* thread = art::Thread::Current();
Alex Light0af8cde2017-04-20 13:35:05 -070092 art::JNIEnvExt* jnienv = thread->GetJniEnv();
Alex Lightd78ddec2017-04-18 15:20:38 -070093 ScopedLocalRef<jthread> thread_jni(
Alex Light0af8cde2017-04-20 13:35:05 -070094 jnienv, PhaseUtil::IsLivePhase() ? jnienv->AddLocalReference<jthread>(thread->GetPeer())
95 : nullptr);
Alex Light79d6c802019-06-27 15:50:11 +000096 jmethodID method_id = art::jni::EncodeArtMethod(method);
Alex Lightd78ddec2017-04-18 15:20:38 -070097 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
98 event_handler->DispatchEvent<ArtJvmtiEvent::kNativeMethodBind>(
99 thread,
Alex Light0af8cde2017-04-20 13:35:05 -0700100 static_cast<JNIEnv*>(jnienv),
Alex Lightd78ddec2017-04-18 15:20:38 -0700101 thread_jni.get(),
Alex Light79d6c802019-06-27 15:50:11 +0000102 method_id,
Alex Lightd78ddec2017-04-18 15:20:38 -0700103 const_cast<void*>(cur_method),
104 new_method);
105 }
106 }
107
108 EventHandler* event_handler = nullptr;
109};
110
111TiMethodCallback gMethodCallback;
112
113void MethodUtil::Register(EventHandler* handler) {
114 gMethodCallback.event_handler = handler;
115 art::ScopedThreadStateChange stsc(art::Thread::Current(),
116 art::ThreadState::kWaitingForDebuggerToAttach);
117 art::ScopedSuspendAll ssa("Add method callback");
Alex Light21611932017-09-26 13:07:39 -0700118 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
119 callbacks->AddMethodCallback(&gMethodCallback);
Alex Lightd78ddec2017-04-18 15:20:38 -0700120}
121
122void MethodUtil::Unregister() {
123 art::ScopedThreadStateChange stsc(art::Thread::Current(),
124 art::ThreadState::kWaitingForDebuggerToAttach);
125 art::ScopedSuspendAll ssa("Remove method callback");
Alex Light21611932017-09-26 13:07:39 -0700126 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
127 callbacks->RemoveMethodCallback(&gMethodCallback);
Alex Lightd78ddec2017-04-18 15:20:38 -0700128}
129
Alex Light4c174282017-07-05 10:18:18 -0700130jvmtiError MethodUtil::GetBytecodes(jvmtiEnv* env,
131 jmethodID method,
132 jint* size_ptr,
133 unsigned char** bytecode_ptr) {
134 if (method == nullptr) {
135 return ERR(INVALID_METHODID);
136 }
137 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
138
139 if (art_method->IsNative()) {
140 return ERR(NATIVE_METHOD);
141 }
142
143 if (size_ptr == nullptr || bytecode_ptr == nullptr) {
144 return ERR(NULL_POINTER);
145 }
146
147 art::ScopedObjectAccess soa(art::Thread::Current());
David Sehr0225f8e2018-01-31 08:52:24 +0000148 art::CodeItemInstructionAccessor accessor(art_method->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800149 if (!accessor.HasCodeItem()) {
Alex Light4c174282017-07-05 10:18:18 -0700150 *size_ptr = 0;
151 *bytecode_ptr = nullptr;
152 return OK;
153 }
154 // 2 bytes per instruction for dex code.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800155 *size_ptr = accessor.InsnsSizeInCodeUnits() * 2;
Alex Light4c174282017-07-05 10:18:18 -0700156 jvmtiError err = env->Allocate(*size_ptr, bytecode_ptr);
157 if (err != OK) {
158 return err;
159 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800160 memcpy(*bytecode_ptr, accessor.Insns(), *size_ptr);
Alex Light4c174282017-07-05 10:18:18 -0700161 return OK;
162}
163
Andreas Gampef71832e2017-01-09 11:38:04 -0800164jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
165 jmethodID method,
166 jint* size_ptr) {
167 if (method == nullptr) {
168 return ERR(INVALID_METHODID);
169 }
170 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
171
172 if (art_method->IsNative()) {
173 return ERR(NATIVE_METHOD);
174 }
175
176 if (size_ptr == nullptr) {
177 return ERR(NULL_POINTER);
178 }
179
180 art::ScopedObjectAccess soa(art::Thread::Current());
181 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700182 // Use the shorty.
183 art::ArtMethod* base_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
184 size_t arg_count = art::ArtMethod::NumArgRegisters(base_method->GetShorty());
185 if (!base_method->IsStatic()) {
186 arg_count++;
187 }
188 *size_ptr = static_cast<jint>(arg_count);
Andreas Gampef71832e2017-01-09 11:38:04 -0800189 return ERR(NONE);
190 }
191
192 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
David Sehr0225f8e2018-01-31 08:52:24 +0000193 *size_ptr = art_method->DexInstructionData().InsSize();
Andreas Gampef71832e2017-01-09 11:38:04 -0800194
195 return ERR(NONE);
196}
197
Alex Lightce68cc62017-07-26 10:30:38 -0700198jvmtiError MethodUtil::GetLocalVariableTable(jvmtiEnv* env,
199 jmethodID method,
200 jint* entry_count_ptr,
201 jvmtiLocalVariableEntry** table_ptr) {
202 if (method == nullptr) {
203 return ERR(INVALID_METHODID);
204 }
205 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
206
207 if (art_method->IsNative()) {
208 return ERR(NATIVE_METHOD);
209 }
210
211 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
212 return ERR(NULL_POINTER);
213 }
214
215 art::ScopedObjectAccess soa(art::Thread::Current());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800216
217 const art::DexFile* const dex_file = art_method->GetDexFile();
218 if (dex_file == nullptr) {
219 return ERR(ABSENT_INFORMATION);
220 }
221
222 // TODO HasCodeItem == false means that the method is abstract (or native, but we check that
Alex Lightce68cc62017-07-26 10:30:38 -0700223 // earlier). We should check what is returned by the RI in this situation since it's not clear
224 // what the appropriate return value is from the spec.
David Sehr0225f8e2018-01-31 08:52:24 +0000225 art::CodeItemDebugInfoAccessor accessor(art_method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800226 if (!accessor.HasCodeItem()) {
Alex Lightce68cc62017-07-26 10:30:38 -0700227 return ERR(ABSENT_INFORMATION);
228 }
229
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700230 std::vector<jvmtiLocalVariableEntry> variables;
231 jvmtiError err = OK;
Alex Lightce68cc62017-07-26 10:30:38 -0700232
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700233 auto release = [&](jint* out_entry_count_ptr, jvmtiLocalVariableEntry** out_table_ptr) {
234 jlong table_size = sizeof(jvmtiLocalVariableEntry) * variables.size();
235 if (err != OK ||
236 (err = env->Allocate(table_size,
237 reinterpret_cast<unsigned char**>(out_table_ptr))) != OK) {
238 for (jvmtiLocalVariableEntry& e : variables) {
239 env->Deallocate(reinterpret_cast<unsigned char*>(e.name));
240 env->Deallocate(reinterpret_cast<unsigned char*>(e.signature));
241 env->Deallocate(reinterpret_cast<unsigned char*>(e.generic_signature));
242 }
243 return err;
Alex Lightce68cc62017-07-26 10:30:38 -0700244 }
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700245 *out_entry_count_ptr = variables.size();
246 memcpy(*out_table_ptr, variables.data(), table_size);
247 return OK;
Alex Lightce68cc62017-07-26 10:30:38 -0700248 };
249
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700250 auto visitor = [&](const art::DexFile::LocalInfo& entry) {
251 if (err != OK) {
252 return;
253 }
254 JvmtiUniquePtr<char[]> name_str = CopyString(env, entry.name_, &err);
255 if (err != OK) {
256 return;
257 }
258 JvmtiUniquePtr<char[]> sig_str = CopyString(env, entry.descriptor_, &err);
259 if (err != OK) {
260 return;
261 }
262 JvmtiUniquePtr<char[]> generic_sig_str = CopyString(env, entry.signature_, &err);
263 if (err != OK) {
264 return;
265 }
266 variables.push_back({
267 .start_location = static_cast<jlocation>(entry.start_address_),
268 .length = static_cast<jint>(entry.end_address_ - entry.start_address_),
269 .name = name_str.release(),
270 .signature = sig_str.release(),
271 .generic_signature = generic_sig_str.release(),
272 .slot = entry.reg_,
273 });
274 };
275
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800276 if (!accessor.DecodeDebugLocalInfo(art_method->IsStatic(),
277 art_method->GetDexMethodIndex(),
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700278 visitor)) {
Alex Lightce68cc62017-07-26 10:30:38 -0700279 // Something went wrong with decoding the debug information. It might as well not be there.
280 return ERR(ABSENT_INFORMATION);
Alex Lightce68cc62017-07-26 10:30:38 -0700281 }
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700282 return release(entry_count_ptr, table_ptr);
Alex Lightce68cc62017-07-26 10:30:38 -0700283}
284
Andreas Gampef71832e2017-01-09 11:38:04 -0800285jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
286 jmethodID method,
287 jint* max_ptr) {
288 if (method == nullptr) {
289 return ERR(INVALID_METHODID);
290 }
291 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
292
293 if (art_method->IsNative()) {
294 return ERR(NATIVE_METHOD);
295 }
296
297 if (max_ptr == nullptr) {
298 return ERR(NULL_POINTER);
299 }
300
301 art::ScopedObjectAccess soa(art::Thread::Current());
302 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
303 // This isn't specified as an error case, so return 0.
304 *max_ptr = 0;
305 return ERR(NONE);
306 }
307
308 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
David Sehr0225f8e2018-01-31 08:52:24 +0000309 *max_ptr = art_method->DexInstructionData().RegistersSize();
Andreas Gampef71832e2017-01-09 11:38:04 -0800310
311 return ERR(NONE);
312}
313
Andreas Gampe3c252f02016-10-27 18:25:17 -0700314jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
315 jmethodID method,
316 char** name_ptr,
317 char** signature_ptr,
318 char** generic_ptr) {
319 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe13b27842016-11-07 16:48:23 -0800320 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700321 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
322
Andreas Gampe54711412017-02-21 12:41:43 -0800323 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700324 if (name_ptr != nullptr) {
325 const char* method_name = art_method->GetName();
326 if (method_name == nullptr) {
327 method_name = "<error>";
328 }
Andreas Gampe54711412017-02-21 12:41:43 -0800329 jvmtiError ret;
330 name_copy = CopyString(env, method_name, &ret);
331 if (name_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700332 return ret;
333 }
Andreas Gampe54711412017-02-21 12:41:43 -0800334 *name_ptr = name_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700335 }
336
Andreas Gampe54711412017-02-21 12:41:43 -0800337 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700338 if (signature_ptr != nullptr) {
339 const art::Signature sig = art_method->GetSignature();
340 std::string str = sig.ToString();
Andreas Gampe54711412017-02-21 12:41:43 -0800341 jvmtiError ret;
342 signature_copy = CopyString(env, str.c_str(), &ret);
343 if (signature_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700344 return ret;
345 }
Andreas Gampe54711412017-02-21 12:41:43 -0800346 *signature_ptr = signature_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700347 }
348
Andreas Gampe862bdd82016-11-18 13:31:13 -0800349 if (generic_ptr != nullptr) {
350 *generic_ptr = nullptr;
Andreas Gampe27dfa052017-02-16 15:04:36 -0800351 if (!art_method->GetDeclaringClass()->IsProxyClass()) {
Vladimir Markoacb906d2018-05-30 10:23:49 +0100352 art::ObjPtr<art::mirror::ObjectArray<art::mirror::String>> str_array =
Andreas Gampe27dfa052017-02-16 15:04:36 -0800353 art::annotations::GetSignatureAnnotationForMethod(art_method);
354 if (str_array != nullptr) {
355 std::ostringstream oss;
356 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
357 oss << str_array->Get(i)->ToModifiedUtf8();
358 }
359 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800360 jvmtiError ret;
361 JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
362 if (generic_copy == nullptr) {
Andreas Gampe27dfa052017-02-16 15:04:36 -0800363 return ret;
364 }
Andreas Gampe54711412017-02-21 12:41:43 -0800365 *generic_ptr = generic_copy.release();
Andreas Gampe27dfa052017-02-16 15:04:36 -0800366 } else if (soa.Self()->IsExceptionPending()) {
367 // TODO: Should we report an error here?
368 soa.Self()->ClearException();
369 }
370 }
Andreas Gampe862bdd82016-11-18 13:31:13 -0800371 }
Andreas Gampe3c252f02016-10-27 18:25:17 -0700372
373 // Everything is fine, release the buffers.
374 name_copy.release();
375 signature_copy.release();
376
377 return ERR(NONE);
378}
379
Andreas Gampe368a2082016-10-28 17:33:13 -0700380jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
381 jmethodID method,
382 jclass* declaring_class_ptr) {
383 if (declaring_class_ptr == nullptr) {
384 return ERR(NULL_POINTER);
385 }
386
Andreas Gampe13b27842016-11-07 16:48:23 -0800387 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700388 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
389
Andreas Gampe13b27842016-11-07 16:48:23 -0800390 art::ScopedObjectAccess soa(art::Thread::Current());
Vladimir Markod93e3742018-07-18 10:58:13 +0100391 art::ObjPtr<art::mirror::Class> klass = art_method->GetDeclaringClass();
Andreas Gampe368a2082016-10-28 17:33:13 -0700392 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
393
394 return ERR(NONE);
395}
396
Andreas Gampef71832e2017-01-09 11:38:04 -0800397jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
398 jmethodID method,
399 jlocation* start_location_ptr,
400 jlocation* end_location_ptr) {
401 if (method == nullptr) {
402 return ERR(INVALID_METHODID);
403 }
404 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
405
406 if (art_method->IsNative()) {
407 return ERR(NATIVE_METHOD);
408 }
409
410 if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
411 return ERR(NULL_POINTER);
412 }
413
414 art::ScopedObjectAccess soa(art::Thread::Current());
415 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700416 // This isn't specified as an error case, so return -1/-1 as the RI does.
417 *start_location_ptr = -1;
418 *end_location_ptr = -1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800419 return ERR(NONE);
420 }
421
422 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
423 *start_location_ptr = 0;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800424 *end_location_ptr = art_method->DexInstructions().InsnsSizeInCodeUnits() - 1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800425
426 return ERR(NONE);
427}
428
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700429jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
430 jmethodID method,
431 jint* modifiers_ptr) {
432 if (modifiers_ptr == nullptr) {
433 return ERR(NULL_POINTER);
434 }
435
Andreas Gampe13b27842016-11-07 16:48:23 -0800436 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700437 uint32_t modifiers = art_method->GetAccessFlags();
438
439 // Note: Keep this code in sync with Executable.fixMethodFlags.
440 if ((modifiers & art::kAccAbstract) != 0) {
441 modifiers &= ~art::kAccNative;
442 }
443 modifiers &= ~art::kAccSynchronized;
444 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
445 modifiers |= art::kAccSynchronized;
446 }
447 modifiers &= art::kAccJavaFlagsMask;
448
449 *modifiers_ptr = modifiers;
450 return ERR(NONE);
451}
452
Andreas Gampeda3e5612016-12-13 19:00:53 -0800453jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
454 jmethodID method,
455 jint* entry_count_ptr,
456 jvmtiLineNumberEntry** table_ptr) {
457 if (method == nullptr) {
458 return ERR(NULL_POINTER);
459 }
460 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
461 DCHECK(!art_method->IsRuntimeMethod());
462
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800463 art::CodeItemDebugInfoAccessor accessor;
Andreas Gampeda3e5612016-12-13 19:00:53 -0800464 const art::DexFile* dex_file;
465 {
466 art::ScopedObjectAccess soa(art::Thread::Current());
467
468 if (art_method->IsProxyMethod()) {
469 return ERR(ABSENT_INFORMATION);
470 }
471 if (art_method->IsNative()) {
472 return ERR(NATIVE_METHOD);
473 }
474 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
475 return ERR(NULL_POINTER);
476 }
477
David Sehr0225f8e2018-01-31 08:52:24 +0000478 accessor = art::CodeItemDebugInfoAccessor(art_method->DexInstructionDebugInfo());
Andreas Gampeda3e5612016-12-13 19:00:53 -0800479 dex_file = art_method->GetDexFile();
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800480 DCHECK(accessor.HasCodeItem()) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
Andreas Gampeda3e5612016-12-13 19:00:53 -0800481 }
482
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700483 std::vector<jvmtiLineNumberEntry> context;
484 bool success = accessor.DecodeDebugPositionInfo([&](const art::DexFile::PositionInfo& entry) {
485 context.push_back({static_cast<jlocation>(entry.address_), static_cast<jint>(entry.line_)});
486 return false;
487 });
Andreas Gampeda3e5612016-12-13 19:00:53 -0800488 if (!success) {
489 return ERR(ABSENT_INFORMATION);
490 }
491
492 unsigned char* data;
493 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
494 jvmtiError alloc_error = env->Allocate(mem_size, &data);
495 if (alloc_error != ERR(NONE)) {
496 return alloc_error;
497 }
498 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
499 memcpy(*table_ptr, context.data(), mem_size);
500 *entry_count_ptr = static_cast<jint>(context.size());
501
502 return ERR(NONE);
503}
504
Andreas Gampefdeef522017-01-09 14:40:25 -0800505template <typename T>
506static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED,
507 jmethodID method,
508 T test,
509 jboolean* is_t_ptr) {
510 if (method == nullptr) {
511 return ERR(INVALID_METHODID);
512 }
513 if (is_t_ptr == nullptr) {
514 return ERR(NULL_POINTER);
515 }
516
517 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
518 *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
519
520 return ERR(NONE);
521}
522
523jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
524 auto test = [](art::ArtMethod* method) {
525 return method->IsNative();
526 };
527 return IsMethodT(env, m, test, is_native_ptr);
528}
529
530jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
531 auto test = [](art::ArtMethod* method) {
532 return method->IsObsolete();
533 };
534 return IsMethodT(env, m, test, is_obsolete_ptr);
535}
536
537jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
538 auto test = [](art::ArtMethod* method) {
539 return method->IsSynthetic();
540 };
541 return IsMethodT(env, m, test, is_synthetic_ptr);
542}
543
Alex Lightbebd7bd2017-07-25 14:05:52 -0700544class CommonLocalVariableClosure : public art::Closure {
545 public:
Alex Lighte2ddce32019-05-22 17:08:35 +0000546 // The verifier isn't always able to be as specific as the local-variable-table. We can only get
547 // 32-bit, 64-bit or reference.
548 enum class VerifierPrimitiveType {
549 k32BitValue, // float, int, short, char, boolean, byte
550 k64BitValue, // double, long
551 kReferenceValue, // Object
552 kZeroValue, // null or zero constant. Might be either k32BitValue or kReferenceValue
553 };
Alex Lightbebd7bd2017-07-25 14:05:52 -0700554
Alex Lighte2ddce32019-05-22 17:08:35 +0000555 using SlotType = std::variant<art::Primitive::Type, VerifierPrimitiveType>;
556
557 CommonLocalVariableClosure(jvmtiEnv* jvmti, jint depth, jint slot)
558 : jvmti_(jvmti), result_(ERR(INTERNAL)), depth_(depth), slot_(slot) {}
559
560 void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700561 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
Alex Lighta4cdd362019-04-18 09:17:10 -0700562 bool needs_instrument;
563 {
564 art::ScopedAssertNoThreadSuspension sants("CommonLocalVariableClosure::Run");
565 std::unique_ptr<art::Context> context(art::Context::Create());
566 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
567 visitor.WalkStack();
568 if (!visitor.FoundFrame()) {
569 // Must have been a bad depth.
570 result_ = ERR(NO_MORE_FRAMES);
571 return;
572 }
573 art::ArtMethod* method = visitor.GetMethod();
574 // Native and 'art' proxy methods don't have registers.
575 if (method->IsNative() || method->IsProxyMethod()) {
576 // TODO It might be useful to fake up support for get at least on proxy frames.
577 result_ = ERR(OPAQUE_FRAME);
578 return;
Alex Light6045bc22019-05-22 08:33:30 -0700579 } else if (slot_ >= method->DexInstructionData().RegistersSize() || slot_ < 0) {
Alex Lighta4cdd362019-04-18 09:17:10 -0700580 result_ = ERR(INVALID_SLOT);
581 return;
582 }
583 needs_instrument = !visitor.IsShadowFrame();
584 uint32_t pc = visitor.GetDexPc(/*abort_on_failure=*/false);
585 if (pc == art::dex::kDexNoIndex) {
586 // Cannot figure out current PC.
587 result_ = ERR(OPAQUE_FRAME);
588 return;
589 }
590 std::string descriptor;
Alex Lighte2ddce32019-05-22 17:08:35 +0000591 SlotType slot_type{ art::Primitive::kPrimVoid };
Alex Lighta4cdd362019-04-18 09:17:10 -0700592 jvmtiError err = GetSlotType(method, pc, &descriptor, &slot_type);
593 if (err != OK) {
594 result_ = err;
595 return;
596 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700597
Alex Lighta4cdd362019-04-18 09:17:10 -0700598 err = GetTypeError(method, slot_type, descriptor);
599 if (err != OK) {
600 result_ = err;
601 return;
602 }
603 result_ = Execute(method, visitor);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700604 }
Alex Light0a5ec3d2017-07-25 16:50:26 -0700605 if (needs_instrument) {
Alex Lighta4cdd362019-04-18 09:17:10 -0700606 DeoptManager::Get()->DeoptimizeThread(self);
Alex Light0a5ec3d2017-07-25 16:50:26 -0700607 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700608 }
609
Alex Light318afe62018-03-22 16:50:10 -0700610 virtual jvmtiError GetResult() {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700611 return result_;
612 }
613
614 protected:
615 virtual jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Alex Light318afe62018-03-22 16:50:10 -0700616 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700617 virtual jvmtiError GetTypeError(art::ArtMethod* method,
Alex Lighte2ddce32019-05-22 17:08:35 +0000618 SlotType type,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700619 const std::string& descriptor)
Alex Light318afe62018-03-22 16:50:10 -0700620 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700621
622 jvmtiError GetSlotType(art::ArtMethod* method,
623 uint32_t dex_pc,
624 /*out*/std::string* descriptor,
Alex Lighte2ddce32019-05-22 17:08:35 +0000625 /*out*/SlotType* type)
626 REQUIRES_SHARED(art::Locks::mutator_lock_);
627
628 jvmtiError InferSlotTypeFromVerifier(art::ArtMethod* method,
629 uint32_t dex_pc,
630 /*out*/ std::string* descriptor,
631 /*out*/ SlotType* type)
632 REQUIRES_SHARED(art::Locks::mutator_lock_) {
633 art::Thread* self = art::Thread::Current();
634 art::StackHandleScope<2> hs(self);
635 std::unique_ptr<art::verifier::MethodVerifier> verifier(
636 art::verifier::MethodVerifier::CalculateVerificationInfo(
637 self,
638 method,
639 hs.NewHandle(method->GetDexCache()),
640 hs.NewHandle(method->GetDeclaringClass()->GetClassLoader())));
641 if (verifier == nullptr) {
642 JVMTI_LOG(WARNING, jvmti_) << "Unable to extract verification information from "
643 << method->PrettyMethod() << " due to hard verification failures! "
644 << "How did this method even get loaded!";
645 return ERR(INTERNAL);
646 }
647 art::verifier::RegisterLine* line = verifier->GetRegLine(dex_pc);
648 if (line == nullptr) {
649 JVMTI_LOG(WARNING, jvmti_) << "Unable to determine register line at dex-pc " << dex_pc
650 << " for method " << method->PrettyMethod();
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800651 return ERR(OPAQUE_FRAME);
652 }
Alex Lighte2ddce32019-05-22 17:08:35 +0000653 const art::verifier::RegType& rt = line->GetRegisterType(verifier.get(), slot_);
654 if (rt.IsUndefined()) {
655 return ERR(INVALID_SLOT);
656 } else if (rt.IsNonZeroReferenceTypes() || rt.IsNull()) {
657 *descriptor = (rt.HasClass() ? rt.GetDescriptor() : "Ljava/lang/Object;");
658 *type = VerifierPrimitiveType::kReferenceValue;
659 return OK;
660 } else if (rt.IsZero()) {
661 *descriptor = "I";
662 *type = VerifierPrimitiveType::kZeroValue;
663 return OK;
664 } else if (rt.IsCategory1Types()) {
665 *descriptor = "I";
666 *type = VerifierPrimitiveType::k32BitValue;
667 return OK;
668 } else if (rt.IsCategory2Types() && rt.IsLowHalf()) {
669 *descriptor = "J";
670 *type = VerifierPrimitiveType::k64BitValue;
671 return OK;
672 } else {
673 // The slot doesn't have a type. Must not be valid here.
David Srbecky99cbfb52019-05-22 11:39:45 +0000674 return ERR(INVALID_SLOT);
Alex Lighte48fd0b2019-05-20 10:04:44 -0700675 }
676 }
677
Alex Lighte2ddce32019-05-22 17:08:35 +0000678 constexpr VerifierPrimitiveType SquashType(SlotType t) {
679 if (std::holds_alternative<art::Primitive::Type>(t)) {
680 switch (std::get<art::Primitive::Type>(t)) {
681 // 32-bit primitives
682 case art::Primitive::kPrimByte:
683 case art::Primitive::kPrimChar:
684 case art::Primitive::kPrimInt:
685 case art::Primitive::kPrimShort:
686 case art::Primitive::kPrimBoolean:
687 case art::Primitive::kPrimFloat:
688 return VerifierPrimitiveType::k32BitValue;
689 // 64-bit primitives
690 case art::Primitive::kPrimLong:
691 case art::Primitive::kPrimDouble:
692 return VerifierPrimitiveType::k64BitValue;
693 case art::Primitive::kPrimNot:
694 return VerifierPrimitiveType::kReferenceValue;
695 case art::Primitive::kPrimVoid:
696 LOG(FATAL) << "Got kPrimVoid";
697 UNREACHABLE();
698 }
699 } else {
700 return std::get<VerifierPrimitiveType>(t);
701 }
702 }
703
704 jvmtiEnv* jvmti_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700705 jvmtiError result_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700706 jint depth_;
707 jint slot_;
Alex Lighte2ddce32019-05-22 17:08:35 +0000708
709 private:
710 DISALLOW_COPY_AND_ASSIGN(CommonLocalVariableClosure);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700711};
712
Alex Lighte2ddce32019-05-22 17:08:35 +0000713std::ostream& operator<<(std::ostream& os,
714 CommonLocalVariableClosure::VerifierPrimitiveType state) {
715 switch (state) {
716 case CommonLocalVariableClosure::VerifierPrimitiveType::k32BitValue:
717 return os << "32BitValue";
718 case CommonLocalVariableClosure::VerifierPrimitiveType::k64BitValue:
719 return os << "64BitValue";
720 case CommonLocalVariableClosure::VerifierPrimitiveType::kReferenceValue:
721 return os << "ReferenceValue";
722 case CommonLocalVariableClosure::VerifierPrimitiveType::kZeroValue:
723 return os << "ZeroValue";
724 }
725}
726
727std::ostream& operator<<(std::ostream& os, CommonLocalVariableClosure::SlotType state) {
728 if (std::holds_alternative<art::Primitive::Type>(state)) {
729 return os << "Primitive::Type[" << std::get<art::Primitive::Type>(state) << "]";
730 } else {
731 return os << "VerifierPrimitiveType["
732 << std::get<CommonLocalVariableClosure::VerifierPrimitiveType>(state) << "]";
733 }
734}
735
736jvmtiError CommonLocalVariableClosure::GetSlotType(art::ArtMethod* method,
737 uint32_t dex_pc,
738 /*out*/ std::string* descriptor,
739 /*out*/ SlotType* type) {
740 const art::DexFile* dex_file = method->GetDexFile();
741 if (dex_file == nullptr) {
742 return ERR(OPAQUE_FRAME);
743 }
744 art::CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
745 if (!accessor.HasCodeItem()) {
746 return ERR(OPAQUE_FRAME);
747 }
748 bool found = false;
749 *type = art::Primitive::kPrimVoid;
750 descriptor->clear();
751 auto visitor = [&](const art::DexFile::LocalInfo& entry) {
752 if (!found && entry.start_address_ <= dex_pc && entry.end_address_ > dex_pc &&
753 entry.reg_ == slot_) {
754 found = true;
755 *type = art::Primitive::GetType(entry.descriptor_[0]);
756 *descriptor = entry.descriptor_;
757 }
758 };
759 if (!accessor.DecodeDebugLocalInfo(method->IsStatic(), method->GetDexMethodIndex(), visitor) ||
760 !found) {
761 // Something went wrong with decoding the debug information. It might as well not be there.
762 // Try to find the type with the verifier.
763 // TODO This is very slow.
764 return InferSlotTypeFromVerifier(method, dex_pc, descriptor, type);
765 } else if (art::kIsDebugBuild) {
766 std::string type_unused;
767 SlotType verifier_type{ art::Primitive::kPrimVoid };
768 DCHECK_EQ(InferSlotTypeFromVerifier(method, dex_pc, &type_unused, &verifier_type), OK)
769 << method->PrettyMethod() << " failed to verify!";
770 if (*type == SlotType{ art::Primitive::kPrimNot }) {
771 // We cannot distinguish between a constant 0 and a null reference so we return that it is a
772 // 32bit value (Due to the way references are read by the interpreter this is safe even if
773 // it's modified, the value will remain null). This is not ideal since it prevents modifying
774 // locals in some circumstances but generally is not a big deal (since one can just modify it
775 // later once it's been determined to be a reference by a later instruction).
776 DCHECK(verifier_type == SlotType { VerifierPrimitiveType::kZeroValue } ||
777 verifier_type == SlotType { VerifierPrimitiveType::kReferenceValue })
778 << "Verifier disagrees on type of slot! debug: " << *type
779 << " verifier: " << verifier_type;
780 } else if (verifier_type == SlotType { VerifierPrimitiveType::kZeroValue }) {
781 DCHECK(VerifierPrimitiveType::k32BitValue == SquashType(*type) ||
782 VerifierPrimitiveType::kReferenceValue == SquashType(*type))
783 << "Verifier disagrees on type of slot! debug: " << *type
784 << " verifier: " << verifier_type;
785 } else {
786 DCHECK_EQ(SquashType(verifier_type), SquashType(*type))
787 << "Verifier disagrees on type of slot! debug: " << *type
788 << " verifier: " << verifier_type;
789 }
790 }
791 return OK;
792}
793
Alex Lightbebd7bd2017-07-25 14:05:52 -0700794class GetLocalVariableClosure : public CommonLocalVariableClosure {
795 public:
Alex Lighte2ddce32019-05-22 17:08:35 +0000796 GetLocalVariableClosure(jvmtiEnv* jvmti,
797 jint depth,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700798 jint slot,
799 art::Primitive::Type type,
800 jvalue* val)
Alex Lighte2ddce32019-05-22 17:08:35 +0000801 : CommonLocalVariableClosure(jvmti, depth, slot),
Alex Light318afe62018-03-22 16:50:10 -0700802 type_(type),
803 val_(val),
804 obj_val_(nullptr) {}
805
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700806 jvmtiError GetResult() override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light318afe62018-03-22 16:50:10 -0700807 if (result_ == OK && type_ == art::Primitive::kPrimNot) {
Alex Lighta4cdd362019-04-18 09:17:10 -0700808 if (obj_val_ == nullptr) {
809 val_->l = nullptr;
810 } else {
811 art::JNIEnvExt* jni = art::Thread::Current()->GetJniEnv();
812 val_->l = static_cast<JNIEnv*>(jni)->NewLocalRef(obj_val_);
813 jni->DeleteGlobalRef(obj_val_);
814 obj_val_ = nullptr;
815 }
Alex Light318afe62018-03-22 16:50:10 -0700816 }
817 return CommonLocalVariableClosure::GetResult();
818 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700819
820 protected:
Alex Lighte2ddce32019-05-22 17:08:35 +0000821 jvmtiError
822 GetTypeError(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor) override
823 REQUIRES_SHARED(art::Locks::mutator_lock_) {
824 jvmtiError res = GetTypeErrorInner(method, slot_type, descriptor);
825 if (res == ERR(TYPE_MISMATCH)) {
826 JVMTI_LOG(INFO, jvmti_) << "Unable to Get local variable in slot " << slot_ << ". Expected"
827 << " slot to be of type compatible with " << SlotType { type_ }
828 << " but slot is " << slot_type;
829 } else if (res != OK) {
830 JVMTI_LOG(INFO, jvmti_) << "Unable to get local variable in slot " << slot_ << ".";
831 }
832 return res;
833 }
834
835 jvmtiError GetTypeErrorInner(art::ArtMethod* method ATTRIBUTE_UNUSED,
836 SlotType slot_type,
837 const std::string& descriptor ATTRIBUTE_UNUSED)
838 REQUIRES_SHARED(art::Locks::mutator_lock_) {
839 switch (type_) {
David Srbecky99cbfb52019-05-22 11:39:45 +0000840 case art::Primitive::kPrimFloat:
Alex Lighte2ddce32019-05-22 17:08:35 +0000841 case art::Primitive::kPrimInt: {
842 if (std::holds_alternative<VerifierPrimitiveType>(slot_type)) {
843 return (slot_type == SlotType { VerifierPrimitiveType::k32BitValue } ||
844 slot_type == SlotType { VerifierPrimitiveType::kZeroValue })
845 ? OK
846 : ERR(TYPE_MISMATCH);
847 } else if (type_ == art::Primitive::kPrimFloat ||
848 slot_type == SlotType { art::Primitive::kPrimFloat }) {
849 // Check that we are actually a float.
850 return (SlotType { type_ } == slot_type) ? OK : ERR(TYPE_MISMATCH);
851 } else {
852 // Some smaller int type.
853 return SquashType(slot_type) == SquashType(SlotType { type_ }) ? OK : ERR(TYPE_MISMATCH);
854 }
855 }
856 case art::Primitive::kPrimLong:
857 case art::Primitive::kPrimDouble: {
858 // todo
859 if (std::holds_alternative<VerifierPrimitiveType>(slot_type)) {
860 return (slot_type == SlotType { VerifierPrimitiveType::k64BitValue })
861 ? OK
862 : ERR(TYPE_MISMATCH);
863 } else {
864 return slot_type == SlotType { type_ } ? OK : ERR(TYPE_MISMATCH);
865 }
866 }
David Srbecky99cbfb52019-05-22 11:39:45 +0000867 case art::Primitive::kPrimNot:
Alex Lighte2ddce32019-05-22 17:08:35 +0000868 return (SquashType(slot_type) == VerifierPrimitiveType::kReferenceValue ||
869 SquashType(slot_type) == VerifierPrimitiveType::kZeroValue)
870 ? OK
871 : ERR(TYPE_MISMATCH);
872 case art::Primitive::kPrimShort:
873 case art::Primitive::kPrimChar:
874 case art::Primitive::kPrimByte:
875 case art::Primitive::kPrimBoolean:
Alex Lightbebd7bd2017-07-25 14:05:52 -0700876 case art::Primitive::kPrimVoid:
877 LOG(FATAL) << "Unexpected primitive type " << slot_type;
878 UNREACHABLE();
879 }
880 }
881
882 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100883 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700884 switch (type_) {
885 case art::Primitive::kPrimNot: {
886 uint32_t ptr_val;
887 if (!visitor.GetVReg(method,
888 static_cast<uint16_t>(slot_),
889 art::kReferenceVReg,
890 &ptr_val)) {
891 return ERR(OPAQUE_FRAME);
892 }
Alex Lighta4cdd362019-04-18 09:17:10 -0700893 art::JNIEnvExt* jni = art::Thread::Current()->GetJniEnv();
894 art::ObjPtr<art::mirror::Object> obj(reinterpret_cast<art::mirror::Object*>(ptr_val));
Alex Light02353362019-05-03 11:27:07 -0700895 ScopedLocalRef<jobject> local(
896 jni, obj.IsNull() ? nullptr : jni->AddLocalReference<jobject>(obj));
Alex Lighta4cdd362019-04-18 09:17:10 -0700897 obj_val_ = jni->NewGlobalRef(local.get());
Alex Lightbebd7bd2017-07-25 14:05:52 -0700898 break;
899 }
900 case art::Primitive::kPrimInt:
901 case art::Primitive::kPrimFloat: {
902 if (!visitor.GetVReg(method,
903 static_cast<uint16_t>(slot_),
904 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg : art::kIntVReg,
905 reinterpret_cast<uint32_t*>(&val_->i))) {
906 return ERR(OPAQUE_FRAME);
907 }
908 break;
909 }
910 case art::Primitive::kPrimDouble:
911 case art::Primitive::kPrimLong: {
912 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
913 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
914 if (!visitor.GetVRegPair(method,
915 static_cast<uint16_t>(slot_),
916 lo_type,
917 high_type,
918 reinterpret_cast<uint64_t*>(&val_->j))) {
919 return ERR(OPAQUE_FRAME);
920 }
921 break;
922 }
923 default: {
924 LOG(FATAL) << "unexpected register type " << type_;
925 UNREACHABLE();
926 }
927 }
928 return OK;
929 }
930
931 private:
932 art::Primitive::Type type_;
933 jvalue* val_;
Alex Lighta4cdd362019-04-18 09:17:10 -0700934 // A global reference to the return value. We use the global reference to safely transfer the
935 // value between threads.
936 jobject obj_val_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700937};
938
Alex Lighte2ddce32019-05-22 17:08:35 +0000939jvmtiError MethodUtil::GetLocalVariableGeneric(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700940 jthread thread,
941 jint depth,
942 jint slot,
943 art::Primitive::Type type,
944 jvalue* val) {
945 if (depth < 0) {
946 return ERR(ILLEGAL_ARGUMENT);
947 }
948 art::Thread* self = art::Thread::Current();
949 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -0700950 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700951 art::Thread* target = nullptr;
952 jvmtiError err = ERR(INTERNAL);
953 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -0700954 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700955 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700956 }
Alex Lighte2ddce32019-05-22 17:08:35 +0000957 GetLocalVariableClosure c(env, depth, slot, type, val);
Alex Lighta4cdd362019-04-18 09:17:10 -0700958 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
959 if (!target->RequestSynchronousCheckpoint(&c)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700960 return ERR(THREAD_NOT_ALIVE);
961 } else {
962 return c.GetResult();
963 }
964}
965
966class SetLocalVariableClosure : public CommonLocalVariableClosure {
967 public:
Alex Lighte2ddce32019-05-22 17:08:35 +0000968 SetLocalVariableClosure(jvmtiEnv* jvmti,
969 art::Thread* caller,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700970 jint depth,
971 jint slot,
972 art::Primitive::Type type,
973 jvalue val)
Alex Lighte2ddce32019-05-22 17:08:35 +0000974 : CommonLocalVariableClosure(jvmti, depth, slot), caller_(caller), type_(type), val_(val) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -0700975
976 protected:
Alex Lighte2ddce32019-05-22 17:08:35 +0000977 jvmtiError
978 GetTypeError(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor) override
979 REQUIRES_SHARED(art::Locks::mutator_lock_) {
980 jvmtiError res = GetTypeErrorInner(method, slot_type, descriptor);
981 if (res != OK) {
982 if (res == ERR(TYPE_MISMATCH)) {
983 std::ostringstream desc_exp;
984 std::ostringstream desc_set;
985 if (type_ == art::Primitive::kPrimNot) {
986 desc_exp << " (type: " << descriptor << ")";
987 art::ObjPtr<art::mirror::Object> new_val(art::Thread::Current()->DecodeJObject(val_.l));
988 desc_set << " (type: "
989 << (new_val.IsNull() ? "NULL" : new_val->GetClass()->PrettyDescriptor()) << ")";
990 }
991 JVMTI_LOG(INFO, jvmti_) << "Unable to Set local variable in slot " << slot_ << ". Expected"
992 << " slot to be of type compatible with " << SlotType{ type_ }
993 << desc_set.str() << " but slot is " << slot_type << desc_exp.str();
994 } else {
995 JVMTI_LOG(INFO, jvmti_) << "Unable to set local variable in slot " << slot_ << ". "
996 << err_.str();
997 }
998 }
999 return res;
1000 }
1001
1002 jvmtiError
1003 GetTypeErrorInner(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor)
1004 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1005 switch (SquashType(SlotType{ type_ })) {
1006 case VerifierPrimitiveType::k32BitValue: {
1007 if (slot_type == SlotType{ VerifierPrimitiveType::kZeroValue }) {
1008 if (val_.i == 0) {
1009 return OK;
1010 } else {
1011 err_ << "Cannot determine if slot " << slot_ << " is a null reference or 32bit "
1012 << "constant. Cannot allow writing to slot.";
1013 return ERR(INTERNAL);
1014 }
1015 } else if (SquashType(slot_type) != VerifierPrimitiveType::k32BitValue) {
1016 return ERR(TYPE_MISMATCH);
1017 } else if (slot_type == SlotType { VerifierPrimitiveType::k32BitValue } ||
1018 slot_type == SlotType { type_ }) {
1019 return OK;
1020 } else if (type_ == art::Primitive::kPrimFloat ||
1021 slot_type == SlotType { art::Primitive::kPrimFloat }) {
1022 // we should have hit the get == type_ above
1023 return ERR(TYPE_MISMATCH);
1024 } else {
1025 // Some smaller type then int.
1026 return OK;
1027 }
1028 }
1029 case VerifierPrimitiveType::k64BitValue: {
1030 if (slot_type == SlotType { VerifierPrimitiveType::k64BitValue } ||
1031 slot_type == SlotType { type_ }) {
1032 return OK;
1033 } else {
1034 return ERR(TYPE_MISMATCH);
1035 }
1036 }
1037 case VerifierPrimitiveType::kReferenceValue: {
1038 if (SquashType(slot_type) != VerifierPrimitiveType::kReferenceValue &&
1039 SquashType(slot_type) != VerifierPrimitiveType::kZeroValue) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001040 return ERR(TYPE_MISMATCH);
1041 } else if (val_.l == nullptr) {
1042 return OK;
Alex Lighte2ddce32019-05-22 17:08:35 +00001043 } else if (slot_type == SlotType { VerifierPrimitiveType::kZeroValue }) {
1044 err_ << "Cannot determine if slot " << slot_ << " is a null "
1045 << "reference or 32bit constant. Cannot allow writing to slot.";
1046 return ERR(INTERNAL);
Alex Lightbebd7bd2017-07-25 14:05:52 -07001047 } else {
1048 art::ClassLinker* cl = art::Runtime::Current()->GetClassLinker();
Alex Lighte2ddce32019-05-22 17:08:35 +00001049 art::ObjPtr<art::mirror::Class> set_class = caller_->DecodeJObject(val_.l)->GetClass();
Alex Lightbebd7bd2017-07-25 14:05:52 -07001050 art::ObjPtr<art::mirror::ClassLoader> loader =
1051 method->GetDeclaringClass()->GetClassLoader();
1052 art::ObjPtr<art::mirror::Class> slot_class =
1053 cl->LookupClass(caller_, descriptor.c_str(), loader);
Alex Lighte2ddce32019-05-22 17:08:35 +00001054 DCHECK(!slot_class.IsNull()) << descriptor << " slot: " << slot_type;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001055 return slot_class->IsAssignableFrom(set_class) ? OK : ERR(TYPE_MISMATCH);
1056 }
1057 }
Alex Lighte2ddce32019-05-22 17:08:35 +00001058 case VerifierPrimitiveType::kZeroValue: {
1059 LOG(FATAL) << "Illegal result from SquashType of art::Primitive::Type " << type_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001060 UNREACHABLE();
Alex Lighte2ddce32019-05-22 17:08:35 +00001061 }
Alex Lightbebd7bd2017-07-25 14:05:52 -07001062 }
1063 }
1064
1065 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001066 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001067 switch (type_) {
1068 case art::Primitive::kPrimNot: {
Vladimir Marko439d1262019-04-12 14:45:07 +01001069 if (!visitor.SetVRegReference(method,
1070 static_cast<uint16_t>(slot_),
1071 caller_->DecodeJObject(val_.l))) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001072 return ERR(OPAQUE_FRAME);
1073 }
1074 break;
1075 }
1076 case art::Primitive::kPrimInt:
1077 case art::Primitive::kPrimFloat: {
1078 if (!visitor.SetVReg(method,
1079 static_cast<uint16_t>(slot_),
1080 static_cast<uint32_t>(val_.i),
1081 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg
1082 : art::kIntVReg)) {
1083 return ERR(OPAQUE_FRAME);
1084 }
1085 break;
1086 }
1087 case art::Primitive::kPrimDouble:
1088 case art::Primitive::kPrimLong: {
1089 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
1090 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
1091 if (!visitor.SetVRegPair(method,
1092 static_cast<uint16_t>(slot_),
1093 static_cast<uint64_t>(val_.j),
1094 lo_type,
1095 high_type)) {
1096 return ERR(OPAQUE_FRAME);
1097 }
1098 break;
1099 }
1100 default: {
1101 LOG(FATAL) << "unexpected register type " << type_;
1102 UNREACHABLE();
1103 }
1104 }
1105 return OK;
1106 }
1107
1108 private:
Alex Light318afe62018-03-22 16:50:10 -07001109 art::Thread* caller_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001110 art::Primitive::Type type_;
1111 jvalue val_;
Alex Lighte2ddce32019-05-22 17:08:35 +00001112 std::ostringstream err_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001113};
1114
Alex Lighte2ddce32019-05-22 17:08:35 +00001115jvmtiError MethodUtil::SetLocalVariableGeneric(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -07001116 jthread thread,
1117 jint depth,
1118 jint slot,
1119 art::Primitive::Type type,
1120 jvalue val) {
1121 if (depth < 0) {
1122 return ERR(ILLEGAL_ARGUMENT);
1123 }
Alex Lightf2858632018-04-02 11:28:50 -07001124 // Make sure that we know not to do any OSR anymore.
1125 // TODO We should really keep track of this at the Frame granularity.
1126 DeoptManager::Get()->SetLocalsUpdated();
Alex Lightbebd7bd2017-07-25 14:05:52 -07001127 art::Thread* self = art::Thread::Current();
1128 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -07001129 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -07001130 art::Thread* target = nullptr;
1131 jvmtiError err = ERR(INTERNAL);
1132 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -07001133 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -07001134 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001135 }
Alex Lighte2ddce32019-05-22 17:08:35 +00001136 SetLocalVariableClosure c(env, self, depth, slot, type, val);
Alex Lightb1e31a82017-10-04 16:57:36 -07001137 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
Alex Light318afe62018-03-22 16:50:10 -07001138 if (!target->RequestSynchronousCheckpoint(&c)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001139 return ERR(THREAD_NOT_ALIVE);
1140 } else {
1141 return c.GetResult();
1142 }
1143}
1144
1145class GetLocalInstanceClosure : public art::Closure {
1146 public:
Alex Light318afe62018-03-22 16:50:10 -07001147 explicit GetLocalInstanceClosure(jint depth)
Alex Lightbebd7bd2017-07-25 14:05:52 -07001148 : result_(ERR(INTERNAL)),
Alex Lightbebd7bd2017-07-25 14:05:52 -07001149 depth_(depth),
Alex Light318afe62018-03-22 16:50:10 -07001150 val_(nullptr) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -07001151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001152 void Run(art::Thread* self) override REQUIRES(art::Locks::mutator_lock_) {
Alex Light318afe62018-03-22 16:50:10 -07001153 art::ScopedAssertNoThreadSuspension sants("GetLocalInstanceClosure::Run");
Alex Lightbebd7bd2017-07-25 14:05:52 -07001154 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
1155 std::unique_ptr<art::Context> context(art::Context::Create());
1156 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
1157 visitor.WalkStack();
1158 if (!visitor.FoundFrame()) {
1159 // Must have been a bad depth.
1160 result_ = ERR(NO_MORE_FRAMES);
1161 return;
1162 }
Alex Lightbebd7bd2017-07-25 14:05:52 -07001163 result_ = OK;
Alex Light318afe62018-03-22 16:50:10 -07001164 val_ = art::GcRoot<art::mirror::Object>(visitor.GetThisObject());
Alex Lightbebd7bd2017-07-25 14:05:52 -07001165 }
1166
Alex Light318afe62018-03-22 16:50:10 -07001167 jvmtiError GetResult(jobject* data_out) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1168 if (result_ == OK) {
1169 *data_out = val_.IsNull()
1170 ? nullptr
1171 : art::Thread::Current()->GetJniEnv()->AddLocalReference<jobject>(val_.Read());
1172 }
Alex Lightbebd7bd2017-07-25 14:05:52 -07001173 return result_;
1174 }
1175
1176 private:
1177 jvmtiError result_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001178 jint depth_;
Alex Light318afe62018-03-22 16:50:10 -07001179 art::GcRoot<art::mirror::Object> val_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001180};
1181
1182jvmtiError MethodUtil::GetLocalInstance(jvmtiEnv* env ATTRIBUTE_UNUSED,
1183 jthread thread,
1184 jint depth,
1185 jobject* data) {
1186 if (depth < 0) {
1187 return ERR(ILLEGAL_ARGUMENT);
1188 }
1189 art::Thread* self = art::Thread::Current();
1190 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -07001191 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -07001192 art::Thread* target = nullptr;
1193 jvmtiError err = ERR(INTERNAL);
1194 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -07001195 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -07001196 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001197 }
Alex Light318afe62018-03-22 16:50:10 -07001198 art::ScopedAssertNoThreadSuspension sants("Performing GetLocalInstance");
1199 GetLocalInstanceClosure c(depth);
1200 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. We
1201 // need to avoid suspending as we wait for the checkpoint to occur since we are (potentially)
1202 // transfering a GcRoot across threads.
1203 if (!target->RequestSynchronousCheckpoint(&c, art::ThreadState::kRunnable)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001204 return ERR(THREAD_NOT_ALIVE);
1205 } else {
Alex Light318afe62018-03-22 16:50:10 -07001206 return c.GetResult(data);
Alex Lightbebd7bd2017-07-25 14:05:52 -07001207 }
1208}
1209
1210#define FOR_JVMTI_JVALUE_TYPES(fn) \
1211 fn(jint, art::Primitive::kPrimInt, i) \
1212 fn(jlong, art::Primitive::kPrimLong, j) \
1213 fn(jfloat, art::Primitive::kPrimFloat, f) \
1214 fn(jdouble, art::Primitive::kPrimDouble, d) \
1215 fn(jobject, art::Primitive::kPrimNot, l)
1216
1217namespace impl {
1218
1219template<typename T> void WriteJvalue(T, jvalue*);
1220template<typename T> void ReadJvalue(jvalue, T*);
1221template<typename T> art::Primitive::Type GetJNIType();
1222
1223#define JNI_TYPE_CHAR(type, prim, id) \
1224template<> art::Primitive::Type GetJNIType<type>() { \
1225 return prim; \
1226}
1227
1228FOR_JVMTI_JVALUE_TYPES(JNI_TYPE_CHAR);
1229
1230#undef JNI_TYPE_CHAR
1231
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001232#define RW_JVALUE(srctype, prim, id) \
1233 template<> void ReadJvalue<srctype>(jvalue in, std::add_pointer<srctype>::type out) { \
Alex Lightbebd7bd2017-07-25 14:05:52 -07001234 *out = in.id; \
1235 } \
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001236 template<> void WriteJvalue<srctype>(srctype in, jvalue* out) { \
Alex Lightbebd7bd2017-07-25 14:05:52 -07001237 out->id = in; \
1238 }
1239
1240FOR_JVMTI_JVALUE_TYPES(RW_JVALUE);
1241
1242#undef RW_JVALUE
1243
1244} // namespace impl
1245
1246template<typename T>
1247jvmtiError MethodUtil::SetLocalVariable(jvmtiEnv* env,
1248 jthread thread,
1249 jint depth,
1250 jint slot,
1251 T data) {
1252 jvalue v = {.j = 0};
1253 art::Primitive::Type type = impl::GetJNIType<T>();
1254 impl::WriteJvalue(data, &v);
1255 return SetLocalVariableGeneric(env, thread, depth, slot, type, v);
1256}
1257
1258template<typename T>
1259jvmtiError MethodUtil::GetLocalVariable(jvmtiEnv* env,
1260 jthread thread,
1261 jint depth,
1262 jint slot,
1263 T* data) {
1264 if (data == nullptr) {
1265 return ERR(NULL_POINTER);
1266 }
1267 jvalue v = {.j = 0};
1268 art::Primitive::Type type = impl::GetJNIType<T>();
1269 jvmtiError err = GetLocalVariableGeneric(env, thread, depth, slot, type, &v);
1270 if (err != OK) {
1271 return err;
1272 } else {
1273 impl::ReadJvalue(v, data);
1274 return OK;
1275 }
1276}
1277
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001278#define GET_SET_LV(srctype, prim, id) \
1279 template jvmtiError MethodUtil::GetLocalVariable<srctype>(jvmtiEnv*, \
1280 jthread, \
1281 jint, \
1282 jint, \
1283 std::add_pointer<srctype>::type); \
1284 template jvmtiError MethodUtil::SetLocalVariable<srctype>(jvmtiEnv*, \
1285 jthread, \
1286 jint, \
1287 jint, \
1288 srctype);
Alex Lightbebd7bd2017-07-25 14:05:52 -07001289
1290FOR_JVMTI_JVALUE_TYPES(GET_SET_LV);
1291
1292#undef GET_SET_LV
1293
1294#undef FOR_JVMTI_JVALUE_TYPES
1295
Andreas Gampe3c252f02016-10-27 18:25:17 -07001296} // namespace openjdkjvmti