Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [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_field.h" |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 33 | #include <unordered_map> |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 34 | |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 35 | #include "android-base/thread_annotations.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 36 | #include "art_field-inl.h" |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 37 | #include "art_field.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 38 | #include "art_jvmti.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 39 | #include "base/enums.h" |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 40 | #include "base/locks.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 41 | #include "dex/dex_file_annotations.h" |
David Sehr | 8c0961f | 2018-01-23 16:11:38 -0800 | [diff] [blame] | 42 | #include "dex/modifiers.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 43 | #include "jni/jni_internal.h" |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 44 | #include "mirror/object_array-inl.h" |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 45 | #include "reflective_value_visitor.h" |
| 46 | #include "runtime.h" |
| 47 | #include "runtime_callbacks.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 48 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 49 | #include "thread-current-inl.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 50 | |
| 51 | namespace openjdkjvmti { |
| 52 | |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 53 | class JvmtiFieldReflectionSource : public art::ReflectionSourceInfo { |
| 54 | public: |
| 55 | JvmtiFieldReflectionSource(bool is_access, art::ArtField* f) |
| 56 | : art::ReflectionSourceInfo(art::ReflectionSourceType::kSourceMiscInternal), |
| 57 | is_access_(is_access), |
| 58 | f_(f) {} |
| 59 | void Describe(std::ostream& os) const override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 60 | art::ReflectionSourceInfo::Describe(os); |
| 61 | os << " jvmti Field" << (is_access_ ? "Access" : "Modification") |
| 62 | << "Watch Target=" << f_->PrettyField(); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | bool is_access_; |
| 67 | art::ArtField* f_; |
| 68 | }; |
| 69 | struct FieldReflectiveValueCallback : public art::ReflectiveValueVisitCallback { |
| 70 | public: |
| 71 | void VisitReflectiveTargets(art::ReflectiveValueVisitor* visitor) |
| 72 | REQUIRES(art::Locks::mutator_lock_) { |
| 73 | art::Thread* self = art::Thread::Current(); |
| 74 | event_handler->ForEachEnv(self, [&](ArtJvmTiEnv* env) NO_THREAD_SAFETY_ANALYSIS { |
| 75 | art::Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 76 | art::WriterMutexLock mu(self, env->event_info_mutex_); |
| 77 | std::vector<std::pair<art::ArtField*, art::ArtField*>> updated_access_fields; |
| 78 | for (auto it : env->access_watched_fields) { |
| 79 | art::ArtField* af = |
| 80 | visitor->VisitField(it, JvmtiFieldReflectionSource(/*is_access=*/true, it)); |
| 81 | if (af != it) { |
| 82 | updated_access_fields.push_back({ af, it }); |
| 83 | } |
| 84 | } |
| 85 | for (auto it : updated_access_fields) { |
| 86 | DCHECK(env->access_watched_fields.find(it.second) != env->access_watched_fields.end()); |
| 87 | env->access_watched_fields.erase(it.second); |
| 88 | env->access_watched_fields.insert(it.first); |
| 89 | } |
| 90 | std::vector<std::pair<art::ArtField*, art::ArtField*>> updated_modify_fields; |
| 91 | for (auto it : env->modify_watched_fields) { |
| 92 | art::ArtField* af = |
| 93 | visitor->VisitField(it, JvmtiFieldReflectionSource(/*is_access=*/false, it)); |
| 94 | if (af != it) { |
| 95 | updated_modify_fields.push_back({ af, it }); |
| 96 | } |
| 97 | } |
| 98 | for (auto it : updated_modify_fields) { |
| 99 | DCHECK(env->modify_watched_fields.find(it.second) != env->modify_watched_fields.end()); |
| 100 | env->modify_watched_fields.erase(it.second); |
| 101 | env->modify_watched_fields.insert(it.first); |
| 102 | } |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | EventHandler* event_handler = nullptr; |
| 107 | }; |
| 108 | |
| 109 | static FieldReflectiveValueCallback gReflectiveValueCallback; |
| 110 | |
| 111 | void FieldUtil::Register(EventHandler* eh) { |
| 112 | gReflectiveValueCallback.event_handler = eh; |
| 113 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 114 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 115 | art::ScopedSuspendAll ssa("Add reflective value visit callback"); |
| 116 | art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks(); |
| 117 | callbacks->AddReflectiveValueVisitCallback(&gReflectiveValueCallback); |
| 118 | } |
| 119 | |
| 120 | void FieldUtil::Unregister() { |
| 121 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 122 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 123 | art::ScopedSuspendAll ssa("Remove reflective value visit callback"); |
| 124 | art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks(); |
| 125 | callbacks->RemoveReflectiveValueVisitCallback(&gReflectiveValueCallback); |
| 126 | } |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 127 | // Note: For all these functions, we could do a check that the field actually belongs to the given |
| 128 | // class. But the spec seems to assume a certain encoding of the field ID, and so doesn't |
| 129 | // specify any errors. |
| 130 | |
| 131 | jvmtiError FieldUtil::GetFieldName(jvmtiEnv* env, |
| 132 | jclass klass, |
| 133 | jfieldID field, |
| 134 | char** name_ptr, |
| 135 | char** signature_ptr, |
| 136 | char** generic_ptr) { |
| 137 | if (klass == nullptr) { |
| 138 | return ERR(INVALID_CLASS); |
| 139 | } |
| 140 | if (field == nullptr) { |
| 141 | return ERR(INVALID_FIELDID); |
| 142 | } |
| 143 | |
| 144 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 145 | art::ArtField* art_field = art::jni::DecodeArtField(field); |
| 146 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 147 | JvmtiUniquePtr<char[]> name_copy; |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 148 | if (name_ptr != nullptr) { |
| 149 | const char* field_name = art_field->GetName(); |
| 150 | if (field_name == nullptr) { |
| 151 | field_name = "<error>"; |
| 152 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 153 | jvmtiError ret; |
| 154 | name_copy = CopyString(env, field_name, &ret); |
| 155 | if (name_copy == nullptr) { |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 156 | return ret; |
| 157 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 158 | *name_ptr = name_copy.get(); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 161 | JvmtiUniquePtr<char[]> signature_copy; |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 162 | if (signature_ptr != nullptr) { |
| 163 | const char* sig = art_field->GetTypeDescriptor(); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 164 | jvmtiError ret; |
| 165 | signature_copy = CopyString(env, sig, &ret); |
| 166 | if (signature_copy == nullptr) { |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 167 | return ret; |
| 168 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 169 | *signature_ptr = signature_copy.get(); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 172 | if (generic_ptr != nullptr) { |
| 173 | *generic_ptr = nullptr; |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 174 | if (!art_field->GetDeclaringClass()->IsProxyClass()) { |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 175 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::String>> str_array = |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 176 | art::annotations::GetSignatureAnnotationForField(art_field); |
| 177 | if (str_array != nullptr) { |
| 178 | std::ostringstream oss; |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 179 | for (auto str : str_array->Iterate()) { |
| 180 | oss << str->ToModifiedUtf8(); |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 181 | } |
| 182 | std::string output_string = oss.str(); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 183 | jvmtiError ret; |
| 184 | JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret); |
| 185 | if (copy == nullptr) { |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 186 | return ret; |
| 187 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 188 | *generic_ptr = copy.release(); |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 189 | } else if (soa.Self()->IsExceptionPending()) { |
| 190 | // TODO: Should we report an error here? |
| 191 | soa.Self()->ClearException(); |
| 192 | } |
| 193 | } |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Everything is fine, release the buffers. |
| 197 | name_copy.release(); |
| 198 | signature_copy.release(); |
| 199 | |
| 200 | return ERR(NONE); |
| 201 | } |
| 202 | |
| 203 | jvmtiError FieldUtil::GetFieldDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 204 | jclass klass, |
| 205 | jfieldID field, |
| 206 | jclass* declaring_class_ptr) { |
| 207 | if (klass == nullptr) { |
| 208 | return ERR(INVALID_CLASS); |
| 209 | } |
| 210 | if (field == nullptr) { |
| 211 | return ERR(INVALID_FIELDID); |
| 212 | } |
| 213 | if (declaring_class_ptr == nullptr) { |
| 214 | return ERR(NULL_POINTER); |
| 215 | } |
| 216 | |
| 217 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 218 | art::ArtField* art_field = art::jni::DecodeArtField(field); |
| 219 | art::ObjPtr<art::mirror::Class> field_klass = art_field->GetDeclaringClass(); |
| 220 | |
| 221 | *declaring_class_ptr = soa.AddLocalReference<jclass>(field_klass); |
| 222 | |
| 223 | return ERR(NONE); |
| 224 | } |
| 225 | |
| 226 | jvmtiError FieldUtil::GetFieldModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 227 | jclass klass, |
| 228 | jfieldID field, |
| 229 | jint* modifiers_ptr) { |
| 230 | if (klass == nullptr) { |
| 231 | return ERR(INVALID_CLASS); |
| 232 | } |
| 233 | if (field == nullptr) { |
| 234 | return ERR(INVALID_FIELDID); |
| 235 | } |
| 236 | if (modifiers_ptr == nullptr) { |
| 237 | return ERR(NULL_POINTER); |
| 238 | } |
| 239 | |
| 240 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 241 | art::ArtField* art_field = art::jni::DecodeArtField(field); |
| 242 | // Note: Keep this code in sync with Field.getModifiers. |
| 243 | uint32_t modifiers = art_field->GetAccessFlags() & 0xFFFF; |
| 244 | |
| 245 | *modifiers_ptr = modifiers; |
| 246 | return ERR(NONE); |
| 247 | } |
| 248 | |
| 249 | jvmtiError FieldUtil::IsFieldSynthetic(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 250 | jclass klass, |
| 251 | jfieldID field, |
| 252 | jboolean* is_synthetic_ptr) { |
| 253 | if (klass == nullptr) { |
| 254 | return ERR(INVALID_CLASS); |
| 255 | } |
| 256 | if (field == nullptr) { |
| 257 | return ERR(INVALID_FIELDID); |
| 258 | } |
| 259 | if (is_synthetic_ptr == nullptr) { |
| 260 | return ERR(NULL_POINTER); |
| 261 | } |
| 262 | |
| 263 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 264 | art::ArtField* art_field = art::jni::DecodeArtField(field); |
| 265 | uint32_t modifiers = art_field->GetAccessFlags(); |
| 266 | |
| 267 | *is_synthetic_ptr = ((modifiers & art::kAccSynthetic) != 0) ? JNI_TRUE : JNI_FALSE; |
| 268 | return ERR(NONE); |
| 269 | } |
| 270 | |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 271 | jvmtiError FieldUtil::SetFieldModificationWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) { |
| 272 | ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv); |
Alex Light | b6106d5 | 2017-10-18 15:02:15 -0700 | [diff] [blame] | 273 | art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 274 | if (klass == nullptr) { |
| 275 | return ERR(INVALID_CLASS); |
| 276 | } |
| 277 | if (field == nullptr) { |
| 278 | return ERR(INVALID_FIELDID); |
| 279 | } |
| 280 | auto res_pair = env->modify_watched_fields.insert(art::jni::DecodeArtField(field)); |
| 281 | if (!res_pair.second) { |
| 282 | // Didn't get inserted because it's already present! |
| 283 | return ERR(DUPLICATE); |
| 284 | } |
| 285 | return OK; |
| 286 | } |
| 287 | |
| 288 | jvmtiError FieldUtil::ClearFieldModificationWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) { |
| 289 | ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv); |
Alex Light | b6106d5 | 2017-10-18 15:02:15 -0700 | [diff] [blame] | 290 | art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 291 | if (klass == nullptr) { |
| 292 | return ERR(INVALID_CLASS); |
| 293 | } |
| 294 | if (field == nullptr) { |
| 295 | return ERR(INVALID_FIELDID); |
| 296 | } |
| 297 | auto pos = env->modify_watched_fields.find(art::jni::DecodeArtField(field)); |
| 298 | if (pos == env->modify_watched_fields.end()) { |
| 299 | return ERR(NOT_FOUND); |
| 300 | } |
| 301 | env->modify_watched_fields.erase(pos); |
| 302 | return OK; |
| 303 | } |
| 304 | |
| 305 | jvmtiError FieldUtil::SetFieldAccessWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) { |
| 306 | ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv); |
Alex Light | b6106d5 | 2017-10-18 15:02:15 -0700 | [diff] [blame] | 307 | art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 308 | if (klass == nullptr) { |
| 309 | return ERR(INVALID_CLASS); |
| 310 | } |
| 311 | if (field == nullptr) { |
| 312 | return ERR(INVALID_FIELDID); |
| 313 | } |
| 314 | auto res_pair = env->access_watched_fields.insert(art::jni::DecodeArtField(field)); |
| 315 | if (!res_pair.second) { |
| 316 | // Didn't get inserted because it's already present! |
| 317 | return ERR(DUPLICATE); |
| 318 | } |
| 319 | return OK; |
| 320 | } |
| 321 | |
| 322 | jvmtiError FieldUtil::ClearFieldAccessWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) { |
| 323 | ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv); |
Alex Light | b6106d5 | 2017-10-18 15:02:15 -0700 | [diff] [blame] | 324 | art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 325 | if (klass == nullptr) { |
| 326 | return ERR(INVALID_CLASS); |
| 327 | } |
| 328 | if (field == nullptr) { |
| 329 | return ERR(INVALID_FIELDID); |
| 330 | } |
| 331 | auto pos = env->access_watched_fields.find(art::jni::DecodeArtField(field)); |
| 332 | if (pos == env->access_watched_fields.end()) { |
| 333 | return ERR(NOT_FOUND); |
| 334 | } |
| 335 | env->access_watched_fields.erase(pos); |
| 336 | return OK; |
| 337 | } |
| 338 | |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 339 | } // namespace openjdkjvmti |