Skip to content

Commit

Permalink
update branch
Browse files Browse the repository at this point in the history
  • Loading branch information
pdschubert committed Dec 16, 2020
2 parents 28b5874 + 6b4fb85 commit f9b9ab8
Show file tree
Hide file tree
Showing 30 changed files with 389 additions and 292 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ matrix:
- libboost-thread1.65-dev
env:
# - MATRIX_EVAL="CC=clang && CXX=clang++"
- os: osx
osx_image: xcode11.3
sudo: enabled
env:
- MATRIX_EVAL="CC=clang && CXX=clang++"
# - os: osx
# osx_image: xcode11.3
# sudo: enabled
# env:
# - MATRIX_EVAL="CC=clang && CXX=clang++"

branches:
- development
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ else()
message(STATUS "Dynamic log disabled")
endif()

configure_file(config.h.in config.h @ONLY)
include_directories(${CMAKE_BINARY_DIR})


include_directories(
${PHASAR_SRC_DIR}/include
)
Expand Down
7 changes: 7 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef __CONFIG_H__
#define __CONFIG_H__

#define PHASAR_SRC_DIR "@CMAKE_SOURCE_DIR@"
#define PHASAR_BUILD_DIR "@CMAKE_BINARY_DIR@"

#endif /* __CONFIG_H__ */
2 changes: 1 addition & 1 deletion include/phasar/DB/ProjectIRDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class ProjectIRDB {
void print() const;

void emitPreprocessedIR(std::ostream &os = std::cout,
bool ShortenIR = true) const;
bool ShortenIR = false) const;

/**
* Allows the (de-)serialization of Instructions, Arguments, GlobalValues and
Expand Down
2 changes: 1 addition & 1 deletion include/phasar/Utils/BitVectorSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ template <typename T> class BitVectorSet {
void erase(const T &Data) noexcept {
auto Search = Position.left.find(Data);
if (Search != Position.left.end()) {
if (!(Bits.size() < Search->second - 1)) {
if (Bits.size() > Search->second) {
Bits[Search->second] = false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/DB/ProjectIRDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <algorithm>
#include <cassert>
#include <iostream>
#include <ostream>
#include <string>

#include "llvm/Bitcode/BitcodeReader.h"
Expand Down Expand Up @@ -61,10 +62,8 @@ ProjectIRDB::ProjectIRDB(const std::vector<std::string> &IRFiles,
: ProjectIRDB(Options | IRDBOptions::OWNS) {
for (const auto &File : IRFiles) {
// if we have a file that is already compiled to llvm ir
if ((File.find(".ll") != std::basic_string<char, std::char_traits<char>,
std::allocator<char>>::npos ||
File.find(".bc") != std::basic_string<char, std::char_traits<char>,
std::allocator<char>>::npos) &&
if ((File.find(".ll") != std::string::npos ||
File.find(".bc") != std::string::npos) &&
boost::filesystem::exists(File)) {
llvm::SMDiagnostic Diag;
std::unique_ptr<llvm::LLVMContext> C(new llvm::LLVMContext);
Expand Down Expand Up @@ -251,6 +250,7 @@ void ProjectIRDB::print() const {
for (const auto &[File, Module] : Modules) {
std::cout << "Module: " << File << std::endl;
llvm::outs() << *Module;
llvm::outs().flush();
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/PhasarLLVM/Passes/ExampleModulePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ llvm::AnalysisKey ExampleModulePass::Key;

ExampleModulePass::ExampleModulePass() = default;

llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) {
llvm::PreservedAnalyses
ExampleModulePass::run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) {
cout << "ExampleModulePass::run()\n";
return llvm::PreservedAnalyses::all();
}
Expand Down
25 changes: 15 additions & 10 deletions lib/PhasarLLVM/Pointer/LLVMPointsToSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,21 @@ void LLVMPointsToSet::computeValuesPointsToSet(const llvm::Value *V) {
// A global object may be used in multiple functions.
for (const auto *User : G->users()) {
if (const auto *Inst = llvm::dyn_cast<llvm::Instruction>(User)) {
computeFunctionsPointsToSet(
const_cast<llvm::Function *>(Inst->getFunction()));
if (isInterestingPointer(User)) {
mergePointsToSets(User, G);
} else if (const auto *Store = llvm::dyn_cast<llvm::StoreInst>(User)) {
if (isInterestingPointer(Store->getValueOperand())) {
// Store->getPointerOperand() doesn't require checking: it is
// always an interesting pointer
mergePointsToSets(Store->getValueOperand(),
Store->getPointerOperand());
// The may be no corresponding function when the instruction is used in
// a vtable, for instance.
if (Inst->getParent()) {
computeFunctionsPointsToSet(
const_cast<llvm::Function *>(Inst->getFunction()));
if (isInterestingPointer(User)) {
mergePointsToSets(User, G);
} else if (const auto *Store =
llvm::dyn_cast<llvm::StoreInst>(User)) {
if (isInterestingPointer(Store->getValueOperand())) {
// Store->getPointerOperand() doesn't require checking: it is
// always an interesting pointer
mergePointsToSets(Store->getValueOperand(),
Store->getPointerOperand());
}
}
}
}
Expand Down
88 changes: 50 additions & 38 deletions lib/PhasarLLVM/TypeHierarchy/LLVMTypeHierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,32 @@ std::string
LLVMTypeHierarchy::removeStructOrClassPrefix(const std::string &TypeName) {
llvm::StringRef SR(TypeName);
if (SR.startswith(StructPrefix)) {
return SR.ltrim(StructPrefix);
return SR.drop_front(StructPrefix.size());
}
if (SR.startswith(ClassPrefix)) {
return SR.ltrim(ClassPrefix);
return SR.drop_front(ClassPrefix.size());
}
return TypeName;
}

std::string LLVMTypeHierarchy::removeTypeInfoPrefix(std::string VarName) {
llvm::StringRef SR(VarName);
if (SR.startswith(TypeInfoPrefixDemang)) {
return SR.ltrim(TypeInfoPrefixDemang);
return SR.drop_front(TypeInfoPrefixDemang.size());
}
if (SR.startswith(TypeInfoPrefix)) {
return SR.ltrim(TypeInfoPrefix);
return SR.drop_front(TypeInfoPrefix.size());
}
return VarName;
}

std::string LLVMTypeHierarchy::removeVTablePrefix(std::string VarName) {
llvm::StringRef SR(VarName);
if (SR.startswith(VTablePrefixDemang)) {
return SR.ltrim(VTablePrefixDemang);
return SR.drop_front(VTablePrefixDemang.size());
}
if (SR.startswith(VTablePrefix)) {
return SR.ltrim(VTablePrefix);
return SR.drop_front(VTablePrefix.size());
}
return VarName;
}
Expand Down Expand Up @@ -163,23 +163,27 @@ LLVMTypeHierarchy::getSubTypes(const llvm::Module &M,
const llvm::StructType &Type) {
// find corresponding type info variable
std::vector<const llvm::StructType *> SubTypes;
if (const auto *TI = ClearNameTIMap[removeStructOrClassPrefix(Type)]) {
if (TI->hasInitializer()) {
if (const auto *I =
llvm::dyn_cast<llvm::ConstantStruct>(TI->getInitializer())) {
for (const auto &Op : I->operands()) {
if (auto *CE = llvm::dyn_cast<llvm::ConstantExpr>(Op)) {
// caution: getAsInstruction allocates, need to delete later
auto *AsI = CE->getAsInstruction();
if (auto *BC = llvm::dyn_cast<llvm::BitCastInst>(AsI)) {
if (BC->getOperand(0)->hasName()) {
auto Name = BC->getOperand(0)->getName();
if (Name.find(TypeInfoPrefix) != llvm::StringRef::npos) {
auto ClearName = removeTypeInfoPrefix(
boost::core::demangle(Name.str().c_str()));
if (const auto *Type = ClearNameTypeMap[ClearName]) {
SubTypes.push_back(Type);
}
std::string ClearName = removeStructOrClassPrefix(Type);
if (const auto *TI = ClearNameTIMap[ClearName]) {
if (!TI->hasInitializer()) {
LOG_IF_ENABLE(BOOST_LOG_SEV(lg::get(), DEBUG)
<< ClearName << " does not have initializer");
return SubTypes;
}
if (const auto *I =
llvm::dyn_cast<llvm::ConstantStruct>(TI->getInitializer())) {
for (const auto &Op : I->operands()) {
if (auto *CE = llvm::dyn_cast<llvm::ConstantExpr>(Op)) {
// caution: getAsInstruction allocates, need to delete later
auto *AsI = CE->getAsInstruction();
if (auto *BC = llvm::dyn_cast<llvm::BitCastInst>(AsI)) {
if (BC->getOperand(0)->hasName()) {
auto Name = BC->getOperand(0)->getName();
if (Name.find(TypeInfoPrefix) != llvm::StringRef::npos) {
auto ClearName = removeTypeInfoPrefix(
boost::core::demangle(Name.str().c_str()));
if (const auto *Type = ClearNameTypeMap[ClearName]) {
SubTypes.push_back(Type);
}
}
}
Expand All @@ -199,21 +203,29 @@ LLVMTypeHierarchy::getVirtualFunctions(const llvm::Module &M,
std::vector<const llvm::Function *> VFS;
if (const auto *TV = ClearNameTVMap[ClearName]) {
if (const auto *TI = llvm::dyn_cast<llvm::GlobalVariable>(TV)) {
if (TI->hasInitializer()) {
if (const auto *I =
llvm::dyn_cast<llvm::ConstantStruct>(TI->getInitializer())) {
for (const auto &Op : I->operands()) {
if (auto *CA = llvm::dyn_cast<llvm::ConstantArray>(Op)) {
for (auto &COp : CA->operands()) {
if (auto *CE = llvm::dyn_cast<llvm::ConstantExpr>(COp)) {
// caution: getAsInstruction allocates, need to delete later
auto *AsI = CE->getAsInstruction();
if (auto *BC = llvm::dyn_cast<llvm::BitCastInst>(AsI)) {
if (BC->getOperand(0)->hasName()) {
if (auto *F =
M.getFunction(BC->getOperand(0)->getName())) {
VFS.push_back(F);
}
if (!TI->hasInitializer()) {
LOG_IF_ENABLE(BOOST_LOG_SEV(lg::get(), DEBUG)
<< ClearName << " does not have initializer");
return VFS;
}
if (const auto *I =
llvm::dyn_cast<llvm::ConstantStruct>(TI->getInitializer())) {
for (const auto &Op : I->operands()) {
if (auto *CA = llvm::dyn_cast<llvm::ConstantArray>(Op)) {
for (auto &COp : CA->operands()) {
if (auto *CE = llvm::dyn_cast<llvm::ConstantExpr>(COp)) {
// caution: getAsInstruction allocates, need to delete later
auto *AsI = CE->getAsInstruction();
if (auto *BC = llvm::dyn_cast<llvm::BitCastInst>(AsI)) {
// if the entry is a GlobalAlias, get its Aliasee
auto *ENTRY = BC->getOperand(0);
while (auto *GA = llvm::dyn_cast<llvm::GlobalAlias>(ENTRY)) {
ENTRY = GA->getAliasee();
}

if (ENTRY->hasName()) {
if (auto *F = M.getFunction(ENTRY->getName())) {
VFS.push_back(F);
}
}
AsI->deleteValue();
Expand Down
2 changes: 2 additions & 0 deletions test/llvm_test_code/type_hierarchies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ set(NoMem2regSources
type_hierarchy_12_b.cpp
type_hierarchy_12_c.cpp
type_hierarchy_13.cpp
type_hierarchy_14.cpp
type_hierarchy_15.cpp
)

foreach(TEST_SRC ${NoMem2regSources})
Expand Down
19 changes: 19 additions & 0 deletions test/llvm_test_code/type_hierarchies/type_hierarchy_14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

// This class has 4 entries in the vtable
// a construtor, test, 2 destructors
// for the destructors, see here
// https://eli.thegreenplace.net/2015/c-deleting-destructors-and-virtual-operator-delete/
class Base {
public:
virtual void test();
virtual ~Base();
};

void Base::test() {}

Base::~Base() {}

void test() {
Base *b = new Base();
b->test();
}
29 changes: 29 additions & 0 deletions test/llvm_test_code/type_hierarchies/type_hierarchy_15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Base {
int x;
public:
virtual void test();
virtual ~Base();
};

class Child: public Base {
public:
virtual ~Child();
};

// the definition of this method is missing
// void Base::test() {
// }

Base::~Base() {
}

Child::~Child() {
}

void use() {
Child *c = new Child();

delete c;
}


3 changes: 1 addition & 2 deletions unittests/PhasarLLVM/ControlFlow/LLVMBasedICFG_OTFTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ using namespace psr;

TEST(LLVMBasedICFG_OTFTest, VirtualCallSite_7) {
ProjectIRDB IRDB(
{unittest::PathToLLTestFiles + "call_graphs/virtual_call_7_cpp.ll"},
IRDBOptions::WPA);
{unittest::PathToLLTestFiles + "call_graphs/virtual_call_7_cpp.ll"});
IRDB.emitPreprocessedIR();
LLVMTypeHierarchy TH(IRDB);
LLVMPointsToSet PT(IRDB, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*****************************************************************************/

#include <iostream>
#include <memory>
#include <set>
#include <string>
#include <tuple>
Expand Down Expand Up @@ -40,7 +41,8 @@ class IDEInstInteractionAnalysisTest : public ::testing::Test {
using IIACompactResult_t =
std::tuple<std::string, std::size_t, std::string,
IDEInstInteractionAnalysisT<std::string, true>::l_t>;
ProjectIRDB *IRDB = nullptr;

std::unique_ptr<ProjectIRDB> IRDB;

void SetUp() override {
boost::log::core::get()->set_logging_enabled(false);
Expand All @@ -52,7 +54,8 @@ class IDEInstInteractionAnalysisTest : public ::testing::Test {
doAnalysisAndCompareResults(const std::string &LlvmFilePath,
const std::set<IIACompactResult_t> &GroundTruth,
bool PrintDump = false) {
IRDB = new ProjectIRDB({PathToLlFiles + LlvmFilePath}, IRDBOptions::WPA);
auto IR_Files = {PathToLlFiles + LlvmFilePath};
IRDB = std::make_unique<ProjectIRDB>(IR_Files, IRDBOptions::WPA);
if (PrintDump) {
IRDB->emitPreprocessedIR(std::cout, false);
}
Expand All @@ -61,8 +64,8 @@ class IDEInstInteractionAnalysisTest : public ::testing::Test {
LLVMPointsToSet PT(*IRDB);
LLVMBasedICFG ICFG(*IRDB, CallGraphAnalysisType::CHA, EntryPoints, &TH,
&PT);
IDEInstInteractionAnalysisT<std::string, true> IIAProblem(IRDB, &TH, &ICFG,
&PT, EntryPoints);
IDEInstInteractionAnalysisT<std::string, true> IIAProblem(
IRDB.get(), &TH, &ICFG, &PT, EntryPoints);
// use Phasar's instruction ids as testing labels
auto Generator = [](const llvm::Instruction *I) -> std::set<std::string> {
std::set<std::string> Labels;
Expand Down Expand Up @@ -103,7 +106,7 @@ class IDEInstInteractionAnalysisTest : public ::testing::Test {
}
}

void TearDown() override { delete IRDB; }
void TearDown() override {}

}; // Test Fixture

Expand Down
Loading

0 comments on commit f9b9ab8

Please sign in to comment.