blob: e5b96374b034733b2cfbf57be858b8fa2ee0ef8d [file] [log] [blame]
Andreas Gampeab2f0d02017-01-05 17:23:45 -08001/* 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 Lightc18eba32019-09-24 14:36:27 -070033#include <unordered_map>
Andreas Gampeab2f0d02017-01-05 17:23:45 -080034
Alex Lightc18eba32019-09-24 14:36:27 -070035#include "android-base/thread_annotations.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080036#include "art_field-inl.h"
Alex Lightc18eba32019-09-24 14:36:27 -070037#include "art_field.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include "art_jvmti.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080039#include "base/enums.h"
Alex Lightc18eba32019-09-24 14:36:27 -070040#include "base/locks.h"
David Sehr9e734c72018-01-04 17:56:19 -080041#include "dex/dex_file_annotations.h"
David Sehr8c0961f2018-01-23 16:11:38 -080042#include "dex/modifiers.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010043#include "jni/jni_internal.h"
Andreas Gampe188abab2017-02-16 10:34:05 -080044#include "mirror/object_array-inl.h"
Alex Lightc18eba32019-09-24 14:36:27 -070045#include "reflective_value_visitor.h"
46#include "runtime.h"
47#include "runtime_callbacks.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080048#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070049#include "thread-current-inl.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080050
51namespace openjdkjvmti {
52
Alex Lightc18eba32019-09-24 14:36:27 -070053class 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};
69struct 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
109static FieldReflectiveValueCallback gReflectiveValueCallback;
110
111void 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
120void 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 Gampeab2f0d02017-01-05 17:23:45 -0800127// 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
131jvmtiError 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 Gampe54711412017-02-21 12:41:43 -0800147 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800148 if (name_ptr != nullptr) {
149 const char* field_name = art_field->GetName();
150 if (field_name == nullptr) {
151 field_name = "<error>";
152 }
Andreas Gampe54711412017-02-21 12:41:43 -0800153 jvmtiError ret;
154 name_copy = CopyString(env, field_name, &ret);
155 if (name_copy == nullptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800156 return ret;
157 }
Andreas Gampe54711412017-02-21 12:41:43 -0800158 *name_ptr = name_copy.get();
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800159 }
160
Andreas Gampe54711412017-02-21 12:41:43 -0800161 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800162 if (signature_ptr != nullptr) {
163 const char* sig = art_field->GetTypeDescriptor();
Andreas Gampe54711412017-02-21 12:41:43 -0800164 jvmtiError ret;
165 signature_copy = CopyString(env, sig, &ret);
166 if (signature_copy == nullptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800167 return ret;
168 }
Andreas Gampe54711412017-02-21 12:41:43 -0800169 *signature_ptr = signature_copy.get();
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800170 }
171
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800172 if (generic_ptr != nullptr) {
173 *generic_ptr = nullptr;
Andreas Gampe188abab2017-02-16 10:34:05 -0800174 if (!art_field->GetDeclaringClass()->IsProxyClass()) {
Vladimir Markoacb906d2018-05-30 10:23:49 +0100175 art::ObjPtr<art::mirror::ObjectArray<art::mirror::String>> str_array =
Andreas Gampe188abab2017-02-16 10:34:05 -0800176 art::annotations::GetSignatureAnnotationForField(art_field);
177 if (str_array != nullptr) {
178 std::ostringstream oss;
179 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
180 oss << str_array->Get(i)->ToModifiedUtf8();
181 }
182 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800183 jvmtiError ret;
184 JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret);
185 if (copy == nullptr) {
Andreas Gampe188abab2017-02-16 10:34:05 -0800186 return ret;
187 }
Andreas Gampe54711412017-02-21 12:41:43 -0800188 *generic_ptr = copy.release();
Andreas Gampe188abab2017-02-16 10:34:05 -0800189 } else if (soa.Self()->IsExceptionPending()) {
190 // TODO: Should we report an error here?
191 soa.Self()->ClearException();
192 }
193 }
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800194 }
195
196 // Everything is fine, release the buffers.
197 name_copy.release();
198 signature_copy.release();
199
200 return ERR(NONE);
201}
202
203jvmtiError 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
226jvmtiError 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
249jvmtiError 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 Light084fa372017-06-16 08:58:34 -0700271jvmtiError FieldUtil::SetFieldModificationWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) {
272 ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
Alex Lightb6106d52017-10-18 15:02:15 -0700273 art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
Alex Light084fa372017-06-16 08:58:34 -0700274 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
288jvmtiError FieldUtil::ClearFieldModificationWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) {
289 ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
Alex Lightb6106d52017-10-18 15:02:15 -0700290 art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
Alex Light084fa372017-06-16 08:58:34 -0700291 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
305jvmtiError FieldUtil::SetFieldAccessWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) {
306 ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
Alex Lightb6106d52017-10-18 15:02:15 -0700307 art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
Alex Light084fa372017-06-16 08:58:34 -0700308 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
322jvmtiError FieldUtil::ClearFieldAccessWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) {
323 ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
Alex Lightb6106d52017-10-18 15:02:15 -0700324 art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
Alex Light084fa372017-06-16 08:58:34 -0700325 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 Gampeab2f0d02017-01-05 17:23:45 -0800339} // namespace openjdkjvmti