Skip to content

Commit 165a45d

Browse files
author
Dave Bartolomeo
committed
C++/C#: Update SimpleSSA to use Allocation instead of IRVariable
1 parent 1bbc875 commit 165a45d

2 files changed

Lines changed: 62 additions & 92 deletions

File tree

  • cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal
  • csharp/ql/src/semmle/code/csharp/ir/implementation/unaliased_ssa/internal
Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,57 @@
11
import AliasAnalysis
22
private import SimpleSSAImports
33
import SimpleSSAPublicImports
4+
private import AliasConfiguration
45

5-
private class IntValue = Ints::IntValue;
6-
7-
private predicate hasResultMemoryAccess(
8-
Instruction instr, IRVariable var, Language::LanguageType type, IntValue bitOffset
9-
) {
10-
resultPointsTo(instr.getResultAddressOperand().getAnyDef(), var, bitOffset) and
11-
type = instr.getResultLanguageType()
12-
}
13-
14-
private predicate hasOperandMemoryAccess(
15-
MemoryOperand operand, IRVariable var, Language::LanguageType type, IntValue bitOffset
16-
) {
17-
resultPointsTo(operand.getAddressOperand().getAnyDef(), var, bitOffset) and
18-
type = operand.getLanguageType()
6+
private predicate isTotalAccess(Allocation var, AddressOperand addrOperand, IRType type) {
7+
exists(Instruction constantBase, int bitOffset |
8+
addressOperandBaseAndConstantOffset(addrOperand, constantBase, bitOffset) and
9+
bitOffset = 0 and
10+
constantBase = var.getABaseInstruction() and
11+
type = var.getIRType()
12+
)
1913
}
2014

2115
/**
22-
* Holds if the specified variable should be modeled in SSA form. For unaliased SSA, we only model a variable if its
23-
* address never escapes and all reads and writes of that variable access the entire variable using the original type
24-
* of the variable.
16+
* Holds if the specified variable should be modeled in SSA form. For unaliased SSA, we only model a
17+
* variable if its address never escapes and all reads and writes of that variable access the entire
18+
* variable using the original type of the variable.
2519
*/
26-
private predicate isVariableModeled(IRVariable var) {
27-
not variableAddressEscapes(var) and
28-
// There's no need to check for the right size. An `IRVariable` never has an `UnknownType`, so the test for
29-
// `type = var.getType()` is sufficient.
30-
forall(Instruction instr, Language::LanguageType type, IntValue bitOffset |
31-
hasResultMemoryAccess(instr, var, type, bitOffset) and
32-
not instr.hasResultMayMemoryAccess()
20+
private predicate isVariableModeled(Allocation var) {
21+
not allocationEscapes(var) and
22+
forall(Instruction instr, AddressOperand addrOperand, IRType type |
23+
addrOperand = instr.getResultAddressOperand() and
24+
type = instr.getResultIRType() and
25+
var = getAddressOperandAllocation(addrOperand)
3326
|
34-
bitOffset = 0 and
35-
type.getIRType() = var.getIRType() and
36-
not instr.hasResultMayMemoryAccess()
27+
isTotalAccess(var, addrOperand, type) and not instr.hasResultMayMemoryAccess()
3728
) and
38-
forall(MemoryOperand operand, Language::LanguageType type, IntValue bitOffset |
39-
hasOperandMemoryAccess(operand, var, type, bitOffset)
29+
forall(MemoryOperand memOperand, AddressOperand addrOperand, IRType type |
30+
addrOperand = memOperand.getAddressOperand() and
31+
type = memOperand.getIRType() and
32+
var = getAddressOperandAllocation(addrOperand)
4033
|
41-
bitOffset = 0 and
42-
type.getIRType() = var.getIRType() and
43-
not operand.hasMayReadMemoryAccess()
34+
isTotalAccess(var, addrOperand, type) and not memOperand.hasMayReadMemoryAccess()
4435
)
4536
}
4637

47-
private newtype TMemoryLocation = MkMemoryLocation(IRVariable var) { isVariableModeled(var) }
38+
private newtype TMemoryLocation = MkMemoryLocation(Allocation var) { isVariableModeled(var) }
4839

49-
private MemoryLocation getMemoryLocation(IRVariable var) { result.getIRVariable() = var }
40+
private MemoryLocation getMemoryLocation(Allocation var) { result.getAllocation() = var }
5041

5142
class MemoryLocation extends TMemoryLocation {
52-
IRVariable var;
43+
Allocation var;
5344

5445
MemoryLocation() { this = MkMemoryLocation(var) }
5546

56-
final string toString() { result = var.toString() }
47+
final string toString() { result = var.getAllocationString() }
48+
49+
final Allocation getAllocation() { result = var }
5750

5851
final Language::Location getLocation() { result = var.getLocation() }
5952

6053
final IRFunction getIRFunction() { result = var.getEnclosingIRFunction() }
6154

62-
final IRVariable getIRVariable() { result = var }
63-
6455
final VirtualVariable getVirtualVariable() { result = this }
6556

6657
final Language::LanguageType getType() { result = var.getLanguageType() }
@@ -77,15 +68,9 @@ Overlap getOverlap(MemoryLocation def, MemoryLocation use) {
7768
}
7869

7970
MemoryLocation getResultMemoryLocation(Instruction instr) {
80-
exists(IRVariable var |
81-
hasResultMemoryAccess(instr, var, _, _) and
82-
result = getMemoryLocation(var)
83-
)
71+
result = getMemoryLocation(getAddressOperandAllocation(instr.getResultAddressOperand()))
8472
}
8573

8674
MemoryLocation getOperandMemoryLocation(MemoryOperand operand) {
87-
exists(IRVariable var |
88-
hasOperandMemoryAccess(operand, var, _, _) and
89-
result = getMemoryLocation(var)
90-
)
75+
result = getMemoryLocation(getAddressOperandAllocation(operand.getAddressOperand()))
9176
}
Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,57 @@
11
import AliasAnalysis
22
private import SimpleSSAImports
33
import SimpleSSAPublicImports
4+
private import AliasConfiguration
45

5-
private class IntValue = Ints::IntValue;
6-
7-
private predicate hasResultMemoryAccess(
8-
Instruction instr, IRVariable var, Language::LanguageType type, IntValue bitOffset
9-
) {
10-
resultPointsTo(instr.getResultAddressOperand().getAnyDef(), var, bitOffset) and
11-
type = instr.getResultLanguageType()
12-
}
13-
14-
private predicate hasOperandMemoryAccess(
15-
MemoryOperand operand, IRVariable var, Language::LanguageType type, IntValue bitOffset
16-
) {
17-
resultPointsTo(operand.getAddressOperand().getAnyDef(), var, bitOffset) and
18-
type = operand.getLanguageType()
6+
private predicate isTotalAccess(Allocation var, AddressOperand addrOperand, IRType type) {
7+
exists(Instruction constantBase, int bitOffset |
8+
addressOperandBaseAndConstantOffset(addrOperand, constantBase, bitOffset) and
9+
bitOffset = 0 and
10+
constantBase = var.getABaseInstruction() and
11+
type = var.getIRType()
12+
)
1913
}
2014

2115
/**
22-
* Holds if the specified variable should be modeled in SSA form. For unaliased SSA, we only model a variable if its
23-
* address never escapes and all reads and writes of that variable access the entire variable using the original type
24-
* of the variable.
16+
* Holds if the specified variable should be modeled in SSA form. For unaliased SSA, we only model a
17+
* variable if its address never escapes and all reads and writes of that variable access the entire
18+
* variable using the original type of the variable.
2519
*/
26-
private predicate isVariableModeled(IRVariable var) {
27-
not variableAddressEscapes(var) and
28-
// There's no need to check for the right size. An `IRVariable` never has an `UnknownType`, so the test for
29-
// `type = var.getType()` is sufficient.
30-
forall(Instruction instr, Language::LanguageType type, IntValue bitOffset |
31-
hasResultMemoryAccess(instr, var, type, bitOffset) and
32-
not instr.hasResultMayMemoryAccess()
20+
private predicate isVariableModeled(Allocation var) {
21+
not allocationEscapes(var) and
22+
forall(Instruction instr, AddressOperand addrOperand, IRType type |
23+
addrOperand = instr.getResultAddressOperand() and
24+
type = instr.getResultIRType() and
25+
var = getAddressOperandAllocation(addrOperand)
3326
|
34-
bitOffset = 0 and
35-
type.getIRType() = var.getIRType() and
36-
not instr.hasResultMayMemoryAccess()
27+
isTotalAccess(var, addrOperand, type) and not instr.hasResultMayMemoryAccess()
3728
) and
38-
forall(MemoryOperand operand, Language::LanguageType type, IntValue bitOffset |
39-
hasOperandMemoryAccess(operand, var, type, bitOffset)
29+
forall(MemoryOperand memOperand, AddressOperand addrOperand, IRType type |
30+
addrOperand = memOperand.getAddressOperand() and
31+
type = memOperand.getIRType() and
32+
var = getAddressOperandAllocation(addrOperand)
4033
|
41-
bitOffset = 0 and
42-
type.getIRType() = var.getIRType() and
43-
not operand.hasMayReadMemoryAccess()
34+
isTotalAccess(var, addrOperand, type) and not memOperand.hasMayReadMemoryAccess()
4435
)
4536
}
4637

47-
private newtype TMemoryLocation = MkMemoryLocation(IRVariable var) { isVariableModeled(var) }
38+
private newtype TMemoryLocation = MkMemoryLocation(Allocation var) { isVariableModeled(var) }
4839

49-
private MemoryLocation getMemoryLocation(IRVariable var) { result.getIRVariable() = var }
40+
private MemoryLocation getMemoryLocation(Allocation var) { result.getAllocation() = var }
5041

5142
class MemoryLocation extends TMemoryLocation {
52-
IRVariable var;
43+
Allocation var;
5344

5445
MemoryLocation() { this = MkMemoryLocation(var) }
5546

56-
final string toString() { result = var.toString() }
47+
final string toString() { result = var.getAllocationString() }
48+
49+
final Allocation getAllocation() { result = var }
5750

5851
final Language::Location getLocation() { result = var.getLocation() }
5952

6053
final IRFunction getIRFunction() { result = var.getEnclosingIRFunction() }
6154

62-
final IRVariable getIRVariable() { result = var }
63-
6455
final VirtualVariable getVirtualVariable() { result = this }
6556

6657
final Language::LanguageType getType() { result = var.getLanguageType() }
@@ -77,15 +68,9 @@ Overlap getOverlap(MemoryLocation def, MemoryLocation use) {
7768
}
7869

7970
MemoryLocation getResultMemoryLocation(Instruction instr) {
80-
exists(IRVariable var |
81-
hasResultMemoryAccess(instr, var, _, _) and
82-
result = getMemoryLocation(var)
83-
)
71+
result = getMemoryLocation(getAddressOperandAllocation(instr.getResultAddressOperand()))
8472
}
8573

8674
MemoryLocation getOperandMemoryLocation(MemoryOperand operand) {
87-
exists(IRVariable var |
88-
hasOperandMemoryAccess(operand, var, _, _) and
89-
result = getMemoryLocation(var)
90-
)
75+
result = getMemoryLocation(getAddressOperandAllocation(operand.getAddressOperand()))
9176
}

0 commit comments

Comments
 (0)