Skip to content

Commit 8a53dc8

Browse files
committed
C++: treat this as a parameter in IR
1 parent b9ecf1a commit 8a53dc8

File tree

20 files changed

+1271
-929
lines changed

20 files changed

+1271
-929
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ class IREllipsisVariable extends IRTempVariable {
223223
final override string toString() { result = "#ellipsis" }
224224
}
225225

226+
/**
227+
* A temporary variable generated to hold the contents of all arguments passed to the `...` of a
228+
* function that accepts a variable number of arguments.
229+
*/
230+
class IRThisVariable extends IRTempVariable {
231+
IRThisVariable() { tag = ThisTempVar() }
232+
233+
final override string toString() { result = "#this" }
234+
}
235+
226236
/**
227237
* A variable generated to represent the contents of a string literal. This variable acts much like
228238
* a read-only global variable.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private predicate isArgumentForParameter(CallInstruction ci, Operand operand, In
204204
init.(InitializeParameterInstruction).getParameter() =
205205
f.getParameter(operand.(PositionalArgumentOperand).getIndex())
206206
or
207-
init instanceof InitializeThisInstruction and
207+
init.(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable and
208208
init.getEnclosingFunction() = f and
209209
operand instanceof ThisArgumentOperand
210210
) and

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ private import AliasAnalysis
55

66
private newtype TAllocation =
77
TVariableAllocation(IRVariable var) or
8-
TIndirectParameterAllocation(IRAutomaticUserVariable var) {
8+
TIndirectParameterAllocation(IRVariable var) {
99
exists(InitializeIndirectionInstruction instr | instr.getIRVariable() = var)
1010
} or
1111
TDynamicAllocation(CallInstruction call) {
@@ -74,7 +74,7 @@ class VariableAllocation extends Allocation, TVariableAllocation {
7474
}
7575

7676
class IndirectParameterAllocation extends Allocation, TIndirectParameterAllocation {
77-
IRAutomaticUserVariable var;
77+
IRVariable var;
7878

7979
IndirectParameterAllocation() { this = TIndirectParameterAllocation(var) }
8080

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ class IREllipsisVariable extends IRTempVariable {
223223
final override string toString() { result = "#ellipsis" }
224224
}
225225

226+
/**
227+
* A temporary variable generated to hold the contents of all arguments passed to the `...` of a
228+
* function that accepts a variable number of arguments.
229+
*/
230+
class IRThisVariable extends IRTempVariable {
231+
IRThisVariable() { tag = ThisTempVar() }
232+
233+
final override string toString() { result = "#this" }
234+
}
235+
226236
/**
227237
* A variable generated to represent the contents of a string literal. This variable acts much like
228238
* a read-only global variable.

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ private module Cached {
3535
getTranslatedFunction(func).hasUserVariable(var, type)
3636
}
3737

38+
cached
39+
predicate hasThisVariable(Function func, CppType type) {
40+
type = getTypeForGLValue(getTranslatedFunction(func).getThisType())
41+
}
42+
3843
cached
3944
predicate hasTempVariable(Function func, Locatable ast, TempVariableTag tag, CppType type) {
4045
exists(TranslatedElement element |

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/InstructionTag.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ private import cpp
22

33
newtype TInstructionTag =
44
OnlyInstructionTag() or // Single instruction (not including implicit Load)
5+
InitializeThisAddressTag() or
56
InitializeThisTag() or
7+
InitializeThisIndirectionAddressTag() or
8+
InitializeThisIndirectionTag() or
69
InitializerVariableAddressTag() or
710
InitializerLoadStringTag() or
811
InitializerStoreTag() or
@@ -70,7 +73,9 @@ newtype TInstructionTag =
7073
VarArgsMoveNextTag() or
7174
VarArgsVAListStoreTag() or
7275
AsmTag() or
73-
AsmInputTag(int elementIndex) { exists(AsmStmt asm | exists(asm.getChild(elementIndex))) }
76+
AsmInputTag(int elementIndex) { exists(AsmStmt asm | exists(asm.getChild(elementIndex))) } or
77+
ThisAddressTag() or
78+
ThisLoadTag()
7479

7580
class InstructionTag extends TInstructionTag {
7681
final string toString() { result = "Tag" }

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -664,31 +664,35 @@ class TranslatedThisExpr extends TranslatedNonConstantExpr {
664664
final override TranslatedElement getChild(int id) { none() }
665665

666666
final override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
667-
tag = OnlyInstructionTag() and
668-
opcode instanceof Opcode::CopyValue and
667+
tag = ThisAddressTag() and
668+
opcode instanceof Opcode::VariableAddress and
669+
resultType = getTypeForGLValue(any(UnknownType t))
670+
or
671+
tag = ThisLoadTag() and
672+
opcode instanceof Opcode::Load and
669673
resultType = getResultType()
670674
}
671675

672-
final override Instruction getResult() { result = getInstruction(OnlyInstructionTag()) }
676+
final override Instruction getResult() { result = getInstruction(ThisLoadTag()) }
673677

674-
final override Instruction getFirstInstruction() { result = getInstruction(OnlyInstructionTag()) }
678+
final override Instruction getFirstInstruction() { result = getInstruction(ThisAddressTag()) }
675679

676680
final override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
677681
kind instanceof GotoEdge and
678-
tag = OnlyInstructionTag() and
682+
tag = ThisAddressTag() and
683+
result = getInstruction(ThisLoadTag())
684+
or
685+
kind instanceof GotoEdge and
686+
tag = ThisLoadTag() and
679687
result = getParent().getChildSuccessor(this)
680688
}
681689

682690
final override Instruction getChildSuccessor(TranslatedElement child) { none() }
683691

684692
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
685-
tag = OnlyInstructionTag() and
686-
operandTag instanceof UnaryOperandTag and
687-
result = getInitializeThisInstruction()
688-
}
689-
690-
private Instruction getInitializeThisInstruction() {
691-
result = getTranslatedFunction(expr.getEnclosingFunction()).getInitializeThisInstruction()
693+
tag = ThisLoadTag() and
694+
operandTag instanceof AddressOperandTag and
695+
result = getInstruction(ThisAddressTag())
692696
}
693697
}
694698

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/TranslatedFunction.qll

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,24 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
117117
(
118118
tag = InitializeNonLocalTag() and
119119
if exists(getThisType())
120-
then result = getInstruction(InitializeThisTag())
120+
then result = getInstruction(InitializeThisAddressTag())
121121
else
122122
if exists(getParameter(0))
123123
then result = getParameter(0).getFirstInstruction()
124124
else result = getBody().getFirstInstruction()
125125
)
126126
or
127+
tag = InitializeThisAddressTag() and
128+
result = getInstruction(InitializeThisTag())
129+
or
130+
tag = InitializeThisTag() and
131+
result = getInstruction(InitializeThisIndirectionAddressTag())
132+
or
133+
tag = InitializeThisIndirectionAddressTag() and
134+
result = getInstruction(InitializeThisIndirectionTag())
135+
or
127136
(
128-
tag = InitializeThisTag() and
137+
tag = InitializeThisIndirectionTag() and
129138
if exists(getParameter(0))
130139
then result = getParameter(0).getFirstInstruction()
131140
else result = getConstructorInitList().getFirstInstruction()
@@ -184,10 +193,23 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
184193
opcode instanceof Opcode::InitializeNonLocal and
185194
resultType = getUnknownType()
186195
or
196+
tag = InitializeThisAddressTag() and
197+
opcode instanceof Opcode::VariableAddress and
198+
resultType = getTypeForGLValue(any(UnknownType t)) and
199+
exists(getThisType())
200+
or
187201
tag = InitializeThisTag() and
188-
opcode instanceof Opcode::InitializeThis and
202+
opcode instanceof Opcode::InitializeParameter and
203+
resultType = getTypeForGLValue(getThisType())
204+
or
205+
tag = InitializeThisIndirectionAddressTag() and
206+
opcode instanceof Opcode::Load and
189207
resultType = getTypeForGLValue(getThisType())
190208
or
209+
tag = InitializeThisIndirectionTag() and
210+
opcode instanceof Opcode::InitializeIndirection and
211+
resultType = getTypeForPRValue(getThisType())
212+
or
191213
tag = ReturnValueAddressTag() and
192214
opcode instanceof Opcode::VariableAddress and
193215
resultType = getTypeForGLValue(getReturnType()) and
@@ -228,10 +250,23 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
228250
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
229251
tag = ReturnTag() and
230252
hasReturnValue() and
231-
(
232-
operandTag instanceof AddressOperandTag and
233-
result = getInstruction(ReturnValueAddressTag())
234-
)
253+
operandTag instanceof AddressOperandTag and
254+
result = getInstruction(ReturnValueAddressTag())
255+
or
256+
tag = InitializeThisTag() and
257+
exists(getThisType()) and
258+
operandTag instanceof AddressOperandTag and
259+
result = getInstruction(InitializeThisAddressTag())
260+
or
261+
tag = InitializeThisIndirectionAddressTag() and
262+
exists(getThisType()) and
263+
operandTag instanceof AddressOperandTag and
264+
result = getInstruction(InitializeThisAddressTag())
265+
or
266+
tag = InitializeThisIndirectionTag() and
267+
exists(getThisType()) and
268+
operandTag instanceof AddressOperandTag and
269+
result = getInstruction(InitializeThisIndirectionAddressTag())
235270
}
236271

237272
final override CppType getInstructionMemoryOperandType(
@@ -245,9 +280,23 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
245280
tag = AliasedUseTag() and
246281
operandTag instanceof SideEffectOperandTag and
247282
result = getUnknownType()
283+
or
284+
tag = InitializeThisIndirectionAddressTag() and
285+
exists(getThisType()) and
286+
operandTag instanceof LoadOperandTag and
287+
result = getTypeForGLValue(getThisType())
248288
}
249289

250290
final override IRVariable getInstructionVariable(InstructionTag tag) {
291+
tag = InitializeThisAddressTag() and
292+
result = getThisVariable()
293+
or
294+
tag = InitializeThisTag() and
295+
result = getThisVariable()
296+
or
297+
tag = InitializeThisIndirectionTag() and
298+
result = getThisVariable()
299+
or
251300
tag = ReturnValueAddressTag() and
252301
result = getReturnVariable()
253302
}
@@ -264,6 +313,9 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
264313
tag = EllipsisTempVar() and
265314
func.isVarargs() and
266315
type = getEllipsisVariablePRValueType()
316+
or
317+
tag = ThisTempVar() and
318+
type = getTypeForGLValue(getThisType())
267319
}
268320

269321
/**
@@ -286,6 +338,13 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
286338
*/
287339
final IREllipsisVariable getEllipsisVariable() { result.getEnclosingFunction() = func }
288340

341+
/**
342+
* Gets the variable that represents the `this` pointer for this function, if any.
343+
*/
344+
final IRThisVariable getThisVariable() {
345+
result = getIRTempVariable(func, ThisTempVar())
346+
}
347+
289348
/**
290349
* Holds if the function has a non-`void` return type.
291350
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ class IREllipsisVariable extends IRTempVariable {
223223
final override string toString() { result = "#ellipsis" }
224224
}
225225

226+
/**
227+
* A temporary variable generated to hold the contents of all arguments passed to the `...` of a
228+
* function that accepts a variable number of arguments.
229+
*/
230+
class IRThisVariable extends IRTempVariable {
231+
IRThisVariable() { tag = ThisTempVar() }
232+
233+
final override string toString() { result = "#this" }
234+
}
235+
226236
/**
227237
* A variable generated to represent the contents of a string literal. This variable acts much like
228238
* a read-only global variable.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private predicate isArgumentForParameter(CallInstruction ci, Operand operand, In
204204
init.(InitializeParameterInstruction).getParameter() =
205205
f.getParameter(operand.(PositionalArgumentOperand).getIndex())
206206
or
207-
init instanceof InitializeThisInstruction and
207+
init.(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable and
208208
init.getEnclosingFunction() = f and
209209
operand instanceof ThisArgumentOperand
210210
) and

0 commit comments

Comments
 (0)