scudo: Split setRandomTag in two. NFCI.

Separate the IRG part from the STZG part since we will need to use
the latter on its own for some upcoming changes.

Differential Revision: https://reviews.llvm.org/D92880
This commit is contained in:
Peter Collingbourne 2020-12-04 15:03:49 -08:00
parent 77fd12a66e
commit 9f8aeb0602
1 changed files with 39 additions and 33 deletions

View File

@ -21,6 +21,9 @@
namespace scudo { namespace scudo {
void setRandomTag(void *Ptr, uptr Size, uptr ExcludeMask, uptr *TaggedBegin,
uptr *TaggedEnd);
#if defined(__aarch64__) || defined(SCUDO_FUZZ) #if defined(__aarch64__) || defined(SCUDO_FUZZ)
inline constexpr bool archSupportsMemoryTagging() { return true; } inline constexpr bool archSupportsMemoryTagging() { return true; }
@ -91,37 +94,32 @@ public:
} }
}; };
inline void setRandomTag(void *Ptr, uptr Size, uptr ExcludeMask, inline uptr selectRandomTag(uptr Ptr, uptr ExcludeMask) {
uptr *TaggedBegin, uptr *TaggedEnd) { uptr TaggedPtr;
void *End;
__asm__ __volatile__( __asm__ __volatile__(
R"( ".arch_extension mte; irg %[TaggedPtr], %[Ptr], %[ExcludeMask]"
.arch_extension mte : [TaggedPtr] "=r"(TaggedPtr)
: [Ptr] "r"(Ptr), [ExcludeMask] "r"(ExcludeMask));
return TaggedPtr;
}
// Set a random tag for Ptr in TaggedPtr. This needs to happen even if inline uptr storeTags(uptr Begin, uptr End) {
// Size = 0 so that TaggedPtr ends up pointing at a valid address. DCHECK(Begin % 16 == 0);
irg %[TaggedPtr], %[Ptr], %[ExcludeMask] if (Begin != End) {
mov %[Cur], %[TaggedPtr] __asm__ __volatile__(
R"(
.arch_extension mte
// Skip the loop if Size = 0. We don't want to do any tagging in this case. 1:
cbz %[Size], 2f stzg %[Cur], [%[Cur]], #16
cmp %[Cur], %[End]
// Set the memory tag of the region b.lt 1b
// [TaggedPtr, TaggedPtr + roundUpTo(Size, 16)) )"
// to the pointer tag stored in TaggedPtr. : [Cur] "+&r"(Begin)
add %[End], %[TaggedPtr], %[Size] : [End] "r"(End)
: "memory");
1: }
stzg %[Cur], [%[Cur]], #16 return Begin;
cmp %[Cur], %[End]
b.lt 1b
2:
)"
:
[TaggedPtr] "=&r"(*TaggedBegin), [Cur] "=&r"(*TaggedEnd), [End] "=&r"(End)
: [Ptr] "r"(Ptr), [Size] "r"(Size), [ExcludeMask] "r"(ExcludeMask)
: "memory");
} }
inline void *prepareTaggedChunk(void *Ptr, uptr Size, uptr ExcludeMask, inline void *prepareTaggedChunk(void *Ptr, uptr Size, uptr ExcludeMask,
@ -224,13 +222,15 @@ struct ScopedDisableMemoryTagChecks {
ScopedDisableMemoryTagChecks() {} ScopedDisableMemoryTagChecks() {}
}; };
inline void setRandomTag(void *Ptr, uptr Size, uptr ExcludeMask, inline uptr selectRandomTag(uptr Ptr, uptr ExcludeMask) {
uptr *TaggedBegin, uptr *TaggedEnd) {
(void)Ptr; (void)Ptr;
(void)Size;
(void)ExcludeMask; (void)ExcludeMask;
(void)TaggedBegin; UNREACHABLE("memory tagging not supported");
(void)TaggedEnd; }
inline uptr storeTags(uptr Begin, uptr End) {
(void)Begin;
(void)End;
UNREACHABLE("memory tagging not supported"); UNREACHABLE("memory tagging not supported");
} }
@ -257,6 +257,12 @@ inline uptr loadTag(uptr Ptr) {
#endif #endif
inline void setRandomTag(void *Ptr, uptr Size, uptr ExcludeMask,
uptr *TaggedBegin, uptr *TaggedEnd) {
*TaggedBegin = selectRandomTag(reinterpret_cast<uptr>(Ptr), ExcludeMask);
*TaggedEnd = storeTags(*TaggedBegin, *TaggedBegin + Size);
}
} // namespace scudo } // namespace scudo
#endif #endif