Static Value-Flow Analysis
Loading...
Searching...
No Matches
AbstractState.cpp
Go to the documentation of this file.
1//===- IntervalExeState.cpp----Interval Domain-------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2022> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22/*
23 * AbstractExeState.cpp
24 *
25 * Created on: Jul 9, 2022
26 * Author: Xiao Cheng, Jiawei Wang
27 *
28 */
29
31#include "Util/SVFUtil.h"
32#include "Util/Options.h"
33
34using namespace SVF;
35using namespace SVFUtil;
36
38{
39 return *this == other;
40}
41
43{
44 size_t h = getVarToVal().size() * 2;
46 for (const auto &t: getVarToVal())
47 {
48 h ^= hf(t.first) + 0x9e3779b9 + (h << 6) + (h >> 2);
49 }
50 size_t h2 = getLocToVal().size() * 2;
51 for (const auto &t: getLocToVal())
52 {
53 h2 ^= hf(t.first) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
54 }
56 return pairH({h, h2});
57}
58
60{
61 // widen interval
62 AbstractState es = *this;
63 for (auto it = es._varToAbsVal.begin(); it != es._varToAbsVal.end(); ++it)
64 {
65 auto key = it->first;
66 if (other._varToAbsVal.find(key) != other._varToAbsVal.end())
67 if (it->second.isInterval() && other._varToAbsVal.at(key).isInterval())
68 it->second.getInterval().widen_with(other._varToAbsVal.at(key).getInterval());
69 }
70 for (auto it = es._addrToAbsVal.begin(); it != es._addrToAbsVal.end(); ++it)
71 {
72 auto key = it->first;
73 if (other._addrToAbsVal.find(key) != other._addrToAbsVal.end())
74 if (it->second.isInterval() && other._addrToAbsVal.at(key).isInterval())
75 it->second.getInterval().widen_with(other._addrToAbsVal.at(key).getInterval());
76 }
77 return es;
78}
79
81{
82 AbstractState es = *this;
83 for (auto it = es._varToAbsVal.begin(); it != es._varToAbsVal.end(); ++it)
84 {
85 auto key = it->first;
86 if (other._varToAbsVal.find(key) != other._varToAbsVal.end())
87 if (it->second.isInterval() && other._varToAbsVal.at(key).isInterval())
88 it->second.getInterval().narrow_with(other._varToAbsVal.at(key).getInterval());
89 }
90 for (auto it = es._addrToAbsVal.begin(); it != es._addrToAbsVal.end(); ++it)
91 {
92 auto key = it->first;
93 if (other._addrToAbsVal.find(key) != other._addrToAbsVal.end())
94 if (it->second.isInterval() && other._addrToAbsVal.at(key).isInterval())
95 it->second.getInterval().narrow_with(other._addrToAbsVal.at(key).getInterval());
96 }
97 return es;
98
99}
100
103{
104 for (auto it = other._varToAbsVal.begin(); it != other._varToAbsVal.end(); ++it)
105 {
106 auto key = it->first;
107 auto oit = _varToAbsVal.find(key);
108 if (oit != _varToAbsVal.end())
109 {
110 oit->second.join_with(it->second);
111 }
112 else
113 {
114 _varToAbsVal.emplace(key, it->second);
115 }
116 }
117 for (auto it = other._addrToAbsVal.begin(); it != other._addrToAbsVal.end(); ++it)
118 {
119 auto key = it->first;
120 auto oit = _addrToAbsVal.find(key);
121 if (oit != _addrToAbsVal.end())
122 {
123 oit->second.join_with(it->second);
124 }
125 else
126 {
127 _addrToAbsVal.emplace(key, it->second);
128 }
129 }
130 _freedAddrs.insert(other._freedAddrs.begin(), other._freedAddrs.end());
131}
132
135{
136 for (auto it = other._varToAbsVal.begin(); it != other._varToAbsVal.end(); ++it)
137 {
138 auto key = it->first;
139 auto oit = _varToAbsVal.find(key);
140 if (oit != _varToAbsVal.end())
141 {
142 oit->second.meet_with(it->second);
143 }
144 }
145 for (auto it = other._addrToAbsVal.begin(); it != other._addrToAbsVal.end(); ++it)
146 {
147 auto key = it->first;
148 auto oit = _addrToAbsVal.find(key);
149 if (oit != _addrToAbsVal.end())
150 {
151 oit->second.meet_with(it->second);
152 }
153 }
155 std::set_intersection(_freedAddrs.begin(), _freedAddrs.end(),
156 other._freedAddrs.begin(), other._freedAddrs.end(),
157 std::inserter(intersection, intersection.begin()));
158 _freedAddrs = std::move(intersection);
159}
160
161// getGepObjAddrs
163{
165 APOffset lb = offset.lb().getIntNumeral() < Options::MaxFieldLimit() ? offset.lb().getIntNumeral()
167 APOffset ub = offset.ub().getIntNumeral() < Options::MaxFieldLimit() ? offset.ub().getIntNumeral()
169 for (APOffset i = lb; i <= ub; i++)
170 {
171 AbstractValue addrs = (*this)[pointer];
172 for (const auto& addr : addrs.getAddrs())
173 {
175 assert(SVFUtil::isa<ObjVar>(PAG::getPAG()->getGNode(baseObj)) && "Fail to get the base object address!");
179 }
180 }
181
182 return gepAddrs;
183}
184// initObjVar
186{
187 NodeID varId = objVar->getId();
188
189 // Check if the object variable has an associated value
190
191 const BaseObjVar* obj = PAG::getPAG()->getBaseObject(objVar->getId());
192
193 // Handle constant data, arrays, and structures
194 if (obj->isConstDataOrConstGlobal() || obj->isConstantArray() || obj->isConstantStruct())
195 {
196 if (const ConstIntObjVar* consInt = SVFUtil::dyn_cast<ConstIntObjVar>(objVar))
197 {
198 s64_t numeral = consInt->getSExtValue();
199 (*this)[varId] = IntervalValue(numeral, numeral);
200 }
201 else if (const ConstFPObjVar* consFP = SVFUtil::dyn_cast<ConstFPObjVar>(objVar))
202 {
203 (*this)[varId] = IntervalValue(consFP->getFPValue(), consFP->getFPValue());
204 }
205 else if (SVFUtil::isa<ConstNullPtrObjVar>(objVar))
206 {
207 (*this)[varId] = IntervalValue(0, 0);
208 }
209 else if (SVFUtil::isa<GlobalObjVar>(objVar))
210 {
212 }
213 else if (obj->isConstantArray() || obj->isConstantStruct())
214 {
215 (*this)[varId] = IntervalValue::top();
216 }
217 else
218 {
219 (*this)[varId] = IntervalValue::top();
220 }
221 }
222 // Handle non-constant memory objects
223 else
224 {
226 }
227 return;
228}
229
230// getElementIndex
232{
233 // If the GEP statement has a constant offset, return it directly as the interval value
234 if (gep->isConstantOffset())
235 return IntervalValue((s64_t)gep->accumulateConstantOffset());
236
237 IntervalValue res(0);
238 // Iterate over the list of offset variable and type pairs in reverse order
239 for (int i = gep->getOffsetVarAndGepTypePairVec().size() - 1; i >= 0; i--)
240 {
241 AccessPath::IdxOperandPair IdxVarAndType = gep->getOffsetVarAndGepTypePairVec()[i];
242 const SVFVar* var = gep->getOffsetVarAndGepTypePairVec()[i].first;
243 const SVFType* type = IdxVarAndType.second;
244
245 // Variables to store the lower and upper bounds of the index value
246 s64_t idxLb;
247 s64_t idxUb;
248
249 // Determine the lower and upper bounds based on whether the value is a constant
250 if (const ConstIntValVar* constInt = SVFUtil::dyn_cast<ConstIntValVar>(var))
251 idxLb = idxUb = constInt->getSExtValue();
252 else
253 {
254 IntervalValue idxItv = (*this)[var->getId()].getInterval();
255 if (idxItv.isBottom())
256 idxLb = idxUb = 0;
257 else
258 {
260 idxUb = idxItv.ub().getIntNumeral();
261 }
262 }
263
264 // Adjust the bounds if the type is a pointer
265 if (SVFUtil::isa<SVFPointerType>(type))
266 {
267 u32_t elemNum = gep->getAccessPath().getElementNum(gep->getAccessPath().gepSrcPointeeType());
268 idxLb = (double)Options::MaxFieldLimit() / elemNum < idxLb ? Options::MaxFieldLimit() : idxLb * elemNum;
269 idxUb = (double)Options::MaxFieldLimit() / elemNum < idxUb ? Options::MaxFieldLimit() : idxUb * elemNum;
270 }
271 // Adjust the bounds for array or struct types using the symbol table info
272 else
273 {
275 {
276 const std::vector<u32_t>& so = PAG::getPAG()->getTypeInfo(type)->getFlattenedElemIdxVec();
277 if (so.empty() || idxUb >= (APOffset)so.size() || idxLb < 0)
278 {
279 idxLb = idxUb = 0;
280 }
281 else
282 {
285 }
286 }
287 else
288 idxLb = idxUb = 0;
289 }
290
291 // Add the calculated interval to the result
292 res = res + IntervalValue(idxLb, idxUb);
293 }
294
295 // Ensure the result is within the bounds of [0, MaxFieldLimit]
297 if (res.isBottom())
298 {
299 res = IntervalValue(0);
300 }
301 return res;
302}
303// getByteOffset
305{
306 // If the GEP statement has a constant byte offset, return it directly as the interval value
307 if (gep->isConstantOffset())
308 return IntervalValue((s64_t)gep->accumulateConstantByteOffset());
309
310 IntervalValue res(0); // Initialize the result interval 'res' to 0.
311
312 // Loop through the offsetVarAndGepTypePairVec in reverse order.
313 for (int i = gep->getOffsetVarAndGepTypePairVec().size() - 1; i >= 0; i--)
314 {
315 const SVFVar* idxOperandVar = gep->getOffsetVarAndGepTypePairVec()[i].first;
316 const SVFType* idxOperandType = gep->getOffsetVarAndGepTypePairVec()[i].second;
317
318 // Calculate the byte offset for array or pointer types
319 if (SVFUtil::isa<SVFArrayType>(idxOperandType) || SVFUtil::isa<SVFPointerType>(idxOperandType))
320 {
322 if (const SVFArrayType* arrOperandType = SVFUtil::dyn_cast<SVFArrayType>(idxOperandType))
323 elemByteSize = arrOperandType->getTypeOfElement()->getByteSize();
324 else if (SVFUtil::isa<SVFPointerType>(idxOperandType))
325 elemByteSize = gep->getAccessPath().gepSrcPointeeType()->getByteSize();
326 else
327 assert(false && "idxOperandType must be ArrType or PtrType");
328
329 if (const ConstIntValVar* op = SVFUtil::dyn_cast<ConstIntValVar>(idxOperandVar))
330 {
331 // Calculate the lower bound (lb) of the interval value
332 s64_t lb = (double)Options::MaxFieldLimit() / elemByteSize >= op->getSExtValue()
333 ? op->getSExtValue() * elemByteSize
335 res = res + IntervalValue(lb, lb);
336 }
337 else
338 {
339 IntervalValue idxVal = (*this)[idxOperandVar->getId()].getInterval();
340
341 if (idxVal.isBottom())
342 res = res + IntervalValue(0, 0);
343 else
344 {
345 // Ensure the bounds are non-negative and within the field limit
346 s64_t ub = (idxVal.ub().getIntNumeral() < 0) ? 0
347 : (double)Options::MaxFieldLimit() / elemByteSize >= idxVal.ub().getIntNumeral()
348 ? elemByteSize * idxVal.ub().getIntNumeral()
350 s64_t lb = (idxVal.lb().getIntNumeral() < 0) ? 0
351 : (double)Options::MaxFieldLimit() / elemByteSize >= idxVal.lb().getIntNumeral()
352 ? elemByteSize * idxVal.lb().getIntNumeral()
354 res = res + IntervalValue(lb, ub);
355 }
356 }
357 }
358 // Process struct subtypes by calculating the byte offset from the beginning to the field of the struct
359 else if (const SVFStructType* structOperandType = SVFUtil::dyn_cast<SVFStructType>(idxOperandType))
360 {
361 res = res + IntervalValue(gep->getAccessPath().getStructFieldOffset(idxOperandVar, structOperandType));
362 }
363 else
364 {
365 assert(false && "gep type pair only support arr/ptr/struct");
366 }
367 }
368 return res; // Return the resulting byte offset as an IntervalValue.
369}
370
372{
373 AbstractValue res;
374 for (auto addr : (*this)[varId].getAddrs())
375 {
376 res.join_with(load(addr)); // q = *p
377 }
378 return res;
379}
380// storeValue
382{
383 for (auto addr : (*this)[varId].getAddrs())
384 {
385 store(addr, val); // *p = q
386 }
387}
388
390{
391 SVFUtil::outs() << "-----------Var and Value-----------\n";
392 u32_t fieldWidth = 20;
393 SVFUtil::outs().flags(std::ios::left);
394 std::vector<std::pair<u32_t, AbstractValue>> varToAbsValVec(_varToAbsVal.begin(), _varToAbsVal.end());
395 std::sort(varToAbsValVec.begin(), varToAbsValVec.end(), [](const auto &a, const auto &b)
396 {
397 return a.first < b.first;
398 });
399 for (const auto &item: varToAbsValVec)
400 {
401 SVFUtil::outs() << std::left << std::setw(fieldWidth) << ("Var" + std::to_string(item.first));
402 if (item.second.isInterval())
403 {
404 SVFUtil::outs() << " Value: " << item.second.getInterval().toString() << "\n";
405 }
406 else if (item.second.isAddr())
407 {
408 SVFUtil::outs() << " Value: {";
409 u32_t i = 0;
410 for (const auto& addr: item.second.getAddrs())
411 {
412 ++i;
413 if (i < item.second.getAddrs().size())
414 {
415 SVFUtil::outs() << "0x" << std::hex << addr << ", ";
416 }
417 else
418 {
419 SVFUtil::outs() << "0x" << std::hex << addr;
420 }
421 }
422 SVFUtil::outs() << "}\n";
423 }
424 else
425 {
426 SVFUtil::outs() << " Value: ⊥\n";
427 }
428 }
429
430 std::vector<std::pair<u32_t, AbstractValue>> addrToAbsValVec(_addrToAbsVal.begin(), _addrToAbsVal.end());
431 std::sort(addrToAbsValVec.begin(), addrToAbsValVec.end(), [](const auto &a, const auto &b)
432 {
433 return a.first < b.first;
434 });
435
436 for (const auto& item: addrToAbsValVec)
437 {
438 std::ostringstream oss;
439 oss << "0x" << std::hex << AbstractState::getVirtualMemAddress(item.first);
440 SVFUtil::outs() << std::left << std::setw(fieldWidth) << oss.str();
441 if (item.second.isInterval())
442 {
443 SVFUtil::outs() << " Value: " << item.second.getInterval().toString() << "\n";
444 }
445 else if (item.second.isAddr())
446 {
447 SVFUtil::outs() << " Value: {";
448 u32_t i = 0;
449 for (const auto& addr: item.second.getAddrs())
450 {
451 ++i;
452 if (i < item.second.getAddrs().size())
453 {
454 SVFUtil::outs() << "0x" << std::hex << addr << ", ";
455 }
456 else
457 {
458 SVFUtil::outs() << "0x" << std::hex << addr;
459 }
460 }
461 SVFUtil::outs() << "}\n";
462 }
463 else
464 {
465 SVFUtil::outs() << " Value: ⊥\n";
466 }
467 }
468 SVFUtil::outs() << "-----------------------------------------\n";
469}
470
472{
473 SVFIR* svfir = PAG::getPAG();
474 if (inVarToAddrsTable(id))
475 {
476 const AbstractValue& addrs = (*this)[id];
477 for (auto addr: addrs.getAddrs())
478 {
480 if (addr_id == 0) // nullptr skip
481 continue;
482 return svfir->getBaseObject(addr_id)->getType();
483 }
484 }
485 else
486 {
487 // do nothing if no record in addrs table.
488 }
489 return nullptr;
490}
491
493{
494 if (const ObjVar* objvar = SVFUtil::dyn_cast<ObjVar>(addr->getRHSVar()))
495 {
496 if (PAG::getPAG()->getBaseObject(objvar->getId())->isConstantByteSize())
497 {
499 return sz;
500 }
501
502 else
503 {
504 const std::vector<SVFVar*>& sizes = addr->getArrSize();
505 // Default element size is set to 1.
506 u32_t elementSize = 1;
507 u64_t res = elementSize;
508 for (const SVFVar* value: sizes)
509 {
510 if (!inVarToValTable(value->getId()))
511 {
512 (*this)[value->getId()] = IntervalValue(Options::MaxFieldLimit());
513 }
515 (*this)[value->getId()].getInterval();
516 res = res * itv.ub().getIntNumeral() > Options::MaxFieldLimit()? Options::MaxFieldLimit(): res * itv.ub().getIntNumeral();
517 }
518 return (u32_t)res;
519 }
520 }
521 assert (false && "Addr rhs value is not ObjVar");
522 abort();
523}
newitem type
Definition cJSON.cpp:2739
cJSON * a
Definition cJSON.cpp:2560
buffer offset
Definition cJSON.cpp:1113
const cJSON *const b
Definition cJSON.h:255
cJSON * item
Definition cJSON.h:222
const AddrToAbsValMap & getLocToVal() const
get loc2val map
u32_t getAllocaInstByteSize(const AddrStmt *addr)
const VarToAbsValMap & getVarToVal() const
get var2val map
void store(u32_t addr, const AbstractValue &val)
void printAbstractState() const
void joinWith(const AbstractState &other)
domain join with other, important! other widen this.
IntervalValue getElementIndex(const GepStmt *gep)
bool equals(const AbstractState &other) const
VarToAbsValMap _varToAbsVal
Map a variable (symbol) to its abstract value.
Set< NodeID > _freedAddrs
AddrToAbsValMap _addrToAbsVal
Map a memory address to its stored abstract value.
const SVFType * getPointeeElement(NodeID id)
virtual AbstractValue & load(u32_t addr)
IntervalValue getByteOffset(const GepStmt *gep)
AbstractValue loadValue(NodeID varId)
bool inVarToAddrsTable(u32_t id) const
whether the variable is in varToAddrs table
u32_t getIDFromAddr(u32_t addr)
Return the internal index if addr is an address otherwise return the value of idx.
static u32_t getVirtualMemAddress(u32_t idx)
The physical address starts with 0x7f...... + idx.
AbstractState narrowing(const AbstractState &other)
domain narrow with other, and return the narrowed domain
virtual bool inVarToValTable(u32_t id) const
whether the variable is in varToVal table
void storeValue(NodeID varId, AbstractValue val)
void meetWith(const AbstractState &other)
domain meet with other, important! other widen this.
AddressValue getGepObjAddrs(u32_t pointer, IntervalValue offset)
void initObjVar(ObjVar *objVar)
AbstractState widening(const AbstractState &other)
domain widen with other, and return the widened domain
void join_with(const AbstractValue &other)
AddressValue & getAddrs()
std::pair< const SVFVar *, const SVFType * > IdxOperandPair
Definition AccessPath.h:63
u32_t getByteSizeOfObj() const
Get the byte size of this object.
const SVFType * getType() const
Get obj type.
s64_t getIntNumeral() const
u32_t getFlattenedElemIdx(const SVFType *T, u32_t origId)
Flattened element idx of an array or struct by considering stride.
Definition IRGraph.cpp:144
const StInfo * getTypeInfo(const SVFType *T) const
Get struct info.
Definition IRGraph.cpp:242
void meet_with(const IntervalValue &other)
Return a intersected IntervalValue.
const BoundedInt & ub() const
Return the upper bound.
bool isBottom() const
static IntervalValue top()
Create the IntervalValue [-inf, +inf].
const BoundedInt & lb() const
Return the lower bound.
static Option< bool > ModelArrays
Definition Options.h:189
static const Option< u32_t > MaxFieldLimit
Maximum number of field derivations for an object.
Definition Options.h:39
NodeID getGepObjVar(const BaseObjVar *baseObj, const APOffset &ap)
Get a field SVFIR Object node according to base mem obj and offset.
Definition SVFIR.cpp:439
const BaseObjVar * getBaseObject(NodeID id) const
Definition SVFIR.h:423
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition SVFIR.h:116
u32_t getByteSize() const
Definition SVFType.h:244
std::vector< u32_t > & getFlattenedElemIdxVec()
Definition SVFType.h:98
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:52
for isBitcode
Definition BasicTypes.h:68
unsigned long long u64_t
Definition GeneralType.h:49
u32_t NodeID
Definition GeneralType.h:56
s64_t APOffset
Definition GeneralType.h:60
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:47
signed long long s64_t
Definition GeneralType.h:50