Skip to content

Commit

Permalink
PreciseLeakSanitizer: Setup runtime library structure
Browse files Browse the repository at this point in the history
  • Loading branch information
purplepig4657 committed Jan 19, 2024
1 parent cdd85e2 commit 311f47b
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 0 deletions.
21 changes: 21 additions & 0 deletions RuntimeLibrary/plsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* collisons with the program. For example: __foo(), __bar(), etc.
*/

#include "plsan.h"

#include <cstddef>

/* Initialization routines called before main() */
__attribute__((constructor)) void __plsan_init() { /* TODO: */
__plsan::Plsan *plsan;
}

void __plsan_alloc(void *addr, size_t size) {
Expand All @@ -24,3 +27,21 @@ void __plsan_exit_func() {
* 2) or the address of a buffer is return value
*/
}

namespace __plsan {

Plsan::Plsan() {
shadow = new PlsanShadow();
storage = new PlsanStorage();
handler = new PlsanHandler();
}

void Plsan::enter_func() {
// enter_func
}

void Plsan::exit_func() {
// exit_func
}

} // namespace __plsan
26 changes: 26 additions & 0 deletions RuntimeLibrary/plsan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef PLSAN_H
#define PLSAN_H

#include "plsan_handler.h"
#include "plsan_shadow.h"
#include "plsan_storage.h"

namespace __plsan {

class Plsan {
public:
Plsan();
// long align_size(long size);

void enter_func();
void exit_func();

private:
PlsanShadow *shadow;
PlsanStorage *storage;
PlsanHandler *handler;
};

} // namespace __plsan

#endif
11 changes: 11 additions & 0 deletions RuntimeLibrary/plsan_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "plsan_handler.h"

namespace __plsan {

void PlsanHandler::exception_check(RefCountAnalysis ref_count_analysis) {
AddrType addrType;
ExceptionType exceptionType;
std::tie(addrType, exceptionType) = ref_count_analysis;
}

} // namespace __plsan
21 changes: 21 additions & 0 deletions RuntimeLibrary/plsan_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef PLSAN_HANDLER_H
#define PLSAN_HANDLER_H

#include <tuple>

namespace __plsan {

enum AddrType { NonDynAlloc, DynAlloc };
enum ExceptionType { None, RefCountZero };
// Something stack trace data structure here.

using RefCountAnalysis = std::tuple<AddrType, ExceptionType /*, StackTrace*/>;

class PlsanHandler {
public:
void exception_check(RefCountAnalysis ref_count_analysis);
};

} // namespace __plsan

#endif
20 changes: 20 additions & 0 deletions RuntimeLibrary/plsan_shadow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "plsan_shadow.h"

#include <sys/mman.h>

namespace __plsan {

PlsanShadow::PlsanShadow() {
shadow_addr = mmap(NULL, MMAP_SIZE, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
if (shadow_addr == MAP_FAILED)
throw "mmap failed\n";
}

void PlsanShadow::alloc_shadow(void *addr, size_t size) {}

void PlsanShadow::update_reference(void **lhs, void *rhs) {}

void *PlsanShadow::addr_to_shadow_addr(void *addr) { return 0; }

} // namespace __plsan
23 changes: 23 additions & 0 deletions RuntimeLibrary/plsan_shadow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef PLSAN_SHADOW_H
#define PLSAN_SHADOW_H

#include <cstddef>

namespace __plsan {

#define MMAP_SIZE ((1L << 48) / 16)

class PlsanShadow {
public:
PlsanShadow();
void alloc_shadow(void *addr, size_t size);
void update_reference(void **lhs, void *rhs);

private:
void *shadow_addr;
void *addr_to_shadow_addr(void *addr);
};

} // namespace __plsan

#endif
11 changes: 11 additions & 0 deletions RuntimeLibrary/plsan_storage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "plsan_storage.h"

void PlsanStorage::push_function() {}

void PlsanStorage::pop_function() {}

void PlsanStorage::add_mem_addr() {}

LocalDynAllocStorage PlsanStorage::get_function_stack() {
return function_stack;
}
19 changes: 19 additions & 0 deletions RuntimeLibrary/plsan_storage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef PLSAN_STORAGE_H
#define PLSAN_STORAGE_H

#include <stack>

using LocalDynAllocStorage = std::stack<std::stack<void *>>;

class PlsanStorage {
public:
void push_function();
void pop_function();
void add_mem_addr();
LocalDynAllocStorage get_function_stack();

private:
LocalDynAllocStorage function_stack;
};

#endif

0 comments on commit 311f47b

Please sign in to comment.