Skip to content

Commit 352e750

Browse files
C++: Implement precise analysis of indirections in AliasedSSA
1 parent 2bc3506 commit 352e750

File tree

3 files changed

+252
-105
lines changed

3 files changed

+252
-105
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
import semmle.code.cpp.ir.implementation.unaliased_ssa.IR as InputIR
2+
import AliasConfiguration as Configuration
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
private import cpp
2+
private import semmle.code.cpp.ir.implementation.unaliased_ssa.IR
3+
private import semmle.code.cpp.ir.implementation.unaliased_ssa.gvn.ValueNumbering
4+
private import AliasAnalysis
5+
6+
/**
7+
* A memory allocation that can be tracked by the AliasedSSA alias analysis.
8+
* For now, we track all variables accessed within the function, including both local variables
9+
* and global variables. In the future, we will track indirect parameters as well.
10+
*/
11+
class Allocation extends ValueNumber {
12+
IRVariable var;
13+
14+
Allocation() {
15+
// For now, we only track variables.
16+
var = this.getAnInstruction().(VariableAddressInstruction).getVariable()
17+
}
18+
19+
final string getAllocationString() {
20+
exists(string suffix |
21+
result = var.toString() + suffix and
22+
if isUnaliased() then
23+
suffix = ""
24+
else
25+
suffix = "*"
26+
)
27+
}
28+
29+
final Type getType() {
30+
result = var.getType()
31+
}
32+
33+
final int getBitSize() {
34+
result = getType().getSize() * 8
35+
}
36+
37+
final predicate alwaysEscapes() {
38+
// An automatic variable only escapes if its address is taken and escapes, but we assume that
39+
// any other kind of variable always escapes.
40+
not var instanceof IRAutomaticVariable
41+
}
42+
43+
final predicate isUnaliased() {
44+
not allocationEscapes(this)
45+
}
46+
47+
final Instruction getABaseInstruction() {
48+
// Any instruction with this value number serves as a base address for this allocation.
49+
result = getAnInstruction()
50+
}
51+
}

0 commit comments

Comments
 (0)