1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ti_heap.h"
18 
19 #include <ios>
20 #include <unordered_map>
21 
22 #include "android-base/logging.h"
23 #include "android-base/thread_annotations.h"
24 #include "arch/context.h"
25 #include "art_field-inl.h"
26 #include "art_jvmti.h"
27 #include "base/logging.h"
28 #include "base/macros.h"
29 #include "base/mutex.h"
30 #include "base/utils.h"
31 #include "class_linker.h"
32 #include "deopt_manager.h"
33 #include "dex/primitive.h"
34 #include "events-inl.h"
35 #include "gc/collector_type.h"
36 #include "gc/gc_cause.h"
37 #include "gc/heap-visit-objects-inl.h"
38 #include "gc/heap-inl.h"
39 #include "gc/scoped_gc_critical_section.h"
40 #include "gc_root-inl.h"
41 #include "handle.h"
42 #include "handle_scope.h"
43 #include "java_frame_root_info.h"
44 #include "jni/jni_env_ext.h"
45 #include "jni/jni_id_manager.h"
46 #include "jni/jni_internal.h"
47 #include "jvmti_weak_table-inl.h"
48 #include "mirror/array-inl.h"
49 #include "mirror/array.h"
50 #include "mirror/class.h"
51 #include "mirror/object-inl.h"
52 #include "mirror/object-refvisitor-inl.h"
53 #include "mirror/object_array-inl.h"
54 #include "mirror/object_array-alloc-inl.h"
55 #include "mirror/object_reference.h"
56 #include "obj_ptr-inl.h"
57 #include "object_callbacks.h"
58 #include "object_tagging.h"
59 #include "offsets.h"
60 #include "read_barrier.h"
61 #include "runtime.h"
62 #include "scoped_thread_state_change-inl.h"
63 #include "stack.h"
64 #include "thread-inl.h"
65 #include "thread_list.h"
66 #include "ti_logging.h"
67 #include "ti_stack.h"
68 #include "ti_thread.h"
69 #include "well_known_classes.h"
70 
71 namespace openjdkjvmti {
72 
73 EventHandler* HeapExtensions::gEventHandler = nullptr;
74 
75 namespace {
76 
77 struct IndexCache {
78   // The number of interface fields implemented by the class. This is a prefix to all assigned
79   // field indices.
80   size_t interface_fields;
81 
82   // It would be nice to also cache the following, but it is complicated to wire up into the
83   // generic visit:
84   // The number of fields in interfaces and superclasses. This is the first index assigned to
85   // fields of the class.
86   // size_t superclass_fields;
87 };
88 using IndexCachingTable = JvmtiWeakTable<IndexCache>;
89 
90 static IndexCachingTable gIndexCachingTable;
91 
92 // Report the contents of a string, if a callback is set.
ReportString(art::ObjPtr<art::mirror::Object> obj,jvmtiEnv * env,ObjectTagTable * tag_table,const jvmtiHeapCallbacks * cb,const void * user_data)93 jint ReportString(art::ObjPtr<art::mirror::Object> obj,
94                   jvmtiEnv* env,
95                   ObjectTagTable* tag_table,
96                   const jvmtiHeapCallbacks* cb,
97                   const void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) {
98   if (UNLIKELY(cb->string_primitive_value_callback != nullptr) && obj->IsString()) {
99     art::ObjPtr<art::mirror::String> str = obj->AsString();
100     int32_t string_length = str->GetLength();
101     JvmtiUniquePtr<uint16_t[]> data;
102 
103     if (string_length > 0) {
104       jvmtiError alloc_error;
105       data = AllocJvmtiUniquePtr<uint16_t[]>(env, string_length, &alloc_error);
106       if (data == nullptr) {
107         // TODO: Not really sure what to do here. Should we abort the iteration and go all the way
108         //       back? For now just warn.
109         LOG(WARNING) << "Unable to allocate buffer for string reporting! Silently dropping value."
110                      << " >" << str->ToModifiedUtf8() << "<";
111         return 0;
112       }
113 
114       if (str->IsCompressed()) {
115         uint8_t* compressed_data = str->GetValueCompressed();
116         for (int32_t i = 0; i != string_length; ++i) {
117           data[i] = compressed_data[i];
118         }
119       } else {
120         // Can copy directly.
121         memcpy(data.get(), str->GetValue(), string_length * sizeof(uint16_t));
122       }
123     }
124 
125     const jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
126     jlong string_tag = tag_table->GetTagOrZero(obj.Ptr());
127     const jlong saved_string_tag = string_tag;
128 
129     jint result = cb->string_primitive_value_callback(class_tag,
130                                                       obj->SizeOf(),
131                                                       &string_tag,
132                                                       data.get(),
133                                                       string_length,
134                                                       const_cast<void*>(user_data));
135     if (string_tag != saved_string_tag) {
136       tag_table->Set(obj.Ptr(), string_tag);
137     }
138 
139     return result;
140   }
141   return 0;
142 }
143 
144 // Report the contents of a primitive array, if a callback is set.
ReportPrimitiveArray(art::ObjPtr<art::mirror::Object> obj,jvmtiEnv * env,ObjectTagTable * tag_table,const jvmtiHeapCallbacks * cb,const void * user_data)145 jint ReportPrimitiveArray(art::ObjPtr<art::mirror::Object> obj,
146                           jvmtiEnv* env,
147                           ObjectTagTable* tag_table,
148                           const jvmtiHeapCallbacks* cb,
149                           const void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) {
150   if (UNLIKELY(cb->array_primitive_value_callback != nullptr) &&
151       obj->IsArrayInstance() &&
152       !obj->IsObjectArray()) {
153     art::ObjPtr<art::mirror::Array> array = obj->AsArray();
154     int32_t array_length = array->GetLength();
155     size_t component_size = array->GetClass()->GetComponentSize();
156     art::Primitive::Type art_prim_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
157     jvmtiPrimitiveType prim_type =
158         static_cast<jvmtiPrimitiveType>(art::Primitive::Descriptor(art_prim_type)[0]);
159     DCHECK(prim_type == JVMTI_PRIMITIVE_TYPE_BOOLEAN ||
160            prim_type == JVMTI_PRIMITIVE_TYPE_BYTE ||
161            prim_type == JVMTI_PRIMITIVE_TYPE_CHAR ||
162            prim_type == JVMTI_PRIMITIVE_TYPE_SHORT ||
163            prim_type == JVMTI_PRIMITIVE_TYPE_INT ||
164            prim_type == JVMTI_PRIMITIVE_TYPE_LONG ||
165            prim_type == JVMTI_PRIMITIVE_TYPE_FLOAT ||
166            prim_type == JVMTI_PRIMITIVE_TYPE_DOUBLE);
167 
168     const jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
169     jlong array_tag = tag_table->GetTagOrZero(obj.Ptr());
170     const jlong saved_array_tag = array_tag;
171 
172     jint result;
173     if (array_length == 0) {
174       result = cb->array_primitive_value_callback(class_tag,
175                                                   obj->SizeOf(),
176                                                   &array_tag,
177                                                   0,
178                                                   prim_type,
179                                                   nullptr,
180                                                   const_cast<void*>(user_data));
181     } else {
182       jvmtiError alloc_error;
183       JvmtiUniquePtr<char[]> data = AllocJvmtiUniquePtr<char[]>(env,
184                                                                 array_length * component_size,
185                                                                 &alloc_error);
186       if (data == nullptr) {
187         // TODO: Not really sure what to do here. Should we abort the iteration and go all the way
188         //       back? For now just warn.
189         LOG(WARNING) << "Unable to allocate buffer for array reporting! Silently dropping value.";
190         return 0;
191       }
192 
193       memcpy(data.get(), array->GetRawData(component_size, 0), array_length * component_size);
194 
195       result = cb->array_primitive_value_callback(class_tag,
196                                                   obj->SizeOf(),
197                                                   &array_tag,
198                                                   array_length,
199                                                   prim_type,
200                                                   data.get(),
201                                                   const_cast<void*>(user_data));
202     }
203 
204     if (array_tag != saved_array_tag) {
205       tag_table->Set(obj.Ptr(), array_tag);
206     }
207 
208     return result;
209   }
210   return 0;
211 }
212 
213 template <typename UserData>
VisitorFalse(art::ObjPtr<art::mirror::Object> obj,art::ObjPtr<art::mirror::Class> klass,art::ArtField & field,size_t field_index,UserData * user_data)214 bool VisitorFalse([[maybe_unused]] art::ObjPtr<art::mirror::Object> obj,
215                   [[maybe_unused]] art::ObjPtr<art::mirror::Class> klass,
216                   [[maybe_unused]] art::ArtField& field,
217                   [[maybe_unused]] size_t field_index,
218                   [[maybe_unused]] UserData* user_data) {
219   return false;
220 }
221 
222 template <typename UserData, bool kCallVisitorOnRecursion>
223 class FieldVisitor {
224  public:
225   // Report the contents of a primitive fields of the given object, if a callback is set.
226   template <typename StaticPrimitiveVisitor,
227             typename StaticReferenceVisitor,
228             typename InstancePrimitiveVisitor,
229             typename InstanceReferenceVisitor>
ReportFields(art::ObjPtr<art::mirror::Object> obj,UserData * user_data,StaticPrimitiveVisitor & static_prim_visitor,StaticReferenceVisitor & static_ref_visitor,InstancePrimitiveVisitor & instance_prim_visitor,InstanceReferenceVisitor & instance_ref_visitor)230   static bool ReportFields(art::ObjPtr<art::mirror::Object> obj,
231                            UserData* user_data,
232                            StaticPrimitiveVisitor& static_prim_visitor,
233                            StaticReferenceVisitor& static_ref_visitor,
234                            InstancePrimitiveVisitor& instance_prim_visitor,
235                            InstanceReferenceVisitor& instance_ref_visitor)
236       REQUIRES_SHARED(art::Locks::mutator_lock_) {
237     FieldVisitor fv(user_data);
238 
239     if (obj->IsClass()) {
240       // When visiting a class, we only visit the static fields of the given class. No field of
241       // superclasses is visited.
242       art::ObjPtr<art::mirror::Class> klass = obj->AsClass();
243       // Only report fields on resolved classes. We need valid field data.
244       if (!klass->IsResolved()) {
245         return false;
246       }
247       return fv.ReportFieldsImpl(nullptr,
248                                  obj->AsClass(),
249                                  obj->AsClass()->IsInterface(),
250                                  static_prim_visitor,
251                                  static_ref_visitor,
252                                  instance_prim_visitor,
253                                  instance_ref_visitor);
254     } else {
255       // See comment above. Just double-checking here, but an instance *should* mean the class was
256       // resolved.
257       DCHECK(obj->GetClass()->IsResolved() || obj->GetClass()->IsErroneousResolved());
258       return fv.ReportFieldsImpl(obj,
259                                  obj->GetClass(),
260                                  false,
261                                  static_prim_visitor,
262                                  static_ref_visitor,
263                                  instance_prim_visitor,
264                                  instance_ref_visitor);
265     }
266   }
267 
268  private:
FieldVisitor(UserData * user_data)269   explicit FieldVisitor(UserData* user_data) : user_data_(user_data) {}
270 
271   // Report the contents of fields of the given object. If obj is null, report the static fields,
272   // otherwise the instance fields.
273   template <typename StaticPrimitiveVisitor,
274             typename StaticReferenceVisitor,
275             typename InstancePrimitiveVisitor,
276             typename InstanceReferenceVisitor>
ReportFieldsImpl(art::ObjPtr<art::mirror::Object> obj,art::ObjPtr<art::mirror::Class> klass,bool skip_java_lang_object,StaticPrimitiveVisitor & static_prim_visitor,StaticReferenceVisitor & static_ref_visitor,InstancePrimitiveVisitor & instance_prim_visitor,InstanceReferenceVisitor & instance_ref_visitor)277   bool ReportFieldsImpl(art::ObjPtr<art::mirror::Object> obj,
278                         art::ObjPtr<art::mirror::Class> klass,
279                         bool skip_java_lang_object,
280                         StaticPrimitiveVisitor& static_prim_visitor,
281                         StaticReferenceVisitor& static_ref_visitor,
282                         InstancePrimitiveVisitor& instance_prim_visitor,
283                         InstanceReferenceVisitor& instance_ref_visitor)
284       REQUIRES_SHARED(art::Locks::mutator_lock_) {
285     // Compute the offset of field indices.
286     size_t interface_field_count = CountInterfaceFields(klass);
287 
288     size_t tmp;
289     bool aborted = ReportFieldsRecursive(obj,
290                                          klass,
291                                          interface_field_count,
292                                          skip_java_lang_object,
293                                          static_prim_visitor,
294                                          static_ref_visitor,
295                                          instance_prim_visitor,
296                                          instance_ref_visitor,
297                                          &tmp);
298     return aborted;
299   }
300 
301   // Visit primitive fields in an object (instance). Return true if the visit was aborted.
302   template <typename StaticPrimitiveVisitor,
303             typename StaticReferenceVisitor,
304             typename InstancePrimitiveVisitor,
305             typename InstanceReferenceVisitor>
ReportFieldsRecursive(art::ObjPtr<art::mirror::Object> obj,art::ObjPtr<art::mirror::Class> klass,size_t interface_fields,bool skip_java_lang_object,StaticPrimitiveVisitor & static_prim_visitor,StaticReferenceVisitor & static_ref_visitor,InstancePrimitiveVisitor & instance_prim_visitor,InstanceReferenceVisitor & instance_ref_visitor,size_t * field_index_out)306   bool ReportFieldsRecursive(art::ObjPtr<art::mirror::Object> obj,
307                              art::ObjPtr<art::mirror::Class> klass,
308                              size_t interface_fields,
309                              bool skip_java_lang_object,
310                              StaticPrimitiveVisitor& static_prim_visitor,
311                              StaticReferenceVisitor& static_ref_visitor,
312                              InstancePrimitiveVisitor& instance_prim_visitor,
313                              InstanceReferenceVisitor& instance_ref_visitor,
314                              size_t* field_index_out)
315       REQUIRES_SHARED(art::Locks::mutator_lock_) {
316     DCHECK(klass != nullptr);
317     size_t field_index;
318     if (klass->GetSuperClass() == nullptr) {
319       // j.l.Object. Start with the fields from interfaces.
320       field_index = interface_fields;
321       if (skip_java_lang_object) {
322         *field_index_out = field_index;
323         return false;
324       }
325     } else {
326       // Report superclass fields.
327       if (kCallVisitorOnRecursion) {
328         if (ReportFieldsRecursive(obj,
329                                   klass->GetSuperClass(),
330                                   interface_fields,
331                                   skip_java_lang_object,
332                                   static_prim_visitor,
333                                   static_ref_visitor,
334                                   instance_prim_visitor,
335                                   instance_ref_visitor,
336                                   &field_index)) {
337           return true;
338         }
339       } else {
340         // Still call, but with empty visitor. This is required for correct counting.
341         ReportFieldsRecursive(obj,
342                               klass->GetSuperClass(),
343                               interface_fields,
344                               skip_java_lang_object,
345                               VisitorFalse<UserData>,
346                               VisitorFalse<UserData>,
347                               VisitorFalse<UserData>,
348                               VisitorFalse<UserData>,
349                               &field_index);
350       }
351     }
352 
353     // Now visit fields for the current klass.
354 
355     for (auto& static_field : klass->GetSFields()) {
356       if (static_field.IsPrimitiveType()) {
357         if (static_prim_visitor(obj,
358                                 klass,
359                                 static_field,
360                                 field_index,
361                                 user_data_)) {
362           return true;
363         }
364       } else {
365         if (static_ref_visitor(obj,
366                                klass,
367                                static_field,
368                                field_index,
369                                user_data_)) {
370           return true;
371         }
372       }
373       field_index++;
374     }
375 
376     for (auto& instance_field : klass->GetIFields()) {
377       if (instance_field.IsPrimitiveType()) {
378         if (instance_prim_visitor(obj,
379                                   klass,
380                                   instance_field,
381                                   field_index,
382                                   user_data_)) {
383           return true;
384         }
385       } else {
386         if (instance_ref_visitor(obj,
387                                  klass,
388                                  instance_field,
389                                  field_index,
390                                  user_data_)) {
391           return true;
392         }
393       }
394       field_index++;
395     }
396 
397     *field_index_out = field_index;
398     return false;
399   }
400 
401   // Implements a visit of the implemented interfaces of a given class.
402   template <typename T>
403   struct RecursiveInterfaceVisit {
VisitStaticopenjdkjvmti::__anon859d03a70111::FieldVisitor::RecursiveInterfaceVisit404     static void VisitStatic(art::Thread* self, art::ObjPtr<art::mirror::Class> klass, T& visitor)
405         REQUIRES_SHARED(art::Locks::mutator_lock_) {
406       RecursiveInterfaceVisit rv;
407       rv.Visit(self, klass, visitor);
408     }
409 
Visitopenjdkjvmti::__anon859d03a70111::FieldVisitor::RecursiveInterfaceVisit410     void Visit(art::Thread* self, art::ObjPtr<art::mirror::Class> klass, T& visitor)
411         REQUIRES_SHARED(art::Locks::mutator_lock_) {
412       // First visit the parent, to get the order right.
413       // (We do this in preparation for actual visiting of interface fields.)
414       if (klass->GetSuperClass() != nullptr) {
415         Visit(self, klass->GetSuperClass(), visitor);
416       }
417       for (uint32_t i = 0; i != klass->NumDirectInterfaces(); ++i) {
418         art::ObjPtr<art::mirror::Class> inf_klass = klass->GetDirectInterface(i);
419         DCHECK(inf_klass != nullptr);
420         VisitInterface(self, inf_klass, visitor);
421       }
422     }
423 
VisitInterfaceopenjdkjvmti::__anon859d03a70111::FieldVisitor::RecursiveInterfaceVisit424     void VisitInterface(art::Thread* self, art::ObjPtr<art::mirror::Class> inf_klass, T& visitor)
425         REQUIRES_SHARED(art::Locks::mutator_lock_) {
426       auto it = visited_interfaces.find(inf_klass.Ptr());
427       if (it != visited_interfaces.end()) {
428         return;
429       }
430       visited_interfaces.insert(inf_klass.Ptr());
431 
432       // Let the visitor know about this one. Note that this order is acceptable, as the ordering
433       // of these fields never matters for known visitors.
434       visitor(inf_klass);
435 
436       // Now visit the superinterfaces.
437       for (uint32_t i = 0; i != inf_klass->NumDirectInterfaces(); ++i) {
438         art::ObjPtr<art::mirror::Class> super_inf_klass = inf_klass->GetDirectInterface(i);
439         DCHECK(super_inf_klass != nullptr);
440         VisitInterface(self, super_inf_klass, visitor);
441       }
442     }
443 
444     std::unordered_set<art::mirror::Class*> visited_interfaces;
445   };
446 
447   // Counting interface fields. Note that we cannot use the interface table, as that only contains
448   // "non-marker" interfaces (= interfaces with methods).
CountInterfaceFields(art::ObjPtr<art::mirror::Class> klass)449   static size_t CountInterfaceFields(art::ObjPtr<art::mirror::Class> klass)
450       REQUIRES_SHARED(art::Locks::mutator_lock_) {
451     // Do we have a cached value?
452     IndexCache tmp;
453     if (gIndexCachingTable.GetTag(klass.Ptr(), &tmp)) {
454       return tmp.interface_fields;
455     }
456 
457     size_t count = 0;
458     auto visitor = [&count](art::ObjPtr<art::mirror::Class> inf_klass)
459         REQUIRES_SHARED(art::Locks::mutator_lock_) {
460       DCHECK(inf_klass->IsInterface());
461       DCHECK_EQ(0u, inf_klass->NumInstanceFields());
462       count += inf_klass->NumStaticFields();
463     };
464     RecursiveInterfaceVisit<decltype(visitor)>::VisitStatic(art::Thread::Current(), klass, visitor);
465 
466     // Store this into the cache.
467     tmp.interface_fields = count;
468     gIndexCachingTable.Set(klass.Ptr(), tmp);
469 
470     return count;
471   }
472 
473   UserData* user_data_;
474 };
475 
476 // Debug helper. Prints the structure of an object.
477 template <bool kStatic, bool kRef>
478 struct DumpVisitor {
Callbackopenjdkjvmti::__anon859d03a70111::DumpVisitor479   static bool Callback([[maybe_unused]] art::ObjPtr<art::mirror::Object> obj,
480                        [[maybe_unused]] art::ObjPtr<art::mirror::Class> klass,
481                        art::ArtField& field,
482                        size_t field_index,
483                        [[maybe_unused]] void* user_data)
484       REQUIRES_SHARED(art::Locks::mutator_lock_) {
485     LOG(ERROR) << (kStatic ? "static " : "instance ")
486                << (kRef ? "ref " : "primitive ")
487                << field.PrettyField()
488                << " @ "
489                << field_index;
490     return false;
491   }
492 };
DumpObjectFields(art::ObjPtr<art::mirror::Object> obj)493 [[maybe_unused]] void DumpObjectFields(art::ObjPtr<art::mirror::Object> obj)
494     REQUIRES_SHARED(art::Locks::mutator_lock_) {
495   if (obj->IsClass()) {
496     FieldVisitor<void, false>:: ReportFields(obj,
497                                              nullptr,
498                                              DumpVisitor<true, false>::Callback,
499                                              DumpVisitor<true, true>::Callback,
500                                              DumpVisitor<false, false>::Callback,
501                                              DumpVisitor<false, true>::Callback);
502   } else {
503     FieldVisitor<void, true>::ReportFields(obj,
504                                            nullptr,
505                                            DumpVisitor<true, false>::Callback,
506                                            DumpVisitor<true, true>::Callback,
507                                            DumpVisitor<false, false>::Callback,
508                                            DumpVisitor<false, true>::Callback);
509   }
510 }
511 
512 class ReportPrimitiveField {
513  public:
Report(art::ObjPtr<art::mirror::Object> obj,ObjectTagTable * tag_table,const jvmtiHeapCallbacks * cb,const void * user_data)514   static bool Report(art::ObjPtr<art::mirror::Object> obj,
515                      ObjectTagTable* tag_table,
516                      const jvmtiHeapCallbacks* cb,
517                      const void* user_data)
518       REQUIRES_SHARED(art::Locks::mutator_lock_) {
519     if (UNLIKELY(cb->primitive_field_callback != nullptr)) {
520       jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
521       ReportPrimitiveField rpf(tag_table, class_tag, cb, user_data);
522       if (obj->IsClass()) {
523         return FieldVisitor<ReportPrimitiveField, false>::ReportFields(
524             obj,
525             &rpf,
526             ReportPrimitiveFieldCallback<true>,
527             VisitorFalse<ReportPrimitiveField>,
528             VisitorFalse<ReportPrimitiveField>,
529             VisitorFalse<ReportPrimitiveField>);
530       } else {
531         return FieldVisitor<ReportPrimitiveField, true>::ReportFields(
532             obj,
533             &rpf,
534             VisitorFalse<ReportPrimitiveField>,
535             VisitorFalse<ReportPrimitiveField>,
536             ReportPrimitiveFieldCallback<false>,
537             VisitorFalse<ReportPrimitiveField>);
538       }
539     }
540     return false;
541   }
542 
543 
544  private:
ReportPrimitiveField(ObjectTagTable * tag_table,jlong class_tag,const jvmtiHeapCallbacks * cb,const void * user_data)545   ReportPrimitiveField(ObjectTagTable* tag_table,
546                        jlong class_tag,
547                        const jvmtiHeapCallbacks* cb,
548                        const void* user_data)
549       : tag_table_(tag_table), class_tag_(class_tag), cb_(cb), user_data_(user_data) {}
550 
551   template <bool kReportStatic>
ReportPrimitiveFieldCallback(art::ObjPtr<art::mirror::Object> obj,art::ObjPtr<art::mirror::Class> klass,art::ArtField & field,size_t field_index,ReportPrimitiveField * user_data)552   static bool ReportPrimitiveFieldCallback(art::ObjPtr<art::mirror::Object> obj,
553                                            art::ObjPtr<art::mirror::Class> klass,
554                                            art::ArtField& field,
555                                            size_t field_index,
556                                            ReportPrimitiveField* user_data)
557       REQUIRES_SHARED(art::Locks::mutator_lock_) {
558     art::Primitive::Type art_prim_type = field.GetTypeAsPrimitiveType();
559     jvmtiPrimitiveType prim_type =
560         static_cast<jvmtiPrimitiveType>(art::Primitive::Descriptor(art_prim_type)[0]);
561     DCHECK(prim_type == JVMTI_PRIMITIVE_TYPE_BOOLEAN ||
562            prim_type == JVMTI_PRIMITIVE_TYPE_BYTE ||
563            prim_type == JVMTI_PRIMITIVE_TYPE_CHAR ||
564            prim_type == JVMTI_PRIMITIVE_TYPE_SHORT ||
565            prim_type == JVMTI_PRIMITIVE_TYPE_INT ||
566            prim_type == JVMTI_PRIMITIVE_TYPE_LONG ||
567            prim_type == JVMTI_PRIMITIVE_TYPE_FLOAT ||
568            prim_type == JVMTI_PRIMITIVE_TYPE_DOUBLE);
569     jvmtiHeapReferenceInfo info;
570     info.field.index = field_index;
571 
572     jvalue value;
573     memset(&value, 0, sizeof(jvalue));
574     art::ObjPtr<art::mirror::Object> src = kReportStatic ? klass : obj;
575     switch (art_prim_type) {
576       case art::Primitive::Type::kPrimBoolean:
577         value.z = field.GetBoolean(src) == 0 ? JNI_FALSE : JNI_TRUE;
578         break;
579       case art::Primitive::Type::kPrimByte:
580         value.b = field.GetByte(src);
581         break;
582       case art::Primitive::Type::kPrimChar:
583         value.c = field.GetChar(src);
584         break;
585       case art::Primitive::Type::kPrimShort:
586         value.s = field.GetShort(src);
587         break;
588       case art::Primitive::Type::kPrimInt:
589         value.i = field.GetInt(src);
590         break;
591       case art::Primitive::Type::kPrimLong:
592         value.j = field.GetLong(src);
593         break;
594       case art::Primitive::Type::kPrimFloat:
595         value.f = field.GetFloat(src);
596         break;
597       case art::Primitive::Type::kPrimDouble:
598         value.d = field.GetDouble(src);
599         break;
600       case art::Primitive::Type::kPrimVoid:
601       case art::Primitive::Type::kPrimNot: {
602         LOG(FATAL) << "Should not reach here";
603         UNREACHABLE();
604       }
605     }
606 
607     jlong obj_tag = user_data->tag_table_->GetTagOrZero(src.Ptr());
608     const jlong saved_obj_tag = obj_tag;
609 
610     jint ret = user_data->cb_->primitive_field_callback(kReportStatic
611                                                             ? JVMTI_HEAP_REFERENCE_STATIC_FIELD
612                                                             : JVMTI_HEAP_REFERENCE_FIELD,
613                                                         &info,
614                                                         user_data->class_tag_,
615                                                         &obj_tag,
616                                                         value,
617                                                         prim_type,
618                                                         const_cast<void*>(user_data->user_data_));
619 
620     if (saved_obj_tag != obj_tag) {
621       user_data->tag_table_->Set(src.Ptr(), obj_tag);
622     }
623 
624     if ((ret & JVMTI_VISIT_ABORT) != 0) {
625       return true;
626     }
627 
628     return false;
629   }
630 
631   ObjectTagTable* tag_table_;
632   jlong class_tag_;
633   const jvmtiHeapCallbacks* cb_;
634   const void* user_data_;
635 };
636 
637 struct HeapFilter {
HeapFilteropenjdkjvmti::__anon859d03a70111::HeapFilter638   explicit HeapFilter(jint heap_filter)
639       : filter_out_tagged((heap_filter & JVMTI_HEAP_FILTER_TAGGED) != 0),
640         filter_out_untagged((heap_filter & JVMTI_HEAP_FILTER_UNTAGGED) != 0),
641         filter_out_class_tagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_TAGGED) != 0),
642         filter_out_class_untagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) != 0),
643         any_filter(filter_out_tagged ||
644                    filter_out_untagged ||
645                    filter_out_class_tagged ||
646                    filter_out_class_untagged) {
647   }
648 
ShouldReportByHeapFilteropenjdkjvmti::__anon859d03a70111::HeapFilter649   bool ShouldReportByHeapFilter(jlong tag, jlong class_tag) const {
650     if (!any_filter) {
651       return true;
652     }
653 
654     if ((tag == 0 && filter_out_untagged) || (tag != 0 && filter_out_tagged)) {
655       return false;
656     }
657 
658     if ((class_tag == 0 && filter_out_class_untagged) ||
659         (class_tag != 0 && filter_out_class_tagged)) {
660       return false;
661     }
662 
663     return true;
664   }
665 
666   const bool filter_out_tagged;
667   const bool filter_out_untagged;
668   const bool filter_out_class_tagged;
669   const bool filter_out_class_untagged;
670   const bool any_filter;
671 };
672 
673 }  // namespace
674 
Register()675 void HeapUtil::Register() {
676   art::Runtime::Current()->AddSystemWeakHolder(&gIndexCachingTable);
677 }
678 
Unregister()679 void HeapUtil::Unregister() {
680   art::Runtime::Current()->RemoveSystemWeakHolder(&gIndexCachingTable);
681 }
682 
IterateOverInstancesOfClass(jvmtiEnv * env,jclass klass,jvmtiHeapObjectFilter filter,jvmtiHeapObjectCallback cb,const void * user_data)683 jvmtiError HeapUtil::IterateOverInstancesOfClass(jvmtiEnv* env,
684                                                  jclass klass,
685                                                  jvmtiHeapObjectFilter filter,
686                                                  jvmtiHeapObjectCallback cb,
687                                                  const void* user_data) {
688   if (cb == nullptr || klass == nullptr) {
689     return ERR(NULL_POINTER);
690   }
691 
692   art::Thread* self = art::Thread::Current();
693   art::ScopedObjectAccess soa(self);      // Now we know we have the shared lock.
694   art::StackHandleScope<1> hs(self);
695 
696   art::ObjPtr<art::mirror::Object> klass_ptr(soa.Decode<art::mirror::Class>(klass));
697   if (!klass_ptr->IsClass()) {
698     return ERR(INVALID_CLASS);
699   }
700   art::Handle<art::mirror::Class> filter_klass(hs.NewHandle(klass_ptr->AsClass()));
701   ObjectTagTable* tag_table = ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get();
702   bool stop_reports = false;
703   auto visitor = [&](art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
704     // Early return, as we can't really stop visiting.
705     if (stop_reports) {
706       return;
707     }
708 
709     art::ScopedAssertNoThreadSuspension no_suspension("IterateOverInstancesOfClass");
710 
711     art::ObjPtr<art::mirror::Class> klass = obj->GetClass();
712 
713     if (filter_klass != nullptr && !filter_klass->IsAssignableFrom(klass)) {
714       return;
715     }
716 
717     jlong tag = 0;
718     tag_table->GetTag(obj, &tag);
719     if ((filter != JVMTI_HEAP_OBJECT_EITHER) &&
720         ((tag == 0 && filter == JVMTI_HEAP_OBJECT_TAGGED) ||
721          (tag != 0 && filter == JVMTI_HEAP_OBJECT_UNTAGGED))) {
722       return;
723     }
724 
725     jlong class_tag = 0;
726     tag_table->GetTag(klass.Ptr(), &class_tag);
727 
728     jlong saved_tag = tag;
729     jint ret = cb(class_tag, obj->SizeOf(), &tag, const_cast<void*>(user_data));
730 
731     stop_reports = (ret == JVMTI_ITERATION_ABORT);
732 
733     if (tag != saved_tag) {
734       tag_table->Set(obj, tag);
735     }
736   };
737   art::Runtime::Current()->GetHeap()->VisitObjects(visitor);
738 
739   return OK;
740 }
741 
742 template <typename T>
DoIterateThroughHeap(T fn,jvmtiEnv * env,ObjectTagTable * tag_table,jint heap_filter_int,jclass klass,const jvmtiHeapCallbacks * callbacks,const void * user_data)743 static jvmtiError DoIterateThroughHeap(T fn,
744                                        jvmtiEnv* env,
745                                        ObjectTagTable* tag_table,
746                                        jint heap_filter_int,
747                                        jclass klass,
748                                        const jvmtiHeapCallbacks* callbacks,
749                                        const void* user_data) {
750   if (callbacks == nullptr) {
751     return ERR(NULL_POINTER);
752   }
753 
754   art::Thread* self = art::Thread::Current();
755   art::ScopedObjectAccess soa(self);      // Now we know we have the shared lock.
756 
757   bool stop_reports = false;
758   const HeapFilter heap_filter(heap_filter_int);
759   art::StackHandleScope<1> hs(self);
760   art::Handle<art::mirror::Class> filter_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
761   auto visitor = [&](art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
762     // Early return, as we can't really stop visiting.
763     if (stop_reports) {
764       return;
765     }
766 
767     art::ScopedAssertNoThreadSuspension no_suspension("IterateThroughHeapCallback");
768 
769     jlong tag = 0;
770     tag_table->GetTag(obj, &tag);
771 
772     jlong class_tag = 0;
773     art::ObjPtr<art::mirror::Class> klass = obj->GetClass();
774     tag_table->GetTag(klass.Ptr(), &class_tag);
775     // For simplicity, even if we find a tag = 0, assume 0 = not tagged.
776 
777     if (!heap_filter.ShouldReportByHeapFilter(tag, class_tag)) {
778       return;
779     }
780 
781     if (filter_klass != nullptr) {
782       if (filter_klass.Get() != klass) {
783         return;
784       }
785     }
786 
787     jlong size = obj->SizeOf();
788 
789     jint length = -1;
790     if (obj->IsArrayInstance()) {
791       length = obj->AsArray()->GetLength();
792     }
793 
794     jlong saved_tag = tag;
795     jint ret = fn(obj, callbacks, class_tag, size, &tag, length, const_cast<void*>(user_data));
796 
797     if (tag != saved_tag) {
798       tag_table->Set(obj, tag);
799     }
800 
801     stop_reports = (ret & JVMTI_VISIT_ABORT) != 0;
802 
803     if (!stop_reports) {
804       jint string_ret = ReportString(obj, env, tag_table, callbacks, user_data);
805       stop_reports = (string_ret & JVMTI_VISIT_ABORT) != 0;
806     }
807 
808     if (!stop_reports) {
809       jint array_ret = ReportPrimitiveArray(obj, env, tag_table, callbacks, user_data);
810       stop_reports = (array_ret & JVMTI_VISIT_ABORT) != 0;
811     }
812 
813     if (!stop_reports) {
814       stop_reports = ReportPrimitiveField::Report(obj, tag_table, callbacks, user_data);
815     }
816   };
817   art::Runtime::Current()->GetHeap()->VisitObjects(visitor);
818 
819   return ERR(NONE);
820 }
821 
IterateThroughHeap(jvmtiEnv * env,jint heap_filter,jclass klass,const jvmtiHeapCallbacks * callbacks,const void * user_data)822 jvmtiError HeapUtil::IterateThroughHeap(jvmtiEnv* env,
823                                         jint heap_filter,
824                                         jclass klass,
825                                         const jvmtiHeapCallbacks* callbacks,
826                                         const void* user_data) {
827   auto JvmtiIterateHeap = []([[maybe_unused]] art::mirror::Object* obj,
828                              const jvmtiHeapCallbacks* cb_callbacks,
829                              jlong class_tag,
830                              jlong size,
831                              jlong* tag,
832                              jint length,
833                              void* cb_user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) {
834     return cb_callbacks->heap_iteration_callback(class_tag,
835                                                  size,
836                                                  tag,
837                                                  length,
838                                                  cb_user_data);
839   };
840   return DoIterateThroughHeap(JvmtiIterateHeap,
841                               env,
842                               ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get(),
843                               heap_filter,
844                               klass,
845                               callbacks,
846                               user_data);
847 }
848 
849 class FollowReferencesHelper final {
850  public:
FollowReferencesHelper(HeapUtil * h,jvmtiEnv * jvmti_env,art::ObjPtr<art::mirror::Object> initial_object,const jvmtiHeapCallbacks * callbacks,art::ObjPtr<art::mirror::Class> class_filter,jint heap_filter,const void * user_data)851   FollowReferencesHelper(HeapUtil* h,
852                          jvmtiEnv* jvmti_env,
853                          art::ObjPtr<art::mirror::Object> initial_object,
854                          const jvmtiHeapCallbacks* callbacks,
855                          art::ObjPtr<art::mirror::Class> class_filter,
856                          jint heap_filter,
857                          const void* user_data)
858       : env(jvmti_env),
859         tag_table_(h->GetTags()),
860         initial_object_(initial_object),
861         callbacks_(callbacks),
862         class_filter_(class_filter),
863         heap_filter_(heap_filter),
864         user_data_(user_data),
865         start_(0),
866         stop_reports_(false) {
867   }
868 
Init()869   void Init()
870       REQUIRES_SHARED(art::Locks::mutator_lock_)
871       REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
872     if (initial_object_.IsNull()) {
873       CollectAndReportRootsVisitor carrv(this, tag_table_, &worklist_, &visited_);
874 
875       // We need precise info (e.g., vregs).
876       constexpr art::VisitRootFlags kRootFlags = static_cast<art::VisitRootFlags>(
877           art::VisitRootFlags::kVisitRootFlagAllRoots | art::VisitRootFlags::kVisitRootFlagPrecise);
878       art::Runtime::Current()->VisitRoots(&carrv, kRootFlags);
879 
880       art::Runtime::Current()->VisitImageRoots(&carrv);
881       stop_reports_ = carrv.IsStopReports();
882 
883       if (stop_reports_) {
884         worklist_.clear();
885       }
886     } else {
887       visited_.insert(initial_object_.Ptr());
888       worklist_.push_back(initial_object_.Ptr());
889     }
890   }
891 
Work()892   void Work()
893       REQUIRES_SHARED(art::Locks::mutator_lock_)
894       REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
895     // Currently implemented as a BFS. To lower overhead, we don't erase elements immediately
896     // from the head of the work list, instead postponing until there's a gap that's "large."
897     //
898     // Alternatively, we can implement a DFS and use the work list as a stack.
899     while (start_ < worklist_.size()) {
900       art::mirror::Object* cur_obj = worklist_[start_];
901       start_++;
902 
903       if (start_ >= kMaxStart) {
904         worklist_.erase(worklist_.begin(), worklist_.begin() + start_);
905         start_ = 0;
906       }
907 
908       VisitObject(cur_obj);
909 
910       if (stop_reports_) {
911         break;
912       }
913     }
914   }
915 
916  private:
917   class CollectAndReportRootsVisitor final : public art::RootVisitor {
918    public:
CollectAndReportRootsVisitor(FollowReferencesHelper * helper,ObjectTagTable * tag_table,std::vector<art::mirror::Object * > * worklist,std::unordered_set<art::mirror::Object * > * visited)919     CollectAndReportRootsVisitor(FollowReferencesHelper* helper,
920                                  ObjectTagTable* tag_table,
921                                  std::vector<art::mirror::Object*>* worklist,
922                                  std::unordered_set<art::mirror::Object*>* visited)
923         : helper_(helper),
924           tag_table_(tag_table),
925           worklist_(worklist),
926           visited_(visited),
927           stop_reports_(false) {}
928 
VisitRoots(art::mirror::Object *** roots,size_t count,const art::RootInfo & info)929     void VisitRoots(art::mirror::Object*** roots, size_t count, const art::RootInfo& info)
930         override
931         REQUIRES_SHARED(art::Locks::mutator_lock_)
932         REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
933       for (size_t i = 0; i != count; ++i) {
934         AddRoot(*roots[i], info);
935       }
936     }
937 
VisitRoots(art::mirror::CompressedReference<art::mirror::Object> ** roots,size_t count,const art::RootInfo & info)938     void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
939                     size_t count,
940                     const art::RootInfo& info)
941         override REQUIRES_SHARED(art::Locks::mutator_lock_)
942         REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
943       for (size_t i = 0; i != count; ++i) {
944         AddRoot(roots[i]->AsMirrorPtr(), info);
945       }
946     }
947 
IsStopReports()948     bool IsStopReports() {
949       return stop_reports_;
950     }
951 
952    private:
AddRoot(art::mirror::Object * root_obj,const art::RootInfo & info)953     void AddRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
954         REQUIRES_SHARED(art::Locks::mutator_lock_)
955         REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
956       if (stop_reports_) {
957         return;
958       }
959       bool add_to_worklist = ReportRoot(root_obj, info);
960       // We use visited_ to mark roots already so we do not need another set.
961       if (visited_->find(root_obj) == visited_->end()) {
962         if (add_to_worklist) {
963           visited_->insert(root_obj);
964           worklist_->push_back(root_obj);
965         }
966       }
967     }
968 
969     // Remove NO_THREAD_SAFETY_ANALYSIS once ASSERT_CAPABILITY works correctly.
FindThread(const art::RootInfo & info)970     art::Thread* FindThread(const art::RootInfo& info) NO_THREAD_SAFETY_ANALYSIS {
971       art::Locks::thread_list_lock_->AssertExclusiveHeld(art::Thread::Current());
972       return art::Runtime::Current()->GetThreadList()->FindThreadByThreadId(info.GetThreadId());
973     }
974 
GetReferenceKind(const art::RootInfo & info,jvmtiHeapReferenceInfo * ref_info)975     jvmtiHeapReferenceKind GetReferenceKind(const art::RootInfo& info,
976                                             jvmtiHeapReferenceInfo* ref_info)
977         REQUIRES_SHARED(art::Locks::mutator_lock_) {
978       // We do not necessarily hold thread_list_lock_ here, but we may if we are called from
979       // VisitThreadRoots, which can happen from JVMTI FollowReferences. If it was acquired in
980       // ThreadList::VisitRoots, it's unsafe to temporarily release it. Thus we act as if we did
981       // not hold the thread_list_lock_ here, and relax CHECKs appropriately. If it does happen,
982       // we are in a SuspendAll situation with concurrent GC disabled, and should not need to run
983       // flip functions. TODO: Find a way to clean this up.
984 
985       // TODO: Fill in ref_info.
986       memset(ref_info, 0, sizeof(jvmtiHeapReferenceInfo));
987 
988       switch (info.GetType()) {
989         case art::RootType::kRootJNIGlobal:
990           return JVMTI_HEAP_REFERENCE_JNI_GLOBAL;
991 
992         case art::RootType::kRootJNILocal:
993         {
994           uint32_t thread_id = info.GetThreadId();
995           ref_info->jni_local.thread_id = thread_id;
996 
997           art::Thread* thread = FindThread(info);
998           if (thread != nullptr) {
999             art::mirror::Object* thread_obj;
1000             if (thread->IsStillStarting()) {
1001               thread_obj = nullptr;
1002             } else {
1003               thread_obj = thread->GetPeerFromOtherThread();
1004             }
1005             if (thread_obj != nullptr) {
1006               ref_info->jni_local.thread_tag = tag_table_->GetTagOrZero(thread_obj);
1007             }
1008           }
1009 
1010           // TODO: We don't have this info.
1011           if (thread != nullptr) {
1012             ref_info->jni_local.depth = 0;
1013             art::ArtMethod* method = thread->GetCurrentMethod(nullptr,
1014                                                               /* check_suspended= */ true,
1015                                                               /* abort_on_error= */ false);
1016             if (method != nullptr) {
1017               ref_info->jni_local.method = art::jni::EncodeArtMethod(method);
1018             }
1019           }
1020 
1021           return JVMTI_HEAP_REFERENCE_JNI_LOCAL;
1022         }
1023 
1024         case art::RootType::kRootJavaFrame:
1025         {
1026           uint32_t thread_id = info.GetThreadId();
1027           ref_info->stack_local.thread_id = thread_id;
1028 
1029           art::Thread* thread = FindThread(info);
1030           if (thread != nullptr) {
1031             art::mirror::Object* thread_obj;
1032             if (thread->IsStillStarting()) {
1033               thread_obj = nullptr;
1034             } else {
1035               thread_obj = thread->GetPeerFromOtherThread();
1036             }
1037             if (thread_obj != nullptr) {
1038               ref_info->stack_local.thread_tag = tag_table_->GetTagOrZero(thread_obj);
1039             }
1040           }
1041 
1042           auto& java_info = static_cast<const art::JavaFrameRootInfo&>(info);
1043           size_t vreg = java_info.GetVReg();
1044           ref_info->stack_local.slot = static_cast<jint>(
1045               vreg <= art::JavaFrameRootInfo::kMaxVReg ? vreg : -1);
1046           const art::StackVisitor* visitor = java_info.GetVisitor();
1047           ref_info->stack_local.location =
1048               static_cast<jlocation>(visitor->GetDexPc(/* abort_on_failure= */ false));
1049           ref_info->stack_local.depth = static_cast<jint>(visitor->GetFrameDepth());
1050           art::ArtMethod* method = visitor->GetMethod();
1051           if (method != nullptr) {
1052             ref_info->stack_local.method = art::jni::EncodeArtMethod(method);
1053           }
1054 
1055           return JVMTI_HEAP_REFERENCE_STACK_LOCAL;
1056         }
1057 
1058         case art::RootType::kRootNativeStack:
1059         case art::RootType::kRootThreadBlock:
1060         case art::RootType::kRootThreadObject:
1061           return JVMTI_HEAP_REFERENCE_THREAD;
1062 
1063         case art::RootType::kRootStickyClass:
1064         case art::RootType::kRootInternedString:
1065           // Note: this isn't a root in the RI.
1066           return JVMTI_HEAP_REFERENCE_SYSTEM_CLASS;
1067 
1068         case art::RootType::kRootMonitorUsed:
1069         case art::RootType::kRootJNIMonitor:
1070           return JVMTI_HEAP_REFERENCE_MONITOR;
1071 
1072         case art::RootType::kRootFinalizing:
1073         case art::RootType::kRootDebugger:
1074         case art::RootType::kRootReferenceCleanup:
1075         case art::RootType::kRootVMInternal:
1076         case art::RootType::kRootUnknown:
1077           return JVMTI_HEAP_REFERENCE_OTHER;
1078       }
1079       LOG(FATAL) << "Unreachable";
1080       UNREACHABLE();
1081     }
1082 
ReportRoot(art::mirror::Object * root_obj,const art::RootInfo & info)1083     bool ReportRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
1084         REQUIRES_SHARED(art::Locks::mutator_lock_)
1085         REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1086       jvmtiHeapReferenceInfo ref_info;
1087       jvmtiHeapReferenceKind kind = GetReferenceKind(info, &ref_info);
1088       jint result = helper_->ReportReference(kind, &ref_info, nullptr, root_obj);
1089       if ((result & JVMTI_VISIT_ABORT) != 0) {
1090         stop_reports_ = true;
1091       }
1092       return (result & JVMTI_VISIT_OBJECTS) != 0;
1093     }
1094 
1095    private:
1096     FollowReferencesHelper* helper_;
1097     ObjectTagTable* tag_table_;
1098     std::vector<art::mirror::Object*>* worklist_;
1099     std::unordered_set<art::mirror::Object*>* visited_;
1100     bool stop_reports_;
1101   };
1102 
VisitObject(art::mirror::Object * obj)1103   void VisitObject(art::mirror::Object* obj)
1104       REQUIRES_SHARED(art::Locks::mutator_lock_)
1105       REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1106     if (obj->IsClass()) {
1107       VisitClass(obj->AsClass().Ptr());
1108       return;
1109     }
1110     if (obj->IsArrayInstance()) {
1111       VisitArray(obj);
1112       return;
1113     }
1114 
1115     // All instance fields.
1116     auto report_instance_field =
1117         [&](art::ObjPtr<art::mirror::Object> src,
1118             [[maybe_unused]] art::ObjPtr<art::mirror::Class> obj_klass,
1119             art::ArtField& field,
1120             size_t field_index,
1121             [[maybe_unused]] void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_)
1122             REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1123               art::ObjPtr<art::mirror::Object> field_value = field.GetObject(src);
1124               if (field_value != nullptr) {
1125                 jvmtiHeapReferenceInfo reference_info;
1126                 memset(&reference_info, 0, sizeof(reference_info));
1127 
1128                 reference_info.field.index = field_index;
1129 
1130                 jvmtiHeapReferenceKind kind =
1131                     field.GetOffset().Int32Value() ==
1132                             art::mirror::Object::ClassOffset().Int32Value() ?
1133                         JVMTI_HEAP_REFERENCE_CLASS :
1134                         JVMTI_HEAP_REFERENCE_FIELD;
1135                 const jvmtiHeapReferenceInfo* reference_info_ptr =
1136                     kind == JVMTI_HEAP_REFERENCE_CLASS ? nullptr : &reference_info;
1137 
1138                 return !ReportReferenceMaybeEnqueue(
1139                     kind, reference_info_ptr, src.Ptr(), field_value.Ptr());
1140               }
1141               return false;
1142             };
1143     stop_reports_ = FieldVisitor<void, true>::ReportFields(obj,
1144                                                            nullptr,
1145                                                            VisitorFalse<void>,
1146                                                            VisitorFalse<void>,
1147                                                            VisitorFalse<void>,
1148                                                            report_instance_field);
1149     if (stop_reports_) {
1150       return;
1151     }
1152 
1153     jint string_ret = ReportString(obj, env, tag_table_, callbacks_, user_data_);
1154     stop_reports_ = (string_ret & JVMTI_VISIT_ABORT) != 0;
1155     if (stop_reports_) {
1156       return;
1157     }
1158 
1159     stop_reports_ = ReportPrimitiveField::Report(obj, tag_table_, callbacks_, user_data_);
1160   }
1161 
VisitArray(art::mirror::Object * array)1162   void VisitArray(art::mirror::Object* array)
1163       REQUIRES_SHARED(art::Locks::mutator_lock_)
1164       REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1165     stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS,
1166                                                  nullptr,
1167                                                  array,
1168                                                  array->GetClass());
1169     if (stop_reports_) {
1170       return;
1171     }
1172 
1173     if (array->IsObjectArray()) {
1174       art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> obj_array =
1175           array->AsObjectArray<art::mirror::Object>();
1176       for (auto elem_pair : art::ZipCount(obj_array->Iterate())) {
1177         if (elem_pair.first != nullptr) {
1178           jvmtiHeapReferenceInfo reference_info;
1179           reference_info.array.index = elem_pair.second;
1180           stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT,
1181                                                        &reference_info,
1182                                                        array,
1183                                                        elem_pair.first.Ptr());
1184           if (stop_reports_) {
1185             break;
1186           }
1187         }
1188       }
1189     } else {
1190       if (!stop_reports_) {
1191         jint array_ret = ReportPrimitiveArray(array, env, tag_table_, callbacks_, user_data_);
1192         stop_reports_ = (array_ret & JVMTI_VISIT_ABORT) != 0;
1193       }
1194     }
1195   }
1196 
VisitClass(art::mirror::Class * klass)1197   void VisitClass(art::mirror::Class* klass)
1198       REQUIRES_SHARED(art::Locks::mutator_lock_)
1199       REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1200     // TODO: Are erroneous classes reported? Are non-prepared ones? For now, just use resolved ones.
1201     if (!klass->IsResolved()) {
1202       return;
1203     }
1204 
1205     // Superclass.
1206     stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_SUPERCLASS,
1207                                                  nullptr,
1208                                                  klass,
1209                                                  klass->GetSuperClass().Ptr());
1210     if (stop_reports_) {
1211       return;
1212     }
1213 
1214     // Directly implemented or extended interfaces.
1215     art::Thread* self = art::Thread::Current();
1216     art::StackHandleScope<1> hs(self);
1217     art::Handle<art::mirror::Class> h_klass(hs.NewHandle<art::mirror::Class>(klass));
1218     for (size_t i = 0; i < h_klass->NumDirectInterfaces(); ++i) {
1219       art::ObjPtr<art::mirror::Class> inf_klass =
1220           art::mirror::Class::ResolveDirectInterface(self, h_klass, i);
1221       if (inf_klass == nullptr) {
1222         // TODO: With a resolved class this should not happen...
1223         self->ClearException();
1224         break;
1225       }
1226 
1227       stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_INTERFACE,
1228                                                    nullptr,
1229                                                    klass,
1230                                                    inf_klass.Ptr());
1231       if (stop_reports_) {
1232         return;
1233       }
1234     }
1235 
1236     // Classloader.
1237     // TODO: What about the boot classpath loader? We'll skip for now, but do we have to find the
1238     //       fake BootClassLoader?
1239     if (klass->GetClassLoader() != nullptr) {
1240       stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS_LOADER,
1241                                                    nullptr,
1242                                                    klass,
1243                                                    klass->GetClassLoader().Ptr());
1244       if (stop_reports_) {
1245         return;
1246       }
1247     }
1248     DCHECK_EQ(h_klass.Get(), klass);
1249 
1250     // Declared static fields.
1251     auto report_static_field =
1252         [&]([[maybe_unused]] art::ObjPtr<art::mirror::Object> obj,
1253             art::ObjPtr<art::mirror::Class> obj_klass,
1254             art::ArtField& field,
1255             size_t field_index,
1256             [[maybe_unused]] void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_)
1257             REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1258               art::ObjPtr<art::mirror::Object> field_value = field.GetObject(obj_klass);
1259               if (field_value != nullptr) {
1260                 jvmtiHeapReferenceInfo reference_info;
1261                 memset(&reference_info, 0, sizeof(reference_info));
1262 
1263                 reference_info.field.index = static_cast<jint>(field_index);
1264 
1265                 return !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_STATIC_FIELD,
1266                                                     &reference_info,
1267                                                     obj_klass.Ptr(),
1268                                                     field_value.Ptr());
1269               }
1270               return false;
1271             };
1272     stop_reports_ = FieldVisitor<void, false>::ReportFields(klass,
1273                                                             nullptr,
1274                                                             VisitorFalse<void>,
1275                                                             report_static_field,
1276                                                             VisitorFalse<void>,
1277                                                             VisitorFalse<void>);
1278     if (stop_reports_) {
1279       return;
1280     }
1281 
1282     stop_reports_ = ReportPrimitiveField::Report(klass, tag_table_, callbacks_, user_data_);
1283   }
1284 
MaybeEnqueue(art::mirror::Object * obj)1285   void MaybeEnqueue(art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1286     if (visited_.find(obj) == visited_.end()) {
1287       worklist_.push_back(obj);
1288       visited_.insert(obj);
1289     }
1290   }
1291 
ReportReferenceMaybeEnqueue(jvmtiHeapReferenceKind kind,const jvmtiHeapReferenceInfo * reference_info,art::mirror::Object * referree,art::mirror::Object * referrer)1292   bool ReportReferenceMaybeEnqueue(jvmtiHeapReferenceKind kind,
1293                                    const jvmtiHeapReferenceInfo* reference_info,
1294                                    art::mirror::Object* referree,
1295                                    art::mirror::Object* referrer)
1296       REQUIRES_SHARED(art::Locks::mutator_lock_)
1297       REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1298     jint result = ReportReference(kind, reference_info, referree, referrer);
1299     if ((result & JVMTI_VISIT_ABORT) == 0) {
1300       if ((result & JVMTI_VISIT_OBJECTS) != 0) {
1301         MaybeEnqueue(referrer);
1302       }
1303       return true;
1304     } else {
1305       return false;
1306     }
1307   }
1308 
ReportReference(jvmtiHeapReferenceKind kind,const jvmtiHeapReferenceInfo * reference_info,art::mirror::Object * referrer,art::mirror::Object * referree)1309   jint ReportReference(jvmtiHeapReferenceKind kind,
1310                        const jvmtiHeapReferenceInfo* reference_info,
1311                        art::mirror::Object* referrer,
1312                        art::mirror::Object* referree)
1313       REQUIRES_SHARED(art::Locks::mutator_lock_)
1314       REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1315     if (referree == nullptr || stop_reports_) {
1316       return 0;
1317     }
1318 
1319     if (UNLIKELY(class_filter_ != nullptr) && class_filter_ != referree->GetClass()) {
1320       return JVMTI_VISIT_OBJECTS;
1321     }
1322 
1323     const jlong class_tag = tag_table_->GetTagOrZero(referree->GetClass());
1324     jlong tag = tag_table_->GetTagOrZero(referree);
1325 
1326     if (!heap_filter_.ShouldReportByHeapFilter(tag, class_tag)) {
1327       return JVMTI_VISIT_OBJECTS;
1328     }
1329 
1330     const jlong referrer_class_tag =
1331         referrer == nullptr ? 0 : tag_table_->GetTagOrZero(referrer->GetClass());
1332     const jlong size = static_cast<jlong>(referree->SizeOf());
1333     jlong saved_tag = tag;
1334     jlong referrer_tag = 0;
1335     jlong saved_referrer_tag = 0;
1336     jlong* referrer_tag_ptr;
1337     if (referrer == nullptr) {
1338       referrer_tag_ptr = nullptr;
1339     } else {
1340       if (referrer == referree) {
1341         referrer_tag_ptr = &tag;
1342       } else {
1343         referrer_tag = saved_referrer_tag = tag_table_->GetTagOrZero(referrer);
1344         referrer_tag_ptr = &referrer_tag;
1345       }
1346     }
1347 
1348     jint length = -1;
1349     if (referree->IsArrayInstance()) {
1350       length = referree->AsArray()->GetLength();
1351     }
1352 
1353     jint result = callbacks_->heap_reference_callback(kind,
1354                                                       reference_info,
1355                                                       class_tag,
1356                                                       referrer_class_tag,
1357                                                       size,
1358                                                       &tag,
1359                                                       referrer_tag_ptr,
1360                                                       length,
1361                                                       const_cast<void*>(user_data_));
1362 
1363     if (tag != saved_tag) {
1364       tag_table_->Set(referree, tag);
1365     }
1366     if (referrer_tag != saved_referrer_tag) {
1367       tag_table_->Set(referrer, referrer_tag);
1368     }
1369 
1370     return result;
1371   }
1372 
1373   jvmtiEnv* env;
1374   ObjectTagTable* tag_table_;
1375   art::ObjPtr<art::mirror::Object> initial_object_;
1376   const jvmtiHeapCallbacks* callbacks_;
1377   art::ObjPtr<art::mirror::Class> class_filter_;
1378   const HeapFilter heap_filter_;
1379   const void* user_data_;
1380 
1381   std::vector<art::mirror::Object*> worklist_;
1382   size_t start_;
1383   static constexpr size_t kMaxStart = 1000000U;
1384 
1385   std::unordered_set<art::mirror::Object*> visited_;
1386 
1387   bool stop_reports_;
1388 
1389   friend class CollectAndReportRootsVisitor;
1390 };
1391 
FollowReferences(jvmtiEnv * env,jint heap_filter,jclass klass,jobject initial_object,const jvmtiHeapCallbacks * callbacks,const void * user_data)1392 jvmtiError HeapUtil::FollowReferences(jvmtiEnv* env,
1393                                       jint heap_filter,
1394                                       jclass klass,
1395                                       jobject initial_object,
1396                                       const jvmtiHeapCallbacks* callbacks,
1397                                       const void* user_data) {
1398   if (callbacks == nullptr) {
1399     return ERR(NULL_POINTER);
1400   }
1401 
1402   art::Thread* self = art::Thread::Current();
1403 
1404   art::gc::Heap* heap = art::Runtime::Current()->GetHeap();
1405   if (heap->IsGcConcurrentAndMoving()) {
1406     // Need to take a heap dump while GC isn't running. See the
1407     // comment in Heap::VisitObjects().
1408     heap->IncrementDisableMovingGC(self);
1409   }
1410   {
1411     art::ScopedObjectAccess soa(self);      // Now we know we have the shared lock.
1412     art::jni::ScopedEnableSuspendAllJniIdQueries sjni;  // make sure we can get JNI ids.
1413     art::ScopedThreadSuspension sts(self, art::ThreadState::kWaitingForVisitObjects);
1414     art::ScopedSuspendAll ssa("FollowReferences");
1415 
1416     art::ObjPtr<art::mirror::Class> class_filter = klass == nullptr
1417         ? nullptr
1418         : art::ObjPtr<art::mirror::Class>::DownCast(self->DecodeJObject(klass));
1419     FollowReferencesHelper frh(this,
1420                                env,
1421                                self->DecodeJObject(initial_object),
1422                                callbacks,
1423                                class_filter,
1424                                heap_filter,
1425                                user_data);
1426     frh.Init();
1427     frh.Work();
1428   }
1429   if (heap->IsGcConcurrentAndMoving()) {
1430     heap->DecrementDisableMovingGC(self);
1431   }
1432 
1433   return ERR(NONE);
1434 }
1435 
GetLoadedClasses(jvmtiEnv * env,jint * class_count_ptr,jclass ** classes_ptr)1436 jvmtiError HeapUtil::GetLoadedClasses(jvmtiEnv* env,
1437                                       jint* class_count_ptr,
1438                                       jclass** classes_ptr) {
1439   if (class_count_ptr == nullptr || classes_ptr == nullptr) {
1440     return ERR(NULL_POINTER);
1441   }
1442 
1443   class ReportClassVisitor : public art::ClassVisitor {
1444    public:
1445     explicit ReportClassVisitor(art::Thread* self) : self_(self) {}
1446 
1447     bool operator()(art::ObjPtr<art::mirror::Class> klass)
1448         override REQUIRES_SHARED(art::Locks::mutator_lock_) {
1449       if (klass->IsLoaded() || klass->IsErroneous()) {
1450         classes_.push_back(self_->GetJniEnv()->AddLocalReference<jclass>(klass));
1451       }
1452       return true;
1453     }
1454 
1455     art::Thread* self_;
1456     std::vector<jclass> classes_;
1457   };
1458 
1459   art::Thread* self = art::Thread::Current();
1460   ReportClassVisitor rcv(self);
1461   {
1462     art::ScopedObjectAccess soa(self);
1463     art::Runtime::Current()->GetClassLinker()->VisitClasses(&rcv);
1464   }
1465 
1466   size_t size = rcv.classes_.size();
1467   jclass* classes = nullptr;
1468   jvmtiError alloc_ret = env->Allocate(static_cast<jlong>(size * sizeof(jclass)),
1469                                        reinterpret_cast<unsigned char**>(&classes));
1470   if (alloc_ret != ERR(NONE)) {
1471     return alloc_ret;
1472   }
1473 
1474   for (size_t i = 0; i < size; ++i) {
1475     classes[i] = rcv.classes_[i];
1476   }
1477   *classes_ptr = classes;
1478   *class_count_ptr = static_cast<jint>(size);
1479 
1480   return ERR(NONE);
1481 }
1482 
ForceGarbageCollection(jvmtiEnv * env)1483 jvmtiError HeapUtil::ForceGarbageCollection([[maybe_unused]] jvmtiEnv* env) {
1484   art::Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
1485 
1486   return ERR(NONE);
1487 }
1488 
1489 static constexpr jint kHeapIdDefault = 0;
1490 static constexpr jint kHeapIdImage = 1;
1491 static constexpr jint kHeapIdZygote = 2;
1492 static constexpr jint kHeapIdApp = 3;
1493 
GetHeapId(art::ObjPtr<art::mirror::Object> obj)1494 static jint GetHeapId(art::ObjPtr<art::mirror::Object> obj)
1495     REQUIRES_SHARED(art::Locks::mutator_lock_) {
1496   if (obj == nullptr) {
1497     return -1;
1498   }
1499 
1500   art::gc::Heap* const heap = art::Runtime::Current()->GetHeap();
1501   const art::gc::space::ContinuousSpace* const space =
1502       heap->FindContinuousSpaceFromObject(obj, true);
1503   jint heap_type = kHeapIdApp;
1504   if (space != nullptr) {
1505     if (space->IsZygoteSpace()) {
1506       heap_type = kHeapIdZygote;
1507     } else if (space->IsImageSpace() && heap->ObjectIsInBootImageSpace(obj)) {
1508       // Only count objects in the boot image as HPROF_HEAP_IMAGE, this leaves app image objects
1509       // as HPROF_HEAP_APP. b/35762934
1510       heap_type = kHeapIdImage;
1511     }
1512   } else {
1513     const auto* los = heap->GetLargeObjectsSpace();
1514     if (los->Contains(obj.Ptr()) && los->IsZygoteLargeObject(art::Thread::Current(), obj.Ptr())) {
1515       heap_type = kHeapIdZygote;
1516     }
1517   }
1518   return heap_type;
1519 };
1520 
GetObjectHeapId(jvmtiEnv * env,jlong tag,jint * heap_id,...)1521 jvmtiError HeapExtensions::GetObjectHeapId(jvmtiEnv* env, jlong tag, jint* heap_id, ...) {
1522   if (heap_id == nullptr) {
1523     return ERR(NULL_POINTER);
1524   }
1525 
1526   art::Thread* self = art::Thread::Current();
1527 
1528   auto work = [&]() REQUIRES_SHARED(art::Locks::mutator_lock_) {
1529     ObjectTagTable* tag_table = ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get();
1530     art::ObjPtr<art::mirror::Object> obj = tag_table->Find(tag);
1531     jint heap_type = GetHeapId(obj);
1532     if (heap_type == -1) {
1533       return ERR(NOT_FOUND);
1534     }
1535     *heap_id = heap_type;
1536     return ERR(NONE);
1537   };
1538 
1539   if (!art::Locks::mutator_lock_->IsSharedHeld(self)) {
1540     if (!self->IsThreadSuspensionAllowable()) {
1541       return ERR(INTERNAL);
1542     }
1543     art::ScopedObjectAccess soa(self);
1544     return work();
1545   } else {
1546     // We cannot use SOA in this case. We might be holding the lock, but may not be in the
1547     // runnable state (e.g., during GC).
1548     art::Locks::mutator_lock_->AssertSharedHeld(self);
1549     // TODO: Investigate why ASSERT_SHARED_CAPABILITY doesn't work.
1550     auto annotalysis_workaround = [&]() NO_THREAD_SAFETY_ANALYSIS {
1551       return work();
1552     };
1553     return annotalysis_workaround();
1554   }
1555 }
1556 
CopyStringAndReturn(jvmtiEnv * env,const char * in,char ** out)1557 static jvmtiError CopyStringAndReturn(jvmtiEnv* env, const char* in, char** out) {
1558   jvmtiError error;
1559   JvmtiUniquePtr<char[]> param_name = CopyString(env, in, &error);
1560   if (param_name == nullptr) {
1561     return error;
1562   }
1563   *out = param_name.release();
1564   return ERR(NONE);
1565 }
1566 
1567 static constexpr const char* kHeapIdDefaultName = "default";
1568 static constexpr const char* kHeapIdImageName = "image";
1569 static constexpr const char* kHeapIdZygoteName = "zygote";
1570 static constexpr const char* kHeapIdAppName = "app";
1571 
GetHeapName(jvmtiEnv * env,jint heap_id,char ** heap_name,...)1572 jvmtiError HeapExtensions::GetHeapName(jvmtiEnv* env, jint heap_id, char** heap_name, ...) {
1573   switch (heap_id) {
1574     case kHeapIdDefault:
1575       return CopyStringAndReturn(env, kHeapIdDefaultName, heap_name);
1576     case kHeapIdImage:
1577       return CopyStringAndReturn(env, kHeapIdImageName, heap_name);
1578     case kHeapIdZygote:
1579       return CopyStringAndReturn(env, kHeapIdZygoteName, heap_name);
1580     case kHeapIdApp:
1581       return CopyStringAndReturn(env, kHeapIdAppName, heap_name);
1582 
1583     default:
1584       return ERR(ILLEGAL_ARGUMENT);
1585   }
1586 }
1587 
IterateThroughHeapExt(jvmtiEnv * env,jint heap_filter,jclass klass,const jvmtiHeapCallbacks * callbacks,const void * user_data)1588 jvmtiError HeapExtensions::IterateThroughHeapExt(jvmtiEnv* env,
1589                                                  jint heap_filter,
1590                                                  jclass klass,
1591                                                  const jvmtiHeapCallbacks* callbacks,
1592                                                  const void* user_data) {
1593   if (ArtJvmTiEnv::AsArtJvmTiEnv(env)->capabilities.can_tag_objects != 1) { \
1594     return ERR(MUST_POSSESS_CAPABILITY); \
1595   }
1596 
1597   // ART extension API: Also pass the heap id.
1598   auto ArtIterateHeap = [](art::mirror::Object* obj,
1599                            const jvmtiHeapCallbacks* cb_callbacks,
1600                            jlong class_tag,
1601                            jlong size,
1602                            jlong* tag,
1603                            jint length,
1604                            void* cb_user_data)
1605       REQUIRES_SHARED(art::Locks::mutator_lock_) {
1606     jint heap_id = GetHeapId(obj);
1607     using ArtExtensionAPI = jint (*)(jlong, jlong, jlong*, jint length, void*, jint);
1608     return reinterpret_cast<ArtExtensionAPI>(cb_callbacks->heap_iteration_callback)(
1609         class_tag, size, tag, length, cb_user_data, heap_id);
1610   };
1611   return DoIterateThroughHeap(ArtIterateHeap,
1612                               env,
1613                               ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get(),
1614                               heap_filter,
1615                               klass,
1616                               callbacks,
1617                               user_data);
1618 }
1619 
1620 namespace {
1621 
1622 using ObjectPtr = art::ObjPtr<art::mirror::Object>;
1623 using ObjectMap = std::unordered_map<ObjectPtr, ObjectPtr, art::HashObjPtr>;
1624 
ReplaceObjectReferences(const ObjectMap & map)1625 static void ReplaceObjectReferences(const ObjectMap& map)
1626     REQUIRES(art::Locks::mutator_lock_,
1627              art::Roles::uninterruptible_) {
1628   art::Runtime::Current()->GetHeap()->VisitObjectsPaused(
1629       [&](art::mirror::Object* ref) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1630         // Rewrite all references in the object if needed.
1631         class ResizeReferenceVisitor {
1632          public:
1633           using CompressedObj = art::mirror::CompressedReference<art::mirror::Object>;
1634           explicit ResizeReferenceVisitor(const ObjectMap& map, ObjectPtr ref)
1635               : map_(map), ref_(ref) {}
1636 
1637           // Ignore class roots.
1638           void VisitRootIfNonNull(CompressedObj* root) const
1639               REQUIRES_SHARED(art::Locks::mutator_lock_) {
1640             if (root != nullptr) {
1641               VisitRoot(root);
1642             }
1643           }
1644           void VisitRoot(CompressedObj* root) const REQUIRES_SHARED(art::Locks::mutator_lock_) {
1645             auto it = map_.find(root->AsMirrorPtr());
1646             if (it != map_.end()) {
1647               root->Assign(it->second);
1648               art::WriteBarrier::ForEveryFieldWrite(ref_);
1649             }
1650           }
1651 
1652           void operator()(art::ObjPtr<art::mirror::Object> obj,
1653                           art::MemberOffset off,
1654                           bool is_static) const
1655               REQUIRES_SHARED(art::Locks::mutator_lock_) {
1656             auto it = map_.find(obj->GetFieldObject<art::mirror::Object>(off));
1657             if (it != map_.end()) {
1658               UNUSED(is_static);
1659               if (UNLIKELY(!is_static && off == art::mirror::Object::ClassOffset())) {
1660                 // We don't want to update the declaring class of any objects. They will be replaced
1661                 // in the heap and we need the declaring class to know its size.
1662                 return;
1663               } else if (UNLIKELY(!is_static && off == art::mirror::Class::SuperClassOffset() &&
1664                                   obj->IsClass())) {
1665                 // We don't want to be messing with the class hierarcy either.
1666                 return;
1667               }
1668               VLOG(plugin) << "Updating field at offset " << off.Uint32Value() << " of type "
1669                            << obj->GetClass()->PrettyClass();
1670               obj->SetFieldObject</*transaction*/ false>(off, it->second);
1671               art::WriteBarrier::ForEveryFieldWrite(obj);
1672             }
1673           }
1674 
1675           // java.lang.ref.Reference visitor.
1676           void operator()([[maybe_unused]] art::ObjPtr<art::mirror::Class> klass,
1677                           art::ObjPtr<art::mirror::Reference> ref) const
1678               REQUIRES_SHARED(art::Locks::mutator_lock_) {
1679             operator()(ref, art::mirror::Reference::ReferentOffset(), /* is_static */ false);
1680           }
1681 
1682          private:
1683           const ObjectMap& map_;
1684           ObjectPtr ref_;
1685         };
1686 
1687         ResizeReferenceVisitor rrv(map, ref);
1688         if (ref->IsClass()) {
1689           // Class object native roots are the ArtField and ArtMethod 'declaring_class_' fields
1690           // which we don't want to be messing with as it would break ref-visitor assumptions about
1691           // what a class looks like. We want to keep the default behavior in other cases (such as
1692           // dex-cache) though. Unfortunately there is no way to tell from the visitor where exactly
1693           // the root came from.
1694           // TODO It might be nice to have the visitors told where the reference came from.
1695           ref->VisitReferences</*kVisitNativeRoots*/false>(rrv, rrv);
1696         } else {
1697           ref->VisitReferences</*kVisitNativeRoots*/true>(rrv, rrv);
1698         }
1699       });
1700 }
1701 
ReplaceStrongRoots(art::Thread * self,const ObjectMap & map)1702 static void ReplaceStrongRoots(art::Thread* self, const ObjectMap& map)
1703     REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) {
1704   // replace root references expcept java frames.
1705   struct ResizeRootVisitor : public art::RootVisitor {
1706    public:
1707     explicit ResizeRootVisitor(const ObjectMap& map) : map_(map) {}
1708 
1709     // TODO It's somewhat annoying to have to have this function implemented twice. It might be
1710     // good/useful to implement operator= for CompressedReference to allow us to use a template to
1711     // implement both of these.
1712     void VisitRoots(art::mirror::Object*** roots, size_t count, const art::RootInfo& info) override
1713         REQUIRES_SHARED(art::Locks::mutator_lock_) {
1714       art::mirror::Object*** end = roots + count;
1715       for (art::mirror::Object** obj = *roots; roots != end; obj = *(++roots)) {
1716         auto it = map_.find(*obj);
1717         if (it != map_.end()) {
1718           // Java frames might have the JIT doing optimizations (for example loop-unrolling or
1719           // eliding bounds checks) so we need deopt them once we're done here.
1720           if (info.GetType() == art::RootType::kRootJavaFrame) {
1721             const art::JavaFrameRootInfo& jfri =
1722                 art::down_cast<const art::JavaFrameRootInfo&>(info);
1723             if (jfri.GetVReg() == art::JavaFrameRootInfo::kMethodDeclaringClass) {
1724               info.Describe(VLOG_STREAM(plugin) << "Not changing declaring-class during stack"
1725                                                 << " walk. Found obsolete java frame id ");
1726               continue;
1727             } else {
1728               info.Describe(VLOG_STREAM(plugin) << "Found java frame id ");
1729               threads_with_roots_.insert(info.GetThreadId());
1730             }
1731           }
1732           *obj = it->second.Ptr();
1733         }
1734       }
1735     }
1736 
1737     void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
1738                     size_t count,
1739                     const art::RootInfo& info) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
1740       art::mirror::CompressedReference<art::mirror::Object>** end = roots + count;
1741       for (art::mirror::CompressedReference<art::mirror::Object>* obj = *roots; roots != end;
1742            obj = *(++roots)) {
1743         auto it = map_.find(obj->AsMirrorPtr());
1744         if (it != map_.end()) {
1745           // Java frames might have the JIT doing optimizations (for example loop-unrolling or
1746           // eliding bounds checks) so we need deopt them once we're done here.
1747           if (info.GetType() == art::RootType::kRootJavaFrame) {
1748             const art::JavaFrameRootInfo& jfri =
1749                 art::down_cast<const art::JavaFrameRootInfo&>(info);
1750             if (jfri.GetVReg() == art::JavaFrameRootInfo::kMethodDeclaringClass) {
1751               info.Describe(VLOG_STREAM(plugin) << "Not changing declaring-class during stack"
1752                                                 << " walk. Found obsolete java frame id ");
1753               continue;
1754             } else {
1755               info.Describe(VLOG_STREAM(plugin) << "Found java frame id ");
1756               threads_with_roots_.insert(info.GetThreadId());
1757             }
1758           }
1759           obj->Assign(it->second);
1760         }
1761       }
1762     }
1763 
1764     const std::unordered_set<uint32_t>& GetThreadsWithJavaFrameRoots() const {
1765       return threads_with_roots_;
1766     }
1767 
1768    private:
1769     const ObjectMap& map_;
1770     std::unordered_set<uint32_t> threads_with_roots_;
1771   };
1772   ResizeRootVisitor rrv(map);
1773   art::Runtime::Current()->VisitRoots(&rrv, art::VisitRootFlags::kVisitRootFlagAllRoots);
1774   // Handle java Frames. Annoyingly the JIT can embed information about the length of the array into
1775   // the compiled code. By changing the length of the array we potentially invalidate these
1776   // assumptions and so could cause (eg) OOB array access or other issues.
1777   if (!rrv.GetThreadsWithJavaFrameRoots().empty()) {
1778     art::MutexLock mu(self, *art::Locks::thread_list_lock_);
1779     art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList();
1780     art::instrumentation::Instrumentation* instr = art::Runtime::Current()->GetInstrumentation();
1781     for (uint32_t id : rrv.GetThreadsWithJavaFrameRoots()) {
1782       art::Thread* t = thread_list->FindThreadByThreadId(id);
1783       CHECK(t != nullptr) << "id " << id << " does not refer to a valid thread."
1784                           << " Where did the roots come from?";
1785       VLOG(plugin) << "Instrumenting thread stack of thread " << *t;
1786       // TODO Use deopt manager. We need a version that doesn't acquire all the locks we
1787       // already have.
1788       // TODO We technically only need to do this if the frames are not already being interpreted.
1789       // The cost for doing an extra stack walk is unlikely to be worth it though.
1790       instr->InstrumentThreadStack(t, /* force_deopt= */ true);
1791     }
1792   }
1793 }
1794 
ReplaceWeakRoots(art::Thread * self,EventHandler * event_handler,const ObjectMap & map)1795 static void ReplaceWeakRoots(art::Thread* self,
1796                              EventHandler* event_handler,
1797                              const ObjectMap& map)
1798     REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) {
1799   // Handle tags. We want to do this seprately from other weak-refs (handled below) because we need
1800   // to send additional events and handle cases where the agent might have tagged the new
1801   // replacement object during the VMObjectAlloc. We do this by removing all tags associated with
1802   // both the obsolete and the new arrays. Then we send the ObsoleteObjectCreated event and cache
1803   // the new tag values. We next update all the other weak-references (the tags have been removed)
1804   // and finally update the tag table with the new values. Doing things in this way (1) keeps all
1805   // code relating to updating weak-references together and (2) ensures we don't end up in strange
1806   // situations where the order of weak-ref visiting affects the final tagging state. Since we have
1807   // the mutator_lock_ and gc-paused throughout this whole process no threads should be able to see
1808   // the interval where the objects are not tagged.
1809   struct NewTagValue {
1810    public:
1811     ObjectPtr obsolete_obj_;
1812     jlong obsolete_tag_;
1813     ObjectPtr new_obj_;
1814     jlong new_tag_;
1815   };
1816 
1817   // Map from the environment to the list of <obsolete_tag, new_tag> pairs that were changed.
1818   std::unordered_map<ArtJvmTiEnv*, std::vector<NewTagValue>> changed_tags;
1819   event_handler->ForEachEnv(self, [&](ArtJvmTiEnv* env) {
1820     // Cannot have REQUIRES(art::Locks::mutator_lock_) since ForEachEnv doesn't require it.
1821     art::Locks::mutator_lock_->AssertExclusiveHeld(self);
1822     env->object_tag_table->Lock();
1823     // Get the tags and clear them (so we don't need to special-case the normal weak-ref visitor)
1824     for (auto it : map) {
1825       jlong new_tag = 0;
1826       jlong obsolete_tag = 0;
1827       bool had_obsolete_tag = env->object_tag_table->RemoveLocked(it.first, &obsolete_tag);
1828       bool had_new_tag = env->object_tag_table->RemoveLocked(it.second, &new_tag);
1829       // Dispatch event.
1830       if (had_obsolete_tag || had_new_tag) {
1831         event_handler->DispatchEventOnEnv<ArtJvmtiEvent::kObsoleteObjectCreated>(
1832             env, self, &obsolete_tag, &new_tag);
1833         changed_tags.try_emplace(env).first->second.push_back(
1834             { it.first, obsolete_tag, it.second, new_tag });
1835       }
1836     }
1837     // After weak-ref update we need to go back and re-add obsoletes. We wait to avoid having to
1838     // deal with the visit-weaks overwriting the initial new_obj_ptr tag and generally making things
1839     // difficult.
1840     env->object_tag_table->Unlock();
1841   });
1842   // Handle weak-refs.
1843   struct ReplaceWeaksVisitor : public art::IsMarkedVisitor {
1844    public:
1845     ReplaceWeaksVisitor(const ObjectMap& map) : map_(map) {}
1846 
1847     art::mirror::Object* IsMarked(art::mirror::Object* obj)
1848         REQUIRES_SHARED(art::Locks::mutator_lock_) {
1849       auto it = map_.find(obj);
1850       if (it != map_.end()) {
1851         return it->second.Ptr();
1852       } else {
1853         return obj;
1854       }
1855     }
1856 
1857    private:
1858     const ObjectMap& map_;
1859   };
1860   ReplaceWeaksVisitor rwv(map);
1861   art::Runtime* runtime = art::Runtime::Current();
1862   runtime->SweepSystemWeaks(&rwv);
1863   runtime->GetThreadList()->SweepInterpreterCaches(&rwv);
1864   // Re-add the object tags. At this point all weak-references to the old_obj_ptr are gone.
1865   event_handler->ForEachEnv(self, [&](ArtJvmTiEnv* env) {
1866     // Cannot have REQUIRES(art::Locks::mutator_lock_) since ForEachEnv doesn't require it.
1867     art::Locks::mutator_lock_->AssertExclusiveHeld(self);
1868     env->object_tag_table->Lock();
1869     auto it = changed_tags.find(env);
1870     if (it != changed_tags.end()) {
1871       for (const NewTagValue& v : it->second) {
1872         env->object_tag_table->SetLocked(v.obsolete_obj_, v.obsolete_tag_);
1873         env->object_tag_table->SetLocked(v.new_obj_, v.new_tag_);
1874       }
1875     }
1876     env->object_tag_table->Unlock();
1877   });
1878 }
1879 
1880 }  // namespace
1881 
ReplaceReference(art::Thread * self,art::ObjPtr<art::mirror::Object> old_obj_ptr,art::ObjPtr<art::mirror::Object> new_obj_ptr)1882 void HeapExtensions::ReplaceReference(art::Thread* self,
1883                                       art::ObjPtr<art::mirror::Object> old_obj_ptr,
1884                                       art::ObjPtr<art::mirror::Object> new_obj_ptr) {
1885   ObjectMap map { { old_obj_ptr, new_obj_ptr } };
1886   ReplaceReferences(self, map);
1887 }
1888 
ReplaceReferences(art::Thread * self,const ObjectMap & map)1889 void HeapExtensions::ReplaceReferences(art::Thread* self, const ObjectMap& map) {
1890   ReplaceObjectReferences(map);
1891   ReplaceStrongRoots(self, map);
1892   ReplaceWeakRoots(self, HeapExtensions::gEventHandler, map);
1893 }
1894 
ChangeArraySize(jvmtiEnv * env,jobject arr,jsize new_size)1895 jvmtiError HeapExtensions::ChangeArraySize(jvmtiEnv* env, jobject arr, jsize new_size) {
1896   if (ArtJvmTiEnv::AsArtJvmTiEnv(env)->capabilities.can_tag_objects != 1) {
1897     return ERR(MUST_POSSESS_CAPABILITY);
1898   }
1899   art::Thread* self = art::Thread::Current();
1900   ScopedNoUserCodeSuspension snucs(self);
1901   art::ScopedObjectAccess soa(self);
1902   if (arr == nullptr) {
1903     JVMTI_LOG(INFO, env) << "Cannot resize a null object";
1904     return ERR(NULL_POINTER);
1905   }
1906   art::ObjPtr<art::mirror::Class> klass(soa.Decode<art::mirror::Object>(arr)->GetClass());
1907   if (!klass->IsArrayClass()) {
1908     JVMTI_LOG(INFO, env) << klass->PrettyClass() << " is not an array class!";
1909     return ERR(ILLEGAL_ARGUMENT);
1910   }
1911   if (new_size < 0) {
1912     JVMTI_LOG(INFO, env) << "Cannot resize an array to a negative size";
1913     return ERR(ILLEGAL_ARGUMENT);
1914   }
1915   // Allocate the new copy.
1916   art::StackHandleScope<2> hs(self);
1917   art::Handle<art::mirror::Array> old_arr(hs.NewHandle(soa.Decode<art::mirror::Array>(arr)));
1918   art::MutableHandle<art::mirror::Array> new_arr(hs.NewHandle<art::mirror::Array>(nullptr));
1919   if (klass->IsObjectArrayClass()) {
1920     new_arr.Assign(
1921         art::mirror::ObjectArray<art::mirror::Object>::Alloc(self, old_arr->GetClass(), new_size));
1922   } else {
1923     // NB This also copies the old array but since we aren't suspended we need to do this again to
1924     // catch any concurrent modifications.
1925     new_arr.Assign(art::mirror::Array::CopyOf(old_arr, self, new_size));
1926   }
1927   if (new_arr.IsNull()) {
1928     self->AssertPendingOOMException();
1929     JVMTI_LOG(INFO, env) << "Unable to allocate " << old_arr->GetClass()->PrettyClass()
1930                          << " (length: " << new_size << ") due to OOME. Error was: "
1931                          << self->GetException()->Dump();
1932     self->ClearException();
1933     return ERR(OUT_OF_MEMORY);
1934   } else {
1935     self->AssertNoPendingException();
1936   }
1937   // Suspend everything.
1938   art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
1939   art::gc::ScopedGCCriticalSection sgccs(
1940       self, art::gc::GcCause::kGcCauseDebugger, art::gc::CollectorType::kCollectorTypeDebugger);
1941   art::ScopedSuspendAll ssa("Resize array!");
1942   // Replace internals.
1943   new_arr->SetLockWord(old_arr->GetLockWord(false), false);
1944   old_arr->SetLockWord(art::LockWord::Default(), false);
1945   // Copy the contents now when everything is suspended.
1946   int32_t size = std::min(old_arr->GetLength(), new_size);
1947   switch (old_arr->GetClass()->GetComponentType()->GetPrimitiveType()) {
1948     case art::Primitive::kPrimBoolean:
1949       new_arr->AsBooleanArray()->Memcpy(0, old_arr->AsBooleanArray(), 0, size);
1950       break;
1951     case art::Primitive::kPrimByte:
1952       new_arr->AsByteArray()->Memcpy(0, old_arr->AsByteArray(), 0, size);
1953       break;
1954     case art::Primitive::kPrimChar:
1955       new_arr->AsCharArray()->Memcpy(0, old_arr->AsCharArray(), 0, size);
1956       break;
1957     case art::Primitive::kPrimShort:
1958       new_arr->AsShortArray()->Memcpy(0, old_arr->AsShortArray(), 0, size);
1959       break;
1960     case art::Primitive::kPrimInt:
1961       new_arr->AsIntArray()->Memcpy(0, old_arr->AsIntArray(), 0, size);
1962       break;
1963     case art::Primitive::kPrimLong:
1964       new_arr->AsLongArray()->Memcpy(0, old_arr->AsLongArray(), 0, size);
1965       break;
1966     case art::Primitive::kPrimFloat:
1967       new_arr->AsFloatArray()->Memcpy(0, old_arr->AsFloatArray(), 0, size);
1968       break;
1969     case art::Primitive::kPrimDouble:
1970       new_arr->AsDoubleArray()->Memcpy(0, old_arr->AsDoubleArray(), 0, size);
1971       break;
1972     case art::Primitive::kPrimNot:
1973       for (int32_t i = 0; i < size; i++) {
1974         new_arr->AsObjectArray<art::mirror::Object>()->Set(
1975             i, old_arr->AsObjectArray<art::mirror::Object>()->Get(i));
1976       }
1977       break;
1978     case art::Primitive::kPrimVoid:
1979       LOG(FATAL) << "void-array is not a legal type!";
1980       UNREACHABLE();
1981   }
1982   // Actually replace all the pointers.
1983   ReplaceReference(self, old_arr.Get(), new_arr.Get());
1984   return OK;
1985 }
1986 
Register(EventHandler * eh)1987 void HeapExtensions::Register(EventHandler* eh) {
1988   gEventHandler = eh;
1989 }
1990 
1991 }  // namespace openjdkjvmti
1992