Skip to content

Commit 4d3ed10

Browse files
authored
[compiler-rt] [Darwin] Strip MTE tags from ASAN and TSAN (llvm#166453)
ASAN and TSAN need to strip tags in order to compute the correct shadow addresses. rdar://163518624
1 parent 518b38c commit 4d3ed10

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

compiler-rt/lib/asan/asan_mapping.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init.
285285
# include "asan_mapping_sparc64.h"
286286
# else
287287
# define MEM_TO_SHADOW(mem) \
288-
(((mem) >> ASAN_SHADOW_SCALE) + (ASAN_SHADOW_OFFSET))
288+
((STRIP_MTE_TAG(mem) >> ASAN_SHADOW_SCALE) + (ASAN_SHADOW_OFFSET))
289289
# define SHADOW_TO_MEM(mem) \
290290
(((mem) - (ASAN_SHADOW_OFFSET)) << (ASAN_SHADOW_SCALE))
291291

@@ -377,6 +377,7 @@ static inline uptr MemToShadowSize(uptr size) {
377377

378378
static inline bool AddrIsInMem(uptr a) {
379379
PROFILE_ASAN_MAPPING();
380+
a = STRIP_MTE_TAG(a);
380381
return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a) ||
381382
(flags()->protect_shadow_gap == 0 && AddrIsInShadowGap(a));
382383
}
@@ -389,6 +390,7 @@ static inline uptr MemToShadow(uptr p) {
389390

390391
static inline bool AddrIsInShadow(uptr a) {
391392
PROFILE_ASAN_MAPPING();
393+
a = STRIP_MTE_TAG(a);
392394
return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
393395
}
394396

compiler-rt/lib/sanitizer_common/sanitizer_platform.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,11 @@
497497
# endif
498498
#endif
499499

500+
#if SANITIZER_APPLE && SANITIZER_WORDSIZE == 64
501+
// MTE uses the lower half of the top byte.
502+
# define STRIP_MTE_TAG(addr) ((addr) & ~((uptr)0x0f << 56))
503+
#else
504+
# define STRIP_MTE_TAG(addr) (addr)
505+
#endif
506+
500507
#endif // SANITIZER_PLATFORM_H

compiler-rt/lib/tsan/rtl/tsan_platform.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,9 @@ struct IsAppMemImpl {
958958
};
959959

960960
ALWAYS_INLINE
961-
bool IsAppMem(uptr mem) { return SelectMapping<IsAppMemImpl>(mem); }
961+
bool IsAppMem(uptr mem) {
962+
return SelectMapping<IsAppMemImpl>(STRIP_MTE_TAG(mem));
963+
}
962964

963965
struct IsShadowMemImpl {
964966
template <typename Mapping>
@@ -997,7 +999,8 @@ struct MemToShadowImpl {
997999

9981000
ALWAYS_INLINE
9991001
RawShadow *MemToShadow(uptr x) {
1000-
return reinterpret_cast<RawShadow *>(SelectMapping<MemToShadowImpl>(x));
1002+
return reinterpret_cast<RawShadow*>(
1003+
SelectMapping<MemToShadowImpl>(STRIP_MTE_TAG(x)));
10011004
}
10021005

10031006
struct MemToMetaImpl {
@@ -1011,7 +1014,9 @@ struct MemToMetaImpl {
10111014
};
10121015

10131016
ALWAYS_INLINE
1014-
u32 *MemToMeta(uptr x) { return SelectMapping<MemToMetaImpl>(x); }
1017+
u32* MemToMeta(uptr x) {
1018+
return SelectMapping<MemToMetaImpl>(STRIP_MTE_TAG(x));
1019+
}
10151020

10161021
struct ShadowToMemImpl {
10171022
template <typename Mapping>

0 commit comments

Comments
 (0)