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

Skip to content

Commit f93c2e4

Browse files
author
Dave Bartolomeo
committed
C++: Remove resultType from the IPA constructors for TInstruction
Making these part of the IPA object identity changes the failure mode for cases where we assign multiple result types to an instruction. Previously, we would just have one instruction with two result types, but now we'd have two instructions, which breaks things worse. This change goes back to how things were before, to avoid any new surprises on real-world code with invalid ASTs or IR.
1 parent 53d4a8e commit f93c2e4

6 files changed

Lines changed: 112 additions & 128 deletions

File tree

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

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ private module Cached {
1919
class TStageInstruction =
2020
TRawInstruction or TPhiInstruction or TChiInstruction or TUnreachedInstruction;
2121

22-
private TRawInstruction rawInstruction(
23-
IRFunctionBase irFunc, Opcode opcode, Language::AST ast, Language::LanguageType resultType
24-
) {
25-
result = TRawInstruction(irFunc, opcode, ast, resultType, _, _) and
22+
private TRawInstruction rawInstruction(IRFunctionBase irFunc, Opcode opcode, Language::AST ast) {
23+
result = TRawInstruction(irFunc, opcode, ast, _, _) and
2624
result instanceof OldInstruction
2725
}
2826

@@ -246,15 +244,15 @@ private module Cached {
246244

247245
cached
248246
Language::AST getInstructionAST(TStageInstruction instr) {
249-
instr = rawInstruction(_, _, result, _)
247+
instr = rawInstruction(_, _, result)
250248
or
251249
exists(RawIR::Instruction blockStartInstr |
252-
instr = phiInstruction(_, _, blockStartInstr, _) and
250+
instr = phiInstruction(_, blockStartInstr, _) and
253251
result = blockStartInstr.getAST()
254252
)
255253
or
256254
exists(RawIR::Instruction primaryInstr |
257-
instr = chiInstruction(_, _, primaryInstr) and
255+
instr = chiInstruction(_, primaryInstr) and
258256
result = primaryInstr.getAST()
259257
)
260258
or
@@ -265,33 +263,40 @@ private module Cached {
265263

266264
cached
267265
Language::LanguageType getInstructionResultType(TStageInstruction instr) {
268-
instr = rawInstruction(_, _, _, result)
266+
result = instr.(RawIR::Instruction).getResultLanguageType()
269267
or
270-
instr = phiInstruction(_, result, _, _)
268+
exists(Alias::MemoryLocation defLocation |
269+
instr = phiInstruction(_, _, defLocation) and
270+
result = defLocation.getType()
271+
)
271272
or
272-
instr = chiInstruction(_, result, _)
273+
exists(Instruction primaryInstr, Alias::VirtualVariable vvar |
274+
instr = chiInstruction(_, primaryInstr) and
275+
hasChiNode(vvar, primaryInstr) and
276+
result = vvar.getType()
277+
)
273278
or
274279
instr = unreachedInstruction(_) and result = Language::getVoidType()
275280
}
276281

277282
cached
278283
Opcode getInstructionOpcode(TStageInstruction instr) {
279-
instr = rawInstruction(_, result, _, _)
284+
instr = rawInstruction(_, result, _)
280285
or
281-
instr = phiInstruction(_, _, _, _) and result instanceof Opcode::Phi
286+
instr = phiInstruction(_, _, _) and result instanceof Opcode::Phi
282287
or
283-
instr = chiInstruction(_, _, _) and result instanceof Opcode::Chi
288+
instr = chiInstruction(_, _) and result instanceof Opcode::Chi
284289
or
285290
instr = unreachedInstruction(_) and result instanceof Opcode::Unreached
286291
}
287292

288293
cached
289294
IRFunctionBase getInstructionEnclosingIRFunction(TStageInstruction instr) {
290-
instr = rawInstruction(result, _, _, _)
295+
instr = rawInstruction(result, _, _)
291296
or
292-
instr = phiInstruction(result, _, _, _)
297+
instr = phiInstruction(result, _, _)
293298
or
294-
instr = chiInstruction(result, _, _)
299+
instr = chiInstruction(result, _)
295300
or
296301
instr = unreachedInstruction(result)
297302
}
@@ -313,11 +318,11 @@ private module Cached {
313318
private Instruction getNewInstruction(OldInstruction instr) { getOldInstruction(result) = instr }
314319

315320
private ChiInstruction getChi(OldInstruction primaryInstr) {
316-
result = chiInstruction(_, _, primaryInstr)
321+
result = chiInstruction(_, primaryInstr)
317322
}
318323

319324
private PhiInstruction getPhi(OldBlock defBlock, Alias::MemoryLocation defLocation) {
320-
result = phiInstruction(_, _, defBlock.getFirstInstruction(), defLocation)
325+
result = phiInstruction(_, defBlock.getFirstInstruction(), defLocation)
321326
}
322327

323328
/**
@@ -883,26 +888,19 @@ module SSA {
883888

884889
cached
885890
predicate hasPhiInstruction(
886-
IRFunction irFunc, Language::LanguageType resultType, OldInstruction blockStartInstr,
887-
Alias::MemoryLocation defLocation
891+
IRFunction irFunc, OldInstruction blockStartInstr, Alias::MemoryLocation defLocation
888892
) {
889893
exists(OldBlock oldBlock |
890894
definitionHasPhiNode(defLocation, oldBlock) and
891895
irFunc = oldBlock.getEnclosingIRFunction() and
892-
blockStartInstr = oldBlock.getFirstInstruction() and
893-
resultType = defLocation.getType()
896+
blockStartInstr = oldBlock.getFirstInstruction()
894897
)
895898
}
896899

897900
cached
898-
predicate hasChiInstruction(
899-
IRFunctionBase irFunc, Language::LanguageType resultType, OldInstruction primaryInstruction
900-
) {
901-
exists(Alias::VirtualVariable vvar |
902-
hasChiNode(vvar, primaryInstruction) and
903-
irFunc = primaryInstruction.getEnclosingIRFunction() and
904-
resultType = vvar.getType()
905-
)
901+
predicate hasChiInstruction(IRFunctionBase irFunc, OldInstruction primaryInstruction) {
902+
hasChiNode(_, primaryInstruction) and
903+
irFunc = primaryInstruction.getEnclosingIRFunction()
906904
}
907905

908906
cached

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,29 @@ private import Imports::Opcode
1414
*/
1515
newtype TInstruction =
1616
TRawInstruction(
17-
IRFunctionBase irFunc, Opcode opcode, Language::AST ast, Language::LanguageType resultType,
17+
IRFunctionBase irFunc, Opcode opcode, Language::AST ast,
1818
IRConstruction::Raw::InstructionTag1 tag1, IRConstruction::Raw::InstructionTag2 tag2
1919
) {
20-
IRConstruction::Raw::hasInstruction(irFunc.getFunction(), opcode, ast, resultType, tag1, tag2)
20+
IRConstruction::Raw::hasInstruction(irFunc.getFunction(), opcode, ast, tag1, tag2)
2121
} or
2222
TUnaliasedSSAPhiInstruction(
23-
IRFunctionBase irFunc, Language::LanguageType resultType, TRawInstruction blockStartInstr,
23+
IRFunctionBase irFunc, TRawInstruction blockStartInstr,
2424
UnaliasedSSA::SSA::MemoryLocation memoryLocation
2525
) {
26-
UnaliasedSSA::SSA::hasPhiInstruction(irFunc, resultType, blockStartInstr, memoryLocation)
27-
} or
28-
TUnaliasedSSAChiInstruction(
29-
IRFunctionBase irFunc, Language::LanguageType resultType, TRawInstruction primaryInstruction
30-
) {
31-
none()
26+
UnaliasedSSA::SSA::hasPhiInstruction(irFunc, blockStartInstr, memoryLocation)
3227
} or
28+
TUnaliasedSSAChiInstruction(IRFunctionBase irFunc, TRawInstruction primaryInstruction) { none() } or
3329
TUnaliasedSSAUnreachedInstruction(IRFunctionBase irFunc) {
3430
UnaliasedSSA::SSA::hasUnreachedInstruction(irFunc)
3531
} or
3632
TAliasedSSAPhiInstruction(
37-
IRFunctionBase irFunc, Language::LanguageType resultType, TRawInstruction blockStartInstr,
33+
IRFunctionBase irFunc, TRawInstruction blockStartInstr,
3834
AliasedSSA::SSA::MemoryLocation memoryLocation
3935
) {
40-
AliasedSSA::SSA::hasPhiInstruction(irFunc, resultType, blockStartInstr, memoryLocation)
36+
AliasedSSA::SSA::hasPhiInstruction(irFunc, blockStartInstr, memoryLocation)
4137
} or
42-
TAliasedSSAChiInstruction(
43-
IRFunctionBase irFunc, Language::LanguageType resultType, TRawInstruction primaryInstruction
44-
) {
45-
AliasedSSA::SSA::hasChiInstruction(irFunc, resultType, primaryInstruction)
38+
TAliasedSSAChiInstruction(IRFunctionBase irFunc, TRawInstruction primaryInstruction) {
39+
AliasedSSA::SSA::hasChiInstruction(irFunc, primaryInstruction)
4640
} or
4741
TAliasedSSAUnreachedInstruction(IRFunctionBase irFunc) {
4842
AliasedSSA::SSA::hasUnreachedInstruction(irFunc)
@@ -58,18 +52,16 @@ module UnaliasedSSAInstructions {
5852
class TPhiInstruction = TUnaliasedSSAPhiInstruction;
5953

6054
TPhiInstruction phiInstruction(
61-
IRFunctionBase irFunc, Language::LanguageType resultType, TRawInstruction blockStartInstr,
55+
IRFunctionBase irFunc, TRawInstruction blockStartInstr,
6256
UnaliasedSSA::SSA::MemoryLocation memoryLocation
6357
) {
64-
result = TUnaliasedSSAPhiInstruction(irFunc, resultType, blockStartInstr, memoryLocation)
58+
result = TUnaliasedSSAPhiInstruction(irFunc, blockStartInstr, memoryLocation)
6559
}
6660

6761
class TChiInstruction = TUnaliasedSSAChiInstruction;
6862

69-
TChiInstruction chiInstruction(
70-
IRFunctionBase irFunc, Language::LanguageType resultType, TRawInstruction primaryInstruction
71-
) {
72-
result = TUnaliasedSSAChiInstruction(irFunc, resultType, primaryInstruction)
63+
TChiInstruction chiInstruction(IRFunctionBase irFunc, TRawInstruction primaryInstruction) {
64+
result = TUnaliasedSSAChiInstruction(irFunc, primaryInstruction)
7365
}
7466

7567
class TUnreachedInstruction = TUnaliasedSSAUnreachedInstruction;
@@ -89,18 +81,16 @@ module AliasedSSAInstructions {
8981
class TPhiInstruction = TAliasedSSAPhiInstruction;
9082

9183
TPhiInstruction phiInstruction(
92-
IRFunctionBase irFunc, Language::LanguageType resultType, TRawInstruction blockStartInstr,
84+
IRFunctionBase irFunc, TRawInstruction blockStartInstr,
9385
AliasedSSA::SSA::MemoryLocation memoryLocation
9486
) {
95-
result = TAliasedSSAPhiInstruction(irFunc, resultType, blockStartInstr, memoryLocation)
87+
result = TAliasedSSAPhiInstruction(irFunc, blockStartInstr, memoryLocation)
9688
}
9789

9890
class TChiInstruction = TAliasedSSAChiInstruction;
9991

100-
TChiInstruction chiInstruction(
101-
IRFunctionBase irFunc, Language::LanguageType resultType, TRawInstruction primaryInstruction
102-
) {
103-
result = TAliasedSSAChiInstruction(irFunc, resultType, primaryInstruction)
92+
TChiInstruction chiInstruction(IRFunctionBase irFunc, TRawInstruction primaryInstruction) {
93+
result = TAliasedSSAChiInstruction(irFunc, primaryInstruction)
10494
}
10595

10696
class TUnreachedInstruction = TAliasedSSAUnreachedInstruction;

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ private import TranslatedStmt
1515
private import TranslatedFunction
1616

1717
TranslatedElement getInstructionTranslatedElement(Instruction instruction) {
18-
instruction = TRawInstruction(_, _, _, _, result, _)
18+
instruction = TRawInstruction(_, _, _, result, _)
1919
}
2020

2121
InstructionTag getInstructionTag(Instruction instruction) {
22-
instruction = TRawInstruction(_, _, _, _, _, result)
22+
instruction = TRawInstruction(_, _, _, _, result)
2323
}
2424

2525
pragma[noinline]
@@ -45,10 +45,9 @@ module Raw {
4545

4646
cached
4747
predicate hasInstruction(
48-
Function func, Opcode opcode, Element ast, CppType resultType, TranslatedElement element,
49-
InstructionTag tag
48+
Function func, Opcode opcode, Element ast, TranslatedElement element, InstructionTag tag
5049
) {
51-
element.hasInstruction(opcode, tag, resultType) and
50+
element.hasInstruction(opcode, tag, _) and
5251
ast = element.getAST() and
5352
func = element.getFunction()
5453
}
@@ -371,22 +370,25 @@ private module Cached {
371370

372371
cached
373372
Locatable getInstructionAST(TStageInstruction instr) {
374-
instr = TRawInstruction(_, _, result, _, _, _)
373+
instr = TRawInstruction(_, _, result, _, _)
375374
}
376375

377376
cached
378377
CppType getInstructionResultType(TStageInstruction instr) {
379-
instr = TRawInstruction(_, _, _, result, _, _)
378+
exists(TranslatedElement element, InstructionTag tag |
379+
instructionOrigin(instr, element, tag) and
380+
element.hasInstruction(_, tag, result)
381+
)
380382
}
381383

382384
cached
383385
Opcode getInstructionOpcode(TStageInstruction instr) {
384-
instr = TRawInstruction(_, result, _, _, _, _)
386+
instr = TRawInstruction(_, result, _, _, _)
385387
}
386388

387389
cached
388390
IRFunctionBase getInstructionEnclosingIRFunction(TStageInstruction instr) {
389-
instr = TRawInstruction(result, _, _, _, _, _)
391+
instr = TRawInstruction(result, _, _, _, _)
390392
}
391393

392394
cached

0 commit comments

Comments
 (0)