Skip to content

Commit

Permalink
broken imsi
Browse files Browse the repository at this point in the history
Signed-off-by: Hyeonggon Yoo <[email protected]>
  • Loading branch information
hygoni committed May 21, 2024
1 parent 00f471e commit f69364c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion compiler-rt/lib/plsan/plsan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
40 changes: 18 additions & 22 deletions compiler-rt/lib/plsan/plsan_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }

Expand All @@ -87,62 +87,58 @@ 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<atomic_uint8_t *>(&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; }

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<atomic_uint8_t *>(&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<atomic_uint8_t *>(&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<atomic_uint8_t *>(&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<atomic_uint8_t *>(&state), &s, s - 1,
&state, &s, s - 1,
memory_order_relaxed));
}

Expand Down
10 changes: 5 additions & 5 deletions compiler-rt/lib/plsan/plsan_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
};
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Instrumentation/PreciseLeakSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ void PreciseLeakSanVisitor::visitCallInst(CallInst &I) {
if (Instruction *argInst = dyn_cast<Instruction>(arg)) {
if (LoadInst *loadInst = dyn_cast<LoadInst>(argInst)) {
Value *ptrToPointer = loadInst->getPointerOperand();
Builder.CreateStore(
StoreInst *InstrumentedInst = Builder.CreateStore(
ConstantPointerNull::get(cast<PointerType>(loadInst->getType())),
ptrToPointer);
InstrumentedInst->setMetadata(Plsan.PlsanMDName, Plsan.PlsanMD);
}
}
}
Expand Down

0 comments on commit f69364c

Please sign in to comment.