Skip to content

Commit ea2081c

Browse files
author
Dave Bartolomeo
authored
Merge pull request github#3459 from dbartol/github/codeql-c-analysis-team/69
C++/C#: Remove `UnmodeledUse` instruction
2 parents f5daeea + b0f7e9c commit ea2081c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+955
-1676
lines changed

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ private newtype TOpcode =
6161
TReThrow() or
6262
TUnwind() or
6363
TUnmodeledDefinition() or
64-
TUnmodeledUse() or
6564
TAliasedDefinition() or
6665
TInitializeNonLocal() or
6766
TAliasedUse() or
@@ -587,14 +586,6 @@ module Opcode {
587586
}
588587
}
589588

590-
class UnmodeledUse extends Opcode, TUnmodeledUse {
591-
final override string toString() { result = "UnmodeledUse" }
592-
593-
final override predicate hasOperandInternal(OperandTag tag) {
594-
tag instanceof UnmodeledUseOperandTag
595-
}
596-
}
597-
598589
class AliasedDefinition extends Opcode, TAliasedDefinition {
599590
final override string toString() { result = "AliasedDefinition" }
600591

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ class IRBlockBase extends TIRBlock {
3131
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
3232
) and
3333
this =
34-
rank[result + 1](IRBlock funcBlock |
35-
funcBlock.getEnclosingFunction() = getEnclosingFunction()
34+
rank[result + 1](IRBlock funcBlock, int sortOverride |
35+
funcBlock.getEnclosingFunction() = getEnclosingFunction() and
36+
// Ensure that the block containing `EnterFunction` always comes first.
37+
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
38+
then sortOverride = 0
39+
else sortOverride = 1
3640
|
37-
funcBlock order by funcBlock.getUniqueId()
41+
funcBlock order by sortOverride, funcBlock.getUniqueId()
3842
)
3943
}
4044

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ module InstructionConsistency {
5555
operand.getOperandTag() = tag
5656
) and
5757
operandCount > 1 and
58-
not tag instanceof UnmodeledUseOperandTag and
5958
message =
6059
"Instruction has " + operandCount + " operands with tag '" + tag.toString() + "'" +
6160
" in function '$@'." and
@@ -158,7 +157,6 @@ module InstructionConsistency {
158157
) {
159158
exists(MemoryOperand operand, Instruction def |
160159
operand = instr.getAnOperand() and
161-
not operand instanceof UnmodeledUseOperand and
162160
def = operand.getAnyDef() and
163161
not def.isResultModeled() and
164162
not def instanceof UnmodeledDefinitionInstruction and
@@ -259,7 +257,6 @@ module InstructionConsistency {
259257
Operand useOperand, string message, IRFunction func, string funcText
260258
) {
261259
exists(IRBlock useBlock, int useIndex, Instruction defInstr, IRBlock defBlock, int defIndex |
262-
not useOperand.getUse() instanceof UnmodeledUseInstruction and
263260
not defInstr instanceof UnmodeledDefinitionInstruction and
264261
pointOfEvaluation(useOperand, useBlock, useIndex) and
265262
defInstr = useOperand.getAnyDef() and

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ class IRFunction extends TIRFunction {
4545
result.getEnclosingIRFunction() = this
4646
}
4747

48-
pragma[noinline]
49-
final UnmodeledUseInstruction getUnmodeledUseInstruction() {
50-
result.getEnclosingIRFunction() = this
51-
}
52-
5348
/**
5449
* Gets the single return instruction for this function.
5550
*/

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ class Instruction extends Construction::TInstruction {
320320
/**
321321
* Holds if the result of this instruction is precisely modeled in SSA. Always
322322
* holds for a register result. For a memory result, a modeled result is
323-
* connected to its actual uses. An unmodeled result is connected to the
324-
* `UnmodeledUse` instruction.
323+
* connected to its actual uses. An unmodeled result has no uses.
325324
*
326325
* For example:
327326
* ```
@@ -1248,12 +1247,6 @@ class AliasedUseInstruction extends Instruction {
12481247
AliasedUseInstruction() { getOpcode() instanceof Opcode::AliasedUse }
12491248
}
12501249

1251-
class UnmodeledUseInstruction extends Instruction {
1252-
UnmodeledUseInstruction() { getOpcode() instanceof Opcode::UnmodeledUse }
1253-
1254-
override string getOperandsString() { result = "mu*" }
1255-
}
1256-
12571250
/**
12581251
* An instruction representing the choice of one of multiple input values based on control flow.
12591252
*

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ private newtype TOperand =
1919
) {
2020
defInstr = Construction::getMemoryOperandDefinition(useInstr, tag, overlap) and
2121
not Construction::isInCycle(useInstr) and
22-
(
23-
strictcount(Construction::getMemoryOperandDefinition(useInstr, tag, _)) = 1
24-
or
25-
tag instanceof UnmodeledUseOperandTag
26-
)
22+
strictcount(Construction::getMemoryOperandDefinition(useInstr, tag, _)) = 1
2723
} or
2824
TPhiOperand(
2925
PhiInstruction useInstr, Instruction defInstr, IRBlock predecessorBlock, Overlap overlap
@@ -327,16 +323,6 @@ class ConditionOperand extends RegisterOperand {
327323
override string toString() { result = "Condition" }
328324
}
329325

330-
/**
331-
* An operand of the special `UnmodeledUse` instruction, representing a value
332-
* whose set of uses is unknown.
333-
*/
334-
class UnmodeledUseOperand extends NonPhiMemoryOperand {
335-
override UnmodeledUseOperandTag tag;
336-
337-
override string toString() { result = "UnmodeledUse" }
338-
}
339-
340326
/**
341327
* The operand representing the target function of an `Call` instruction.
342328
*/

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ private predicate resultMayReachReturn(Instruction instr) { operandMayReachRetur
247247
private predicate resultEscapesNonReturn(Instruction instr) {
248248
// The result escapes if it has at least one use that escapes.
249249
operandEscapesNonReturn(instr.getAUse())
250+
or
251+
// The result also escapes if it is not modeled in SSA, because we do not know where it might be
252+
// used.
253+
not instr.isResultModeled()
250254
}
251255

252256
/**

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,10 @@ private module Cached {
128128
oldOperand = oldInstruction.getAnOperand() and
129129
tag = oldOperand.getOperandTag() and
130130
(
131-
(
132-
if exists(Alias::getOperandMemoryLocation(oldOperand))
133-
then hasMemoryOperandDefinition(oldInstruction, oldOperand, overlap, result)
134-
else (
135-
result = instruction.getEnclosingIRFunction().getUnmodeledDefinitionInstruction() and
136-
overlap instanceof MustTotallyOverlap
137-
)
138-
)
139-
or
140-
// Connect any definitions that are not being modeled in SSA to the
141-
// `UnmodeledUse` instruction.
142-
exists(OldInstruction oldDefinition |
143-
instruction instanceof UnmodeledUseInstruction and
144-
tag instanceof UnmodeledUseOperandTag and
145-
oldDefinition = oldOperand.getAnyDef() and
146-
not exists(Alias::getResultMemoryLocation(oldDefinition)) and
147-
result = getNewInstruction(oldDefinition) and
131+
if exists(Alias::getOperandMemoryLocation(oldOperand))
132+
then hasMemoryOperandDefinition(oldInstruction, oldOperand, overlap, result)
133+
else (
134+
result = instruction.getEnclosingIRFunction().getUnmodeledDefinitionInstruction() and
148135
overlap instanceof MustTotallyOverlap
149136
)
150137
)
@@ -154,13 +141,6 @@ private module Cached {
154141
tag instanceof ChiPartialOperandTag and
155142
overlap instanceof MustExactlyOverlap
156143
or
157-
exists(IRFunction f |
158-
tag instanceof UnmodeledUseOperandTag and
159-
result = f.getUnmodeledDefinitionInstruction() and
160-
instruction = f.getUnmodeledUseInstruction() and
161-
overlap instanceof MustTotallyOverlap
162-
)
163-
or
164144
tag instanceof ChiTotalOperandTag and
165145
result = getChiInstructionTotalOperand(instruction) and
166146
overlap instanceof MustExactlyOverlap

cpp/ql/src/semmle/code/cpp/ir/implementation/internal/OperandTag.qll

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ private newtype TOperandTag =
1515
TLeftOperand() or
1616
TRightOperand() or
1717
TConditionOperand() or
18-
TUnmodeledUseOperand() or
1918
TCallTargetOperand() or
2019
TThisArgumentOperand() or
2120
TPositionalArgumentOperand(int argIndex) { Language::hasPositionalArgIndex(argIndex) } or
@@ -165,18 +164,6 @@ class ConditionOperandTag extends RegisterOperandTag, TConditionOperand {
165164

166165
ConditionOperandTag conditionOperand() { result = TConditionOperand() }
167166

168-
/**
169-
* An operand of the special `UnmodeledUse` instruction, representing a value
170-
* whose set of uses is unknown.
171-
*/
172-
class UnmodeledUseOperandTag extends MemoryOperandTag, TUnmodeledUseOperand {
173-
final override string toString() { result = "UnmodeledUse" }
174-
175-
final override int getSortOrder() { result = 9 }
176-
}
177-
178-
UnmodeledUseOperandTag unmodeledUseOperand() { result = TUnmodeledUseOperand() }
179-
180167
/**
181168
* The operand representing the target function of an `Call` instruction.
182169
*/

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ class IRBlockBase extends TIRBlock {
3131
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
3232
) and
3333
this =
34-
rank[result + 1](IRBlock funcBlock |
35-
funcBlock.getEnclosingFunction() = getEnclosingFunction()
34+
rank[result + 1](IRBlock funcBlock, int sortOverride |
35+
funcBlock.getEnclosingFunction() = getEnclosingFunction() and
36+
// Ensure that the block containing `EnterFunction` always comes first.
37+
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
38+
then sortOverride = 0
39+
else sortOverride = 1
3640
|
37-
funcBlock order by funcBlock.getUniqueId()
41+
funcBlock order by sortOverride, funcBlock.getUniqueId()
3842
)
3943
}
4044

0 commit comments

Comments
 (0)