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

Skip to content

Commit 5e946cc

Browse files
author
Robert Marsh
committed
C++: add param read side effects to IR exit blocks
1 parent e8dd022 commit 5e946cc

9 files changed

Lines changed: 441 additions & 238 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private newtype TOpcode =
99
TExitFunction() or
1010
TReturnValue() or
1111
TReturnVoid() or
12+
TReturnIndirection() or
1213
TCopyValue() or
1314
TLoad() or
1415
TStore() or
@@ -202,6 +203,10 @@ module Opcode {
202203
final override string toString() { result = "ReturnVoid" }
203204
}
204205

206+
class ReturnIndirection extends MemoryAccessOpcode, TReturnIndirection {
207+
final override string toString() { result = "ReturnIndirection" }
208+
}
209+
205210
class CopyValue extends UnaryOpcode, CopyOpcode, TCopyValue {
206211
final override string toString() { result = "CopyValue" }
207212
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ module InstructionSanity {
4949
(
5050
opcode instanceof ReadSideEffectOpcode or
5151
opcode instanceof Opcode::InlineAsm or
52-
opcode instanceof Opcode::CallSideEffect
52+
opcode instanceof Opcode::CallSideEffect or
53+
opcode instanceof Opcode::ReturnIndirection
5354
) and
5455
tag instanceof SideEffectOperandTag
5556
)
@@ -743,6 +744,18 @@ class ReturnValueInstruction extends ReturnInstruction {
743744
final Instruction getReturnValue() { result = getReturnValueOperand().getDef() }
744745
}
745746

747+
class ReturnIndirectionInstruction extends Instruction {
748+
ReturnIndirectionInstruction() { getOpcode() instanceof Opcode::ReturnIndirection }
749+
750+
final SideEffectOperand getSideEffectOperand() { result = getAnOperand() }
751+
752+
final Instruction getSideEffect() { result = getSideEffectOperand().getDef() }
753+
754+
final AddressOperand getSourceAddressOperand() { result = getAnOperand() }
755+
756+
final Instruction getSourceAddress() { result = getSourceAddressOperand().getDef() }
757+
}
758+
746759
class CopyInstruction extends Instruction {
747760
CopyInstruction() { getOpcode() instanceof CopyOpcode }
748761

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ module InstructionSanity {
4949
(
5050
opcode instanceof ReadSideEffectOpcode or
5151
opcode instanceof Opcode::InlineAsm or
52-
opcode instanceof Opcode::CallSideEffect
52+
opcode instanceof Opcode::CallSideEffect or
53+
opcode instanceof Opcode::ReturnIndirection
5354
) and
5455
tag instanceof SideEffectOperandTag
5556
)
@@ -743,6 +744,18 @@ class ReturnValueInstruction extends ReturnInstruction {
743744
final Instruction getReturnValue() { result = getReturnValueOperand().getDef() }
744745
}
745746

747+
class ReturnIndirectionInstruction extends Instruction {
748+
ReturnIndirectionInstruction() { getOpcode() instanceof Opcode::ReturnIndirection }
749+
750+
final SideEffectOperand getSideEffectOperand() { result = getAnOperand() }
751+
752+
final Instruction getSideEffect() { result = getSideEffectOperand().getDef() }
753+
754+
final AddressOperand getSourceAddressOperand() { result = getAnOperand() }
755+
756+
final Instruction getSourceAddress() { result = getSourceAddressOperand().getDef() }
757+
}
758+
746759
class CopyInstruction extends Instruction {
747760
CopyInstruction() { getOpcode() instanceof CopyOpcode }
748761

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,16 @@ newtype TTranslatedElement =
346346
translateFunction(func)
347347
)
348348
} or
349+
TTranslatedReadEffects(Function func) { translateFunction(func) } or
350+
// The read side effects in a function's return block
351+
TTranslatedReadEffect(Parameter param) {
352+
translateFunction(param.getFunction()) and
353+
exists(Type t | t = param.getUnspecifiedType() |
354+
t instanceof ArrayType or
355+
t instanceof PointerType or
356+
t instanceof ReferenceType
357+
)
358+
} or
349359
// A local declaration
350360
TTranslatedDeclarationEntry(DeclarationEntry entry) {
351361
exists(DeclStmt declStmt |

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

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
3434
final override Function getFunction() { result = func }
3535

3636
final override TranslatedElement getChild(int id) {
37+
id = -4 and result = getReadEffects()
38+
or
3739
id = -3 and result = getConstructorInitList()
3840
or
3941
id = -2 and result = getBody()
@@ -53,6 +55,8 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
5355

5456
final private TranslatedStmt getBody() { result = getTranslatedStmt(func.getEntryPoint()) }
5557

58+
final private TranslatedReadEffects getReadEffects() { result = getTranslatedReadEffects(func) }
59+
5660
final private TranslatedParameter getParameter(int index) {
5761
result = getTranslatedParameter(func.getParameter(index))
5862
}
@@ -113,8 +117,11 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
113117
child = getBody() and
114118
result = getReturnSuccessorInstruction()
115119
or
120+
child = getDestructorDestructionList() and
121+
result = getReadEffects().getFirstInstruction()
122+
or
116123
(
117-
child = getDestructorDestructionList() and
124+
child = getReadEffects() and
118125
if getReturnType() instanceof VoidType
119126
then result = getInstruction(ReturnTag())
120127
else result = getInstruction(ReturnValueAddressTag())
@@ -531,3 +538,101 @@ class TranslatedDestructorDestructionList extends TranslatedElement,
531538
)
532539
}
533540
}
541+
542+
TranslatedReadEffects getTranslatedReadEffects(Function func) { result.getAST() = func }
543+
544+
class TranslatedReadEffects extends TranslatedElement, TTranslatedReadEffects {
545+
Function func;
546+
547+
TranslatedReadEffects() { this = TTranslatedReadEffects(func) }
548+
549+
override Locatable getAST() { result = func }
550+
551+
override Function getFunction() { result = func }
552+
553+
override string toString() { result = "read effects: " + func.toString() }
554+
555+
override TranslatedElement getChild(int id) {
556+
result = getTranslatedReadEffect(func.getParameter(id))
557+
}
558+
559+
override Instruction getFirstInstruction() {
560+
if exists(getAChild())
561+
then
562+
result = min(TranslatedReadEffect child, int id | child = getChild(id) | child order by id)
563+
.getFirstInstruction()
564+
else result = getParent().getChildSuccessor(this)
565+
}
566+
567+
override Instruction getChildSuccessor(TranslatedElement child) {
568+
exists(int id | child = getChild(id) |
569+
if exists(TranslatedReadEffect child2, int id2 | id2 > id and child2 = getChild(id2))
570+
then
571+
result = min(TranslatedReadEffect child2, int id2 |
572+
child2 = getChild(id2) and id2 > id
573+
|
574+
child2 order by id2
575+
).getFirstInstruction()
576+
else result = getParent().getChildSuccessor(this)
577+
)
578+
}
579+
580+
override predicate hasInstruction(
581+
Opcode opcode, InstructionTag tag, Type resultType, boolean isGLValue
582+
) {
583+
none()
584+
}
585+
586+
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
587+
}
588+
589+
private TranslatedReadEffect getTranslatedReadEffect(Parameter param) { result.getAST() = param }
590+
591+
class TranslatedReadEffect extends TranslatedElement, TTranslatedReadEffect {
592+
Parameter param;
593+
594+
TranslatedReadEffect() { this = TTranslatedReadEffect(param) }
595+
596+
override Locatable getAST() { result = param }
597+
598+
override string toString() { result = "read effect: " + param.toString() }
599+
600+
override TranslatedElement getChild(int id) { none() }
601+
602+
override Instruction getChildSuccessor(TranslatedElement child) { none() }
603+
604+
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind edge) {
605+
tag = OnlyInstructionTag() and
606+
edge = gotoEdge() and
607+
result = getParent().getChildSuccessor(this)
608+
}
609+
610+
override Instruction getFirstInstruction() { result = getInstruction(OnlyInstructionTag()) }
611+
612+
override Function getFunction() { result = param.getFunction() }
613+
614+
override predicate hasInstruction(
615+
Opcode opcode, InstructionTag tag, Type resultType, boolean isGLValue
616+
) {
617+
opcode instanceof Opcode::ReturnIndirection and
618+
tag = OnlyInstructionTag() and
619+
resultType instanceof VoidType and
620+
isGLValue = false
621+
}
622+
623+
final override Instruction getInstructionOperand(InstructionTag tag, OperandTag operandTag) {
624+
tag = OnlyInstructionTag() and
625+
operandTag = sideEffectOperand() and
626+
result = getTranslatedFunction(getFunction()).getUnmodeledDefinitionInstruction()
627+
or
628+
tag = OnlyInstructionTag() and
629+
operandTag = addressOperand() and
630+
result = getTranslatedParameter(param).getInstruction(InitializerIndirectAddressTag())
631+
}
632+
633+
final override Type getInstructionOperandType(InstructionTag tag, TypedOperandTag operandTag) {
634+
tag = OnlyInstructionTag() and
635+
operandTag = sideEffectOperand() and
636+
result instanceof UnknownType
637+
}
638+
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ module InstructionSanity {
4949
(
5050
opcode instanceof ReadSideEffectOpcode or
5151
opcode instanceof Opcode::InlineAsm or
52-
opcode instanceof Opcode::CallSideEffect
52+
opcode instanceof Opcode::CallSideEffect or
53+
opcode instanceof Opcode::ReturnIndirection
5354
) and
5455
tag instanceof SideEffectOperandTag
5556
)
@@ -743,6 +744,18 @@ class ReturnValueInstruction extends ReturnInstruction {
743744
final Instruction getReturnValue() { result = getReturnValueOperand().getDef() }
744745
}
745746

747+
class ReturnIndirectionInstruction extends Instruction {
748+
ReturnIndirectionInstruction() { getOpcode() instanceof Opcode::ReturnIndirection }
749+
750+
final SideEffectOperand getSideEffectOperand() { result = getAnOperand() }
751+
752+
final Instruction getSideEffect() { result = getSideEffectOperand().getDef() }
753+
754+
final AddressOperand getSourceAddressOperand() { result = getAnOperand() }
755+
756+
final Instruction getSourceAddress() { result = getSourceAddressOperand().getDef() }
757+
}
758+
746759
class CopyInstruction extends Instruction {
747760
CopyInstruction() { getOpcode() instanceof CopyOpcode }
748761

0 commit comments

Comments
 (0)