Skip to content

Commit d6f2291

Browse files
committed
[compiler-rt] Strip MTE tags from ASAN and TSAN
ASAN and TSAN need to strip tags in order to compute the correct shadow addresses.
1 parent b21949e commit d6f2291

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

compiler-rt/lib/asan/asan_mapping.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,18 @@ extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init.
281281

282282
} // namespace __asan
283283

284+
# if SANITIZER_APPLE && SANITIZER_WORDSIZE == 64
285+
# define TAG_MASK ((uptr)0x0f << 56) // Lower half of top byte
286+
# define STRIP_TAG(addr) ((addr) & ~TAG_MASK)
287+
# else
288+
# define STRIP_TAG(addr) (addr)
289+
# endif
290+
284291
# if defined(__sparc__) && SANITIZER_WORDSIZE == 64
285292
# include "asan_mapping_sparc64.h"
286293
# else
287294
# define MEM_TO_SHADOW(mem) \
288-
(((mem) >> ASAN_SHADOW_SCALE) + (ASAN_SHADOW_OFFSET))
295+
((STRIP_TAG(mem) >> ASAN_SHADOW_SCALE) + (ASAN_SHADOW_OFFSET))
289296
# define SHADOW_TO_MEM(mem) \
290297
(((mem) - (ASAN_SHADOW_OFFSET)) << (ASAN_SHADOW_SCALE))
291298

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

378385
static inline bool AddrIsInMem(uptr a) {
379386
PROFILE_ASAN_MAPPING();
387+
a = STRIP_TAG(a);
380388
return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a) ||
381389
(flags()->protect_shadow_gap == 0 && AddrIsInShadowGap(a));
382390
}
@@ -389,6 +397,7 @@ static inline uptr MemToShadow(uptr p) {
389397

390398
static inline bool AddrIsInShadow(uptr a) {
391399
PROFILE_ASAN_MAPPING();
400+
a = STRIP_TAG(a);
392401
return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
393402
}
394403

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,16 @@ uptr MetaShadowBeg(void) { return SelectMapping<MappingField>(kMetaShadowBeg); }
947947
ALWAYS_INLINE
948948
uptr MetaShadowEnd(void) { return SelectMapping<MappingField>(kMetaShadowEnd); }
949949

950+
ALWAYS_INLINE
951+
uptr StripTag(uptr addr) {
952+
#if SANITIZER_APPLE
953+
constexpr uptr kTagMask = ((uptr)0x0f << 56); // Lower half of top byte
954+
return addr & ~kTagMask;
955+
#else
956+
return addr;
957+
#endif
958+
}
959+
950960
struct IsAppMemImpl {
951961
template <typename Mapping>
952962
static bool Apply(uptr mem) {
@@ -958,7 +968,7 @@ struct IsAppMemImpl {
958968
};
959969

960970
ALWAYS_INLINE
961-
bool IsAppMem(uptr mem) { return SelectMapping<IsAppMemImpl>(mem); }
971+
bool IsAppMem(uptr mem) { return SelectMapping<IsAppMemImpl>(StripTag(mem)); }
962972

963973
struct IsShadowMemImpl {
964974
template <typename Mapping>
@@ -997,7 +1007,7 @@ struct MemToShadowImpl {
9971007

9981008
ALWAYS_INLINE
9991009
RawShadow *MemToShadow(uptr x) {
1000-
return reinterpret_cast<RawShadow *>(SelectMapping<MemToShadowImpl>(x));
1010+
return reinterpret_cast<RawShadow *>(SelectMapping<MemToShadowImpl>(StripTag(mem)));
10011011
}
10021012

10031013
struct MemToMetaImpl {
@@ -1011,7 +1021,7 @@ struct MemToMetaImpl {
10111021
};
10121022

10131023
ALWAYS_INLINE
1014-
u32 *MemToMeta(uptr x) { return SelectMapping<MemToMetaImpl>(x); }
1024+
u32 *MemToMeta(uptr x) { return SelectMapping<MemToMetaImpl>(StripTag(x)); }
10151025

10161026
struct ShadowToMemImpl {
10171027
template <typename Mapping>

0 commit comments

Comments
 (0)