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