Skip to content

Commit 260a8d4

Browse files
authored
Merge pull request github#4702 from MathiasVP/qualifier-as-parameter-for-callee
C++: Abstractions for treating qualifiers as parameters in IR
2 parents 2277242 + 9d21b22 commit 260a8d4

File tree

7 files changed

+258
-29
lines changed

7 files changed

+258
-29
lines changed

cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ private class PrimaryArgumentNode extends ArgumentNode {
2828

2929
PrimaryArgumentNode() { exists(CallInstruction call | op = call.getAnArgumentOperand()) }
3030

31-
override predicate argumentOf(DataFlowCall call, int pos) {
32-
op = call.getPositionalArgumentOperand(pos)
33-
or
34-
op = call.getThisArgumentOperand() and pos = -1
35-
}
31+
override predicate argumentOf(DataFlowCall call, int pos) { op = call.getArgumentOperand(pos) }
3632

3733
override string toString() {
3834
result = "Argument " + op.(PositionalArgumentOperand).getIndex()
@@ -110,10 +106,10 @@ class ReturnIndirectionNode extends ReturnNode {
110106
override ReturnIndirectionInstruction primary;
111107

112108
override ReturnKind getKind() {
113-
result = TIndirectReturnKind(-1) and
114-
primary.isThisIndirection()
115-
or
116-
result = TIndirectReturnKind(primary.getParameter().getIndex())
109+
exists(int index |
110+
primary.hasIndex(index) and
111+
result = TIndirectReturnKind(index)
112+
)
117113
}
118114
}
119115

@@ -500,13 +496,6 @@ class DataFlowType = IRType;
500496

501497
/** A function call relevant for data flow. */
502498
class DataFlowCall extends CallInstruction {
503-
/**
504-
* Gets the nth argument for this call.
505-
*
506-
* The range of `n` is from `0` to `getNumberOfArguments() - 1`.
507-
*/
508-
Node getArgument(int n) { result.asInstruction() = this.getPositionalArgument(n) }
509-
510499
Function getEnclosingCallable() { result = this.getEnclosingFunction() }
511500
}
512501

cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,8 @@ class ParameterIndirectionNode extends ParameterNode {
266266

267267
override predicate isParameterOf(Function f, int pos) {
268268
exists(int index |
269-
f.getParameter(index) = instr.getParameter()
270-
or
271-
index = -1 and
272-
instr.getIRVariable().(IRThisVariable).getEnclosingFunction() = f
269+
instr.getEnclosingFunction() = f and
270+
instr.hasIndex(index)
273271
|
274272
pos = getArgumentPosOfSideEffect(index)
275273
)
@@ -476,16 +474,8 @@ class DefinitionByReferenceNode extends InstructionNode {
476474
instr
477475
.getPrimaryInstruction()
478476
.(CallInstruction)
479-
.getPositionalArgument(instr.getIndex())
477+
.getArgument(instr.getIndex())
480478
.getUnconvertedResultExpression()
481-
or
482-
result =
483-
instr
484-
.getPrimaryInstruction()
485-
.(CallInstruction)
486-
.getThisArgument()
487-
.getUnconvertedResultExpression() and
488-
instr.getIndex() = -1
489479
}
490480

491481
/** Gets the parameter through which this value is assigned. */

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,17 @@ class InitializeParameterInstruction extends VariableInstruction {
588588
* Gets the parameter initialized by this instruction.
589589
*/
590590
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
591+
592+
/**
593+
* Holds if this instruction initializes the parameter with index `index`, or
594+
* if `index` is `-1` and this instruction initializes `this`.
595+
*/
596+
pragma[noinline]
597+
final predicate hasIndex(int index) {
598+
index >= 0 and index = this.getParameter().getIndex()
599+
or
600+
index = -1 and this.getIRVariable() instanceof IRThisVariable
601+
}
591602
}
592603

593604
/**
@@ -601,6 +612,18 @@ class InitializeIndirectionInstruction extends VariableInstruction {
601612
* Gets the parameter initialized by this instruction.
602613
*/
603614
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
615+
616+
/**
617+
* Holds if this instruction initializes the memory pointed to by the parameter with
618+
* index `index`, or if `index` is `-1` and this instruction initializes the memory
619+
* pointed to by `this`.
620+
*/
621+
pragma[noinline]
622+
final predicate hasIndex(int index) {
623+
index >= 0 and index = this.getParameter().getIndex()
624+
or
625+
index = -1 and this.getIRVariable() instanceof IRThisVariable
626+
}
604627
}
605628

606629
/**
@@ -775,6 +798,17 @@ class ReturnIndirectionInstruction extends VariableInstruction {
775798
* Holds if this instruction is the return indirection for `this`.
776799
*/
777800
final predicate isThisIndirection() { var instanceof IRThisVariable }
801+
802+
/**
803+
* Holds if this instruction is the return indirection for the parameter with index `index`, or
804+
* if this instruction is the return indirection for `this` and `index` is `-1`.
805+
*/
806+
pragma[noinline]
807+
final predicate hasIndex(int index) {
808+
index >= 0 and index = this.getParameter().getIndex()
809+
or
810+
index = -1 and this.isThisIndirection()
811+
}
778812
}
779813

780814
/**
@@ -1587,6 +1621,22 @@ class CallInstruction extends Instruction {
15871621
result = getPositionalArgumentOperand(index).getDef()
15881622
}
15891623

1624+
/**
1625+
* Gets the argument operand at the specified index, or `this` if `index` is `-1`.
1626+
*/
1627+
pragma[noinline]
1628+
final ArgumentOperand getArgumentOperand(int index) {
1629+
index >= 0 and result = getPositionalArgumentOperand(index)
1630+
or
1631+
index = -1 and result = getThisArgumentOperand()
1632+
}
1633+
1634+
/**
1635+
* Gets the argument at the specified index, or `this` if `index` is `-1`.
1636+
*/
1637+
pragma[noinline]
1638+
final Instruction getArgument(int index) { result = getArgumentOperand(index).getDef() }
1639+
15901640
/**
15911641
* Gets the number of arguments of the call, including the `this` pointer, if any.
15921642
*/

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,17 @@ class InitializeParameterInstruction extends VariableInstruction {
588588
* Gets the parameter initialized by this instruction.
589589
*/
590590
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
591+
592+
/**
593+
* Holds if this instruction initializes the parameter with index `index`, or
594+
* if `index` is `-1` and this instruction initializes `this`.
595+
*/
596+
pragma[noinline]
597+
final predicate hasIndex(int index) {
598+
index >= 0 and index = this.getParameter().getIndex()
599+
or
600+
index = -1 and this.getIRVariable() instanceof IRThisVariable
601+
}
591602
}
592603

593604
/**
@@ -601,6 +612,18 @@ class InitializeIndirectionInstruction extends VariableInstruction {
601612
* Gets the parameter initialized by this instruction.
602613
*/
603614
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
615+
616+
/**
617+
* Holds if this instruction initializes the memory pointed to by the parameter with
618+
* index `index`, or if `index` is `-1` and this instruction initializes the memory
619+
* pointed to by `this`.
620+
*/
621+
pragma[noinline]
622+
final predicate hasIndex(int index) {
623+
index >= 0 and index = this.getParameter().getIndex()
624+
or
625+
index = -1 and this.getIRVariable() instanceof IRThisVariable
626+
}
604627
}
605628

606629
/**
@@ -775,6 +798,17 @@ class ReturnIndirectionInstruction extends VariableInstruction {
775798
* Holds if this instruction is the return indirection for `this`.
776799
*/
777800
final predicate isThisIndirection() { var instanceof IRThisVariable }
801+
802+
/**
803+
* Holds if this instruction is the return indirection for the parameter with index `index`, or
804+
* if this instruction is the return indirection for `this` and `index` is `-1`.
805+
*/
806+
pragma[noinline]
807+
final predicate hasIndex(int index) {
808+
index >= 0 and index = this.getParameter().getIndex()
809+
or
810+
index = -1 and this.isThisIndirection()
811+
}
778812
}
779813

780814
/**
@@ -1587,6 +1621,22 @@ class CallInstruction extends Instruction {
15871621
result = getPositionalArgumentOperand(index).getDef()
15881622
}
15891623

1624+
/**
1625+
* Gets the argument operand at the specified index, or `this` if `index` is `-1`.
1626+
*/
1627+
pragma[noinline]
1628+
final ArgumentOperand getArgumentOperand(int index) {
1629+
index >= 0 and result = getPositionalArgumentOperand(index)
1630+
or
1631+
index = -1 and result = getThisArgumentOperand()
1632+
}
1633+
1634+
/**
1635+
* Gets the argument at the specified index, or `this` if `index` is `-1`.
1636+
*/
1637+
pragma[noinline]
1638+
final Instruction getArgument(int index) { result = getArgumentOperand(index).getDef() }
1639+
15901640
/**
15911641
* Gets the number of arguments of the call, including the `this` pointer, if any.
15921642
*/

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,17 @@ class InitializeParameterInstruction extends VariableInstruction {
588588
* Gets the parameter initialized by this instruction.
589589
*/
590590
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
591+
592+
/**
593+
* Holds if this instruction initializes the parameter with index `index`, or
594+
* if `index` is `-1` and this instruction initializes `this`.
595+
*/
596+
pragma[noinline]
597+
final predicate hasIndex(int index) {
598+
index >= 0 and index = this.getParameter().getIndex()
599+
or
600+
index = -1 and this.getIRVariable() instanceof IRThisVariable
601+
}
591602
}
592603

593604
/**
@@ -601,6 +612,18 @@ class InitializeIndirectionInstruction extends VariableInstruction {
601612
* Gets the parameter initialized by this instruction.
602613
*/
603614
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
615+
616+
/**
617+
* Holds if this instruction initializes the memory pointed to by the parameter with
618+
* index `index`, or if `index` is `-1` and this instruction initializes the memory
619+
* pointed to by `this`.
620+
*/
621+
pragma[noinline]
622+
final predicate hasIndex(int index) {
623+
index >= 0 and index = this.getParameter().getIndex()
624+
or
625+
index = -1 and this.getIRVariable() instanceof IRThisVariable
626+
}
604627
}
605628

606629
/**
@@ -775,6 +798,17 @@ class ReturnIndirectionInstruction extends VariableInstruction {
775798
* Holds if this instruction is the return indirection for `this`.
776799
*/
777800
final predicate isThisIndirection() { var instanceof IRThisVariable }
801+
802+
/**
803+
* Holds if this instruction is the return indirection for the parameter with index `index`, or
804+
* if this instruction is the return indirection for `this` and `index` is `-1`.
805+
*/
806+
pragma[noinline]
807+
final predicate hasIndex(int index) {
808+
index >= 0 and index = this.getParameter().getIndex()
809+
or
810+
index = -1 and this.isThisIndirection()
811+
}
778812
}
779813

780814
/**
@@ -1587,6 +1621,22 @@ class CallInstruction extends Instruction {
15871621
result = getPositionalArgumentOperand(index).getDef()
15881622
}
15891623

1624+
/**
1625+
* Gets the argument operand at the specified index, or `this` if `index` is `-1`.
1626+
*/
1627+
pragma[noinline]
1628+
final ArgumentOperand getArgumentOperand(int index) {
1629+
index >= 0 and result = getPositionalArgumentOperand(index)
1630+
or
1631+
index = -1 and result = getThisArgumentOperand()
1632+
}
1633+
1634+
/**
1635+
* Gets the argument at the specified index, or `this` if `index` is `-1`.
1636+
*/
1637+
pragma[noinline]
1638+
final Instruction getArgument(int index) { result = getArgumentOperand(index).getDef() }
1639+
15901640
/**
15911641
* Gets the number of arguments of the call, including the `this` pointer, if any.
15921642
*/

csharp/ql/src/experimental/ir/implementation/raw/Instruction.qll

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,17 @@ class InitializeParameterInstruction extends VariableInstruction {
588588
* Gets the parameter initialized by this instruction.
589589
*/
590590
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
591+
592+
/**
593+
* Holds if this instruction initializes the parameter with index `index`, or
594+
* if `index` is `-1` and this instruction initializes `this`.
595+
*/
596+
pragma[noinline]
597+
final predicate hasIndex(int index) {
598+
index >= 0 and index = this.getParameter().getIndex()
599+
or
600+
index = -1 and this.getIRVariable() instanceof IRThisVariable
601+
}
591602
}
592603

593604
/**
@@ -601,6 +612,18 @@ class InitializeIndirectionInstruction extends VariableInstruction {
601612
* Gets the parameter initialized by this instruction.
602613
*/
603614
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
615+
616+
/**
617+
* Holds if this instruction initializes the memory pointed to by the parameter with
618+
* index `index`, or if `index` is `-1` and this instruction initializes the memory
619+
* pointed to by `this`.
620+
*/
621+
pragma[noinline]
622+
final predicate hasIndex(int index) {
623+
index >= 0 and index = this.getParameter().getIndex()
624+
or
625+
index = -1 and this.getIRVariable() instanceof IRThisVariable
626+
}
604627
}
605628

606629
/**
@@ -775,6 +798,17 @@ class ReturnIndirectionInstruction extends VariableInstruction {
775798
* Holds if this instruction is the return indirection for `this`.
776799
*/
777800
final predicate isThisIndirection() { var instanceof IRThisVariable }
801+
802+
/**
803+
* Holds if this instruction is the return indirection for the parameter with index `index`, or
804+
* if this instruction is the return indirection for `this` and `index` is `-1`.
805+
*/
806+
pragma[noinline]
807+
final predicate hasIndex(int index) {
808+
index >= 0 and index = this.getParameter().getIndex()
809+
or
810+
index = -1 and this.isThisIndirection()
811+
}
778812
}
779813

780814
/**
@@ -1587,6 +1621,22 @@ class CallInstruction extends Instruction {
15871621
result = getPositionalArgumentOperand(index).getDef()
15881622
}
15891623

1624+
/**
1625+
* Gets the argument operand at the specified index, or `this` if `index` is `-1`.
1626+
*/
1627+
pragma[noinline]
1628+
final ArgumentOperand getArgumentOperand(int index) {
1629+
index >= 0 and result = getPositionalArgumentOperand(index)
1630+
or
1631+
index = -1 and result = getThisArgumentOperand()
1632+
}
1633+
1634+
/**
1635+
* Gets the argument at the specified index, or `this` if `index` is `-1`.
1636+
*/
1637+
pragma[noinline]
1638+
final Instruction getArgument(int index) { result = getArgumentOperand(index).getDef() }
1639+
15901640
/**
15911641
* Gets the number of arguments of the call, including the `this` pointer, if any.
15921642
*/

0 commit comments

Comments
 (0)