Skip to content

Opaque Pointers Integration #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
d2c7bcb
replaced llvm-14 with llvm-15 in relevant files
mxHuber May 15, 2024
ab3fa86
annotation fix + test fix
mxHuber May 29, 2024
f703041
removed bitcasts and fixed tests
mxHuber Jun 3, 2024
b78d077
Fix Annotation.cpp + TaintConfigTest
fabianbs96 Jun 4, 2024
09e34aa
Quick-fix LLVMTypeHierarchyTest
fabianbs96 Jun 4, 2024
147ecd1
fix one IIA test
fabianbs96 Jun 4, 2024
fda53ef
Fix StringtestCpp for generalized LCA
fabianbs96 Jun 4, 2024
9b449d1
first half of tests fixed
mxHuber Jun 5, 2024
6b12b7b
fixed first half of tests
mxHuber Jun 5, 2024
bbe775a
fixed all but one test in IDEInstInteractionAnalysisTest
mxHuber Jun 6, 2024
c235afc
fixed all but two tests
mxHuber Jun 6, 2024
311cb68
Fix IIAFlowFact equality
fabianbs96 Jun 7, 2024
04a895d
fixed some newly failed tests
mxHuber Jun 12, 2024
e31c5ae
trade soundness for precision in LLVMAliasSet
fabianbs96 Jun 12, 2024
b6f4a10
Merge remote-tracking branch 'upstream/development' into f-clang-15
mxHuber Jun 12, 2024
e8c7f65
ci.yml update
mxHuber Jun 12, 2024
c3081ed
Revert "fixed some newly failed tests"
mxHuber Jun 12, 2024
6ced614
pre-commit hook
mxHuber Jun 12, 2024
12a9d06
Two Tests + xtaint09 test fix for pipeline
mxHuber Jun 12, 2024
9f5902a
Basic Opaque Pointer Impl, bugged
mxHuber Jun 13, 2024
812e6cb
switching to DebugInfoFinder
mxHuber Jun 13, 2024
8f89d1d
re-add the quick-fix for LLVMTypeHierarchy
fabianbs96 Jun 16, 2024
2bfffa8
OpaquePtr type mapping, missing subroutines
mxHuber Jun 17, 2024
2a91b6d
Introducing a pass to save ptr types
mxHuber Jun 18, 2024
d581987
Revert "Introducing a pass to save ptr types"
mxHuber Jun 19, 2024
9eb9929
moving phasar to DIBasedTypeHierarchy
mxHuber Jun 19, 2024
0b7575e
full switch to DIBasedTypeHierarchy + Test fixes
mxHuber Jun 23, 2024
5b39a18
fixed PathTracingTest
mxHuber Jun 25, 2024
85b0f24
dtaresolver deprecated and test fixes
mxHuber Jul 1, 2024
16c4a8e
Fixed OTFTest
mxHuber Jul 1, 2024
0514c7d
trimmed trailing whitespace
mxHuber Jul 1, 2024
2d8c9d8
minor fixes
mxHuber Jul 10, 2024
4463625
readded TypeToDIType map for RTAResolver
mxHuber Jul 10, 2024
df7d190
pre-commit clang-format fix
mxHuber Jul 10, 2024
ce2c6e8
pre-commit clang-format llvmbasedicfg.cpp
mxHuber Jul 10, 2024
8a4d812
moved RTAResolver to DITypes
mxHuber Jul 10, 2024
fd11ada
implemented review suggestions
mxHuber Aug 4, 2024
9c7f378
Log error if trying to instantiate DTAResolver + minor
fabianbs96 Aug 6, 2024
022b426
Add breaking changes
fabianbs96 Aug 6, 2024
d482f4d
Merge branch 'development' into f-clang-15
fabianbs96 Aug 8, 2024
b4783b9
Also compare gep type in IIA EqualGEPDescriptor
fabianbs96 Aug 9, 2024
49707eb
Merge branch 'development' into f-clang-15
fabianbs96 Aug 9, 2024
3848eef
Merge branch 'development' into f-clang-15
fabianbs96 Oct 4, 2024
0cc3c7f
Merge branch 'development' into f-clang-15
fabianbs96 Oct 24, 2024
70cdc6b
debugging CI
fabianbs96 Oct 24, 2024
417e584
debugging
fabianbs96 Oct 24, 2024
e5bc4c8
DITH fix attempt for CI
fabianbs96 Oct 24, 2024
7c2a330
Merge branch 'development' into f-clang-15
fabianbs96 Nov 28, 2024
02d27e8
fix build
fabianbs96 Nov 29, 2024
c6a6758
Start adding more sophisticated type extraction (WIP)
fabianbs Oct 7, 2024
2723068
Handle function calls in getVarTypeFromIR
fabianbs96 Oct 8, 2024
ecdc7c5
better fallback handling for getDebugLocation, etc
fabianbs96 Oct 8, 2024
d410c3c
cleanup getVarTypeFromIR
fabianbs96 Feb 6, 2025
c5e6859
Merge branch 'development' into f-clang-15
fabianbs96 Feb 6, 2025
b2c5cf9
Merge branch 'development' into f-clang-15
fabianbs96 Feb 14, 2025
66c2b6f
Merge branch 'development' into f-clang-15
fabianbs96 Feb 22, 2025
70874e9
Merge branch 'development' into f-clang-15
fabianbs96 Mar 3, 2025
f42adc9
Merge branch 'development' into f-clang-15
fabianbs96 Mar 4, 2025
fb28587
Add libzstd-dev as apt dependency; otherwise the docker build fails o…
fabianbs96 Mar 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
dtaresolver deprecated and test fixes
  • Loading branch information
mxHuber committed Jul 1, 2024
commit 85b0f24f98553a32a8ee5ddde603ab1e701baa92
9 changes: 2 additions & 7 deletions include/phasar/PhasarLLVM/ControlFlow/LLVMVFTableProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace llvm {
class Module;
class StructType;
class DIType;
class GlobalVariable;
} // namespace llvm

Expand All @@ -30,17 +30,12 @@ class LLVMVFTableProvider {
explicit LLVMVFTableProvider(const llvm::Module &Mod);
explicit LLVMVFTableProvider(const LLVMProjectIRDB &IRDB);

[[nodiscard]] bool hasVFTable(const llvm::StructType *Type) const;
[[nodiscard]] bool hasVFTable(const llvm::DIType *Type) const;
[[nodiscard]] const LLVMVFTable *
getVFTableOrNull(const llvm::StructType *Type) const;
[[nodiscard]] const LLVMVFTable *
getVFTableOrNull(const llvm::DIType *Type) const;

private:
std::unordered_map<const llvm::StructType *, LLVMVFTable> TypeVFTMap;
std::unordered_map<const llvm::DIType *, LLVMVFTable> DITypeVFTMap;
std::map<const llvm::DIType *, const llvm::Type *> DITypeToType;
std::unordered_map<const llvm::DIType *, LLVMVFTable> TypeVFTMap;
};
} // namespace psr

Expand Down
20 changes: 13 additions & 7 deletions include/phasar/PhasarLLVM/ControlFlow/Resolver/DTAResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,33 @@ class DTAResolver : public CHAResolver {
* An heuristic that return true if the bitcast instruction is interesting to
* take into the DTA relational graph
*/
static bool
[[deprecated("Does not work with opaque pointers anymore")]] static bool
heuristicAntiConstructorThisType(const llvm::BitCastInst *BitCast);

/**
* Another heuristic that return true if the bitcast instruction is
* interesting to take into the DTA relational graph (use the presence or not
* of vtable)
*/
bool heuristicAntiConstructorVtablePos(const llvm::BitCastInst *BitCast);
[[deprecated("Does not work with opaque pointers anymore")]] bool
heuristicAntiConstructorVtablePos(const llvm::BitCastInst *BitCast);

public:
DTAResolver(const LLVMProjectIRDB *IRDB, const LLVMVFTableProvider *VTP,
const DIBasedTypeHierarchy *TH);
[[deprecated("Does not work with opaque pointers anymore")]] DTAResolver(
const LLVMProjectIRDB *IRDB, const LLVMVFTableProvider *VTP,
const DIBasedTypeHierarchy *TH);

~DTAResolver() override = default;

FunctionSetTy resolveVirtualCall(const llvm::CallBase *CallSite) override;
[[deprecated("Does not work with opaque pointers anymore")]] FunctionSetTy
resolveVirtualCall(const llvm::CallBase *CallSite) override;

void otherInst(const llvm::Instruction *Inst) override;
[[deprecated("Does not work with opaque pointers anymore")]] void
otherInst(const llvm::Instruction *Inst) override;

[[nodiscard]] std::string str() const override;
[[nodiscard]] [[deprecated(
"Does not work with opaque pointers anymore")]] std::string
str() const override;
};
} // namespace psr

Expand Down
4 changes: 0 additions & 4 deletions include/phasar/PhasarLLVM/ControlFlow/Resolver/Resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ class Resolver {
getNonPureVirtualVFTEntry(const llvm::DIType *T, unsigned Idx,
const llvm::CallBase *CallSite);

const llvm::Function *
getNonPureVirtualVFTEntry(const llvm::StructType *T, unsigned Idx,
const llvm::CallBase *CallSite);

public:
using FunctionSetTy = llvm::SmallDenseSet<const llvm::Function *, 4>;

Expand Down
1 change: 0 additions & 1 deletion include/phasar/PhasarLLVM/TypeHierarchy/LLVMVFTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace psr {
*/
class LLVMVFTable : public VFTable<const llvm::Function *> {
private:
friend class LLVMTypeHierarchy;
friend class DIBasedTypeHierarchy;
std::vector<const llvm::Function *> VFT;

Expand Down
121 changes: 38 additions & 83 deletions lib/PhasarLLVM/ControlFlow/LLVMVFTableProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "phasar/PhasarLLVM/Utils/LLVMIRToSrc.h"
#include "phasar/Utils/Logger.h"

#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/Demangle/Demangle.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
Expand All @@ -15,57 +16,41 @@

using namespace psr;

static std::vector<const llvm::Function *> getVirtualFunctions(
const llvm::StringMap<const llvm::GlobalVariable *> &ClearNameTVMap,
const llvm::StructType &Type) {
auto ClearName =
DIBasedTypeHierarchy::removeStructOrClassPrefix(Type.getName());

auto It = ClearNameTVMap.find(ClearName);

if (It != ClearNameTVMap.end()) {
if (const auto *TI = llvm::dyn_cast<llvm::GlobalVariable>(It->second)) {
if (!TI->hasInitializer()) {
PHASAR_LOG_LEVEL_CAT(DEBUG, "DIBasedTypeHierarchy",
ClearName << " does not have initializer");
return {};
}
if (const auto *I =
llvm::dyn_cast<llvm::ConstantStruct>(TI->getInitializer())) {
return LLVMVFTable::getVFVectorFromIRVTable(*I);
}
}
static std::string getTypeName(const llvm::DIType *DITy) {
if (const auto *CompTy = llvm::dyn_cast<llvm::DICompositeType>(DITy)) {
auto Ident = CompTy->getIdentifier();
return Ident.empty() ? llvm::demangle(CompTy->getName().str())
: llvm::demangle(Ident.str());
}
return {};
return llvm::demangle(DITy->getName().str());
}

static std::vector<const llvm::Function *> getVirtualFunctionsDIBased(
static std::vector<const llvm::Function *> getVirtualFunctions(
const llvm::StringMap<const llvm::GlobalVariable *> &ClearNameTVMap,
const llvm::DIType &Type) {
auto ClearName =
DIBasedTypeHierarchy::removeStructOrClassPrefix(Type.getName());
const llvm::DIType *Type) {
auto ClearName = getTypeName(Type);

if (ClearName.substr(0, 18) == "typeinfo name for ") {
ClearName = ClearName.substr(18, ClearName.size() - 1);
}

auto It = ClearNameTVMap.find(ClearName);

if (It != ClearNameTVMap.end()) {
if (const auto *TI = llvm::dyn_cast<llvm::GlobalVariable>(It->second)) {
if (!TI->hasInitializer()) {
PHASAR_LOG_LEVEL_CAT(DEBUG, "DIBasedTypeHierarchy",
ClearName << " does not have initializer");
return {};
}
if (const auto *I =
llvm::dyn_cast<llvm::ConstantStruct>(TI->getInitializer())) {
return LLVMVFTable::getVFVectorFromIRVTable(*I);
}
if (!It->second->hasInitializer()) {
PHASAR_LOG_LEVEL_CAT(DEBUG, "DIBasedTypeHierarchy",
ClearName << " does not have initializer");
return {};
}
if (const auto *I = llvm::dyn_cast<llvm::ConstantStruct>(
It->second->getInitializer())) {
return LLVMVFTable::getVFVectorFromIRVTable(*I);
}
}
return {};
}

LLVMVFTableProvider::LLVMVFTableProvider(const llvm::Module &Mod) {
auto StructTypes = Mod.getIdentifiedStructTypes();

llvm::StringMap<const llvm::GlobalVariable *> ClearNameTVMap;

for (const auto &Glob : Mod.globals()) {
Expand All @@ -76,63 +61,33 @@ LLVMVFTableProvider::LLVMVFTableProvider(const llvm::Module &Mod) {
}
}

for (const auto *Ty : StructTypes) {
TypeVFTMap.try_emplace(Ty, getVirtualFunctions(ClearNameTVMap, *Ty));
}
}

LLVMVFTableProvider::LLVMVFTableProvider(const LLVMProjectIRDB &IRDB)
: LLVMVFTableProvider(*IRDB.getModule()) {
for (const auto *Instr : IRDB.getAllInstructions()) {
if (const auto *Val = llvm::dyn_cast<llvm::Value>(Instr)) {
if (const auto *DILocalVar = getDILocalVariable(Val)) {
if (const auto *DerivedTy =
llvm::dyn_cast<llvm::DIDerivedType>(DILocalVar->getType())) {
DITypeToType[DerivedTy->getBaseType()] = Val->getType();
continue;
llvm::DebugInfoFinder DIF;
DIF.processModule(Mod);
for (const auto *Ty : DIF.types()) {
if (const auto *DerivedTy = llvm::dyn_cast<llvm::DIDerivedType>(Ty)) {
if (const auto *BaseTy = DerivedTy->getBaseType()) {
if (const auto *CompTy =
llvm::dyn_cast<llvm::DICompositeType>(BaseTy)) {
if (CompTy->getTag() == llvm::dwarf::DW_TAG_class_type ||
CompTy->getTag() == llvm::dwarf::DW_TAG_structure_type) {
TypeVFTMap.try_emplace(CompTy,
getVirtualFunctions(ClearNameTVMap, CompTy));
}
}
DITypeToType[DILocalVar->getType()] = Val->getType();
}
}
}

llvm::StringMap<const llvm::GlobalVariable *> ClearNameTVMap;

for (const auto &Glob : IRDB.getModule()->globals()) {
if (DIBasedTypeHierarchy::isVTable(Glob.getName())) {
auto Demang = llvm::demangle(Glob.getName().str());
auto ClearName = DIBasedTypeHierarchy::removeVTablePrefix(Demang);
ClearNameTVMap.try_emplace(ClearName, &Glob);
}
}

for (const auto &Elem : DITypeToType) {
DITypeVFTMap.try_emplace(
Elem.first, getVirtualFunctionsDIBased(ClearNameTVMap, *Elem.first));
}
}

bool LLVMVFTableProvider::hasVFTable(const llvm::StructType *Type) const {
return TypeVFTMap.count(Type);
}
LLVMVFTableProvider::LLVMVFTableProvider(const LLVMProjectIRDB &IRDB)
: LLVMVFTableProvider(*IRDB.getModule()) {}

bool LLVMVFTableProvider::hasVFTable(const llvm::DIType *Type) const {
return DITypeToType.count(Type);
return TypeVFTMap.count(Type);
}

const LLVMVFTable *
LLVMVFTableProvider::getVFTableOrNull(const llvm::StructType *Type) const {
LLVMVFTableProvider::getVFTableOrNull(const llvm::DIType *Type) const {
auto It = TypeVFTMap.find(Type);
return It != TypeVFTMap.end() ? &It->second : nullptr;
}

const LLVMVFTable *
LLVMVFTableProvider::getVFTableOrNull(const llvm::DIType *Type) const {
if (const auto *Ty = DITypeToType.at(Type)) {
if (const auto *StructTy = llvm::dyn_cast<llvm::StructType>(Ty)) {
return getVFTableOrNull(StructTy);
}
}

return nullptr;
}
23 changes: 22 additions & 1 deletion lib/PhasarLLVM/ControlFlow/Resolver/DTAResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "llvm/IR/Operator.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"

#include <memory>

Expand All @@ -45,6 +46,10 @@ DTAResolver::DTAResolver(const LLVMProjectIRDB *IRDB,

bool DTAResolver::heuristicAntiConstructorThisType(
const llvm::BitCastInst *BitCast) {

llvm::report_fatal_error("Does not work with opaque pointers anymore");

#if 0
// We check if the caller is a constructor, and if the this argument has the
// same type as the source type of the bitcast. If it is the case, it returns
// false, true otherwise.
Expand All @@ -62,13 +67,20 @@ bool DTAResolver::heuristicAntiConstructorThisType(
}

return true;
#endif
}

bool DTAResolver::heuristicAntiConstructorVtablePos(
const llvm::BitCastInst *BitCast) {

llvm::report_fatal_error("Does not work with opaque pointers anymore");

#if 0

// Better heuristic than the previous one, can handle the CRTP. Based on the
// previous one.


if (heuristicAntiConstructorThisType(BitCast)) {
return true;
}
Expand Down Expand Up @@ -142,9 +154,14 @@ bool DTAResolver::heuristicAntiConstructorVtablePos(
}

return (BitcastNum > VtableNum);
#endif
}

void DTAResolver::otherInst(const llvm::Instruction *Inst) {

llvm::report_fatal_error("Does not work with opaque pointers anymore");

#if 0
if (Inst->getType()->isOpaquePointerTy()) {
/// XXX: We may want to get these information on a different way, e.g. by
/// analyzing the debug info
Expand All @@ -165,10 +182,13 @@ void DTAResolver::otherInst(const llvm::Instruction *Inst) {
TypeGraph.addLink(DestStructType, SrcStructType);
}
}
#endif
}

auto DTAResolver::resolveVirtualCall(const llvm::CallBase *CallSite)
-> FunctionSetTy {
llvm::report_fatal_error("Does not work with opaque pointers anymore");

#if 0
FunctionSetTy PossibleCallTargets;

PHASAR_LOG_LEVEL(DEBUG,
Expand Down Expand Up @@ -220,6 +240,7 @@ auto DTAResolver::resolveVirtualCall(const llvm::CallBase *CallSite)
#endif

return PossibleCallTargets;
#endif
}

std::string DTAResolver::str() const { return "DTA"; }
22 changes: 1 addition & 21 deletions lib/PhasarLLVM/ControlFlow/Resolver/Resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,6 @@ Resolver::getNonPureVirtualVFTEntry(const llvm::DIType *T, unsigned Idx,
return nullptr;
}

if (const auto *StructTy =
llvm::dyn_cast<llvm::StructType>(DITypeToType[T])) {
if (const auto *VT = VTP->getVFTableOrNull(StructTy)) {
const auto *Target = VT->getFunction(Idx);
if (Target &&
Target->getName() != DIBasedTypeHierarchy::PureVirtualCallName &&
isConsistentCall(CallSite, Target)) {
return Target;
}
}
}

return nullptr;
}

const llvm::Function *
Resolver::getNonPureVirtualVFTEntry(const llvm::StructType *T, unsigned Idx,
const llvm::CallBase *CallSite) {
if (!VTP) {
return nullptr;
}
if (const auto *VT = VTP->getVFTableOrNull(T)) {
const auto *Target = VT->getFunction(Idx);
if (Target &&
Expand All @@ -208,6 +187,7 @@ Resolver::getNonPureVirtualVFTEntry(const llvm::StructType *T, unsigned Idx,
return Target;
}
}

return nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

using namespace std;
using namespace psr;

TEST(LLVMBasedICFG_DTATest, VirtualCallSite_5) {
#if 0
LLVMProjectIRDB IRDB(unittest::PathToLLTestFiles +
"call_graphs/virtual_call_5_cpp.ll");
DIBasedTypeHierarchy TH(IRDB);
Expand All @@ -38,9 +38,11 @@ TEST(LLVMBasedICFG_DTATest, VirtualCallSite_5) {
ASSERT_TRUE(llvm::is_contained(ICFG.getCallersOf(VFuncA), I));
ASSERT_TRUE(llvm::is_contained(ICFG.getCallersOf(VFuncB), I));
}
#endif
}

TEST(LLVMBasedICFG_DTATest, VirtualCallSite_6) {
#if 0
LLVMProjectIRDB IRDB(unittest::PathToLLTestFiles +
"call_graphs/virtual_call_6_cpp.ll");
DIBasedTypeHierarchy TH(IRDB);
Expand All @@ -57,6 +59,7 @@ TEST(LLVMBasedICFG_DTATest, VirtualCallSite_6) {
const auto &Callers = ICFG.getCallersOf(VFuncA);
ASSERT_EQ(Callers.size(), 1U);
ASSERT_TRUE(llvm::is_contained(Callers, I));
#endif
}

int main(int Argc, char **Argv) {
Expand Down
Loading