Skip to content

Commit 9f8aeb0

Browse files
committed
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
1 parent 77fd12a commit 9f8aeb0

File tree

1 file changed

+40
-34
lines changed
  • compiler-rt/lib/scudo/standalone

1 file changed

+40
-34
lines changed

compiler-rt/lib/scudo/standalone/memtag.h

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
namespace scudo {
2323

24+
void setRandomTag(void *Ptr, uptr Size, uptr ExcludeMask, uptr *TaggedBegin,
25+
uptr *TaggedEnd);
26+
2427
#if defined(__aarch64__) || defined(SCUDO_FUZZ)
2528

2629
inline constexpr bool archSupportsMemoryTagging() { return true; }
@@ -91,37 +94,32 @@ class ScopedDisableMemoryTagChecks {
9194
}
9295
};
9396

94-
inline void setRandomTag(void *Ptr, uptr Size, uptr ExcludeMask,
95-
uptr *TaggedBegin, uptr *TaggedEnd) {
96-
void *End;
97+
inline uptr selectRandomTag(uptr Ptr, uptr ExcludeMask) {
98+
uptr TaggedPtr;
9799
__asm__ __volatile__(
98-
R"(
99-
.arch_extension mte
100-
101-
// Set a random tag for Ptr in TaggedPtr. This needs to happen even if
102-
// Size = 0 so that TaggedPtr ends up pointing at a valid address.
103-
irg %[TaggedPtr], %[Ptr], %[ExcludeMask]
104-
mov %[Cur], %[TaggedPtr]
105-
106-
// Skip the loop if Size = 0. We don't want to do any tagging in this case.
107-
cbz %[Size], 2f
108-
109-
// Set the memory tag of the region
110-
// [TaggedPtr, TaggedPtr + roundUpTo(Size, 16))
111-
// to the pointer tag stored in TaggedPtr.
112-
add %[End], %[TaggedPtr], %[Size]
113-
114-
1:
115-
stzg %[Cur], [%[Cur]], #16
116-
cmp %[Cur], %[End]
117-
b.lt 1b
100+
".arch_extension mte; irg %[TaggedPtr], %[Ptr], %[ExcludeMask]"
101+
: [TaggedPtr] "=r"(TaggedPtr)
102+
: [Ptr] "r"(Ptr), [ExcludeMask] "r"(ExcludeMask));
103+
return TaggedPtr;
104+
}
118105

119-
2:
120-
)"
121-
:
122-
[TaggedPtr] "=&r"(*TaggedBegin), [Cur] "=&r"(*TaggedEnd), [End] "=&r"(End)
123-
: [Ptr] "r"(Ptr), [Size] "r"(Size), [ExcludeMask] "r"(ExcludeMask)
124-
: "memory");
106+
inline uptr storeTags(uptr Begin, uptr End) {
107+
DCHECK(Begin % 16 == 0);
108+
if (Begin != End) {
109+
__asm__ __volatile__(
110+
R"(
111+
.arch_extension mte
112+
113+
1:
114+
stzg %[Cur], [%[Cur]], #16
115+
cmp %[Cur], %[End]
116+
b.lt 1b
117+
)"
118+
: [Cur] "+&r"(Begin)
119+
: [End] "r"(End)
120+
: "memory");
121+
}
122+
return Begin;
125123
}
126124

127125
inline void *prepareTaggedChunk(void *Ptr, uptr Size, uptr ExcludeMask,
@@ -224,13 +222,15 @@ struct ScopedDisableMemoryTagChecks {
224222
ScopedDisableMemoryTagChecks() {}
225223
};
226224

227-
inline void setRandomTag(void *Ptr, uptr Size, uptr ExcludeMask,
228-
uptr *TaggedBegin, uptr *TaggedEnd) {
225+
inline uptr selectRandomTag(uptr Ptr, uptr ExcludeMask) {
229226
(void)Ptr;
230-
(void)Size;
231227
(void)ExcludeMask;
232-
(void)TaggedBegin;
233-
(void)TaggedEnd;
228+
UNREACHABLE("memory tagging not supported");
229+
}
230+
231+
inline uptr storeTags(uptr Begin, uptr End) {
232+
(void)Begin;
233+
(void)End;
234234
UNREACHABLE("memory tagging not supported");
235235
}
236236

@@ -257,6 +257,12 @@ inline uptr loadTag(uptr Ptr) {
257257

258258
#endif
259259

260+
inline void setRandomTag(void *Ptr, uptr Size, uptr ExcludeMask,
261+
uptr *TaggedBegin, uptr *TaggedEnd) {
262+
*TaggedBegin = selectRandomTag(reinterpret_cast<uptr>(Ptr), ExcludeMask);
263+
*TaggedEnd = storeTags(*TaggedBegin, *TaggedBegin + Size);
264+
}
265+
260266
} // namespace scudo
261267

262268
#endif

0 commit comments

Comments
 (0)