From f69364c2884f3ada876076f9e10f3c92594914ce Mon Sep 17 00:00:00 2001 From: Hyeonggon Yoo <42.hyeyoo@gmail.com> Date: Tue, 21 May 2024 15:58:24 +0900 Subject: [PATCH] broken imsi Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- compiler-rt/lib/plsan/plsan.h | 2 +- compiler-rt/lib/plsan/plsan_allocator.cpp | 40 +++++++++---------- compiler-rt/lib/plsan/plsan_allocator.h | 10 ++--- .../Instrumentation/PreciseLeakSanitizer.cpp | 3 +- 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/compiler-rt/lib/plsan/plsan.h b/compiler-rt/lib/plsan/plsan.h index 5cc75cca7..eb587e6b6 100644 --- a/compiler-rt/lib/plsan/plsan.h +++ b/compiler-rt/lib/plsan/plsan.h @@ -35,7 +35,7 @@ bool PtrIsAllocatedFromPlsan(Metadata *metadata); bool IsSameObject(Metadata *metadata, const void *p, const void *q); void IncRefCount(Metadata *metadata); void DecRefCount(Metadata *metadata); -u8 GetRefCount(Metadata *metadata); +u32 GetRefCount(Metadata *metadata); bool IsAllocated(Metadata *metadata); u32 GetAllocTraceID(Metadata *metadata); diff --git a/compiler-rt/lib/plsan/plsan_allocator.cpp b/compiler-rt/lib/plsan/plsan_allocator.cpp index 901a2e243..02564b282 100644 --- a/compiler-rt/lib/plsan/plsan_allocator.cpp +++ b/compiler-rt/lib/plsan/plsan_allocator.cpp @@ -78,7 +78,7 @@ bool IsSameObject(Metadata *metadata, const void *x, const void *y) { return begin <= y && (uptr)y < (uptr)begin + metadata->GetRequestedSize(); } -u8 GetRefCount(Metadata *metadata) { return metadata->GetRefCount(); } +u32 GetRefCount(Metadata *metadata) { return metadata->GetRefCount(); } bool IsAllocated(Metadata *metadata) { return metadata->IsAllocated(); } @@ -87,9 +87,8 @@ u32 GetAllocTraceID(Metadata *metadata) { return metadata->GetAllocTraceId(); } inline void Metadata::SetAllocated(u32 stack, u64 size) { requested_size = size; alloc_trace_id = stack; - u8 s = (1 << 7); - atomic_store(reinterpret_cast(&state), s, - memory_order_relaxed); + u32 s = (1 << 31); + atomic_store(&state, s, memory_order_relaxed); } inline void Metadata::SetLsanTag(__lsan::ChunkTag tag) { lsan_tag = tag; } @@ -97,52 +96,49 @@ inline void Metadata::SetLsanTag(__lsan::ChunkTag tag) { lsan_tag = tag; } inline __lsan::ChunkTag Metadata::GetLsanTag() const { return lsan_tag; } inline void Metadata::SetUnallocated() { - u8 s = 0; + u32 s = 0; requested_size = 0; alloc_trace_id = 0; - atomic_store(reinterpret_cast(&state), s, - memory_order_relaxed); + atomic_store(&state, s, memory_order_relaxed); } bool Metadata::IsAllocated() const { - u8 s = atomic_load_relaxed(&state); - return s >> 7; + u32 s = atomic_load_relaxed(&state); + return s >> (sizeof(state) * 8 - 1); } inline u64 Metadata::GetRequestedSize() const { return requested_size; } u32 Metadata::GetAllocTraceId() const { return alloc_trace_id; } -inline u8 Metadata::GetRefCount() const { - u8 s = atomic_load_relaxed(&state); - return s & ~(1 << 7); +inline u32 Metadata::GetRefCount() const { + u32 s = atomic_load_relaxed(&state); + return s & ~(1 << 31); } -inline void Metadata::SetRefCount(u8 val) { - atomic_store(reinterpret_cast(&state), val, - memory_order_relaxed); +inline void Metadata::SetRefCount(u32 val) { + atomic_store(&state, val, memory_order_relaxed); } inline void Metadata::IncRefCount() { - u8 s; - // FIXME: Change state to atomic uint8_t and use atomic_load_relaxed() + u32 s; do { s = atomic_load_relaxed(&state); CHECK(s != (PLSAN_ALLOCATED|PLSAN_REFCOUNT_MAX)); } while (!atomic_compare_exchange_strong( - reinterpret_cast(&state), &s, s + 1, + &state, &s, s + 1, memory_order_relaxed)); } inline void Metadata::DecRefCount() { - u8 s; - // FIXME: Change state to atomic uint8_t and use atomic_load_relaxed() + u32 s; do { s = atomic_load_relaxed(&state); // reference count should not be zero when decrementing - CHECK(s == 0 || (s != (PLSAN_ALLOCATED|PLSAN_REFCOUNT_MIN))); + CHECK(s != (PLSAN_ALLOCATED|PLSAN_REFCOUNT_MIN)); + CHECK(s != 0); } while (!atomic_compare_exchange_strong( - reinterpret_cast(&state), &s, s - 1, + &state, &s, s - 1, memory_order_relaxed)); } diff --git a/compiler-rt/lib/plsan/plsan_allocator.h b/compiler-rt/lib/plsan/plsan_allocator.h index c0e1e26e0..af25b3cf3 100644 --- a/compiler-rt/lib/plsan/plsan_allocator.h +++ b/compiler-rt/lib/plsan/plsan_allocator.h @@ -26,15 +26,15 @@ #error Unsupported platform #endif -#define PLSAN_ALLOCATED (1 << (sizeof(state) * 8 - 1)) -#define PLSAN_REFCOUNT_MAX 255 +#define PLSAN_ALLOCATED (1L << (sizeof(state) * 8 - 1)) +#define PLSAN_REFCOUNT_MAX ((1L << (sizeof(state) * 8 - 1)) - 1) #define PLSAN_REFCOUNT_MIN 0 namespace __plsan { struct Metadata { private: // msb: allocated, remaining bits: refcount - atomic_uint8_t state; + atomic_uint32_t state; __lsan::ChunkTag lsan_tag : 2; #if SANITIZER_WORDSIZE == 64 uptr requested_size : 54; @@ -54,8 +54,8 @@ struct Metadata { inline u32 GetAllocThreadId() const; inline void SetLsanTag(__lsan::ChunkTag tag); inline __lsan::ChunkTag GetLsanTag() const; - inline u8 GetRefCount() const; - inline void SetRefCount(u8 val); + inline u32 GetRefCount() const; + inline void SetRefCount(u32 val); inline void IncRefCount(); inline void DecRefCount(); }; diff --git a/llvm/lib/Transforms/Instrumentation/PreciseLeakSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/PreciseLeakSanitizer.cpp index 7b4ab6f51..310370acc 100644 --- a/llvm/lib/Transforms/Instrumentation/PreciseLeakSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/PreciseLeakSanitizer.cpp @@ -197,9 +197,10 @@ void PreciseLeakSanVisitor::visitCallInst(CallInst &I) { if (Instruction *argInst = dyn_cast(arg)) { if (LoadInst *loadInst = dyn_cast(argInst)) { Value *ptrToPointer = loadInst->getPointerOperand(); - Builder.CreateStore( + StoreInst *InstrumentedInst = Builder.CreateStore( ConstantPointerNull::get(cast(loadInst->getType())), ptrToPointer); + InstrumentedInst->setMetadata(Plsan.PlsanMDName, Plsan.PlsanMD); } } }