• 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.h"
18 
19 #include <iomanip>
20 #include <sstream>
21 
22 #include "art_field-inl.h"
23 #include "mirror/class-inl.h"
24 #include "mirror/object-refvisitor-inl.h"
25 
26 namespace art {
27 namespace gc {
28 
DumpRAMAroundAddress(uintptr_t addr,uintptr_t bytes) const29 std::string Verification::DumpRAMAroundAddress(uintptr_t addr, uintptr_t bytes) const {
30   const uintptr_t dump_start = addr - bytes;
31   const uintptr_t dump_end = addr + bytes;
32   std::ostringstream oss;
33   if (dump_start < dump_end &&
34       IsAddressInHeapSpace(reinterpret_cast<const void*>(dump_start)) &&
35       IsAddressInHeapSpace(reinterpret_cast<const void*>(dump_end - 1))) {
36     oss << " adjacent_ram=";
37     for (uintptr_t p = dump_start; p < dump_end; ++p) {
38       if (p == addr) {
39         // Marker of where the address is.
40         oss << "|";
41       }
42       uint8_t* ptr = reinterpret_cast<uint8_t*>(p);
43       oss << std::hex << std::setfill('0') << std::setw(2) << static_cast<uintptr_t>(*ptr);
44     }
45   } else {
46     oss << " <invalid address>";
47   }
48   return oss.str();
49 }
50 
DumpObjectInfo(const void * addr,const char * tag) const51 std::string Verification::DumpObjectInfo(const void* addr, const char* tag) const {
52   std::ostringstream oss;
53   oss << tag << "=" << addr;
54   if (IsValidHeapObjectAddress(addr)) {
55     mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<void*>(addr));
56     mirror::Class* klass = obj->GetClass<kVerifyNone, kWithoutReadBarrier>();
57     oss << " klass=" << klass;
58     if (IsValidClass(klass)) {
59       oss << "(" << klass->PrettyClass() << ")";
60       if (klass->IsArrayClass<kVerifyNone, kWithoutReadBarrier>()) {
61         oss << " length=" << obj->AsArray<kVerifyNone, kWithoutReadBarrier>()->GetLength();
62       }
63     } else {
64       oss << " <invalid address>";
65     }
66     space::Space* const space = heap_->FindSpaceFromAddress(addr);
67     if (space != nullptr) {
68       oss << " space=" << *space;
69     }
70     accounting::CardTable* card_table = heap_->GetCardTable();
71     if (card_table->AddrIsInCardTable(addr)) {
72       oss << " card=" << static_cast<size_t>(
73           card_table->GetCard(reinterpret_cast<const mirror::Object*>(addr)));
74     }
75     // Dump adjacent RAM.
76     oss << DumpRAMAroundAddress(reinterpret_cast<uintptr_t>(addr), 4 * kObjectAlignment);
77   } else {
78     oss << " <invalid address>";
79   }
80   return oss.str();
81 }
82 
LogHeapCorruption(ObjPtr<mirror::Object> holder,MemberOffset offset,mirror::Object * ref,bool fatal) const83 void Verification::LogHeapCorruption(ObjPtr<mirror::Object> holder,
84                                      MemberOffset offset,
85                                      mirror::Object* ref,
86                                      bool fatal) const {
87   // Lowest priority logging first:
88   PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT);
89   MemMap::DumpMaps(LOG_STREAM(FATAL_WITHOUT_ABORT), true);
90   // Buffer the output in the string stream since it is more important than the stack traces
91   // and we want it to have log priority. The stack traces are printed from Runtime::Abort
92   // which is called from LOG(FATAL) but before the abort message.
93   std::ostringstream oss;
94   oss << "GC tried to mark invalid reference " << ref << std::endl;
95   oss << DumpObjectInfo(ref, "ref") << "\n";
96   oss << DumpObjectInfo(holder.Ptr(), "holder");
97   if (holder != nullptr) {
98     mirror::Class* holder_klass = holder->GetClass<kVerifyNone, kWithoutReadBarrier>();
99     if (IsValidClass(holder_klass)) {
100       oss << " field_offset=" << offset.Uint32Value();
101       ArtField* field = holder->FindFieldByOffset(offset);
102       if (field != nullptr) {
103         oss << " name=" << field->GetName();
104       }
105     }
106     mirror::HeapReference<mirror::Object>* addr = holder->GetFieldObjectReferenceAddr(offset);
107     oss << " reference addr"
108         << DumpRAMAroundAddress(reinterpret_cast<uintptr_t>(addr), 4 * kObjectAlignment);
109   }
110 
111   if (fatal) {
112     LOG(FATAL) << oss.str();
113   } else {
114     LOG(FATAL_WITHOUT_ABORT) << oss.str();
115   }
116 }
117 
IsAddressInHeapSpace(const void * addr,space::Space ** out_space) const118 bool Verification::IsAddressInHeapSpace(const void* addr, space::Space** out_space) const {
119   space::Space* const space = heap_->FindSpaceFromAddress(addr);
120   if (space != nullptr) {
121     if (out_space != nullptr) {
122       *out_space = space;
123     }
124     return true;
125   }
126   return false;
127 }
128 
IsValidHeapObjectAddress(const void * addr,space::Space ** out_space) const129 bool Verification::IsValidHeapObjectAddress(const void* addr, space::Space** out_space) const {
130   return IsAligned<kObjectAlignment>(addr) && IsAddressInHeapSpace(addr, out_space);
131 }
132 
IsValidClass(const void * addr) const133 bool Verification::IsValidClass(const void* addr) const {
134   if (!IsValidHeapObjectAddress(addr)) {
135     return false;
136   }
137   mirror::Class* klass = reinterpret_cast<mirror::Class*>(const_cast<void*>(addr));
138   mirror::Class* k1 = klass->GetClass<kVerifyNone, kWithoutReadBarrier>();
139   if (!IsValidHeapObjectAddress(k1)) {
140     return false;
141   }
142   // k should be class class, take the class again to verify.
143   // Note that this check may not be valid for the no image space since the class class might move
144   // around from moving GC.
145   mirror::Class* k2 = k1->GetClass<kVerifyNone, kWithoutReadBarrier>();
146   if (!IsValidHeapObjectAddress(k2)) {
147     return false;
148   }
149   return k1 == k2;
150 }
151 
152 using ObjectSet = std::set<mirror::Object*>;
153 using WorkQueue = std::deque<std::pair<mirror::Object*, std::string>>;
154 
155 // Use for visiting the GcRoots held live by ArtFields, ArtMethods, and ClassLoaders.
156 class Verification::BFSFindReachable {
157  public:
BFSFindReachable(ObjectSet * visited)158   explicit BFSFindReachable(ObjectSet* visited) : visited_(visited) {}
159 
operator ()(mirror::Object * obj,MemberOffset offset,bool is_static ATTRIBUTE_UNUSED) const160   void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const
161       REQUIRES_SHARED(Locks::mutator_lock_) {
162     ArtField* field = obj->FindFieldByOffset(offset);
163     Visit(obj->GetFieldObject<mirror::Object>(offset),
164           field != nullptr ? field->GetName() : "");
165   }
166 
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const167   void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
168       REQUIRES_SHARED(Locks::mutator_lock_) {
169     if (!root->IsNull()) {
170       VisitRoot(root);
171     }
172   }
173 
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const174   void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
175       REQUIRES_SHARED(Locks::mutator_lock_) {
176     Visit(root->AsMirrorPtr(), "!nativeRoot");
177   }
178 
Visit(mirror::Object * ref,const std::string & field_name) const179   void Visit(mirror::Object* ref, const std::string& field_name) const
180       REQUIRES_SHARED(Locks::mutator_lock_) {
181     if (ref != nullptr && visited_->insert(ref).second) {
182       new_visited_.emplace_back(ref, field_name);
183     }
184   }
185 
NewlyVisited() const186   const WorkQueue& NewlyVisited() const {
187     return new_visited_;
188   }
189 
190  private:
191   ObjectSet* visited_;
192   mutable WorkQueue new_visited_;
193 };
194 
195 class Verification::CollectRootVisitor : public SingleRootVisitor {
196  public:
CollectRootVisitor(ObjectSet * visited,WorkQueue * work)197   CollectRootVisitor(ObjectSet* visited, WorkQueue* work) : visited_(visited), work_(work) {}
198 
VisitRoot(mirror::Object * obj,const RootInfo & info)199   void VisitRoot(mirror::Object* obj, const RootInfo& info)
200       OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
201     if (obj != nullptr && visited_->insert(obj).second) {
202       std::ostringstream oss;
203       oss << info.ToString() << " = " << obj << "(" << obj->PrettyTypeOf() << ")";
204       work_->emplace_back(obj, oss.str());
205     }
206   }
207 
208  private:
209   ObjectSet* const visited_;
210   WorkQueue* const work_;
211 };
212 
FirstPathFromRootSet(ObjPtr<mirror::Object> target) const213 std::string Verification::FirstPathFromRootSet(ObjPtr<mirror::Object> target) const {
214   Runtime* const runtime =  Runtime::Current();
215   std::set<mirror::Object*> visited;
216   std::deque<std::pair<mirror::Object*, std::string>> work;
217   {
218     CollectRootVisitor root_visitor(&visited, &work);
219     runtime->VisitRoots(&root_visitor, kVisitRootFlagAllRoots);
220   }
221   while (!work.empty()) {
222     auto pair = work.front();
223     work.pop_front();
224     if (pair.first == target) {
225       return pair.second;
226     }
227     BFSFindReachable visitor(&visited);
228     pair.first->VisitReferences(visitor, VoidFunctor());
229     for (auto&& pair2 : visitor.NewlyVisited()) {
230       std::ostringstream oss;
231       mirror::Object* obj = pair2.first;
232       oss << pair.second << " -> " << obj << "(" << obj->PrettyTypeOf() << ")." << pair2.second;
233       work.emplace_back(obj, oss.str());
234     }
235   }
236   return "<no path found>";
237 }
238 
239 }  // namespace gc
240 }  // namespace art
241