diff --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp --- a/compiler-rt/lib/hwasan/hwasan_allocator.cpp +++ b/compiler-rt/lib/hwasan/hwasan_allocator.cpp @@ -196,6 +196,7 @@ : tagged_ptr; void *aligned_ptr = reinterpret_cast( RoundDownTo(reinterpret_cast(untagged_ptr), kShadowAlignment)); + tag_t pointer_tag = GetTagFromPointer(reinterpret_cast(tagged_ptr)); Metadata *meta = reinterpret_cast(allocator.GetMetaData(aligned_ptr)); uptr orig_size = meta->get_requested_size(); @@ -236,7 +237,8 @@ // The tag can be zero if tagging is disabled on this thread. do { tag = t->GenerateRandomTag(/*num_bits=*/8); - } while (UNLIKELY(tag < kShadowAlignment && tag != 0)); + } while ( + UNLIKELY((tag < kShadowAlignment || tag == pointer_tag) && tag != 0)); } else { static_assert(kFallbackFreeTag >= kShadowAlignment, "fallback tag must not be a short granule tag.");