• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 "verification-inl.h"
18 
19 #include <iomanip>
20 #include <sstream>
21 
22 #include "art_field-inl.h"
23 #include "base/file_utils.h"
24 #include "base/logging.h"
25 #include "mirror/class-inl.h"
26 #include "mirror/object-refvisitor-inl.h"
27 
28 namespace art {
29 namespace gc {
30 
DumpRAMAroundAddress(uintptr_t addr,uintptr_t bytes) const31 std::string Verification::DumpRAMAroundAddress(uintptr_t addr, uintptr_t bytes) const {
32   uintptr_t* dump_start = reinterpret_cast<uintptr_t*>(addr - bytes);
33   uintptr_t* dump_end = reinterpret_cast<uintptr_t*>(addr + bytes);
34   std::ostringstream oss;
35   oss << " adjacent_ram=";
36   for (const uintptr_t* p = dump_start; p < dump_end; ++p) {
37     if (p == reinterpret_cast<uintptr_t*>(addr)) {
38       // Marker of where the address is.
39       oss << "|";
40     }
41     oss << std::hex << std::setfill('0') << std::setw(sizeof(uintptr_t) * 2) << *p << " ";
42   }
43   return oss.str();
44 }
45 
DumpObjectInfo(const void * addr,const char * tag) const46 std::string Verification::DumpObjectInfo(const void* addr, const char* tag) const {
47   std::ostringstream oss;
48   oss << tag << "=" << addr;
49   if (IsValidHeapObjectAddress(addr)) {
50     mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<void*>(addr));
51     mirror::Class* klass = obj->GetClass<kVerifyNone, kWithoutReadBarrier>();
52     oss << " klass=" << klass;
53     if (IsValidClass(klass)) {
54       oss << "(" << klass->PrettyClass() << ")";
55       if (klass->IsArrayClass<kVerifyNone>()) {
56         oss << " length=" << obj->AsArray<kVerifyNone>()->GetLength();
57       }
58     } else {
59       oss << " <invalid address>";
60     }
61     space::Space* const space = heap_->FindSpaceFromAddress(addr);
62     if (space != nullptr) {
63       oss << " space=" << *space;
64     }
65     accounting::CardTable* card_table = heap_->GetCardTable();
66     if (card_table->AddrIsInCardTable(addr)) {
67       oss << " card=" << static_cast<size_t>(
68           card_table->GetCard(reinterpret_cast<const mirror::Object*>(addr)));
69     }
70     // Dump adjacent RAM.
71     oss << DumpRAMAroundAddress(reinterpret_cast<uintptr_t>(addr), 4 * kObjectAlignment);
72   } else {
73     oss << " <invalid address>";
74   }
75   return oss.str();
76 }
77 
LogHeapCorruption(ObjPtr<mirror::Object> holder,MemberOffset offset,mirror::Object * ref,bool fatal) const78 void Verification::LogHeapCorruption(ObjPtr<mirror::Object> holder,
79                                      MemberOffset offset,
80                                      mirror::Object* ref,
81                                      bool fatal) const {
82   // Highest priority logging first.
83   // Buffer the output in the string stream since it is more important than the stack traces
84   // and we want it to have log priority. The stack traces are printed from Runtime::Abort
85   // which is called from LOG(FATAL) but before the abort message.
86   std::ostringstream oss;
87   oss << "GC tried to mark invalid reference " << ref << std::endl;
88   oss << DumpObjectInfo(ref, "ref") << "\n";
89   oss << DumpObjectInfo(holder.Ptr(), "holder") << "\n";
90   if (holder != nullptr) {
91     mirror::Class* holder_klass = holder->GetClass<kVerifyNone, kWithoutReadBarrier>();
92     if (IsValidClass(holder_klass)) {
93       oss << " field_offset=" << offset.Uint32Value();
94       ArtField* field = holder->FindFieldByOffset(offset);
95       if (field != nullptr) {
96         oss << " name=" << field->GetName();
97       }
98     }
99     mirror::HeapReference<mirror::Object>* addr = holder->GetFieldObjectReferenceAddr(offset);
100     oss << " reference addr"
101         << DumpRAMAroundAddress(reinterpret_cast<uintptr_t>(addr), 4 * kObjectAlignment);
102   }
103   Runtime::Current()->GetHeap()->DumpSpaces(oss);
104   MemMap::DumpMaps(oss, /* terse= */ true);
105 
106   if (fatal) {
107     LOG(FATAL) << oss.str();
108   } else {
109     LOG(FATAL_WITHOUT_ABORT) << oss.str();
110   }
111 }
112 
IsAddressInHeapSpace(const void * addr,space::Space ** out_space) const113 bool Verification::IsAddressInHeapSpace(const void* addr, space::Space** out_space) const {
114   space::Space* const space = heap_->FindSpaceFromAddress(addr);
115   if (space != nullptr) {
116     if (out_space != nullptr) {
117       *out_space = space;
118     }
119     return true;
120   }
121   return false;
122 }
123 
IsValidHeapObjectAddress(const void * addr,space::Space ** out_space) const124 bool Verification::IsValidHeapObjectAddress(const void* addr, space::Space** out_space) const {
125   return IsAligned<kObjectAlignment>(addr) && IsAddressInHeapSpace(addr, out_space);
126 }
127 
128 using ObjectSet = std::set<mirror::Object*>;
129 using WorkQueue = std::deque<std::pair<mirror::Object*, std::string>>;
130 
131 // Use for visiting the GcRoots held live by ArtFields, ArtMethods, and ClassLoaders.
132 class Verification::BFSFindReachable {
133  public:
BFSFindReachable(ObjectSet * visited)134   explicit BFSFindReachable(ObjectSet* visited) : visited_(visited) {}
135 
operator ()(mirror::Object * obj,MemberOffset offset,bool is_static ATTRIBUTE_UNUSED) const136   void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const
137       REQUIRES_SHARED(Locks::mutator_lock_) {
138     ArtField* field = obj->FindFieldByOffset(offset);
139     Visit(obj->GetFieldObject<mirror::Object>(offset),
140           field != nullptr ? field->GetName() : "");
141   }
142 
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const143   void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
144       REQUIRES_SHARED(Locks::mutator_lock_) {
145     if (!root->IsNull()) {
146       VisitRoot(root);
147     }
148   }
149 
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const150   void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
151       REQUIRES_SHARED(Locks::mutator_lock_) {
152     Visit(root->AsMirrorPtr(), "!nativeRoot");
153   }
154 
Visit(mirror::Object * ref,const std::string & field_name) const155   void Visit(mirror::Object* ref, const std::string& field_name) const
156       REQUIRES_SHARED(Locks::mutator_lock_) {
157     if (ref != nullptr && visited_->insert(ref).second) {
158       new_visited_.emplace_back(ref, field_name);
159     }
160   }
161 
NewlyVisited() const162   const WorkQueue& NewlyVisited() const {
163     return new_visited_;
164   }
165 
166  private:
167   ObjectSet* visited_;
168   mutable WorkQueue new_visited_;
169 };
170 
171 class Verification::CollectRootVisitor : public SingleRootVisitor {
172  public:
CollectRootVisitor(ObjectSet * visited,WorkQueue * work)173   CollectRootVisitor(ObjectSet* visited, WorkQueue* work) : visited_(visited), work_(work) {}
174 
VisitRoot(mirror::Object * obj,const RootInfo & info)175   void VisitRoot(mirror::Object* obj, const RootInfo& info)
176       override REQUIRES_SHARED(Locks::mutator_lock_) {
177     if (obj != nullptr && visited_->insert(obj).second) {
178       std::ostringstream oss;
179       oss << info.ToString() << " = " << obj << "(" << obj->PrettyTypeOf() << ")";
180       work_->emplace_back(obj, oss.str());
181     }
182   }
183 
184  private:
185   ObjectSet* const visited_;
186   WorkQueue* const work_;
187 };
188 
FirstPathFromRootSet(ObjPtr<mirror::Object> target) const189 std::string Verification::FirstPathFromRootSet(ObjPtr<mirror::Object> target) const {
190   Runtime* const runtime =  Runtime::Current();
191   std::set<mirror::Object*> visited;
192   std::deque<std::pair<mirror::Object*, std::string>> work;
193   {
194     CollectRootVisitor root_visitor(&visited, &work);
195     runtime->VisitRoots(&root_visitor, kVisitRootFlagAllRoots);
196   }
197   while (!work.empty()) {
198     auto pair = work.front();
199     work.pop_front();
200     if (pair.first == target) {
201       return pair.second;
202     }
203     BFSFindReachable visitor(&visited);
204     pair.first->VisitReferences(visitor, VoidFunctor());
205     for (auto&& pair2 : visitor.NewlyVisited()) {
206       std::ostringstream oss;
207       mirror::Object* obj = pair2.first;
208       oss << pair.second << " -> " << obj << "(" << obj->PrettyTypeOf() << ")." << pair2.second;
209       work.emplace_back(obj, oss.str());
210     }
211   }
212   return "<no path found>";
213 }
214 
215 }  // namespace gc
216 }  // namespace art
217