Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 927f935

Browse files
Robert Marshdave-bartolomeo
authored andcommitted
C++: hook ChiInstructions into the operand graph
1 parent a33b591 commit 927f935

11 files changed

Lines changed: 1356 additions & 1173 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,19 @@ class PhiMemoryAccess extends MemoryAccessKind, TPhiMemoryAccess {
8484
* The operand is a ChiOld operand, which accesses the same memory as its
8585
* definition.
8686
*/
87-
class ChiOldMemoryAccess extends MemoryAccessKind, TChiOldMemoryAccess {
87+
class ChiTotalMemoryAccess extends MemoryAccessKind, TChiOldMemoryAccess {
8888
override string toString() {
89-
result = "chi(old)"
89+
result = "chi(total)"
9090
}
9191
}
9292

9393
/**
9494
* The operand is a ChiUpdate operand, which accesses the same memory as its
9595
* definition.
9696
*/
97-
class ChiUpdateMemoryAccess extends MemoryAccessKind, TChiUpdateMemoryAccess {
97+
class ChiPartialMemoryAccess extends MemoryAccessKind, TChiUpdateMemoryAccess {
9898
override string toString() {
99-
result = "chi(updated)"
99+
result = "chi(partial)"
100100
}
101101
}
102102
/**

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module InstructionSanity {
3939
opcode instanceof Opcode::ThrowValue and tag instanceof ExceptionOperandTag or
4040
opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag or
4141
opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag or
42+
opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag or
43+
opcode instanceof Opcode::Chi and tag instanceof ChiPartialOperandTag or
4244
(
4345
(opcode instanceof ReadSideEffectOpcode or opcode instanceof MayWriteSideEffectOpcode) and
4446
tag instanceof SideEffectOperandTag
@@ -1346,7 +1348,7 @@ class ChiInstruction extends Instruction {
13461348
}
13471349

13481350
override final MemoryAccessKind getResultMemoryAccess() {
1349-
result instanceof ChiUpdateMemoryAccess
1351+
result instanceof ChiTotalMemoryAccess
13501352
}
13511353
}
13521354

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ class UnknownVirtualVariable extends VirtualVariable, TUnknownVirtualVariable {
8585
private newtype TMemoryAccess =
8686
TVariableMemoryAccess(VirtualIRVariable vvar, IntValue offset, IntValue size) {
8787
exists(Instruction instr |
88-
instr.getResultMemoryAccess() instanceof IndirectMemoryAccess and
88+
exists(MemoryAccessKind mak | instr.getResultMemoryAccess() = mak and not mak instanceof PhiMemoryAccess) and
8989
resultPointsTo(instr.getAnOperand().(AddressOperand).getDefinitionInstruction(), vvar.getIRVariable(), offset) and
90-
instr.getResultSize() = size
90+
if exists(instr.getResultSize())
91+
then instr.getResultSize() = size
92+
else size = Ints::unknown()
9193
)
9294
}
9395
or
@@ -107,7 +109,7 @@ class MemoryAccess extends TMemoryAccess {
107109
VirtualVariable getVirtualVariable() {
108110
none()
109111
}
110-
112+
111113
predicate isPartialMemoryAccess() {
112114
none()
113115
}
@@ -148,6 +150,10 @@ class VariableMemoryAccess extends TVariableMemoryAccess, MemoryAccess {
148150
class UnknownMemoryAccess extends TUnknownMemoryAccess, MemoryAccess {
149151
UnknownVirtualVariable vvar;
150152

153+
UnknownMemoryAccess() {
154+
this = TUnknownMemoryAccess(vvar)
155+
}
156+
151157
final override string toString() {
152158
result = vvar.toString()
153159
}
@@ -201,7 +207,7 @@ Overlap getOverlap(MemoryAccess def, MemoryAccess use) {
201207
}
202208

203209
MemoryAccess getResultMemoryAccess(Instruction instr) {
204-
instr.getResultMemoryAccess() instanceof IndirectMemoryAccess and
210+
exists(instr.getResultMemoryAccess()) and
205211
if exists(IRVariable var, IntValue i |
206212
resultPointsTo(instr.getAnOperand().(AddressOperand).getDefinitionInstruction(), var, i)
207213
)
@@ -214,13 +220,13 @@ MemoryAccess getResultMemoryAccess(Instruction instr) {
214220
}
215221

216222
MemoryAccess getOperandMemoryAccess(Operand operand) {
217-
operand.getMemoryAccess() instanceof IndirectMemoryAccess and
223+
exists(operand.getMemoryAccess()) and
218224
if exists(IRVariable var, IntValue i |
219225
resultPointsTo(operand.getAddressOperand().getDefinitionInstruction(), var, i)
220226
)
221227
then exists(IRVariable var, IntValue i |
222228
resultPointsTo(operand.getAddressOperand().getDefinitionInstruction(), var, i) and
223-
result = getVariableMemoryAccess(var, i, operand.getAddressOperand().getDefinitionInstruction().getResultSize())
229+
result = getVariableMemoryAccess(var, i, operand.getDefinitionInstruction().getResultSize())
224230
)
225231
else
226232
result = TUnknownMemoryAccess(TUnknownVirtualVariable(operand.getInstruction().getFunctionIR()))

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ cached private module Cached {
149149
hasUseAtRank(vvar, useBlock, useRank, oldInstruction) and
150150
definitionReachesUse(vvar, defBlock, defRank, useBlock, useRank) and
151151
if defIndex >= 0 then
152-
result = getNewInstruction(defBlock.getInstruction(defIndex))
152+
result = getNewFinalInstruction(defBlock.getInstruction(defIndex))
153153
else
154-
result = getPhiInstruction(instruction.getFunction(), defBlock, vvar)
154+
result = getPhiInstruction(instruction.getFunction(), defBlock, vvar)
155155
)
156-
)
156+
)
157157
else (
158158
result = instruction.getFunctionIR().getUnmodeledDefinitionInstruction()
159159
)
@@ -168,9 +168,13 @@ cached private module Cached {
168168
result = getNewInstruction(oldDefinition)
169169
)
170170
)
171-
else
171+
else
172172
result = getNewInstruction(oldOperand.getDefinitionInstruction())
173-
)
173+
) or
174+
instruction.getTag() = ChiTag(getOldInstruction(result)) and
175+
tag instanceof ChiPartialOperandTag
176+
or
177+
result = getChiInstructionTotalOperand(instruction.(ChiInstruction), tag.(ChiTotalOperandTag))
174178
}
175179

176180
cached Instruction getPhiInstructionOperandDefinition(PhiInstruction instr,
@@ -184,12 +188,24 @@ cached private module Cached {
184188
hasDefinitionAtRank(vvar, defBlock, defRank, defIndex) and
185189
definitionReachesEndOfBlock(vvar, defBlock, defRank, predBlock) and
186190
if defIndex >= 0 then
187-
result = getNewInstruction(defBlock.getInstruction(defIndex))
191+
result = getNewFinalInstruction(defBlock.getInstruction(defIndex))
188192
else
189193
result = getPhiInstruction(instr.getFunction(), defBlock, vvar)
190194
)
191195
}
192196

197+
cached Instruction getChiInstructionTotalOperand(ChiInstruction chiInstr, ChiTotalOperandTag tag) {
198+
exists(Alias::VirtualVariable vvar, OldIR::Instruction oldInstr, OldIR::IRBlock defBlock,
199+
int defRank, int defIndex, OldIR::IRBlock useBlock, int useRank |
200+
ChiTag(oldInstr) = chiInstr.getTag() and
201+
vvar = Alias::getResultMemoryAccess(oldInstr).getVirtualVariable() and
202+
hasDefinitionAtRank(vvar, defBlock, defRank, defIndex) and
203+
hasUseAtRank(vvar, useBlock, useRank, oldInstr) and
204+
definitionReachesUse(vvar, defBlock, defRank, useBlock, useRank) and
205+
result = getNewFinalInstruction(defBlock.getInstruction(defIndex))
206+
)
207+
}
208+
193209
cached Instruction getPhiInstructionBlockStart(PhiInstruction instr) {
194210
exists(OldIR::IRBlock oldBlock |
195211
instr.getTag() = PhiTag(_, oldBlock) and
@@ -300,7 +316,12 @@ cached private module Cached {
300316
private predicate hasUse(Alias::VirtualVariable vvar,
301317
OldIR::Instruction use, OldIR::IRBlock block, int index) {
302318
exists(Alias::MemoryAccess access |
303-
access = Alias::getOperandMemoryAccess(use.getAnOperand()) and
319+
(
320+
access = Alias::getOperandMemoryAccess(use.getAnOperand())
321+
or
322+
access = Alias::getResultMemoryAccess(use) and
323+
access.isPartialMemoryAccess()
324+
) and
304325
block.getInstruction(index) = use and
305326
vvar = access.getVirtualVariable()
306327
)
@@ -438,7 +459,8 @@ cached private module Cached {
438459
ma = Alias::getResultMemoryAccess(def) and
439460
ma.isPartialMemoryAccess() and
440461
ma.getVirtualVariable() = vvar
441-
)
462+
) and
463+
not def instanceof OldIR::UnmodeledDefinitionInstruction
442464
}
443465
}
444466

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module InstructionSanity {
3939
opcode instanceof Opcode::ThrowValue and tag instanceof ExceptionOperandTag or
4040
opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag or
4141
opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag or
42+
opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag or
43+
opcode instanceof Opcode::Chi and tag instanceof ChiPartialOperandTag or
4244
(
4345
(opcode instanceof ReadSideEffectOpcode or opcode instanceof MayWriteSideEffectOpcode) and
4446
tag instanceof SideEffectOperandTag
@@ -1346,7 +1348,7 @@ class ChiInstruction extends Instruction {
13461348
}
13471349

13481350
override final MemoryAccessKind getResultMemoryAccess() {
1349-
result instanceof ChiUpdateMemoryAccess
1351+
result instanceof ChiTotalMemoryAccess
13501352
}
13511353
}
13521354

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module InstructionSanity {
3939
opcode instanceof Opcode::ThrowValue and tag instanceof ExceptionOperandTag or
4040
opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag or
4141
opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag or
42+
opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag or
43+
opcode instanceof Opcode::Chi and tag instanceof ChiPartialOperandTag or
4244
(
4345
(opcode instanceof ReadSideEffectOpcode or opcode instanceof MayWriteSideEffectOpcode) and
4446
tag instanceof SideEffectOperandTag
@@ -1346,7 +1348,7 @@ class ChiInstruction extends Instruction {
13461348
}
13471349

13481350
override final MemoryAccessKind getResultMemoryAccess() {
1349-
result instanceof ChiUpdateMemoryAccess
1351+
result instanceof ChiTotalMemoryAccess
13501352
}
13511353
}
13521354

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ cached private module Cached {
149149
hasUseAtRank(vvar, useBlock, useRank, oldInstruction) and
150150
definitionReachesUse(vvar, defBlock, defRank, useBlock, useRank) and
151151
if defIndex >= 0 then
152-
result = getNewInstruction(defBlock.getInstruction(defIndex))
152+
result = getNewFinalInstruction(defBlock.getInstruction(defIndex))
153153
else
154-
result = getPhiInstruction(instruction.getFunction(), defBlock, vvar)
154+
result = getPhiInstruction(instruction.getFunction(), defBlock, vvar)
155155
)
156156
)
157157
else (
@@ -170,7 +170,11 @@ cached private module Cached {
170170
)
171171
else
172172
result = getNewInstruction(oldOperand.getDefinitionInstruction())
173-
)
173+
) or
174+
instruction.getTag() = ChiTag(getOldInstruction(result)) and
175+
tag instanceof ChiPartialOperandTag
176+
or
177+
result = getChiInstructionTotalOperand(instruction.(ChiInstruction), tag.(ChiTotalOperandTag))
174178
}
175179

176180
cached Instruction getPhiInstructionOperandDefinition(PhiInstruction instr,
@@ -184,12 +188,24 @@ cached private module Cached {
184188
hasDefinitionAtRank(vvar, defBlock, defRank, defIndex) and
185189
definitionReachesEndOfBlock(vvar, defBlock, defRank, predBlock) and
186190
if defIndex >= 0 then
187-
result = getNewInstruction(defBlock.getInstruction(defIndex))
191+
result = getNewFinalInstruction(defBlock.getInstruction(defIndex))
188192
else
189193
result = getPhiInstruction(instr.getFunction(), defBlock, vvar)
190194
)
191195
}
192196

197+
cached Instruction getChiInstructionTotalOperand(ChiInstruction chiInstr, ChiTotalOperandTag tag) {
198+
exists(Alias::VirtualVariable vvar, OldIR::Instruction oldInstr, OldIR::IRBlock defBlock,
199+
int defRank, int defIndex, OldIR::IRBlock useBlock, int useRank |
200+
ChiTag(oldInstr) = chiInstr.getTag() and
201+
vvar = Alias::getResultMemoryAccess(oldInstr).getVirtualVariable() and
202+
hasDefinitionAtRank(vvar, defBlock, defRank, defIndex) and
203+
hasUseAtRank(vvar, useBlock, useRank, oldInstr) and
204+
definitionReachesUse(vvar, defBlock, defRank, useBlock, useRank) and
205+
result = getNewFinalInstruction(defBlock.getInstruction(defIndex))
206+
)
207+
}
208+
193209
cached Instruction getPhiInstructionBlockStart(PhiInstruction instr) {
194210
exists(OldIR::IRBlock oldBlock |
195211
instr.getTag() = PhiTag(_, oldBlock) and
@@ -300,7 +316,12 @@ cached private module Cached {
300316
private predicate hasUse(Alias::VirtualVariable vvar,
301317
OldIR::Instruction use, OldIR::IRBlock block, int index) {
302318
exists(Alias::MemoryAccess access |
303-
access = Alias::getOperandMemoryAccess(use.getAnOperand()) and
319+
(
320+
access = Alias::getOperandMemoryAccess(use.getAnOperand())
321+
or
322+
access = Alias::getResultMemoryAccess(use) and
323+
access.isPartialMemoryAccess()
324+
) and
304325
block.getInstruction(index) = use and
305326
vvar = access.getVirtualVariable()
306327
)
@@ -438,7 +459,8 @@ cached private module Cached {
438459
ma = Alias::getResultMemoryAccess(def) and
439460
ma.isPartialMemoryAccess() and
440461
ma.getVirtualVariable() = vvar
441-
)
462+
) and
463+
not def instanceof OldIR::UnmodeledDefinitionInstruction
442464
}
443465
}
444466

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ private newtype TOperandTag =
2828
exists(op.getChild(argIndex))
2929
)
3030
} or
31-
TChiOldOperand() or
32-
TChiUpdateOperand()
31+
TChiTotalOperand() or
32+
TChiPartialOperand()
3333

3434
/**
3535
* Identifies the kind of operand on an instruction. Each `Instruction` has at
@@ -315,19 +315,19 @@ PositionalArgumentOperandTag positionalArgumentOperand(int argIndex) {
315315
result = TPositionalArgumentOperand(argIndex)
316316
}
317317

318-
class ChiOldOperand extends OperandTag, TChiOldOperand {
318+
class ChiTotalOperandTag extends OperandTag, TChiTotalOperand {
319319
override final string toString() {
320-
result = "ChiOld"
320+
result = "ChiTotal"
321321
}
322322

323323
override final int getSortOrder() {
324324
result = 14
325325
}
326326
}
327327

328-
class ChiUpdateOperand extends OperandTag, TChiUpdateOperand {
328+
class ChiPartialOperandTag extends OperandTag, TChiPartialOperand {
329329
override final string toString() {
330-
result = "ChiUpdate"
330+
result = "ChiPartial"
331331
}
332332

333333
override final int getSortOrder() {
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
| constant_func.cpp:1:5:1:18 | IR: ReturnConstant | 7 |
2-
| constant_func.cpp:5:5:5:21 | IR: ReturnConstantPhi | 7 |
3-
| constant_func.cpp:25:5:25:25 | IR: ReturnConstantPhiLoop | 7 |

0 commit comments

Comments
 (0)