Skip to content

Commit 54b9f21

Browse files
author
Dave Bartolomeo
committed
C++: Allow annotating IR dumps with Alias Analysis info
This commit adds a `PrintAliasAnalysis.qll` module, which can be imported alongside `PrintIR.qll` to annotate those dumps with alias analysis results.
1 parent 3b04bed commit 54b9f21

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

config/identical-files.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@
249249
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/AliasAnalysis.qll",
250250
"csharp/ql/src/experimental/ir/implementation/unaliased_ssa/internal/AliasAnalysis.qll"
251251
],
252+
"SSA PrintAliasAnalysis": [
253+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/PrintAliasAnalysis.qll",
254+
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/PrintAliasAnalysis.qll"
255+
],
252256
"C++ SSA AliasAnalysisImports": [
253257
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/AliasAnalysisImports.qll",
254258
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/AliasAnalysisImports.qll"

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,46 @@ predicate addressOperandAllocationAndOffset(
400400
)
401401
)
402402
}
403+
404+
/**
405+
* Predicates used only for printing annotated IR dumps. These should not be used in production
406+
* queries.
407+
*/
408+
module Print {
409+
string getOperandProperty(Operand operand, string key) {
410+
key = "alloc" and
411+
result =
412+
strictconcat(Configuration::Allocation allocation, IntValue bitOffset |
413+
addressOperandAllocationAndOffset(operand, allocation, bitOffset)
414+
|
415+
allocation.toString() + Ints::getBitOffsetString(bitOffset), ", "
416+
)
417+
or
418+
key = "prop" and
419+
result =
420+
strictconcat(Instruction destInstr, IntValue bitOffset, string value |
421+
operandIsPropagatedIncludingByCall(operand, bitOffset, destInstr) and
422+
if destInstr = operand.getUse()
423+
then value = "@" + Ints::getBitOffsetString(bitOffset) + "->result"
424+
else value = "@" + Ints::getBitOffsetString(bitOffset) + "->" + destInstr.getResultId()
425+
|
426+
value, ", "
427+
)
428+
}
429+
430+
string getInstructionProperty(Instruction instr, string key) {
431+
key = "prop" and
432+
result =
433+
strictconcat(IntValue bitOffset, Operand sourceOperand, string value |
434+
operandIsPropagatedIncludingByCall(sourceOperand, bitOffset, instr) and
435+
if instr = sourceOperand.getUse()
436+
then value = sourceOperand.getDumpId() + Ints::getBitOffsetString(bitOffset) + "->@"
437+
else
438+
value =
439+
sourceOperand.getUse().getResultId() + "." + sourceOperand.getDumpId() +
440+
Ints::getBitOffsetString(bitOffset) + "->@"
441+
|
442+
value, ", "
443+
)
444+
}
445+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Include this module to annotate IR dumps with information computed by `AliasAnalysis.qll`.
3+
*/
4+
5+
private import AliasAnalysisInternal
6+
private import InputIR
7+
private import AliasAnalysisImports
8+
private import AliasAnalysis
9+
private import semmle.code.cpp.ir.internal.IntegerConstant
10+
11+
private class AliasPropertyProvider extends IRPropertyProvider {
12+
override string getOperandProperty(Operand operand, string key) {
13+
result = Print::getOperandProperty(operand, key)
14+
}
15+
16+
override string getInstructionProperty(Instruction instr, string key) {
17+
result = Print::getInstructionProperty(instr, key)
18+
}
19+
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,46 @@ predicate addressOperandAllocationAndOffset(
400400
)
401401
)
402402
}
403+
404+
/**
405+
* Predicates used only for printing annotated IR dumps. These should not be used in production
406+
* queries.
407+
*/
408+
module Print {
409+
string getOperandProperty(Operand operand, string key) {
410+
key = "alloc" and
411+
result =
412+
strictconcat(Configuration::Allocation allocation, IntValue bitOffset |
413+
addressOperandAllocationAndOffset(operand, allocation, bitOffset)
414+
|
415+
allocation.toString() + Ints::getBitOffsetString(bitOffset), ", "
416+
)
417+
or
418+
key = "prop" and
419+
result =
420+
strictconcat(Instruction destInstr, IntValue bitOffset, string value |
421+
operandIsPropagatedIncludingByCall(operand, bitOffset, destInstr) and
422+
if destInstr = operand.getUse()
423+
then value = "@" + Ints::getBitOffsetString(bitOffset) + "->result"
424+
else value = "@" + Ints::getBitOffsetString(bitOffset) + "->" + destInstr.getResultId()
425+
|
426+
value, ", "
427+
)
428+
}
429+
430+
string getInstructionProperty(Instruction instr, string key) {
431+
key = "prop" and
432+
result =
433+
strictconcat(IntValue bitOffset, Operand sourceOperand, string value |
434+
operandIsPropagatedIncludingByCall(sourceOperand, bitOffset, instr) and
435+
if instr = sourceOperand.getUse()
436+
then value = sourceOperand.getDumpId() + Ints::getBitOffsetString(bitOffset) + "->@"
437+
else
438+
value =
439+
sourceOperand.getUse().getResultId() + "." + sourceOperand.getDumpId() +
440+
Ints::getBitOffsetString(bitOffset) + "->@"
441+
|
442+
value, ", "
443+
)
444+
}
445+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Include this module to annotate IR dumps with information computed by `AliasAnalysis.qll`.
3+
*/
4+
5+
private import AliasAnalysisInternal
6+
private import InputIR
7+
private import AliasAnalysisImports
8+
private import AliasAnalysis
9+
private import semmle.code.cpp.ir.internal.IntegerConstant
10+
11+
private class AliasPropertyProvider extends IRPropertyProvider {
12+
override string getOperandProperty(Operand operand, string key) {
13+
result = Print::getOperandProperty(operand, key)
14+
}
15+
16+
override string getInstructionProperty(Instruction instr, string key) {
17+
result = Print::getInstructionProperty(instr, key)
18+
}
19+
}

csharp/ql/src/experimental/ir/implementation/unaliased_ssa/internal/AliasAnalysis.qll

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,46 @@ predicate addressOperandAllocationAndOffset(
400400
)
401401
)
402402
}
403+
404+
/**
405+
* Predicates used only for printing annotated IR dumps. These should not be used in production
406+
* queries.
407+
*/
408+
module Print {
409+
string getOperandProperty(Operand operand, string key) {
410+
key = "alloc" and
411+
result =
412+
strictconcat(Configuration::Allocation allocation, IntValue bitOffset |
413+
addressOperandAllocationAndOffset(operand, allocation, bitOffset)
414+
|
415+
allocation.toString() + Ints::getBitOffsetString(bitOffset), ", "
416+
)
417+
or
418+
key = "prop" and
419+
result =
420+
strictconcat(Instruction destInstr, IntValue bitOffset, string value |
421+
operandIsPropagatedIncludingByCall(operand, bitOffset, destInstr) and
422+
if destInstr = operand.getUse()
423+
then value = "@" + Ints::getBitOffsetString(bitOffset) + "->result"
424+
else value = "@" + Ints::getBitOffsetString(bitOffset) + "->" + destInstr.getResultId()
425+
|
426+
value, ", "
427+
)
428+
}
429+
430+
string getInstructionProperty(Instruction instr, string key) {
431+
key = "prop" and
432+
result =
433+
strictconcat(IntValue bitOffset, Operand sourceOperand, string value |
434+
operandIsPropagatedIncludingByCall(sourceOperand, bitOffset, instr) and
435+
if instr = sourceOperand.getUse()
436+
then value = sourceOperand.getDumpId() + Ints::getBitOffsetString(bitOffset) + "->@"
437+
else
438+
value =
439+
sourceOperand.getUse().getResultId() + "." + sourceOperand.getDumpId() +
440+
Ints::getBitOffsetString(bitOffset) + "->@"
441+
|
442+
value, ", "
443+
)
444+
}
445+
}

0 commit comments

Comments
 (0)