Spatial
Simple Static Analysis in LLVM
|
Spatial is a framework to easily implement analysis over LLVM IR.
spatial/Token/AliasToken.h
spatial
opt -load /usr/local/lib/libSpatial.so -load yourPass.so ...
Spatial provides a common API to deal with all the LLVM's entities, for example, instructions, operands, arguments, global variables and etc.
Spatial represents LLVM instructions by classifying them on the pointer redirection over its operands.
For example, an instruction X = *Y
can be represented as {(X, 1), (Y, 2)}
&X
is represented as (X, 0)
X
is represented as (X, 1)
*X
is represented as (X, 2)
and so on
The analysis writer only needs to think about the analysis at various premutations of the indirections for the operand. This way Spatial allow the analysis writer to only write for one instruction in each category.
Spatial represets each entity as a token. It provides simple and consistent API calls to various utilities, for example,
getName()
returns the name of the token
isMem()
returns true if the token is a memory location
more can be found at /lib/Token/Alias.h
Directly creating new tokens from the entities is supported but not recommended until required as it requires to the token bank to avoid duplication of tokens. There are cases where we need to explicitly create a token example GEP instructions.
AT is an object of AliasTokens class and should be unique to a module. It store all the tokens for a single module getAliasToken returns alias token from AliasTokens either by creating a new one or using the already existing one.
Creating of dummy token can be useful in few use cases. Spatial supports generation of dummy token,
Spatial provides a clean way to extract information for an instruction. It will also log a warning for any instruction that are not supported right now.
Worklist remains the center of the dataflow analysis. Spatial provides basic support for creating and manipulating worklist at the instruction level. The design does not scale for complex situations, for example, bi-directional analysis; for such cases it is better to implement one on the fly to tailor your needs.
Now create instruction worklist with instructions, basicblock, module and even function.
Spatial allows you to push an instruction, basicblock, module and function into the worklist. It always pops out an instruction.
Spatial provides in built points-to graph supports and also provide features using other parts of the infrastructure.
Spatial provides direct ways to add points-to information derived for each token. insert(Op1, Op2, R1, R2)
takes {(X, 1), (Y, 2)}
as insert(X, Y, 1, 2)
and inserts the required edge.
Flow-sensitive analysis generally merge values along the branch. Spatial's points-to graph implementation provide easy ways to merge points-to graph.
More details at /spatial/include/AliasGraph.h
Spatial provides two methods to retrieve pointee set, getPointee(X)
and getUniquePointee(X)
Spatial has spacial utility functions that are frequently used by the analysis writers. More details at /spatial/lib/Utils.cpp
Spatial provides functions to get instruction successors and predecessors.
Not all functions are meant to be analyzed, for example, at least the one without a body. Use this utility to skip functions from your analysis.
Make your analysis context sensitive at ease with Spatial.
Initialize the ValueContext
object with the datatype of your dataflow value
Initailize a context for a given llvm::Function F
and initial dataflow value Initial
as follows:
Manipulate data structures storing dataflow values directly through getDataFlowIn
and getDataFlowOut
You may want to update the context graph after initializing a new context.
To get the previously saved context for a given llvm::Function F
and initial dataflow value Initial
do as follow:
if the value of SavedContext
is less than 0, implies that this context was not saved previously.
When you reach the boundary instruction, you may want to update the result for the context C
To iterate over all the contexts which invoked this context using a function call
Spatial provides support for analysis benchmarking starting with alias analysis. The present implementation relies on trigger functions, for example, MAY_ALIAS(a,b)
Create an instance and use it for extracting benchmark data and evaluations
AT
is an object of AliasTokens
class and should be unique to a module. It store all the tokens for a single modulegetAliasToken
returns alias token from AliasTokens
either by creating a new one or using the already existing one.It will react to specific function calls and will extract out information for its arguments.
Use the function evaluate for evaluating the results.
Spatial stands on the shoulders of extensive experience gained from implementing alias analysis. To demonstrate its usefulness and robustness, we implemented a flow-sensitive and context-sensitive variant of alias analysis and also a demand demand driven variants. These implementations can also be used a reference.