Skip to content

Commit 4d86fcf

Browse files
kccarichardson
authored andcommitted
Add more debug code for google/sanitizers#1193 (getting desperate, not being able to reproduce it for a few months, but the users are seeing it)
mode debug code Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D84819
2 parents d1c8107 + 5874304 commit 4d86fcf

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

+36-1
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,25 @@ struct Allocator {
732732
return reinterpret_cast<AsanChunk *>(alloc_beg);
733733
}
734734

735+
AsanChunk *GetAsanChunkDebug(void *alloc_beg) {
736+
if (!alloc_beg) return nullptr;
737+
if (!allocator.FromPrimary(alloc_beg)) {
738+
uptr *meta = reinterpret_cast<uptr *>(allocator.GetMetaData(alloc_beg));
739+
AsanChunk *m = reinterpret_cast<AsanChunk *>(meta[1]);
740+
Printf("GetAsanChunkDebug1 alloc_beg %p meta %p m %p\n", alloc_beg, meta, m);
741+
return m;
742+
}
743+
uptr *alloc_magic = reinterpret_cast<uptr *>(alloc_beg);
744+
Printf(
745+
"GetAsanChunkDebug2 alloc_beg %p alloc_magic %p alloc_magic[0] %p "
746+
"alloc_magic[1] %p\n",
747+
alloc_beg, alloc_magic, alloc_magic[0], alloc_magic[1]);
748+
if (alloc_magic[0] == kAllocBegMagic)
749+
return reinterpret_cast<AsanChunk *>(alloc_magic[1]);
750+
return reinterpret_cast<AsanChunk *>(alloc_beg);
751+
}
752+
753+
735754
AsanChunk *GetAsanChunkByAddr(uptr p) {
736755
void *alloc_beg = allocator.GetBlockBegin(reinterpret_cast<void *>(p));
737756
return GetAsanChunk(alloc_beg);
@@ -744,6 +763,13 @@ struct Allocator {
744763
return GetAsanChunk(alloc_beg);
745764
}
746765

766+
AsanChunk *GetAsanChunkByAddrFastLockedDebug(uptr p) {
767+
void *alloc_beg =
768+
allocator.GetBlockBeginFastLockedDebug(reinterpret_cast<void *>(p));
769+
Printf("GetAsanChunkByAddrFastLockedDebug p %p alloc_beg %p\n", p, alloc_beg);
770+
return GetAsanChunkDebug(alloc_beg);
771+
}
772+
747773
uptr AllocationSize(uptr p) {
748774
AsanChunk *m = GetAsanChunkByAddr(p);
749775
if (!m) return 0;
@@ -1040,16 +1066,25 @@ uptr PointsIntoChunk(void* p) {
10401066
// Debug code. Delete once issue #1193 is chased down.
10411067
extern "C" SANITIZER_WEAK_ATTRIBUTE const char *__lsan_current_stage;
10421068

1069+
void GetUserBeginDebug(uptr chunk) {
1070+
Printf("GetUserBeginDebug1 chunk %p\n", chunk);
1071+
__asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLockedDebug(chunk);
1072+
Printf("GetUserBeginDebug2 m %p\n", m);
1073+
}
1074+
10431075
uptr GetUserBegin(uptr chunk) {
10441076
__asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLocked(chunk);
1045-
if (!m)
1077+
if (!m) {
10461078
Printf(
10471079
"ASAN is about to crash with a CHECK failure.\n"
10481080
"The ASAN developers are trying to chase down this bug,\n"
10491081
"so if you've encountered this bug please let us know.\n"
10501082
"See also: https://github.com/google/sanitizers/issues/1193\n"
1083+
"Internal ref b/149237057\n"
10511084
"chunk: %p caller %p __lsan_current_stage %s\n",
10521085
chunk, GET_CALLER_PC(), __lsan_current_stage);
1086+
GetUserBeginDebug(chunk);
1087+
}
10531088
CHECK(m);
10541089
return m->Beg();
10551090
}

compiler-rt/lib/sanitizer_common/sanitizer_allocator_combined.h

+7
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ class CombinedAllocator {
142142
return secondary_.GetBlockBeginFastLocked(p);
143143
}
144144

145+
void *GetBlockBeginFastLockedDebug(void *p) {
146+
if (primary_.PointerIsMine(p))
147+
return primary_.GetBlockBeginDebug(p);
148+
return secondary_.GetBlockBeginFastLocked(p);
149+
}
150+
151+
145152
usize GetActuallyAllocatedSize(void *p) {
146153
if (primary_.PointerIsMine(p))
147154
return primary_.GetActuallyAllocatedSize(p);

compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class SizeClassAllocator32 {
210210
uptr res = beg + (n * (u32)size);
211211
return reinterpret_cast<void*>(res);
212212
}
213+
void *GetBlockBeginDebug(const void *p) { return GetBlockBegin(p); }
213214

214215
usize GetActuallyAllocatedSize(void *p) {
215216
CHECK(PointerIsMine(p));

compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary64.h

+23
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,29 @@ class SizeClassAllocator64 {
199199
return nullptr;
200200
}
201201

202+
void *GetBlockBeginDebug(const void *p) {
203+
uptr class_id = GetSizeClass(p);
204+
uptr size = ClassIdToSize(class_id);
205+
Printf("GetBlockBeginDebug1 p %p class_id %p size %p\n", p, class_id, size);
206+
if (!size) return nullptr;
207+
uptr chunk_idx = GetChunkIdx((uptr)p, size);
208+
uptr reg_beg = GetRegionBegin(p);
209+
uptr beg = chunk_idx * size;
210+
uptr next_beg = beg + size;
211+
Printf(
212+
"GetBlockBeginDebug2 chunk_idx %p reg_beg %p beg %p next_beg %p "
213+
"kNumClasses %p\n",
214+
chunk_idx, reg_beg, beg, next_beg, kNumClasses);
215+
if (class_id >= kNumClasses) return nullptr;
216+
const RegionInfo *region = AddressSpaceView::Load(GetRegionInfo(class_id));
217+
Printf("GetBlockBeginDebug3 region %p region->mapped_user %p\n", region,
218+
region->mapped_user);
219+
if (region->mapped_user >= next_beg)
220+
return reinterpret_cast<void*>(reg_beg + beg);
221+
return nullptr;
222+
}
223+
224+
202225
usize GetActuallyAllocatedSize(void *p) {
203226
CHECK(PointerIsMine(p));
204227
return ClassIdToSize(GetSizeClass(p));

0 commit comments

Comments
 (0)