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

Skip to content

Commit 6ab0eaa

Browse files
authored
Merge pull request #926 from dave-bartolomeo/dave/MemoryOperand
C++: Rationalize `RegisterOperand` vs. `MemoryOperand`
2 parents 9caa9c1 + f5121d7 commit 6ab0eaa

29 files changed

Lines changed: 678 additions & 461 deletions

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ abstract class BuiltInOpcode extends Opcode {}
105105

106106
abstract class SideEffectOpcode extends Opcode {}
107107

108+
/**
109+
* An opcode that reads a value from memory.
110+
*/
111+
abstract class OpcodeWithLoad extends MemoryAccessOpcode {}
112+
108113
/**
109114
* An opcode that reads from a set of memory locations as a side effect.
110115
*/
@@ -133,10 +138,10 @@ module Opcode {
133138
class InitializeThis extends Opcode, TInitializeThis { override final string toString() { result = "InitializeThis" } }
134139
class EnterFunction extends Opcode, TEnterFunction { override final string toString() { result = "EnterFunction" } }
135140
class ExitFunction extends Opcode, TExitFunction { override final string toString() { result = "ExitFunction" } }
136-
class ReturnValue extends ReturnOpcode, MemoryAccessOpcode, TReturnValue { override final string toString() { result = "ReturnValue" } }
141+
class ReturnValue extends ReturnOpcode, OpcodeWithLoad, TReturnValue { override final string toString() { result = "ReturnValue" } }
137142
class ReturnVoid extends ReturnOpcode, TReturnVoid { override final string toString() { result = "ReturnVoid" } }
138-
class CopyValue extends CopyOpcode, TCopyValue { override final string toString() { result = "CopyValue" } }
139-
class Load extends CopyOpcode, MemoryAccessOpcode, TLoad { override final string toString() { result = "Load" } }
143+
class CopyValue extends UnaryOpcode, CopyOpcode, TCopyValue { override final string toString() { result = "CopyValue" } }
144+
class Load extends CopyOpcode, OpcodeWithLoad, TLoad { override final string toString() { result = "Load" } }
140145
class Store extends CopyOpcode, MemoryAccessOpcode, TStore { override final string toString() { result = "Store" } }
141146
class Add extends BinaryOpcode, TAdd { override final string toString() { result = "Add" } }
142147
class Sub extends BinaryOpcode, TSub { override final string toString() { result = "Sub" } }
@@ -177,7 +182,7 @@ module Opcode {
177182
class Call extends Opcode, TCall { override final string toString() { result = "Call" } }
178183
class CatchByType extends CatchOpcode, TCatchByType { override final string toString() { result = "CatchByType" } }
179184
class CatchAny extends CatchOpcode, TCatchAny { override final string toString() { result = "CatchAny" } }
180-
class ThrowValue extends ThrowOpcode, MemoryAccessOpcode, TThrowValue { override final string toString() { result = "ThrowValue" } }
185+
class ThrowValue extends ThrowOpcode, OpcodeWithLoad, TThrowValue { override final string toString() { result = "ThrowValue" } }
181186
class ReThrow extends ThrowOpcode, TReThrow { override final string toString() { result = "ReThrow" } }
182187
class Unwind extends Opcode, TUnwind { override final string toString() { result = "Unwind" } }
183188
class UnmodeledDefinition extends Opcode, TUnmodeledDefinition { override final string toString() { result = "UnmodeledDefinition" } }

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import cpp
77
import semmle.code.cpp.ir.implementation.EdgeKind
88
import semmle.code.cpp.ir.implementation.MemoryAccessKind
99
import semmle.code.cpp.ir.implementation.Opcode
10+
private import semmle.code.cpp.Print
1011
private import semmle.code.cpp.ir.implementation.Opcode
1112
private import semmle.code.cpp.ir.internal.OperandTag
1213

@@ -29,12 +30,11 @@ module InstructionSanity {
2930
tag instanceof RightOperandTag
3031
)
3132
) or
32-
opcode instanceof CopyOpcode and tag instanceof CopySourceOperandTag or
3333
opcode instanceof MemoryAccessOpcode and tag instanceof AddressOperandTag or
3434
opcode instanceof BufferAccessOpcode and tag instanceof BufferSizeOperand or
3535
opcode instanceof OpcodeWithCondition and tag instanceof ConditionOperandTag or
36-
opcode instanceof Opcode::ReturnValue and tag instanceof ReturnValueOperandTag or
37-
opcode instanceof Opcode::ThrowValue and tag instanceof ExceptionOperandTag or
36+
opcode instanceof OpcodeWithLoad and tag instanceof LoadOperandTag or
37+
opcode instanceof Opcode::Store and tag instanceof StoreValueOperandTag or
3838
opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag or
3939
opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag or
4040
opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag or
@@ -93,6 +93,14 @@ module InstructionSanity {
9393
)
9494
}
9595

96+
query predicate missingOperandType(Operand operand, string message) {
97+
exists(Function func |
98+
not exists(operand.getType()) and
99+
func = operand.getUseInstruction().getEnclosingFunction() and
100+
message = "Operand missing type in function '" + getIdentityString(func) + "'."
101+
)
102+
}
103+
96104
/**
97105
* Holds if an instruction, other than `ExitFunction`, has no successors.
98106
*/
@@ -714,7 +722,7 @@ class ReturnValueInstruction extends ReturnInstruction {
714722
getOpcode() instanceof Opcode::ReturnValue
715723
}
716724

717-
final ReturnValueOperand getReturnValueOperand() {
725+
final LoadOperand getReturnValueOperand() {
718726
result = getAnOperand()
719727
}
720728

@@ -728,19 +736,23 @@ class CopyInstruction extends Instruction {
728736
getOpcode() instanceof CopyOpcode
729737
}
730738

731-
final CopySourceOperand getSourceValueOperand() {
732-
result = getAnOperand()
739+
Operand getSourceValueOperand() {
740+
none()
733741
}
734-
742+
735743
final Instruction getSourceValue() {
736744
result = getSourceValueOperand().getDefinitionInstruction()
737745
}
738746
}
739747

740-
class CopyValueInstruction extends CopyInstruction {
748+
class CopyValueInstruction extends CopyInstruction, UnaryInstruction {
741749
CopyValueInstruction() {
742750
getOpcode() instanceof Opcode::CopyValue
743751
}
752+
753+
override final UnaryOperand getSourceValueOperand() {
754+
result = getAnOperand()
755+
}
744756
}
745757

746758
class LoadInstruction extends CopyInstruction {
@@ -755,6 +767,10 @@ class LoadInstruction extends CopyInstruction {
755767
final Instruction getSourceAddress() {
756768
result = getSourceAddressOperand().getDefinitionInstruction()
757769
}
770+
771+
override final LoadOperand getSourceValueOperand() {
772+
result = getAnOperand()
773+
}
758774
}
759775

760776
class StoreInstruction extends CopyInstruction {
@@ -773,6 +789,10 @@ class StoreInstruction extends CopyInstruction {
773789
final Instruction getDestinationAddress() {
774790
result = getDestinationAddressOperand().getDefinitionInstruction()
775791
}
792+
793+
override final StoreValueOperand getSourceValueOperand() {
794+
result = getAnOperand()
795+
}
776796
}
777797

778798
class ConditionalBranchInstruction extends Instruction {
@@ -1442,7 +1462,7 @@ class ThrowValueInstruction extends ThrowInstruction {
14421462
/**
14431463
* Gets the operand for the exception thrown by this instruction.
14441464
*/
1445-
final ExceptionOperand getExceptionOperand() {
1465+
final LoadOperand getExceptionOperand() {
14461466
result = getAnOperand()
14471467
}
14481468

0 commit comments

Comments
 (0)