1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "verification-inl.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <iomanip>
20*795d594fSAndroid Build Coastguard Worker #include <sstream>
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker #include "art_field-inl.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"
27*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
28*795d594fSAndroid Build Coastguard Worker #include "mirror/object-refvisitor-inl.h"
29*795d594fSAndroid Build Coastguard Worker
30*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
31*795d594fSAndroid Build Coastguard Worker namespace gc {
32*795d594fSAndroid Build Coastguard Worker
DumpRAMAroundAddress(uintptr_t addr,uintptr_t bytes) const33*795d594fSAndroid Build Coastguard Worker std::string Verification::DumpRAMAroundAddress(uintptr_t addr, uintptr_t bytes) const {
34*795d594fSAndroid Build Coastguard Worker uintptr_t* dump_start = reinterpret_cast<uintptr_t*>(addr - bytes);
35*795d594fSAndroid Build Coastguard Worker uintptr_t* dump_end = reinterpret_cast<uintptr_t*>(addr + bytes);
36*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
37*795d594fSAndroid Build Coastguard Worker oss << " adjacent_ram=";
38*795d594fSAndroid Build Coastguard Worker
39*795d594fSAndroid Build Coastguard Worker {
40*795d594fSAndroid Build Coastguard Worker // Check if the RAM is accessible.
41*795d594fSAndroid Build Coastguard Worker android::base::unique_fd read_fd, write_fd;
42*795d594fSAndroid Build Coastguard Worker if (!android::base::Pipe(&read_fd, &write_fd)) {
43*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not create pipe, RAM being dumped may be unaccessible";
44*795d594fSAndroid Build Coastguard Worker } else {
45*795d594fSAndroid Build Coastguard Worker size_t count = 2 * bytes;
46*795d594fSAndroid Build Coastguard Worker if (write(write_fd.get(), dump_start, count) != static_cast<ssize_t>(count)) {
47*795d594fSAndroid Build Coastguard Worker oss << "unaccessible";
48*795d594fSAndroid Build Coastguard Worker dump_start = dump_end;
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker
53*795d594fSAndroid Build Coastguard Worker for (const uintptr_t* p = dump_start; p < dump_end; ++p) {
54*795d594fSAndroid Build Coastguard Worker if (p == reinterpret_cast<uintptr_t*>(addr)) {
55*795d594fSAndroid Build Coastguard Worker // Marker of where the address is.
56*795d594fSAndroid Build Coastguard Worker oss << "|";
57*795d594fSAndroid Build Coastguard Worker }
58*795d594fSAndroid Build Coastguard Worker oss << std::hex << std::setfill('0') << std::setw(sizeof(uintptr_t) * 2) << *p << " ";
59*795d594fSAndroid Build Coastguard Worker }
60*795d594fSAndroid Build Coastguard Worker return oss.str();
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker
DumpObjectInfo(const void * addr,const char * tag) const63*795d594fSAndroid Build Coastguard Worker std::string Verification::DumpObjectInfo(const void* addr, const char* tag) const {
64*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
65*795d594fSAndroid Build Coastguard Worker oss << tag << "=" << addr;
66*795d594fSAndroid Build Coastguard Worker if (IsValidHeapObjectAddress(addr)) {
67*795d594fSAndroid Build Coastguard Worker mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<void*>(addr));
68*795d594fSAndroid Build Coastguard Worker mirror::Class* klass = obj->GetClass<kVerifyNone, kWithoutReadBarrier>();
69*795d594fSAndroid Build Coastguard Worker oss << " klass=" << klass;
70*795d594fSAndroid Build Coastguard Worker if (IsValidClass(klass)) {
71*795d594fSAndroid Build Coastguard Worker oss << "(" << klass->PrettyClass() << ")";
72*795d594fSAndroid Build Coastguard Worker if (klass->IsArrayClass<kVerifyNone>()) {
73*795d594fSAndroid Build Coastguard Worker oss << " length=" << obj->AsArray<kVerifyNone>()->GetLength();
74*795d594fSAndroid Build Coastguard Worker }
75*795d594fSAndroid Build Coastguard Worker } else {
76*795d594fSAndroid Build Coastguard Worker oss << " <invalid address>";
77*795d594fSAndroid Build Coastguard Worker }
78*795d594fSAndroid Build Coastguard Worker space::Space* const space = heap_->FindSpaceFromAddress(addr);
79*795d594fSAndroid Build Coastguard Worker if (space != nullptr) {
80*795d594fSAndroid Build Coastguard Worker oss << " space=" << *space;
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker accounting::CardTable* card_table = heap_->GetCardTable();
83*795d594fSAndroid Build Coastguard Worker if (card_table->AddrIsInCardTable(addr)) {
84*795d594fSAndroid Build Coastguard Worker oss << " card=" << static_cast<size_t>(
85*795d594fSAndroid Build Coastguard Worker card_table->GetCard(reinterpret_cast<const mirror::Object*>(addr)));
86*795d594fSAndroid Build Coastguard Worker }
87*795d594fSAndroid Build Coastguard Worker // Dump adjacent RAM.
88*795d594fSAndroid Build Coastguard Worker oss << DumpRAMAroundAddress(reinterpret_cast<uintptr_t>(addr), 4 * kObjectAlignment);
89*795d594fSAndroid Build Coastguard Worker } else {
90*795d594fSAndroid Build Coastguard Worker oss << " <invalid address>";
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker return oss.str();
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker
LogHeapCorruption(ObjPtr<mirror::Object> holder,MemberOffset offset,mirror::Object * ref,bool fatal) const95*795d594fSAndroid Build Coastguard Worker void Verification::LogHeapCorruption(ObjPtr<mirror::Object> holder,
96*795d594fSAndroid Build Coastguard Worker MemberOffset offset,
97*795d594fSAndroid Build Coastguard Worker mirror::Object* ref,
98*795d594fSAndroid Build Coastguard Worker bool fatal) const {
99*795d594fSAndroid Build Coastguard Worker // Highest priority logging first.
100*795d594fSAndroid Build Coastguard Worker // Buffer the output in the string stream since it is more important than the stack traces
101*795d594fSAndroid Build Coastguard Worker // and we want it to have log priority. The stack traces are printed from Runtime::Abort
102*795d594fSAndroid Build Coastguard Worker // which is called from LOG(FATAL) but before the abort message.
103*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
104*795d594fSAndroid Build Coastguard Worker oss << "GC tried to mark invalid reference " << ref << std::endl;
105*795d594fSAndroid Build Coastguard Worker oss << DumpObjectInfo(ref, "ref") << "\n";
106*795d594fSAndroid Build Coastguard Worker oss << DumpObjectInfo(holder.Ptr(), "holder") << "\n";
107*795d594fSAndroid Build Coastguard Worker if (holder != nullptr) {
108*795d594fSAndroid Build Coastguard Worker mirror::Class* holder_klass = holder->GetClass<kVerifyNone, kWithoutReadBarrier>();
109*795d594fSAndroid Build Coastguard Worker if (IsValidClass(holder_klass)) {
110*795d594fSAndroid Build Coastguard Worker oss << " field_offset=" << offset.Uint32Value();
111*795d594fSAndroid Build Coastguard Worker ArtField* field = holder->FindFieldByOffset(offset);
112*795d594fSAndroid Build Coastguard Worker if (field != nullptr) {
113*795d594fSAndroid Build Coastguard Worker oss << " name=" << field->GetName();
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker }
116*795d594fSAndroid Build Coastguard Worker mirror::HeapReference<mirror::Object>* addr = holder->GetFieldObjectReferenceAddr(offset);
117*795d594fSAndroid Build Coastguard Worker oss << " reference addr"
118*795d594fSAndroid Build Coastguard Worker << DumpRAMAroundAddress(reinterpret_cast<uintptr_t>(addr), 4 * kObjectAlignment);
119*795d594fSAndroid Build Coastguard Worker }
120*795d594fSAndroid Build Coastguard Worker Runtime::Current()->GetHeap()->DumpSpaces(oss);
121*795d594fSAndroid Build Coastguard Worker MemMap::DumpMaps(oss, /* terse= */ true);
122*795d594fSAndroid Build Coastguard Worker
123*795d594fSAndroid Build Coastguard Worker if (fatal) {
124*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << oss.str();
125*795d594fSAndroid Build Coastguard Worker } else {
126*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << oss.str();
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker
IsAddressInHeapSpace(const void * addr,space::Space ** out_space) const130*795d594fSAndroid Build Coastguard Worker bool Verification::IsAddressInHeapSpace(const void* addr, space::Space** out_space) const {
131*795d594fSAndroid Build Coastguard Worker space::Space* const space = heap_->FindSpaceFromAddress(addr);
132*795d594fSAndroid Build Coastguard Worker if (space != nullptr) {
133*795d594fSAndroid Build Coastguard Worker if (out_space != nullptr) {
134*795d594fSAndroid Build Coastguard Worker *out_space = space;
135*795d594fSAndroid Build Coastguard Worker }
136*795d594fSAndroid Build Coastguard Worker return true;
137*795d594fSAndroid Build Coastguard Worker }
138*795d594fSAndroid Build Coastguard Worker return false;
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker
IsValidHeapObjectAddress(const void * addr,space::Space ** out_space) const141*795d594fSAndroid Build Coastguard Worker bool Verification::IsValidHeapObjectAddress(const void* addr, space::Space** out_space) const {
142*795d594fSAndroid Build Coastguard Worker return IsAligned<kObjectAlignment>(addr) && IsAddressInHeapSpace(addr, out_space);
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker
145*795d594fSAndroid Build Coastguard Worker using ObjectSet = std::set<mirror::Object*>;
146*795d594fSAndroid Build Coastguard Worker using WorkQueue = std::deque<std::pair<mirror::Object*, std::string>>;
147*795d594fSAndroid Build Coastguard Worker
148*795d594fSAndroid Build Coastguard Worker // Use for visiting the GcRoots held live by ArtFields, ArtMethods, and ClassLoaders.
149*795d594fSAndroid Build Coastguard Worker class Verification::BFSFindReachable {
150*795d594fSAndroid Build Coastguard Worker public:
BFSFindReachable(ObjectSet * visited)151*795d594fSAndroid Build Coastguard Worker explicit BFSFindReachable(ObjectSet* visited) : visited_(visited) {}
152*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * obj,MemberOffset offset,bool is_static) const153*795d594fSAndroid Build Coastguard Worker void operator()(mirror::Object* obj, MemberOffset offset, [[maybe_unused]] bool is_static) const
154*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
155*795d594fSAndroid Build Coastguard Worker ArtField* field = obj->FindFieldByOffset(offset);
156*795d594fSAndroid Build Coastguard Worker Visit(obj->GetFieldObject<mirror::Object>(offset),
157*795d594fSAndroid Build Coastguard Worker field != nullptr ? field->GetName() : "");
158*795d594fSAndroid Build Coastguard Worker }
159*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const160*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
161*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
162*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
163*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
164*795d594fSAndroid Build Coastguard Worker }
165*795d594fSAndroid Build Coastguard Worker }
166*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const167*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
168*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
169*795d594fSAndroid Build Coastguard Worker Visit(root->AsMirrorPtr(), "!nativeRoot");
170*795d594fSAndroid Build Coastguard Worker }
171*795d594fSAndroid Build Coastguard Worker
Visit(mirror::Object * ref,const std::string & field_name) const172*795d594fSAndroid Build Coastguard Worker void Visit(mirror::Object* ref, const std::string& field_name) const
173*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
174*795d594fSAndroid Build Coastguard Worker if (ref != nullptr && visited_->insert(ref).second) {
175*795d594fSAndroid Build Coastguard Worker new_visited_.emplace_back(ref, field_name);
176*795d594fSAndroid Build Coastguard Worker }
177*795d594fSAndroid Build Coastguard Worker }
178*795d594fSAndroid Build Coastguard Worker
NewlyVisited() const179*795d594fSAndroid Build Coastguard Worker const WorkQueue& NewlyVisited() const {
180*795d594fSAndroid Build Coastguard Worker return new_visited_;
181*795d594fSAndroid Build Coastguard Worker }
182*795d594fSAndroid Build Coastguard Worker
183*795d594fSAndroid Build Coastguard Worker private:
184*795d594fSAndroid Build Coastguard Worker ObjectSet* visited_;
185*795d594fSAndroid Build Coastguard Worker mutable WorkQueue new_visited_;
186*795d594fSAndroid Build Coastguard Worker };
187*795d594fSAndroid Build Coastguard Worker
188*795d594fSAndroid Build Coastguard Worker class Verification::CollectRootVisitor : public SingleRootVisitor {
189*795d594fSAndroid Build Coastguard Worker public:
CollectRootVisitor(ObjectSet * visited,WorkQueue * work)190*795d594fSAndroid Build Coastguard Worker CollectRootVisitor(ObjectSet* visited, WorkQueue* work) : visited_(visited), work_(work) {}
191*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::Object * obj,const RootInfo & info)192*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::Object* obj, const RootInfo& info)
193*795d594fSAndroid Build Coastguard Worker override REQUIRES_SHARED(Locks::mutator_lock_) {
194*795d594fSAndroid Build Coastguard Worker if (obj != nullptr && visited_->insert(obj).second) {
195*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
196*795d594fSAndroid Build Coastguard Worker oss << info.ToString() << " = " << obj << "(" << obj->PrettyTypeOf() << ")";
197*795d594fSAndroid Build Coastguard Worker work_->emplace_back(obj, oss.str());
198*795d594fSAndroid Build Coastguard Worker }
199*795d594fSAndroid Build Coastguard Worker }
200*795d594fSAndroid Build Coastguard Worker
201*795d594fSAndroid Build Coastguard Worker private:
202*795d594fSAndroid Build Coastguard Worker ObjectSet* const visited_;
203*795d594fSAndroid Build Coastguard Worker WorkQueue* const work_;
204*795d594fSAndroid Build Coastguard Worker };
205*795d594fSAndroid Build Coastguard Worker
FirstPathFromRootSet(ObjPtr<mirror::Object> target) const206*795d594fSAndroid Build Coastguard Worker std::string Verification::FirstPathFromRootSet(ObjPtr<mirror::Object> target) const {
207*795d594fSAndroid Build Coastguard Worker Runtime* const runtime = Runtime::Current();
208*795d594fSAndroid Build Coastguard Worker std::set<mirror::Object*> visited;
209*795d594fSAndroid Build Coastguard Worker std::deque<std::pair<mirror::Object*, std::string>> work;
210*795d594fSAndroid Build Coastguard Worker {
211*795d594fSAndroid Build Coastguard Worker CollectRootVisitor root_visitor(&visited, &work);
212*795d594fSAndroid Build Coastguard Worker runtime->VisitRoots(&root_visitor, kVisitRootFlagAllRoots);
213*795d594fSAndroid Build Coastguard Worker }
214*795d594fSAndroid Build Coastguard Worker while (!work.empty()) {
215*795d594fSAndroid Build Coastguard Worker auto pair = work.front();
216*795d594fSAndroid Build Coastguard Worker work.pop_front();
217*795d594fSAndroid Build Coastguard Worker if (pair.first == target) {
218*795d594fSAndroid Build Coastguard Worker return pair.second;
219*795d594fSAndroid Build Coastguard Worker }
220*795d594fSAndroid Build Coastguard Worker BFSFindReachable visitor(&visited);
221*795d594fSAndroid Build Coastguard Worker pair.first->VisitReferences(visitor, VoidFunctor());
222*795d594fSAndroid Build Coastguard Worker for (auto&& pair2 : visitor.NewlyVisited()) {
223*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
224*795d594fSAndroid Build Coastguard Worker mirror::Object* obj = pair2.first;
225*795d594fSAndroid Build Coastguard Worker oss << pair.second << " -> " << obj << "(" << obj->PrettyTypeOf() << ")." << pair2.second;
226*795d594fSAndroid Build Coastguard Worker work.emplace_back(obj, oss.str());
227*795d594fSAndroid Build Coastguard Worker }
228*795d594fSAndroid Build Coastguard Worker }
229*795d594fSAndroid Build Coastguard Worker return "<no path found>";
230*795d594fSAndroid Build Coastguard Worker }
231*795d594fSAndroid Build Coastguard Worker
232*795d594fSAndroid Build Coastguard Worker } // namespace gc
233*795d594fSAndroid Build Coastguard Worker } // namespace art
234