Skip to content

Commit 533eeff

Browse files
author
Dave Bartolomeo
committed
C++: Fix MemoryLocation with multiple VirtualVariables
While investigating a bug with `TInstruction` sharing, I discovered that we had a case where alias analysis could create two `VirtualVariable`s for the same `Allocation`. For an indirect parameter allocation, we were using the type of the pointer variable as the type of the indirect allocation, instead of just `Unknown`. If the `IRType` of the pointer variable was the same type as the type of at least one access to the indirect allocation, we'd create both an `EntireAllocationVirtualVariable` and a `VariableVirtualVariable` for the allocation. I added a new consistency test to guard against this in the future. This also turned out to be the root cause of the one existing known consistency failure in the IR tests.
1 parent 674c184 commit 533eeff

15 files changed

+93
-4
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/AliasConfiguration.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class IndirectParameterAllocation extends Allocation, TIndirectParameterAllocati
9090

9191
final override string getUniqueId() { result = var.getUniqueId() }
9292

93-
final override IRType getIRType() { result = var.getIRType() }
93+
final override IRType getIRType() { result instanceof IRUnknownType }
9494

9595
final override predicate isReadOnly() { none() }
9696

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/SSAConstruction.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,9 @@ private module CachedForDebugging {
913913
}
914914

915915
module SSAConsistency {
916+
/**
917+
* Holds if a `MemoryOperand` has more than one `MemoryLocation` assigned by alias analysis.
918+
*/
916919
query predicate multipleOperandMemoryLocations(
917920
OldIR::MemoryOperand operand, string message, OldIR::IRFunction func, string funcText
918921
) {
@@ -925,6 +928,9 @@ module SSAConsistency {
925928
)
926929
}
927930

931+
/**
932+
* Holds if a `MemoryLocation` does not have an associated `VirtualVariable`.
933+
*/
928934
query predicate missingVirtualVariableForMemoryLocation(
929935
Alias::MemoryLocation location, string message, OldIR::IRFunction func, string funcText
930936
) {
@@ -933,4 +939,25 @@ module SSAConsistency {
933939
funcText = Language::getIdentityString(func.getFunction()) and
934940
message = "Memory location has no virtual variable in function '$@'."
935941
}
942+
943+
/**
944+
* Holds if a `MemoryLocation` is a member of more than one `VirtualVariable`.
945+
*/
946+
query predicate multipleVirtualVariablesForMemoryLocation(
947+
Alias::MemoryLocation location, string message, OldIR::IRFunction func, string funcText
948+
) {
949+
exists(int vvarCount |
950+
vvarCount = strictcount(location.getVirtualVariable()) and
951+
vvarCount > 1 and
952+
func = location.getIRFunction() and
953+
funcText = Language::getIdentityString(func.getFunction()) and
954+
message =
955+
"Memory location has " + vvarCount.toString() + " virtual variables in function '$@': (" +
956+
concat(Alias::VirtualVariable vvar |
957+
vvar = location.getVirtualVariable()
958+
|
959+
vvar.toString(), ", "
960+
) + ")."
961+
)
962+
}
936963
}

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/SSAConstruction.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,9 @@ private module CachedForDebugging {
913913
}
914914

915915
module SSAConsistency {
916+
/**
917+
* Holds if a `MemoryOperand` has more than one `MemoryLocation` assigned by alias analysis.
918+
*/
916919
query predicate multipleOperandMemoryLocations(
917920
OldIR::MemoryOperand operand, string message, OldIR::IRFunction func, string funcText
918921
) {
@@ -925,6 +928,9 @@ module SSAConsistency {
925928
)
926929
}
927930

931+
/**
932+
* Holds if a `MemoryLocation` does not have an associated `VirtualVariable`.
933+
*/
928934
query predicate missingVirtualVariableForMemoryLocation(
929935
Alias::MemoryLocation location, string message, OldIR::IRFunction func, string funcText
930936
) {
@@ -933,4 +939,25 @@ module SSAConsistency {
933939
funcText = Language::getIdentityString(func.getFunction()) and
934940
message = "Memory location has no virtual variable in function '$@'."
935941
}
942+
943+
/**
944+
* Holds if a `MemoryLocation` is a member of more than one `VirtualVariable`.
945+
*/
946+
query predicate multipleVirtualVariablesForMemoryLocation(
947+
Alias::MemoryLocation location, string message, OldIR::IRFunction func, string funcText
948+
) {
949+
exists(int vvarCount |
950+
vvarCount = strictcount(location.getVirtualVariable()) and
951+
vvarCount > 1 and
952+
func = location.getIRFunction() and
953+
funcText = Language::getIdentityString(func.getFunction()) and
954+
message =
955+
"Memory location has " + vvarCount.toString() + " virtual variables in function '$@': (" +
956+
concat(Alias::VirtualVariable vvar |
957+
vvar = location.getVirtualVariable()
958+
|
959+
vvar.toString(), ", "
960+
) + ")."
961+
)
962+
}
936963
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
multipleOperandMemoryLocations
22
missingVirtualVariableForMemoryLocation
3+
multipleVirtualVariablesForMemoryLocation
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
multipleOperandMemoryLocations
22
missingVirtualVariableForMemoryLocation
3+
multipleVirtualVariablesForMemoryLocation
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
multipleOperandMemoryLocations
22
missingVirtualVariableForMemoryLocation
3+
multipleVirtualVariablesForMemoryLocation
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
multipleOperandMemoryLocations
22
missingVirtualVariableForMemoryLocation
3+
multipleVirtualVariablesForMemoryLocation

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_consistency_unsound.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ switchInstructionWithoutDefaultEdge
2020
notMarkedAsConflated
2121
wronglyMarkedAsConflated
2222
invalidOverlap
23-
| ssa.cpp:301:27:301:30 | SideEffect | MemoryOperand 'SideEffect' has a `getDefinitionOverlap()` of 'MayPartiallyOverlap'. | ssa.cpp:301:5:301:8 | IR: main | int main(int, char**) |
2423
missingCanonicalLanguageType
2524
multipleCanonicalLanguageTypes
2625
missingIRType

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir_unsound.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ ssa.cpp:
14261426
# 302| m302_8(unknown) = Chi : total:m301_4, partial:m302_7
14271427
# 302| v302_9(void) = ^BufferReadSideEffect[1] : &:r302_5, ~m301_10
14281428
# 302| m302_10(unknown) = ^BufferMayWriteSideEffect[1] : &:r302_5
1429-
# 302| m302_11(char *) = Chi : total:m301_10, partial:m302_10
1429+
# 302| m302_11(unknown) = Chi : total:m301_10, partial:m302_10
14301430
# 303| r303_1(glval<unknown>) = FunctionAddress[unknownFunction] :
14311431
# 303| r303_2(glval<int>) = VariableAddress[argc] :
14321432
# 303| r303_3(int) = Load : &:r303_2, m301_6
@@ -1437,7 +1437,7 @@ ssa.cpp:
14371437
# 303| m303_8(unknown) = Chi : total:m302_8, partial:m303_7
14381438
# 303| v303_9(void) = ^BufferReadSideEffect[1] : &:r303_5, ~m302_11
14391439
# 303| m303_10(unknown) = ^BufferMayWriteSideEffect[1] : &:r303_5
1440-
# 303| m303_11(char *) = Chi : total:m302_11, partial:m303_10
1440+
# 303| m303_11(unknown) = Chi : total:m302_11, partial:m303_10
14411441
# 304| r304_1(glval<int>) = VariableAddress[#return] :
14421442
# 304| r304_2(glval<char **>) = VariableAddress[argv] :
14431443
# 304| r304_3(char **) = Load : &:r304_2, m301_8
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
multipleOperandMemoryLocations
22
missingVirtualVariableForMemoryLocation
3+
multipleVirtualVariablesForMemoryLocation

0 commit comments

Comments
 (0)