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"
33 #include <unordered_map>
34
35 #include "android-base/thread_annotations.h"
36 #include "art_field-inl.h"
37 #include "art_field.h"
38 #include "art_jvmti.h"
39 #include "base/enums.h"
40 #include "base/locks.h"
41 #include "dex/dex_file_annotations.h"
42 #include "dex/modifiers.h"
43 #include "jni/jni_internal.h"
44 #include "mirror/object_array-inl.h"
45 #include "reflective_value_visitor.h"
46 #include "runtime.h"
47 #include "runtime_callbacks.h"
48 #include "scoped_thread_state_change-inl.h"
49 #include "thread-current-inl.h"
50
51 namespace openjdkjvmti {
52
53 class JvmtiFieldReflectionSource : public art::ReflectionSourceInfo {
54 public:
JvmtiFieldReflectionSource(bool is_access,art::ArtField * f)55 JvmtiFieldReflectionSource(bool is_access, art::ArtField* f)
56 : art::ReflectionSourceInfo(art::ReflectionSourceType::kSourceMiscInternal),
57 is_access_(is_access),
58 f_(f) {}
Describe(std::ostream & os) const59 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:
VisitReflectiveTargetsopenjdkjvmti::FieldReflectiveValueCallback71 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
Register(EventHandler * eh)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
Unregister()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 }
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
GetFieldName(jvmtiEnv * env,jclass klass,jfieldID field,char ** name_ptr,char ** signature_ptr,char ** generic_ptr)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
147 JvmtiUniquePtr<char[]> name_copy;
148 if (name_ptr != nullptr) {
149 const char* field_name = art_field->GetName();
150 if (field_name == nullptr) {
151 field_name = "<error>";
152 }
153 jvmtiError ret;
154 name_copy = CopyString(env, field_name, &ret);
155 if (name_copy == nullptr) {
156 return ret;
157 }
158 *name_ptr = name_copy.get();
159 }
160
161 JvmtiUniquePtr<char[]> signature_copy;
162 if (signature_ptr != nullptr) {
163 const char* sig = art_field->GetTypeDescriptor();
164 jvmtiError ret;
165 signature_copy = CopyString(env, sig, &ret);
166 if (signature_copy == nullptr) {
167 return ret;
168 }
169 *signature_ptr = signature_copy.get();
170 }
171
172 if (generic_ptr != nullptr) {
173 *generic_ptr = nullptr;
174 if (!art_field->GetDeclaringClass()->IsProxyClass()) {
175 art::ObjPtr<art::mirror::ObjectArray<art::mirror::String>> str_array =
176 art::annotations::GetSignatureAnnotationForField(art_field);
177 if (str_array != nullptr) {
178 std::ostringstream oss;
179 for (auto str : str_array->Iterate()) {
180 oss << str->ToModifiedUtf8();
181 }
182 std::string output_string = oss.str();
183 jvmtiError ret;
184 JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret);
185 if (copy == nullptr) {
186 return ret;
187 }
188 *generic_ptr = copy.release();
189 } else if (soa.Self()->IsExceptionPending()) {
190 // TODO: Should we report an error here?
191 soa.Self()->ClearException();
192 }
193 }
194 }
195
196 // Everything is fine, release the buffers.
197 name_copy.release();
198 signature_copy.release();
199
200 return ERR(NONE);
201 }
202
GetFieldDeclaringClass(jvmtiEnv * env ATTRIBUTE_UNUSED,jclass klass,jfieldID field,jclass * declaring_class_ptr)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
GetFieldModifiers(jvmtiEnv * env ATTRIBUTE_UNUSED,jclass klass,jfieldID field,jint * modifiers_ptr)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
IsFieldSynthetic(jvmtiEnv * env ATTRIBUTE_UNUSED,jclass klass,jfieldID field,jboolean * is_synthetic_ptr)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
SetFieldModificationWatch(jvmtiEnv * jenv,jclass klass,jfieldID field)271 jvmtiError FieldUtil::SetFieldModificationWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) {
272 ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
273 art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
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
ClearFieldModificationWatch(jvmtiEnv * jenv,jclass klass,jfieldID field)288 jvmtiError FieldUtil::ClearFieldModificationWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) {
289 ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
290 art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
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
SetFieldAccessWatch(jvmtiEnv * jenv,jclass klass,jfieldID field)305 jvmtiError FieldUtil::SetFieldAccessWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) {
306 ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
307 art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
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
ClearFieldAccessWatch(jvmtiEnv * jenv,jclass klass,jfieldID field)322 jvmtiError FieldUtil::ClearFieldAccessWatch(jvmtiEnv* jenv, jclass klass, jfieldID field) {
323 ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
324 art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
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
339 } // namespace openjdkjvmti
340