Skip to content

Commit

Permalink
IMSI 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kimseongyu committed Apr 8, 2024
1 parent 3dda770 commit d4241d2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
4 changes: 3 additions & 1 deletion compiler-rt/lib/plsan/plsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,15 @@ __attribute__((constructor(0))) void __plsan_init() {

InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);

plsan_metadata_init();

if (common_flags()->detect_leaks) {
__lsan::ScopedInterceptorDisabler disabler;
Symbolizer::LateInitialize();
}

VPrintf(1, "PreciseLeakSanitizer init done\n");
page_table_init();

plsan_init_is_running = false;
plsan_inited = true;
}
Expand Down
11 changes: 8 additions & 3 deletions compiler-rt/lib/plsan/plsan_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ void GetAllocatorCacheRange(uptr *begin, uptr *end) {
}

Metadata *GetMetadata(const void *p) {
// we can calculate metadata address using only pointer value
// p = allocator.GetBlockBegin(p);
if (!p)
return nullptr;

return reinterpret_cast<struct Metadata *>(get_metadata((unsigned long)p));

// return reinterpret_cast<struct Metadata *>(allocator.GetMetaData(p));
return reinterpret_cast<struct Metadata *>(get_metadata((uptr)p));
}

void IncRefCount(Metadata *metadata) {
Expand Down Expand Up @@ -165,7 +168,9 @@ void PlsanAllocatorInit() {
static void RegisterAllocation(const StackTrace *stack, void *p, uptr size) {
if (!p)
return;
set_metadata((unsigned long)p, size);
// when pointer is allocated, we have to set about metadata
set_metadata((uptr)p, size);

Metadata *m = GetMetadata(p);
m->SetAllocated(StackDepotPut(*stack), size);
m->SetLsanTag(__lsan::DisabledInThisThread() ? __lsan::kIgnored
Expand Down
24 changes: 17 additions & 7 deletions compiler-rt/lib/plsan/plsan_metalloc.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <sys/mman.h> // for mmap, memadvise
#include <string.h> // for memchr
#include <stdlib.h> // for getenv
#include <stdio.h> // for printf
#include <plsan_metalloc.h>
#include "sys/mman.h" // for mmap, memadvise
#include "string.h" // for memchr
#include "stdlib.h" // for getenv
#include "stdio.h" // for printf
#include "sanitizer_common/sanitizer_common.h"

#include "plsan_metalloc.h"

#define unlikely(x) __builtin_expect((x),0)

Expand All @@ -19,6 +21,8 @@
// Number of real pages covered by each reftable entry
// #define REALPAGESPERREFENTRY ((unsigned long)(METALLOC_USEDSIZE >> sizeof(unsigned long)) * PTPAGESPERREFENTRY)

using namespace __sanitizer;

//unsigned long pageTable[PAGETABLESIZE];
bool isPageTableAlloced = false;
//unsigned short refTable[REFTABLESIZE];
Expand Down Expand Up @@ -96,13 +100,19 @@ unsigned long get_metapagetable_entry(void *ptr) {
return pageTable[page];
}

void plsan_metadata_init() {
page_table_init();

// set_metadata();
}

void set_metadata(unsigned long ptr, unsigned long size) {
unsigned long page_align_offset = METALLOC_PAGESIZE - 1;
unsigned long page_align_mask = ~((unsigned long)METALLOC_PAGESIZE - 1);
unsigned long aligned_start = ptr & page_align_mask;
unsigned long aligned_size = ((size + ptr - aligned_start) + page_align_offset) & page_align_mask;
void* metadata = allocate_metadata(aligned_size, METADATA_ALIGN);
set_metapagetable_entries((void *)aligned_start, aligned_size, metadata, METADATA_ALIGN);
void *exec_metadata = allocate_metadata(aligned_size, METADATA_ALIGN);
set_metapagetable_entries((void*)aligned_start, aligned_size, exec_metadata, METADATA_ALIGN);
}

void* get_metadata(unsigned long ptrInt){
Expand Down
4 changes: 4 additions & 0 deletions compiler-rt/lib/plsan/plsan_metalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@

#define SYSTEM_PAGESIZE METALLOC_PAGESIZE

// plsan's sizeof(metadata) is 8 bytes
#define METADATASIZE 8

#define METADATA_ALIGN 8

//extern unsigned long pageTable[];
#define pageTable ((unsigned long*)(0x400000000000))
void page_table_init();
void* allocate_metadata(unsigned long size, unsigned long alignment);
void deallocate_metadata(void *ptr, unsigned long size, unsigned long alignment);
void set_metapagetable_entries(void *ptr, unsigned long size, void *metaptr, int alignment);
unsigned long get_metapagetable_entry(void *ptr);
void plsan_metadata_init();
void set_metadata(unsigned long ptr, unsigned long size);
void* get_metadata(unsigned long ptrInt);
#endif

0 comments on commit d4241d2

Please sign in to comment.