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