Skip to content

Commit 0b84329

Browse files
committed
C++: Expose the 'AddressOperand' from both 'hasResultMemoryAccess' and 'hasOperandMemoryAccess' and add a boolean column indicating whether they relate to multiple 'Allocation's.
1 parent 1c8cf3c commit 0b84329

File tree

1 file changed

+39
-32
lines changed
  • cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/internal

1 file changed

+39
-32
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/internal/AliasedSSA.qll

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,45 @@ private predicate isIndirectOrBufferMemoryAccess(MemoryAccessKind kind) {
1616
kind instanceof BufferMemoryAccess
1717
}
1818

19+
private predicate hasMemoryAccess(
20+
AddressOperand addrOperand, Allocation var, IntValue startBitOffset, boolean grouped
21+
) {
22+
addressOperandAllocationAndOffset(addrOperand, var, startBitOffset) and
23+
if strictcount(Allocation alloc | addressOperandAllocationAndOffset(addrOperand, alloc, _)) > 1
24+
then grouped = true
25+
else grouped = false
26+
}
27+
1928
private predicate hasResultMemoryAccess(
20-
Instruction instr, Allocation var, IRType type, Language::LanguageType languageType,
21-
IntValue startBitOffset, IntValue endBitOffset, boolean isMayAccess
29+
AddressOperand address, Instruction instr, Allocation var, IRType type,
30+
Language::LanguageType languageType, IntValue startBitOffset, IntValue endBitOffset,
31+
boolean isMayAccess, boolean grouped
2232
) {
23-
exists(AddressOperand addrOperand |
24-
addrOperand = instr.getResultAddressOperand() and
25-
addressOperandAllocationAndOffset(addrOperand, var, startBitOffset) and
26-
languageType = instr.getResultLanguageType() and
27-
type = languageType.getIRType() and
28-
isIndirectOrBufferMemoryAccess(instr.getResultMemoryAccess()) and
29-
(if instr.hasResultMayMemoryAccess() then isMayAccess = true else isMayAccess = false) and
30-
if exists(type.getByteSize())
31-
then endBitOffset = Ints::add(startBitOffset, Ints::mul(type.getByteSize(), 8))
32-
else endBitOffset = Ints::unknown()
33-
)
33+
address = instr.getResultAddressOperand() and
34+
hasMemoryAccess(address, var, startBitOffset, grouped) and
35+
languageType = instr.getResultLanguageType() and
36+
type = languageType.getIRType() and
37+
isIndirectOrBufferMemoryAccess(instr.getResultMemoryAccess()) and
38+
(if instr.hasResultMayMemoryAccess() then isMayAccess = true else isMayAccess = false) and
39+
if exists(type.getByteSize())
40+
then endBitOffset = Ints::add(startBitOffset, Ints::mul(type.getByteSize(), 8))
41+
else endBitOffset = Ints::unknown()
3442
}
3543

3644
private predicate hasOperandMemoryAccess(
37-
MemoryOperand operand, Allocation var, IRType type, Language::LanguageType languageType,
38-
IntValue startBitOffset, IntValue endBitOffset, boolean isMayAccess
45+
AddressOperand address, MemoryOperand operand, Allocation var, IRType type,
46+
Language::LanguageType languageType, IntValue startBitOffset, IntValue endBitOffset,
47+
boolean isMayAccess, boolean grouped
3948
) {
40-
exists(AddressOperand addrOperand |
41-
addrOperand = operand.getAddressOperand() and
42-
addressOperandAllocationAndOffset(addrOperand, var, startBitOffset) and
43-
languageType = operand.getLanguageType() and
44-
type = languageType.getIRType() and
45-
isIndirectOrBufferMemoryAccess(operand.getMemoryAccess()) and
46-
(if operand.hasMayReadMemoryAccess() then isMayAccess = true else isMayAccess = false) and
47-
if exists(type.getByteSize())
48-
then endBitOffset = Ints::add(startBitOffset, Ints::mul(type.getByteSize(), 8))
49-
else endBitOffset = Ints::unknown()
50-
)
49+
address = operand.getAddressOperand() and
50+
hasMemoryAccess(address, var, startBitOffset, grouped) and
51+
languageType = operand.getLanguageType() and
52+
type = languageType.getIRType() and
53+
isIndirectOrBufferMemoryAccess(operand.getMemoryAccess()) and
54+
(if operand.hasMayReadMemoryAccess() then isMayAccess = true else isMayAccess = false) and
55+
if exists(type.getByteSize())
56+
then endBitOffset = Ints::add(startBitOffset, Ints::mul(type.getByteSize(), 8))
57+
else endBitOffset = Ints::unknown()
5158
}
5259

5360
private newtype TMemoryLocation =
@@ -56,9 +63,9 @@ private newtype TMemoryLocation =
5663
IntValue endBitOffset, boolean isMayAccess
5764
) {
5865
(
59-
hasResultMemoryAccess(_, var, type, _, startBitOffset, endBitOffset, isMayAccess)
66+
hasResultMemoryAccess(_, _, var, type, _, startBitOffset, endBitOffset, isMayAccess, false)
6067
or
61-
hasOperandMemoryAccess(_, var, type, _, startBitOffset, endBitOffset, isMayAccess)
68+
hasOperandMemoryAccess(_, _, var, type, _, startBitOffset, endBitOffset, isMayAccess, false)
6269
or
6370
// For a stack variable, always create a memory location for the entire variable.
6471
var.isAlwaysAllocatedOnStack() and
@@ -211,13 +218,13 @@ class VariableMemoryLocation extends TVariableMemoryLocation, AllocationMemoryLo
211218
final override Language::LanguageType getType() {
212219
if
213220
strictcount(Language::LanguageType accessType |
214-
hasResultMemoryAccess(_, var, type, accessType, startBitOffset, endBitOffset, _) or
215-
hasOperandMemoryAccess(_, var, type, accessType, startBitOffset, endBitOffset, _)
221+
hasResultMemoryAccess(_, _, var, type, accessType, startBitOffset, endBitOffset, _, false) or
222+
hasOperandMemoryAccess(_, _, var, type, accessType, startBitOffset, endBitOffset, _, false)
216223
) = 1
217224
then
218225
// All of the accesses have the same `LanguageType`, so just use that.
219-
hasResultMemoryAccess(_, var, type, result, startBitOffset, endBitOffset, _) or
220-
hasOperandMemoryAccess(_, var, type, result, startBitOffset, endBitOffset, _)
226+
hasResultMemoryAccess(_, _, var, type, result, startBitOffset, endBitOffset, _, false) or
227+
hasOperandMemoryAccess(_, _, var, type, result, startBitOffset, endBitOffset, _, false)
221228
else
222229
// There is no single type for all accesses, so just use the canonical one for this `IRType`.
223230
result = type.getCanonicalLanguageType()

0 commit comments

Comments
 (0)