Skip to content

Commit 56cbd0c

Browse files
author
Dave Bartolomeo
committed
C++/C#: Make AliasedUse access only non-local memory
The `AliasedUse` instruction is supposed to represent future uses of aliased memory after the function returns. Since local variables from that function are no longer allocated after the function returns, the `AliasedUse` instruction should access only the set of aliased locations that does not include locals from the current stack frame.
1 parent 1223388 commit 56cbd0c

File tree

9 files changed

+66
-9
lines changed

9 files changed

+66
-9
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/MemoryAccessKind.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ private newtype TMemoryAccessKind =
55
TBufferMayMemoryAccess() or
66
TEscapedMemoryAccess() or
77
TEscapedMayMemoryAccess() or
8+
TNonLocalMayMemoryAccess() or
89
TPhiMemoryAccess() or
910
TUnmodeledMemoryAccess() or
1011
TChiTotalMemoryAccess() or
@@ -80,6 +81,14 @@ class EscapedMayMemoryAccess extends MemoryAccessKind, TEscapedMayMemoryAccess {
8081
override string toString() { result = "escaped(may)" }
8182
}
8283

84+
/**
85+
* The operand or result may access all memory whose address has escaped, other than data on the
86+
* stack frame of the current function.
87+
*/
88+
class NonLocalMayMemoryAccess extends MemoryAccessKind, TNonLocalMayMemoryAccess {
89+
override string toString() { result = "nonlocal(may)" }
90+
}
91+
8392
/**
8493
* The operand is a Phi operand, which accesses the same memory as its
8594
* definition.

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Operand.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class SideEffectOperand extends TypedOperand {
389389

390390
override MemoryAccessKind getMemoryAccess() {
391391
useInstr instanceof AliasedUseInstruction and
392-
result instanceof EscapedMayMemoryAccess
392+
result instanceof NonLocalMayMemoryAccess
393393
or
394394
useInstr instanceof CallSideEffectInstruction and
395395
result instanceof EscapedMayMemoryAccess

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private newtype TMemoryLocation =
3535
hasOperandMemoryAccess(_, var, type, startBitOffset, endBitOffset)
3636
} or
3737
TUnknownMemoryLocation(IRFunction irFunc) or
38+
TUnknownNonLocalMemoryLocation(IRFunction irFunc) or
3839
TUnknownVirtualVariable(IRFunction irFunc)
3940

4041
/**
@@ -133,6 +134,24 @@ class UnknownMemoryLocation extends TUnknownMemoryLocation, MemoryLocation {
133134
final override string getUniqueId() { result = "{Unknown}" }
134135
}
135136

137+
/**
138+
* An access to memory that is not known to be confined to a specific `IRVariable`, but is known to
139+
* not access memory on the current function's stack frame.
140+
*/
141+
class UnknownNonLocalMemoryLocation extends TUnknownNonLocalMemoryLocation, MemoryLocation {
142+
IRFunction irFunc;
143+
144+
UnknownNonLocalMemoryLocation() { this = TUnknownNonLocalMemoryLocation(irFunc) }
145+
146+
final override string toString() { result = "{UnknownNonLocal}" }
147+
148+
final override VirtualVariable getVirtualVariable() { result = TUnknownVirtualVariable(irFunc) }
149+
150+
final override Type getType() { result instanceof UnknownType }
151+
152+
final override string getUniqueId() { result = "{UnknownNonLocal}" }
153+
}
154+
136155
/**
137156
* An access to all aliased memory.
138157
*/
@@ -163,6 +182,13 @@ Overlap getOverlap(MemoryLocation def, MemoryLocation use) {
163182
def instanceof UnknownMemoryLocation and
164183
result instanceof MayPartiallyOverlap
165184
or
185+
// An UnknownNonLocalMemoryLocation may partially overlap any location within the same virtual
186+
// variable, except a local variable.
187+
def.getVirtualVariable() = use.getVirtualVariable() and
188+
def instanceof UnknownNonLocalMemoryLocation and
189+
result instanceof MayPartiallyOverlap and
190+
not use.(VariableMemoryLocation).getVariable() instanceof IRAutomaticVariable
191+
or
166192
exists(VariableMemoryLocation defVariableLocation |
167193
defVariableLocation = def and
168194
(
@@ -171,6 +197,13 @@ Overlap getOverlap(MemoryLocation def, MemoryLocation use) {
171197
(use instanceof UnknownMemoryLocation or use instanceof UnknownVirtualVariable) and
172198
result instanceof MayPartiallyOverlap
173199
or
200+
// A VariableMemoryLocation that is not a local variable may partially overlap an unknown
201+
// non-local location within the same virtual variable.
202+
def.getVirtualVariable() = use.getVirtualVariable() and
203+
use instanceof UnknownNonLocalMemoryLocation and
204+
result instanceof MayPartiallyOverlap and
205+
not defVariableLocation.getVariable() instanceof IRAutomaticVariable
206+
or
174207
// A VariableMemoryLocation overlaps another location within the same variable based on the relationship
175208
// of the two offset intervals.
176209
exists(Overlap intervalOverlap |
@@ -296,6 +329,9 @@ MemoryLocation getResultMemoryLocation(Instruction instr) {
296329
or
297330
kind instanceof EscapedMayMemoryAccess and
298331
result = TUnknownMemoryLocation(instr.getEnclosingIRFunction())
332+
or
333+
kind instanceof NonLocalMayMemoryAccess and
334+
result = TUnknownNonLocalMemoryLocation(instr.getEnclosingIRFunction())
299335
)
300336
)
301337
}
@@ -320,6 +356,9 @@ MemoryLocation getOperandMemoryLocation(MemoryOperand operand) {
320356
or
321357
kind instanceof EscapedMayMemoryAccess and
322358
result = TUnknownMemoryLocation(operand.getEnclosingIRFunction())
359+
or
360+
kind instanceof NonLocalMayMemoryAccess and
361+
result = TUnknownNonLocalMemoryLocation(operand.getEnclosingIRFunction())
323362
)
324363
)
325364
}

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Operand.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class SideEffectOperand extends TypedOperand {
389389

390390
override MemoryAccessKind getMemoryAccess() {
391391
useInstr instanceof AliasedUseInstruction and
392-
result instanceof EscapedMayMemoryAccess
392+
result instanceof NonLocalMayMemoryAccess
393393
or
394394
useInstr instanceof CallSideEffectInstruction and
395395
result instanceof EscapedMayMemoryAccess

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Operand.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class SideEffectOperand extends TypedOperand {
389389

390390
override MemoryAccessKind getMemoryAccess() {
391391
useInstr instanceof AliasedUseInstruction and
392-
result instanceof EscapedMayMemoryAccess
392+
result instanceof NonLocalMayMemoryAccess
393393
or
394394
useInstr instanceof CallSideEffectInstruction and
395395
result instanceof EscapedMayMemoryAccess

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ ssa.cpp:
340340
# 98| v0_19(void) = NoOp :
341341
# 95| v0_20(void) = ReturnVoid :
342342
# 95| v0_21(void) = UnmodeledUse : mu*
343-
# 95| v0_22(void) = AliasedUse : ~m0_18
343+
# 95| v0_22(void) = AliasedUse : ~m0_15
344344
# 95| v0_23(void) = ExitFunction :
345345

346346
# 100| void MustTotallyOverlap(Point)
@@ -396,7 +396,7 @@ ssa.cpp:
396396
# 109| v0_25(void) = NoOp :
397397
# 105| v0_26(void) = ReturnVoid :
398398
# 105| v0_27(void) = UnmodeledUse : mu*
399-
# 105| v0_28(void) = AliasedUse : ~m0_24
399+
# 105| v0_28(void) = AliasedUse : ~m0_21
400400
# 105| v0_29(void) = ExitFunction :
401401

402402
# 111| void MayPartiallyOverlap(int, int)
@@ -468,7 +468,7 @@ ssa.cpp:
468468
# 120| v0_33(void) = NoOp :
469469
# 116| v0_34(void) = ReturnVoid :
470470
# 116| v0_35(void) = UnmodeledUse : mu*
471-
# 116| v0_36(void) = AliasedUse : ~m0_32
471+
# 116| v0_36(void) = AliasedUse : ~m0_29
472472
# 116| v0_37(void) = ExitFunction :
473473

474474
# 122| void MergeMustExactlyOverlap(bool, int, int)
@@ -865,5 +865,5 @@ ssa.cpp:
865865
# 207| r0_23(glval<int>) = VariableAddress[#return] :
866866
# 207| v0_24(void) = ReturnValue : &:r0_23, m0_22
867867
# 207| v0_25(void) = UnmodeledUse : mu*
868-
# 207| v0_26(void) = AliasedUse : ~m0_18
868+
# 207| v0_26(void) = AliasedUse : ~m0_1
869869
# 207| v0_27(void) = ExitFunction :

csharp/ql/src/semmle/code/csharp/ir/implementation/MemoryAccessKind.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ private newtype TMemoryAccessKind =
55
TBufferMayMemoryAccess() or
66
TEscapedMemoryAccess() or
77
TEscapedMayMemoryAccess() or
8+
TNonLocalMayMemoryAccess() or
89
TPhiMemoryAccess() or
910
TUnmodeledMemoryAccess() or
1011
TChiTotalMemoryAccess() or
@@ -80,6 +81,14 @@ class EscapedMayMemoryAccess extends MemoryAccessKind, TEscapedMayMemoryAccess {
8081
override string toString() { result = "escaped(may)" }
8182
}
8283

84+
/**
85+
* The operand or result may access all memory whose address has escaped, other than data on the
86+
* stack frame of the current function.
87+
*/
88+
class NonLocalMayMemoryAccess extends MemoryAccessKind, TNonLocalMayMemoryAccess {
89+
override string toString() { result = "nonlocal(may)" }
90+
}
91+
8392
/**
8493
* The operand is a Phi operand, which accesses the same memory as its
8594
* definition.

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/Operand.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class SideEffectOperand extends TypedOperand {
389389

390390
override MemoryAccessKind getMemoryAccess() {
391391
useInstr instanceof AliasedUseInstruction and
392-
result instanceof EscapedMayMemoryAccess
392+
result instanceof NonLocalMayMemoryAccess
393393
or
394394
useInstr instanceof CallSideEffectInstruction and
395395
result instanceof EscapedMayMemoryAccess

csharp/ql/src/semmle/code/csharp/ir/implementation/unaliased_ssa/Operand.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class SideEffectOperand extends TypedOperand {
389389

390390
override MemoryAccessKind getMemoryAccess() {
391391
useInstr instanceof AliasedUseInstruction and
392-
result instanceof EscapedMayMemoryAccess
392+
result instanceof NonLocalMayMemoryAccess
393393
or
394394
useInstr instanceof CallSideEffectInstruction and
395395
result instanceof EscapedMayMemoryAccess

0 commit comments

Comments
 (0)