Skip to content

Commit 82655c1

Browse files
[MSan] Tweak CopyOrigin
There could be some mis-alignments when copying origins not aligned. I believe inaligned memcpy is rare so the cases do not matter too much in practice. 1) About the change at line 50 Let dst be (void*)5, then d=5, beg=4 so we need to write 3 (4+4-5) bytes from 5 to 7. 2) About the change around line 77. Let dst be (void*)5, because of lines 50-55, the bytes from 5-7 were already writen. So the aligned copy is from 8. Reviewed-by: eugenis Differential Revision: https://reviews.llvm.org/D94552
1 parent 25eb7b0 commit 82655c1

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

compiler-rt/lib/msan/msan_poisoning.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void CopyOrigin(const void *dst, const void *src, uptr size,
4747
uptr beg = d & ~3UL;
4848
// Copy left unaligned origin if that memory is poisoned.
4949
if (beg < d) {
50-
u32 o = GetOriginIfPoisoned((uptr)src, d - beg);
50+
u32 o = GetOriginIfPoisoned((uptr)src, beg + 4 - d);
5151
if (o) {
5252
if (__msan_get_track_origins() > 1) o = ChainOrigin(o, stack);
5353
*(u32 *)MEM_TO_ORIGIN(beg) = o;
@@ -71,12 +71,13 @@ void CopyOrigin(const void *dst, const void *src, uptr size,
7171
if (beg < end) {
7272
// Align src up.
7373
uptr s = ((uptr)src + 3) & ~3UL;
74+
uptr aligned_beg = ((uptr)dst + 3) & ~3UL;
7475
// FIXME: factor out to msan_copy_origin_aligned
7576
if (__msan_get_track_origins() > 1) {
7677
u32 *src = (u32 *)MEM_TO_ORIGIN(s);
7778
u32 *src_s = (u32 *)MEM_TO_SHADOW(s);
78-
u32 *src_end = (u32 *)MEM_TO_ORIGIN(s + (end - beg));
79-
u32 *dst = (u32 *)MEM_TO_ORIGIN(beg);
79+
u32 *src_end = (u32 *)MEM_TO_ORIGIN(s + (end - aligned_beg));
80+
u32 *dst = (u32 *)MEM_TO_ORIGIN(aligned_beg);
8081
u32 src_o = 0;
8182
u32 dst_o = 0;
8283
for (; src < src_end; ++src, ++src_s, ++dst) {
@@ -88,8 +89,9 @@ void CopyOrigin(const void *dst, const void *src, uptr size,
8889
*dst = dst_o;
8990
}
9091
} else {
91-
REAL(memcpy)((void *)MEM_TO_ORIGIN(beg), (void *)MEM_TO_ORIGIN(s),
92-
end - beg);
92+
REAL(memcpy)
93+
((void *)MEM_TO_ORIGIN(aligned_beg), (void *)MEM_TO_ORIGIN(s),
94+
end - aligned_beg);
9395
}
9496
}
9597
}

0 commit comments

Comments
 (0)