Skip to content

Commit e4e778b

Browse files
committed
Merge branch 'blipper-llvmcxxshorthands' into development
2 parents 7ed6898 + d50a4bd commit e4e778b

4 files changed

Lines changed: 64 additions & 44 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef INCLUDE_PHASAR_UTILS_LLVMCXXSHORTHANDS_H_
2+
#define INCLUDE_PHASAR_UTILS_LLVMCXXSHORTHANDS_H_
3+
// This contains LLVM IR Shorthands specific to C++
4+
// See https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io/en/latest/object-oriented-constructs/classes.html
5+
// for examples
6+
7+
#include "llvm/IR/Instructions.h"
8+
#include "llvm/IR/Value.h"
9+
10+
namespace psr {
11+
bool isTouchVTableInst(const llvm::StoreInst *Store);
12+
}
13+
#endif // INCLUDE_PHASAR_UTILS_LLVMCXXSHORTHANDS_H_

include/phasar/Utils/LLVMShorthands.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,12 @@
2020
#include <string>
2121
#include <vector>
2222

23+
#include "llvm/IR/Instructions.h"
24+
#include "llvm/IR/Function.h"
2325
#include "llvm/IR/Value.h"
2426

2527
#include "phasar/Utils/Utilities.h"
2628

27-
namespace llvm {
28-
class CallInst;
29-
class FunctionType;
30-
class Function;
31-
class Argument;
32-
class Instruction;
33-
class TerminatorInst;
34-
class StoreInst;
35-
class Module;
36-
class StringRef;
37-
class BranchInst;
38-
} // namespace llvm
39-
4029
namespace psr {
4130

4231
static inline void deleteValue(llvm::Value *V) { V->deleteValue(); }

lib/PhasarLLVM/DataFlowSolver/IfdsIde/Problems/IFDSConstAnalysis.cpp

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "phasar/Utils/LLVMIRToSrc.h"
2929
#include "phasar/Utils/LLVMShorthands.h"
30+
#include "phasar/Utils/LLVMCXXShorthands.h"
3031
#include "phasar/Utils/Logger.h"
3132
#include "phasar/Utils/PAMMMacros.h"
3233
#include "phasar/Utils/Utilities.h"
@@ -55,37 +56,10 @@ IFDSConstAnalysis::getNormalFlowFunction(IFDSConstAnalysis::n_t Curr,
5556
if (const auto *Store = llvm::dyn_cast<llvm::StoreInst>(Curr)) {
5657
// If the store instruction sets up or updates the vtable, i.e. value
5758
// operand is vtable pointer, ignore it!
58-
// Setting up the vtable is counted towards the initialization of an
59-
// object - the object stays immutable.
60-
// To identifiy such a store instruction, we need to check the stored
61-
// value, which is of i32 (...)** type, e.g.
62-
// i32 (...)** bitcast (i8** getelementptr inbounds ([3 x i8*], [3 x i8*]*
63-
// @_ZTV5Child, i32 0, i32 2) to i32 (...)**)
64-
//
65-
// WARNING: The VTT could also be stored, which would make this analysis
66-
// fail
67-
if (const auto *CE =
68-
llvm::dyn_cast<llvm::ConstantExpr>(Store->getValueOperand())) {
69-
// llvm::ConstantExpr *CE = const_cast<llvm::ConstantExpr *>(ConstCE);
70-
std::unique_ptr<llvm::Instruction, decltype(&deleteValue)> CEInst(
71-
CE->getAsInstruction(), &deleteValue);
72-
if (auto *CF =
73-
llvm::dyn_cast<llvm::ConstantExpr>(CEInst->getOperand(0))) {
74-
std::unique_ptr<llvm::Instruction, decltype(&deleteValue)> CFInst(
75-
CF->getAsInstruction(), &deleteValue);
76-
if (auto *VTable =
77-
llvm::dyn_cast<llvm::GlobalVariable>(CFInst->getOperand(0))) {
78-
if (VTable->hasName() &&
79-
llvm::demangle(VTable->getName().str()).find("vtable") !=
80-
string::npos) {
81-
LOG_IF_ENABLE(
82-
BOOST_LOG_SEV(lg::get(), DEBUG)
83-
<< "Store Instruction sets up or updates vtable - ignored!");
84-
return Identity<IFDSConstAnalysis::d_t>::getInstance();
85-
}
86-
}
87-
}
88-
} /* end vtable set-up instruction */
59+
if (isTouchVTableInst(Store)) {
60+
return Identity<IFDSConstAnalysis::d_t>::getInstance();
61+
}
62+
8963
IFDSConstAnalysis::d_t PointerOp = Store->getPointerOperand();
9064
LOG_IF_ENABLE(BOOST_LOG_SEV(lg::get(), DEBUG)
9165
<< "Pointer operand of store Instruction: "

lib/Utils/LLVMCXXShorthands.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "phasar/Utils/LLVMCXXShorthands.h"
2+
#include <llvm/Demangle/Demangle.h>
3+
#include <llvm/IR/Constants.h>
4+
#include "llvm/IR/GlobalVariable.h"
5+
#include "llvm/IR/Instructions.h"
6+
#include <phasar/Utils/Logger.h>
7+
8+
namespace psr {
9+
// Setting up the vtable is counted towards the initialization of an
10+
// object - the object stays immutable.
11+
// To identifiy such a store instruction, we need to check the stored
12+
// value, which is of i32 (...)** type, e.g.
13+
// i32 (...)** bitcast (i8** getelementptr inbounds ([3 x i8*], [3 x i8*]*
14+
// @_ZTV5Child, i32 0, i32 2) to i32 (...)**)
15+
//
16+
// WARNING: The VTT could also be stored, which would make this analysis
17+
// fail
18+
bool isTouchVTableInst(const llvm::StoreInst *Store) {
19+
if (const auto *CE =
20+
llvm::dyn_cast<llvm::ConstantExpr>(Store->getValueOperand())) {
21+
// llvm::ConstantExpr *CE = const_cast<llvm::ConstantExpr *>(ConstCE);
22+
auto *CEInst = const_cast<llvm::ConstantExpr *>(CE)->getAsInstruction();
23+
if (auto *CF = llvm::dyn_cast<llvm::ConstantExpr>(CEInst->getOperand(0))) {
24+
auto *CFInst = CF->getAsInstruction();
25+
if (auto *VTable =
26+
llvm::dyn_cast<llvm::GlobalVariable>(CFInst->getOperand(0))) {
27+
if (VTable->hasName() &&
28+
llvm::demangle(VTable->getName().str()).find("vtable") !=
29+
std::string::npos) {
30+
LOG_IF_ENABLE(
31+
BOOST_LOG_SEV(lg::get(), DEBUG)
32+
<< "Store Instruction sets up or updates vtable - ignored!");
33+
CEInst->deleteValue();
34+
CFInst->deleteValue();
35+
return true;
36+
}
37+
}
38+
CFInst->deleteValue();
39+
}
40+
CEInst->deleteValue();
41+
} /* end vtable set-up instruction */
42+
return false;
43+
}
44+
}

0 commit comments

Comments
 (0)