-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from purplepig4657/1-set-up-an-initial-develop…
…ment-environment set up an initial development environment
- Loading branch information
Showing
11 changed files
with
291 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,75 @@ | ||
#include "llvm/Pass.h" | ||
#include "PreciseLeakSanitizer.h" | ||
|
||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/Passes/PassBuilder.h" | ||
#include "llvm/Passes/PassPlugin.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
#include "llvm/Transforms/Utils/ModuleUtils.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
void PreciseLeakSanVisitor::visitStoreInst(StoreInst &I) { return; } | ||
|
||
void PreciseLeakSanVisitor::visitReturnInst(ReturnInst &I) { return; } | ||
|
||
struct PreciseLeakSanitizer : public PassInfoMixin<PreciseLeakSanitizer> { | ||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) { | ||
bool transformed = false; | ||
for (auto &F : M) { | ||
for (auto &BB : F) { | ||
for (auto &I : BB) { | ||
if (auto *CI = dyn_cast<CallInst>(&I)) { | ||
IRBuilder builder(CI); | ||
void PreciseLeakSanVisitor::visitCallInst(CallInst &I) { return; } | ||
|
||
if (CI->getName() == "malloc") { | ||
void PreciseLeakSanVisitor::visitCallMalloc(CallInst &I) { return; } | ||
|
||
} else if (CI->getName() == "calloc") { | ||
void PreciseLeakSanVisitor::visitCallCalloc(CallInst &I) { return; } | ||
|
||
} else if (CI->getName() == "realloc") { | ||
void PreciseLeakSanVisitor::visitCallNew(CallInst &I) { return; } | ||
|
||
} else if (CI->getName() == "new") { | ||
void PreciseLeakSanVisitor::visitCallArrTyNew(CallInst &I) { return; } | ||
|
||
} else if (CI->getName() == "new[]") { | ||
void PreciseLeakSanVisitor::visitCallFree(CallInst &I) { return; } | ||
|
||
} else if (CI->getName() == "free") { | ||
void PreciseLeakSanVisitor::visitCallMemset(CallInst &I) { return; } | ||
|
||
} else if (CI->getName() == "memset") { | ||
void PreciseLeakSanVisitor::visitCallMemcpy(CallInst &I) { return; } | ||
|
||
} else if (CI->getName() == "memcpy") { | ||
void PreciseLeakSanVisitor::visitCallMemmove(CallInst &I) { return; } | ||
|
||
} else if (CI->getName() == "memmove") { | ||
void PreciseLeakSanVisitor::visitCallBzero(CallInst &I) { return; } | ||
|
||
} else if (CI->getName() == "bzero") { | ||
} | ||
} else if (auto *SI = dyn_cast<StoreInst>(&I)) { | ||
bool PreciseLeakSanitizer::initializeModule(Module &M) { return false; } | ||
|
||
} else if (auto *RI = dyn_cast<ReturnInst>(&I)) { | ||
} | ||
} | ||
bool PreciseLeakSanitizer::run(Module &M) { | ||
for (Function &F : M) { | ||
for (BasicBlock &BB : F) { | ||
for (Instruction &I : BB) { | ||
PreciseLeakSanVisitor().visit(I); | ||
} | ||
} | ||
return (transformed ? PreservedAnalyses::none() : PreservedAnalyses::all()); | ||
}; | ||
}; | ||
} | ||
return false; | ||
} | ||
|
||
} // namespace | ||
PreservedAnalyses PreciseLeakSanitizerPass::run(Module &M, | ||
ModuleAnalysisManager &) { | ||
return (PreciseLeakSanitizer().run(M) ? PreservedAnalyses::none() | ||
: PreservedAnalyses::all()); | ||
} | ||
|
||
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo | ||
llvmGetPassPluginInfo() { | ||
return {.APIVersion = LLVM_PLUGIN_API_VERSION, | ||
.PluginName = "PreciseLeakSanitizer", | ||
.PluginVersion = "v0.1", | ||
.RegisterPassBuilderCallbacks = [](PassBuilder &PB) { | ||
llvm::PassPluginLibraryInfo getPreciseLeakSanitizerPluginInfo() { | ||
return {LLVM_PLUGIN_API_VERSION, "precise-leak-sanitizer", | ||
LLVM_VERSION_STRING, [](PassBuilder &PB) { | ||
PB.registerPipelineParsingCallback( | ||
[](StringRef Name, ModulePassManager &MPM, | ||
ArrayRef<PassBuilder::PipelineElement>) { | ||
if (Name == "precise-leak-sanitizer") { | ||
MPM.addPass(PreciseLeakSanitizerPass()); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
PB.registerPipelineStartEPCallback( | ||
[](ModulePassManager &MPM, OptimizationLevel Level) { | ||
MPM.addPass(PreciseLeakSanitizer()); | ||
MPM.addPass(PreciseLeakSanitizerPass()); | ||
}); | ||
}}; | ||
} | ||
|
||
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo | ||
llvmGetPassPluginInfo() { | ||
return getPreciseLeakSanitizerPluginInfo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifndef PRECISE_LEAK_SANITIZER_H | ||
#define PRECISE_LEAK_SANITIZER_H | ||
|
||
#include "llvm/IR/InstVisitor.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/Pass.h" | ||
|
||
using namespace llvm; | ||
|
||
class PreciseLeakSanitizer { | ||
private: | ||
friend class PreciseLeakSanVisitor; | ||
|
||
Module *Mod; | ||
LLVMContext *Ctx; | ||
|
||
// RT library functions | ||
FunctionType *RefCountFnTy; | ||
FunctionType *AllocSizeAlignFnTy; | ||
FunctionType *InitDynAllocShadowMemFnTy; | ||
FunctionCallee RefCountFn; | ||
FunctionCallee AllocSizeAlignFn; | ||
FunctionCallee InitDynAllocShadowMemFn; | ||
StringRef RefCountFnName = "refCount"; | ||
StringRef AllocSizeAlignFnName = "allocSizeAlign"; | ||
StringRef InitDynAllocShadowMemFnName = "initDynAllocShadowMem"; | ||
|
||
bool initializeModule(Module &M); | ||
|
||
public: | ||
bool run(Module &M); | ||
}; | ||
|
||
class PreciseLeakSanVisitor : public InstVisitor<PreciseLeakSanVisitor> { | ||
public: | ||
void visitStoreInst(StoreInst &I); | ||
void visitReturnInst(ReturnInst &I); | ||
void visitCallInst(CallInst &I); | ||
|
||
private: | ||
void visitCallMalloc(CallInst &I); | ||
void visitCallCalloc(CallInst &I); | ||
void visitCallNew(CallInst &I); | ||
void visitCallArrTyNew(CallInst &I); | ||
void visitCallFree(CallInst &I); | ||
void visitCallMemset(CallInst &I); | ||
void visitCallMemcpy(CallInst &I); | ||
void visitCallMemmove(CallInst &I); | ||
void visitCallBzero(CallInst &I); | ||
}; | ||
|
||
struct PreciseLeakSanitizerPass | ||
: public PassInfoMixin<PreciseLeakSanitizerPass> { | ||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,25 @@ | ||
add_library(plsan plsan.cpp) | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
project(Plsan) | ||
|
||
# Add source files to the project | ||
set(SOURCES | ||
plsan.cpp | ||
plsan_storage.cpp | ||
plsan_handler.cpp | ||
plsan_shadow.cpp | ||
) | ||
|
||
# Add header files to the project | ||
set(HEADERS | ||
plsan.h | ||
plsan_storage.h | ||
plsan_handler.h | ||
plsan_shadow.h | ||
) | ||
|
||
# Add library target | ||
add_library(plsan STATIC ${SOURCES} ${HEADERS}) | ||
|
||
# Specify include directories | ||
target_include_directories(plsan PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#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"; | ||
} | ||
|
||
PlsanShadow::~PlsanShadow() { munmap(shadow_addr, MMAP_SIZE); } | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef PLSAN_SHADOW_H | ||
#define PLSAN_SHADOW_H | ||
|
||
#include <cstddef> | ||
|
||
namespace __plsan { | ||
|
||
#define MMAP_SIZE ((1L << 48) / 16) | ||
|
||
class PlsanShadow { | ||
public: | ||
PlsanShadow(); | ||
~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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |