blob: d8ee981063be72031ef4ca1655e3a70b330aaba2 [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 Lightd78ddec2017-04-18 15:20:38 -070096 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
97 event_handler->DispatchEvent<ArtJvmtiEvent::kNativeMethodBind>(
98 thread,
Alex Light0af8cde2017-04-20 13:35:05 -070099 static_cast<JNIEnv*>(jnienv),
Alex Lightd78ddec2017-04-18 15:20:38 -0700100 thread_jni.get(),
101 art::jni::EncodeArtMethod(method),
102 const_cast<void*>(cur_method),
103 new_method);
104 }
105 }
106
107 EventHandler* event_handler = nullptr;
108};
109
110TiMethodCallback gMethodCallback;
111
112void MethodUtil::Register(EventHandler* handler) {
113 gMethodCallback.event_handler = handler;
114 art::ScopedThreadStateChange stsc(art::Thread::Current(),
115 art::ThreadState::kWaitingForDebuggerToAttach);
116 art::ScopedSuspendAll ssa("Add method callback");
Alex Light21611932017-09-26 13:07:39 -0700117 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
118 callbacks->AddMethodCallback(&gMethodCallback);
Alex Lightd78ddec2017-04-18 15:20:38 -0700119}
120
121void MethodUtil::Unregister() {
122 art::ScopedThreadStateChange stsc(art::Thread::Current(),
123 art::ThreadState::kWaitingForDebuggerToAttach);
124 art::ScopedSuspendAll ssa("Remove method callback");
Alex Light21611932017-09-26 13:07:39 -0700125 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
126 callbacks->RemoveMethodCallback(&gMethodCallback);
Alex Lightd78ddec2017-04-18 15:20:38 -0700127}
128
Alex Light4c174282017-07-05 10:18:18 -0700129jvmtiError MethodUtil::GetBytecodes(jvmtiEnv* env,
130 jmethodID method,
131 jint* size_ptr,
132 unsigned char** bytecode_ptr) {
133 if (method == nullptr) {
134 return ERR(INVALID_METHODID);
135 }
136 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
137
138 if (art_method->IsNative()) {
139 return ERR(NATIVE_METHOD);
140 }
141
142 if (size_ptr == nullptr || bytecode_ptr == nullptr) {
143 return ERR(NULL_POINTER);
144 }
145
146 art::ScopedObjectAccess soa(art::Thread::Current());
David Sehr0225f8e2018-01-31 08:52:24 +0000147 art::CodeItemInstructionAccessor accessor(art_method->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800148 if (!accessor.HasCodeItem()) {
Alex Light4c174282017-07-05 10:18:18 -0700149 *size_ptr = 0;
150 *bytecode_ptr = nullptr;
151 return OK;
152 }
153 // 2 bytes per instruction for dex code.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800154 *size_ptr = accessor.InsnsSizeInCodeUnits() * 2;
Alex Light4c174282017-07-05 10:18:18 -0700155 jvmtiError err = env->Allocate(*size_ptr, bytecode_ptr);
156 if (err != OK) {
157 return err;
158 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800159 memcpy(*bytecode_ptr, accessor.Insns(), *size_ptr);
Alex Light4c174282017-07-05 10:18:18 -0700160 return OK;
161}
162
Andreas Gampef71832e2017-01-09 11:38:04 -0800163jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
164 jmethodID method,
165 jint* size_ptr) {
166 if (method == nullptr) {
167 return ERR(INVALID_METHODID);
168 }
169 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
170
171 if (art_method->IsNative()) {
172 return ERR(NATIVE_METHOD);
173 }
174
175 if (size_ptr == nullptr) {
176 return ERR(NULL_POINTER);
177 }
178
179 art::ScopedObjectAccess soa(art::Thread::Current());
180 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700181 // Use the shorty.
182 art::ArtMethod* base_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
183 size_t arg_count = art::ArtMethod::NumArgRegisters(base_method->GetShorty());
184 if (!base_method->IsStatic()) {
185 arg_count++;
186 }
187 *size_ptr = static_cast<jint>(arg_count);
Andreas Gampef71832e2017-01-09 11:38:04 -0800188 return ERR(NONE);
189 }
190
191 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
David Sehr0225f8e2018-01-31 08:52:24 +0000192 *size_ptr = art_method->DexInstructionData().InsSize();
Andreas Gampef71832e2017-01-09 11:38:04 -0800193
194 return ERR(NONE);
195}
196
Alex Lightce68cc62017-07-26 10:30:38 -0700197jvmtiError MethodUtil::GetLocalVariableTable(jvmtiEnv* env,
198 jmethodID method,
199 jint* entry_count_ptr,
200 jvmtiLocalVariableEntry** table_ptr) {
201 if (method == nullptr) {
202 return ERR(INVALID_METHODID);
203 }
204 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
205
206 if (art_method->IsNative()) {
207 return ERR(NATIVE_METHOD);
208 }
209
210 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
211 return ERR(NULL_POINTER);
212 }
213
214 art::ScopedObjectAccess soa(art::Thread::Current());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800215
216 const art::DexFile* const dex_file = art_method->GetDexFile();
217 if (dex_file == nullptr) {
218 return ERR(ABSENT_INFORMATION);
219 }
220
221 // TODO HasCodeItem == false means that the method is abstract (or native, but we check that
Alex Lightce68cc62017-07-26 10:30:38 -0700222 // earlier). We should check what is returned by the RI in this situation since it's not clear
223 // what the appropriate return value is from the spec.
David Sehr0225f8e2018-01-31 08:52:24 +0000224 art::CodeItemDebugInfoAccessor accessor(art_method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800225 if (!accessor.HasCodeItem()) {
Alex Lightce68cc62017-07-26 10:30:38 -0700226 return ERR(ABSENT_INFORMATION);
227 }
228
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700229 std::vector<jvmtiLocalVariableEntry> variables;
230 jvmtiError err = OK;
Alex Lightce68cc62017-07-26 10:30:38 -0700231
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700232 auto release = [&](jint* out_entry_count_ptr, jvmtiLocalVariableEntry** out_table_ptr) {
233 jlong table_size = sizeof(jvmtiLocalVariableEntry) * variables.size();
234 if (err != OK ||
235 (err = env->Allocate(table_size,
236 reinterpret_cast<unsigned char**>(out_table_ptr))) != OK) {
237 for (jvmtiLocalVariableEntry& e : variables) {
238 env->Deallocate(reinterpret_cast<unsigned char*>(e.name));
239 env->Deallocate(reinterpret_cast<unsigned char*>(e.signature));
240 env->Deallocate(reinterpret_cast<unsigned char*>(e.generic_signature));
241 }
242 return err;
Alex Lightce68cc62017-07-26 10:30:38 -0700243 }
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700244 *out_entry_count_ptr = variables.size();
245 memcpy(*out_table_ptr, variables.data(), table_size);
246 return OK;
Alex Lightce68cc62017-07-26 10:30:38 -0700247 };
248
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700249 auto visitor = [&](const art::DexFile::LocalInfo& entry) {
250 if (err != OK) {
251 return;
252 }
253 JvmtiUniquePtr<char[]> name_str = CopyString(env, entry.name_, &err);
254 if (err != OK) {
255 return;
256 }
257 JvmtiUniquePtr<char[]> sig_str = CopyString(env, entry.descriptor_, &err);
258 if (err != OK) {
259 return;
260 }
261 JvmtiUniquePtr<char[]> generic_sig_str = CopyString(env, entry.signature_, &err);
262 if (err != OK) {
263 return;
264 }
265 variables.push_back({
266 .start_location = static_cast<jlocation>(entry.start_address_),
267 .length = static_cast<jint>(entry.end_address_ - entry.start_address_),
268 .name = name_str.release(),
269 .signature = sig_str.release(),
270 .generic_signature = generic_sig_str.release(),
271 .slot = entry.reg_,
272 });
273 };
274
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800275 if (!accessor.DecodeDebugLocalInfo(art_method->IsStatic(),
276 art_method->GetDexMethodIndex(),
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700277 visitor)) {
Alex Lightce68cc62017-07-26 10:30:38 -0700278 // Something went wrong with decoding the debug information. It might as well not be there.
279 return ERR(ABSENT_INFORMATION);
Alex Lightce68cc62017-07-26 10:30:38 -0700280 }
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700281 return release(entry_count_ptr, table_ptr);
Alex Lightce68cc62017-07-26 10:30:38 -0700282}
283
Andreas Gampef71832e2017-01-09 11:38:04 -0800284jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
285 jmethodID method,
286 jint* max_ptr) {
287 if (method == nullptr) {
288 return ERR(INVALID_METHODID);
289 }
290 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
291
292 if (art_method->IsNative()) {
293 return ERR(NATIVE_METHOD);
294 }
295
296 if (max_ptr == nullptr) {
297 return ERR(NULL_POINTER);
298 }
299
300 art::ScopedObjectAccess soa(art::Thread::Current());
301 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
302 // This isn't specified as an error case, so return 0.
303 *max_ptr = 0;
304 return ERR(NONE);
305 }
306
307 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
David Sehr0225f8e2018-01-31 08:52:24 +0000308 *max_ptr = art_method->DexInstructionData().RegistersSize();
Andreas Gampef71832e2017-01-09 11:38:04 -0800309
310 return ERR(NONE);
311}
312
Andreas Gampe3c252f02016-10-27 18:25:17 -0700313jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
314 jmethodID method,
315 char** name_ptr,
316 char** signature_ptr,
317 char** generic_ptr) {
318 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe13b27842016-11-07 16:48:23 -0800319 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700320 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
321
Andreas Gampe54711412017-02-21 12:41:43 -0800322 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700323 if (name_ptr != nullptr) {
324 const char* method_name = art_method->GetName();
325 if (method_name == nullptr) {
326 method_name = "<error>";
327 }
Andreas Gampe54711412017-02-21 12:41:43 -0800328 jvmtiError ret;
329 name_copy = CopyString(env, method_name, &ret);
330 if (name_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700331 return ret;
332 }
Andreas Gampe54711412017-02-21 12:41:43 -0800333 *name_ptr = name_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700334 }
335
Andreas Gampe54711412017-02-21 12:41:43 -0800336 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700337 if (signature_ptr != nullptr) {
338 const art::Signature sig = art_method->GetSignature();
339 std::string str = sig.ToString();
Andreas Gampe54711412017-02-21 12:41:43 -0800340 jvmtiError ret;
341 signature_copy = CopyString(env, str.c_str(), &ret);
342 if (signature_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700343 return ret;
344 }
Andreas Gampe54711412017-02-21 12:41:43 -0800345 *signature_ptr = signature_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700346 }
347
Andreas Gampe862bdd82016-11-18 13:31:13 -0800348 if (generic_ptr != nullptr) {
349 *generic_ptr = nullptr;
Andreas Gampe27dfa052017-02-16 15:04:36 -0800350 if (!art_method->GetDeclaringClass()->IsProxyClass()) {
Vladimir Markoacb906d2018-05-30 10:23:49 +0100351 art::ObjPtr<art::mirror::ObjectArray<art::mirror::String>> str_array =
Andreas Gampe27dfa052017-02-16 15:04:36 -0800352 art::annotations::GetSignatureAnnotationForMethod(art_method);
353 if (str_array != nullptr) {
354 std::ostringstream oss;
355 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
356 oss << str_array->Get(i)->ToModifiedUtf8();
357 }
358 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800359 jvmtiError ret;
360 JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
361 if (generic_copy == nullptr) {
Andreas Gampe27dfa052017-02-16 15:04:36 -0800362 return ret;
363 }
Andreas Gampe54711412017-02-21 12:41:43 -0800364 *generic_ptr = generic_copy.release();
Andreas Gampe27dfa052017-02-16 15:04:36 -0800365 } else if (soa.Self()->IsExceptionPending()) {
366 // TODO: Should we report an error here?
367 soa.Self()->ClearException();
368 }
369 }
Andreas Gampe862bdd82016-11-18 13:31:13 -0800370 }
Andreas Gampe3c252f02016-10-27 18:25:17 -0700371
372 // Everything is fine, release the buffers.
373 name_copy.release();
374 signature_copy.release();
375
376 return ERR(NONE);
377}
378
Andreas Gampe368a2082016-10-28 17:33:13 -0700379jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
380 jmethodID method,
381 jclass* declaring_class_ptr) {
382 if (declaring_class_ptr == nullptr) {
383 return ERR(NULL_POINTER);
384 }
385
Andreas Gampe13b27842016-11-07 16:48:23 -0800386 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700387 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
388
Andreas Gampe13b27842016-11-07 16:48:23 -0800389 art::ScopedObjectAccess soa(art::Thread::Current());
Vladimir Markod93e3742018-07-18 10:58:13 +0100390 art::ObjPtr<art::mirror::Class> klass = art_method->GetDeclaringClass();
Andreas Gampe368a2082016-10-28 17:33:13 -0700391 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
392
393 return ERR(NONE);
394}
395
Andreas Gampef71832e2017-01-09 11:38:04 -0800396jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
397 jmethodID method,
398 jlocation* start_location_ptr,
399 jlocation* end_location_ptr) {
400 if (method == nullptr) {
401 return ERR(INVALID_METHODID);
402 }
403 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
404
405 if (art_method->IsNative()) {
406 return ERR(NATIVE_METHOD);
407 }
408
409 if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
410 return ERR(NULL_POINTER);
411 }
412
413 art::ScopedObjectAccess soa(art::Thread::Current());
414 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700415 // This isn't specified as an error case, so return -1/-1 as the RI does.
416 *start_location_ptr = -1;
417 *end_location_ptr = -1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800418 return ERR(NONE);
419 }
420
421 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
422 *start_location_ptr = 0;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800423 *end_location_ptr = art_method->DexInstructions().InsnsSizeInCodeUnits() - 1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800424
425 return ERR(NONE);
426}
427
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700428jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
429 jmethodID method,
430 jint* modifiers_ptr) {
431 if (modifiers_ptr == nullptr) {
432 return ERR(NULL_POINTER);
433 }
434
Andreas Gampe13b27842016-11-07 16:48:23 -0800435 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700436 uint32_t modifiers = art_method->GetAccessFlags();
437
438 // Note: Keep this code in sync with Executable.fixMethodFlags.
439 if ((modifiers & art::kAccAbstract) != 0) {
440 modifiers &= ~art::kAccNative;
441 }
442 modifiers &= ~art::kAccSynchronized;
443 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
444 modifiers |= art::kAccSynchronized;
445 }
446 modifiers &= art::kAccJavaFlagsMask;
447
448 *modifiers_ptr = modifiers;
449 return ERR(NONE);
450}
451
Andreas Gampeda3e5612016-12-13 19:00:53 -0800452jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
453 jmethodID method,
454 jint* entry_count_ptr,
455 jvmtiLineNumberEntry** table_ptr) {
456 if (method == nullptr) {
457 return ERR(NULL_POINTER);
458 }
459 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
460 DCHECK(!art_method->IsRuntimeMethod());
461
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800462 art::CodeItemDebugInfoAccessor accessor;
Andreas Gampeda3e5612016-12-13 19:00:53 -0800463 const art::DexFile* dex_file;
464 {
465 art::ScopedObjectAccess soa(art::Thread::Current());
466
467 if (art_method->IsProxyMethod()) {
468 return ERR(ABSENT_INFORMATION);
469 }
470 if (art_method->IsNative()) {
471 return ERR(NATIVE_METHOD);
472 }
473 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
474 return ERR(NULL_POINTER);
475 }
476
David Sehr0225f8e2018-01-31 08:52:24 +0000477 accessor = art::CodeItemDebugInfoAccessor(art_method->DexInstructionDebugInfo());
Andreas Gampeda3e5612016-12-13 19:00:53 -0800478 dex_file = art_method->GetDexFile();
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800479 DCHECK(accessor.HasCodeItem()) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
Andreas Gampeda3e5612016-12-13 19:00:53 -0800480 }
481
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700482 std::vector<jvmtiLineNumberEntry> context;
483 bool success = accessor.DecodeDebugPositionInfo([&](const art::DexFile::PositionInfo& entry) {
484 context.push_back({static_cast<jlocation>(entry.address_), static_cast<jint>(entry.line_)});
485 return false;
486 });
Andreas Gampeda3e5612016-12-13 19:00:53 -0800487 if (!success) {
488 return ERR(ABSENT_INFORMATION);
489 }
490
491 unsigned char* data;
492 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
493 jvmtiError alloc_error = env->Allocate(mem_size, &data);
494 if (alloc_error != ERR(NONE)) {
495 return alloc_error;
496 }
497 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
498 memcpy(*table_ptr, context.data(), mem_size);
499 *entry_count_ptr = static_cast<jint>(context.size());
500
501 return ERR(NONE);
502}
503
Andreas Gampefdeef522017-01-09 14:40:25 -0800504template <typename T>
505static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED,
506 jmethodID method,
507 T test,
508 jboolean* is_t_ptr) {
509 if (method == nullptr) {
510 return ERR(INVALID_METHODID);
511 }
512 if (is_t_ptr == nullptr) {
513 return ERR(NULL_POINTER);
514 }
515
516 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
517 *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
518
519 return ERR(NONE);
520}
521
522jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
523 auto test = [](art::ArtMethod* method) {
524 return method->IsNative();
525 };
526 return IsMethodT(env, m, test, is_native_ptr);
527}
528
529jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
530 auto test = [](art::ArtMethod* method) {
531 return method->IsObsolete();
532 };
533 return IsMethodT(env, m, test, is_obsolete_ptr);
534}
535
536jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
537 auto test = [](art::ArtMethod* method) {
538 return method->IsSynthetic();
539 };
540 return IsMethodT(env, m, test, is_synthetic_ptr);
541}
542
Alex Lightbebd7bd2017-07-25 14:05:52 -0700543class CommonLocalVariableClosure : public art::Closure {
544 public:
Alex Lighte2ddce32019-05-22 17:08:35 +0000545 // The verifier isn't always able to be as specific as the local-variable-table. We can only get
546 // 32-bit, 64-bit or reference.
547 enum class VerifierPrimitiveType {
548 k32BitValue, // float, int, short, char, boolean, byte
549 k64BitValue, // double, long
550 kReferenceValue, // Object
551 kZeroValue, // null or zero constant. Might be either k32BitValue or kReferenceValue
552 };
Alex Lightbebd7bd2017-07-25 14:05:52 -0700553
Alex Lighte2ddce32019-05-22 17:08:35 +0000554 using SlotType = std::variant<art::Primitive::Type, VerifierPrimitiveType>;
555
556 CommonLocalVariableClosure(jvmtiEnv* jvmti, jint depth, jint slot)
557 : jvmti_(jvmti), result_(ERR(INTERNAL)), depth_(depth), slot_(slot) {}
558
559 void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700560 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
Alex Lighta4cdd362019-04-18 09:17:10 -0700561 bool needs_instrument;
562 {
563 art::ScopedAssertNoThreadSuspension sants("CommonLocalVariableClosure::Run");
564 std::unique_ptr<art::Context> context(art::Context::Create());
565 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
566 visitor.WalkStack();
567 if (!visitor.FoundFrame()) {
568 // Must have been a bad depth.
569 result_ = ERR(NO_MORE_FRAMES);
570 return;
571 }
572 art::ArtMethod* method = visitor.GetMethod();
573 // Native and 'art' proxy methods don't have registers.
574 if (method->IsNative() || method->IsProxyMethod()) {
575 // TODO It might be useful to fake up support for get at least on proxy frames.
576 result_ = ERR(OPAQUE_FRAME);
577 return;
Alex Light6045bc22019-05-22 08:33:30 -0700578 } else if (slot_ >= method->DexInstructionData().RegistersSize() || slot_ < 0) {
Alex Lighta4cdd362019-04-18 09:17:10 -0700579 result_ = ERR(INVALID_SLOT);
580 return;
581 }
582 needs_instrument = !visitor.IsShadowFrame();
583 uint32_t pc = visitor.GetDexPc(/*abort_on_failure=*/false);
584 if (pc == art::dex::kDexNoIndex) {
585 // Cannot figure out current PC.
586 result_ = ERR(OPAQUE_FRAME);
587 return;
588 }
589 std::string descriptor;
Alex Lighte2ddce32019-05-22 17:08:35 +0000590 SlotType slot_type{ art::Primitive::kPrimVoid };
Alex Lighta4cdd362019-04-18 09:17:10 -0700591 jvmtiError err = GetSlotType(method, pc, &descriptor, &slot_type);
592 if (err != OK) {
593 result_ = err;
594 return;
595 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700596
Alex Lighta4cdd362019-04-18 09:17:10 -0700597 err = GetTypeError(method, slot_type, descriptor);
598 if (err != OK) {
599 result_ = err;
600 return;
601 }
602 result_ = Execute(method, visitor);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700603 }
Alex Light0a5ec3d2017-07-25 16:50:26 -0700604 if (needs_instrument) {
Alex Lighta4cdd362019-04-18 09:17:10 -0700605 DeoptManager::Get()->DeoptimizeThread(self);
Alex Light0a5ec3d2017-07-25 16:50:26 -0700606 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700607 }
608
Alex Light318afe62018-03-22 16:50:10 -0700609 virtual jvmtiError GetResult() {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700610 return result_;
611 }
612
613 protected:
614 virtual jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Alex Light318afe62018-03-22 16:50:10 -0700615 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700616 virtual jvmtiError GetTypeError(art::ArtMethod* method,
Alex Lighte2ddce32019-05-22 17:08:35 +0000617 SlotType type,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700618 const std::string& descriptor)
Alex Light318afe62018-03-22 16:50:10 -0700619 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700620
621 jvmtiError GetSlotType(art::ArtMethod* method,
622 uint32_t dex_pc,
623 /*out*/std::string* descriptor,
Alex Lighte2ddce32019-05-22 17:08:35 +0000624 /*out*/SlotType* type)
625 REQUIRES_SHARED(art::Locks::mutator_lock_);
626
627 jvmtiError InferSlotTypeFromVerifier(art::ArtMethod* method,
628 uint32_t dex_pc,
629 /*out*/ std::string* descriptor,
630 /*out*/ SlotType* type)
631 REQUIRES_SHARED(art::Locks::mutator_lock_) {
632 art::Thread* self = art::Thread::Current();
633 art::StackHandleScope<2> hs(self);
634 std::unique_ptr<art::verifier::MethodVerifier> verifier(
635 art::verifier::MethodVerifier::CalculateVerificationInfo(
636 self,
637 method,
638 hs.NewHandle(method->GetDexCache()),
639 hs.NewHandle(method->GetDeclaringClass()->GetClassLoader())));
640 if (verifier == nullptr) {
641 JVMTI_LOG(WARNING, jvmti_) << "Unable to extract verification information from "
642 << method->PrettyMethod() << " due to hard verification failures! "
643 << "How did this method even get loaded!";
644 return ERR(INTERNAL);
645 }
646 art::verifier::RegisterLine* line = verifier->GetRegLine(dex_pc);
647 if (line == nullptr) {
648 JVMTI_LOG(WARNING, jvmti_) << "Unable to determine register line at dex-pc " << dex_pc
649 << " for method " << method->PrettyMethod();
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800650 return ERR(OPAQUE_FRAME);
651 }
Alex Lighte2ddce32019-05-22 17:08:35 +0000652 const art::verifier::RegType& rt = line->GetRegisterType(verifier.get(), slot_);
653 if (rt.IsUndefined()) {
654 return ERR(INVALID_SLOT);
655 } else if (rt.IsNonZeroReferenceTypes() || rt.IsNull()) {
656 *descriptor = (rt.HasClass() ? rt.GetDescriptor() : "Ljava/lang/Object;");
657 *type = VerifierPrimitiveType::kReferenceValue;
658 return OK;
659 } else if (rt.IsZero()) {
660 *descriptor = "I";
661 *type = VerifierPrimitiveType::kZeroValue;
662 return OK;
663 } else if (rt.IsCategory1Types()) {
664 *descriptor = "I";
665 *type = VerifierPrimitiveType::k32BitValue;
666 return OK;
667 } else if (rt.IsCategory2Types() && rt.IsLowHalf()) {
668 *descriptor = "J";
669 *type = VerifierPrimitiveType::k64BitValue;
670 return OK;
671 } else {
672 // The slot doesn't have a type. Must not be valid here.
David Srbecky99cbfb52019-05-22 11:39:45 +0000673 return ERR(INVALID_SLOT);
Alex Lighte48fd0b2019-05-20 10:04:44 -0700674 }
675 }
676
Alex Lighte2ddce32019-05-22 17:08:35 +0000677 constexpr VerifierPrimitiveType SquashType(SlotType t) {
678 if (std::holds_alternative<art::Primitive::Type>(t)) {
679 switch (std::get<art::Primitive::Type>(t)) {
680 // 32-bit primitives
681 case art::Primitive::kPrimByte:
682 case art::Primitive::kPrimChar:
683 case art::Primitive::kPrimInt:
684 case art::Primitive::kPrimShort:
685 case art::Primitive::kPrimBoolean:
686 case art::Primitive::kPrimFloat:
687 return VerifierPrimitiveType::k32BitValue;
688 // 64-bit primitives
689 case art::Primitive::kPrimLong:
690 case art::Primitive::kPrimDouble:
691 return VerifierPrimitiveType::k64BitValue;
692 case art::Primitive::kPrimNot:
693 return VerifierPrimitiveType::kReferenceValue;
694 case art::Primitive::kPrimVoid:
695 LOG(FATAL) << "Got kPrimVoid";
696 UNREACHABLE();
697 }
698 } else {
699 return std::get<VerifierPrimitiveType>(t);
700 }
701 }
702
703 jvmtiEnv* jvmti_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700704 jvmtiError result_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700705 jint depth_;
706 jint slot_;
Alex Lighte2ddce32019-05-22 17:08:35 +0000707
708 private:
709 DISALLOW_COPY_AND_ASSIGN(CommonLocalVariableClosure);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700710};
711
Alex Lighte2ddce32019-05-22 17:08:35 +0000712std::ostream& operator<<(std::ostream& os,
713 CommonLocalVariableClosure::VerifierPrimitiveType state) {
714 switch (state) {
715 case CommonLocalVariableClosure::VerifierPrimitiveType::k32BitValue:
716 return os << "32BitValue";
717 case CommonLocalVariableClosure::VerifierPrimitiveType::k64BitValue:
718 return os << "64BitValue";
719 case CommonLocalVariableClosure::VerifierPrimitiveType::kReferenceValue:
720 return os << "ReferenceValue";
721 case CommonLocalVariableClosure::VerifierPrimitiveType::kZeroValue:
722 return os << "ZeroValue";
723 }
724}
725
726std::ostream& operator<<(std::ostream& os, CommonLocalVariableClosure::SlotType state) {
727 if (std::holds_alternative<art::Primitive::Type>(state)) {
728 return os << "Primitive::Type[" << std::get<art::Primitive::Type>(state) << "]";
729 } else {
730 return os << "VerifierPrimitiveType["
731 << std::get<CommonLocalVariableClosure::VerifierPrimitiveType>(state) << "]";
732 }
733}
734
735jvmtiError CommonLocalVariableClosure::GetSlotType(art::ArtMethod* method,
736 uint32_t dex_pc,
737 /*out*/ std::string* descriptor,
738 /*out*/ SlotType* type) {
739 const art::DexFile* dex_file = method->GetDexFile();
740 if (dex_file == nullptr) {
741 return ERR(OPAQUE_FRAME);
742 }
743 art::CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
744 if (!accessor.HasCodeItem()) {
745 return ERR(OPAQUE_FRAME);
746 }
747 bool found = false;
748 *type = art::Primitive::kPrimVoid;
749 descriptor->clear();
750 auto visitor = [&](const art::DexFile::LocalInfo& entry) {
751 if (!found && entry.start_address_ <= dex_pc && entry.end_address_ > dex_pc &&
752 entry.reg_ == slot_) {
753 found = true;
754 *type = art::Primitive::GetType(entry.descriptor_[0]);
755 *descriptor = entry.descriptor_;
756 }
757 };
758 if (!accessor.DecodeDebugLocalInfo(method->IsStatic(), method->GetDexMethodIndex(), visitor) ||
759 !found) {
760 // Something went wrong with decoding the debug information. It might as well not be there.
761 // Try to find the type with the verifier.
762 // TODO This is very slow.
763 return InferSlotTypeFromVerifier(method, dex_pc, descriptor, type);
764 } else if (art::kIsDebugBuild) {
765 std::string type_unused;
766 SlotType verifier_type{ art::Primitive::kPrimVoid };
767 DCHECK_EQ(InferSlotTypeFromVerifier(method, dex_pc, &type_unused, &verifier_type), OK)
768 << method->PrettyMethod() << " failed to verify!";
769 if (*type == SlotType{ art::Primitive::kPrimNot }) {
770 // We cannot distinguish between a constant 0 and a null reference so we return that it is a
771 // 32bit value (Due to the way references are read by the interpreter this is safe even if
772 // it's modified, the value will remain null). This is not ideal since it prevents modifying
773 // locals in some circumstances but generally is not a big deal (since one can just modify it
774 // later once it's been determined to be a reference by a later instruction).
775 DCHECK(verifier_type == SlotType { VerifierPrimitiveType::kZeroValue } ||
776 verifier_type == SlotType { VerifierPrimitiveType::kReferenceValue })
777 << "Verifier disagrees on type of slot! debug: " << *type
778 << " verifier: " << verifier_type;
779 } else if (verifier_type == SlotType { VerifierPrimitiveType::kZeroValue }) {
780 DCHECK(VerifierPrimitiveType::k32BitValue == SquashType(*type) ||
781 VerifierPrimitiveType::kReferenceValue == SquashType(*type))
782 << "Verifier disagrees on type of slot! debug: " << *type
783 << " verifier: " << verifier_type;
784 } else {
785 DCHECK_EQ(SquashType(verifier_type), SquashType(*type))
786 << "Verifier disagrees on type of slot! debug: " << *type
787 << " verifier: " << verifier_type;
788 }
789 }
790 return OK;
791}
792
Alex Lightbebd7bd2017-07-25 14:05:52 -0700793class GetLocalVariableClosure : public CommonLocalVariableClosure {
794 public:
Alex Lighte2ddce32019-05-22 17:08:35 +0000795 GetLocalVariableClosure(jvmtiEnv* jvmti,
796 jint depth,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700797 jint slot,
798 art::Primitive::Type type,
799 jvalue* val)
Alex Lighte2ddce32019-05-22 17:08:35 +0000800 : CommonLocalVariableClosure(jvmti, depth, slot),
Alex Light318afe62018-03-22 16:50:10 -0700801 type_(type),
802 val_(val),
803 obj_val_(nullptr) {}
804
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700805 jvmtiError GetResult() override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light318afe62018-03-22 16:50:10 -0700806 if (result_ == OK && type_ == art::Primitive::kPrimNot) {
Alex Lighta4cdd362019-04-18 09:17:10 -0700807 if (obj_val_ == nullptr) {
808 val_->l = nullptr;
809 } else {
810 art::JNIEnvExt* jni = art::Thread::Current()->GetJniEnv();
811 val_->l = static_cast<JNIEnv*>(jni)->NewLocalRef(obj_val_);
812 jni->DeleteGlobalRef(obj_val_);
813 obj_val_ = nullptr;
814 }
Alex Light318afe62018-03-22 16:50:10 -0700815 }
816 return CommonLocalVariableClosure::GetResult();
817 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700818
819 protected:
Alex Lighte2ddce32019-05-22 17:08:35 +0000820 jvmtiError
821 GetTypeError(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor) override
822 REQUIRES_SHARED(art::Locks::mutator_lock_) {
823 jvmtiError res = GetTypeErrorInner(method, slot_type, descriptor);
824 if (res == ERR(TYPE_MISMATCH)) {
825 JVMTI_LOG(INFO, jvmti_) << "Unable to Get local variable in slot " << slot_ << ". Expected"
826 << " slot to be of type compatible with " << SlotType { type_ }
827 << " but slot is " << slot_type;
828 } else if (res != OK) {
829 JVMTI_LOG(INFO, jvmti_) << "Unable to get local variable in slot " << slot_ << ".";
830 }
831 return res;
832 }
833
834 jvmtiError GetTypeErrorInner(art::ArtMethod* method ATTRIBUTE_UNUSED,
835 SlotType slot_type,
836 const std::string& descriptor ATTRIBUTE_UNUSED)
837 REQUIRES_SHARED(art::Locks::mutator_lock_) {
838 switch (type_) {
David Srbecky99cbfb52019-05-22 11:39:45 +0000839 case art::Primitive::kPrimFloat:
Alex Lighte2ddce32019-05-22 17:08:35 +0000840 case art::Primitive::kPrimInt: {
841 if (std::holds_alternative<VerifierPrimitiveType>(slot_type)) {
842 return (slot_type == SlotType { VerifierPrimitiveType::k32BitValue } ||
843 slot_type == SlotType { VerifierPrimitiveType::kZeroValue })
844 ? OK
845 : ERR(TYPE_MISMATCH);
846 } else if (type_ == art::Primitive::kPrimFloat ||
847 slot_type == SlotType { art::Primitive::kPrimFloat }) {
848 // Check that we are actually a float.
849 return (SlotType { type_ } == slot_type) ? OK : ERR(TYPE_MISMATCH);
850 } else {
851 // Some smaller int type.
852 return SquashType(slot_type) == SquashType(SlotType { type_ }) ? OK : ERR(TYPE_MISMATCH);
853 }
854 }
855 case art::Primitive::kPrimLong:
856 case art::Primitive::kPrimDouble: {
857 // todo
858 if (std::holds_alternative<VerifierPrimitiveType>(slot_type)) {
859 return (slot_type == SlotType { VerifierPrimitiveType::k64BitValue })
860 ? OK
861 : ERR(TYPE_MISMATCH);
862 } else {
863 return slot_type == SlotType { type_ } ? OK : ERR(TYPE_MISMATCH);
864 }
865 }
David Srbecky99cbfb52019-05-22 11:39:45 +0000866 case art::Primitive::kPrimNot:
Alex Lighte2ddce32019-05-22 17:08:35 +0000867 return (SquashType(slot_type) == VerifierPrimitiveType::kReferenceValue ||
868 SquashType(slot_type) == VerifierPrimitiveType::kZeroValue)
869 ? OK
870 : ERR(TYPE_MISMATCH);
871 case art::Primitive::kPrimShort:
872 case art::Primitive::kPrimChar:
873 case art::Primitive::kPrimByte:
874 case art::Primitive::kPrimBoolean:
Alex Lightbebd7bd2017-07-25 14:05:52 -0700875 case art::Primitive::kPrimVoid:
876 LOG(FATAL) << "Unexpected primitive type " << slot_type;
877 UNREACHABLE();
878 }
879 }
880
881 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100882 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700883 switch (type_) {
884 case art::Primitive::kPrimNot: {
885 uint32_t ptr_val;
886 if (!visitor.GetVReg(method,
887 static_cast<uint16_t>(slot_),
888 art::kReferenceVReg,
889 &ptr_val)) {
890 return ERR(OPAQUE_FRAME);
891 }
Alex Lighta4cdd362019-04-18 09:17:10 -0700892 art::JNIEnvExt* jni = art::Thread::Current()->GetJniEnv();
893 art::ObjPtr<art::mirror::Object> obj(reinterpret_cast<art::mirror::Object*>(ptr_val));
Alex Light02353362019-05-03 11:27:07 -0700894 ScopedLocalRef<jobject> local(
895 jni, obj.IsNull() ? nullptr : jni->AddLocalReference<jobject>(obj));
Alex Lighta4cdd362019-04-18 09:17:10 -0700896 obj_val_ = jni->NewGlobalRef(local.get());
Alex Lightbebd7bd2017-07-25 14:05:52 -0700897 break;
898 }
899 case art::Primitive::kPrimInt:
900 case art::Primitive::kPrimFloat: {
901 if (!visitor.GetVReg(method,
902 static_cast<uint16_t>(slot_),
903 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg : art::kIntVReg,
904 reinterpret_cast<uint32_t*>(&val_->i))) {
905 return ERR(OPAQUE_FRAME);
906 }
907 break;
908 }
909 case art::Primitive::kPrimDouble:
910 case art::Primitive::kPrimLong: {
911 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
912 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
913 if (!visitor.GetVRegPair(method,
914 static_cast<uint16_t>(slot_),
915 lo_type,
916 high_type,
917 reinterpret_cast<uint64_t*>(&val_->j))) {
918 return ERR(OPAQUE_FRAME);
919 }
920 break;
921 }
922 default: {
923 LOG(FATAL) << "unexpected register type " << type_;
924 UNREACHABLE();
925 }
926 }
927 return OK;
928 }
929
930 private:
931 art::Primitive::Type type_;
932 jvalue* val_;
Alex Lighta4cdd362019-04-18 09:17:10 -0700933 // A global reference to the return value. We use the global reference to safely transfer the
934 // value between threads.
935 jobject obj_val_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700936};
937
Alex Lighte2ddce32019-05-22 17:08:35 +0000938jvmtiError MethodUtil::GetLocalVariableGeneric(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700939 jthread thread,
940 jint depth,
941 jint slot,
942 art::Primitive::Type type,
943 jvalue* val) {
944 if (depth < 0) {
945 return ERR(ILLEGAL_ARGUMENT);
946 }
947 art::Thread* self = art::Thread::Current();
948 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -0700949 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700950 art::Thread* target = nullptr;
951 jvmtiError err = ERR(INTERNAL);
952 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -0700953 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700954 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700955 }
Alex Lighte2ddce32019-05-22 17:08:35 +0000956 GetLocalVariableClosure c(env, depth, slot, type, val);
Alex Lighta4cdd362019-04-18 09:17:10 -0700957 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
958 if (!target->RequestSynchronousCheckpoint(&c)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700959 return ERR(THREAD_NOT_ALIVE);
960 } else {
961 return c.GetResult();
962 }
963}
964
965class SetLocalVariableClosure : public CommonLocalVariableClosure {
966 public:
Alex Lighte2ddce32019-05-22 17:08:35 +0000967 SetLocalVariableClosure(jvmtiEnv* jvmti,
968 art::Thread* caller,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700969 jint depth,
970 jint slot,
971 art::Primitive::Type type,
972 jvalue val)
Alex Lighte2ddce32019-05-22 17:08:35 +0000973 : CommonLocalVariableClosure(jvmti, depth, slot), caller_(caller), type_(type), val_(val) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -0700974
975 protected:
Alex Lighte2ddce32019-05-22 17:08:35 +0000976 jvmtiError
977 GetTypeError(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor) override
978 REQUIRES_SHARED(art::Locks::mutator_lock_) {
979 jvmtiError res = GetTypeErrorInner(method, slot_type, descriptor);
980 if (res != OK) {
981 if (res == ERR(TYPE_MISMATCH)) {
982 std::ostringstream desc_exp;
983 std::ostringstream desc_set;
984 if (type_ == art::Primitive::kPrimNot) {
985 desc_exp << " (type: " << descriptor << ")";
986 art::ObjPtr<art::mirror::Object> new_val(art::Thread::Current()->DecodeJObject(val_.l));
987 desc_set << " (type: "
988 << (new_val.IsNull() ? "NULL" : new_val->GetClass()->PrettyDescriptor()) << ")";
989 }
990 JVMTI_LOG(INFO, jvmti_) << "Unable to Set local variable in slot " << slot_ << ". Expected"
991 << " slot to be of type compatible with " << SlotType{ type_ }
992 << desc_set.str() << " but slot is " << slot_type << desc_exp.str();
993 } else {
994 JVMTI_LOG(INFO, jvmti_) << "Unable to set local variable in slot " << slot_ << ". "
995 << err_.str();
996 }
997 }
998 return res;
999 }
1000
1001 jvmtiError
1002 GetTypeErrorInner(art::ArtMethod* method, SlotType slot_type, const std::string& descriptor)
1003 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1004 switch (SquashType(SlotType{ type_ })) {
1005 case VerifierPrimitiveType::k32BitValue: {
1006 if (slot_type == SlotType{ VerifierPrimitiveType::kZeroValue }) {
1007 if (val_.i == 0) {
1008 return OK;
1009 } else {
1010 err_ << "Cannot determine if slot " << slot_ << " is a null reference or 32bit "
1011 << "constant. Cannot allow writing to slot.";
1012 return ERR(INTERNAL);
1013 }
1014 } else if (SquashType(slot_type) != VerifierPrimitiveType::k32BitValue) {
1015 return ERR(TYPE_MISMATCH);
1016 } else if (slot_type == SlotType { VerifierPrimitiveType::k32BitValue } ||
1017 slot_type == SlotType { type_ }) {
1018 return OK;
1019 } else if (type_ == art::Primitive::kPrimFloat ||
1020 slot_type == SlotType { art::Primitive::kPrimFloat }) {
1021 // we should have hit the get == type_ above
1022 return ERR(TYPE_MISMATCH);
1023 } else {
1024 // Some smaller type then int.
1025 return OK;
1026 }
1027 }
1028 case VerifierPrimitiveType::k64BitValue: {
1029 if (slot_type == SlotType { VerifierPrimitiveType::k64BitValue } ||
1030 slot_type == SlotType { type_ }) {
1031 return OK;
1032 } else {
1033 return ERR(TYPE_MISMATCH);
1034 }
1035 }
1036 case VerifierPrimitiveType::kReferenceValue: {
1037 if (SquashType(slot_type) != VerifierPrimitiveType::kReferenceValue &&
1038 SquashType(slot_type) != VerifierPrimitiveType::kZeroValue) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001039 return ERR(TYPE_MISMATCH);
1040 } else if (val_.l == nullptr) {
1041 return OK;
Alex Lighte2ddce32019-05-22 17:08:35 +00001042 } else if (slot_type == SlotType { VerifierPrimitiveType::kZeroValue }) {
1043 err_ << "Cannot determine if slot " << slot_ << " is a null "
1044 << "reference or 32bit constant. Cannot allow writing to slot.";
1045 return ERR(INTERNAL);
Alex Lightbebd7bd2017-07-25 14:05:52 -07001046 } else {
1047 art::ClassLinker* cl = art::Runtime::Current()->GetClassLinker();
Alex Lighte2ddce32019-05-22 17:08:35 +00001048 art::ObjPtr<art::mirror::Class> set_class = caller_->DecodeJObject(val_.l)->GetClass();
Alex Lightbebd7bd2017-07-25 14:05:52 -07001049 art::ObjPtr<art::mirror::ClassLoader> loader =
1050 method->GetDeclaringClass()->GetClassLoader();
1051 art::ObjPtr<art::mirror::Class> slot_class =
1052 cl->LookupClass(caller_, descriptor.c_str(), loader);
Alex Lighte2ddce32019-05-22 17:08:35 +00001053 DCHECK(!slot_class.IsNull()) << descriptor << " slot: " << slot_type;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001054 return slot_class->IsAssignableFrom(set_class) ? OK : ERR(TYPE_MISMATCH);
1055 }
1056 }
Alex Lighte2ddce32019-05-22 17:08:35 +00001057 case VerifierPrimitiveType::kZeroValue: {
1058 LOG(FATAL) << "Illegal result from SquashType of art::Primitive::Type " << type_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001059 UNREACHABLE();
Alex Lighte2ddce32019-05-22 17:08:35 +00001060 }
Alex Lightbebd7bd2017-07-25 14:05:52 -07001061 }
1062 }
1063
1064 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001065 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001066 switch (type_) {
1067 case art::Primitive::kPrimNot: {
Vladimir Marko439d1262019-04-12 14:45:07 +01001068 if (!visitor.SetVRegReference(method,
1069 static_cast<uint16_t>(slot_),
1070 caller_->DecodeJObject(val_.l))) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001071 return ERR(OPAQUE_FRAME);
1072 }
1073 break;
1074 }
1075 case art::Primitive::kPrimInt:
1076 case art::Primitive::kPrimFloat: {
1077 if (!visitor.SetVReg(method,
1078 static_cast<uint16_t>(slot_),
1079 static_cast<uint32_t>(val_.i),
1080 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg
1081 : art::kIntVReg)) {
1082 return ERR(OPAQUE_FRAME);
1083 }
1084 break;
1085 }
1086 case art::Primitive::kPrimDouble:
1087 case art::Primitive::kPrimLong: {
1088 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
1089 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
1090 if (!visitor.SetVRegPair(method,
1091 static_cast<uint16_t>(slot_),
1092 static_cast<uint64_t>(val_.j),
1093 lo_type,
1094 high_type)) {
1095 return ERR(OPAQUE_FRAME);
1096 }
1097 break;
1098 }
1099 default: {
1100 LOG(FATAL) << "unexpected register type " << type_;
1101 UNREACHABLE();
1102 }
1103 }
1104 return OK;
1105 }
1106
1107 private:
Alex Light318afe62018-03-22 16:50:10 -07001108 art::Thread* caller_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001109 art::Primitive::Type type_;
1110 jvalue val_;
Alex Lighte2ddce32019-05-22 17:08:35 +00001111 std::ostringstream err_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001112};
1113
Alex Lighte2ddce32019-05-22 17:08:35 +00001114jvmtiError MethodUtil::SetLocalVariableGeneric(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -07001115 jthread thread,
1116 jint depth,
1117 jint slot,
1118 art::Primitive::Type type,
1119 jvalue val) {
1120 if (depth < 0) {
1121 return ERR(ILLEGAL_ARGUMENT);
1122 }
Alex Lightf2858632018-04-02 11:28:50 -07001123 // Make sure that we know not to do any OSR anymore.
1124 // TODO We should really keep track of this at the Frame granularity.
1125 DeoptManager::Get()->SetLocalsUpdated();
Alex Lightbebd7bd2017-07-25 14:05:52 -07001126 art::Thread* self = art::Thread::Current();
1127 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -07001128 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -07001129 art::Thread* target = nullptr;
1130 jvmtiError err = ERR(INTERNAL);
1131 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -07001132 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -07001133 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001134 }
Alex Lighte2ddce32019-05-22 17:08:35 +00001135 SetLocalVariableClosure c(env, self, depth, slot, type, val);
Alex Lightb1e31a82017-10-04 16:57:36 -07001136 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
Alex Light318afe62018-03-22 16:50:10 -07001137 if (!target->RequestSynchronousCheckpoint(&c)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001138 return ERR(THREAD_NOT_ALIVE);
1139 } else {
1140 return c.GetResult();
1141 }
1142}
1143
1144class GetLocalInstanceClosure : public art::Closure {
1145 public:
Alex Light318afe62018-03-22 16:50:10 -07001146 explicit GetLocalInstanceClosure(jint depth)
Alex Lightbebd7bd2017-07-25 14:05:52 -07001147 : result_(ERR(INTERNAL)),
Alex Lightbebd7bd2017-07-25 14:05:52 -07001148 depth_(depth),
Alex Light318afe62018-03-22 16:50:10 -07001149 val_(nullptr) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -07001150
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001151 void Run(art::Thread* self) override REQUIRES(art::Locks::mutator_lock_) {
Alex Light318afe62018-03-22 16:50:10 -07001152 art::ScopedAssertNoThreadSuspension sants("GetLocalInstanceClosure::Run");
Alex Lightbebd7bd2017-07-25 14:05:52 -07001153 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
1154 std::unique_ptr<art::Context> context(art::Context::Create());
1155 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
1156 visitor.WalkStack();
1157 if (!visitor.FoundFrame()) {
1158 // Must have been a bad depth.
1159 result_ = ERR(NO_MORE_FRAMES);
1160 return;
1161 }
Alex Lightbebd7bd2017-07-25 14:05:52 -07001162 result_ = OK;
Alex Light318afe62018-03-22 16:50:10 -07001163 val_ = art::GcRoot<art::mirror::Object>(visitor.GetThisObject());
Alex Lightbebd7bd2017-07-25 14:05:52 -07001164 }
1165
Alex Light318afe62018-03-22 16:50:10 -07001166 jvmtiError GetResult(jobject* data_out) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1167 if (result_ == OK) {
1168 *data_out = val_.IsNull()
1169 ? nullptr
1170 : art::Thread::Current()->GetJniEnv()->AddLocalReference<jobject>(val_.Read());
1171 }
Alex Lightbebd7bd2017-07-25 14:05:52 -07001172 return result_;
1173 }
1174
1175 private:
1176 jvmtiError result_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001177 jint depth_;
Alex Light318afe62018-03-22 16:50:10 -07001178 art::GcRoot<art::mirror::Object> val_;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001179};
1180
1181jvmtiError MethodUtil::GetLocalInstance(jvmtiEnv* env ATTRIBUTE_UNUSED,
1182 jthread thread,
1183 jint depth,
1184 jobject* data) {
1185 if (depth < 0) {
1186 return ERR(ILLEGAL_ARGUMENT);
1187 }
1188 art::Thread* self = art::Thread::Current();
1189 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -07001190 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -07001191 art::Thread* target = nullptr;
1192 jvmtiError err = ERR(INTERNAL);
1193 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -07001194 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -07001195 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -07001196 }
Alex Light318afe62018-03-22 16:50:10 -07001197 art::ScopedAssertNoThreadSuspension sants("Performing GetLocalInstance");
1198 GetLocalInstanceClosure c(depth);
1199 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. We
1200 // need to avoid suspending as we wait for the checkpoint to occur since we are (potentially)
1201 // transfering a GcRoot across threads.
1202 if (!target->RequestSynchronousCheckpoint(&c, art::ThreadState::kRunnable)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -07001203 return ERR(THREAD_NOT_ALIVE);
1204 } else {
Alex Light318afe62018-03-22 16:50:10 -07001205 return c.GetResult(data);
Alex Lightbebd7bd2017-07-25 14:05:52 -07001206 }
1207}
1208
1209#define FOR_JVMTI_JVALUE_TYPES(fn) \
1210 fn(jint, art::Primitive::kPrimInt, i) \
1211 fn(jlong, art::Primitive::kPrimLong, j) \
1212 fn(jfloat, art::Primitive::kPrimFloat, f) \
1213 fn(jdouble, art::Primitive::kPrimDouble, d) \
1214 fn(jobject, art::Primitive::kPrimNot, l)
1215
1216namespace impl {
1217
1218template<typename T> void WriteJvalue(T, jvalue*);
1219template<typename T> void ReadJvalue(jvalue, T*);
1220template<typename T> art::Primitive::Type GetJNIType();
1221
1222#define JNI_TYPE_CHAR(type, prim, id) \
1223template<> art::Primitive::Type GetJNIType<type>() { \
1224 return prim; \
1225}
1226
1227FOR_JVMTI_JVALUE_TYPES(JNI_TYPE_CHAR);
1228
1229#undef JNI_TYPE_CHAR
1230
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001231#define RW_JVALUE(srctype, prim, id) \
1232 template<> void ReadJvalue<srctype>(jvalue in, std::add_pointer<srctype>::type out) { \
Alex Lightbebd7bd2017-07-25 14:05:52 -07001233 *out = in.id; \
1234 } \
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001235 template<> void WriteJvalue<srctype>(srctype in, jvalue* out) { \
Alex Lightbebd7bd2017-07-25 14:05:52 -07001236 out->id = in; \
1237 }
1238
1239FOR_JVMTI_JVALUE_TYPES(RW_JVALUE);
1240
1241#undef RW_JVALUE
1242
1243} // namespace impl
1244
1245template<typename T>
1246jvmtiError MethodUtil::SetLocalVariable(jvmtiEnv* env,
1247 jthread thread,
1248 jint depth,
1249 jint slot,
1250 T data) {
1251 jvalue v = {.j = 0};
1252 art::Primitive::Type type = impl::GetJNIType<T>();
1253 impl::WriteJvalue(data, &v);
1254 return SetLocalVariableGeneric(env, thread, depth, slot, type, v);
1255}
1256
1257template<typename T>
1258jvmtiError MethodUtil::GetLocalVariable(jvmtiEnv* env,
1259 jthread thread,
1260 jint depth,
1261 jint slot,
1262 T* data) {
1263 if (data == nullptr) {
1264 return ERR(NULL_POINTER);
1265 }
1266 jvalue v = {.j = 0};
1267 art::Primitive::Type type = impl::GetJNIType<T>();
1268 jvmtiError err = GetLocalVariableGeneric(env, thread, depth, slot, type, &v);
1269 if (err != OK) {
1270 return err;
1271 } else {
1272 impl::ReadJvalue(v, data);
1273 return OK;
1274 }
1275}
1276
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001277#define GET_SET_LV(srctype, prim, id) \
1278 template jvmtiError MethodUtil::GetLocalVariable<srctype>(jvmtiEnv*, \
1279 jthread, \
1280 jint, \
1281 jint, \
1282 std::add_pointer<srctype>::type); \
1283 template jvmtiError MethodUtil::SetLocalVariable<srctype>(jvmtiEnv*, \
1284 jthread, \
1285 jint, \
1286 jint, \
1287 srctype);
Alex Lightbebd7bd2017-07-25 14:05:52 -07001288
1289FOR_JVMTI_JVALUE_TYPES(GET_SET_LV);
1290
1291#undef GET_SET_LV
1292
1293#undef FOR_JVMTI_JVALUE_TYPES
1294
Andreas Gampe3c252f02016-10-27 18:25:17 -07001295} // namespace openjdkjvmti