[compiler-rt] r340979 - [hwasan] rename some variables and functions for better readability, NFC
Kostya Serebryany via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 29 15:21:22 PDT 2018
Author: kcc
Date: Wed Aug 29 15:21:22 2018
New Revision: 340979
URL: http://llvm.org/viewvc/llvm-project?rev=340979&view=rev
Log:
[hwasan] rename some variables and functions for better readability, NFC
Modified:
compiler-rt/trunk/lib/hwasan/hwasan.cc
compiler-rt/trunk/lib/hwasan/hwasan.h
compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc
compiler-rt/trunk/lib/hwasan/hwasan_report.cc
Modified: compiler-rt/trunk/lib/hwasan/hwasan.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan.cc?rev=340979&r1=340978&r2=340979&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan.cc (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan.cc Wed Aug 29 15:21:22 2018
@@ -228,7 +228,7 @@ void __hwasan_init() {
}
void __hwasan_print_shadow(const void *p, uptr sz) {
- uptr ptr_raw = GetAddressFromPointer((uptr)p);
+ uptr ptr_raw = UntagAddr(reinterpret_cast<uptr>(p));
uptr shadow_first = MEM_TO_SHADOW(ptr_raw);
uptr shadow_last = MEM_TO_SHADOW(ptr_raw + sz - 1);
Printf("HWASan shadow map for %zx .. %zx (pointer tag %x)\n", ptr_raw,
@@ -243,7 +243,7 @@ sptr __hwasan_test_shadow(const void *p,
tag_t ptr_tag = GetTagFromPointer((uptr)p);
if (ptr_tag == 0)
return -1;
- uptr ptr_raw = GetAddressFromPointer((uptr)p);
+ uptr ptr_raw = UntagAddr(reinterpret_cast<uptr>(p));
uptr shadow_first = MEM_TO_SHADOW(ptr_raw);
uptr shadow_last = MEM_TO_SHADOW(ptr_raw + sz - 1);
for (uptr s = shadow_first; s <= shadow_last; ++s)
Modified: compiler-rt/trunk/lib/hwasan/hwasan.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan.h?rev=340979&r1=340978&r2=340979&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan.h (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan.h Wed Aug 29 15:21:22 2018
@@ -45,12 +45,13 @@ static inline tag_t GetTagFromPointer(up
return p >> kAddressTagShift;
}
-static inline uptr GetAddressFromPointer(uptr p) {
- return p & ~kAddressTagMask;
+static inline uptr UntagAddr(uptr tagged_addr) {
+ return tagged_addr & ~kAddressTagMask;
}
-static inline void * GetAddressFromPointer(const void *p) {
- return (void *)((uptr)p & ~kAddressTagMask);
+static inline void *UntagPtr(const void *tagged_ptr) {
+ return reinterpret_cast<void *>(
+ UntagAddr(reinterpret_cast<uptr>(tagged_ptr)));
}
static inline uptr AddTagToPointer(uptr p, tag_t tag) {
Modified: compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc?rev=340979&r1=340978&r2=340979&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc Wed Aug 29 15:21:22 2018
@@ -174,23 +174,24 @@ static void *HwasanAllocate(StackTrace *
return user_ptr;
}
-static bool PointerAndMemoryTagsMatch(void *user_ptr) {
- CHECK(user_ptr);
- tag_t ptr_tag = GetTagFromPointer(reinterpret_cast<uptr>(user_ptr));
+static bool PointerAndMemoryTagsMatch(void *tagged_ptr) {
+ CHECK(tagged_ptr);
+ tag_t ptr_tag = GetTagFromPointer(reinterpret_cast<uptr>(tagged_ptr));
tag_t mem_tag = *reinterpret_cast<tag_t *>(
- MEM_TO_SHADOW(GetAddressFromPointer(user_ptr)));
+ MEM_TO_SHADOW(UntagPtr(tagged_ptr)));
return ptr_tag == mem_tag;
}
-void HwasanDeallocate(StackTrace *stack, void *user_ptr) {
- CHECK(user_ptr);
- HWASAN_FREE_HOOK(user_ptr);
-
- if (!PointerAndMemoryTagsMatch(user_ptr))
- ReportInvalidFree(stack, reinterpret_cast<uptr>(user_ptr));
-
- void *p = GetAddressFromPointer(user_ptr);
- Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
+void HwasanDeallocate(StackTrace *stack, void *tagged_ptr) {
+ CHECK(tagged_ptr);
+ HWASAN_FREE_HOOK(tagged_ptr);
+
+ if (!PointerAndMemoryTagsMatch(tagged_ptr))
+ ReportInvalidFree(stack, reinterpret_cast<uptr>(tagged_ptr));
+
+ void *untagged_ptr = UntagPtr(tagged_ptr);
+ Metadata *meta =
+ reinterpret_cast<Metadata *>(allocator.GetMetaData(untagged_ptr));
uptr size = meta->requested_size;
meta->state = CHUNK_FREE;
meta->requested_size = 0;
@@ -201,43 +202,44 @@ void HwasanDeallocate(StackTrace *stack,
HwasanThread *t = GetCurrentThread();
if (flags()->max_free_fill_size > 0) {
uptr fill_size = Min(size, (uptr)flags()->max_free_fill_size);
- internal_memset(p, flags()->free_fill_byte, fill_size);
+ internal_memset(untagged_ptr, flags()->free_fill_byte, fill_size);
}
if (flags()->tag_in_free &&
atomic_load_relaxed(&hwasan_allocator_tagging_enabled))
- TagMemoryAligned((uptr)p, size,
+ TagMemoryAligned((uptr)untagged_ptr, size,
t ? t->GenerateRandomTag() : kFallbackFreeTag);
if (t) {
AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
- allocator.Deallocate(cache, p);
+ allocator.Deallocate(cache, untagged_ptr);
if (auto *ha = t->heap_allocations())
- ha->push({reinterpret_cast<uptr>(user_ptr), free_context_id,
+ ha->push({reinterpret_cast<uptr>(tagged_ptr), free_context_id,
static_cast<u32>(size)});
} else {
SpinMutexLock l(&fallback_mutex);
AllocatorCache *cache = &fallback_allocator_cache;
- allocator.Deallocate(cache, p);
+ allocator.Deallocate(cache, untagged_ptr);
}
}
-void *HwasanReallocate(StackTrace *stack, void *user_old_p, uptr new_size,
+void *HwasanReallocate(StackTrace *stack, void *tagged_ptr_old, uptr new_size,
uptr alignment) {
alignment = Max(alignment, kShadowAlignment);
new_size = RoundUpTo(new_size, kShadowAlignment);
- if (!PointerAndMemoryTagsMatch(user_old_p))
- ReportInvalidFree(stack, reinterpret_cast<uptr>(user_old_p));
+ if (!PointerAndMemoryTagsMatch(tagged_ptr_old))
+ ReportInvalidFree(stack, reinterpret_cast<uptr>(tagged_ptr_old));
- void *new_p = HwasanAllocate(stack, new_size, alignment, false /*zeroise*/);
- if (user_old_p && new_p) {
- void *untagged_ptr_old = GetAddressFromPointer(user_old_p);
+ void *tagged_ptr_new =
+ HwasanAllocate(stack, new_size, alignment, false /*zeroise*/);
+ if (tagged_ptr_old && tagged_ptr_new) {
+ void *untagged_ptr_old = UntagPtr(tagged_ptr_old);
Metadata *meta =
reinterpret_cast<Metadata *>(allocator.GetMetaData(untagged_ptr_old));
- internal_memcpy(GetAddressFromPointer(new_p), untagged_ptr_old,
+ internal_memcpy(UntagPtr(tagged_ptr_new), untagged_ptr_old,
Min(new_size, static_cast<uptr>(meta->requested_size)));
- HwasanDeallocate(stack, user_old_p);
+ HwasanDeallocate(stack, tagged_ptr_old);
}
- return new_p;
+ return tagged_ptr_new;
}
void *HwasanCalloc(StackTrace *stack, uptr nmemb, uptr size) {
@@ -258,12 +260,12 @@ HwasanChunkView FindHeapChunkByAddress(u
return HwasanChunkView(reinterpret_cast<uptr>(block), metadata);
}
-static uptr AllocationSize(const void *user_ptr) {
- const void *p = GetAddressFromPointer(user_ptr);
- if (!p) return 0;
- const void *beg = allocator.GetBlockBegin(p);
- if (beg != p) return 0;
- Metadata *b = (Metadata *)allocator.GetMetaData(p);
+static uptr AllocationSize(const void *tagged_ptr) {
+ const void *untagged_ptr = UntagPtr(tagged_ptr);
+ if (!untagged_ptr) return 0;
+ const void *beg = allocator.GetBlockBegin(untagged_ptr);
+ if (beg != untagged_ptr) return 0;
+ Metadata *b = (Metadata *)allocator.GetMetaData(untagged_ptr);
return b->requested_size;
}
Modified: compiler-rt/trunk/lib/hwasan/hwasan_report.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_report.cc?rev=340979&r1=340978&r2=340979&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_report.cc (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_report.cc Wed Aug 29 15:21:22 2018
@@ -147,25 +147,25 @@ static void PrintTagsAroundAddr(tag_t *t
}
}
-void ReportInvalidFree(StackTrace *stack, uptr addr) {
+void ReportInvalidFree(StackTrace *stack, uptr tagged_addr) {
ScopedErrorReportLock l;
- uptr address = GetAddressFromPointer(addr);
- tag_t ptr_tag = GetTagFromPointer(addr);
- tag_t *tag_ptr = reinterpret_cast<tag_t*>(MEM_TO_SHADOW(address));
+ uptr untagged_addr = UntagAddr(tagged_addr);
+ tag_t ptr_tag = GetTagFromPointer(tagged_addr);
+ tag_t *tag_ptr = reinterpret_cast<tag_t*>(MEM_TO_SHADOW(untagged_addr));
tag_t mem_tag = *tag_ptr;
Decorator d;
Printf("%s", d.Error());
uptr pc = stack->size ? stack->trace[0] : 0;
const char *bug_type = "invalid-free";
Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type,
- address, pc);
+ untagged_addr, pc);
Printf("%s", d.Access());
Printf("tags: %02x/%02x (ptr/mem)\n", ptr_tag, mem_tag);
Printf("%s", d.Default());
stack->Print();
- PrintAddressDescription(address, 0);
+ PrintAddressDescription(untagged_addr, 0);
PrintTagsAroundAddr(tag_ptr);
@@ -173,30 +173,30 @@ void ReportInvalidFree(StackTrace *stack
Die();
}
-void ReportTagMismatch(StackTrace *stack, uptr addr, uptr access_size,
+void ReportTagMismatch(StackTrace *stack, uptr tagged_addr, uptr access_size,
bool is_store) {
ScopedErrorReportLock l;
Decorator d;
Printf("%s", d.Error());
- uptr address = GetAddressFromPointer(addr);
+ uptr untagged_addr = UntagAddr(tagged_addr);
// TODO: when possible, try to print heap-use-after-free, etc.
const char *bug_type = "tag-mismatch";
uptr pc = stack->size ? stack->trace[0] : 0;
Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type,
- address, pc);
+ untagged_addr, pc);
- tag_t ptr_tag = GetTagFromPointer(addr);
- tag_t *tag_ptr = reinterpret_cast<tag_t*>(MEM_TO_SHADOW(address));
+ tag_t ptr_tag = GetTagFromPointer(tagged_addr);
+ tag_t *tag_ptr = reinterpret_cast<tag_t*>(MEM_TO_SHADOW(untagged_addr));
tag_t mem_tag = *tag_ptr;
Printf("%s", d.Access());
Printf("%s of size %zu at %p tags: %02x/%02x (ptr/mem)\n",
- is_store ? "WRITE" : "READ", access_size, address, ptr_tag, mem_tag);
+ is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag, mem_tag);
Printf("%s", d.Default());
stack->Print();
- PrintAddressDescription(address, access_size);
+ PrintAddressDescription(untagged_addr, access_size);
// Temporary functionality; to be folded into PrintAddressDescription.
// TODOs:
@@ -206,7 +206,7 @@ void ReportTagMismatch(StackTrace *stack
// * use the allocations found in the ring buffer for the main report.
HeapAllocationRecord har;
HwasanThread *t = GetCurrentThread();
- if (t && FindHeapAllocation(t->heap_allocations(), addr, &har))
+ if (t && FindHeapAllocation(t->heap_allocations(), tagged_addr, &har))
Printf("Address found in the ring buffer: %p %u %u\n", har.tagged_addr,
har.free_context_id, har.requested_size);
More information about the llvm-commits
mailing list