Skip to content

Commit c7f240d

Browse files
committed
add some assertions
1 parent eedddb9 commit c7f240d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

compiler-rt/lib/tsan/rtl/tsan_rtl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,16 @@ void UnmapShadow(ThreadState* thr, uptr addr, uptr size) {
582582
#endif
583583

584584
void MapShadow(uptr addr, uptr size) {
585+
// Although named MapShadow, this function's semantic is unrelated to
586+
// UnmapShadow. This function currently only used for Go's lazy allocation
587+
// of shadow, whose targets are program section (e.g., bss, data, etc.).
588+
// Therefore, we can guarantee that the addr and size align to kShadowCell
589+
// and kMetaShadowCell by the following assertions.
590+
DCHECK_EQ(addr % kShadowCell, 0);
591+
DCHECK_EQ(size % kShadowCell, 0);
592+
DCHECK_EQ(addr % kMetaShadowCell, 0);
593+
DCHECK_EQ(size % kMetaShadowCell, 0);
594+
585595
// Ensure thead registry lock held, so as to synchronize
586596
// with DoReset, which also access the mapped_shadow_* ctxt fields.
587597
ThreadRegistryLock lock0(&ctx->thread_registry);

compiler-rt/lib/tsan/rtl/tsan_sync.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ void MetaMap::MoveMemory(uptr src, uptr dst, uptr sz) {
246246
// there are no concurrent accesses to the regions (e.g. stop-the-world).
247247
CHECK_NE(src, dst);
248248
CHECK_NE(sz, 0);
249+
250+
// The current MoveMemory implementation behaves incorrectly when src, dst,
251+
// and sz are not aligned to kMetaShadowCell.
252+
// For example, with kMetaShadowCell == 8:
253+
// - src = 4: unexpectedly clears the metadata for the range [0, 4).
254+
// - src = 16, dst = 4, size = 8: A sync variable for addr = 20, which should
255+
// be moved to the metadata for address 8, is incorrectly moved to the
256+
// metadata for address 0 instead.
257+
// - src = 0, sz = 4: fails to move the tail metadata.
258+
// Therefore, the following assertions is needed.
259+
DCHECK_EQ(src % kMetaShadowCell, 0);
260+
DCHECK_EQ(dst % kMetaShadowCell, 0);
261+
DCHECK_EQ(sz % kMetaShadowCell, 0);
262+
249263
uptr diff = dst - src;
250264
u32 *src_meta, *dst_meta, *src_meta_end;
251265
uptr inc;

0 commit comments

Comments
 (0)