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