struct IndexCache { // The number of interface fields implemented by the class. This is a prefix to all assigned // field indices.
size_t interface_fields;
// It would be nice to also cache the following, but it is complicated to wire up into the // generic visit: // The number of fields in interfaces and superclasses. This is the first index assigned to // fields of the class. // size_t superclass_fields;
}; using IndexCachingTable = JvmtiWeakTable<IndexCache>;
static IndexCachingTable gIndexCachingTable;
// Report the contents of a string, if a callback is set.
jint ReportString(art::ObjPtr<art::mirror::Object> obj,
jvmtiEnv* env,
ObjectTagTable* tag_table, const jvmtiHeapCallbacks* cb, constvoid* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) { if (UNLIKELY(cb->string_primitive_value_callback != nullptr) && obj->IsString()) {
art::ObjPtr<art::mirror::String> str = obj->AsString();
int32_t string_length = str->GetLength();
JvmtiUniquePtr<uint16_t[]> data;
if (string_length > 0) {
jvmtiError alloc_error;
data = AllocJvmtiUniquePtr<uint16_t[]>(env, string_length, &alloc_error); if (data == nullptr) { // TODO: Not really sure what to do here. Should we abort the iteration and go all the way // back? For now just warn.
LOG(WARNING) << "Unable to allocate buffer for string reporting! Silently dropping value."
<< " >" << str->ToModifiedUtf8() << "<"; return0;
}
if (str->IsCompressed()) {
uint8_t* compressed_data = str->GetValueCompressed(); for (int32_t i = 0; i != string_length; ++i) {
data[i] = compressed_data[i];
}
} else { // Can copy directly.
memcpy(data.get(), str->GetValue(), string_length * sizeof(uint16_t));
}
}
jint result; if (array_length == 0) {
result = cb->array_primitive_value_callback(class_tag,
obj->SizeOf(),
&array_tag, 0,
prim_type,
nullptr, const_cast<void*>(user_data));
} else {
jvmtiError alloc_error;
JvmtiUniquePtr<char[]> data = AllocJvmtiUniquePtr<char[]>(env,
array_length * component_size,
&alloc_error); if (data == nullptr) { // TODO: Not really sure what to do here. Should we abort the iteration and go all the way // back? For now just warn.
LOG(WARNING) << "Unable to allocate buffer for array reporting! Silently dropping value."; return0;
}
template <typename UserData, bool kCallVisitorOnRecursion> class FieldVisitor { public: // Report the contents of a primitive fields of the given object, if a callback is set. template <typename StaticPrimitiveVisitor, typename StaticReferenceVisitor, typename InstancePrimitiveVisitor, typename InstanceReferenceVisitor> staticbool 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)
REQUIRES_SHARED(art::Locks::mutator_lock_) {
FieldVisitor fv(user_data);
if (obj->IsClass()) { // When visiting a class, we only visit the static fields of the given class. No field of // superclasses is visited.
art::ObjPtr<art::mirror::Class> klass = obj->AsClass(); // Only report fields on resolved classes. We need valid field data. if (!klass->IsResolved()) { returnfalse;
} return fv.ReportFieldsImpl(nullptr,
obj->AsClass(),
obj->AsClass()->IsInterface(),
static_prim_visitor,
static_ref_visitor,
instance_prim_visitor,
instance_ref_visitor);
} else { // See comment above. Just double-checking here, but an instance *should* mean the class was // resolved.
DCHECK(obj->GetClass()->IsResolved() || obj->GetClass()->IsErroneousResolved()); return fv.ReportFieldsImpl(obj,
obj->GetClass(), false,
static_prim_visitor,
static_ref_visitor,
instance_prim_visitor,
instance_ref_visitor);
}
}
// Report the contents of fields of the given object. If obj is null, report the static fields, // otherwise the instance fields. template <typename StaticPrimitiveVisitor, typename StaticReferenceVisitor, typename InstancePrimitiveVisitor, typename InstanceReferenceVisitor> bool 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)
REQUIRES_SHARED(art::Locks::mutator_lock_) { // Compute the offset of field indices.
size_t interface_field_count = CountInterfaceFields(klass);
// Visit primitive fields in an object (instance). Return true if the visit was aborted. template <typename StaticPrimitiveVisitor, typename StaticReferenceVisitor, typename InstancePrimitiveVisitor, typename InstanceReferenceVisitor> bool 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)
REQUIRES_SHARED(art::Locks::mutator_lock_) {
DCHECK(klass != nullptr);
size_t field_index; if (klass->GetSuperClass() == nullptr) { // j.l.Object. Start with the fields from interfaces.
field_index = interface_fields; if (skip_java_lang_object) {
*field_index_out = field_index; returnfalse;
}
} else { // Report superclass fields. if (kCallVisitorOnRecursion) { if (ReportFieldsRecursive(obj,
klass->GetSuperClass(),
interface_fields,
skip_java_lang_object,
static_prim_visitor,
static_ref_visitor,
instance_prim_visitor,
instance_ref_visitor,
&field_index)) { returntrue;
}
} else { // Still call, but with empty visitor. This is required for correct counting.
ReportFieldsRecursive(obj,
klass->GetSuperClass(),
interface_fields,
skip_java_lang_object,
VisitorFalse<UserData>,
VisitorFalse<UserData>,
VisitorFalse<UserData>,
VisitorFalse<UserData>,
&field_index);
}
}
// Now visit fields for the current klass.
for (auto& field : klass->GetFields()) { if (field.IsStatic()) { if (field.IsPrimitiveType()) { if (static_prim_visitor(obj,
klass,
field,
field_index,
user_data_)) { returntrue;
}
} else { if (static_ref_visitor(obj,
klass,
field,
field_index,
user_data_)) { returntrue;
}
}
} else { if (field.IsPrimitiveType()) { if (instance_prim_visitor(obj,
klass,
field,
field_index,
user_data_)) { returntrue;
}
} else { if (instance_ref_visitor(obj,
klass,
field,
field_index,
user_data_)) { returntrue;
}
}
}
field_index++;
}
*field_index_out = field_index; returnfalse;
}
// Implements a visit of the implemented interfaces of a given class. template <typename T> struct RecursiveInterfaceVisit { staticvoid VisitStatic(art::Thread* self, art::ObjPtr<art::mirror::Class> klass, T& visitor)
REQUIRES_SHARED(art::Locks::mutator_lock_) {
RecursiveInterfaceVisit rv;
rv.Visit(self, klass, visitor);
}
void Visit(art::Thread* self, art::ObjPtr<art::mirror::Class> klass, T& visitor)
REQUIRES_SHARED(art::Locks::mutator_lock_) { // First visit the parent, to get the order right. // (We do this in preparation for actual visiting of interface fields.) if (klass->GetSuperClass() != nullptr) {
Visit(self, klass->GetSuperClass(), visitor);
} for (uint32_t i = 0; i != klass->NumDirectInterfaces(); ++i) {
art::ObjPtr<art::mirror::Class> inf_klass = klass->GetDirectInterface(i);
DCHECK(inf_klass != nullptr);
VisitInterface(self, inf_klass, visitor);
}
}
void VisitInterface(art::Thread* self, art::ObjPtr<art::mirror::Class> inf_klass, T& visitor)
REQUIRES_SHARED(art::Locks::mutator_lock_) { auto it = visited_interfaces.find(inf_klass.Ptr()); if (it != visited_interfaces.end()) { return;
}
visited_interfaces.insert(inf_klass.Ptr());
// Let the visitor know about this one. Note that this order is acceptable, as the ordering // of these fields never matters for known visitors.
visitor(inf_klass);
// Now visit the superinterfaces. for (uint32_t i = 0; i != inf_klass->NumDirectInterfaces(); ++i) {
art::ObjPtr<art::mirror::Class> super_inf_klass = inf_klass->GetDirectInterface(i);
DCHECK(super_inf_klass != nullptr);
VisitInterface(self, super_inf_klass, visitor);
}
}
// Counting interface fields. Note that we cannot use the interface table, as that only contains // "non-marker" interfaces (= interfaces with methods). static size_t CountInterfaceFields(art::ObjPtr<art::mirror::Class> klass)
REQUIRES_SHARED(art::Locks::mutator_lock_) { // Do we have a cached value?
IndexCache tmp; if (gIndexCachingTable.GetTag(klass.Ptr(), &tmp)) { return tmp.interface_fields;
}
art::Thread* self = art::Thread::Current();
art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
art::StackHandleScope<1> hs(self);
art::ObjPtr<art::mirror::Object> klass_ptr(soa.Decode<art::mirror::Class>(klass)); if (!klass_ptr->IsClass()) { return ERR(INVALID_CLASS);
}
art::Handle<art::mirror::Class> filter_klass(hs.NewHandle(klass_ptr->AsClass()));
ObjectTagTable* tag_table = ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get(); bool stop_reports = false; auto visitor = [&](art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) { // Early return, as we can't really stop visiting. if (stop_reports) { return;
}
jlong class_tag = 0;
art::ObjPtr<art::mirror::Class> klass = obj->GetClass();
tag_table->GetTag(klass.Ptr(), &class_tag); // For simplicity, even if we find a tag = 0, assume 0 = not tagged.
if (!heap_filter.ShouldReportByHeapFilter(tag, class_tag)) { return;
}
if (filter_klass != nullptr) { if (filter_klass.Get() != klass) { return;
}
}
void Work()
REQUIRES_SHARED(art::Locks::mutator_lock_)
REQUIRES(!*tag_table_->GetAllowDisallowLock()) { // Currently implemented as a BFS. To lower overhead, we don't erase elements immediately // from the head of the work list, instead postponing until there's a gap that's "large." // // Alternatively, we can implement a DFS and use the work list as a stack. while (start_ < worklist_.size()) {
art::mirror::Object* cur_obj = worklist_[start_];
start_++;
private: class CollectAndReportRootsVisitor final : public art::RootVisitor { public:
CollectAndReportRootsVisitor(FollowReferencesHelper* helper,
ObjectTagTable* tag_table,
std::vector<art::mirror::Object*>* worklist,
std::unordered_set<art::mirror::Object*>* visited)
: helper_(helper),
tag_table_(tag_table),
worklist_(worklist),
visited_(visited),
stop_reports_(false) {}
void VisitRoots(art::mirror::Object*** roots, size_t count, const art::RootInfo& info)
override
REQUIRES_SHARED(art::Locks::mutator_lock_)
REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) { for (size_t i = 0; i != count; ++i) {
AddRoot(*roots[i], info);
}
}
void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
size_t count, const art::RootInfo& info)
override REQUIRES_SHARED(art::Locks::mutator_lock_)
REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) { for (size_t i = 0; i != count; ++i) {
AddRoot(roots[i]->AsMirrorPtr(), info);
}
}
bool IsStopReports() { return stop_reports_;
}
private: void AddRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
REQUIRES_SHARED(art::Locks::mutator_lock_)
REQUIRES(!*tag_table_->GetAllowDisallowLock()) { if (stop_reports_) { return;
} bool add_to_worklist = ReportRoot(root_obj, info); // We use visited_ to mark roots already so we do not need another set. if (visited_->find(root_obj) == visited_->end()) { if (add_to_worklist) {
visited_->insert(root_obj);
worklist_->push_back(root_obj);
}
}
}
// Remove NO_THREAD_SAFETY_ANALYSIS once ASSERT_CAPABILITY works correctly.
art::Thread* FindThread(const art::RootInfo& info) NO_THREAD_SAFETY_ANALYSIS {
art::Locks::thread_list_lock_->AssertExclusiveHeld(art::Thread::Current()); return art::Runtime::Current()->GetThreadList()->FindThreadByThreadId(info.GetThreadId());
}
jvmtiHeapReferenceKind GetReferenceKind(const art::RootInfo& info,
jvmtiHeapReferenceInfo* ref_info)
REQUIRES_SHARED(art::Locks::mutator_lock_) { // We do not necessarily hold thread_list_lock_ here, but we may if we are called from // VisitThreadRoots, which can happen from JVMTI FollowReferences. If it was acquired in // ThreadList::VisitRoots, it's unsafe to temporarily release it. Thus we act as if we did // not hold the thread_list_lock_ here, and relax CHECKs appropriately. If it does happen, // we are in a SuspendAll situation with concurrent GC disabled, and should not need to run // flip functions. TODO: Find a way to clean this up.
// TODO: Fill in ref_info.
memset(ref_info, 0, sizeof(jvmtiHeapReferenceInfo));
switch (info.GetType()) { case art::RootType::kRootJNIGlobal: return JVMTI_HEAP_REFERENCE_JNI_GLOBAL;
case art::RootType::kRootJNILocal:
{
uint32_t thread_id = info.GetThreadId();
ref_info->jni_local.thread_id = thread_id;
case art::RootType::kRootNativeStack: case art::RootType::kRootThreadBlock: case art::RootType::kRootThreadObject: return JVMTI_HEAP_REFERENCE_THREAD;
case art::RootType::kRootStickyClass: case art::RootType::kRootInternedString: // Note: this isn't a root in the RI. return JVMTI_HEAP_REFERENCE_SYSTEM_CLASS;
case art::RootType::kRootMonitorUsed: case art::RootType::kRootJNIMonitor: return JVMTI_HEAP_REFERENCE_MONITOR;
case art::RootType::kRootFinalizing: case art::RootType::kRootDebugger: case art::RootType::kRootReferenceCleanup: case art::RootType::kRootVMInternal: case art::RootType::kRootUnknown: return JVMTI_HEAP_REFERENCE_OTHER;
}
LOG(FATAL) << "Unreachable";
UNREACHABLE();
}
void VisitClass(art::mirror::Class* klass)
REQUIRES_SHARED(art::Locks::mutator_lock_)
REQUIRES(!*tag_table_->GetAllowDisallowLock()) { // TODO: Are erroneous classes reported? Are non-prepared ones? For now, just use resolved ones. if (!klass->IsResolved()) { return;
}
// Directly implemented or extended interfaces.
art::Thread* self = art::Thread::Current();
art::StackHandleScope<1> hs(self);
art::Handle<art::mirror::Class> h_klass(hs.NewHandle<art::mirror::Class>(klass)); for (size_t i = 0; i < h_klass->NumDirectInterfaces(); ++i) {
art::ObjPtr<art::mirror::Class> inf_klass =
art::mirror::Class::ResolveDirectInterface(self, h_klass, i); if (inf_klass == nullptr) { // TODO: With a resolved class this should not happen...
self->ClearException(); break;
}
// Classloader. // TODO: What about the boot classpath loader? We'll skip for now, but do we have to find the // fake BootClassLoader? if (klass->GetClassLoader() != nullptr) {
stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS_LOADER,
nullptr,
klass,
klass->GetClassLoader().Ptr()); if (stop_reports_) { return;
}
}
DCHECK_EQ(h_klass.Get(), klass);
art::gc::Heap* heap = art::Runtime::Current()->GetHeap(); if (heap->IsGcConcurrentAndMoving()) { // Need to take a heap dump while GC isn't running. See the // comment in Heap::VisitObjects().
heap->IncrementDisableMovingGC(self);
}
{
art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
art::jni::ScopedEnableSuspendAllJniIdQueries sjni; // make sure we can get JNI ids.
art::ScopedThreadSuspension sts(self, art::ThreadState::kWaitingForVisitObjects);
art::ScopedSuspendAll ssa("FollowReferences");
if (!art::Locks::mutator_lock_->IsSharedHeld(self)) { if (!self->IsThreadSuspensionAllowable()) { return ERR(INTERNAL);
}
art::ScopedObjectAccess soa(self); return work();
} else { // We cannot use SOA in this case. We might be holding the lock, but may not be in the // runnable state (e.g., during GC).
art::Locks::mutator_lock_->AssertSharedHeld(self); // TODO: Investigate why ASSERT_SHARED_CAPABILITY doesn't work. auto annotalysis_workaround = [&]() NO_THREAD_SAFETY_ANALYSIS { return work();
}; return annotalysis_workaround();
}
}
using ObjectPtr = art::ObjPtr<art::mirror::Object>; using ObjectMap = std::unordered_map<ObjectPtr, ObjectPtr, art::HashObjPtr>;
staticvoid ReplaceObjectReferences(const ObjectMap& map)
REQUIRES(art::Locks::mutator_lock_,
art::Roles::uninterruptible_) {
art::Runtime::Current()->GetHeap()->VisitObjectsPaused(
[&](art::mirror::Object* ref) REQUIRES_SHARED(art::Locks::mutator_lock_) { // Rewrite all references in the object if needed. class ResizeReferenceVisitor { public: using CompressedObj = art::mirror::CompressedReference<art::mirror::Object>; explicit ResizeReferenceVisitor(const ObjectMap& map, ObjectPtr ref)
: map_(map), ref_(ref) {}
// Ignore class roots. void VisitRootIfNonNull(CompressedObj* root) const
REQUIRES_SHARED(art::Locks::mutator_lock_) { if (root != nullptr) {
VisitRoot(root);
}
} void VisitRoot(CompressedObj* root) const REQUIRES_SHARED(art::Locks::mutator_lock_) { auto it = map_.find(root->AsMirrorPtr()); if (it != map_.end()) {
root->Assign(it->second);
art::WriteBarrier::ForEveryFieldWrite(ref_);
}
}
voidoperator()(art::ObjPtr<art::mirror::Object> obj,
art::MemberOffset off, bool is_static) const
REQUIRES_SHARED(art::Locks::mutator_lock_) { auto it = map_.find(obj->GetFieldObject<art::mirror::Object>(off)); if (it != map_.end()) {
UNUSED(is_static); if (UNLIKELY(!is_static && off == art::mirror::Object::ClassOffset())) { // We don't want to update the declaring class of any objects. They will be replaced // in the heap and we need the declaring class to know its size. return;
} elseif (UNLIKELY(!is_static && off == art::mirror::Class::SuperClassOffset() &&
obj->IsClass())) { // We don't want to be messing with the class hierarcy either. return;
}
VLOG(plugin) << "Updating field at offset " << off.Uint32Value() << " of type "
<< obj->GetClass()->PrettyClass();
obj->SetFieldObject</*transaction*/ false>(off, it->second);
art::WriteBarrier::ForEveryFieldWrite(obj);
}
}
ResizeReferenceVisitor rrv(map, ref); if (ref->IsClass()) { // Class object native roots are the ArtField and ArtMethod 'declaring_class_' fields // which we don't want to be messing with as it would break ref-visitor assumptions about // what a class looks like. We want to keep the default behavior in other cases (such as // dex-cache) though. Unfortunately there is no way to tell from the visitor where exactly // the root came from. // TODO It might be nice to have the visitors told where the reference came from.
ref->VisitReferences</*kVisitNativeRoots*/false>(rrv, rrv);
} else {
ref->VisitReferences</*kVisitNativeRoots*/true>(rrv, rrv);
}
});
}
// TODO It's somewhat annoying to have to have this function implemented twice. It might be // good/useful to implement operator= for CompressedReference to allow us to use a template to // implement both of these. void VisitRoots(art::mirror::Object*** roots, size_t count, const art::RootInfo& info) override
REQUIRES_SHARED(art::Locks::mutator_lock_) {
art::mirror::Object*** end = roots + count; for (; roots != end; ++roots) {
art::mirror::Object** obj = *roots; auto it = map_.find(*obj); if (it != map_.end()) { // Java frames might have the JIT doing optimizations (for example loop-unrolling or // eliding bounds checks) so we need deopt them once we're done here. if (info.GetType() == art::RootType::kRootJavaFrame) { const art::JavaFrameRootInfo& jfri =
art::down_cast<const art::JavaFrameRootInfo&>(info); if (jfri.GetVReg() == art::JavaFrameRootInfo::kMethodDeclaringClass) {
info.Describe(VLOG_STREAM(plugin) << "Not changing declaring-class during stack"
<< " walk. Found obsolete java frame id "); continue;
} else {
info.Describe(VLOG_STREAM(plugin) << "Found java frame id ");
threads_with_roots_.insert(info.GetThreadId());
}
}
*obj = it->second.Ptr();
}
}
}
void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
size_t count, const art::RootInfo& info) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
art::mirror::CompressedReference<art::mirror::Object>** end = roots + count; for (; roots != end; ++roots) {
art::mirror::CompressedReference<art::mirror::Object>* obj = *roots; auto it = map_.find(obj->AsMirrorPtr()); if (it != map_.end()) { // Java frames might have the JIT doing optimizations (for example loop-unrolling or // eliding bounds checks) so we need deopt them once we're done here. if (info.GetType() == art::RootType::kRootJavaFrame) { const art::JavaFrameRootInfo& jfri =
art::down_cast<const art::JavaFrameRootInfo&>(info); if (jfri.GetVReg() == art::JavaFrameRootInfo::kMethodDeclaringClass) {
info.Describe(VLOG_STREAM(plugin) << "Not changing declaring-class during stack"
<< " walk. Found obsolete java frame id "); continue;
} else {
info.Describe(VLOG_STREAM(plugin) << "Found java frame id ");
threads_with_roots_.insert(info.GetThreadId());
}
}
obj->Assign(it->second);
}
}
}
private: const ObjectMap& map_;
std::unordered_set<uint32_t> threads_with_roots_;
};
ResizeRootVisitor rrv(map);
art::Runtime::Current()->VisitRoots(&rrv, art::VisitRootFlags::kVisitRootFlagAllRoots); // Handle java Frames. Annoyingly the JIT can embed information about the length of the array into // the compiled code. By changing the length of the array we potentially invalidate these // assumptions and so could cause (eg) OOB array access or other issues. if (!rrv.GetThreadsWithJavaFrameRoots().empty()) {
art::MutexLock mu(self, *art::Locks::thread_list_lock_);
art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList();
art::instrumentation::Instrumentation* instr = art::Runtime::Current()->GetInstrumentation(); for (uint32_t id : rrv.GetThreadsWithJavaFrameRoots()) {
art::Thread* t = thread_list->FindThreadByThreadId(id);
CHECK(t != nullptr) << "id " << id << " does not refer to a valid thread."
<< " Where did the roots come from?";
VLOG(plugin) << "Instrumenting thread stack of thread " << *t; // TODO Use deopt manager. We need a version that doesn't acquire all the locks we // already have. // TODO We technically only need to do this if the frames are not already being interpreted. // The cost for doing an extra stack walk is unlikely to be worth it though.
instr->InstrumentThreadStack(t, /* force_deopt= */ true);
}
}
}
staticvoid ReplaceWeakRoots(art::Thread* self,
EventHandler* event_handler, const ObjectMap& map)
REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) { // Handle tags. We want to do this seprately from other weak-refs (handled below) because we need // to send additional events and handle cases where the agent might have tagged the new // replacement object during the VMObjectAlloc. We do this by removing all tags associated with // both the obsolete and the new arrays. Then we send the ObsoleteObjectCreated event and cache // the new tag values. We next update all the other weak-references (the tags have been removed) // and finally update the tag table with the new values. Doing things in this way (1) keeps all // code relating to updating weak-references together and (2) ensures we don't end up in strange // situations where the order of weak-ref visiting affects the final tagging state. Since we have // the mutator_lock_ and gc-paused throughout this whole process no threads should be able to see // the interval where the objects are not tagged. struct NewTagValue { public:
ObjectPtr obsolete_obj_;
jlong obsolete_tag_;
ObjectPtr new_obj_;
jlong new_tag_;
};
// Map from the environment to the list of <obsolete_tag, new_tag> pairs that were changed.
std::unordered_map<ArtJvmTiEnv*, std::vector<NewTagValue>> changed_tags;
event_handler->ForEachEnv(self, [&](ArtJvmTiEnv* env) { // Cannot have REQUIRES(art::Locks::mutator_lock_) since ForEachEnv doesn't require it.
art::Locks::mutator_lock_->AssertExclusiveHeld(self);
env->object_tag_table->Lock(); // Get the tags and clear them (so we don't need to special-case the normal weak-ref visitor) for (auto it : map) {
jlong new_tag = 0;
jlong obsolete_tag = 0; bool had_obsolete_tag = env->object_tag_table->RemoveLocked(it.first, &obsolete_tag); bool had_new_tag = env->object_tag_table->RemoveLocked(it.second, &new_tag); // Dispatch event. if (had_obsolete_tag || had_new_tag) {
event_handler->DispatchEventOnEnv<ArtJvmtiEvent::kObsoleteObjectCreated>(
env, self, &obsolete_tag, &new_tag);
changed_tags.try_emplace(env).first->second.push_back(
{ it.first, obsolete_tag, it.second, new_tag });
}
} // After weak-ref update we need to go back and re-add obsoletes. We wait to avoid having to // deal with the visit-weaks overwriting the initial new_obj_ptr tag and generally making things // difficult.
env->object_tag_table->Unlock();
}); // Handle weak-refs. struct ReplaceWeaksVisitor : public art::IsMarkedVisitor { public:
ReplaceWeaksVisitor(const ObjectMap& map) : map_(map) {}
art::mirror::Object* IsMarked(art::mirror::Object* obj)
REQUIRES_SHARED(art::Locks::mutator_lock_) { auto it = map_.find(obj); if (it != map_.end()) { return it->second.Ptr();
} else { return obj;
}
}
private: const ObjectMap& map_;
};
ReplaceWeaksVisitor rwv(map);
art::Runtime* runtime = art::Runtime::Current();
runtime->SweepSystemWeaks(&rwv);
runtime->GetThreadList()->ClearInterpreterCaches(); // Re-add the object tags. At this point all weak-references to the old_obj_ptr are gone.
event_handler->ForEachEnv(self, [&](ArtJvmTiEnv* env) { // Cannot have REQUIRES(art::Locks::mutator_lock_) since ForEachEnv doesn't require it.
art::Locks::mutator_lock_->AssertExclusiveHeld(self);
env->object_tag_table->Lock(); auto it = changed_tags.find(env); if (it != changed_tags.end()) { for (const NewTagValue& v : it->second) {
env->object_tag_table->SetLocked(v.obsolete_obj_, v.obsolete_tag_);
env->object_tag_table->SetLocked(v.new_obj_, v.new_tag_);
}
}
env->object_tag_table->Unlock();
});
}
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.28Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.