Skip to content

Commit b54ee4c

Browse files
committed
Apply further comments
1 parent 9e341f5 commit b54ee4c

12 files changed

Lines changed: 133 additions & 142 deletions

File tree

include/phasar/PhasarLLVM/DataFlowSolver/IfdsIde/Problems/ExtendedTaintAnalysis/AbstractMemoryLocation.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ class AbstractMemoryLocationImpl final : public AbstractMemoryLoactionStorage {
117117
/// Are *this and TV equivalent?
118118
[[nodiscard]] bool equivalent(const AbstractMemoryLocationImpl &TV) const;
119119

120-
[[nodiscard]] bool equivalentExceptPointerArithmetics(
121-
const AbstractMemoryLocationImpl &TV) const;
120+
[[nodiscard]] bool
121+
equivalentExceptPointerArithmetics(const AbstractMemoryLocationImpl &TV,
122+
unsigned PALevel = 1) const;
122123

123124
/// Are *this and TV equivalent wrt aliasing?
124125
bool mustAlias(

include/phasar/PhasarLLVM/DataFlowSolver/IfdsIde/Problems/IDEExtendedTaintAnalysis.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ class IDEExtendedTaintAnalysis
123123
/// llvm::StoreInst, llvm::MemSetInst, etc.
124124
FlowFunctionPtrType getStoreFF(const llvm::Value *PointerOp,
125125
const llvm::Value *ValueOp,
126-
const llvm::Instruction *Store);
126+
const llvm::Instruction *Store,
127+
unsigned PALevel = 1);
127128

128129
void populateWithMayAliases(SourceConfigTy &Facts) const;
129130

include/phasar/PhasarLLVM/DataFlowSolver/IfdsIde/Problems/IFDSFieldSensTaintAnalysis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ class IFDSFieldSensTaintAnalysis
119119
}
120120

121121
private:
122-
const TaintConfig &taintConfig;
122+
const TaintConfig &Config;
123123

124-
TraceStats traceStats;
124+
TraceStats Stats;
125125
};
126126

127127
} // namespace psr

include/phasar/PhasarLLVM/TaintConfig/TaintConfig.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ class TaintConfig {
6161

6262
TaintConfig(const psr::ProjectIRDB &Code, const nlohmann::json &Config);
6363
TaintConfig(const psr::ProjectIRDB &AnnotatedCode);
64-
TaintConfig(TaintDescriptionCallBackTy SourceCB,
65-
TaintDescriptionCallBackTy SinkCB);
64+
TaintConfig(
65+
TaintDescriptionCallBackTy SourceCB, TaintDescriptionCallBackTy SinkCB,
66+
TaintDescriptionCallBackTy SanitizerCB = TaintDescriptionCallBackTy{});
67+
68+
void registerSourceCallBack(TaintDescriptionCallBackTy CB);
69+
void registerSinkCallBack(TaintDescriptionCallBackTy CB);
70+
void registerSanitizerCallBack(TaintDescriptionCallBackTy CB);
6671

67-
void registerSourceCallBack(const TaintDescriptionCallBackTy &CB);
68-
void registerSinkCallBack(const TaintDescriptionCallBackTy &CB);
6972
[[nodiscard]] const TaintDescriptionCallBackTy &
7073
getRegisteredSourceCallBack() const;
7174
[[nodiscard]] const TaintDescriptionCallBackTy &
@@ -129,6 +132,7 @@ class TaintConfig {
129132
std::unordered_set<const llvm::Value *> SanitizerValues;
130133
TaintDescriptionCallBackTy SourceCallBack;
131134
TaintDescriptionCallBackTy SinkCallBack;
135+
TaintDescriptionCallBackTy SanitizerCallBack;
132136
};
133137

134138
//===----------------------------------------------------------------------===//

lib/PhasarLLVM/DataFlowSolver/IfdsIde/Problems/ExtendedTaintAnalysis/AbstractMemoryLocation.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,16 @@ bool AbstractMemoryLocationImpl::equivalent(
9696
}
9797

9898
bool AbstractMemoryLocationImpl::equivalentExceptPointerArithmetics(
99-
const AbstractMemoryLocationImpl &TV) const {
99+
const AbstractMemoryLocationImpl &TV, unsigned PALevel) const {
100100
if (base() != TV.base()) {
101101
return false;
102102
}
103103
size_t MinSize = std::min(offsets().size(), TV.offsets().size());
104-
if (MinSize == 1) {
104+
if (MinSize <= PALevel) {
105105
return true;
106106
}
107-
return offsets().slice(0, MinSize - 1) == TV.offsets().slice(0, MinSize - 1);
107+
return offsets().slice(0, MinSize - PALevel) ==
108+
TV.offsets().slice(0, MinSize - PALevel);
108109
}
109110

110111
bool AbstractMemoryLocationImpl::mustAlias(
@@ -195,7 +196,7 @@ std::ostream &operator<<(std::ostream &OS, const AbstractMemoryLocation &TV) {
195196

196197
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
197198
const AbstractMemoryLocation &TV) {
198-
// TODO: better representation
199+
// -> Think about better representation
199200
OS << "(";
200201
if (LLVMZeroValue::getInstance()->isLLVMZeroValue(TV->base())) {
201202
OS << "<ZERO>";

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

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <type_traits>
1212

1313
#include "llvm/ADT/SmallSet.h"
14+
#include "llvm/IR/IntrinsicInst.h"
15+
#include "llvm/Support/Casting.h"
1416

1517
#include "phasar/DB/ProjectIRDB.h"
1618
#include "phasar/PhasarLLVM/ControlFlow/LLVMBasedICFG.h"
@@ -30,22 +32,6 @@
3032
#include "phasar/Utils/Utilities.h"
3133

3234
namespace psr::XTaint {
33-
// Misc:
34-
35-
template <typename ContainerTy>
36-
inline void printValues(const ContainerTy &Facts,
37-
std::ostream &OS = std::cerr) {
38-
OS << "{";
39-
40-
for (const auto *Fact : Facts) {
41-
OS << "\n\t" << llvmIRToString(Fact);
42-
}
43-
44-
if (!Facts.empty()) {
45-
OS << "\n";
46-
}
47-
OS << "}";
48-
}
4935

5036
IDEExtendedTaintAnalysis::IDEExtendedTaintAnalysis(
5137
const ProjectIRDB *IRDB, const LLVMTypeHierarchy *TH,
@@ -138,15 +124,16 @@ IDEExtendedTaintAnalysis::getNormalFlowFunction(n_t Curr,
138124
IDEExtendedTaintAnalysis::FlowFunctionPtrType
139125
IDEExtendedTaintAnalysis::getStoreFF(const llvm::Value *PointerOp,
140126
const llvm::Value *ValueOp,
141-
const llvm::Instruction *Store) {
127+
const llvm::Instruction *Store,
128+
unsigned PALevel) {
142129

143130
auto TV = makeFlowFact(ValueOp);
144131
auto PTS = this->PT->getPointsToSet(PointerOp, Store);
145132

146133
auto Mem = makeFlowFact(PointerOp);
147134
return makeLambdaFlow<d_t>([this, TV, Mem, PTS{std::move(PTS)}, PointerOp,
148-
ValueOp,
149-
Store](d_t Source) mutable -> std::set<d_t> {
135+
ValueOp, Store,
136+
PALevel](d_t Source) mutable -> std::set<d_t> {
150137
if (Source->isZero()) {
151138
std::set<d_t> Ret = {Source};
152139
generateFromZero(Ret, Store, PointerOp, ValueOp,
@@ -160,7 +147,7 @@ IDEExtendedTaintAnalysis::getStoreFF(const llvm::Value *PointerOp,
160147
/// easily reachable from TV by simply doing dome pointer arithmetics.
161148
/// Hence, when loading the value of TV back from Mem this still holds and
162149
/// must be preserved by the analysis.
163-
if (TV->equivalentExceptPointerArithmetics(Source)) {
150+
if (TV->equivalentExceptPointerArithmetics(Source, PALevel)) {
164151
auto Offset = Source - TV;
165152

166153
// generate all may-aliases of Store->getPointerOperand()
@@ -322,17 +309,14 @@ IDEExtendedTaintAnalysis::getCallFlowFunction(n_t CallStmt, f_t DestFun) {
322309
return {Source};
323310
}
324311
std::set<d_t> Ret;
325-
/// Don't qualify the 'auto' here, because we should not rely on those
326-
/// iterators to be pointers
327-
328-
// NOLINTNEXTLINE(llvm-qualified-auto, readability-qualified-auto)
329-
auto It = call->arg_begin();
330-
// NOLINTNEXTLINE(llvm-qualified-auto, readability-qualified-auto)
331-
auto End = call->arg_end();
332-
// NOLINTNEXTLINE(llvm-qualified-auto, readability-qualified-auto)
333-
auto FIt = DestFun->arg_begin();
334-
// NOLINTNEXTLINE(llvm-qualified-auto, readability-qualified-auto)
335-
auto FEnd = DestFun->arg_end();
312+
313+
using ArgIterator = llvm::User::const_op_iterator;
314+
using ParamIterator = llvm::Function::const_arg_iterator;
315+
316+
ArgIterator It = call->arg_begin();
317+
ArgIterator End = call->arg_end();
318+
ParamIterator FIt = DestFun->arg_begin();
319+
ParamIterator FEnd = DestFun->arg_end();
336320

337321
const std::string CalleeName =
338322
(DestFun->hasName()) ? DestFun->getName().str() : "none";
@@ -390,17 +374,13 @@ IDEExtendedTaintAnalysis::getRetFlowFunction(n_t CallSite, f_t CalleeFun,
390374
d_t Source) {
391375
std::set<d_t> Ret;
392376

393-
/// Don't qualify the 'auto' here, because we should not rely on those
394-
/// iterators to be pointers
377+
using ArgIterator = llvm::User::const_op_iterator;
378+
using ParamIterator = llvm::Function::const_arg_iterator;
395379

396-
// NOLINTNEXTLINE(llvm-qualified-auto, readability-qualified-auto)
397-
auto It = Call->arg_begin();
398-
// NOLINTNEXTLINE(llvm-qualified-auto, readability-qualified-auto)
399-
auto End = Call->arg_end();
400-
// NOLINTNEXTLINE(llvm-qualified-auto, readability-qualified-auto)
401-
auto FIt = CalleeFun->arg_begin();
402-
// NOLINTNEXTLINE(llvm-qualified-auto, readability-qualified-auto)
403-
auto FEnd = CalleeFun->arg_end();
380+
ArgIterator It = Call->arg_begin();
381+
ArgIterator End = Call->arg_end();
382+
ParamIterator FIt = CalleeFun->arg_begin();
383+
ParamIterator FEnd = CalleeFun->arg_end();
404384

405385
for (; FIt != FEnd && It != End; ++FIt, ++It) {
406386
// Only map back pointer parameters, since for all others we have
@@ -458,12 +438,18 @@ IDEExtendedTaintAnalysis::getSummaryFlowFunction(n_t CallStmt, f_t DestFun) {
458438
return handleConfig(CallStmt, std::move(SrcConfig), std::move(SinkConfig));
459439
}
460440

461-
/// TODO: MemSet
462-
// ...
463-
// } else if (auto MemSet = llvm::dyn_cast<llvm::MemSetInst>(call)) {
464-
// // Basically, MemSet is the same as Store
465-
// return getStoreFF(MemSet->getDest(), MemSet->getValue(), MemSet);
466-
// }
441+
if (const auto *MemSet = llvm::dyn_cast<llvm::MemSetInst>(CallStmt)) {
442+
/// Basically, MemSet is the same as Store
443+
return getStoreFF(MemSet->getRawDest(), MemSet->getValue(), MemSet);
444+
}
445+
446+
if (const auto *MemTrn = llvm::dyn_cast<llvm::MemTransferInst>(CallStmt)) {
447+
/// Basically, MemCpy/MemMove are the same as Store.
448+
/// We just need to take care about the additional level of indirection
449+
/// i.e., not the source itself is stored, but the value it is pointing to
450+
return getStoreFF(MemTrn->getRawDest(), MemTrn->getRawSource(), MemTrn,
451+
/*PALevel*/ 2);
452+
}
467453

468454
return nullptr;
469455
}
@@ -484,8 +470,6 @@ auto IDEExtendedTaintAnalysis::getNormalEdgeFunction(n_t Curr, d_t CurrNode,
484470

485471
if (EntryPoints.count(Curr->getFunction()->getName().str()) &&
486472
Curr == &Curr->getFunction()->front().front()) {
487-
// std::cout << "edge seed: " << CurrNode << " --> " << SuccNode
488-
// << " with null\n";
489473
return getGenEdgeFunction(BBO);
490474
}
491475

@@ -494,10 +478,7 @@ auto IDEExtendedTaintAnalysis::getNormalEdgeFunction(n_t Curr, d_t CurrNode,
494478
if (const auto *Store = llvm::dyn_cast<llvm::StoreInst>(Curr)) {
495479
return {Store->getPointerOperand(), Store->getValueOperand()};
496480
}
497-
/// TODO: MemSetInst inherits from CallInst, so move it to summaryEF
498-
// if (const auto *MemSet = llvm::dyn_cast<llvm::MemSetInst>(Curr)) {
499-
// return {MemSet->getDest(), MemSet->getValue()};
500-
// }
481+
501482
return {nullptr, nullptr};
502483
}();
503484

@@ -516,17 +497,12 @@ auto IDEExtendedTaintAnalysis::getNormalEdgeFunction(n_t Curr, d_t CurrNode,
516497

517498
auto SaniConfig = getSanitizerConfigAt(Curr);
518499
if (!SaniConfig.empty()) {
519-
// std::cerr << "NormalEF: handleEdgeConfig at " << llvmIRToString(Curr)
520-
// << " on " << CurrNode << " --> " << SuccNode << "\n";
521500
if (isMustAlias(SaniConfig, CurrNode)) {
522501
return makeEF<GenEdgeFunction>(BBO, Curr);
523502
}
524503
}
525504
}
526505

527-
// std::cerr << "StoreInst with EdgeID at " << llvmIRToString(Curr) << " on "
528-
// << CurrNode << " --> " << SuccNode << "\n";
529-
530506
return getEdgeIdentity(Curr);
531507
}
532508

@@ -642,7 +618,11 @@ auto IDEExtendedTaintAnalysis::getSummaryEdgeFunction(n_t Curr, d_t CurrNode,
642618
return makeEF<GenEdgeFunction>(BBO, Curr);
643619
}
644620

645-
/// TODO: MemSet
621+
// MemIntrinsic covers memset, memcpy and memmove
622+
if (const auto *MemSet = llvm::dyn_cast<llvm::MemIntrinsic>(Curr);
623+
MemSet && CurrNode->mustAlias(makeFlowFact(MemSet->getRawDest()), *PT)) {
624+
return makeEF<GenEdgeFunction>(BBO, Curr);
625+
}
646626

647627
return Ret;
648628
}
@@ -782,11 +762,6 @@ void IDEExtendedTaintAnalysis::doPostProcessing(
782762
const auto *Load = getApproxLoadFrom(L);
783763

784764
switch (Sani.getKind()) {
785-
// case EdgeDomain::Bot:
786-
// rem.push_back(L);
787-
// std::cerr << "Sanitize " << llvmIRToShortString(L) << " with Bottom "
788-
// << std::endl;
789-
// break;
790765
case EdgeDomain::Sanitized:
791766
Rem.push_back(L);
792767
std::cerr << "Sanitize " << llvmIRToShortString(L) << " from parent "

0 commit comments

Comments
 (0)