|
Static Value-Flow Analysis
|
#include <SVFIRBuilder.h>
Private Member Functions | |
| LLVMModuleSet * | llvmModuleSet () |
Private Attributes | |
| SVFIR * | pag |
| const SVFBasicBlock * | curBB |
| Current basic block during SVFIR construction when visiting the module. | |
| const Value * | curVal |
| Current Value during SVFIR construction when visiting the module. | |
Friends | |
| class | GraphDBSVFIRBuilder |
SVFIR Builder to create SVF variables and statements and PAG
Definition at line 47 of file SVFIRBuilder.h.
|
inline |
Constructor.
Definition at line 58 of file SVFIRBuilder.h.
|
inlinevirtual |
Add Address edge.
Definition at line 302 of file SVFIRBuilder.h.
|
inlineprotected |
Add Address edge from ext call with args like "%5 = call i8* @malloc(i64 noundef 5)".
Definition at line 324 of file SVFIRBuilder.h.
|
inlineprotected |
Add Address edge from allocinst with arraysize like "%4 = alloca i8, i64 3".
Definition at line 313 of file SVFIRBuilder.h.
|
inlineprotected |
Add Copy edge.
Definition at line 437 of file SVFIRBuilder.h.
|
inlineprotected |
Definition at line 295 of file SVFIRBuilder.h.
|
inlineprotected |
Add Branch statement.
Definition at line 449 of file SVFIRBuilder.h.
|
inlineprotected |
Add Call edge.
Definition at line 473 of file SVFIRBuilder.h.
|
inlineprotected |
Add Copy edge.
Definition at line 431 of file SVFIRBuilder.h.
|
protectedvirtual |
Add the load/store constraints and temp. nodes for the complex constraint *D = *S (where D/S may point to structs).
If sz is 0, we will add edges for all fields.
Definition at line 82 of file SVFIRExtAPI.cpp.
|
inlineprotected |
Definition at line 370 of file SVFIRBuilder.h.
| void SVF::SVFIRBuilder::addEdge | ( | NodeID | src, |
| NodeID | dst, | ||
| SVFStmt::PEDGEK | kind, | ||
| APOffset | offset = 0, |
||
| Instruction * | cs = nullptr |
||
| ) |
|
inlineprotected |
Add Gep edge.
Definition at line 485 of file SVFIRBuilder.h.
|
inlineprotected |
Add global black hole Address edge.
Definition at line 270 of file SVFIRBuilder.h.
Add Load edge.
Definition at line 455 of file SVFIRBuilder.h.
|
inlineprotected |
Add Offset(Gep) edge.
Definition at line 491 of file SVFIRBuilder.h.
|
inlineprotected |
Add NullPtr PAGNode.
Definition at line 280 of file SVFIRBuilder.h.
|
inlineprotected |
Add Copy edge.
If we already added this phi node, then skip this adding
Definition at line 418 of file SVFIRBuilder.h.
|
inlineprotected |
Add Return edge.
Definition at line 479 of file SVFIRBuilder.h.
|
inlineprotected |
Add SelectStmt.
Definition at line 425 of file SVFIRBuilder.h.
Add Store edge.
Definition at line 461 of file SVFIRBuilder.h.
|
inlineprotected |
Add Thread fork edge for parameter passing.
Definition at line 503 of file SVFIRBuilder.h.
|
inlineprotected |
Add Thread join edge for parameter passing.
Definition at line 509 of file SVFIRBuilder.h.
Add Unary edge.
Definition at line 443 of file SVFIRBuilder.h.
|
inlineprotected |
Add Variant(Gep) edge.
Definition at line 497 of file SVFIRBuilder.h.
|
virtual |
Start building SVFIR here.
Start building SVFIR here
build icfg
initial external library information initial SVFIR nodes
initial SVFIR edges: // handle globals
build callgraph
handle functions
collect return node of function fun
Return SVFIR node will not be created for function which can not reach the return instruction due to call to abort(), exit(), etc. In 176.gcc of SPEC 2000, function build_objc_string() from c-lang.c shows an example when fun.doesNotReturn() evaluates to TRUE because of abort().
To be noted, we do not record arguments which are in declared function without body TODO: what about external functions with SVFIR imported by commandline?
Definition at line 54 of file SVFIRBuilder.cpp.
|
protected |
Compute offset of a gep instruction or gep constant expression.
Return the object node offset according to GEP insn (V). Given a gep edge p = q + i, if "i" is a constant then we return its offset size otherwise if "i" is a variable determined by runtime, then it is a variant offset Return TRUE if the offset of this GEP insn is a constant.
Definition at line 632 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::createFunObjVars | ( | ) |
set fun in bb
Definition at line 324 of file SVFIRBuilder.cpp.
|
protected |
Get a base SVFVar given a pointer Return the source node of its connected normal gep edge Otherwise return the node id itself s32_t offset : gep offset
if this node is already a base node
Definition at line 1798 of file SVFIRBuilder.cpp.
|
protectedvirtual |
Handle external call.
Find the base type and the max possible offset of an object pointed to by (V).
use user-specified size for this copy operation if the size is a constaint int
Definition at line 44 of file SVFIRExtAPI.cpp.
Get the base value of (i8* src and i8* dst) for external argument (e.g. memcpy(i8* dst, i8* src, int size))
Example 1:
%0 = getelementptr inbounds struct.outer, struct.inner base, i32 0, i32 0 call void @llvm.memcpy(ptr inner, ptr %0, i64 24, i1 false) The base value for %0 is base. Note: the base is recognized as the base value if the offset (field index) is 0
Example 2: https://github.com/SVF-tools/SVF/issues/1650 https://github.com/SVF-tools/SVF/pull/1652
@i1 = dso_local global struct.inner { i32 0, ptr @f1, ptr @f2 } @n1 = dso_local global struct.outer { i32 0, ptr @i1 }
inner = alloca struct.inner %0 = load ptr, ptr getelementptr inbounds (struct.outer, ptr @n1, i32 0, i32 1) call void @llvm.memcpy(ptr inner, ptr %0, i64 24, i1 false)
The base value for %0 is @i1
Example 3:
@conststruct = internal global <{ [40 x i8], [4 x i8], [4 x i8], [2512 x i8] }> <{ [40 x i8] undef, [4 x i8] zeroinitializer, [4 x i8] undef, [2512 x i8] zeroinitializer }>, align 8
%0 = load ptr, ptr getelementptr inbounds (<{ [40 x i8], [4 x i8], [4 x i8], [2512 x i8] }>, ptr @conststruct, i64 0, i32 0, i64 16)
The base value for %0 is still %0
Definition at line 1527 of file SVFIRBuilder.cpp.
|
inlineprotected |
Definition at line 380 of file SVFIRBuilder.h.
|
inlineprotected |
Definition at line 264 of file SVFIRBuilder.h.
Definition at line 260 of file SVFIRBuilder.h.
|
protected |
Add a temp field value node according to base value and offset this node is after the initial node method, it is out of scope of symInfo table
Definition at line 1656 of file SVFIRBuilder.cpp.
|
protected |
Get the field of the global variable node FIXME:Here we only get the field that actually used in the program We ignore the initialization of global variable field that not used in the program
if we did not find the constant expression in the program, then we need to create a gep node for this field
Definition at line 836 of file SVFIRBuilder.cpp.
GetObject - Return the object node (stack/global/heap/function) according to a LLVM Value.
Definition at line 108 of file SVFIRBuilder.h.
|
inline |
getReturnNode - Return the node representing the unique return value of a function.
Definition at line 114 of file SVFIRBuilder.h.
Get different kinds of node.
Definition at line 98 of file SVFIRBuilder.h.
getVarargNode - Return the node representing the unique variadic argument of a function.
Definition at line 120 of file SVFIRBuilder.h.
Handle direct call.
Add the constraints for a direct, non-external call.
FIXME: this assertion should be placed for correct checking except bug program like 188.ammp, 300.twolf
Definition at line 1433 of file SVFIRBuilder.cpp.
pthread_create has 1 arg. apr_thread_create has 2 arg.
Connect actual parameter to formal parameter of the start routine
handle indirect calls at pthread create APIs e.g., pthread_create(&t1, nullptr, fp, ...); const Value* fun = ThreadAPI::getThreadAPI()->getForkedFun(inst); if(!SVFUtilisa<Function>(fun)) pag->addIndirectCallsites(cs,pag->getValueNode(fun));
If forkedFun does not pass to spawnee as function type but as void pointer remember to update inter-procedural callgraph/SVFIR/SVFG etc. when indirect call targets are resolved We don't connect the callgraph here, further investigation is need to handle mod-ref during SVFG construction.
TODO: inter-procedural SVFIR edges for thread joins
Definition at line 128 of file SVFIRExtAPI.cpp.
|
protected |
Handle indirect call.
Indirect call is resolved on-the-fly during pointer analysis
Definition at line 1587 of file SVFIRBuilder.cpp.
|
protected |
Infer field index from byteoffset.
Definition at line 621 of file SVFIRBuilder.cpp.
Definition at line 256 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::initFunObjVar | ( | ) |
Function
set realDefFun for all functions
Definition at line 185 of file SVFIRBuilder.cpp.
|
protected |
src should not point to anything yet
Definition at line 863 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::initialiseBaseObjVars | ( | ) |
Definition at line 366 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::initialiseNodes | ( | ) |
Initialize nodes and edges.
add address edges for constant nodes.
add argvalvar for svffunctions
Definition at line 526 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::initialiseValVars | ( | ) |
Definition at line 448 of file SVFIRBuilder.cpp.
set exit block: exit basic block must have no successors and have a return instruction
Definition at line 213 of file SVFIRBuilder.cpp.
|
inlineprivate |
Definition at line 519 of file SVFIRBuilder.h.
Process constant expression.
Handle constant expression, and connect the gep edge
Definition at line 705 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::sanityCheck | ( | ) |
Sanity check for SVFIR.
Definition at line 1630 of file SVFIRBuilder.cpp.
|
protected |
We assume every GepValVar and its GepStmt are unique across whole program
We will have one unique function exit ICFGNode for all returns
Definition at line 1711 of file SVFIRBuilder.cpp.
|
inlineprotected |
Set current basic block in order to keep track of control flow information.
Definition at line 250 of file SVFIRBuilder.h.
|
inlineprotected |
Definition at line 255 of file SVFIRBuilder.h.
| void SVFIRBuilder::updateCallGraph | ( | CallGraph * | callgraph | ) |
connect PAG edges based on callgraph
Definition at line 1595 of file SVFIRBuilder.cpp.
|
virtual |
Our visit overrides.
Visit alloca instructions Add edge V (dst) <– O (src), V here is a value node on SVFIR, O is object node on SVFIR
Definition at line 994 of file SVFIRBuilder.cpp.
|
inline |
Definition at line 195 of file SVFIRBuilder.h.
|
inline |
Definition at line 199 of file SVFIRBuilder.h.
| void SVFIRBuilder::visitBinaryOperator | ( | BinaryOperator & | inst | ) |
Visit Binary Operator
Definition at line 1109 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitBranchInst | ( | BranchInst & | inst | ) |
Branch and switch instructions are treated as UnaryOP br cmp label if.then, label if.else
set conditional svf var
Definition at line 1288 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitCallBrInst | ( | CallBrInst & | I | ) |
Definition at line 1176 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitCallInst | ( | CallInst & | I | ) |
Definition at line 1166 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitCallSite | ( | CallBase * | cs | ) |
Collect callsite arguments and returns
Definition at line 1184 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitCastInst | ( | CastInst & | I | ) |
Definition at line 1095 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitCmpInst | ( | CmpInst & | inst | ) |
Visit compare instruction
Definition at line 1137 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitExtractElementInst | ( | ExtractElementInst & | inst | ) |
The �extractelement� instruction extracts a single scalar element from a vector at a specified index. TODO: for now we just assume the pointer after extraction points to blackhole The first operand of an �extractelement� instruction is a value of vector type. The second operand is an index indicating the position from which to extract the element.
<result> = extractelement <4 x i32> vec, i32 0 ; yields i32
Definition at line 1278 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitExtractValueInst | ( | ExtractValueInst & | inst | ) |
visit extract value instructions for structures in registers TODO: for now we just assume the pointer after extraction points to blackhole for example %24 = extractvalue { i32, struct.s_hash* } call34, 0 %24 is a pointer points to first field of a register value call34 however we can not create call34 as an memory object, as it is register value. Is that necessary treat extract value as getelementptr instruction later to get more precise results?
Definition at line 1264 of file SVFIRBuilder.cpp.
|
inline |
Definition at line 191 of file SVFIRBuilder.h.
| void SVFIRBuilder::visitFreezeInst | ( | FreezeInst & | inst | ) |
<result> = freeze ty <val> If <val> is undef or poison, ‘freeze’ returns an arbitrary, but fixed value of type ty Otherwise, this instruction is a no-op and returns the input <val>
<result> = freeze ty <val> If <val> is undef or poison, ‘freeze’ returns an arbitrary, but fixed value of type ty Otherwise, this instruction is a no-op and returns the input <val> For now, we assume <val> is never a poison or undef.
Definition at line 1418 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitGetElementPtrInst | ( | GetElementPtrInst & | inst | ) |
Visit getelementptr instructions
Definition at line 1069 of file SVFIRBuilder.cpp.
|
protected |
Handle globals including (global variable and functions)
Visit global variables for building SVFIR
initialize global variable
initialize global functions
Definition at line 941 of file SVFIRBuilder.cpp.
|
inline |
Definition at line 171 of file SVFIRBuilder.h.
|
inline |
Definition at line 145 of file SVFIRBuilder.h.
|
inline |
Provide base case for our instruction visit.
Definition at line 205 of file SVFIRBuilder.h.
| void SVFIRBuilder::visitInvokeInst | ( | InvokeInst & | II | ) |
Definition at line 1171 of file SVFIRBuilder.cpp.
|
inline |
Definition at line 179 of file SVFIRBuilder.h.
| void SVFIRBuilder::visitLoadInst | ( | LoadInst & | I | ) |
Definition at line 1037 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitPHINode | ( | PHINode & | inst | ) |
Visit phi instructions
Definition at line 1012 of file SVFIRBuilder.cpp.
|
inline |
| void SVFIRBuilder::visitReturnInst | ( | ReturnInst & | inst | ) |
Visit return instructions of a function
Definition at line 1235 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitSelectInst | ( | SelectInst & | inst | ) |
Visit select instructions
Two operands have same incoming basic block, both are the current BB
Definition at line 1153 of file SVFIRBuilder.cpp.
|
inline |
Definition at line 175 of file SVFIRBuilder.h.
| void SVFIRBuilder::visitStoreInst | ( | StoreInst & | inst | ) |
Visit store instructions
Definition at line 1051 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitSwitchInst | ( | SwitchInst & | inst | ) |
The following implementation follows ICFGBuilder::processFunBody.
See more: https://github.com/SVF-tools/SVF/pull/1191
Given the code:
switch (a) { case 0: printf("0\n"); break; case 1: case 2: case 3: printf("a >=1 && a <= 3\n"); break; case 4: case 6: case 7: printf("a >= 4 && a <=7\n"); break; default: printf("a < 0 || a > 7"); break; }
Generate the IR:
switch i32 %0, label sw.default [ i32 0, label sw.bb i32 1, label sw.bb1 i32 2, label sw.bb1 i32 3, label sw.bb1 i32 4, label sw.bb3 i32 6, label sw.bb3 i32 7, label sw.bb3 ]
We can get every case basic block and related case value: [ {sw.default, -1}, {sw.bb, 0}, {sw.bb1, 1}, {sw.bb1, 2}, {sw.bb1, 3}, {sw.bb3, 4}, {sw.bb3, 6}, {sw.bb3, 7}, ] Note: default case value is nullptr For larger number, we preserve case value just -1 now see more: https://github.com/SVF-tools/SVF/pull/992
branch condition value
default case is set to -1;
set conditional svf var
Definition at line 1370 of file SVFIRBuilder.cpp.
| void SVFIRBuilder::visitUnaryOperator | ( | UnaryOperator & | inst | ) |
Visit Unary Operator
Definition at line 1124 of file SVFIRBuilder.cpp.
|
inline |
Definition at line 188 of file SVFIRBuilder.h.
| void SVFIRBuilder::visitVAArgInst | ( | VAArgInst & | inst | ) |
TODO: var arguments need to be handled. https://llvm.org/docs/LangRef.html#id1911
ap = alloca struct.va_list ap2 = bitcast struct.va_list* ap to i8* ; Read a single integer argument from ap2 tmp = va_arg i8* ap2, i32 (VAArgInst) TODO: for now, create a copy edge from ap2 to tmp, we assume here tmp should point to the n-th argument of the var_args
Definition at line 1406 of file SVFIRBuilder.cpp.
|
inline |
Definition at line 160 of file SVFIRBuilder.h.
|
inline |
Definition at line 161 of file SVFIRBuilder.h.
|
inline |
Definition at line 162 of file SVFIRBuilder.h.
Definition at line 49 of file SVFIRBuilder.h.
|
private |
Current basic block during SVFIR construction when visiting the module.
Definition at line 53 of file SVFIRBuilder.h.
Current Value during SVFIR construction when visiting the module.
Definition at line 54 of file SVFIRBuilder.h.
|
private |
Definition at line 52 of file SVFIRBuilder.h.