21
21
22
22
namespace scudo {
23
23
24
+ void setRandomTag (void *Ptr, uptr Size, uptr ExcludeMask, uptr *TaggedBegin,
25
+ uptr *TaggedEnd);
26
+
24
27
#if defined(__aarch64__) || defined(SCUDO_FUZZ)
25
28
26
29
inline constexpr bool archSupportsMemoryTagging () { return true ; }
@@ -91,37 +94,32 @@ class ScopedDisableMemoryTagChecks {
91
94
}
92
95
};
93
96
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;
97
99
__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
+ }
118
105
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;
125
123
}
126
124
127
125
inline void *prepareTaggedChunk (void *Ptr, uptr Size, uptr ExcludeMask,
@@ -224,13 +222,15 @@ struct ScopedDisableMemoryTagChecks {
224
222
ScopedDisableMemoryTagChecks () {}
225
223
};
226
224
227
- inline void setRandomTag (void *Ptr, uptr Size, uptr ExcludeMask,
228
- uptr *TaggedBegin, uptr *TaggedEnd) {
225
+ inline uptr selectRandomTag (uptr Ptr, uptr ExcludeMask) {
229
226
(void )Ptr;
230
- (void )Size;
231
227
(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;
234
234
UNREACHABLE (" memory tagging not supported" );
235
235
}
236
236
@@ -257,6 +257,12 @@ inline uptr loadTag(uptr Ptr) {
257
257
258
258
#endif
259
259
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
+
260
266
} // namespace scudo
261
267
262
268
#endif
0 commit comments