forked from secure-software-engineering/phasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing a pass to save ptr types
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/****************************************************************************** | ||
* Copyright (c) 2018 Philipp Schubert. | ||
* All rights reserved. This program and the accompanying materials are made | ||
* available under the terms of LICENSE.txt. | ||
* | ||
* Contributors: | ||
* Philipp Schubert and others | ||
*****************************************************************************/ | ||
|
||
#ifndef PHASAR_PHASARPASS_OPAQUEPTRTYPASS_H_ | ||
#define PHASAR_PHASARPASS_OPAQUEPTRTYPASS_H_ | ||
|
||
#include "llvm/Pass.h" | ||
|
||
namespace llvm { | ||
class Module; | ||
} // namespace llvm | ||
|
||
namespace psr { | ||
|
||
class OpaquePtrTyPass : public llvm::ModulePass { | ||
public: | ||
static inline char ID = 12; // NOLINT FIXME: make const when LLVM supports it | ||
|
||
explicit OpaquePtrTyPass(); | ||
OpaquePtrTyPass(const OpaquePtrTyPass &) = delete; | ||
OpaquePtrTyPass &operator=(const OpaquePtrTyPass &) = delete; | ||
~OpaquePtrTyPass() override = default; | ||
|
||
[[nodiscard]] llvm::StringRef getPassName() const override; | ||
|
||
bool runOnModule(llvm::Module &M) override; | ||
}; | ||
|
||
} // namespace psr | ||
|
||
#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,68 @@ | ||
/****************************************************************************** | ||
* Copyright (c) 2024 Philipp Schubert. | ||
* All rights reserved. This program and the accompanying materials are made | ||
* available under the terms of LICENSE.txt. | ||
* | ||
* Contributors: | ||
* Maximilian Leo Huber and others | ||
*****************************************************************************/ | ||
|
||
#include "phasar/PhasarLLVM/Passes/OpaquePtrTyPass.h" | ||
|
||
#include "phasar/Utils/Logger.h" | ||
#include "phasar/Utils/NlohmannLogging.h" | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/Support/FileSystem.h" | ||
|
||
#include <map> | ||
|
||
namespace psr { | ||
|
||
OpaquePtrTyPass::OpaquePtrTyPass() : llvm::ModulePass(ID) {} | ||
|
||
llvm::StringRef OpaquePtrTyPass::getPassName() const { | ||
return "OpaquePtrTyPass"; | ||
} | ||
|
||
bool OpaquePtrTyPass::runOnModule(llvm::Module &M) { | ||
llvm::outs() << "OpaquePtrTyPass::runOnModule()\n"; | ||
|
||
std::map<std::string, std::string> PtrTypes; | ||
|
||
// get pointer types | ||
for (const auto &Func : M.getFunctionList()) { | ||
if (Func.isDeclaration()) { | ||
continue; | ||
} | ||
|
||
PtrTypes[Func.getName().str()] = | ||
Func.getFunctionType()->getStructName().str(); | ||
} | ||
|
||
// save pointer types to json file | ||
nlohmann::json Json(PtrTypes); | ||
std::string PathToJson = "./OpaquePtrTyPassJsons/"; | ||
std::string FileName = PathToJson + "PointerTypes.json"; | ||
|
||
llvm::sys::fs::create_directories(PathToJson); | ||
std::error_code EC; | ||
llvm::raw_fd_ostream FileStream(llvm::StringRef(FileName), EC); | ||
|
||
if (EC) { | ||
PHASAR_LOG_LEVEL(ERROR, EC.message()); | ||
return false; | ||
} | ||
|
||
FileStream << Json; | ||
|
||
llvm::outs() << "Json with pointer types saved to: " << PathToJson << "\n"; | ||
|
||
return true; | ||
} | ||
static llvm::RegisterPass<OpaquePtrTyPass> | ||
Phasar("opaque-pointer-type-pass", "PhASAR Opaque Pointer Type Pass", false, | ||
false); | ||
|
||
} // namespace psr |