diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/EdgeKind.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/EdgeKind.qll index 3266c8b1661..e7b21f0f6ec 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/EdgeKind.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/EdgeKind.qll @@ -1,12 +1,13 @@ private import internal.EdgeKindInternal private newtype TEdgeKind = - TGotoEdge() or // Single successor (including fall-through) - TTrueEdge() or // 'true' edge of conditional branch - TFalseEdge() or // 'false' edge of conditional branch - TExceptionEdge() or // Thrown exception - TDefaultEdge() or // 'default' label of switch - TCaseEdge(string minValue, string maxValue) { // Case label of switch + TGotoEdge() or // Single successor (including fall-through) + TTrueEdge() or // 'true' edge of conditional branch + TFalseEdge() or // 'false' edge of conditional branch + TExceptionEdge() or // Thrown exception + TDefaultEdge() or // 'default' label of switch + TCaseEdge(string minValue, string maxValue) { + // Case label of switch Language::hasCaseEdge(minValue, maxValue) } @@ -24,70 +25,50 @@ abstract class EdgeKind extends TEdgeKind { * or `IRBlock`. */ class GotoEdge extends EdgeKind, TGotoEdge { - override final string toString() { - result = "Goto" - } + final override string toString() { result = "Goto" } } -GotoEdge gotoEdge() { - result = TGotoEdge() -} +GotoEdge gotoEdge() { result = TGotoEdge() } /** * A "true" edge, representing the successor of a conditional branch when the * condition is non-zero. */ class TrueEdge extends EdgeKind, TTrueEdge { - override final string toString() { - result = "True" - } + final override string toString() { result = "True" } } -TrueEdge trueEdge() { - result = TTrueEdge() -} +TrueEdge trueEdge() { result = TTrueEdge() } /** * A "false" edge, representing the successor of a conditional branch when the * condition is zero. */ class FalseEdge extends EdgeKind, TFalseEdge { - override final string toString() { - result = "False" - } + final override string toString() { result = "False" } } -FalseEdge falseEdge() { - result = TFalseEdge() -} +FalseEdge falseEdge() { result = TFalseEdge() } /** * An "exception" edge, representing the successor of an instruction when that * instruction's evaluation throws an exception. */ class ExceptionEdge extends EdgeKind, TExceptionEdge { - override final string toString() { - result = "Exception" - } + final override string toString() { result = "Exception" } } -ExceptionEdge exceptionEdge() { - result = TExceptionEdge() -} +ExceptionEdge exceptionEdge() { result = TExceptionEdge() } /** * A "default" edge, representing the successor of a `Switch` instruction when * none of the case values matches the condition value. */ class DefaultEdge extends EdgeKind, TDefaultEdge { - override final string toString() { - result = "Default" - } + final override string toString() { result = "Default" } } -DefaultEdge defaultEdge() { - result = TDefaultEdge() -} +DefaultEdge defaultEdge() { result = TDefaultEdge() } /** * A "case" edge, representing the successor of a `Switch` instruction when the @@ -95,28 +76,20 @@ DefaultEdge defaultEdge() { */ class CaseEdge extends EdgeKind, TCaseEdge { string minValue; + string maxValue; - CaseEdge() { - this = TCaseEdge(minValue, maxValue) + CaseEdge() { this = TCaseEdge(minValue, maxValue) } + + final override string toString() { + if minValue = maxValue + then result = "Case[" + minValue + "]" + else result = "Case[" + minValue + ".." + maxValue + "]" } - override final string toString() { - if minValue = maxValue then - result = "Case[" + minValue + "]" - else - result = "Case[" + minValue + ".." + maxValue + "]" - } + string getMinValue() { result = minValue } - string getMinValue() { - result = minValue - } - - string getMaxValue() { - result = maxValue - } + string getMaxValue() { result = maxValue } } -CaseEdge caseEdge(string minValue, string maxValue) { - result = TCaseEdge(minValue, maxValue) -} +CaseEdge caseEdge(string minValue, string maxValue) { result = TCaseEdge(minValue, maxValue) } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/MemoryAccessKind.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/MemoryAccessKind.qll index db45b8a2678..d4f1599d78e 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/MemoryAccessKind.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/MemoryAccessKind.qll @@ -15,17 +15,13 @@ private newtype TMemoryAccessKind = * memory result. */ class MemoryAccessKind extends TMemoryAccessKind { - string toString() { - none() - } + string toString() { none() } /** * Holds if the operand or result accesses memory pointed to by the `AddressOperand` on the * same instruction. */ - predicate usesAddressOperand() { - none() - } + predicate usesAddressOperand() { none() } } /** @@ -33,13 +29,9 @@ class MemoryAccessKind extends TMemoryAccessKind { * same instruction. */ class IndirectMemoryAccess extends MemoryAccessKind, TIndirectMemoryAccess { - override string toString() { - result = "indirect" - } - - override final predicate usesAddressOperand() { - any() - } + override string toString() { result = "indirect" } + + final override predicate usesAddressOperand() { any() } } /** @@ -47,13 +39,9 @@ class IndirectMemoryAccess extends MemoryAccessKind, TIndirectMemoryAccess { * `AddressOperand` on the same instruction. */ class IndirectMayMemoryAccess extends MemoryAccessKind, TIndirectMayMemoryAccess { - override string toString() { - result = "indirect(may)" - } + override string toString() { result = "indirect(may)" } - override final predicate usesAddressOperand() { - any() - } + final override predicate usesAddressOperand() { any() } } /** @@ -62,13 +50,9 @@ class IndirectMayMemoryAccess extends MemoryAccessKind, TIndirectMayMemoryAccess * `BufferSizeOperand`. */ class BufferMemoryAccess extends MemoryAccessKind, TBufferMemoryAccess { - override string toString() { - result = "buffer" - } + override string toString() { result = "buffer" } - override final predicate usesAddressOperand() { - any() - } + final override predicate usesAddressOperand() { any() } } /** @@ -77,31 +61,23 @@ class BufferMemoryAccess extends MemoryAccessKind, TBufferMemoryAccess { * elements given by the `BufferSizeOperand`. */ class BufferMayMemoryAccess extends MemoryAccessKind, TBufferMayMemoryAccess { - override string toString() { - result = "buffer(may)" - } + override string toString() { result = "buffer(may)" } - override final predicate usesAddressOperand() { - any() - } + final override predicate usesAddressOperand() { any() } } /** * The operand or result accesses all memory whose address has escaped. */ class EscapedMemoryAccess extends MemoryAccessKind, TEscapedMemoryAccess { - override string toString() { - result = "escaped" - } + override string toString() { result = "escaped" } } /** * The operand or result may access all memory whose address has escaped. */ class EscapedMayMemoryAccess extends MemoryAccessKind, TEscapedMayMemoryAccess { - override string toString() { - result = "escaped(may)" - } + override string toString() { result = "escaped(may)" } } /** @@ -109,9 +85,7 @@ class EscapedMayMemoryAccess extends MemoryAccessKind, TEscapedMayMemoryAccess { * definition. */ class PhiMemoryAccess extends MemoryAccessKind, TPhiMemoryAccess { - override string toString() { - result = "phi" - } + override string toString() { result = "phi" } } /** @@ -119,9 +93,7 @@ class PhiMemoryAccess extends MemoryAccessKind, TPhiMemoryAccess { * definition. */ class ChiTotalMemoryAccess extends MemoryAccessKind, TChiTotalMemoryAccess { - override string toString() { - result = "chi(total)" - } + override string toString() { result = "chi(total)" } } /** @@ -129,9 +101,7 @@ class ChiTotalMemoryAccess extends MemoryAccessKind, TChiTotalMemoryAccess { * definition. */ class ChiPartialMemoryAccess extends MemoryAccessKind, TChiPartialMemoryAccess { - override string toString() { - result = "chi(partial)" - } + override string toString() { result = "chi(partial)" } } /** @@ -139,7 +109,5 @@ class ChiPartialMemoryAccess extends MemoryAccessKind, TChiPartialMemoryAccess { * `UnmodeledDefinition` and on the operands of `UnmodeledUse`. */ class UnmodeledMemoryAccess extends MemoryAccessKind, TUnmodeledMemoryAccess { - override string toString() { - result = "unmodeled" - } + override string toString() { result = "unmodeled" } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/Opcode.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/Opcode.qll index dc19598beab..a16b8eb88a0 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/Opcode.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/Opcode.qll @@ -77,152 +77,382 @@ private newtype TOpcode = TNewObj() class Opcode extends TOpcode { - string toString() { - result = "UnknownOpcode" - } + string toString() { result = "UnknownOpcode" } } -abstract class UnaryOpcode extends Opcode {} +abstract class UnaryOpcode extends Opcode { } -abstract class BinaryOpcode extends Opcode {} +abstract class BinaryOpcode extends Opcode { } -abstract class PointerArithmeticOpcode extends BinaryOpcode {} +abstract class PointerArithmeticOpcode extends BinaryOpcode { } -abstract class PointerOffsetOpcode extends PointerArithmeticOpcode {} +abstract class PointerOffsetOpcode extends PointerArithmeticOpcode { } -abstract class ArithmeticOpcode extends Opcode {} +abstract class ArithmeticOpcode extends Opcode { } -abstract class BinaryArithmeticOpcode extends BinaryOpcode, ArithmeticOpcode {} +abstract class BinaryArithmeticOpcode extends BinaryOpcode, ArithmeticOpcode { } -abstract class UnaryArithmeticOpcode extends UnaryOpcode, ArithmeticOpcode {} +abstract class UnaryArithmeticOpcode extends UnaryOpcode, ArithmeticOpcode { } -abstract class BitwiseOpcode extends Opcode {} +abstract class BitwiseOpcode extends Opcode { } -abstract class BinaryBitwiseOpcode extends BinaryOpcode, BitwiseOpcode {} +abstract class BinaryBitwiseOpcode extends BinaryOpcode, BitwiseOpcode { } -abstract class UnaryBitwiseOpcode extends UnaryOpcode, BitwiseOpcode {} +abstract class UnaryBitwiseOpcode extends UnaryOpcode, BitwiseOpcode { } -abstract class CompareOpcode extends BinaryOpcode {} +abstract class CompareOpcode extends BinaryOpcode { } -abstract class RelationalOpcode extends CompareOpcode {} +abstract class RelationalOpcode extends CompareOpcode { } -abstract class CopyOpcode extends Opcode {} +abstract class CopyOpcode extends Opcode { } -abstract class MemoryAccessOpcode extends Opcode {} +abstract class MemoryAccessOpcode extends Opcode { } -abstract class ReturnOpcode extends Opcode {} +abstract class ReturnOpcode extends Opcode { } -abstract class ThrowOpcode extends Opcode {} +abstract class ThrowOpcode extends Opcode { } -abstract class CatchOpcode extends Opcode {} +abstract class CatchOpcode extends Opcode { } -abstract class OpcodeWithCondition extends Opcode {} +abstract class OpcodeWithCondition extends Opcode { } -abstract class BuiltInOperationOpcode extends Opcode {} +abstract class BuiltInOperationOpcode extends Opcode { } -abstract class SideEffectOpcode extends Opcode {} +abstract class SideEffectOpcode extends Opcode { } /** * An opcode that reads a value from memory. */ -abstract class OpcodeWithLoad extends MemoryAccessOpcode {} +abstract class OpcodeWithLoad extends MemoryAccessOpcode { } /** * An opcode that reads from a set of memory locations as a side effect. */ -abstract class ReadSideEffectOpcode extends SideEffectOpcode {} +abstract class ReadSideEffectOpcode extends SideEffectOpcode { } /** * An opcode that writes to a set of memory locations as a side effect. */ -abstract class WriteSideEffectOpcode extends SideEffectOpcode {} +abstract class WriteSideEffectOpcode extends SideEffectOpcode { } /** * An opcode that may overwrite some, all, or none of an existing set of memory locations. Modeled * as a read of the original contents, plus a "may" write of the new contents. */ -abstract class MayWriteSideEffectOpcode extends SideEffectOpcode {} +abstract class MayWriteSideEffectOpcode extends SideEffectOpcode { } /** * An opcode that accesses a buffer via an `AddressOperand` and a `BufferSizeOperand`. */ -abstract class BufferAccessOpcode extends MemoryAccessOpcode {} +abstract class BufferAccessOpcode extends MemoryAccessOpcode { } module Opcode { - class NoOp extends Opcode, TNoOp { override final string toString() { result = "NoOp" } } - class Uninitialized extends MemoryAccessOpcode, TUninitialized { override final string toString() { result = "Uninitialized" } } - class Error extends Opcode, TError { override final string toString() { result = "Error" } } - class InitializeParameter extends MemoryAccessOpcode, TInitializeParameter { override final string toString() { result = "InitializeParameter" } } - class InitializeThis extends Opcode, TInitializeThis { override final string toString() { result = "InitializeThis" } } - class EnterFunction extends Opcode, TEnterFunction { override final string toString() { result = "EnterFunction" } } - class ExitFunction extends Opcode, TExitFunction { override final string toString() { result = "ExitFunction" } } - class ReturnValue extends ReturnOpcode, OpcodeWithLoad, TReturnValue { override final string toString() { result = "ReturnValue" } } - class ReturnVoid extends ReturnOpcode, TReturnVoid { override final string toString() { result = "ReturnVoid" } } - class CopyValue extends UnaryOpcode, CopyOpcode, TCopyValue { override final string toString() { result = "CopyValue" } } - class Load extends CopyOpcode, OpcodeWithLoad, TLoad { override final string toString() { result = "Load" } } - class Store extends CopyOpcode, MemoryAccessOpcode, TStore { override final string toString() { result = "Store" } } - class Add extends BinaryArithmeticOpcode, TAdd { override final string toString() { result = "Add" } } - class Sub extends BinaryArithmeticOpcode, TSub { override final string toString() { result = "Sub" } } - class Mul extends BinaryArithmeticOpcode, TMul { override final string toString() { result = "Mul" } } - class Div extends BinaryArithmeticOpcode, TDiv { override final string toString() { result = "Div" } } - class Rem extends BinaryArithmeticOpcode, TRem { override final string toString() { result = "Rem" } } - class Negate extends UnaryArithmeticOpcode, TNegate { override final string toString() { result = "Negate" } } - class ShiftLeft extends BinaryBitwiseOpcode, TShiftLeft { override final string toString() { result = "ShiftLeft" } } - class ShiftRight extends BinaryBitwiseOpcode, TShiftRight { override final string toString() { result = "ShiftRight" } } - class BitAnd extends BinaryBitwiseOpcode, TBitAnd { override final string toString() { result = "BitAnd" } } - class BitOr extends BinaryBitwiseOpcode, TBitOr { override final string toString() { result = "BitOr" } } - class BitXor extends BinaryBitwiseOpcode, TBitXor { override final string toString() { result = "BitXor" } } - class BitComplement extends UnaryBitwiseOpcode, TBitComplement { override final string toString() { result = "BitComplement" } } - class LogicalNot extends UnaryOpcode, TLogicalNot { override final string toString() { result = "LogicalNot" } } - class CompareEQ extends CompareOpcode, TCompareEQ { override final string toString() { result = "CompareEQ" } } - class CompareNE extends CompareOpcode, TCompareNE { override final string toString() { result = "CompareNE" } } - class CompareLT extends RelationalOpcode, TCompareLT { override final string toString() { result = "CompareLT" } } - class CompareGT extends RelationalOpcode, TCompareGT { override final string toString() { result = "CompareGT" } } - class CompareLE extends RelationalOpcode, TCompareLE { override final string toString() { result = "CompareLE" } } - class CompareGE extends RelationalOpcode, TCompareGE { override final string toString() { result = "CompareGE" } } - class PointerAdd extends PointerOffsetOpcode, TPointerAdd { override final string toString() { result = "PointerAdd" } } - class PointerSub extends PointerOffsetOpcode, TPointerSub { override final string toString() { result = "PointerSub" } } - class PointerDiff extends PointerArithmeticOpcode, TPointerDiff { override final string toString() { result = "PointerDiff" } } - class Convert extends UnaryOpcode, TConvert { override final string toString() { result = "Convert" } } - class ConvertToBase extends UnaryOpcode, TConvertToBase { override final string toString() { result = "ConvertToBase" } } - class ConvertToVirtualBase extends UnaryOpcode, TConvertToVirtualBase { override final string toString() { result = "ConvertToVirtualBase" } } - class ConvertToDerived extends UnaryOpcode, TConvertToDerived { override final string toString() { result = "ConvertToDerived" } } - class CheckedConvertOrNull extends UnaryOpcode, TCheckedConvertOrNull { override final string toString() { result = "CheckedConvertOrNull" } } - class CheckedConvertOrThrow extends UnaryOpcode, TCheckedConvertOrThrow { override final string toString() { result = "CheckedConvertOrThrow" } } - class DynamicCastToVoid extends UnaryOpcode, TDynamicCastToVoid { override final string toString() { result = "DynamicCastToVoid" } } - class VariableAddress extends Opcode, TVariableAddress { override final string toString() { result = "VariableAddress" } } - class FieldAddress extends UnaryOpcode, TFieldAddress { override final string toString() { result = "FieldAddress" } } - class ElementsAddress extends UnaryOpcode, TElementsAddress { override final string toString() { result = "ElementsAddress" } } - class FunctionAddress extends Opcode, TFunctionAddress { override final string toString() { result = "FunctionAddress" } } - class Constant extends Opcode, TConstant { override final string toString() { result = "Constant" } } - class StringConstant extends Opcode, TStringConstant { override final string toString() { result = "StringConstant" } } - class ConditionalBranch extends OpcodeWithCondition, TConditionalBranch { override final string toString() { result = "ConditionalBranch" } } - class Switch extends OpcodeWithCondition, TSwitch { override final string toString() { result = "Switch" } } - class Call extends Opcode, TCall { override final string toString() { result = "Call" } } - class CatchByType extends CatchOpcode, TCatchByType { override final string toString() { result = "CatchByType" } } - class CatchAny extends CatchOpcode, TCatchAny { override final string toString() { result = "CatchAny" } } - class ThrowValue extends ThrowOpcode, OpcodeWithLoad, TThrowValue { override final string toString() { result = "ThrowValue" } } - class ReThrow extends ThrowOpcode, TReThrow { override final string toString() { result = "ReThrow" } } - class Unwind extends Opcode, TUnwind { override final string toString() { result = "Unwind" } } - class UnmodeledDefinition extends Opcode, TUnmodeledDefinition { override final string toString() { result = "UnmodeledDefinition" } } - class UnmodeledUse extends Opcode, TUnmodeledUse { override final string toString() { result = "UnmodeledUse" } } - class AliasedDefinition extends Opcode, TAliasedDefinition { override final string toString() { result = "AliasedDefinition" } } - class Phi extends Opcode, TPhi { override final string toString() { result = "Phi" } } - class BuiltIn extends BuiltInOperationOpcode, TBuiltIn { override final string toString() { result = "BuiltIn" } } - class VarArgsStart extends BuiltInOperationOpcode, TVarArgsStart { override final string toString() { result = "VarArgsStart" } } - class VarArgsEnd extends BuiltInOperationOpcode, TVarArgsEnd { override final string toString() { result = "VarArgsEnd" } } - class VarArg extends BuiltInOperationOpcode, TVarArg { override final string toString() { result = "VarArg" } } - class VarArgCopy extends BuiltInOperationOpcode, TVarArgCopy { override final string toString() { result = "VarArgCopy" } } - class CallSideEffect extends MayWriteSideEffectOpcode, TCallSideEffect { override final string toString() { result = "CallSideEffect" } } - class CallReadSideEffect extends ReadSideEffectOpcode, TCallReadSideEffect { override final string toString() { result = "CallReadSideEffect" } } - class IndirectReadSideEffect extends ReadSideEffectOpcode, MemoryAccessOpcode, TIndirectReadSideEffect { override final string toString() { result = "IndirectReadSideEffect" } } - class IndirectWriteSideEffect extends WriteSideEffectOpcode, MemoryAccessOpcode, TIndirectWriteSideEffect { override final string toString() { result = "IndirectWriteSideEffect" } } - class IndirectMayWriteSideEffect extends MayWriteSideEffectOpcode, MemoryAccessOpcode, TIndirectMayWriteSideEffect { override final string toString() { result = "IndirectMayWriteSideEffect" } } - class BufferReadSideEffect extends ReadSideEffectOpcode, BufferAccessOpcode, TBufferReadSideEffect { override final string toString() { result = "BufferReadSideEffect" } } - class BufferWriteSideEffect extends WriteSideEffectOpcode, BufferAccessOpcode, TBufferWriteSideEffect { override final string toString() { result = "BufferWriteSideEffect" } } - class BufferMayWriteSideEffect extends MayWriteSideEffectOpcode, BufferAccessOpcode, TBufferMayWriteSideEffect { override final string toString() { result = "BufferMayWriteSideEffect" } } - class Chi extends Opcode, TChi { override final string toString() { result = "Chi" } } - class InlineAsm extends Opcode, TInlineAsm { override final string toString() { result = "InlineAsm" } } - class Unreached extends Opcode, TUnreached { override final string toString() { result = "Unreached" } } - class NewObj extends Opcode, TNewObj { override final string toString() { result = "NewObj" } } + class NoOp extends Opcode, TNoOp { + final override string toString() { result = "NoOp" } + } + + class Uninitialized extends MemoryAccessOpcode, TUninitialized { + final override string toString() { result = "Uninitialized" } + } + + class Error extends Opcode, TError { + final override string toString() { result = "Error" } + } + + class InitializeParameter extends MemoryAccessOpcode, TInitializeParameter { + final override string toString() { result = "InitializeParameter" } + } + + class InitializeThis extends Opcode, TInitializeThis { + final override string toString() { result = "InitializeThis" } + } + + class EnterFunction extends Opcode, TEnterFunction { + final override string toString() { result = "EnterFunction" } + } + + class ExitFunction extends Opcode, TExitFunction { + final override string toString() { result = "ExitFunction" } + } + + class ReturnValue extends ReturnOpcode, OpcodeWithLoad, TReturnValue { + final override string toString() { result = "ReturnValue" } + } + + class ReturnVoid extends ReturnOpcode, TReturnVoid { + final override string toString() { result = "ReturnVoid" } + } + + class CopyValue extends UnaryOpcode, CopyOpcode, TCopyValue { + final override string toString() { result = "CopyValue" } + } + + class Load extends CopyOpcode, OpcodeWithLoad, TLoad { + final override string toString() { result = "Load" } + } + + class Store extends CopyOpcode, MemoryAccessOpcode, TStore { + final override string toString() { result = "Store" } + } + + class Add extends BinaryArithmeticOpcode, TAdd { + final override string toString() { result = "Add" } + } + + class Sub extends BinaryArithmeticOpcode, TSub { + final override string toString() { result = "Sub" } + } + + class Mul extends BinaryArithmeticOpcode, TMul { + final override string toString() { result = "Mul" } + } + + class Div extends BinaryArithmeticOpcode, TDiv { + final override string toString() { result = "Div" } + } + + class Rem extends BinaryArithmeticOpcode, TRem { + final override string toString() { result = "Rem" } + } + + class Negate extends UnaryArithmeticOpcode, TNegate { + final override string toString() { result = "Negate" } + } + + class ShiftLeft extends BinaryBitwiseOpcode, TShiftLeft { + final override string toString() { result = "ShiftLeft" } + } + + class ShiftRight extends BinaryBitwiseOpcode, TShiftRight { + final override string toString() { result = "ShiftRight" } + } + + class BitAnd extends BinaryBitwiseOpcode, TBitAnd { + final override string toString() { result = "BitAnd" } + } + + class BitOr extends BinaryBitwiseOpcode, TBitOr { + final override string toString() { result = "BitOr" } + } + + class BitXor extends BinaryBitwiseOpcode, TBitXor { + final override string toString() { result = "BitXor" } + } + + class BitComplement extends UnaryBitwiseOpcode, TBitComplement { + final override string toString() { result = "BitComplement" } + } + + class LogicalNot extends UnaryOpcode, TLogicalNot { + final override string toString() { result = "LogicalNot" } + } + + class CompareEQ extends CompareOpcode, TCompareEQ { + final override string toString() { result = "CompareEQ" } + } + + class CompareNE extends CompareOpcode, TCompareNE { + final override string toString() { result = "CompareNE" } + } + + class CompareLT extends RelationalOpcode, TCompareLT { + final override string toString() { result = "CompareLT" } + } + + class CompareGT extends RelationalOpcode, TCompareGT { + final override string toString() { result = "CompareGT" } + } + + class CompareLE extends RelationalOpcode, TCompareLE { + final override string toString() { result = "CompareLE" } + } + + class CompareGE extends RelationalOpcode, TCompareGE { + final override string toString() { result = "CompareGE" } + } + + class PointerAdd extends PointerOffsetOpcode, TPointerAdd { + final override string toString() { result = "PointerAdd" } + } + + class PointerSub extends PointerOffsetOpcode, TPointerSub { + final override string toString() { result = "PointerSub" } + } + + class PointerDiff extends PointerArithmeticOpcode, TPointerDiff { + final override string toString() { result = "PointerDiff" } + } + + class Convert extends UnaryOpcode, TConvert { + final override string toString() { result = "Convert" } + } + + class ConvertToBase extends UnaryOpcode, TConvertToBase { + final override string toString() { result = "ConvertToBase" } + } + + class ConvertToVirtualBase extends UnaryOpcode, TConvertToVirtualBase { + final override string toString() { result = "ConvertToVirtualBase" } + } + + class ConvertToDerived extends UnaryOpcode, TConvertToDerived { + final override string toString() { result = "ConvertToDerived" } + } + + class CheckedConvertOrNull extends UnaryOpcode, TCheckedConvertOrNull { + final override string toString() { result = "CheckedConvertOrNull" } + } + + class CheckedConvertOrThrow extends UnaryOpcode, TCheckedConvertOrThrow { + final override string toString() { result = "CheckedConvertOrThrow" } + } + + class DynamicCastToVoid extends UnaryOpcode, TDynamicCastToVoid { + final override string toString() { result = "DynamicCastToVoid" } + } + + class VariableAddress extends Opcode, TVariableAddress { + final override string toString() { result = "VariableAddress" } + } + + class FieldAddress extends UnaryOpcode, TFieldAddress { + final override string toString() { result = "FieldAddress" } + } + + class ElementsAddress extends UnaryOpcode, TElementsAddress { + final override string toString() { result = "ElementsAddress" } + } + + class FunctionAddress extends Opcode, TFunctionAddress { + final override string toString() { result = "FunctionAddress" } + } + + class Constant extends Opcode, TConstant { + final override string toString() { result = "Constant" } + } + + class StringConstant extends Opcode, TStringConstant { + final override string toString() { result = "StringConstant" } + } + + class ConditionalBranch extends OpcodeWithCondition, TConditionalBranch { + final override string toString() { result = "ConditionalBranch" } + } + + class Switch extends OpcodeWithCondition, TSwitch { + final override string toString() { result = "Switch" } + } + + class Call extends Opcode, TCall { + final override string toString() { result = "Call" } + } + + class CatchByType extends CatchOpcode, TCatchByType { + final override string toString() { result = "CatchByType" } + } + + class CatchAny extends CatchOpcode, TCatchAny { + final override string toString() { result = "CatchAny" } + } + + class ThrowValue extends ThrowOpcode, OpcodeWithLoad, TThrowValue { + final override string toString() { result = "ThrowValue" } + } + + class ReThrow extends ThrowOpcode, TReThrow { + final override string toString() { result = "ReThrow" } + } + + class Unwind extends Opcode, TUnwind { + final override string toString() { result = "Unwind" } + } + + class UnmodeledDefinition extends Opcode, TUnmodeledDefinition { + final override string toString() { result = "UnmodeledDefinition" } + } + + class UnmodeledUse extends Opcode, TUnmodeledUse { + final override string toString() { result = "UnmodeledUse" } + } + + class AliasedDefinition extends Opcode, TAliasedDefinition { + final override string toString() { result = "AliasedDefinition" } + } + + class Phi extends Opcode, TPhi { + final override string toString() { result = "Phi" } + } + + class BuiltIn extends BuiltInOperationOpcode, TBuiltIn { + final override string toString() { result = "BuiltIn" } + } + + class VarArgsStart extends BuiltInOperationOpcode, TVarArgsStart { + final override string toString() { result = "VarArgsStart" } + } + + class VarArgsEnd extends BuiltInOperationOpcode, TVarArgsEnd { + final override string toString() { result = "VarArgsEnd" } + } + + class VarArg extends BuiltInOperationOpcode, TVarArg { + final override string toString() { result = "VarArg" } + } + + class VarArgCopy extends BuiltInOperationOpcode, TVarArgCopy { + final override string toString() { result = "VarArgCopy" } + } + + class CallSideEffect extends MayWriteSideEffectOpcode, TCallSideEffect { + final override string toString() { result = "CallSideEffect" } + } + + class CallReadSideEffect extends ReadSideEffectOpcode, TCallReadSideEffect { + final override string toString() { result = "CallReadSideEffect" } + } + + class IndirectReadSideEffect extends ReadSideEffectOpcode, MemoryAccessOpcode, + TIndirectReadSideEffect { + final override string toString() { result = "IndirectReadSideEffect" } + } + + class IndirectWriteSideEffect extends WriteSideEffectOpcode, MemoryAccessOpcode, + TIndirectWriteSideEffect { + final override string toString() { result = "IndirectWriteSideEffect" } + } + + class IndirectMayWriteSideEffect extends MayWriteSideEffectOpcode, MemoryAccessOpcode, + TIndirectMayWriteSideEffect { + final override string toString() { result = "IndirectMayWriteSideEffect" } + } + + class BufferReadSideEffect extends ReadSideEffectOpcode, BufferAccessOpcode, TBufferReadSideEffect { + final override string toString() { result = "BufferReadSideEffect" } + } + + class BufferWriteSideEffect extends WriteSideEffectOpcode, BufferAccessOpcode, + TBufferWriteSideEffect { + final override string toString() { result = "BufferWriteSideEffect" } + } + + class BufferMayWriteSideEffect extends MayWriteSideEffectOpcode, BufferAccessOpcode, + TBufferMayWriteSideEffect { + final override string toString() { result = "BufferMayWriteSideEffect" } + } + + class Chi extends Opcode, TChi { + final override string toString() { result = "Chi" } + } + + class InlineAsm extends Opcode, TInlineAsm { + final override string toString() { result = "InlineAsm" } + } + + class Unreached extends Opcode, TUnreached { + final override string toString() { result = "Unreached" } + } + + class NewObj extends Opcode, TNewObj { + final override string toString() { result = "NewObj" } + } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/TempVariableTag.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/TempVariableTag.qll index da5cfca3f02..ec6de78cfa4 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/TempVariableTag.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/TempVariableTag.qll @@ -2,7 +2,5 @@ private import internal.TempVariableTagInternal private import Imports::TempVariableTag class TempVariableTag extends TTempVariableTag { - string toString() { - result = getTempVariableTagId(this) - } + string toString() { result = getTempVariableTagId(this) } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IR.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IR.qll index 5bc9493f4ab..278040f8ab8 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IR.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IR.qll @@ -15,21 +15,15 @@ private newtype TIRPropertyProvider = MkIRPropertyProvider() * single instance of this class to specify the additional properties computed by the library. */ class IRPropertyProvider extends TIRPropertyProvider { - string toString() { - result = "IRPropertyProvider" - } + string toString() { result = "IRPropertyProvider" } /** * Gets the value of the property named `key` for the specified instruction. */ - string getInstructionProperty(Instruction instruction, string key) { - none() - } + string getInstructionProperty(Instruction instruction, string key) { none() } /** * Gets the value of the property named `key` for the specified block. */ - string getBlockProperty(IRBlock block, string key) { - none() - } + string getBlockProperty(IRBlock block, string key) { none() } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRBlock.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRBlock.qll index c62a2dbc5ea..e0322a00e15 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRBlock.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRBlock.qll @@ -16,32 +16,25 @@ private import Cached * Most consumers should use the class `IRBlock`. */ class IRBlockBase extends TIRBlock { - final string toString() { - result = getFirstInstruction(this).toString() - } + final string toString() { result = getFirstInstruction(this).toString() } + + final Language::Location getLocation() { result = getFirstInstruction().getLocation() } + + final string getUniqueId() { result = getFirstInstruction(this).getUniqueId() } - final Language::Location getLocation() { - result = getFirstInstruction().getLocation() - } - - final string getUniqueId() { - result = getFirstInstruction(this).getUniqueId() - } - /** * Gets the zero-based index of the block within its function. This is used * by debugging and printing code only. */ int getDisplayIndex() { this = rank[result + 1](IRBlock funcBlock | - funcBlock.getEnclosingFunction() = getEnclosingFunction() | - funcBlock order by funcBlock.getUniqueId() - ) + funcBlock.getEnclosingFunction() = getEnclosingFunction() + | + funcBlock order by funcBlock.getUniqueId() + ) } - final Instruction getInstruction(int index) { - result = getInstruction(this, index) - } + final Instruction getInstruction(int index) { result = getInstruction(this, index) } final PhiInstruction getAPhiInstruction() { Construction::getPhiInstructionBlockStart(result) = getFirstInstruction() @@ -52,17 +45,11 @@ class IRBlockBase extends TIRBlock { result = getAPhiInstruction() } - final Instruction getFirstInstruction() { - result = getFirstInstruction(this) - } + final Instruction getFirstInstruction() { result = getFirstInstruction(this) } - final Instruction getLastInstruction() { - result = getInstruction(getInstructionCount() - 1) - } + final Instruction getLastInstruction() { result = getInstruction(getInstructionCount() - 1) } - final int getInstructionCount() { - result = getInstructionCount(this) - } + final int getInstructionCount() { result = getInstructionCount(this) } final IRFunction getEnclosingIRFunction() { result = getFirstInstruction(this).getEnclosingIRFunction() @@ -79,40 +66,26 @@ class IRBlockBase extends TIRBlock { * instruction of another block. */ class IRBlock extends IRBlockBase { - final IRBlock getASuccessor() { - blockSuccessor(this, result) - } + final IRBlock getASuccessor() { blockSuccessor(this, result) } - final IRBlock getAPredecessor() { - blockSuccessor(result, this) - } + final IRBlock getAPredecessor() { blockSuccessor(result, this) } - final IRBlock getSuccessor(EdgeKind kind) { - blockSuccessor(this, result, kind) - } + final IRBlock getSuccessor(EdgeKind kind) { blockSuccessor(this, result, kind) } - final IRBlock getBackEdgeSuccessor(EdgeKind kind) { - backEdgeSuccessor(this, result, kind) - } + final IRBlock getBackEdgeSuccessor(EdgeKind kind) { backEdgeSuccessor(this, result, kind) } - final predicate immediatelyDominates(IRBlock block) { - blockImmediatelyDominates(this, block) - } + final predicate immediatelyDominates(IRBlock block) { blockImmediatelyDominates(this, block) } - final predicate strictlyDominates(IRBlock block) { - blockImmediatelyDominates+(this, block) - } + final predicate strictlyDominates(IRBlock block) { blockImmediatelyDominates+(this, block) } - final predicate dominates(IRBlock block) { - strictlyDominates(block) or this = block - } + final predicate dominates(IRBlock block) { strictlyDominates(block) or this = block } pragma[noinline] final IRBlock dominanceFrontier() { dominates(result.getAPredecessor()) and not strictlyDominates(result) } - + /** * Holds if this block is reachable from the entry point of its function */ @@ -125,22 +98,21 @@ class IRBlock extends IRBlockBase { private predicate startsBasicBlock(Instruction instr) { not instr instanceof PhiInstruction and ( - count(Instruction predecessor | - instr = predecessor.getASuccessor() - ) != 1 or // Multiple predecessors or no predecessor + count(Instruction predecessor | instr = predecessor.getASuccessor()) != 1 // Multiple predecessors or no predecessor + or exists(Instruction predecessor | instr = predecessor.getASuccessor() and - strictcount(Instruction other | - other = predecessor.getASuccessor() - ) > 1 - ) or // Predecessor has multiple successors + strictcount(Instruction other | other = predecessor.getASuccessor()) > 1 + ) // Predecessor has multiple successors + or exists(Instruction predecessor, EdgeKind kind | instr = predecessor.getSuccessor(kind) and not kind instanceof GotoEdge - ) or // Incoming edge is not a GotoEdge + ) // Incoming edge is not a GotoEdge + or exists(Instruction predecessor | instr = Construction::getInstructionBackEdgeSuccessor(predecessor, _) - ) // A back edge enters this instruction + ) // A back edge enters this instruction ) } @@ -148,11 +120,10 @@ private predicate isEntryBlock(TIRBlock block) { block = MkIRBlock(any(EnterFunctionInstruction enter)) } -private cached module Cached { - cached newtype TIRBlock = - MkIRBlock(Instruction firstInstr) { - startsBasicBlock(firstInstr) - } +cached +private module Cached { + cached + newtype TIRBlock = MkIRBlock(Instruction firstInstr) { startsBasicBlock(firstInstr) } /** Holds if `i2` follows `i1` in a `IRBlock`. */ private predicate adjacentInBlock(Instruction i1, Instruction i2) { @@ -165,15 +136,16 @@ private cached module Cached { shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index) /** Holds if `i` is the `index`th instruction in `block`. */ - cached Instruction getInstruction(TIRBlock block, int index) { + cached + Instruction getInstruction(TIRBlock block, int index) { result = getInstructionFromFirst(getFirstInstruction(block), index) } - cached int getInstructionCount(TIRBlock block) { - result = strictcount(getInstruction(block, _)) - } + cached + int getInstructionCount(TIRBlock block) { result = strictcount(getInstruction(block, _)) } - cached predicate blockSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { + cached + predicate blockSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { exists(Instruction predLast, Instruction succFirst | predLast = getInstruction(pred, getInstructionCount(pred) - 1) and succFirst = predLast.getSuccessor(kind) and @@ -185,7 +157,8 @@ private cached module Cached { private predicate blockIdentity(TIRBlock b1, TIRBlock b2) { b1 = b2 } pragma[noopt] - cached predicate backEdgeSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { + cached + predicate backEdgeSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { backEdgeSuccessorRaw(pred, succ, kind) or // See the QLDoc on `backEdgeSuccessorRaw`. @@ -226,14 +199,12 @@ private cached module Cached { ) } - cached predicate blockSuccessor(TIRBlock pred, TIRBlock succ) { - blockSuccessor(pred, succ, _) - } + cached + predicate blockSuccessor(TIRBlock pred, TIRBlock succ) { blockSuccessor(pred, succ, _) } - cached predicate blockImmediatelyDominates(TIRBlock dominator, TIRBlock block) = + cached + predicate blockImmediatelyDominates(TIRBlock dominator, TIRBlock block) = idominance(isEntryBlock/1, blockSuccessor/2)(_, dominator, block) } -Instruction getFirstInstruction(TIRBlock block) { - block = MkIRBlock(result) -} +Instruction getFirstInstruction(TIRBlock block) { block = MkIRBlock(result) } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRFunction.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRFunction.qll index 1dd61fb9db1..1e9c2d1d913 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRFunction.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRFunction.qll @@ -2,9 +2,7 @@ private import internal.IRInternal import Instruction private newtype TIRFunction = - MkIRFunction(Language::Function func) { - Construction::functionHasIR(func) - } + MkIRFunction(Language::Function func) { Construction::functionHasIR(func) } /** * Represents the IR for a function. @@ -12,27 +10,19 @@ private newtype TIRFunction = class IRFunction extends TIRFunction { Language::Function func; - IRFunction() { - this = MkIRFunction(func) - } + IRFunction() { this = MkIRFunction(func) } - final string toString() { - result = "IR: " + func.toString() - } + final string toString() { result = "IR: " + func.toString() } /** * Gets the function whose IR is represented. */ - final Language::Function getFunction() { - result = func - } + final Language::Function getFunction() { result = func } /** * Gets the location of the function. */ - final Language::Location getLocation() { - result = func.getLocation() - } + final Language::Location getLocation() { result = func.getLocation() } /** * Gets the entry point for this function. @@ -64,38 +54,28 @@ class IRFunction extends TIRFunction { * Gets the single return instruction for this function. */ pragma[noinline] - final ReturnInstruction getReturnInstruction() { - result.getEnclosingIRFunction() = this - } + final ReturnInstruction getReturnInstruction() { result.getEnclosingIRFunction() = this } /** * Gets the variable used to hold the return value of this function. If this * function does not return a value, this predicate does not hold. */ pragma[noinline] - final IRReturnVariable getReturnVariable() { - result.getEnclosingIRFunction() = this - } - + final IRReturnVariable getReturnVariable() { result.getEnclosingIRFunction() = this } + /** * Gets the block containing the entry point of this function. - */ + */ pragma[noinline] - final IRBlock getEntryBlock() { - result.getFirstInstruction() = getEnterFunctionInstruction() - } + final IRBlock getEntryBlock() { result.getFirstInstruction() = getEnterFunctionInstruction() } /** * Gets all instructions in this function. */ - final Instruction getAnInstruction() { - result.getEnclosingIRFunction() = this - } + final Instruction getAnInstruction() { result.getEnclosingIRFunction() = this } /** * Gets all blocks in this function. */ - final IRBlock getABlock() { - result.getEnclosingIRFunction() = this - } + final IRBlock getABlock() { result.getEnclosingIRFunction() = this } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRSanity.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRSanity.qll index 9e21452c1fb..3921472dc8e 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRSanity.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRSanity.qll @@ -1,3 +1,2 @@ private import IR import InstructionSanity - diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRVariable.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRVariable.qll index b8c6af20a60..2c1b43672fc 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRVariable.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRVariable.qll @@ -37,27 +37,21 @@ abstract class IRVariable extends TIRVariable { * within the function. */ abstract string getUniqueId(); - + /** * Gets the source location of this variable. */ - final Language::Location getLocation() { - result = getAST().getLocation() - } + final Language::Location getLocation() { result = getAST().getLocation() } /** * Gets the IR for the function that references this variable. */ - final IRFunction getEnclosingIRFunction() { - result.getFunction() = func - } + final IRFunction getEnclosingIRFunction() { result.getFunction() = func } /** * Gets the function that references this variable. */ - final Language::Function getEnclosingFunction() { - result = func - } + final Language::Function getEnclosingFunction() { result = func } } /** @@ -65,34 +59,25 @@ abstract class IRVariable extends TIRVariable { */ class IRUserVariable extends IRVariable, TIRUserVariable { Language::Variable var; + Language::Type type; - IRUserVariable() { - this = TIRUserVariable(var, type, func) - } + IRUserVariable() { this = TIRUserVariable(var, type, func) } - override final string toString() { - result = getVariable().toString() - } + final override string toString() { result = getVariable().toString() } - override final Language::AST getAST() { - result = var - } + final override Language::AST getAST() { result = var } - override final string getUniqueId() { + final override string getUniqueId() { result = getVariable().toString() + " " + getVariable().getLocation().toString() } - override final Language::Type getType() { - result = type - } + final override Language::Type getType() { result = type } /** * Gets the original user-declared variable. */ - Language::Variable getVariable() { - result = var - } + Language::Variable getVariable() { result = var } } /** @@ -100,31 +85,22 @@ class IRUserVariable extends IRVariable, TIRUserVariable { * stack. This includes all parameters, non-static local variables, and * temporary variables. */ -abstract class IRAutomaticVariable extends IRVariable { -} +abstract class IRAutomaticVariable extends IRVariable { } class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable { override Language::AutomaticVariable var; - IRAutomaticUserVariable() { - Language::isVariableAutomatic(var) - } + IRAutomaticUserVariable() { Language::isVariableAutomatic(var) } - final override Language::AutomaticVariable getVariable() { - result = var - } + final override Language::AutomaticVariable getVariable() { result = var } } class IRStaticUserVariable extends IRUserVariable { override Language::StaticVariable var; - IRStaticUserVariable() { - not Language::isVariableAutomatic(var) - } + IRStaticUserVariable() { not Language::isVariableAutomatic(var) } - final override Language::StaticVariable getVariable() { - result = var - } + final override Language::StaticVariable getVariable() { result = var } } IRTempVariable getIRTempVariable(Language::AST ast, TempVariableTag tag) { @@ -134,55 +110,39 @@ IRTempVariable getIRTempVariable(Language::AST ast, TempVariableTag tag) { class IRTempVariable extends IRVariable, IRAutomaticVariable, TIRTempVariable { Language::AST ast; + TempVariableTag tag; + Language::Type type; - IRTempVariable() { - this = TIRTempVariable(func, ast, tag, type) - } + IRTempVariable() { this = TIRTempVariable(func, ast, tag, type) } - override final Language::Type getType() { - result = type - } + final override Language::Type getType() { result = type } - override final Language::AST getAST() { - result = ast - } + final override Language::AST getAST() { result = ast } - override final string getUniqueId() { + final override string getUniqueId() { result = "Temp: " + Construction::getTempVariableUniqueId(this) } - final TempVariableTag getTag() { - result = tag - } + final TempVariableTag getTag() { result = tag } override string toString() { result = getBaseString() + ast.getLocation().getStartLine().toString() + ":" + - ast.getLocation().getStartColumn().toString() + ast.getLocation().getStartColumn().toString() } - string getBaseString() { - result = "#temp" - } + string getBaseString() { result = "#temp" } } class IRReturnVariable extends IRTempVariable { - IRReturnVariable() { - tag = ReturnValueTempVar() - } + IRReturnVariable() { tag = ReturnValueTempVar() } - override final string toString() { - result = "#return" - } + final override string toString() { result = "#return" } } class IRThrowVariable extends IRTempVariable { - IRThrowVariable() { - tag = ThrowTempVar() - } + IRThrowVariable() { tag = ThrowTempVar() } - override string getBaseString() { - result = "#throw" - } + override string getBaseString() { result = "#throw" } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll index 7b621c95dcc..ea0a3f72998 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll @@ -20,27 +20,38 @@ module InstructionSanity { exists(Opcode opcode | opcode = instr.getOpcode() and ( - opcode instanceof UnaryOpcode and tag instanceof UnaryOperandTag or + opcode instanceof UnaryOpcode and tag instanceof UnaryOperandTag + or + opcode instanceof BinaryOpcode and ( - opcode instanceof BinaryOpcode and - ( - tag instanceof LeftOperandTag or - tag instanceof RightOperandTag - ) - ) or - opcode instanceof MemoryAccessOpcode and tag instanceof AddressOperandTag or - opcode instanceof BufferAccessOpcode and tag instanceof BufferSizeOperand or - opcode instanceof OpcodeWithCondition and tag instanceof ConditionOperandTag or - opcode instanceof OpcodeWithLoad and tag instanceof LoadOperandTag or - opcode instanceof Opcode::Store and tag instanceof StoreValueOperandTag or - opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag or - opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag or - opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag or - opcode instanceof Opcode::Chi and tag instanceof ChiPartialOperandTag or - ( - (opcode instanceof ReadSideEffectOpcode or opcode instanceof MayWriteSideEffectOpcode or opcode instanceof Opcode::InlineAsm) and - tag instanceof SideEffectOperandTag + tag instanceof LeftOperandTag or + tag instanceof RightOperandTag ) + or + opcode instanceof MemoryAccessOpcode and tag instanceof AddressOperandTag + or + opcode instanceof BufferAccessOpcode and tag instanceof BufferSizeOperand + or + opcode instanceof OpcodeWithCondition and tag instanceof ConditionOperandTag + or + opcode instanceof OpcodeWithLoad and tag instanceof LoadOperandTag + or + opcode instanceof Opcode::Store and tag instanceof StoreValueOperandTag + or + opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag + or + opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag + or + opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag + or + opcode instanceof Opcode::Chi and tag instanceof ChiPartialOperandTag + or + ( + opcode instanceof ReadSideEffectOpcode or + opcode instanceof MayWriteSideEffectOpcode or + opcode instanceof Opcode::InlineAsm + ) and + tag instanceof SideEffectOperandTag ) ) } @@ -55,8 +66,8 @@ module InstructionSanity { operand = instr.getAnOperand() and operand.getOperandTag() = tag ) and - message = "Instruction '" + instr.getOpcode().toString() + "' is missing an expected operand with tag '" + - tag.toString() + "' in function '$@'." and + message = "Instruction '" + instr.getOpcode().toString() + + "' is missing an expected operand with tag '" + tag.toString() + "' in function '$@'." and func = instr.getEnclosingIRFunction() and funcText = Language::getIdentityString(func.getFunction()) ) @@ -68,10 +79,13 @@ module InstructionSanity { query predicate unexpectedOperand(Instruction instr, OperandTag tag) { exists(NonPhiOperand operand | operand = instr.getAnOperand() and - operand.getOperandTag() = tag) and + operand.getOperandTag() = tag + ) and not expectsOperand(instr, tag) and not (instr instanceof CallInstruction and tag instanceof ArgumentOperandTag) and - not (instr instanceof BuiltInOperationInstruction and tag instanceof PositionalArgumentOperandTag) and + not ( + instr instanceof BuiltInOperationInstruction and tag instanceof PositionalArgumentOperandTag + ) and not (instr instanceof InlineAsmInstruction and tag instanceof AsmOperandTag) } @@ -121,9 +135,7 @@ module InstructionSanity { * Holds if there are multiple (`n`) edges of kind `kind` from `source`, * where `target` is among the targets of those edges. */ - query predicate ambiguousSuccessors( - Instruction source, EdgeKind kind, int n, Instruction target - ) { + query predicate ambiguousSuccessors(Instruction source, EdgeKind kind, int n, Instruction target) { n = strictcount(Instruction t | source.getSuccessor(kind) = t) and n > 1 and source.getSuccessor(kind) = target @@ -222,11 +234,10 @@ module InstructionSanity { * of evaluation is at the end of the corresponding predecessor block. */ private predicate pointOfEvaluation(Operand operand, IRBlock block, int index) { - ( - block = operand.(PhiInputOperand).getPredecessorBlock() and - index = block.getInstructionCount() - ) or - exists (Instruction use | + block = operand.(PhiInputOperand).getPredecessorBlock() and + index = block.getInstructionCount() + or + exists(Instruction use | use = operand.(NonPhiOperand).getUse() and block.getInstruction(index) = use ) @@ -235,31 +246,28 @@ module InstructionSanity { /** * Holds if `useOperand` has a definition that does not dominate the use. */ - query predicate useNotDominatedByDefinition(Operand useOperand, string message, IRFunction func, - string funcText) { - - exists (IRBlock useBlock, int useIndex, Instruction defInstr, IRBlock defBlock, int defIndex | + query predicate useNotDominatedByDefinition( + Operand useOperand, string message, IRFunction func, string funcText + ) { + exists(IRBlock useBlock, int useIndex, Instruction defInstr, IRBlock defBlock, int defIndex | not useOperand.getUse() instanceof UnmodeledUseInstruction and pointOfEvaluation(useOperand, useBlock, useIndex) and defInstr = useOperand.getAnyDef() and ( - ( - defInstr instanceof PhiInstruction and - defBlock = defInstr.getBlock() and - defIndex = -1 - ) + defInstr instanceof PhiInstruction and + defBlock = defInstr.getBlock() and + defIndex = -1 or - defBlock.getInstruction(defIndex) = defInstr + defBlock.getInstruction(defIndex) = defInstr ) and not ( - defBlock.strictlyDominates(useBlock) or - ( - defBlock = useBlock and - defIndex < useIndex - ) + defBlock.strictlyDominates(useBlock) + or + defBlock = useBlock and + defIndex < useIndex ) and message = "Operand '" + useOperand.toString() + - "' is not dominated by its definition in function '$@'." and + "' is not dominated by its definition in function '$@'." and func = useOperand.getEnclosingIRFunction() and funcText = Language::getIdentityString(func.getFunction()) ) @@ -270,9 +278,7 @@ module InstructionSanity { * Represents a single operation in the IR. */ class Instruction extends Construction::TInstruction { - final string toString() { - result = getOpcode().toString() + ": " + getAST().toString() - } + final string toString() { result = getOpcode().toString() + ": " + getAST().toString() } /** * Gets a string showing the result, opcode, and operands of the instruction, equivalent to what @@ -291,36 +297,27 @@ class Instruction extends Construction::TInstruction { * VariableAddress[x] */ final string getOperationString() { - if exists(getImmediateString()) then - result = getOperationPrefix() + getOpcode().toString() + "[" + getImmediateString() + "]" - else - result = getOperationPrefix() + getOpcode().toString() + if exists(getImmediateString()) + then result = getOperationPrefix() + getOpcode().toString() + "[" + getImmediateString() + "]" + else result = getOperationPrefix() + getOpcode().toString() } /** * Gets a string describing the immediate value of this instruction, if any. */ - string getImmediateString() { - none() - } + string getImmediateString() { none() } private string getOperationPrefix() { - if this instanceof SideEffectInstruction then - result = "^" - else - result = "" + if this instanceof SideEffectInstruction then result = "^" else result = "" } private string getResultPrefix() { - if getResultType() instanceof Language::VoidType then - result = "v" - else if hasMemoryResult() then - if isResultModeled() then - result = "m" - else - result = "mu" + if getResultType() instanceof Language::VoidType + then result = "v" else - result = "r" + if hasMemoryResult() + then if isResultModeled() then result = "m" else result = "mu" + else result = "r" } /** @@ -335,36 +332,32 @@ class Instruction extends Construction::TInstruction { phiCount = count(block.getAPhiInstruction()) and this = block.getInstruction(index) and result = index + phiCount - ) or - ( - this instanceof PhiInstruction and - this = rank[result + 1](PhiInstruction phiInstr | - phiInstr = block.getAPhiInstruction() | + ) + or + this instanceof PhiInstruction and + this = rank[result + 1](PhiInstruction phiInstr | + phiInstr = block.getAPhiInstruction() + | phiInstr order by phiInstr.getUniqueId() ) - ) ) ) } bindingset[type] private string getValueCategoryString(string type) { - if isGLValue() then - result = "glval<" + type + ">" - else - result = type + if isGLValue() then result = "glval<" + type + ">" else result = type } string getResultTypeString() { exists(string valcat | valcat = getValueCategoryString(getResultType().toString()) and - if (getResultType() instanceof Language::UnknownType and - not isGLValue() and - exists(getResultSize())) then ( - result = valcat + "[" + getResultSize().toString() + "]" - ) - else - result = valcat + if + getResultType() instanceof Language::UnknownType and + not isGLValue() and + exists(getResultSize()) + then result = valcat + "[" + getResultSize().toString() + "]" + else result = valcat ) } @@ -377,7 +370,7 @@ class Instruction extends Construction::TInstruction { */ string getResultId() { result = getResultPrefix() + getBlock().getDisplayIndex().toString() + "_" + - getDisplayIndexInBlock().toString() + getDisplayIndexInBlock().toString() } /** @@ -387,9 +380,7 @@ class Instruction extends Construction::TInstruction { * * Example: `r1_1(int*)` */ - final string getResultString() { - result = getResultId() + "(" + getResultTypeString() + ")" - } + final string getResultString() { result = getResultId() + "(" + getResultTypeString() + ")" } /** * Gets a string describing the operands of this instruction, suitable for @@ -399,9 +390,10 @@ class Instruction extends Construction::TInstruction { */ string getOperandsString() { result = concat(Operand operand | - operand = getAnOperand() | - operand.getDumpString(), ", " order by operand.getDumpSortOrder() - ) + operand = getAnOperand() + | + operand.getDumpString(), ", " order by operand.getDumpSortOrder() + ) } /** @@ -411,16 +403,12 @@ class Instruction extends Construction::TInstruction { * This is used for sorting IR output for tests, and is likely to be * inefficient for any other use. */ - final string getUniqueId() { - result = Construction::getInstructionUniqueId(this) - } + final string getUniqueId() { result = Construction::getInstructionUniqueId(this) } /** * Gets the basic block that contains this instruction. */ - final IRBlock getBlock() { - result.getAnInstruction() = this - } + final IRBlock getBlock() { result.getAnInstruction() = this } /** * Gets the function that contains this instruction. @@ -439,31 +427,27 @@ class Instruction extends Construction::TInstruction { /** * Gets the AST that caused this instruction to be generated. */ - final Language::AST getAST() { - result = Construction::getInstructionAST(this) - } + final Language::AST getAST() { result = Construction::getInstructionAST(this) } /** * Gets the location of the source code for this instruction. */ - final Language::Location getLocation() { - result = getAST().getLocation() - } + final Language::Location getLocation() { result = getAST().getLocation() } /** * Gets the `Expr` whose result is computed by this instruction, if any. */ final Language::Expr getConvertedResultExpression() { - result = Construction::getInstructionConvertedResultExpression(this) + result = Construction::getInstructionConvertedResultExpression(this) } - + /** * Gets the unconverted `Expr` whose result is computed by this instruction, if any. */ final Language::Expr getUnconvertedResultExpression() { - result = Construction::getInstructionUnconvertedResultExpression(this) + result = Construction::getInstructionUnconvertedResultExpression(this) } - + /** * Gets the type of the result produced by this instruction. If the * instruction does not produce a result, its result type will be `VoidType`. @@ -471,9 +455,7 @@ class Instruction extends Construction::TInstruction { * If `isGLValue()` holds, then the result type of this instruction should be * thought of as "pointer to `getResultType()`". */ - final Language::Type getResultType() { - Construction::instructionHasType(this, result, _) - } + final Language::Type getResultType() { Construction::instructionHasType(this, result, _) } /** * Holds if the result produced by this instruction is a glvalue. If this @@ -493,9 +475,7 @@ class Instruction extends Construction::TInstruction { * result of the `Load` instruction is a prvalue of type `int`, representing * the integer value loaded from variable `x`. */ - final predicate isGLValue() { - Construction::instructionHasType(this, _, true) - } + final predicate isGLValue() { Construction::instructionHasType(this, _, true) } /** * Gets the size of the result produced by this instruction, in bytes. If the @@ -505,53 +485,42 @@ class Instruction extends Construction::TInstruction { * `getResultSize()` will always be the size of a pointer. */ final int getResultSize() { - if isGLValue() then ( + if isGLValue() + then // a glvalue is always pointer-sized. result = Language::getPointerSize() - ) - else if getResultType() instanceof Language::UnknownType then - result = Construction::getInstructionResultSize(this) - else ( - result = Language::getTypeSize(getResultType()) - ) + else + if getResultType() instanceof Language::UnknownType + then result = Construction::getInstructionResultSize(this) + else result = Language::getTypeSize(getResultType()) } /** * Gets the opcode that specifies the operation performed by this instruction. */ - final Opcode getOpcode() { - result = Construction::getInstructionOpcode(this) - } + final Opcode getOpcode() { result = Construction::getInstructionOpcode(this) } /** * Gets all direct uses of the result of this instruction. The result can be * an `Operand` for which `isDefinitionInexact` holds. */ - final Operand getAUse() { - result.getAnyDef() = this - } + final Operand getAUse() { result.getAnyDef() = this } /** * Gets all of this instruction's operands. */ - final Operand getAnOperand() { - result.getUse() = this - } + final Operand getAnOperand() { result.getUse() = this } /** * Holds if this instruction produces a memory result. */ - final predicate hasMemoryResult() { - exists(getResultMemoryAccess()) - } + final predicate hasMemoryResult() { exists(getResultMemoryAccess()) } /** * Gets the kind of memory access performed by this instruction's result. * Holds only for instructions with a memory result. */ - MemoryAccessKind getResultMemoryAccess() { - none() - } + MemoryAccessKind getResultMemoryAccess() { none() } /** * Gets the operand that holds the memory address to which this instruction stores its @@ -568,9 +537,7 @@ class Instruction extends Construction::TInstruction { * result, if any. For example, in `m3 = Store r1, r2`, the result of `getResultAddressOperand()` * is the instruction that defines `r1`. */ - final Instruction getResultAddress() { - result = getResultAddressOperand().getDef() - } + final Instruction getResultAddress() { result = getResultAddressOperand().getDef() } /** * Holds if the result of this instruction is precisely modeled in SSA. Always @@ -623,137 +590,89 @@ class Instruction extends Construction::TInstruction { /** * Gets all direct successors of this instruction. */ - final Instruction getASuccessor() { - result = getSuccessor(_) - } + final Instruction getASuccessor() { result = getSuccessor(_) } /** * Gets a predecessor of this instruction such that the predecessor reaches * this instruction along the control flow edge specified by `kind`. */ - final Instruction getPredecessor(EdgeKind kind) { - result.getSuccessor(kind) = this - } + final Instruction getPredecessor(EdgeKind kind) { result.getSuccessor(kind) = this } /** * Gets all direct predecessors of this instruction. */ - final Instruction getAPredecessor() { - result = getPredecessor(_) - } + final Instruction getAPredecessor() { result = getPredecessor(_) } } class VariableInstruction extends Instruction { IRVariable var; - VariableInstruction() { - var = Construction::getInstructionVariable(this) - } + VariableInstruction() { var = Construction::getInstructionVariable(this) } - override final string getImmediateString() { - result = var.toString() - } + final override string getImmediateString() { result = var.toString() } - final IRVariable getVariable() { - result = var - } + final IRVariable getVariable() { result = var } } class FieldInstruction extends Instruction { Language::Field field; - FieldInstruction() { - field = Construction::getInstructionField(this) - } + FieldInstruction() { field = Construction::getInstructionField(this) } - override final string getImmediateString() { - result = field.toString() - } + final override string getImmediateString() { result = field.toString() } - final Language::Field getField() { - result = field - } + final Language::Field getField() { result = field } } class FunctionInstruction extends Instruction { Language::Function funcSymbol; - FunctionInstruction() { - funcSymbol = Construction::getInstructionFunction(this) - } + FunctionInstruction() { funcSymbol = Construction::getInstructionFunction(this) } - override final string getImmediateString() { - result = funcSymbol.toString() - } + final override string getImmediateString() { result = funcSymbol.toString() } - final Language::Function getFunctionSymbol() { - result = funcSymbol - } + final Language::Function getFunctionSymbol() { result = funcSymbol } } class ConstantValueInstruction extends Instruction { string value; - ConstantValueInstruction() { - value = Construction::getInstructionConstantValue(this) - } + ConstantValueInstruction() { value = Construction::getInstructionConstantValue(this) } - override final string getImmediateString() { - result = value - } + final override string getImmediateString() { result = value } - final string getValue() { - result = value - } + final string getValue() { result = value } } class EnterFunctionInstruction extends Instruction { - EnterFunctionInstruction() { - getOpcode() instanceof Opcode::EnterFunction - } + EnterFunctionInstruction() { getOpcode() instanceof Opcode::EnterFunction } } class VariableAddressInstruction extends VariableInstruction { - VariableAddressInstruction() { - getOpcode() instanceof Opcode::VariableAddress - } + VariableAddressInstruction() { getOpcode() instanceof Opcode::VariableAddress } } class InitializeParameterInstruction extends VariableInstruction { - InitializeParameterInstruction() { - getOpcode() instanceof Opcode::InitializeParameter - } + InitializeParameterInstruction() { getOpcode() instanceof Opcode::InitializeParameter } - final Language::Parameter getParameter() { - result = var.(IRUserVariable).getVariable() - } + final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } } /** * An instruction that initializes the `this` pointer parameter of the enclosing function. */ class InitializeThisInstruction extends Instruction { - InitializeThisInstruction() { - getOpcode() instanceof Opcode::InitializeThis - } + InitializeThisInstruction() { getOpcode() instanceof Opcode::InitializeThis } } class FieldAddressInstruction extends FieldInstruction { - FieldAddressInstruction() { - getOpcode() instanceof Opcode::FieldAddress - } + FieldAddressInstruction() { getOpcode() instanceof Opcode::FieldAddress } - final UnaryOperand getObjectAddressOperand() { - result = getAnOperand() - } + final UnaryOperand getObjectAddressOperand() { result = getAnOperand() } - final Instruction getObjectAddress() { - result = getObjectAddressOperand().getDef() - } + final Instruction getObjectAddress() { result = getObjectAddressOperand().getDef() } } /** @@ -767,207 +686,125 @@ class FieldAddressInstruction extends FieldInstruction { * taken may want to ignore any function that contains an `ErrorInstruction`. */ class ErrorInstruction extends Instruction { - ErrorInstruction() { - getOpcode() instanceof Opcode::Error - } + ErrorInstruction() { getOpcode() instanceof Opcode::Error } } class UninitializedInstruction extends VariableInstruction { - UninitializedInstruction() { - getOpcode() instanceof Opcode::Uninitialized - } + UninitializedInstruction() { getOpcode() instanceof Opcode::Uninitialized } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } /** * Gets the variable that is uninitialized. */ - final Language::Variable getLocalVariable() { - result = var.(IRUserVariable).getVariable() - } + final Language::Variable getLocalVariable() { result = var.(IRUserVariable).getVariable() } } class NoOpInstruction extends Instruction { - NoOpInstruction() { - getOpcode() instanceof Opcode::NoOp - } + NoOpInstruction() { getOpcode() instanceof Opcode::NoOp } } class ReturnInstruction extends Instruction { - ReturnInstruction() { - getOpcode() instanceof ReturnOpcode - } + ReturnInstruction() { getOpcode() instanceof ReturnOpcode } } class ReturnVoidInstruction extends ReturnInstruction { - ReturnVoidInstruction() { - getOpcode() instanceof Opcode::ReturnVoid - } + ReturnVoidInstruction() { getOpcode() instanceof Opcode::ReturnVoid } } class ReturnValueInstruction extends ReturnInstruction { - ReturnValueInstruction() { - getOpcode() instanceof Opcode::ReturnValue - } + ReturnValueInstruction() { getOpcode() instanceof Opcode::ReturnValue } - final LoadOperand getReturnValueOperand() { - result = getAnOperand() - } - - final Instruction getReturnValue() { - result = getReturnValueOperand().getDef() - } + final LoadOperand getReturnValueOperand() { result = getAnOperand() } + + final Instruction getReturnValue() { result = getReturnValueOperand().getDef() } } class CopyInstruction extends Instruction { - CopyInstruction() { - getOpcode() instanceof CopyOpcode - } + CopyInstruction() { getOpcode() instanceof CopyOpcode } - Operand getSourceValueOperand() { - none() - } + Operand getSourceValueOperand() { none() } - final Instruction getSourceValue() { - result = getSourceValueOperand().getDef() - } + final Instruction getSourceValue() { result = getSourceValueOperand().getDef() } } class CopyValueInstruction extends CopyInstruction, UnaryInstruction { - CopyValueInstruction() { - getOpcode() instanceof Opcode::CopyValue - } + CopyValueInstruction() { getOpcode() instanceof Opcode::CopyValue } - override final UnaryOperand getSourceValueOperand() { - result = getAnOperand() - } + final override UnaryOperand getSourceValueOperand() { result = getAnOperand() } } class LoadInstruction extends CopyInstruction { - LoadInstruction() { - getOpcode() instanceof Opcode::Load - } + LoadInstruction() { getOpcode() instanceof Opcode::Load } - final AddressOperand getSourceAddressOperand() { - result = getAnOperand() - } - - final Instruction getSourceAddress() { - result = getSourceAddressOperand().getDef() - } + final AddressOperand getSourceAddressOperand() { result = getAnOperand() } - override final LoadOperand getSourceValueOperand() { - result = getAnOperand() - } + final Instruction getSourceAddress() { result = getSourceAddressOperand().getDef() } + + final override LoadOperand getSourceValueOperand() { result = getAnOperand() } } class StoreInstruction extends CopyInstruction { - StoreInstruction() { - getOpcode() instanceof Opcode::Store - } + StoreInstruction() { getOpcode() instanceof Opcode::Store } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } - final AddressOperand getDestinationAddressOperand() { - result = getAnOperand() - } - - final Instruction getDestinationAddress() { - result = getDestinationAddressOperand().getDef() - } + final AddressOperand getDestinationAddressOperand() { result = getAnOperand() } - override final StoreValueOperand getSourceValueOperand() { - result = getAnOperand() - } + final Instruction getDestinationAddress() { result = getDestinationAddressOperand().getDef() } + + final override StoreValueOperand getSourceValueOperand() { result = getAnOperand() } } class ConditionalBranchInstruction extends Instruction { - ConditionalBranchInstruction() { - getOpcode() instanceof Opcode::ConditionalBranch - } + ConditionalBranchInstruction() { getOpcode() instanceof Opcode::ConditionalBranch } - final ConditionOperand getConditionOperand() { - result = getAnOperand() - } + final ConditionOperand getConditionOperand() { result = getAnOperand() } - final Instruction getCondition() { - result = getConditionOperand().getDef() - } + final Instruction getCondition() { result = getConditionOperand().getDef() } - final Instruction getTrueSuccessor() { - result = getSuccessor(trueEdge()) - } + final Instruction getTrueSuccessor() { result = getSuccessor(trueEdge()) } - final Instruction getFalseSuccessor() { - result = getSuccessor(falseEdge()) - } + final Instruction getFalseSuccessor() { result = getSuccessor(falseEdge()) } } class ExitFunctionInstruction extends Instruction { - ExitFunctionInstruction() { - getOpcode() instanceof Opcode::ExitFunction - } + ExitFunctionInstruction() { getOpcode() instanceof Opcode::ExitFunction } } class ConstantInstruction extends ConstantValueInstruction { - ConstantInstruction() { - getOpcode() instanceof Opcode::Constant - } + ConstantInstruction() { getOpcode() instanceof Opcode::Constant } } class IntegerConstantInstruction extends ConstantInstruction { - IntegerConstantInstruction() { - getResultType() instanceof Language::IntegralType - } + IntegerConstantInstruction() { getResultType() instanceof Language::IntegralType } } class FloatConstantInstruction extends ConstantInstruction { - FloatConstantInstruction() { - getResultType() instanceof Language::FloatingPointType - } + FloatConstantInstruction() { getResultType() instanceof Language::FloatingPointType } } class StringConstantInstruction extends Instruction { Language::StringLiteral value; - StringConstantInstruction() { - value = Construction::getInstructionStringLiteral(this) - } + StringConstantInstruction() { value = Construction::getInstructionStringLiteral(this) } - override final string getImmediateString() { - result = Language::getStringLiteralText(value) - } + final override string getImmediateString() { result = Language::getStringLiteralText(value) } - final Language::StringLiteral getValue() { - result = value - } + final Language::StringLiteral getValue() { result = value } } class BinaryInstruction extends Instruction { - BinaryInstruction() { - getOpcode() instanceof BinaryOpcode - } + BinaryInstruction() { getOpcode() instanceof BinaryOpcode } - final LeftOperand getLeftOperand() { - result = getAnOperand() - } - - final RightOperand getRightOperand() { - result = getAnOperand() - } + final LeftOperand getLeftOperand() { result = getAnOperand() } - final Instruction getLeft() { - result = getLeftOperand().getDef() - } + final RightOperand getRightOperand() { result = getAnOperand() } + + final Instruction getLeft() { result = getLeftOperand().getDef() } + + final Instruction getRight() { result = getRightOperand().getDef() } - final Instruction getRight() { - result = getRightOperand().getDef() - } - /** * Holds if this instruction's operands are `op1` and `op2`, in either order. */ @@ -979,89 +816,63 @@ class BinaryInstruction extends Instruction { } class ArithmeticInstruction extends Instruction { - ArithmeticInstruction() { - getOpcode() instanceof ArithmeticOpcode - } + ArithmeticInstruction() { getOpcode() instanceof ArithmeticOpcode } } -class BinaryArithmeticInstruction extends ArithmeticInstruction, BinaryInstruction {} +class BinaryArithmeticInstruction extends ArithmeticInstruction, BinaryInstruction { } -class UnaryArithmeticInstruction extends ArithmeticInstruction, UnaryInstruction {} +class UnaryArithmeticInstruction extends ArithmeticInstruction, UnaryInstruction { } class AddInstruction extends BinaryArithmeticInstruction { - AddInstruction() { - getOpcode() instanceof Opcode::Add - } + AddInstruction() { getOpcode() instanceof Opcode::Add } } class SubInstruction extends BinaryArithmeticInstruction { - SubInstruction() { - getOpcode() instanceof Opcode::Sub - } + SubInstruction() { getOpcode() instanceof Opcode::Sub } } class MulInstruction extends BinaryArithmeticInstruction { - MulInstruction() { - getOpcode() instanceof Opcode::Mul - } + MulInstruction() { getOpcode() instanceof Opcode::Mul } } class DivInstruction extends BinaryArithmeticInstruction { - DivInstruction() { - getOpcode() instanceof Opcode::Div - } + DivInstruction() { getOpcode() instanceof Opcode::Div } } class RemInstruction extends BinaryArithmeticInstruction { - RemInstruction() { - getOpcode() instanceof Opcode::Rem - } + RemInstruction() { getOpcode() instanceof Opcode::Rem } } class NegateInstruction extends UnaryArithmeticInstruction { - NegateInstruction() { - getOpcode() instanceof Opcode::Negate - } + NegateInstruction() { getOpcode() instanceof Opcode::Negate } } class BitwiseInstruction extends Instruction { - BitwiseInstruction() { - getOpcode() instanceof BitwiseOpcode - } + BitwiseInstruction() { getOpcode() instanceof BitwiseOpcode } } -class BinaryBitwiseInstruction extends BitwiseInstruction, BinaryInstruction {} +class BinaryBitwiseInstruction extends BitwiseInstruction, BinaryInstruction { } -class UnaryBitwiseInstruction extends BitwiseInstruction, UnaryInstruction {} +class UnaryBitwiseInstruction extends BitwiseInstruction, UnaryInstruction { } class BitAndInstruction extends BinaryBitwiseInstruction { - BitAndInstruction() { - getOpcode() instanceof Opcode::BitAnd - } + BitAndInstruction() { getOpcode() instanceof Opcode::BitAnd } } class BitOrInstruction extends BinaryBitwiseInstruction { - BitOrInstruction() { - getOpcode() instanceof Opcode::BitOr - } + BitOrInstruction() { getOpcode() instanceof Opcode::BitOr } } class BitXorInstruction extends BinaryBitwiseInstruction { - BitXorInstruction() { - getOpcode() instanceof Opcode::BitXor - } + BitXorInstruction() { getOpcode() instanceof Opcode::BitXor } } class ShiftLeftInstruction extends BinaryBitwiseInstruction { - ShiftLeftInstruction() { - getOpcode() instanceof Opcode::ShiftLeft - } + ShiftLeftInstruction() { getOpcode() instanceof Opcode::ShiftLeft } } class ShiftRightInstruction extends BinaryBitwiseInstruction { - ShiftRightInstruction() { - getOpcode() instanceof Opcode::ShiftRight - } + ShiftRightInstruction() { getOpcode() instanceof Opcode::ShiftRight } } class PointerArithmeticInstruction extends BinaryInstruction { @@ -1072,57 +883,37 @@ class PointerArithmeticInstruction extends BinaryInstruction { elementSize = Construction::getInstructionElementSize(this) } - override final string getImmediateString() { - result = elementSize.toString() - } + final override string getImmediateString() { result = elementSize.toString() } - final int getElementSize() { - result = elementSize - } + final int getElementSize() { result = elementSize } } class PointerOffsetInstruction extends PointerArithmeticInstruction { - PointerOffsetInstruction() { - getOpcode() instanceof PointerOffsetOpcode - } + PointerOffsetInstruction() { getOpcode() instanceof PointerOffsetOpcode } } class PointerAddInstruction extends PointerOffsetInstruction { - PointerAddInstruction() { - getOpcode() instanceof Opcode::PointerAdd - } + PointerAddInstruction() { getOpcode() instanceof Opcode::PointerAdd } } class PointerSubInstruction extends PointerOffsetInstruction { - PointerSubInstruction() { - getOpcode() instanceof Opcode::PointerSub - } + PointerSubInstruction() { getOpcode() instanceof Opcode::PointerSub } } class PointerDiffInstruction extends PointerArithmeticInstruction { - PointerDiffInstruction() { - getOpcode() instanceof Opcode::PointerDiff - } + PointerDiffInstruction() { getOpcode() instanceof Opcode::PointerDiff } } class UnaryInstruction extends Instruction { - UnaryInstruction() { - getOpcode() instanceof UnaryOpcode - } + UnaryInstruction() { getOpcode() instanceof UnaryOpcode } - final UnaryOperand getUnaryOperand() { - result = getAnOperand() - } - - final Instruction getUnary() { - result = getUnaryOperand().getDef() - } + final UnaryOperand getUnaryOperand() { result = getAnOperand() } + + final Instruction getUnary() { result = getUnaryOperand().getDef() } } class ConvertInstruction extends UnaryInstruction { - ConvertInstruction() { - getOpcode() instanceof Opcode::Convert - } + ConvertInstruction() { getOpcode() instanceof Opcode::Convert } } /** @@ -1131,13 +922,14 @@ class ConvertInstruction extends UnaryInstruction { */ class InheritanceConversionInstruction extends UnaryInstruction { Language::Class baseClass; + Language::Class derivedClass; InheritanceConversionInstruction() { Construction::getInstructionInheritance(this, baseClass, derivedClass) } - override final string getImmediateString() { + final override string getImmediateString() { result = derivedClass.toString() + " : " + baseClass.toString() } @@ -1155,16 +947,12 @@ class InheritanceConversionInstruction extends UnaryInstruction { * base class of the derived class, or a virtual base class of the * derived class. */ - final Language::Class getBaseClass() { - result = baseClass - } + final Language::Class getBaseClass() { result = baseClass } /** * Gets the derived class of the conversion. */ - final Language::Class getDerivedClass() { - result = derivedClass - } + final Language::Class getDerivedClass() { result = derivedClass } } /** @@ -1172,9 +960,7 @@ class InheritanceConversionInstruction extends UnaryInstruction { * to the address of a direct non-virtual base class. */ class ConvertToBaseInstruction extends InheritanceConversionInstruction { - ConvertToBaseInstruction() { - getOpcode() instanceof Opcode::ConvertToBase - } + ConvertToBaseInstruction() { getOpcode() instanceof Opcode::ConvertToBase } } /** @@ -1182,9 +968,7 @@ class ConvertToBaseInstruction extends InheritanceConversionInstruction { * to the address of a virtual base class. */ class ConvertToVirtualBaseInstruction extends InheritanceConversionInstruction { - ConvertToVirtualBaseInstruction() { - getOpcode() instanceof Opcode::ConvertToVirtualBase - } + ConvertToVirtualBaseInstruction() { getOpcode() instanceof Opcode::ConvertToVirtualBase } } /** @@ -1192,48 +976,34 @@ class ConvertToVirtualBaseInstruction extends InheritanceConversionInstruction { * to the address of a direct non-virtual derived class. */ class ConvertToDerivedInstruction extends InheritanceConversionInstruction { - ConvertToDerivedInstruction() { - getOpcode() instanceof Opcode::ConvertToDerived - } + ConvertToDerivedInstruction() { getOpcode() instanceof Opcode::ConvertToDerived } } class BitComplementInstruction extends UnaryBitwiseInstruction { - BitComplementInstruction() { - getOpcode() instanceof Opcode::BitComplement - } + BitComplementInstruction() { getOpcode() instanceof Opcode::BitComplement } } class LogicalNotInstruction extends UnaryInstruction { - LogicalNotInstruction() { - getOpcode() instanceof Opcode::LogicalNot - } + LogicalNotInstruction() { getOpcode() instanceof Opcode::LogicalNot } } class CompareInstruction extends BinaryInstruction { - CompareInstruction() { - getOpcode() instanceof CompareOpcode - } + CompareInstruction() { getOpcode() instanceof CompareOpcode } } class CompareEQInstruction extends CompareInstruction { - CompareEQInstruction() { - getOpcode() instanceof Opcode::CompareEQ - } + CompareEQInstruction() { getOpcode() instanceof Opcode::CompareEQ } } class CompareNEInstruction extends CompareInstruction { - CompareNEInstruction() { - getOpcode() instanceof Opcode::CompareNE - } + CompareNEInstruction() { getOpcode() instanceof Opcode::CompareNE } } /** * Represents an instruction that does a relative comparison of two values, such as `<` or `>=`. */ class RelationalInstruction extends CompareInstruction { - RelationalInstruction() { - getOpcode() instanceof RelationalOpcode - } + RelationalInstruction() { getOpcode() instanceof RelationalOpcode } /** * Gets the operand on the "greater" (or "greater-or-equal") side @@ -1241,9 +1011,7 @@ class RelationalInstruction extends CompareInstruction { * if the overall instruction evaluates to `true`; for example on * `x <= 20` this is the `20`, and on `y > 0` it is `y`. */ - Instruction getGreater() { - none() - } + Instruction getGreater() { none() } /** * Gets the operand on the "lesser" (or "lesser-or-equal") side @@ -1251,144 +1019,88 @@ class RelationalInstruction extends CompareInstruction { * if the overall instruction evaluates to `true`; for example on * `x <= 20` this is `x`, and on `y > 0` it is the `0`. */ - Instruction getLesser() { - none() - } + Instruction getLesser() { none() } /** * Holds if this relational instruction is strict (is not an "or-equal" instruction). */ - predicate isStrict() { - none() - } + predicate isStrict() { none() } } class CompareLTInstruction extends RelationalInstruction { - CompareLTInstruction() { - getOpcode() instanceof Opcode::CompareLT - } + CompareLTInstruction() { getOpcode() instanceof Opcode::CompareLT } - override Instruction getLesser() { - result = getLeft() - } + override Instruction getLesser() { result = getLeft() } - override Instruction getGreater() { - result = getRight() - } + override Instruction getGreater() { result = getRight() } - override predicate isStrict() { - any() - } + override predicate isStrict() { any() } } class CompareGTInstruction extends RelationalInstruction { - CompareGTInstruction() { - getOpcode() instanceof Opcode::CompareGT - } + CompareGTInstruction() { getOpcode() instanceof Opcode::CompareGT } - override Instruction getLesser() { - result = getRight() - } + override Instruction getLesser() { result = getRight() } - override Instruction getGreater() { - result = getLeft() - } + override Instruction getGreater() { result = getLeft() } - override predicate isStrict() { - any() - } + override predicate isStrict() { any() } } class CompareLEInstruction extends RelationalInstruction { - CompareLEInstruction() { - getOpcode() instanceof Opcode::CompareLE - } + CompareLEInstruction() { getOpcode() instanceof Opcode::CompareLE } - override Instruction getLesser() { - result = getLeft() - } + override Instruction getLesser() { result = getLeft() } - override Instruction getGreater() { - result = getRight() - } + override Instruction getGreater() { result = getRight() } - override predicate isStrict() { - none() - } + override predicate isStrict() { none() } } class CompareGEInstruction extends RelationalInstruction { - CompareGEInstruction() { - getOpcode() instanceof Opcode::CompareGE - } + CompareGEInstruction() { getOpcode() instanceof Opcode::CompareGE } - override Instruction getLesser() { - result = getRight() - } + override Instruction getLesser() { result = getRight() } - override Instruction getGreater() { - result = getLeft() - } + override Instruction getGreater() { result = getLeft() } - override predicate isStrict() { - none() - } + override predicate isStrict() { none() } } class SwitchInstruction extends Instruction { - SwitchInstruction() { - getOpcode() instanceof Opcode::Switch - } + SwitchInstruction() { getOpcode() instanceof Opcode::Switch } - final ConditionOperand getExpressionOperand() { - result = getAnOperand() - } + final ConditionOperand getExpressionOperand() { result = getAnOperand() } - final Instruction getExpression() { - result = getExpressionOperand().getDef() - } + final Instruction getExpression() { result = getExpressionOperand().getDef() } - final Instruction getACaseSuccessor() { - exists(CaseEdge edge | - result = getSuccessor(edge) - ) - } + final Instruction getACaseSuccessor() { exists(CaseEdge edge | result = getSuccessor(edge)) } - final Instruction getDefaultSuccessor() { - result = getSuccessor(defaultEdge()) - } + final Instruction getDefaultSuccessor() { result = getSuccessor(defaultEdge()) } } /** * An instruction that calls a function. */ class CallInstruction extends Instruction { - CallInstruction() { - getOpcode() instanceof Opcode::Call - } + CallInstruction() { getOpcode() instanceof Opcode::Call } /** * Gets the operand the specifies the target function of the call. */ - final CallTargetOperand getCallTargetOperand() { - result = getAnOperand() - } + final CallTargetOperand getCallTargetOperand() { result = getAnOperand() } /** * Gets the `Instruction` that computes the target function of the call. This is usually a * `FunctionAddress` instruction, but can also be an arbitrary instruction that produces a * function pointer. */ - final Instruction getCallTarget() { - result = getCallTargetOperand().getDef() - } + final Instruction getCallTarget() { result = getCallTargetOperand().getDef() } /** * Gets all of the argument operands of the call, including the `this` pointer, if any. */ - final ArgumentOperand getAnArgumentOperand() { - result = getAnOperand() - } + final ArgumentOperand getAnArgumentOperand() { result = getAnOperand() } /** * Gets the `Function` that the call targets, if this is statically known. @@ -1400,23 +1112,17 @@ class CallInstruction extends Instruction { /** * Gets all of the arguments of the call, including the `this` pointer, if any. */ - final Instruction getAnArgument() { - result = getAnArgumentOperand().getDef() - } + final Instruction getAnArgument() { result = getAnArgumentOperand().getDef() } /** * Gets the `this` pointer argument operand of the call, if any. */ - final ThisArgumentOperand getThisArgumentOperand() { - result = getAnOperand() - } + final ThisArgumentOperand getThisArgumentOperand() { result = getAnOperand() } /** * Gets the `this` pointer argument of the call, if any. */ - final Instruction getThisArgument() { - result = getThisArgumentOperand().getDef() - } + final Instruction getThisArgument() { result = getThisArgumentOperand().getDef() } /** * Gets the argument operand at the specified index. @@ -1438,9 +1144,7 @@ class CallInstruction extends Instruction { * An instruction representing a side effect of a function call. */ class SideEffectInstruction extends Instruction { - SideEffectInstruction() { - getOpcode() instanceof SideEffectOpcode - } + SideEffectInstruction() { getOpcode() instanceof SideEffectOpcode } final Instruction getPrimaryInstruction() { result = Construction::getPrimaryInstructionForSideEffect(this) @@ -1452,11 +1156,9 @@ class SideEffectInstruction extends Instruction { * accessed by that call. */ class CallSideEffectInstruction extends SideEffectInstruction { - CallSideEffectInstruction() { - getOpcode() instanceof Opcode::CallSideEffect - } + CallSideEffectInstruction() { getOpcode() instanceof Opcode::CallSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof EscapedMayMemoryAccess } } @@ -1466,40 +1168,30 @@ class CallSideEffectInstruction extends SideEffectInstruction { * by that call. */ class CallReadSideEffectInstruction extends SideEffectInstruction { - CallReadSideEffectInstruction() { - getOpcode() instanceof Opcode::CallReadSideEffect - } + CallReadSideEffectInstruction() { getOpcode() instanceof Opcode::CallReadSideEffect } } /** * An instruction representing the read of an indirect parameter within a function call. */ class IndirectReadSideEffectInstruction extends SideEffectInstruction { - IndirectReadSideEffectInstruction() { - getOpcode() instanceof Opcode::IndirectReadSideEffect - } + IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect } } /** * An instruction representing the read of an indirect buffer parameter within a function call. */ class BufferReadSideEffectInstruction extends SideEffectInstruction { - BufferReadSideEffectInstruction() { - getOpcode() instanceof Opcode::BufferReadSideEffect - } + BufferReadSideEffectInstruction() { getOpcode() instanceof Opcode::BufferReadSideEffect } } /** * An instruction representing the write of an indirect parameter within a function call. */ class IndirectWriteSideEffectInstruction extends SideEffectInstruction { - IndirectWriteSideEffectInstruction() { - getOpcode() instanceof Opcode::IndirectWriteSideEffect - } + IndirectWriteSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } } /** @@ -1507,13 +1199,9 @@ class IndirectWriteSideEffectInstruction extends SideEffectInstruction { * entire buffer is overwritten. */ class BufferWriteSideEffectInstruction extends SideEffectInstruction { - BufferWriteSideEffectInstruction() { - getOpcode() instanceof Opcode::BufferWriteSideEffect - } + BufferWriteSideEffectInstruction() { getOpcode() instanceof Opcode::BufferWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof BufferMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof BufferMemoryAccess } } /** @@ -1526,21 +1214,19 @@ class IndirectMayWriteSideEffectInstruction extends SideEffectInstruction { getOpcode() instanceof Opcode::IndirectMayWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMayMemoryAccess } } /** - * An instruction representing the write of an indirect buffer parameter within a function call. + * An instruction representing the write of an indirect buffer parameter within a function call. * Unlike `BufferWriteSideEffectInstruction`, the buffer might not be completely overwritten. */ class BufferMayWriteSideEffectInstruction extends SideEffectInstruction { - BufferMayWriteSideEffectInstruction() { - getOpcode() instanceof Opcode::BufferMayWriteSideEffect - } + BufferMayWriteSideEffectInstruction() { getOpcode() instanceof Opcode::BufferMayWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof BufferMayMemoryAccess } } @@ -1549,11 +1235,9 @@ class BufferMayWriteSideEffectInstruction extends SideEffectInstruction { * An instruction representing a GNU or MSVC inline assembly statement. */ class InlineAsmInstruction extends Instruction { - InlineAsmInstruction() { - getOpcode() instanceof Opcode::InlineAsm - } - - override final MemoryAccessKind getResultMemoryAccess() { + InlineAsmInstruction() { getOpcode() instanceof Opcode::InlineAsm } + + final override MemoryAccessKind getResultMemoryAccess() { result instanceof EscapedMayMemoryAccess } } @@ -1562,73 +1246,55 @@ class InlineAsmInstruction extends Instruction { * An instruction that throws an exception. */ class ThrowInstruction extends Instruction { - ThrowInstruction() { - getOpcode() instanceof ThrowOpcode - } + ThrowInstruction() { getOpcode() instanceof ThrowOpcode } } /** * An instruction that throws a new exception. */ class ThrowValueInstruction extends ThrowInstruction { - ThrowValueInstruction() { - getOpcode() instanceof Opcode::ThrowValue - } + ThrowValueInstruction() { getOpcode() instanceof Opcode::ThrowValue } /** * Gets the address operand of the exception thrown by this instruction. */ - final AddressOperand getExceptionAddressOperand() { - result = getAnOperand() - } + final AddressOperand getExceptionAddressOperand() { result = getAnOperand() } /** * Gets the address of the exception thrown by this instruction. */ - final Instruction getExceptionAddress() { - result = getExceptionAddressOperand().getDef() - } + final Instruction getExceptionAddress() { result = getExceptionAddressOperand().getDef() } /** * Gets the operand for the exception thrown by this instruction. */ - final LoadOperand getExceptionOperand() { - result = getAnOperand() - } + final LoadOperand getExceptionOperand() { result = getAnOperand() } /** * Gets the exception thrown by this instruction. */ - final Instruction getException() { - result = getExceptionOperand().getDef() - } + final Instruction getException() { result = getExceptionOperand().getDef() } } /** * An instruction that re-throws the current exception. */ class ReThrowInstruction extends ThrowInstruction { - ReThrowInstruction() { - getOpcode() instanceof Opcode::ReThrow - } + ReThrowInstruction() { getOpcode() instanceof Opcode::ReThrow } } /** * An instruction that exits the current function by propagating an exception. */ class UnwindInstruction extends Instruction { - UnwindInstruction() { - getOpcode() instanceof Opcode::Unwind - } + UnwindInstruction() { getOpcode() instanceof Opcode::Unwind } } /** * An instruction that starts a `catch` handler. */ class CatchInstruction extends Instruction { - CatchInstruction() { - getOpcode() instanceof CatchOpcode - } + CatchInstruction() { getOpcode() instanceof CatchOpcode } } /** @@ -1642,33 +1308,25 @@ class CatchByTypeInstruction extends CatchInstruction { exceptionType = Construction::getInstructionExceptionType(this) } - final override string getImmediateString() { - result = exceptionType.toString() - } + final override string getImmediateString() { result = exceptionType.toString() } /** * Gets the type of exception to be caught. */ - final Language::Type getExceptionType() { - result = exceptionType - } + final Language::Type getExceptionType() { result = exceptionType } } /** * An instruction that catches any exception. */ class CatchAnyInstruction extends CatchInstruction { - CatchAnyInstruction() { - getOpcode() instanceof Opcode::CatchAny - } + CatchAnyInstruction() { getOpcode() instanceof Opcode::CatchAny } } class UnmodeledDefinitionInstruction extends Instruction { - UnmodeledDefinitionInstruction() { - getOpcode() instanceof Opcode::UnmodeledDefinition - } + UnmodeledDefinitionInstruction() { getOpcode() instanceof Opcode::UnmodeledDefinition } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof UnmodeledMemoryAccess } } @@ -1677,23 +1335,15 @@ class UnmodeledDefinitionInstruction extends Instruction { * An instruction that initializes all escaped memory. */ class AliasedDefinitionInstruction extends Instruction { - AliasedDefinitionInstruction() { - getOpcode() instanceof Opcode::AliasedDefinition - } + AliasedDefinitionInstruction() { getOpcode() instanceof Opcode::AliasedDefinition } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof EscapedMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof EscapedMemoryAccess } } class UnmodeledUseInstruction extends Instruction { - UnmodeledUseInstruction() { - getOpcode() instanceof Opcode::UnmodeledUse - } + UnmodeledUseInstruction() { getOpcode() instanceof Opcode::UnmodeledUse } - override string getOperandsString() { - result = "mu*" - } + override string getOperandsString() { result = "mu*" } } /** @@ -1707,20 +1357,14 @@ class UnmodeledUseInstruction extends Instruction { * runtime. */ class PhiInstruction extends Instruction { - PhiInstruction() { - getOpcode() instanceof Opcode::Phi - } + PhiInstruction() { getOpcode() instanceof Opcode::Phi } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof PhiMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof PhiMemoryAccess } /** * Gets all of the instruction's `PhiInputOperand`s, representing the values that flow from each predecessor block. */ - final PhiInputOperand getAnInputOperand() { - result = this.getAnOperand() - } + final PhiInputOperand getAnInputOperand() { result = this.getAnOperand() } /** * Gets an instruction that defines the input to one of the operands of this @@ -1729,9 +1373,7 @@ class PhiInstruction extends Instruction { * results as `getAnInputOperand()` or fewer. */ pragma[noinline] - final Instruction getAnInput() { - result = this.getAnInputOperand().getDef() - } + final Instruction getAnInput() { result = this.getAnInputOperand().getDef() } } /** @@ -1777,43 +1419,31 @@ class PhiInstruction extends Instruction { * https://link.springer.com/content/pdf/10.1007%2F3-540-61053-7_66.pdf. */ class ChiInstruction extends Instruction { - ChiInstruction() { - getOpcode() instanceof Opcode::Chi - } + ChiInstruction() { getOpcode() instanceof Opcode::Chi } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof ChiTotalMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof ChiTotalMemoryAccess } /** * Gets the operand that represents the previous state of all memory that might be aliased by the * memory write. */ - final ChiTotalOperand getTotalOperand() { - result = getAnOperand() - } + final ChiTotalOperand getTotalOperand() { result = getAnOperand() } /** * Gets the operand that represents the previous state of all memory that might be aliased by the * memory write. */ - final Instruction getTotal() { - result = getTotalOperand().getDef() - } + final Instruction getTotal() { result = getTotalOperand().getDef() } /** * Gets the operand that represents the new value written by the memory write. */ - final ChiPartialOperand getPartialOperand() { - result = getAnOperand() - } + final ChiPartialOperand getPartialOperand() { result = getAnOperand() } /** * Gets the operand that represents the new value written by the memory write. */ - final Instruction getPartial() { - result = getPartialOperand().getDef() - } + final Instruction getPartial() { result = getPartialOperand().getDef() } } /** @@ -1822,9 +1452,7 @@ class ChiInstruction extends Instruction { * infeasible. */ class UnreachedInstruction extends Instruction { - UnreachedInstruction() { - getOpcode() instanceof Opcode::Unreached - } + UnreachedInstruction() { getOpcode() instanceof Opcode::Unreached } } /** @@ -1839,9 +1467,7 @@ class BuiltInOperationInstruction extends Instruction { operation = Construction::getInstructionBuiltInOperation(this) } - final Language::BuiltInOperation getBuiltInOperation() { - result = operation - } + final Language::BuiltInOperation getBuiltInOperation() { result = operation } } /** @@ -1849,11 +1475,7 @@ class BuiltInOperationInstruction extends Instruction { * actual operation is specified by the `getBuiltInOperation()` predicate. */ class BuiltInInstruction extends BuiltInOperationInstruction { - BuiltInInstruction() { - getOpcode() instanceof Opcode::BuiltIn - } + BuiltInInstruction() { getOpcode() instanceof Opcode::BuiltIn } - override final string getImmediateString() { - result = getBuiltInOperation().toString() - } + final override string getImmediateString() { result = getBuiltInOperation().toString() } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Operand.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Operand.qll index 29af8267cb7..fda04820848 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Operand.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Operand.qll @@ -12,11 +12,15 @@ private newtype TOperand = defInstr = Construction::getRegisterOperandDefinition(useInstr, tag) and not isInCycle(useInstr) } or - TNonPhiMemoryOperand(Instruction useInstr, MemoryOperandTag tag, Instruction defInstr, Overlap overlap) { + TNonPhiMemoryOperand( + Instruction useInstr, MemoryOperandTag tag, Instruction defInstr, Overlap overlap + ) { defInstr = Construction::getMemoryOperandDefinition(useInstr, tag, overlap) and not isInCycle(useInstr) } or - TPhiOperand(PhiInstruction useInstr, Instruction defInstr, IRBlock predecessorBlock, Overlap overlap) { + TPhiOperand( + PhiInstruction useInstr, Instruction defInstr, IRBlock predecessorBlock, Overlap overlap + ) { defInstr = Construction::getPhiOperandDefinition(useInstr, predecessorBlock, overlap) } @@ -46,24 +50,16 @@ private predicate isInCycle(Instruction instr) { * A source operand of an `Instruction`. The operand represents a value consumed by the instruction. */ class Operand extends TOperand { - string toString() { - result = "Operand" - } + string toString() { result = "Operand" } - final Language::Location getLocation() { - result = getUse().getLocation() - } + final Language::Location getLocation() { result = getUse().getLocation() } - final IRFunction getEnclosingIRFunction() { - result = getUse().getEnclosingIRFunction() - } + final IRFunction getEnclosingIRFunction() { result = getUse().getEnclosingIRFunction() } /** * Gets the `Instruction` that consumes this operand. */ - Instruction getUse() { - none() - } + Instruction getUse() { none() } /** * Gets the `Instruction` whose result is the value of the operand. Unlike @@ -71,9 +67,7 @@ class Operand extends TOperand { * means that the resulting instruction may only _partially_ or _potentially_ * be the value of this operand. */ - Instruction getAnyDef() { - none() - } + Instruction getAnyDef() { none() } /** * Gets the `Instruction` whose result is the value of the operand. Unlike @@ -91,10 +85,7 @@ class Operand extends TOperand { * * Gets the `Instruction` that consumes this operand. */ - deprecated - final Instruction getUseInstruction() { - result = getUse() - } + deprecated final Instruction getUseInstruction() { result = getUse() } /** * DEPRECATED: use `getAnyDef` or `getDef`. The exact replacement for this @@ -103,31 +94,22 @@ class Operand extends TOperand { * * Gets the `Instruction` whose result is the value of the operand. */ - deprecated - final Instruction getDefinitionInstruction() { - result = getAnyDef() - } + deprecated final Instruction getDefinitionInstruction() { result = getAnyDef() } /** * Gets the overlap relationship between the operand's definition and its use. */ - Overlap getDefinitionOverlap() { - none() - } + Overlap getDefinitionOverlap() { none() } /** * Holds if the result of the definition instruction does not exactly overlap this use. */ - final predicate isDefinitionInexact() { - not getDefinitionOverlap() instanceof MustExactlyOverlap - } + final predicate isDefinitionInexact() { not getDefinitionOverlap() instanceof MustExactlyOverlap } /** * Gets a prefix to use when dumping the operand in an operand list. */ - string getDumpLabel() { - result = "" - } + string getDumpLabel() { result = "" } /** * Gets a string describing this operand, suitable for display in IR dumps. This consists of the @@ -146,18 +128,13 @@ class Operand extends TOperand { * the empty string. */ private string getInexactSpecifier() { - if isDefinitionInexact() then - result = "~" - else - result = "" + if isDefinitionInexact() then result = "~" else result = "" } /** * Get the order in which the operand should be sorted in the operand list. */ - int getDumpSortOrder() { - result = -1 - } + int getDumpSortOrder() { result = -1 } /** * Gets the type of the value consumed by this operand. This is usually the same as the @@ -166,9 +143,7 @@ class Operand extends TOperand { * the definition type, such as in the case of a partial read or a read from a pointer that * has been cast to a different type. */ - Language::Type getType() { - result = getAnyDef().getResultType() - } + Language::Type getType() { result = getAnyDef().getResultType() } /** * Holds if the value consumed by this operand is a glvalue. If this @@ -177,17 +152,13 @@ class Operand extends TOperand { * not hold, the value of the operand represents a value whose type is * given by `getResultType()`. */ - predicate isGLValue() { - getAnyDef().isGLValue() - } + predicate isGLValue() { getAnyDef().isGLValue() } /** * Gets the size of the value consumed by this operand, in bytes. If the operand does not have * a known constant size, this predicate does not hold. */ - int getSize() { - result = Language::getTypeSize(getType()) - } + int getSize() { result = Language::getTypeSize(getType()) } } /** @@ -207,9 +178,7 @@ class MemoryOperand extends Operand { /** * Gets the kind of memory access performed by the operand. */ - MemoryAccessKind getMemoryAccess() { - none() - } + MemoryAccessKind getMemoryAccess() { none() } /** * Returns the operand that holds the memory address from which the current operand loads its @@ -227,7 +196,9 @@ class MemoryOperand extends Operand { */ class NonPhiOperand extends Operand { Instruction useInstr; + Instruction defInstr; + OperandTag tag; NonPhiOperand() { @@ -235,25 +206,15 @@ class NonPhiOperand extends Operand { this = TNonPhiMemoryOperand(useInstr, tag, defInstr, _) } - override final Instruction getUse() { - result = useInstr - } + final override Instruction getUse() { result = useInstr } - override final Instruction getAnyDef() { - result = defInstr - } + final override Instruction getAnyDef() { result = defInstr } - override final string getDumpLabel() { - result = tag.getLabel() - } + final override string getDumpLabel() { result = tag.getLabel() } - override final int getDumpSortOrder() { - result = tag.getSortOrder() - } + final override int getDumpSortOrder() { result = tag.getSortOrder() } - final OperandTag getOperandTag() { - result = tag - } + final OperandTag getOperandTag() { result = tag } } /** @@ -262,7 +223,7 @@ class NonPhiOperand extends Operand { class RegisterOperand extends NonPhiOperand, TRegisterOperand { override RegisterOperandTag tag; - override final Overlap getDefinitionOverlap() { + final override Overlap getDefinitionOverlap() { // All register results overlap exactly with their uses. result instanceof MustExactlyOverlap } @@ -270,21 +231,18 @@ class RegisterOperand extends NonPhiOperand, TRegisterOperand { class NonPhiMemoryOperand extends NonPhiOperand, MemoryOperand, TNonPhiMemoryOperand { override MemoryOperandTag tag; + Overlap overlap; - NonPhiMemoryOperand() { - this = TNonPhiMemoryOperand(useInstr, tag, defInstr, overlap) - } + NonPhiMemoryOperand() { this = TNonPhiMemoryOperand(useInstr, tag, defInstr, overlap) } - override final Overlap getDefinitionOverlap() { - result = overlap - } + final override Overlap getDefinitionOverlap() { result = overlap } } class TypedOperand extends NonPhiMemoryOperand { override TypedOperandTag tag; - override final Language::Type getType() { + final override Language::Type getType() { result = Construction::getInstructionOperandType(useInstr, tag) } } @@ -296,9 +254,7 @@ class TypedOperand extends NonPhiMemoryOperand { class AddressOperand extends RegisterOperand { override AddressOperandTag tag; - override string toString() { - result = "Address" - } + override string toString() { result = "Address" } } /** @@ -308,13 +264,9 @@ class AddressOperand extends RegisterOperand { class LoadOperand extends TypedOperand { override LoadOperandTag tag; - override string toString() { - result = "Load" - } + override string toString() { result = "Load" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof IndirectMemoryAccess } } /** @@ -323,9 +275,7 @@ class LoadOperand extends TypedOperand { class StoreValueOperand extends RegisterOperand { override StoreValueOperandTag tag; - override string toString() { - result = "StoreValue" - } + override string toString() { result = "StoreValue" } } /** @@ -334,9 +284,7 @@ class StoreValueOperand extends RegisterOperand { class UnaryOperand extends RegisterOperand { override UnaryOperandTag tag; - override string toString() { - result = "Unary" - } + override string toString() { result = "Unary" } } /** @@ -345,9 +293,7 @@ class UnaryOperand extends RegisterOperand { class LeftOperand extends RegisterOperand { override LeftOperandTag tag; - override string toString() { - result = "Left" - } + override string toString() { result = "Left" } } /** @@ -356,9 +302,7 @@ class LeftOperand extends RegisterOperand { class RightOperand extends RegisterOperand { override RightOperandTag tag; - override string toString() { - result = "Right" - } + override string toString() { result = "Right" } } /** @@ -367,9 +311,7 @@ class RightOperand extends RegisterOperand { class ConditionOperand extends RegisterOperand { override ConditionOperandTag tag; - override string toString() { - result = "Condition" - } + override string toString() { result = "Condition" } } /** @@ -379,13 +321,9 @@ class ConditionOperand extends RegisterOperand { class UnmodeledUseOperand extends NonPhiMemoryOperand { override UnmodeledUseOperandTag tag; - override string toString() { - result = "UnmodeledUse" - } + override string toString() { result = "UnmodeledUse" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof UnmodeledMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof UnmodeledMemoryAccess } } /** @@ -394,9 +332,7 @@ class UnmodeledUseOperand extends NonPhiMemoryOperand { class CallTargetOperand extends RegisterOperand { override CallTargetOperandTag tag; - override string toString() { - result = "CallTarget" - } + override string toString() { result = "CallTarget" } } /** @@ -415,9 +351,7 @@ class ArgumentOperand extends RegisterOperand { class ThisArgumentOperand extends ArgumentOperand { override ThisArgumentOperandTag tag; - override string toString() { - result = "ThisArgument" - } + override string toString() { result = "ThisArgument" } } /** @@ -425,32 +359,26 @@ class ThisArgumentOperand extends ArgumentOperand { */ class PositionalArgumentOperand extends ArgumentOperand { override PositionalArgumentOperandTag tag; + int argIndex; - PositionalArgumentOperand() { - argIndex = tag.getArgIndex() - } + PositionalArgumentOperand() { argIndex = tag.getArgIndex() } - override string toString() { - result = "Arg(" + argIndex + ")" - } + override string toString() { result = "Arg(" + argIndex + ")" } /** * Gets the zero-based index of the argument. */ - final int getIndex() { - result = argIndex - } + final int getIndex() { result = argIndex } } class SideEffectOperand extends TypedOperand { override SideEffectOperandTag tag; - override final int getSize() { - if getType() instanceof Language::UnknownType then - result = Construction::getInstructionOperandSize(useInstr, tag) - else - result = Language::getTypeSize(getType()) + final override int getSize() { + if getType() instanceof Language::UnknownType + then result = Construction::getInstructionOperandSize(useInstr, tag) + else result = Language::getTypeSize(getType()) } override MemoryAccessKind getMemoryAccess() { @@ -485,48 +413,35 @@ class SideEffectOperand extends TypedOperand { */ class PhiInputOperand extends MemoryOperand, TPhiOperand { PhiInstruction useInstr; + Instruction defInstr; + IRBlock predecessorBlock; + Overlap overlap; - PhiInputOperand() { - this = TPhiOperand(useInstr, defInstr, predecessorBlock, overlap) - } + PhiInputOperand() { this = TPhiOperand(useInstr, defInstr, predecessorBlock, overlap) } - override string toString() { - result = "Phi" - } + override string toString() { result = "Phi" } - override final PhiInstruction getUse() { - result = useInstr - } + final override PhiInstruction getUse() { result = useInstr } - override final Instruction getAnyDef() { - result = defInstr - } + final override Instruction getAnyDef() { result = defInstr } - override final Overlap getDefinitionOverlap() { - result = overlap - } + final override Overlap getDefinitionOverlap() { result = overlap } - override final int getDumpSortOrder() { - result = 11 + getPredecessorBlock().getDisplayIndex() - } + final override int getDumpSortOrder() { result = 11 + getPredecessorBlock().getDisplayIndex() } - override final string getDumpLabel() { + final override string getDumpLabel() { result = "from " + getPredecessorBlock().getDisplayIndex().toString() + ":" } /** * Gets the predecessor block from which this value comes. */ - final IRBlock getPredecessorBlock() { - result = predecessorBlock - } + final IRBlock getPredecessorBlock() { result = predecessorBlock } - override final MemoryAccessKind getMemoryAccess() { - result instanceof PhiMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof PhiMemoryAccess } } /** @@ -535,27 +450,18 @@ class PhiInputOperand extends MemoryOperand, TPhiOperand { class ChiTotalOperand extends NonPhiMemoryOperand { override ChiTotalOperandTag tag; - override string toString() { - result = "ChiTotal" - } + override string toString() { result = "ChiTotal" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof ChiTotalMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof ChiTotalMemoryAccess } } - /** * The partial operand of a Chi node, representing the value being written to part of the memory. */ class ChiPartialOperand extends NonPhiMemoryOperand { override ChiPartialOperandTag tag; - override string toString() { - result = "ChiPartial" - } + override string toString() { result = "ChiPartial" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof ChiPartialMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof ChiPartialMemoryAccess } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/PrintIR.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/PrintIR.qll index 4bad5e3fd3e..c24756a2212 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/PrintIR.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/PrintIR.qll @@ -9,23 +9,17 @@ private newtype TPrintIRConfiguration = MkPrintIRConfiguration() * The query can extend this class to control which functions are printed. */ class PrintIRConfiguration extends TPrintIRConfiguration { - string toString() { - result = "PrintIRConfiguration" - } + string toString() { result = "PrintIRConfiguration" } /** * Holds if the IR for `func` should be printed. By default, holds for all * functions. */ - predicate shouldPrintFunction(Language::Function func) { - any() - } + predicate shouldPrintFunction(Language::Function func) { any() } } private predicate shouldPrintFunction(Language::Function func) { - exists(PrintIRConfiguration config | - config.shouldPrintFunction(func) - ) + exists(PrintIRConfiguration config | config.shouldPrintFunction(func)) } /** @@ -38,27 +32,17 @@ private class FilteredIRConfiguration extends IRConfiguration { } private string getAdditionalInstructionProperty(Instruction instr, string key) { - exists(IRPropertyProvider provider | - result = provider.getInstructionProperty(instr, key) - ) + exists(IRPropertyProvider provider | result = provider.getInstructionProperty(instr, key)) } private string getAdditionalBlockProperty(IRBlock block, string key) { - exists(IRPropertyProvider provider | - result = provider.getBlockProperty(block, key) - ) + exists(IRPropertyProvider provider | result = provider.getBlockProperty(block, key)) } private newtype TPrintableIRNode = - TPrintableIRFunction(IRFunction irFunc) { - shouldPrintFunction(irFunc.getFunction()) - } or - TPrintableIRBlock(IRBlock block) { - shouldPrintFunction(block.getEnclosingFunction()) - } or - TPrintableInstruction(Instruction instr) { - shouldPrintFunction(instr.getEnclosingFunction()) - } + TPrintableIRFunction(IRFunction irFunc) { shouldPrintFunction(irFunc.getFunction()) } or + TPrintableIRBlock(IRBlock block) { shouldPrintFunction(block.getEnclosingFunction()) } or + TPrintableInstruction(Instruction instr) { shouldPrintFunction(instr.getEnclosingFunction()) } /** * A node to be emitted in the IR graph. @@ -85,29 +69,28 @@ abstract class PrintableIRNode extends TPrintableIRNode { * Gets the parent of this node. */ abstract PrintableIRNode getParent(); - + /** * Gets the kind of graph represented by this node ("graph" or "tree"). */ - string getGraphKind() { - none() - } + string getGraphKind() { none() } /** * Holds if this node should always be rendered as text, even in a graphical * viewer. */ - predicate forceText() { - none() - } + predicate forceText() { none() } /** * Gets the value of the node property with the specified key. */ string getProperty(string key) { - key = "semmle.label" and result = getLabel() or - key = "semmle.order" and result = getOrder().toString() or - key = "semmle.graphKind" and result = getGraphKind() or + key = "semmle.label" and result = getLabel() + or + key = "semmle.order" and result = getOrder().toString() + or + key = "semmle.graphKind" and result = getGraphKind() + or key = "semmle.forceText" and forceText() and result = "true" } } @@ -118,37 +101,28 @@ abstract class PrintableIRNode extends TPrintableIRNode { class PrintableIRFunction extends PrintableIRNode, TPrintableIRFunction { IRFunction irFunc; - PrintableIRFunction() { - this = TPrintableIRFunction(irFunc) - } + PrintableIRFunction() { this = TPrintableIRFunction(irFunc) } - override string toString() { - result = irFunc.toString() - } + override string toString() { result = irFunc.toString() } - override Language::Location getLocation() { - result = irFunc.getLocation() - } + override Language::Location getLocation() { result = irFunc.getLocation() } - override string getLabel() { - result = Language::getIdentityString(irFunc.getFunction()) - } + override string getLabel() { result = Language::getIdentityString(irFunc.getFunction()) } override int getOrder() { this = rank[result + 1](PrintableIRFunction orderedFunc, Language::Location location | - location = orderedFunc.getIRFunction().getLocation() | - orderedFunc order by location.getFile().getAbsolutePath(), location.getStartLine(), - location.getStartColumn(), orderedFunc.getLabel() - ) + location = orderedFunc.getIRFunction().getLocation() + | + orderedFunc + order by + location.getFile().getAbsolutePath(), location.getStartLine(), location.getStartColumn(), + orderedFunc.getLabel() + ) } - override final PrintableIRNode getParent() { - none() - } + final override PrintableIRNode getParent() { none() } - final IRFunction getIRFunction() { - result = irFunc - } + final IRFunction getIRFunction() { result = irFunc } } /** @@ -157,35 +131,21 @@ class PrintableIRFunction extends PrintableIRNode, TPrintableIRFunction { class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock { IRBlock block; - PrintableIRBlock() { - this = TPrintableIRBlock(block) - } + PrintableIRBlock() { this = TPrintableIRBlock(block) } - override string toString() { - result = getLabel() - } + override string toString() { result = getLabel() } - override Language::Location getLocation() { - result = block.getLocation() - } + override Language::Location getLocation() { result = block.getLocation() } - override string getLabel() { - result = "Block " + block.getDisplayIndex().toString() - } + override string getLabel() { result = "Block " + block.getDisplayIndex().toString() } - override int getOrder() { - result = block.getDisplayIndex() - } + override int getOrder() { result = block.getDisplayIndex() } - override final string getGraphKind() { - result = "tree" - } + final override string getGraphKind() { result = "tree" } - override final predicate forceText() { - any() - } + final override predicate forceText() { any() } - override final PrintableIRFunction getParent() { + final override PrintableIRFunction getParent() { result.getIRFunction() = block.getEnclosingIRFunction() } @@ -194,9 +154,7 @@ class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock { result = getAdditionalBlockProperty(block, key) } - final IRBlock getBlock() { - result = block - } + final IRBlock getBlock() { result = block } } /** @@ -205,45 +163,35 @@ class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock { class PrintableInstruction extends PrintableIRNode, TPrintableInstruction { Instruction instr; - PrintableInstruction() { - this = TPrintableInstruction(instr) - } + PrintableInstruction() { this = TPrintableInstruction(instr) } - override string toString() { - result = instr.toString() - } + override string toString() { result = instr.toString() } - override Language::Location getLocation() { - result = instr.getLocation() - } + override Language::Location getLocation() { result = instr.getLocation() } override string getLabel() { exists(IRBlock block | instr = block.getAnInstruction() and - exists(string resultString, string operationString, string operandsString, - int resultWidth, int operationWidth | + exists( + string resultString, string operationString, string operandsString, int resultWidth, + int operationWidth + | resultString = instr.getResultString() and operationString = instr.getOperationString() and operandsString = instr.getOperandsString() and columnWidths(block, resultWidth, operationWidth) and - result = resultString + getPaddingString(resultWidth - resultString.length()) + - " = " + operationString + getPaddingString(operationWidth - operationString.length()) + - " : " + operandsString + result = resultString + getPaddingString(resultWidth - resultString.length()) + " = " + + operationString + getPaddingString(operationWidth - operationString.length()) + " : " + + operandsString ) ) } - override int getOrder() { - result = instr.getDisplayIndexInBlock() - } + override int getOrder() { result = instr.getDisplayIndexInBlock() } - override final PrintableIRBlock getParent() { - result.getBlock() = instr.getBlock() - } + final override PrintableIRBlock getParent() { result.getBlock() = instr.getBlock() } - final Instruction getInstruction() { - result = instr - } + final Instruction getInstruction() { result = instr } override string getProperty(string key) { result = PrintableIRNode.super.getProperty(key) or @@ -253,19 +201,26 @@ class PrintableInstruction extends PrintableIRNode, TPrintableInstruction { private predicate columnWidths(IRBlock block, int resultWidth, int operationWidth) { resultWidth = max(Instruction instr | instr.getBlock() = block | instr.getResultString().length()) and - operationWidth = max(Instruction instr | instr.getBlock() = block | instr.getOperationString().length()) + operationWidth = max(Instruction instr | + instr.getBlock() = block + | + instr.getOperationString().length() + ) } private int maxColumnWidth() { result = max(Instruction instr, int width | - width = instr.getResultString().length() or - width = instr.getOperationString().length() or - width = instr.getOperandsString().length() | - width) + width = instr.getResultString().length() or + width = instr.getOperationString().length() or + width = instr.getOperandsString().length() + | + width + ) } private string getPaddingString(int n) { - n = 0 and result = "" or + n = 0 and result = "" + or n > 0 and n <= maxColumnWidth() and result = getPaddingString(n - 1) + " " } @@ -275,9 +230,10 @@ query predicate nodes(PrintableIRNode node, string key, string value) { private int getSuccessorIndex(IRBlock pred, IRBlock succ) { succ = rank[result + 1](IRBlock aSucc, EdgeKind kind | - aSucc = pred.getSuccessor(kind) | - aSucc order by kind.toString() - ) + aSucc = pred.getSuccessor(kind) + | + aSucc order by kind.toString() + ) } query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key, string value) { @@ -291,11 +247,10 @@ query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key, if predBlock.getBackEdgeSuccessor(kind) = succBlock then value = kind.toString() + " (back edge)" else value = kind.toString() - ) or - ( - key = "semmle.order" and - value = getSuccessorIndex(predBlock, succBlock).toString() ) + or + key = "semmle.order" and + value = getSuccessorIndex(predBlock, succBlock).toString() ) ) } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/internal/OperandTag.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/internal/OperandTag.qll index ae32229522f..d1172670a79 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/internal/OperandTag.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/internal/OperandTag.qll @@ -13,14 +13,10 @@ private newtype TOperandTag = TUnmodeledUseOperand() or TCallTargetOperand() or TThisArgumentOperand() or - TPositionalArgumentOperand(int argIndex) { - Language::hasPositionalArgIndex(argIndex) - } or + TPositionalArgumentOperand(int argIndex) { Language::hasPositionalArgIndex(argIndex) } or TChiTotalOperand() or TChiPartialOperand() or - TAsmOperand(int index) { - Language::hasAsmOperandIndex(index) - } + TAsmOperand(int index) { Language::hasAsmOperandIndex(index) } /** * Identifies the kind of operand on an instruction. Each `Instruction` has at @@ -28,291 +24,195 @@ private newtype TOperandTag = * an `Instruction` is determined by the instruction's opcode. */ abstract class OperandTag extends TOperandTag { - abstract string toString(); + abstract string toString(); - abstract int getSortOrder(); + abstract int getSortOrder(); - string getLabel() { - result = "" - } + string getLabel() { result = "" } } /** * An operand that consumes a memory result (e.g. the `LoadOperand` on a `Load` instruction). */ -abstract class MemoryOperandTag extends OperandTag { -} +abstract class MemoryOperandTag extends OperandTag { } /** * An operand that consumes a register (non-memory) result. */ -abstract class RegisterOperandTag extends OperandTag { -} +abstract class RegisterOperandTag extends OperandTag { } /** * A memory operand whose type may be different from the result type of its definition instruction. */ -abstract class TypedOperandTag extends MemoryOperandTag { -} +abstract class TypedOperandTag extends MemoryOperandTag { } // Note: individual subtypes are listed in the order that the operands should // appear in the operand list of the instruction when printing. - /** * The address operand of an instruction that loads or stores a value from * memory (e.g. `Load`, `Store`, `InitializeParameter`, `IndirectReadSideEffect`). */ class AddressOperandTag extends RegisterOperandTag, TAddressOperand { - override final string toString() { - result = "Address" - } + final override string toString() { result = "Address" } - override final int getSortOrder() { - result = 0 - } - - override final string getLabel() { - result = "&:" - } + final override int getSortOrder() { result = 0 } + + final override string getLabel() { result = "&:" } } -AddressOperandTag addressOperand() { - result = TAddressOperand() -} +AddressOperandTag addressOperand() { result = TAddressOperand() } /** * The buffer size operand of an instruction that represents a read or write of * a buffer. */ class BufferSizeOperand extends RegisterOperandTag, TBufferSizeOperand { - override final string toString() { - result = "BufferSize" - } + final override string toString() { result = "BufferSize" } - override final int getSortOrder() { - result = 1 - } + final override int getSortOrder() { result = 1 } } /** * The operand representing the read side effect of a `SideEffectInstruction`. */ class SideEffectOperandTag extends TypedOperandTag, TSideEffectOperand { - override final string toString() { - result = "SideEffect" - } + final override string toString() { result = "SideEffect" } - override final int getSortOrder() { - result = 2 - } + final override int getSortOrder() { result = 2 } } -SideEffectOperandTag sideEffectOperand() { - result = TSideEffectOperand() -} +SideEffectOperandTag sideEffectOperand() { result = TSideEffectOperand() } /** * The source value operand of an instruction that loads a value from memory (e.g. `Load`, * `ReturnValue`, `ThrowValue`). */ class LoadOperandTag extends TypedOperandTag, TLoadOperand { - override final string toString() { - result = "Load" - } + final override string toString() { result = "Load" } - override final int getSortOrder() { - result = 3 - } + final override int getSortOrder() { result = 3 } } -LoadOperandTag loadOperand() { - result = TLoadOperand() -} +LoadOperandTag loadOperand() { result = TLoadOperand() } /** * The source value operand of a `Store` instruction. */ class StoreValueOperandTag extends RegisterOperandTag, TStoreValueOperand { - override final string toString() { - result = "StoreValue" - } + final override string toString() { result = "StoreValue" } - override final int getSortOrder() { - result = 4 - } + final override int getSortOrder() { result = 4 } } -StoreValueOperandTag storeValueOperand() { - result = TStoreValueOperand() -} +StoreValueOperandTag storeValueOperand() { result = TStoreValueOperand() } /** * The sole operand of a unary instruction (e.g. `Convert`, `Negate`, `Copy`). */ class UnaryOperandTag extends RegisterOperandTag, TUnaryOperand { - override final string toString() { - result = "Unary" - } + final override string toString() { result = "Unary" } - override final int getSortOrder() { - result = 5 - } + final override int getSortOrder() { result = 5 } } -UnaryOperandTag unaryOperand() { - result = TUnaryOperand() -} +UnaryOperandTag unaryOperand() { result = TUnaryOperand() } /** * The left operand of a binary instruction (e.g. `Add`, `CompareEQ`). */ class LeftOperandTag extends RegisterOperandTag, TLeftOperand { - override final string toString() { - result = "Left" - } + final override string toString() { result = "Left" } - override final int getSortOrder() { - result = 6 - } + final override int getSortOrder() { result = 6 } } -LeftOperandTag leftOperand() { - result = TLeftOperand() -} +LeftOperandTag leftOperand() { result = TLeftOperand() } /** * The right operand of a binary instruction (e.g. `Add`, `CompareEQ`). */ class RightOperandTag extends RegisterOperandTag, TRightOperand { - override final string toString() { - result = "Right" - } + final override string toString() { result = "Right" } - override final int getSortOrder() { - result = 7 - } + final override int getSortOrder() { result = 7 } } -RightOperandTag rightOperand() { - result = TRightOperand() -} +RightOperandTag rightOperand() { result = TRightOperand() } /** * The condition operand of a `ConditionalBranch` or `Switch` instruction. */ class ConditionOperandTag extends RegisterOperandTag, TConditionOperand { - override final string toString() { - result = "Condition" - } + final override string toString() { result = "Condition" } - override final int getSortOrder() { - result = 8 - } + final override int getSortOrder() { result = 8 } } -ConditionOperandTag conditionOperand() { - result = TConditionOperand() -} +ConditionOperandTag conditionOperand() { result = TConditionOperand() } /** * An operand of the special `UnmodeledUse` instruction, representing a value * whose set of uses is unknown. */ class UnmodeledUseOperandTag extends MemoryOperandTag, TUnmodeledUseOperand { - override final string toString() { - result = "UnmodeledUse" - } + final override string toString() { result = "UnmodeledUse" } - override final int getSortOrder() { - result = 9 - } + final override int getSortOrder() { result = 9 } } -UnmodeledUseOperandTag unmodeledUseOperand() { - result = TUnmodeledUseOperand() -} +UnmodeledUseOperandTag unmodeledUseOperand() { result = TUnmodeledUseOperand() } /** * The operand representing the target function of an `Call` instruction. */ class CallTargetOperandTag extends RegisterOperandTag, TCallTargetOperand { - override final string toString() { - result = "CallTarget" - } + final override string toString() { result = "CallTarget" } - override final int getSortOrder() { - result = 10 - } + final override int getSortOrder() { result = 10 } - override final string getLabel() { - result = "func:" - } + final override string getLabel() { result = "func:" } } -CallTargetOperandTag callTargetOperand() { - result = TCallTargetOperand() -} +CallTargetOperandTag callTargetOperand() { result = TCallTargetOperand() } /** * An operand representing an argument to a function call. This includes both * positional arguments (represented by `PositionalArgumentOperand`) and the * implicit `this` argument, if any (represented by `ThisArgumentOperand`). */ -abstract class ArgumentOperandTag extends RegisterOperandTag { -} +abstract class ArgumentOperandTag extends RegisterOperandTag { } /** * An operand representing the implicit 'this' argument to a member function * call. */ class ThisArgumentOperandTag extends ArgumentOperandTag, TThisArgumentOperand { - ThisArgumentOperandTag() { - this = TThisArgumentOperand() - } + ThisArgumentOperandTag() { this = TThisArgumentOperand() } - override final string toString() { - result = "Arg(this)" - } + final override string toString() { result = "Arg(this)" } - override final int getSortOrder() { - result = 11 - } + final override int getSortOrder() { result = 11 } - override final string getLabel() { - result = "this:" - } + final override string getLabel() { result = "this:" } } -ThisArgumentOperandTag thisArgumentOperand() { - result = TThisArgumentOperand() -} +ThisArgumentOperandTag thisArgumentOperand() { result = TThisArgumentOperand() } /** * An operand representing an argument to a function call. */ -class PositionalArgumentOperandTag extends ArgumentOperandTag, - TPositionalArgumentOperand { +class PositionalArgumentOperandTag extends ArgumentOperandTag, TPositionalArgumentOperand { int argIndex; - PositionalArgumentOperandTag() { - this = TPositionalArgumentOperand(argIndex) - } + PositionalArgumentOperandTag() { this = TPositionalArgumentOperand(argIndex) } - override final string toString() { - result = "Arg(" + argIndex + ")" - } + final override string toString() { result = "Arg(" + argIndex + ")" } - override final int getSortOrder() { - result = 12 + argIndex - } + final override int getSortOrder() { result = 12 + argIndex } - override final string getLabel() { - result = argIndex.toString() + ":" - } - - final int getArgIndex() { - result = argIndex - } + final override string getLabel() { result = argIndex.toString() + ":" } + + final int getArgIndex() { result = argIndex } } PositionalArgumentOperandTag positionalArgumentOperand(int argIndex) { @@ -320,61 +220,35 @@ PositionalArgumentOperandTag positionalArgumentOperand(int argIndex) { } class ChiTotalOperandTag extends MemoryOperandTag, TChiTotalOperand { - override final string toString() { - result = "ChiTotal" - } + final override string toString() { result = "ChiTotal" } - override final int getSortOrder() { - result = 13 - } + final override int getSortOrder() { result = 13 } - override final string getLabel() { - result = "total:" - } + final override string getLabel() { result = "total:" } } -ChiTotalOperandTag chiTotalOperand() { - result = TChiTotalOperand() -} +ChiTotalOperandTag chiTotalOperand() { result = TChiTotalOperand() } class ChiPartialOperandTag extends MemoryOperandTag, TChiPartialOperand { - override final string toString() { - result = "ChiPartial" - } + final override string toString() { result = "ChiPartial" } - override final int getSortOrder() { - result = 14 - } + final override int getSortOrder() { result = 14 } - override final string getLabel() { - result = "partial:" - } + final override string getLabel() { result = "partial:" } } -ChiPartialOperandTag chiPartialOperand() { - result = TChiPartialOperand() -} +ChiPartialOperandTag chiPartialOperand() { result = TChiPartialOperand() } class AsmOperandTag extends RegisterOperandTag, TAsmOperand { int index; - AsmOperandTag() { - this = TAsmOperand(index) - } + AsmOperandTag() { this = TAsmOperand(index) } - override final string toString() { - result = "AsmOperand(" + index + ")" - } + final override string toString() { result = "AsmOperand(" + index + ")" } - override final int getSortOrder() { - result = 15 + index - } + final override int getSortOrder() { result = 15 + index } - override final string getLabel() { - result = index.toString() + ":" - } + final override string getLabel() { result = index.toString() + ":" } } -AsmOperandTag asmOperand(int index) { - result = TAsmOperand(index) -} \ No newline at end of file +AsmOperandTag asmOperand(int index) { result = TAsmOperand(index) } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/internal/TIRVariable.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/internal/TIRVariable.qll index c0edfe0e553..908a208b83a 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/internal/TIRVariable.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/internal/TIRVariable.qll @@ -2,11 +2,11 @@ private import TIRVariableInternal private import Imports::TempVariableTag newtype TIRVariable = - TIRUserVariable(Language::Variable var, Language::Type type, - Language::Function func) { + TIRUserVariable(Language::Variable var, Language::Type type, Language::Function func) { Construction::hasUserVariable(func, var, type) } or - TIRTempVariable(Language::Function func, Language::AST ast, TempVariableTag tag, - Language::Type type) { + TIRTempVariable( + Language::Function func, Language::AST ast, TempVariableTag tag, Language::Type type + ) { Construction::hasTempVariable(func, ast, tag, type) } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IR.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IR.qll index 5bc9493f4ab..278040f8ab8 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IR.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IR.qll @@ -15,21 +15,15 @@ private newtype TIRPropertyProvider = MkIRPropertyProvider() * single instance of this class to specify the additional properties computed by the library. */ class IRPropertyProvider extends TIRPropertyProvider { - string toString() { - result = "IRPropertyProvider" - } + string toString() { result = "IRPropertyProvider" } /** * Gets the value of the property named `key` for the specified instruction. */ - string getInstructionProperty(Instruction instruction, string key) { - none() - } + string getInstructionProperty(Instruction instruction, string key) { none() } /** * Gets the value of the property named `key` for the specified block. */ - string getBlockProperty(IRBlock block, string key) { - none() - } + string getBlockProperty(IRBlock block, string key) { none() } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRBlock.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRBlock.qll index c62a2dbc5ea..e0322a00e15 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRBlock.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRBlock.qll @@ -16,32 +16,25 @@ private import Cached * Most consumers should use the class `IRBlock`. */ class IRBlockBase extends TIRBlock { - final string toString() { - result = getFirstInstruction(this).toString() - } + final string toString() { result = getFirstInstruction(this).toString() } + + final Language::Location getLocation() { result = getFirstInstruction().getLocation() } + + final string getUniqueId() { result = getFirstInstruction(this).getUniqueId() } - final Language::Location getLocation() { - result = getFirstInstruction().getLocation() - } - - final string getUniqueId() { - result = getFirstInstruction(this).getUniqueId() - } - /** * Gets the zero-based index of the block within its function. This is used * by debugging and printing code only. */ int getDisplayIndex() { this = rank[result + 1](IRBlock funcBlock | - funcBlock.getEnclosingFunction() = getEnclosingFunction() | - funcBlock order by funcBlock.getUniqueId() - ) + funcBlock.getEnclosingFunction() = getEnclosingFunction() + | + funcBlock order by funcBlock.getUniqueId() + ) } - final Instruction getInstruction(int index) { - result = getInstruction(this, index) - } + final Instruction getInstruction(int index) { result = getInstruction(this, index) } final PhiInstruction getAPhiInstruction() { Construction::getPhiInstructionBlockStart(result) = getFirstInstruction() @@ -52,17 +45,11 @@ class IRBlockBase extends TIRBlock { result = getAPhiInstruction() } - final Instruction getFirstInstruction() { - result = getFirstInstruction(this) - } + final Instruction getFirstInstruction() { result = getFirstInstruction(this) } - final Instruction getLastInstruction() { - result = getInstruction(getInstructionCount() - 1) - } + final Instruction getLastInstruction() { result = getInstruction(getInstructionCount() - 1) } - final int getInstructionCount() { - result = getInstructionCount(this) - } + final int getInstructionCount() { result = getInstructionCount(this) } final IRFunction getEnclosingIRFunction() { result = getFirstInstruction(this).getEnclosingIRFunction() @@ -79,40 +66,26 @@ class IRBlockBase extends TIRBlock { * instruction of another block. */ class IRBlock extends IRBlockBase { - final IRBlock getASuccessor() { - blockSuccessor(this, result) - } + final IRBlock getASuccessor() { blockSuccessor(this, result) } - final IRBlock getAPredecessor() { - blockSuccessor(result, this) - } + final IRBlock getAPredecessor() { blockSuccessor(result, this) } - final IRBlock getSuccessor(EdgeKind kind) { - blockSuccessor(this, result, kind) - } + final IRBlock getSuccessor(EdgeKind kind) { blockSuccessor(this, result, kind) } - final IRBlock getBackEdgeSuccessor(EdgeKind kind) { - backEdgeSuccessor(this, result, kind) - } + final IRBlock getBackEdgeSuccessor(EdgeKind kind) { backEdgeSuccessor(this, result, kind) } - final predicate immediatelyDominates(IRBlock block) { - blockImmediatelyDominates(this, block) - } + final predicate immediatelyDominates(IRBlock block) { blockImmediatelyDominates(this, block) } - final predicate strictlyDominates(IRBlock block) { - blockImmediatelyDominates+(this, block) - } + final predicate strictlyDominates(IRBlock block) { blockImmediatelyDominates+(this, block) } - final predicate dominates(IRBlock block) { - strictlyDominates(block) or this = block - } + final predicate dominates(IRBlock block) { strictlyDominates(block) or this = block } pragma[noinline] final IRBlock dominanceFrontier() { dominates(result.getAPredecessor()) and not strictlyDominates(result) } - + /** * Holds if this block is reachable from the entry point of its function */ @@ -125,22 +98,21 @@ class IRBlock extends IRBlockBase { private predicate startsBasicBlock(Instruction instr) { not instr instanceof PhiInstruction and ( - count(Instruction predecessor | - instr = predecessor.getASuccessor() - ) != 1 or // Multiple predecessors or no predecessor + count(Instruction predecessor | instr = predecessor.getASuccessor()) != 1 // Multiple predecessors or no predecessor + or exists(Instruction predecessor | instr = predecessor.getASuccessor() and - strictcount(Instruction other | - other = predecessor.getASuccessor() - ) > 1 - ) or // Predecessor has multiple successors + strictcount(Instruction other | other = predecessor.getASuccessor()) > 1 + ) // Predecessor has multiple successors + or exists(Instruction predecessor, EdgeKind kind | instr = predecessor.getSuccessor(kind) and not kind instanceof GotoEdge - ) or // Incoming edge is not a GotoEdge + ) // Incoming edge is not a GotoEdge + or exists(Instruction predecessor | instr = Construction::getInstructionBackEdgeSuccessor(predecessor, _) - ) // A back edge enters this instruction + ) // A back edge enters this instruction ) } @@ -148,11 +120,10 @@ private predicate isEntryBlock(TIRBlock block) { block = MkIRBlock(any(EnterFunctionInstruction enter)) } -private cached module Cached { - cached newtype TIRBlock = - MkIRBlock(Instruction firstInstr) { - startsBasicBlock(firstInstr) - } +cached +private module Cached { + cached + newtype TIRBlock = MkIRBlock(Instruction firstInstr) { startsBasicBlock(firstInstr) } /** Holds if `i2` follows `i1` in a `IRBlock`. */ private predicate adjacentInBlock(Instruction i1, Instruction i2) { @@ -165,15 +136,16 @@ private cached module Cached { shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index) /** Holds if `i` is the `index`th instruction in `block`. */ - cached Instruction getInstruction(TIRBlock block, int index) { + cached + Instruction getInstruction(TIRBlock block, int index) { result = getInstructionFromFirst(getFirstInstruction(block), index) } - cached int getInstructionCount(TIRBlock block) { - result = strictcount(getInstruction(block, _)) - } + cached + int getInstructionCount(TIRBlock block) { result = strictcount(getInstruction(block, _)) } - cached predicate blockSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { + cached + predicate blockSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { exists(Instruction predLast, Instruction succFirst | predLast = getInstruction(pred, getInstructionCount(pred) - 1) and succFirst = predLast.getSuccessor(kind) and @@ -185,7 +157,8 @@ private cached module Cached { private predicate blockIdentity(TIRBlock b1, TIRBlock b2) { b1 = b2 } pragma[noopt] - cached predicate backEdgeSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { + cached + predicate backEdgeSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { backEdgeSuccessorRaw(pred, succ, kind) or // See the QLDoc on `backEdgeSuccessorRaw`. @@ -226,14 +199,12 @@ private cached module Cached { ) } - cached predicate blockSuccessor(TIRBlock pred, TIRBlock succ) { - blockSuccessor(pred, succ, _) - } + cached + predicate blockSuccessor(TIRBlock pred, TIRBlock succ) { blockSuccessor(pred, succ, _) } - cached predicate blockImmediatelyDominates(TIRBlock dominator, TIRBlock block) = + cached + predicate blockImmediatelyDominates(TIRBlock dominator, TIRBlock block) = idominance(isEntryBlock/1, blockSuccessor/2)(_, dominator, block) } -Instruction getFirstInstruction(TIRBlock block) { - block = MkIRBlock(result) -} +Instruction getFirstInstruction(TIRBlock block) { block = MkIRBlock(result) } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRFunction.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRFunction.qll index 1dd61fb9db1..1e9c2d1d913 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRFunction.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRFunction.qll @@ -2,9 +2,7 @@ private import internal.IRInternal import Instruction private newtype TIRFunction = - MkIRFunction(Language::Function func) { - Construction::functionHasIR(func) - } + MkIRFunction(Language::Function func) { Construction::functionHasIR(func) } /** * Represents the IR for a function. @@ -12,27 +10,19 @@ private newtype TIRFunction = class IRFunction extends TIRFunction { Language::Function func; - IRFunction() { - this = MkIRFunction(func) - } + IRFunction() { this = MkIRFunction(func) } - final string toString() { - result = "IR: " + func.toString() - } + final string toString() { result = "IR: " + func.toString() } /** * Gets the function whose IR is represented. */ - final Language::Function getFunction() { - result = func - } + final Language::Function getFunction() { result = func } /** * Gets the location of the function. */ - final Language::Location getLocation() { - result = func.getLocation() - } + final Language::Location getLocation() { result = func.getLocation() } /** * Gets the entry point for this function. @@ -64,38 +54,28 @@ class IRFunction extends TIRFunction { * Gets the single return instruction for this function. */ pragma[noinline] - final ReturnInstruction getReturnInstruction() { - result.getEnclosingIRFunction() = this - } + final ReturnInstruction getReturnInstruction() { result.getEnclosingIRFunction() = this } /** * Gets the variable used to hold the return value of this function. If this * function does not return a value, this predicate does not hold. */ pragma[noinline] - final IRReturnVariable getReturnVariable() { - result.getEnclosingIRFunction() = this - } - + final IRReturnVariable getReturnVariable() { result.getEnclosingIRFunction() = this } + /** * Gets the block containing the entry point of this function. - */ + */ pragma[noinline] - final IRBlock getEntryBlock() { - result.getFirstInstruction() = getEnterFunctionInstruction() - } + final IRBlock getEntryBlock() { result.getFirstInstruction() = getEnterFunctionInstruction() } /** * Gets all instructions in this function. */ - final Instruction getAnInstruction() { - result.getEnclosingIRFunction() = this - } + final Instruction getAnInstruction() { result.getEnclosingIRFunction() = this } /** * Gets all blocks in this function. */ - final IRBlock getABlock() { - result.getEnclosingIRFunction() = this - } + final IRBlock getABlock() { result.getEnclosingIRFunction() = this } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRSanity.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRSanity.qll index 9e21452c1fb..3921472dc8e 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRSanity.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRSanity.qll @@ -1,3 +1,2 @@ private import IR import InstructionSanity - diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRVariable.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRVariable.qll index b8c6af20a60..2c1b43672fc 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRVariable.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRVariable.qll @@ -37,27 +37,21 @@ abstract class IRVariable extends TIRVariable { * within the function. */ abstract string getUniqueId(); - + /** * Gets the source location of this variable. */ - final Language::Location getLocation() { - result = getAST().getLocation() - } + final Language::Location getLocation() { result = getAST().getLocation() } /** * Gets the IR for the function that references this variable. */ - final IRFunction getEnclosingIRFunction() { - result.getFunction() = func - } + final IRFunction getEnclosingIRFunction() { result.getFunction() = func } /** * Gets the function that references this variable. */ - final Language::Function getEnclosingFunction() { - result = func - } + final Language::Function getEnclosingFunction() { result = func } } /** @@ -65,34 +59,25 @@ abstract class IRVariable extends TIRVariable { */ class IRUserVariable extends IRVariable, TIRUserVariable { Language::Variable var; + Language::Type type; - IRUserVariable() { - this = TIRUserVariable(var, type, func) - } + IRUserVariable() { this = TIRUserVariable(var, type, func) } - override final string toString() { - result = getVariable().toString() - } + final override string toString() { result = getVariable().toString() } - override final Language::AST getAST() { - result = var - } + final override Language::AST getAST() { result = var } - override final string getUniqueId() { + final override string getUniqueId() { result = getVariable().toString() + " " + getVariable().getLocation().toString() } - override final Language::Type getType() { - result = type - } + final override Language::Type getType() { result = type } /** * Gets the original user-declared variable. */ - Language::Variable getVariable() { - result = var - } + Language::Variable getVariable() { result = var } } /** @@ -100,31 +85,22 @@ class IRUserVariable extends IRVariable, TIRUserVariable { * stack. This includes all parameters, non-static local variables, and * temporary variables. */ -abstract class IRAutomaticVariable extends IRVariable { -} +abstract class IRAutomaticVariable extends IRVariable { } class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable { override Language::AutomaticVariable var; - IRAutomaticUserVariable() { - Language::isVariableAutomatic(var) - } + IRAutomaticUserVariable() { Language::isVariableAutomatic(var) } - final override Language::AutomaticVariable getVariable() { - result = var - } + final override Language::AutomaticVariable getVariable() { result = var } } class IRStaticUserVariable extends IRUserVariable { override Language::StaticVariable var; - IRStaticUserVariable() { - not Language::isVariableAutomatic(var) - } + IRStaticUserVariable() { not Language::isVariableAutomatic(var) } - final override Language::StaticVariable getVariable() { - result = var - } + final override Language::StaticVariable getVariable() { result = var } } IRTempVariable getIRTempVariable(Language::AST ast, TempVariableTag tag) { @@ -134,55 +110,39 @@ IRTempVariable getIRTempVariable(Language::AST ast, TempVariableTag tag) { class IRTempVariable extends IRVariable, IRAutomaticVariable, TIRTempVariable { Language::AST ast; + TempVariableTag tag; + Language::Type type; - IRTempVariable() { - this = TIRTempVariable(func, ast, tag, type) - } + IRTempVariable() { this = TIRTempVariable(func, ast, tag, type) } - override final Language::Type getType() { - result = type - } + final override Language::Type getType() { result = type } - override final Language::AST getAST() { - result = ast - } + final override Language::AST getAST() { result = ast } - override final string getUniqueId() { + final override string getUniqueId() { result = "Temp: " + Construction::getTempVariableUniqueId(this) } - final TempVariableTag getTag() { - result = tag - } + final TempVariableTag getTag() { result = tag } override string toString() { result = getBaseString() + ast.getLocation().getStartLine().toString() + ":" + - ast.getLocation().getStartColumn().toString() + ast.getLocation().getStartColumn().toString() } - string getBaseString() { - result = "#temp" - } + string getBaseString() { result = "#temp" } } class IRReturnVariable extends IRTempVariable { - IRReturnVariable() { - tag = ReturnValueTempVar() - } + IRReturnVariable() { tag = ReturnValueTempVar() } - override final string toString() { - result = "#return" - } + final override string toString() { result = "#return" } } class IRThrowVariable extends IRTempVariable { - IRThrowVariable() { - tag = ThrowTempVar() - } + IRThrowVariable() { tag = ThrowTempVar() } - override string getBaseString() { - result = "#throw" - } + override string getBaseString() { result = "#throw" } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll index 7b621c95dcc..ea0a3f72998 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll @@ -20,27 +20,38 @@ module InstructionSanity { exists(Opcode opcode | opcode = instr.getOpcode() and ( - opcode instanceof UnaryOpcode and tag instanceof UnaryOperandTag or + opcode instanceof UnaryOpcode and tag instanceof UnaryOperandTag + or + opcode instanceof BinaryOpcode and ( - opcode instanceof BinaryOpcode and - ( - tag instanceof LeftOperandTag or - tag instanceof RightOperandTag - ) - ) or - opcode instanceof MemoryAccessOpcode and tag instanceof AddressOperandTag or - opcode instanceof BufferAccessOpcode and tag instanceof BufferSizeOperand or - opcode instanceof OpcodeWithCondition and tag instanceof ConditionOperandTag or - opcode instanceof OpcodeWithLoad and tag instanceof LoadOperandTag or - opcode instanceof Opcode::Store and tag instanceof StoreValueOperandTag or - opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag or - opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag or - opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag or - opcode instanceof Opcode::Chi and tag instanceof ChiPartialOperandTag or - ( - (opcode instanceof ReadSideEffectOpcode or opcode instanceof MayWriteSideEffectOpcode or opcode instanceof Opcode::InlineAsm) and - tag instanceof SideEffectOperandTag + tag instanceof LeftOperandTag or + tag instanceof RightOperandTag ) + or + opcode instanceof MemoryAccessOpcode and tag instanceof AddressOperandTag + or + opcode instanceof BufferAccessOpcode and tag instanceof BufferSizeOperand + or + opcode instanceof OpcodeWithCondition and tag instanceof ConditionOperandTag + or + opcode instanceof OpcodeWithLoad and tag instanceof LoadOperandTag + or + opcode instanceof Opcode::Store and tag instanceof StoreValueOperandTag + or + opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag + or + opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag + or + opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag + or + opcode instanceof Opcode::Chi and tag instanceof ChiPartialOperandTag + or + ( + opcode instanceof ReadSideEffectOpcode or + opcode instanceof MayWriteSideEffectOpcode or + opcode instanceof Opcode::InlineAsm + ) and + tag instanceof SideEffectOperandTag ) ) } @@ -55,8 +66,8 @@ module InstructionSanity { operand = instr.getAnOperand() and operand.getOperandTag() = tag ) and - message = "Instruction '" + instr.getOpcode().toString() + "' is missing an expected operand with tag '" + - tag.toString() + "' in function '$@'." and + message = "Instruction '" + instr.getOpcode().toString() + + "' is missing an expected operand with tag '" + tag.toString() + "' in function '$@'." and func = instr.getEnclosingIRFunction() and funcText = Language::getIdentityString(func.getFunction()) ) @@ -68,10 +79,13 @@ module InstructionSanity { query predicate unexpectedOperand(Instruction instr, OperandTag tag) { exists(NonPhiOperand operand | operand = instr.getAnOperand() and - operand.getOperandTag() = tag) and + operand.getOperandTag() = tag + ) and not expectsOperand(instr, tag) and not (instr instanceof CallInstruction and tag instanceof ArgumentOperandTag) and - not (instr instanceof BuiltInOperationInstruction and tag instanceof PositionalArgumentOperandTag) and + not ( + instr instanceof BuiltInOperationInstruction and tag instanceof PositionalArgumentOperandTag + ) and not (instr instanceof InlineAsmInstruction and tag instanceof AsmOperandTag) } @@ -121,9 +135,7 @@ module InstructionSanity { * Holds if there are multiple (`n`) edges of kind `kind` from `source`, * where `target` is among the targets of those edges. */ - query predicate ambiguousSuccessors( - Instruction source, EdgeKind kind, int n, Instruction target - ) { + query predicate ambiguousSuccessors(Instruction source, EdgeKind kind, int n, Instruction target) { n = strictcount(Instruction t | source.getSuccessor(kind) = t) and n > 1 and source.getSuccessor(kind) = target @@ -222,11 +234,10 @@ module InstructionSanity { * of evaluation is at the end of the corresponding predecessor block. */ private predicate pointOfEvaluation(Operand operand, IRBlock block, int index) { - ( - block = operand.(PhiInputOperand).getPredecessorBlock() and - index = block.getInstructionCount() - ) or - exists (Instruction use | + block = operand.(PhiInputOperand).getPredecessorBlock() and + index = block.getInstructionCount() + or + exists(Instruction use | use = operand.(NonPhiOperand).getUse() and block.getInstruction(index) = use ) @@ -235,31 +246,28 @@ module InstructionSanity { /** * Holds if `useOperand` has a definition that does not dominate the use. */ - query predicate useNotDominatedByDefinition(Operand useOperand, string message, IRFunction func, - string funcText) { - - exists (IRBlock useBlock, int useIndex, Instruction defInstr, IRBlock defBlock, int defIndex | + query predicate useNotDominatedByDefinition( + Operand useOperand, string message, IRFunction func, string funcText + ) { + exists(IRBlock useBlock, int useIndex, Instruction defInstr, IRBlock defBlock, int defIndex | not useOperand.getUse() instanceof UnmodeledUseInstruction and pointOfEvaluation(useOperand, useBlock, useIndex) and defInstr = useOperand.getAnyDef() and ( - ( - defInstr instanceof PhiInstruction and - defBlock = defInstr.getBlock() and - defIndex = -1 - ) + defInstr instanceof PhiInstruction and + defBlock = defInstr.getBlock() and + defIndex = -1 or - defBlock.getInstruction(defIndex) = defInstr + defBlock.getInstruction(defIndex) = defInstr ) and not ( - defBlock.strictlyDominates(useBlock) or - ( - defBlock = useBlock and - defIndex < useIndex - ) + defBlock.strictlyDominates(useBlock) + or + defBlock = useBlock and + defIndex < useIndex ) and message = "Operand '" + useOperand.toString() + - "' is not dominated by its definition in function '$@'." and + "' is not dominated by its definition in function '$@'." and func = useOperand.getEnclosingIRFunction() and funcText = Language::getIdentityString(func.getFunction()) ) @@ -270,9 +278,7 @@ module InstructionSanity { * Represents a single operation in the IR. */ class Instruction extends Construction::TInstruction { - final string toString() { - result = getOpcode().toString() + ": " + getAST().toString() - } + final string toString() { result = getOpcode().toString() + ": " + getAST().toString() } /** * Gets a string showing the result, opcode, and operands of the instruction, equivalent to what @@ -291,36 +297,27 @@ class Instruction extends Construction::TInstruction { * VariableAddress[x] */ final string getOperationString() { - if exists(getImmediateString()) then - result = getOperationPrefix() + getOpcode().toString() + "[" + getImmediateString() + "]" - else - result = getOperationPrefix() + getOpcode().toString() + if exists(getImmediateString()) + then result = getOperationPrefix() + getOpcode().toString() + "[" + getImmediateString() + "]" + else result = getOperationPrefix() + getOpcode().toString() } /** * Gets a string describing the immediate value of this instruction, if any. */ - string getImmediateString() { - none() - } + string getImmediateString() { none() } private string getOperationPrefix() { - if this instanceof SideEffectInstruction then - result = "^" - else - result = "" + if this instanceof SideEffectInstruction then result = "^" else result = "" } private string getResultPrefix() { - if getResultType() instanceof Language::VoidType then - result = "v" - else if hasMemoryResult() then - if isResultModeled() then - result = "m" - else - result = "mu" + if getResultType() instanceof Language::VoidType + then result = "v" else - result = "r" + if hasMemoryResult() + then if isResultModeled() then result = "m" else result = "mu" + else result = "r" } /** @@ -335,36 +332,32 @@ class Instruction extends Construction::TInstruction { phiCount = count(block.getAPhiInstruction()) and this = block.getInstruction(index) and result = index + phiCount - ) or - ( - this instanceof PhiInstruction and - this = rank[result + 1](PhiInstruction phiInstr | - phiInstr = block.getAPhiInstruction() | + ) + or + this instanceof PhiInstruction and + this = rank[result + 1](PhiInstruction phiInstr | + phiInstr = block.getAPhiInstruction() + | phiInstr order by phiInstr.getUniqueId() ) - ) ) ) } bindingset[type] private string getValueCategoryString(string type) { - if isGLValue() then - result = "glval<" + type + ">" - else - result = type + if isGLValue() then result = "glval<" + type + ">" else result = type } string getResultTypeString() { exists(string valcat | valcat = getValueCategoryString(getResultType().toString()) and - if (getResultType() instanceof Language::UnknownType and - not isGLValue() and - exists(getResultSize())) then ( - result = valcat + "[" + getResultSize().toString() + "]" - ) - else - result = valcat + if + getResultType() instanceof Language::UnknownType and + not isGLValue() and + exists(getResultSize()) + then result = valcat + "[" + getResultSize().toString() + "]" + else result = valcat ) } @@ -377,7 +370,7 @@ class Instruction extends Construction::TInstruction { */ string getResultId() { result = getResultPrefix() + getBlock().getDisplayIndex().toString() + "_" + - getDisplayIndexInBlock().toString() + getDisplayIndexInBlock().toString() } /** @@ -387,9 +380,7 @@ class Instruction extends Construction::TInstruction { * * Example: `r1_1(int*)` */ - final string getResultString() { - result = getResultId() + "(" + getResultTypeString() + ")" - } + final string getResultString() { result = getResultId() + "(" + getResultTypeString() + ")" } /** * Gets a string describing the operands of this instruction, suitable for @@ -399,9 +390,10 @@ class Instruction extends Construction::TInstruction { */ string getOperandsString() { result = concat(Operand operand | - operand = getAnOperand() | - operand.getDumpString(), ", " order by operand.getDumpSortOrder() - ) + operand = getAnOperand() + | + operand.getDumpString(), ", " order by operand.getDumpSortOrder() + ) } /** @@ -411,16 +403,12 @@ class Instruction extends Construction::TInstruction { * This is used for sorting IR output for tests, and is likely to be * inefficient for any other use. */ - final string getUniqueId() { - result = Construction::getInstructionUniqueId(this) - } + final string getUniqueId() { result = Construction::getInstructionUniqueId(this) } /** * Gets the basic block that contains this instruction. */ - final IRBlock getBlock() { - result.getAnInstruction() = this - } + final IRBlock getBlock() { result.getAnInstruction() = this } /** * Gets the function that contains this instruction. @@ -439,31 +427,27 @@ class Instruction extends Construction::TInstruction { /** * Gets the AST that caused this instruction to be generated. */ - final Language::AST getAST() { - result = Construction::getInstructionAST(this) - } + final Language::AST getAST() { result = Construction::getInstructionAST(this) } /** * Gets the location of the source code for this instruction. */ - final Language::Location getLocation() { - result = getAST().getLocation() - } + final Language::Location getLocation() { result = getAST().getLocation() } /** * Gets the `Expr` whose result is computed by this instruction, if any. */ final Language::Expr getConvertedResultExpression() { - result = Construction::getInstructionConvertedResultExpression(this) + result = Construction::getInstructionConvertedResultExpression(this) } - + /** * Gets the unconverted `Expr` whose result is computed by this instruction, if any. */ final Language::Expr getUnconvertedResultExpression() { - result = Construction::getInstructionUnconvertedResultExpression(this) + result = Construction::getInstructionUnconvertedResultExpression(this) } - + /** * Gets the type of the result produced by this instruction. If the * instruction does not produce a result, its result type will be `VoidType`. @@ -471,9 +455,7 @@ class Instruction extends Construction::TInstruction { * If `isGLValue()` holds, then the result type of this instruction should be * thought of as "pointer to `getResultType()`". */ - final Language::Type getResultType() { - Construction::instructionHasType(this, result, _) - } + final Language::Type getResultType() { Construction::instructionHasType(this, result, _) } /** * Holds if the result produced by this instruction is a glvalue. If this @@ -493,9 +475,7 @@ class Instruction extends Construction::TInstruction { * result of the `Load` instruction is a prvalue of type `int`, representing * the integer value loaded from variable `x`. */ - final predicate isGLValue() { - Construction::instructionHasType(this, _, true) - } + final predicate isGLValue() { Construction::instructionHasType(this, _, true) } /** * Gets the size of the result produced by this instruction, in bytes. If the @@ -505,53 +485,42 @@ class Instruction extends Construction::TInstruction { * `getResultSize()` will always be the size of a pointer. */ final int getResultSize() { - if isGLValue() then ( + if isGLValue() + then // a glvalue is always pointer-sized. result = Language::getPointerSize() - ) - else if getResultType() instanceof Language::UnknownType then - result = Construction::getInstructionResultSize(this) - else ( - result = Language::getTypeSize(getResultType()) - ) + else + if getResultType() instanceof Language::UnknownType + then result = Construction::getInstructionResultSize(this) + else result = Language::getTypeSize(getResultType()) } /** * Gets the opcode that specifies the operation performed by this instruction. */ - final Opcode getOpcode() { - result = Construction::getInstructionOpcode(this) - } + final Opcode getOpcode() { result = Construction::getInstructionOpcode(this) } /** * Gets all direct uses of the result of this instruction. The result can be * an `Operand` for which `isDefinitionInexact` holds. */ - final Operand getAUse() { - result.getAnyDef() = this - } + final Operand getAUse() { result.getAnyDef() = this } /** * Gets all of this instruction's operands. */ - final Operand getAnOperand() { - result.getUse() = this - } + final Operand getAnOperand() { result.getUse() = this } /** * Holds if this instruction produces a memory result. */ - final predicate hasMemoryResult() { - exists(getResultMemoryAccess()) - } + final predicate hasMemoryResult() { exists(getResultMemoryAccess()) } /** * Gets the kind of memory access performed by this instruction's result. * Holds only for instructions with a memory result. */ - MemoryAccessKind getResultMemoryAccess() { - none() - } + MemoryAccessKind getResultMemoryAccess() { none() } /** * Gets the operand that holds the memory address to which this instruction stores its @@ -568,9 +537,7 @@ class Instruction extends Construction::TInstruction { * result, if any. For example, in `m3 = Store r1, r2`, the result of `getResultAddressOperand()` * is the instruction that defines `r1`. */ - final Instruction getResultAddress() { - result = getResultAddressOperand().getDef() - } + final Instruction getResultAddress() { result = getResultAddressOperand().getDef() } /** * Holds if the result of this instruction is precisely modeled in SSA. Always @@ -623,137 +590,89 @@ class Instruction extends Construction::TInstruction { /** * Gets all direct successors of this instruction. */ - final Instruction getASuccessor() { - result = getSuccessor(_) - } + final Instruction getASuccessor() { result = getSuccessor(_) } /** * Gets a predecessor of this instruction such that the predecessor reaches * this instruction along the control flow edge specified by `kind`. */ - final Instruction getPredecessor(EdgeKind kind) { - result.getSuccessor(kind) = this - } + final Instruction getPredecessor(EdgeKind kind) { result.getSuccessor(kind) = this } /** * Gets all direct predecessors of this instruction. */ - final Instruction getAPredecessor() { - result = getPredecessor(_) - } + final Instruction getAPredecessor() { result = getPredecessor(_) } } class VariableInstruction extends Instruction { IRVariable var; - VariableInstruction() { - var = Construction::getInstructionVariable(this) - } + VariableInstruction() { var = Construction::getInstructionVariable(this) } - override final string getImmediateString() { - result = var.toString() - } + final override string getImmediateString() { result = var.toString() } - final IRVariable getVariable() { - result = var - } + final IRVariable getVariable() { result = var } } class FieldInstruction extends Instruction { Language::Field field; - FieldInstruction() { - field = Construction::getInstructionField(this) - } + FieldInstruction() { field = Construction::getInstructionField(this) } - override final string getImmediateString() { - result = field.toString() - } + final override string getImmediateString() { result = field.toString() } - final Language::Field getField() { - result = field - } + final Language::Field getField() { result = field } } class FunctionInstruction extends Instruction { Language::Function funcSymbol; - FunctionInstruction() { - funcSymbol = Construction::getInstructionFunction(this) - } + FunctionInstruction() { funcSymbol = Construction::getInstructionFunction(this) } - override final string getImmediateString() { - result = funcSymbol.toString() - } + final override string getImmediateString() { result = funcSymbol.toString() } - final Language::Function getFunctionSymbol() { - result = funcSymbol - } + final Language::Function getFunctionSymbol() { result = funcSymbol } } class ConstantValueInstruction extends Instruction { string value; - ConstantValueInstruction() { - value = Construction::getInstructionConstantValue(this) - } + ConstantValueInstruction() { value = Construction::getInstructionConstantValue(this) } - override final string getImmediateString() { - result = value - } + final override string getImmediateString() { result = value } - final string getValue() { - result = value - } + final string getValue() { result = value } } class EnterFunctionInstruction extends Instruction { - EnterFunctionInstruction() { - getOpcode() instanceof Opcode::EnterFunction - } + EnterFunctionInstruction() { getOpcode() instanceof Opcode::EnterFunction } } class VariableAddressInstruction extends VariableInstruction { - VariableAddressInstruction() { - getOpcode() instanceof Opcode::VariableAddress - } + VariableAddressInstruction() { getOpcode() instanceof Opcode::VariableAddress } } class InitializeParameterInstruction extends VariableInstruction { - InitializeParameterInstruction() { - getOpcode() instanceof Opcode::InitializeParameter - } + InitializeParameterInstruction() { getOpcode() instanceof Opcode::InitializeParameter } - final Language::Parameter getParameter() { - result = var.(IRUserVariable).getVariable() - } + final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } } /** * An instruction that initializes the `this` pointer parameter of the enclosing function. */ class InitializeThisInstruction extends Instruction { - InitializeThisInstruction() { - getOpcode() instanceof Opcode::InitializeThis - } + InitializeThisInstruction() { getOpcode() instanceof Opcode::InitializeThis } } class FieldAddressInstruction extends FieldInstruction { - FieldAddressInstruction() { - getOpcode() instanceof Opcode::FieldAddress - } + FieldAddressInstruction() { getOpcode() instanceof Opcode::FieldAddress } - final UnaryOperand getObjectAddressOperand() { - result = getAnOperand() - } + final UnaryOperand getObjectAddressOperand() { result = getAnOperand() } - final Instruction getObjectAddress() { - result = getObjectAddressOperand().getDef() - } + final Instruction getObjectAddress() { result = getObjectAddressOperand().getDef() } } /** @@ -767,207 +686,125 @@ class FieldAddressInstruction extends FieldInstruction { * taken may want to ignore any function that contains an `ErrorInstruction`. */ class ErrorInstruction extends Instruction { - ErrorInstruction() { - getOpcode() instanceof Opcode::Error - } + ErrorInstruction() { getOpcode() instanceof Opcode::Error } } class UninitializedInstruction extends VariableInstruction { - UninitializedInstruction() { - getOpcode() instanceof Opcode::Uninitialized - } + UninitializedInstruction() { getOpcode() instanceof Opcode::Uninitialized } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } /** * Gets the variable that is uninitialized. */ - final Language::Variable getLocalVariable() { - result = var.(IRUserVariable).getVariable() - } + final Language::Variable getLocalVariable() { result = var.(IRUserVariable).getVariable() } } class NoOpInstruction extends Instruction { - NoOpInstruction() { - getOpcode() instanceof Opcode::NoOp - } + NoOpInstruction() { getOpcode() instanceof Opcode::NoOp } } class ReturnInstruction extends Instruction { - ReturnInstruction() { - getOpcode() instanceof ReturnOpcode - } + ReturnInstruction() { getOpcode() instanceof ReturnOpcode } } class ReturnVoidInstruction extends ReturnInstruction { - ReturnVoidInstruction() { - getOpcode() instanceof Opcode::ReturnVoid - } + ReturnVoidInstruction() { getOpcode() instanceof Opcode::ReturnVoid } } class ReturnValueInstruction extends ReturnInstruction { - ReturnValueInstruction() { - getOpcode() instanceof Opcode::ReturnValue - } + ReturnValueInstruction() { getOpcode() instanceof Opcode::ReturnValue } - final LoadOperand getReturnValueOperand() { - result = getAnOperand() - } - - final Instruction getReturnValue() { - result = getReturnValueOperand().getDef() - } + final LoadOperand getReturnValueOperand() { result = getAnOperand() } + + final Instruction getReturnValue() { result = getReturnValueOperand().getDef() } } class CopyInstruction extends Instruction { - CopyInstruction() { - getOpcode() instanceof CopyOpcode - } + CopyInstruction() { getOpcode() instanceof CopyOpcode } - Operand getSourceValueOperand() { - none() - } + Operand getSourceValueOperand() { none() } - final Instruction getSourceValue() { - result = getSourceValueOperand().getDef() - } + final Instruction getSourceValue() { result = getSourceValueOperand().getDef() } } class CopyValueInstruction extends CopyInstruction, UnaryInstruction { - CopyValueInstruction() { - getOpcode() instanceof Opcode::CopyValue - } + CopyValueInstruction() { getOpcode() instanceof Opcode::CopyValue } - override final UnaryOperand getSourceValueOperand() { - result = getAnOperand() - } + final override UnaryOperand getSourceValueOperand() { result = getAnOperand() } } class LoadInstruction extends CopyInstruction { - LoadInstruction() { - getOpcode() instanceof Opcode::Load - } + LoadInstruction() { getOpcode() instanceof Opcode::Load } - final AddressOperand getSourceAddressOperand() { - result = getAnOperand() - } - - final Instruction getSourceAddress() { - result = getSourceAddressOperand().getDef() - } + final AddressOperand getSourceAddressOperand() { result = getAnOperand() } - override final LoadOperand getSourceValueOperand() { - result = getAnOperand() - } + final Instruction getSourceAddress() { result = getSourceAddressOperand().getDef() } + + final override LoadOperand getSourceValueOperand() { result = getAnOperand() } } class StoreInstruction extends CopyInstruction { - StoreInstruction() { - getOpcode() instanceof Opcode::Store - } + StoreInstruction() { getOpcode() instanceof Opcode::Store } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } - final AddressOperand getDestinationAddressOperand() { - result = getAnOperand() - } - - final Instruction getDestinationAddress() { - result = getDestinationAddressOperand().getDef() - } + final AddressOperand getDestinationAddressOperand() { result = getAnOperand() } - override final StoreValueOperand getSourceValueOperand() { - result = getAnOperand() - } + final Instruction getDestinationAddress() { result = getDestinationAddressOperand().getDef() } + + final override StoreValueOperand getSourceValueOperand() { result = getAnOperand() } } class ConditionalBranchInstruction extends Instruction { - ConditionalBranchInstruction() { - getOpcode() instanceof Opcode::ConditionalBranch - } + ConditionalBranchInstruction() { getOpcode() instanceof Opcode::ConditionalBranch } - final ConditionOperand getConditionOperand() { - result = getAnOperand() - } + final ConditionOperand getConditionOperand() { result = getAnOperand() } - final Instruction getCondition() { - result = getConditionOperand().getDef() - } + final Instruction getCondition() { result = getConditionOperand().getDef() } - final Instruction getTrueSuccessor() { - result = getSuccessor(trueEdge()) - } + final Instruction getTrueSuccessor() { result = getSuccessor(trueEdge()) } - final Instruction getFalseSuccessor() { - result = getSuccessor(falseEdge()) - } + final Instruction getFalseSuccessor() { result = getSuccessor(falseEdge()) } } class ExitFunctionInstruction extends Instruction { - ExitFunctionInstruction() { - getOpcode() instanceof Opcode::ExitFunction - } + ExitFunctionInstruction() { getOpcode() instanceof Opcode::ExitFunction } } class ConstantInstruction extends ConstantValueInstruction { - ConstantInstruction() { - getOpcode() instanceof Opcode::Constant - } + ConstantInstruction() { getOpcode() instanceof Opcode::Constant } } class IntegerConstantInstruction extends ConstantInstruction { - IntegerConstantInstruction() { - getResultType() instanceof Language::IntegralType - } + IntegerConstantInstruction() { getResultType() instanceof Language::IntegralType } } class FloatConstantInstruction extends ConstantInstruction { - FloatConstantInstruction() { - getResultType() instanceof Language::FloatingPointType - } + FloatConstantInstruction() { getResultType() instanceof Language::FloatingPointType } } class StringConstantInstruction extends Instruction { Language::StringLiteral value; - StringConstantInstruction() { - value = Construction::getInstructionStringLiteral(this) - } + StringConstantInstruction() { value = Construction::getInstructionStringLiteral(this) } - override final string getImmediateString() { - result = Language::getStringLiteralText(value) - } + final override string getImmediateString() { result = Language::getStringLiteralText(value) } - final Language::StringLiteral getValue() { - result = value - } + final Language::StringLiteral getValue() { result = value } } class BinaryInstruction extends Instruction { - BinaryInstruction() { - getOpcode() instanceof BinaryOpcode - } + BinaryInstruction() { getOpcode() instanceof BinaryOpcode } - final LeftOperand getLeftOperand() { - result = getAnOperand() - } - - final RightOperand getRightOperand() { - result = getAnOperand() - } + final LeftOperand getLeftOperand() { result = getAnOperand() } - final Instruction getLeft() { - result = getLeftOperand().getDef() - } + final RightOperand getRightOperand() { result = getAnOperand() } + + final Instruction getLeft() { result = getLeftOperand().getDef() } + + final Instruction getRight() { result = getRightOperand().getDef() } - final Instruction getRight() { - result = getRightOperand().getDef() - } - /** * Holds if this instruction's operands are `op1` and `op2`, in either order. */ @@ -979,89 +816,63 @@ class BinaryInstruction extends Instruction { } class ArithmeticInstruction extends Instruction { - ArithmeticInstruction() { - getOpcode() instanceof ArithmeticOpcode - } + ArithmeticInstruction() { getOpcode() instanceof ArithmeticOpcode } } -class BinaryArithmeticInstruction extends ArithmeticInstruction, BinaryInstruction {} +class BinaryArithmeticInstruction extends ArithmeticInstruction, BinaryInstruction { } -class UnaryArithmeticInstruction extends ArithmeticInstruction, UnaryInstruction {} +class UnaryArithmeticInstruction extends ArithmeticInstruction, UnaryInstruction { } class AddInstruction extends BinaryArithmeticInstruction { - AddInstruction() { - getOpcode() instanceof Opcode::Add - } + AddInstruction() { getOpcode() instanceof Opcode::Add } } class SubInstruction extends BinaryArithmeticInstruction { - SubInstruction() { - getOpcode() instanceof Opcode::Sub - } + SubInstruction() { getOpcode() instanceof Opcode::Sub } } class MulInstruction extends BinaryArithmeticInstruction { - MulInstruction() { - getOpcode() instanceof Opcode::Mul - } + MulInstruction() { getOpcode() instanceof Opcode::Mul } } class DivInstruction extends BinaryArithmeticInstruction { - DivInstruction() { - getOpcode() instanceof Opcode::Div - } + DivInstruction() { getOpcode() instanceof Opcode::Div } } class RemInstruction extends BinaryArithmeticInstruction { - RemInstruction() { - getOpcode() instanceof Opcode::Rem - } + RemInstruction() { getOpcode() instanceof Opcode::Rem } } class NegateInstruction extends UnaryArithmeticInstruction { - NegateInstruction() { - getOpcode() instanceof Opcode::Negate - } + NegateInstruction() { getOpcode() instanceof Opcode::Negate } } class BitwiseInstruction extends Instruction { - BitwiseInstruction() { - getOpcode() instanceof BitwiseOpcode - } + BitwiseInstruction() { getOpcode() instanceof BitwiseOpcode } } -class BinaryBitwiseInstruction extends BitwiseInstruction, BinaryInstruction {} +class BinaryBitwiseInstruction extends BitwiseInstruction, BinaryInstruction { } -class UnaryBitwiseInstruction extends BitwiseInstruction, UnaryInstruction {} +class UnaryBitwiseInstruction extends BitwiseInstruction, UnaryInstruction { } class BitAndInstruction extends BinaryBitwiseInstruction { - BitAndInstruction() { - getOpcode() instanceof Opcode::BitAnd - } + BitAndInstruction() { getOpcode() instanceof Opcode::BitAnd } } class BitOrInstruction extends BinaryBitwiseInstruction { - BitOrInstruction() { - getOpcode() instanceof Opcode::BitOr - } + BitOrInstruction() { getOpcode() instanceof Opcode::BitOr } } class BitXorInstruction extends BinaryBitwiseInstruction { - BitXorInstruction() { - getOpcode() instanceof Opcode::BitXor - } + BitXorInstruction() { getOpcode() instanceof Opcode::BitXor } } class ShiftLeftInstruction extends BinaryBitwiseInstruction { - ShiftLeftInstruction() { - getOpcode() instanceof Opcode::ShiftLeft - } + ShiftLeftInstruction() { getOpcode() instanceof Opcode::ShiftLeft } } class ShiftRightInstruction extends BinaryBitwiseInstruction { - ShiftRightInstruction() { - getOpcode() instanceof Opcode::ShiftRight - } + ShiftRightInstruction() { getOpcode() instanceof Opcode::ShiftRight } } class PointerArithmeticInstruction extends BinaryInstruction { @@ -1072,57 +883,37 @@ class PointerArithmeticInstruction extends BinaryInstruction { elementSize = Construction::getInstructionElementSize(this) } - override final string getImmediateString() { - result = elementSize.toString() - } + final override string getImmediateString() { result = elementSize.toString() } - final int getElementSize() { - result = elementSize - } + final int getElementSize() { result = elementSize } } class PointerOffsetInstruction extends PointerArithmeticInstruction { - PointerOffsetInstruction() { - getOpcode() instanceof PointerOffsetOpcode - } + PointerOffsetInstruction() { getOpcode() instanceof PointerOffsetOpcode } } class PointerAddInstruction extends PointerOffsetInstruction { - PointerAddInstruction() { - getOpcode() instanceof Opcode::PointerAdd - } + PointerAddInstruction() { getOpcode() instanceof Opcode::PointerAdd } } class PointerSubInstruction extends PointerOffsetInstruction { - PointerSubInstruction() { - getOpcode() instanceof Opcode::PointerSub - } + PointerSubInstruction() { getOpcode() instanceof Opcode::PointerSub } } class PointerDiffInstruction extends PointerArithmeticInstruction { - PointerDiffInstruction() { - getOpcode() instanceof Opcode::PointerDiff - } + PointerDiffInstruction() { getOpcode() instanceof Opcode::PointerDiff } } class UnaryInstruction extends Instruction { - UnaryInstruction() { - getOpcode() instanceof UnaryOpcode - } + UnaryInstruction() { getOpcode() instanceof UnaryOpcode } - final UnaryOperand getUnaryOperand() { - result = getAnOperand() - } - - final Instruction getUnary() { - result = getUnaryOperand().getDef() - } + final UnaryOperand getUnaryOperand() { result = getAnOperand() } + + final Instruction getUnary() { result = getUnaryOperand().getDef() } } class ConvertInstruction extends UnaryInstruction { - ConvertInstruction() { - getOpcode() instanceof Opcode::Convert - } + ConvertInstruction() { getOpcode() instanceof Opcode::Convert } } /** @@ -1131,13 +922,14 @@ class ConvertInstruction extends UnaryInstruction { */ class InheritanceConversionInstruction extends UnaryInstruction { Language::Class baseClass; + Language::Class derivedClass; InheritanceConversionInstruction() { Construction::getInstructionInheritance(this, baseClass, derivedClass) } - override final string getImmediateString() { + final override string getImmediateString() { result = derivedClass.toString() + " : " + baseClass.toString() } @@ -1155,16 +947,12 @@ class InheritanceConversionInstruction extends UnaryInstruction { * base class of the derived class, or a virtual base class of the * derived class. */ - final Language::Class getBaseClass() { - result = baseClass - } + final Language::Class getBaseClass() { result = baseClass } /** * Gets the derived class of the conversion. */ - final Language::Class getDerivedClass() { - result = derivedClass - } + final Language::Class getDerivedClass() { result = derivedClass } } /** @@ -1172,9 +960,7 @@ class InheritanceConversionInstruction extends UnaryInstruction { * to the address of a direct non-virtual base class. */ class ConvertToBaseInstruction extends InheritanceConversionInstruction { - ConvertToBaseInstruction() { - getOpcode() instanceof Opcode::ConvertToBase - } + ConvertToBaseInstruction() { getOpcode() instanceof Opcode::ConvertToBase } } /** @@ -1182,9 +968,7 @@ class ConvertToBaseInstruction extends InheritanceConversionInstruction { * to the address of a virtual base class. */ class ConvertToVirtualBaseInstruction extends InheritanceConversionInstruction { - ConvertToVirtualBaseInstruction() { - getOpcode() instanceof Opcode::ConvertToVirtualBase - } + ConvertToVirtualBaseInstruction() { getOpcode() instanceof Opcode::ConvertToVirtualBase } } /** @@ -1192,48 +976,34 @@ class ConvertToVirtualBaseInstruction extends InheritanceConversionInstruction { * to the address of a direct non-virtual derived class. */ class ConvertToDerivedInstruction extends InheritanceConversionInstruction { - ConvertToDerivedInstruction() { - getOpcode() instanceof Opcode::ConvertToDerived - } + ConvertToDerivedInstruction() { getOpcode() instanceof Opcode::ConvertToDerived } } class BitComplementInstruction extends UnaryBitwiseInstruction { - BitComplementInstruction() { - getOpcode() instanceof Opcode::BitComplement - } + BitComplementInstruction() { getOpcode() instanceof Opcode::BitComplement } } class LogicalNotInstruction extends UnaryInstruction { - LogicalNotInstruction() { - getOpcode() instanceof Opcode::LogicalNot - } + LogicalNotInstruction() { getOpcode() instanceof Opcode::LogicalNot } } class CompareInstruction extends BinaryInstruction { - CompareInstruction() { - getOpcode() instanceof CompareOpcode - } + CompareInstruction() { getOpcode() instanceof CompareOpcode } } class CompareEQInstruction extends CompareInstruction { - CompareEQInstruction() { - getOpcode() instanceof Opcode::CompareEQ - } + CompareEQInstruction() { getOpcode() instanceof Opcode::CompareEQ } } class CompareNEInstruction extends CompareInstruction { - CompareNEInstruction() { - getOpcode() instanceof Opcode::CompareNE - } + CompareNEInstruction() { getOpcode() instanceof Opcode::CompareNE } } /** * Represents an instruction that does a relative comparison of two values, such as `<` or `>=`. */ class RelationalInstruction extends CompareInstruction { - RelationalInstruction() { - getOpcode() instanceof RelationalOpcode - } + RelationalInstruction() { getOpcode() instanceof RelationalOpcode } /** * Gets the operand on the "greater" (or "greater-or-equal") side @@ -1241,9 +1011,7 @@ class RelationalInstruction extends CompareInstruction { * if the overall instruction evaluates to `true`; for example on * `x <= 20` this is the `20`, and on `y > 0` it is `y`. */ - Instruction getGreater() { - none() - } + Instruction getGreater() { none() } /** * Gets the operand on the "lesser" (or "lesser-or-equal") side @@ -1251,144 +1019,88 @@ class RelationalInstruction extends CompareInstruction { * if the overall instruction evaluates to `true`; for example on * `x <= 20` this is `x`, and on `y > 0` it is the `0`. */ - Instruction getLesser() { - none() - } + Instruction getLesser() { none() } /** * Holds if this relational instruction is strict (is not an "or-equal" instruction). */ - predicate isStrict() { - none() - } + predicate isStrict() { none() } } class CompareLTInstruction extends RelationalInstruction { - CompareLTInstruction() { - getOpcode() instanceof Opcode::CompareLT - } + CompareLTInstruction() { getOpcode() instanceof Opcode::CompareLT } - override Instruction getLesser() { - result = getLeft() - } + override Instruction getLesser() { result = getLeft() } - override Instruction getGreater() { - result = getRight() - } + override Instruction getGreater() { result = getRight() } - override predicate isStrict() { - any() - } + override predicate isStrict() { any() } } class CompareGTInstruction extends RelationalInstruction { - CompareGTInstruction() { - getOpcode() instanceof Opcode::CompareGT - } + CompareGTInstruction() { getOpcode() instanceof Opcode::CompareGT } - override Instruction getLesser() { - result = getRight() - } + override Instruction getLesser() { result = getRight() } - override Instruction getGreater() { - result = getLeft() - } + override Instruction getGreater() { result = getLeft() } - override predicate isStrict() { - any() - } + override predicate isStrict() { any() } } class CompareLEInstruction extends RelationalInstruction { - CompareLEInstruction() { - getOpcode() instanceof Opcode::CompareLE - } + CompareLEInstruction() { getOpcode() instanceof Opcode::CompareLE } - override Instruction getLesser() { - result = getLeft() - } + override Instruction getLesser() { result = getLeft() } - override Instruction getGreater() { - result = getRight() - } + override Instruction getGreater() { result = getRight() } - override predicate isStrict() { - none() - } + override predicate isStrict() { none() } } class CompareGEInstruction extends RelationalInstruction { - CompareGEInstruction() { - getOpcode() instanceof Opcode::CompareGE - } + CompareGEInstruction() { getOpcode() instanceof Opcode::CompareGE } - override Instruction getLesser() { - result = getRight() - } + override Instruction getLesser() { result = getRight() } - override Instruction getGreater() { - result = getLeft() - } + override Instruction getGreater() { result = getLeft() } - override predicate isStrict() { - none() - } + override predicate isStrict() { none() } } class SwitchInstruction extends Instruction { - SwitchInstruction() { - getOpcode() instanceof Opcode::Switch - } + SwitchInstruction() { getOpcode() instanceof Opcode::Switch } - final ConditionOperand getExpressionOperand() { - result = getAnOperand() - } + final ConditionOperand getExpressionOperand() { result = getAnOperand() } - final Instruction getExpression() { - result = getExpressionOperand().getDef() - } + final Instruction getExpression() { result = getExpressionOperand().getDef() } - final Instruction getACaseSuccessor() { - exists(CaseEdge edge | - result = getSuccessor(edge) - ) - } + final Instruction getACaseSuccessor() { exists(CaseEdge edge | result = getSuccessor(edge)) } - final Instruction getDefaultSuccessor() { - result = getSuccessor(defaultEdge()) - } + final Instruction getDefaultSuccessor() { result = getSuccessor(defaultEdge()) } } /** * An instruction that calls a function. */ class CallInstruction extends Instruction { - CallInstruction() { - getOpcode() instanceof Opcode::Call - } + CallInstruction() { getOpcode() instanceof Opcode::Call } /** * Gets the operand the specifies the target function of the call. */ - final CallTargetOperand getCallTargetOperand() { - result = getAnOperand() - } + final CallTargetOperand getCallTargetOperand() { result = getAnOperand() } /** * Gets the `Instruction` that computes the target function of the call. This is usually a * `FunctionAddress` instruction, but can also be an arbitrary instruction that produces a * function pointer. */ - final Instruction getCallTarget() { - result = getCallTargetOperand().getDef() - } + final Instruction getCallTarget() { result = getCallTargetOperand().getDef() } /** * Gets all of the argument operands of the call, including the `this` pointer, if any. */ - final ArgumentOperand getAnArgumentOperand() { - result = getAnOperand() - } + final ArgumentOperand getAnArgumentOperand() { result = getAnOperand() } /** * Gets the `Function` that the call targets, if this is statically known. @@ -1400,23 +1112,17 @@ class CallInstruction extends Instruction { /** * Gets all of the arguments of the call, including the `this` pointer, if any. */ - final Instruction getAnArgument() { - result = getAnArgumentOperand().getDef() - } + final Instruction getAnArgument() { result = getAnArgumentOperand().getDef() } /** * Gets the `this` pointer argument operand of the call, if any. */ - final ThisArgumentOperand getThisArgumentOperand() { - result = getAnOperand() - } + final ThisArgumentOperand getThisArgumentOperand() { result = getAnOperand() } /** * Gets the `this` pointer argument of the call, if any. */ - final Instruction getThisArgument() { - result = getThisArgumentOperand().getDef() - } + final Instruction getThisArgument() { result = getThisArgumentOperand().getDef() } /** * Gets the argument operand at the specified index. @@ -1438,9 +1144,7 @@ class CallInstruction extends Instruction { * An instruction representing a side effect of a function call. */ class SideEffectInstruction extends Instruction { - SideEffectInstruction() { - getOpcode() instanceof SideEffectOpcode - } + SideEffectInstruction() { getOpcode() instanceof SideEffectOpcode } final Instruction getPrimaryInstruction() { result = Construction::getPrimaryInstructionForSideEffect(this) @@ -1452,11 +1156,9 @@ class SideEffectInstruction extends Instruction { * accessed by that call. */ class CallSideEffectInstruction extends SideEffectInstruction { - CallSideEffectInstruction() { - getOpcode() instanceof Opcode::CallSideEffect - } + CallSideEffectInstruction() { getOpcode() instanceof Opcode::CallSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof EscapedMayMemoryAccess } } @@ -1466,40 +1168,30 @@ class CallSideEffectInstruction extends SideEffectInstruction { * by that call. */ class CallReadSideEffectInstruction extends SideEffectInstruction { - CallReadSideEffectInstruction() { - getOpcode() instanceof Opcode::CallReadSideEffect - } + CallReadSideEffectInstruction() { getOpcode() instanceof Opcode::CallReadSideEffect } } /** * An instruction representing the read of an indirect parameter within a function call. */ class IndirectReadSideEffectInstruction extends SideEffectInstruction { - IndirectReadSideEffectInstruction() { - getOpcode() instanceof Opcode::IndirectReadSideEffect - } + IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect } } /** * An instruction representing the read of an indirect buffer parameter within a function call. */ class BufferReadSideEffectInstruction extends SideEffectInstruction { - BufferReadSideEffectInstruction() { - getOpcode() instanceof Opcode::BufferReadSideEffect - } + BufferReadSideEffectInstruction() { getOpcode() instanceof Opcode::BufferReadSideEffect } } /** * An instruction representing the write of an indirect parameter within a function call. */ class IndirectWriteSideEffectInstruction extends SideEffectInstruction { - IndirectWriteSideEffectInstruction() { - getOpcode() instanceof Opcode::IndirectWriteSideEffect - } + IndirectWriteSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } } /** @@ -1507,13 +1199,9 @@ class IndirectWriteSideEffectInstruction extends SideEffectInstruction { * entire buffer is overwritten. */ class BufferWriteSideEffectInstruction extends SideEffectInstruction { - BufferWriteSideEffectInstruction() { - getOpcode() instanceof Opcode::BufferWriteSideEffect - } + BufferWriteSideEffectInstruction() { getOpcode() instanceof Opcode::BufferWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof BufferMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof BufferMemoryAccess } } /** @@ -1526,21 +1214,19 @@ class IndirectMayWriteSideEffectInstruction extends SideEffectInstruction { getOpcode() instanceof Opcode::IndirectMayWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMayMemoryAccess } } /** - * An instruction representing the write of an indirect buffer parameter within a function call. + * An instruction representing the write of an indirect buffer parameter within a function call. * Unlike `BufferWriteSideEffectInstruction`, the buffer might not be completely overwritten. */ class BufferMayWriteSideEffectInstruction extends SideEffectInstruction { - BufferMayWriteSideEffectInstruction() { - getOpcode() instanceof Opcode::BufferMayWriteSideEffect - } + BufferMayWriteSideEffectInstruction() { getOpcode() instanceof Opcode::BufferMayWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof BufferMayMemoryAccess } } @@ -1549,11 +1235,9 @@ class BufferMayWriteSideEffectInstruction extends SideEffectInstruction { * An instruction representing a GNU or MSVC inline assembly statement. */ class InlineAsmInstruction extends Instruction { - InlineAsmInstruction() { - getOpcode() instanceof Opcode::InlineAsm - } - - override final MemoryAccessKind getResultMemoryAccess() { + InlineAsmInstruction() { getOpcode() instanceof Opcode::InlineAsm } + + final override MemoryAccessKind getResultMemoryAccess() { result instanceof EscapedMayMemoryAccess } } @@ -1562,73 +1246,55 @@ class InlineAsmInstruction extends Instruction { * An instruction that throws an exception. */ class ThrowInstruction extends Instruction { - ThrowInstruction() { - getOpcode() instanceof ThrowOpcode - } + ThrowInstruction() { getOpcode() instanceof ThrowOpcode } } /** * An instruction that throws a new exception. */ class ThrowValueInstruction extends ThrowInstruction { - ThrowValueInstruction() { - getOpcode() instanceof Opcode::ThrowValue - } + ThrowValueInstruction() { getOpcode() instanceof Opcode::ThrowValue } /** * Gets the address operand of the exception thrown by this instruction. */ - final AddressOperand getExceptionAddressOperand() { - result = getAnOperand() - } + final AddressOperand getExceptionAddressOperand() { result = getAnOperand() } /** * Gets the address of the exception thrown by this instruction. */ - final Instruction getExceptionAddress() { - result = getExceptionAddressOperand().getDef() - } + final Instruction getExceptionAddress() { result = getExceptionAddressOperand().getDef() } /** * Gets the operand for the exception thrown by this instruction. */ - final LoadOperand getExceptionOperand() { - result = getAnOperand() - } + final LoadOperand getExceptionOperand() { result = getAnOperand() } /** * Gets the exception thrown by this instruction. */ - final Instruction getException() { - result = getExceptionOperand().getDef() - } + final Instruction getException() { result = getExceptionOperand().getDef() } } /** * An instruction that re-throws the current exception. */ class ReThrowInstruction extends ThrowInstruction { - ReThrowInstruction() { - getOpcode() instanceof Opcode::ReThrow - } + ReThrowInstruction() { getOpcode() instanceof Opcode::ReThrow } } /** * An instruction that exits the current function by propagating an exception. */ class UnwindInstruction extends Instruction { - UnwindInstruction() { - getOpcode() instanceof Opcode::Unwind - } + UnwindInstruction() { getOpcode() instanceof Opcode::Unwind } } /** * An instruction that starts a `catch` handler. */ class CatchInstruction extends Instruction { - CatchInstruction() { - getOpcode() instanceof CatchOpcode - } + CatchInstruction() { getOpcode() instanceof CatchOpcode } } /** @@ -1642,33 +1308,25 @@ class CatchByTypeInstruction extends CatchInstruction { exceptionType = Construction::getInstructionExceptionType(this) } - final override string getImmediateString() { - result = exceptionType.toString() - } + final override string getImmediateString() { result = exceptionType.toString() } /** * Gets the type of exception to be caught. */ - final Language::Type getExceptionType() { - result = exceptionType - } + final Language::Type getExceptionType() { result = exceptionType } } /** * An instruction that catches any exception. */ class CatchAnyInstruction extends CatchInstruction { - CatchAnyInstruction() { - getOpcode() instanceof Opcode::CatchAny - } + CatchAnyInstruction() { getOpcode() instanceof Opcode::CatchAny } } class UnmodeledDefinitionInstruction extends Instruction { - UnmodeledDefinitionInstruction() { - getOpcode() instanceof Opcode::UnmodeledDefinition - } + UnmodeledDefinitionInstruction() { getOpcode() instanceof Opcode::UnmodeledDefinition } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof UnmodeledMemoryAccess } } @@ -1677,23 +1335,15 @@ class UnmodeledDefinitionInstruction extends Instruction { * An instruction that initializes all escaped memory. */ class AliasedDefinitionInstruction extends Instruction { - AliasedDefinitionInstruction() { - getOpcode() instanceof Opcode::AliasedDefinition - } + AliasedDefinitionInstruction() { getOpcode() instanceof Opcode::AliasedDefinition } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof EscapedMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof EscapedMemoryAccess } } class UnmodeledUseInstruction extends Instruction { - UnmodeledUseInstruction() { - getOpcode() instanceof Opcode::UnmodeledUse - } + UnmodeledUseInstruction() { getOpcode() instanceof Opcode::UnmodeledUse } - override string getOperandsString() { - result = "mu*" - } + override string getOperandsString() { result = "mu*" } } /** @@ -1707,20 +1357,14 @@ class UnmodeledUseInstruction extends Instruction { * runtime. */ class PhiInstruction extends Instruction { - PhiInstruction() { - getOpcode() instanceof Opcode::Phi - } + PhiInstruction() { getOpcode() instanceof Opcode::Phi } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof PhiMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof PhiMemoryAccess } /** * Gets all of the instruction's `PhiInputOperand`s, representing the values that flow from each predecessor block. */ - final PhiInputOperand getAnInputOperand() { - result = this.getAnOperand() - } + final PhiInputOperand getAnInputOperand() { result = this.getAnOperand() } /** * Gets an instruction that defines the input to one of the operands of this @@ -1729,9 +1373,7 @@ class PhiInstruction extends Instruction { * results as `getAnInputOperand()` or fewer. */ pragma[noinline] - final Instruction getAnInput() { - result = this.getAnInputOperand().getDef() - } + final Instruction getAnInput() { result = this.getAnInputOperand().getDef() } } /** @@ -1777,43 +1419,31 @@ class PhiInstruction extends Instruction { * https://link.springer.com/content/pdf/10.1007%2F3-540-61053-7_66.pdf. */ class ChiInstruction extends Instruction { - ChiInstruction() { - getOpcode() instanceof Opcode::Chi - } + ChiInstruction() { getOpcode() instanceof Opcode::Chi } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof ChiTotalMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof ChiTotalMemoryAccess } /** * Gets the operand that represents the previous state of all memory that might be aliased by the * memory write. */ - final ChiTotalOperand getTotalOperand() { - result = getAnOperand() - } + final ChiTotalOperand getTotalOperand() { result = getAnOperand() } /** * Gets the operand that represents the previous state of all memory that might be aliased by the * memory write. */ - final Instruction getTotal() { - result = getTotalOperand().getDef() - } + final Instruction getTotal() { result = getTotalOperand().getDef() } /** * Gets the operand that represents the new value written by the memory write. */ - final ChiPartialOperand getPartialOperand() { - result = getAnOperand() - } + final ChiPartialOperand getPartialOperand() { result = getAnOperand() } /** * Gets the operand that represents the new value written by the memory write. */ - final Instruction getPartial() { - result = getPartialOperand().getDef() - } + final Instruction getPartial() { result = getPartialOperand().getDef() } } /** @@ -1822,9 +1452,7 @@ class ChiInstruction extends Instruction { * infeasible. */ class UnreachedInstruction extends Instruction { - UnreachedInstruction() { - getOpcode() instanceof Opcode::Unreached - } + UnreachedInstruction() { getOpcode() instanceof Opcode::Unreached } } /** @@ -1839,9 +1467,7 @@ class BuiltInOperationInstruction extends Instruction { operation = Construction::getInstructionBuiltInOperation(this) } - final Language::BuiltInOperation getBuiltInOperation() { - result = operation - } + final Language::BuiltInOperation getBuiltInOperation() { result = operation } } /** @@ -1849,11 +1475,7 @@ class BuiltInOperationInstruction extends Instruction { * actual operation is specified by the `getBuiltInOperation()` predicate. */ class BuiltInInstruction extends BuiltInOperationInstruction { - BuiltInInstruction() { - getOpcode() instanceof Opcode::BuiltIn - } + BuiltInInstruction() { getOpcode() instanceof Opcode::BuiltIn } - override final string getImmediateString() { - result = getBuiltInOperation().toString() - } + final override string getImmediateString() { result = getBuiltInOperation().toString() } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Operand.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Operand.qll index 29af8267cb7..fda04820848 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Operand.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Operand.qll @@ -12,11 +12,15 @@ private newtype TOperand = defInstr = Construction::getRegisterOperandDefinition(useInstr, tag) and not isInCycle(useInstr) } or - TNonPhiMemoryOperand(Instruction useInstr, MemoryOperandTag tag, Instruction defInstr, Overlap overlap) { + TNonPhiMemoryOperand( + Instruction useInstr, MemoryOperandTag tag, Instruction defInstr, Overlap overlap + ) { defInstr = Construction::getMemoryOperandDefinition(useInstr, tag, overlap) and not isInCycle(useInstr) } or - TPhiOperand(PhiInstruction useInstr, Instruction defInstr, IRBlock predecessorBlock, Overlap overlap) { + TPhiOperand( + PhiInstruction useInstr, Instruction defInstr, IRBlock predecessorBlock, Overlap overlap + ) { defInstr = Construction::getPhiOperandDefinition(useInstr, predecessorBlock, overlap) } @@ -46,24 +50,16 @@ private predicate isInCycle(Instruction instr) { * A source operand of an `Instruction`. The operand represents a value consumed by the instruction. */ class Operand extends TOperand { - string toString() { - result = "Operand" - } + string toString() { result = "Operand" } - final Language::Location getLocation() { - result = getUse().getLocation() - } + final Language::Location getLocation() { result = getUse().getLocation() } - final IRFunction getEnclosingIRFunction() { - result = getUse().getEnclosingIRFunction() - } + final IRFunction getEnclosingIRFunction() { result = getUse().getEnclosingIRFunction() } /** * Gets the `Instruction` that consumes this operand. */ - Instruction getUse() { - none() - } + Instruction getUse() { none() } /** * Gets the `Instruction` whose result is the value of the operand. Unlike @@ -71,9 +67,7 @@ class Operand extends TOperand { * means that the resulting instruction may only _partially_ or _potentially_ * be the value of this operand. */ - Instruction getAnyDef() { - none() - } + Instruction getAnyDef() { none() } /** * Gets the `Instruction` whose result is the value of the operand. Unlike @@ -91,10 +85,7 @@ class Operand extends TOperand { * * Gets the `Instruction` that consumes this operand. */ - deprecated - final Instruction getUseInstruction() { - result = getUse() - } + deprecated final Instruction getUseInstruction() { result = getUse() } /** * DEPRECATED: use `getAnyDef` or `getDef`. The exact replacement for this @@ -103,31 +94,22 @@ class Operand extends TOperand { * * Gets the `Instruction` whose result is the value of the operand. */ - deprecated - final Instruction getDefinitionInstruction() { - result = getAnyDef() - } + deprecated final Instruction getDefinitionInstruction() { result = getAnyDef() } /** * Gets the overlap relationship between the operand's definition and its use. */ - Overlap getDefinitionOverlap() { - none() - } + Overlap getDefinitionOverlap() { none() } /** * Holds if the result of the definition instruction does not exactly overlap this use. */ - final predicate isDefinitionInexact() { - not getDefinitionOverlap() instanceof MustExactlyOverlap - } + final predicate isDefinitionInexact() { not getDefinitionOverlap() instanceof MustExactlyOverlap } /** * Gets a prefix to use when dumping the operand in an operand list. */ - string getDumpLabel() { - result = "" - } + string getDumpLabel() { result = "" } /** * Gets a string describing this operand, suitable for display in IR dumps. This consists of the @@ -146,18 +128,13 @@ class Operand extends TOperand { * the empty string. */ private string getInexactSpecifier() { - if isDefinitionInexact() then - result = "~" - else - result = "" + if isDefinitionInexact() then result = "~" else result = "" } /** * Get the order in which the operand should be sorted in the operand list. */ - int getDumpSortOrder() { - result = -1 - } + int getDumpSortOrder() { result = -1 } /** * Gets the type of the value consumed by this operand. This is usually the same as the @@ -166,9 +143,7 @@ class Operand extends TOperand { * the definition type, such as in the case of a partial read or a read from a pointer that * has been cast to a different type. */ - Language::Type getType() { - result = getAnyDef().getResultType() - } + Language::Type getType() { result = getAnyDef().getResultType() } /** * Holds if the value consumed by this operand is a glvalue. If this @@ -177,17 +152,13 @@ class Operand extends TOperand { * not hold, the value of the operand represents a value whose type is * given by `getResultType()`. */ - predicate isGLValue() { - getAnyDef().isGLValue() - } + predicate isGLValue() { getAnyDef().isGLValue() } /** * Gets the size of the value consumed by this operand, in bytes. If the operand does not have * a known constant size, this predicate does not hold. */ - int getSize() { - result = Language::getTypeSize(getType()) - } + int getSize() { result = Language::getTypeSize(getType()) } } /** @@ -207,9 +178,7 @@ class MemoryOperand extends Operand { /** * Gets the kind of memory access performed by the operand. */ - MemoryAccessKind getMemoryAccess() { - none() - } + MemoryAccessKind getMemoryAccess() { none() } /** * Returns the operand that holds the memory address from which the current operand loads its @@ -227,7 +196,9 @@ class MemoryOperand extends Operand { */ class NonPhiOperand extends Operand { Instruction useInstr; + Instruction defInstr; + OperandTag tag; NonPhiOperand() { @@ -235,25 +206,15 @@ class NonPhiOperand extends Operand { this = TNonPhiMemoryOperand(useInstr, tag, defInstr, _) } - override final Instruction getUse() { - result = useInstr - } + final override Instruction getUse() { result = useInstr } - override final Instruction getAnyDef() { - result = defInstr - } + final override Instruction getAnyDef() { result = defInstr } - override final string getDumpLabel() { - result = tag.getLabel() - } + final override string getDumpLabel() { result = tag.getLabel() } - override final int getDumpSortOrder() { - result = tag.getSortOrder() - } + final override int getDumpSortOrder() { result = tag.getSortOrder() } - final OperandTag getOperandTag() { - result = tag - } + final OperandTag getOperandTag() { result = tag } } /** @@ -262,7 +223,7 @@ class NonPhiOperand extends Operand { class RegisterOperand extends NonPhiOperand, TRegisterOperand { override RegisterOperandTag tag; - override final Overlap getDefinitionOverlap() { + final override Overlap getDefinitionOverlap() { // All register results overlap exactly with their uses. result instanceof MustExactlyOverlap } @@ -270,21 +231,18 @@ class RegisterOperand extends NonPhiOperand, TRegisterOperand { class NonPhiMemoryOperand extends NonPhiOperand, MemoryOperand, TNonPhiMemoryOperand { override MemoryOperandTag tag; + Overlap overlap; - NonPhiMemoryOperand() { - this = TNonPhiMemoryOperand(useInstr, tag, defInstr, overlap) - } + NonPhiMemoryOperand() { this = TNonPhiMemoryOperand(useInstr, tag, defInstr, overlap) } - override final Overlap getDefinitionOverlap() { - result = overlap - } + final override Overlap getDefinitionOverlap() { result = overlap } } class TypedOperand extends NonPhiMemoryOperand { override TypedOperandTag tag; - override final Language::Type getType() { + final override Language::Type getType() { result = Construction::getInstructionOperandType(useInstr, tag) } } @@ -296,9 +254,7 @@ class TypedOperand extends NonPhiMemoryOperand { class AddressOperand extends RegisterOperand { override AddressOperandTag tag; - override string toString() { - result = "Address" - } + override string toString() { result = "Address" } } /** @@ -308,13 +264,9 @@ class AddressOperand extends RegisterOperand { class LoadOperand extends TypedOperand { override LoadOperandTag tag; - override string toString() { - result = "Load" - } + override string toString() { result = "Load" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof IndirectMemoryAccess } } /** @@ -323,9 +275,7 @@ class LoadOperand extends TypedOperand { class StoreValueOperand extends RegisterOperand { override StoreValueOperandTag tag; - override string toString() { - result = "StoreValue" - } + override string toString() { result = "StoreValue" } } /** @@ -334,9 +284,7 @@ class StoreValueOperand extends RegisterOperand { class UnaryOperand extends RegisterOperand { override UnaryOperandTag tag; - override string toString() { - result = "Unary" - } + override string toString() { result = "Unary" } } /** @@ -345,9 +293,7 @@ class UnaryOperand extends RegisterOperand { class LeftOperand extends RegisterOperand { override LeftOperandTag tag; - override string toString() { - result = "Left" - } + override string toString() { result = "Left" } } /** @@ -356,9 +302,7 @@ class LeftOperand extends RegisterOperand { class RightOperand extends RegisterOperand { override RightOperandTag tag; - override string toString() { - result = "Right" - } + override string toString() { result = "Right" } } /** @@ -367,9 +311,7 @@ class RightOperand extends RegisterOperand { class ConditionOperand extends RegisterOperand { override ConditionOperandTag tag; - override string toString() { - result = "Condition" - } + override string toString() { result = "Condition" } } /** @@ -379,13 +321,9 @@ class ConditionOperand extends RegisterOperand { class UnmodeledUseOperand extends NonPhiMemoryOperand { override UnmodeledUseOperandTag tag; - override string toString() { - result = "UnmodeledUse" - } + override string toString() { result = "UnmodeledUse" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof UnmodeledMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof UnmodeledMemoryAccess } } /** @@ -394,9 +332,7 @@ class UnmodeledUseOperand extends NonPhiMemoryOperand { class CallTargetOperand extends RegisterOperand { override CallTargetOperandTag tag; - override string toString() { - result = "CallTarget" - } + override string toString() { result = "CallTarget" } } /** @@ -415,9 +351,7 @@ class ArgumentOperand extends RegisterOperand { class ThisArgumentOperand extends ArgumentOperand { override ThisArgumentOperandTag tag; - override string toString() { - result = "ThisArgument" - } + override string toString() { result = "ThisArgument" } } /** @@ -425,32 +359,26 @@ class ThisArgumentOperand extends ArgumentOperand { */ class PositionalArgumentOperand extends ArgumentOperand { override PositionalArgumentOperandTag tag; + int argIndex; - PositionalArgumentOperand() { - argIndex = tag.getArgIndex() - } + PositionalArgumentOperand() { argIndex = tag.getArgIndex() } - override string toString() { - result = "Arg(" + argIndex + ")" - } + override string toString() { result = "Arg(" + argIndex + ")" } /** * Gets the zero-based index of the argument. */ - final int getIndex() { - result = argIndex - } + final int getIndex() { result = argIndex } } class SideEffectOperand extends TypedOperand { override SideEffectOperandTag tag; - override final int getSize() { - if getType() instanceof Language::UnknownType then - result = Construction::getInstructionOperandSize(useInstr, tag) - else - result = Language::getTypeSize(getType()) + final override int getSize() { + if getType() instanceof Language::UnknownType + then result = Construction::getInstructionOperandSize(useInstr, tag) + else result = Language::getTypeSize(getType()) } override MemoryAccessKind getMemoryAccess() { @@ -485,48 +413,35 @@ class SideEffectOperand extends TypedOperand { */ class PhiInputOperand extends MemoryOperand, TPhiOperand { PhiInstruction useInstr; + Instruction defInstr; + IRBlock predecessorBlock; + Overlap overlap; - PhiInputOperand() { - this = TPhiOperand(useInstr, defInstr, predecessorBlock, overlap) - } + PhiInputOperand() { this = TPhiOperand(useInstr, defInstr, predecessorBlock, overlap) } - override string toString() { - result = "Phi" - } + override string toString() { result = "Phi" } - override final PhiInstruction getUse() { - result = useInstr - } + final override PhiInstruction getUse() { result = useInstr } - override final Instruction getAnyDef() { - result = defInstr - } + final override Instruction getAnyDef() { result = defInstr } - override final Overlap getDefinitionOverlap() { - result = overlap - } + final override Overlap getDefinitionOverlap() { result = overlap } - override final int getDumpSortOrder() { - result = 11 + getPredecessorBlock().getDisplayIndex() - } + final override int getDumpSortOrder() { result = 11 + getPredecessorBlock().getDisplayIndex() } - override final string getDumpLabel() { + final override string getDumpLabel() { result = "from " + getPredecessorBlock().getDisplayIndex().toString() + ":" } /** * Gets the predecessor block from which this value comes. */ - final IRBlock getPredecessorBlock() { - result = predecessorBlock - } + final IRBlock getPredecessorBlock() { result = predecessorBlock } - override final MemoryAccessKind getMemoryAccess() { - result instanceof PhiMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof PhiMemoryAccess } } /** @@ -535,27 +450,18 @@ class PhiInputOperand extends MemoryOperand, TPhiOperand { class ChiTotalOperand extends NonPhiMemoryOperand { override ChiTotalOperandTag tag; - override string toString() { - result = "ChiTotal" - } + override string toString() { result = "ChiTotal" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof ChiTotalMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof ChiTotalMemoryAccess } } - /** * The partial operand of a Chi node, representing the value being written to part of the memory. */ class ChiPartialOperand extends NonPhiMemoryOperand { override ChiPartialOperandTag tag; - override string toString() { - result = "ChiPartial" - } + override string toString() { result = "ChiPartial" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof ChiPartialMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof ChiPartialMemoryAccess } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/PrintIR.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/PrintIR.qll index 4bad5e3fd3e..c24756a2212 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/PrintIR.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/PrintIR.qll @@ -9,23 +9,17 @@ private newtype TPrintIRConfiguration = MkPrintIRConfiguration() * The query can extend this class to control which functions are printed. */ class PrintIRConfiguration extends TPrintIRConfiguration { - string toString() { - result = "PrintIRConfiguration" - } + string toString() { result = "PrintIRConfiguration" } /** * Holds if the IR for `func` should be printed. By default, holds for all * functions. */ - predicate shouldPrintFunction(Language::Function func) { - any() - } + predicate shouldPrintFunction(Language::Function func) { any() } } private predicate shouldPrintFunction(Language::Function func) { - exists(PrintIRConfiguration config | - config.shouldPrintFunction(func) - ) + exists(PrintIRConfiguration config | config.shouldPrintFunction(func)) } /** @@ -38,27 +32,17 @@ private class FilteredIRConfiguration extends IRConfiguration { } private string getAdditionalInstructionProperty(Instruction instr, string key) { - exists(IRPropertyProvider provider | - result = provider.getInstructionProperty(instr, key) - ) + exists(IRPropertyProvider provider | result = provider.getInstructionProperty(instr, key)) } private string getAdditionalBlockProperty(IRBlock block, string key) { - exists(IRPropertyProvider provider | - result = provider.getBlockProperty(block, key) - ) + exists(IRPropertyProvider provider | result = provider.getBlockProperty(block, key)) } private newtype TPrintableIRNode = - TPrintableIRFunction(IRFunction irFunc) { - shouldPrintFunction(irFunc.getFunction()) - } or - TPrintableIRBlock(IRBlock block) { - shouldPrintFunction(block.getEnclosingFunction()) - } or - TPrintableInstruction(Instruction instr) { - shouldPrintFunction(instr.getEnclosingFunction()) - } + TPrintableIRFunction(IRFunction irFunc) { shouldPrintFunction(irFunc.getFunction()) } or + TPrintableIRBlock(IRBlock block) { shouldPrintFunction(block.getEnclosingFunction()) } or + TPrintableInstruction(Instruction instr) { shouldPrintFunction(instr.getEnclosingFunction()) } /** * A node to be emitted in the IR graph. @@ -85,29 +69,28 @@ abstract class PrintableIRNode extends TPrintableIRNode { * Gets the parent of this node. */ abstract PrintableIRNode getParent(); - + /** * Gets the kind of graph represented by this node ("graph" or "tree"). */ - string getGraphKind() { - none() - } + string getGraphKind() { none() } /** * Holds if this node should always be rendered as text, even in a graphical * viewer. */ - predicate forceText() { - none() - } + predicate forceText() { none() } /** * Gets the value of the node property with the specified key. */ string getProperty(string key) { - key = "semmle.label" and result = getLabel() or - key = "semmle.order" and result = getOrder().toString() or - key = "semmle.graphKind" and result = getGraphKind() or + key = "semmle.label" and result = getLabel() + or + key = "semmle.order" and result = getOrder().toString() + or + key = "semmle.graphKind" and result = getGraphKind() + or key = "semmle.forceText" and forceText() and result = "true" } } @@ -118,37 +101,28 @@ abstract class PrintableIRNode extends TPrintableIRNode { class PrintableIRFunction extends PrintableIRNode, TPrintableIRFunction { IRFunction irFunc; - PrintableIRFunction() { - this = TPrintableIRFunction(irFunc) - } + PrintableIRFunction() { this = TPrintableIRFunction(irFunc) } - override string toString() { - result = irFunc.toString() - } + override string toString() { result = irFunc.toString() } - override Language::Location getLocation() { - result = irFunc.getLocation() - } + override Language::Location getLocation() { result = irFunc.getLocation() } - override string getLabel() { - result = Language::getIdentityString(irFunc.getFunction()) - } + override string getLabel() { result = Language::getIdentityString(irFunc.getFunction()) } override int getOrder() { this = rank[result + 1](PrintableIRFunction orderedFunc, Language::Location location | - location = orderedFunc.getIRFunction().getLocation() | - orderedFunc order by location.getFile().getAbsolutePath(), location.getStartLine(), - location.getStartColumn(), orderedFunc.getLabel() - ) + location = orderedFunc.getIRFunction().getLocation() + | + orderedFunc + order by + location.getFile().getAbsolutePath(), location.getStartLine(), location.getStartColumn(), + orderedFunc.getLabel() + ) } - override final PrintableIRNode getParent() { - none() - } + final override PrintableIRNode getParent() { none() } - final IRFunction getIRFunction() { - result = irFunc - } + final IRFunction getIRFunction() { result = irFunc } } /** @@ -157,35 +131,21 @@ class PrintableIRFunction extends PrintableIRNode, TPrintableIRFunction { class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock { IRBlock block; - PrintableIRBlock() { - this = TPrintableIRBlock(block) - } + PrintableIRBlock() { this = TPrintableIRBlock(block) } - override string toString() { - result = getLabel() - } + override string toString() { result = getLabel() } - override Language::Location getLocation() { - result = block.getLocation() - } + override Language::Location getLocation() { result = block.getLocation() } - override string getLabel() { - result = "Block " + block.getDisplayIndex().toString() - } + override string getLabel() { result = "Block " + block.getDisplayIndex().toString() } - override int getOrder() { - result = block.getDisplayIndex() - } + override int getOrder() { result = block.getDisplayIndex() } - override final string getGraphKind() { - result = "tree" - } + final override string getGraphKind() { result = "tree" } - override final predicate forceText() { - any() - } + final override predicate forceText() { any() } - override final PrintableIRFunction getParent() { + final override PrintableIRFunction getParent() { result.getIRFunction() = block.getEnclosingIRFunction() } @@ -194,9 +154,7 @@ class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock { result = getAdditionalBlockProperty(block, key) } - final IRBlock getBlock() { - result = block - } + final IRBlock getBlock() { result = block } } /** @@ -205,45 +163,35 @@ class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock { class PrintableInstruction extends PrintableIRNode, TPrintableInstruction { Instruction instr; - PrintableInstruction() { - this = TPrintableInstruction(instr) - } + PrintableInstruction() { this = TPrintableInstruction(instr) } - override string toString() { - result = instr.toString() - } + override string toString() { result = instr.toString() } - override Language::Location getLocation() { - result = instr.getLocation() - } + override Language::Location getLocation() { result = instr.getLocation() } override string getLabel() { exists(IRBlock block | instr = block.getAnInstruction() and - exists(string resultString, string operationString, string operandsString, - int resultWidth, int operationWidth | + exists( + string resultString, string operationString, string operandsString, int resultWidth, + int operationWidth + | resultString = instr.getResultString() and operationString = instr.getOperationString() and operandsString = instr.getOperandsString() and columnWidths(block, resultWidth, operationWidth) and - result = resultString + getPaddingString(resultWidth - resultString.length()) + - " = " + operationString + getPaddingString(operationWidth - operationString.length()) + - " : " + operandsString + result = resultString + getPaddingString(resultWidth - resultString.length()) + " = " + + operationString + getPaddingString(operationWidth - operationString.length()) + " : " + + operandsString ) ) } - override int getOrder() { - result = instr.getDisplayIndexInBlock() - } + override int getOrder() { result = instr.getDisplayIndexInBlock() } - override final PrintableIRBlock getParent() { - result.getBlock() = instr.getBlock() - } + final override PrintableIRBlock getParent() { result.getBlock() = instr.getBlock() } - final Instruction getInstruction() { - result = instr - } + final Instruction getInstruction() { result = instr } override string getProperty(string key) { result = PrintableIRNode.super.getProperty(key) or @@ -253,19 +201,26 @@ class PrintableInstruction extends PrintableIRNode, TPrintableInstruction { private predicate columnWidths(IRBlock block, int resultWidth, int operationWidth) { resultWidth = max(Instruction instr | instr.getBlock() = block | instr.getResultString().length()) and - operationWidth = max(Instruction instr | instr.getBlock() = block | instr.getOperationString().length()) + operationWidth = max(Instruction instr | + instr.getBlock() = block + | + instr.getOperationString().length() + ) } private int maxColumnWidth() { result = max(Instruction instr, int width | - width = instr.getResultString().length() or - width = instr.getOperationString().length() or - width = instr.getOperandsString().length() | - width) + width = instr.getResultString().length() or + width = instr.getOperationString().length() or + width = instr.getOperandsString().length() + | + width + ) } private string getPaddingString(int n) { - n = 0 and result = "" or + n = 0 and result = "" + or n > 0 and n <= maxColumnWidth() and result = getPaddingString(n - 1) + " " } @@ -275,9 +230,10 @@ query predicate nodes(PrintableIRNode node, string key, string value) { private int getSuccessorIndex(IRBlock pred, IRBlock succ) { succ = rank[result + 1](IRBlock aSucc, EdgeKind kind | - aSucc = pred.getSuccessor(kind) | - aSucc order by kind.toString() - ) + aSucc = pred.getSuccessor(kind) + | + aSucc order by kind.toString() + ) } query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key, string value) { @@ -291,11 +247,10 @@ query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key, if predBlock.getBackEdgeSuccessor(kind) = succBlock then value = kind.toString() + " (back edge)" else value = kind.toString() - ) or - ( - key = "semmle.order" and - value = getSuccessorIndex(predBlock, succBlock).toString() ) + or + key = "semmle.order" and + value = getSuccessorIndex(predBlock, succBlock).toString() ) ) } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IR.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IR.qll index 5bc9493f4ab..278040f8ab8 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IR.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IR.qll @@ -15,21 +15,15 @@ private newtype TIRPropertyProvider = MkIRPropertyProvider() * single instance of this class to specify the additional properties computed by the library. */ class IRPropertyProvider extends TIRPropertyProvider { - string toString() { - result = "IRPropertyProvider" - } + string toString() { result = "IRPropertyProvider" } /** * Gets the value of the property named `key` for the specified instruction. */ - string getInstructionProperty(Instruction instruction, string key) { - none() - } + string getInstructionProperty(Instruction instruction, string key) { none() } /** * Gets the value of the property named `key` for the specified block. */ - string getBlockProperty(IRBlock block, string key) { - none() - } + string getBlockProperty(IRBlock block, string key) { none() } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRBlock.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRBlock.qll index c62a2dbc5ea..e0322a00e15 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRBlock.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRBlock.qll @@ -16,32 +16,25 @@ private import Cached * Most consumers should use the class `IRBlock`. */ class IRBlockBase extends TIRBlock { - final string toString() { - result = getFirstInstruction(this).toString() - } + final string toString() { result = getFirstInstruction(this).toString() } + + final Language::Location getLocation() { result = getFirstInstruction().getLocation() } + + final string getUniqueId() { result = getFirstInstruction(this).getUniqueId() } - final Language::Location getLocation() { - result = getFirstInstruction().getLocation() - } - - final string getUniqueId() { - result = getFirstInstruction(this).getUniqueId() - } - /** * Gets the zero-based index of the block within its function. This is used * by debugging and printing code only. */ int getDisplayIndex() { this = rank[result + 1](IRBlock funcBlock | - funcBlock.getEnclosingFunction() = getEnclosingFunction() | - funcBlock order by funcBlock.getUniqueId() - ) + funcBlock.getEnclosingFunction() = getEnclosingFunction() + | + funcBlock order by funcBlock.getUniqueId() + ) } - final Instruction getInstruction(int index) { - result = getInstruction(this, index) - } + final Instruction getInstruction(int index) { result = getInstruction(this, index) } final PhiInstruction getAPhiInstruction() { Construction::getPhiInstructionBlockStart(result) = getFirstInstruction() @@ -52,17 +45,11 @@ class IRBlockBase extends TIRBlock { result = getAPhiInstruction() } - final Instruction getFirstInstruction() { - result = getFirstInstruction(this) - } + final Instruction getFirstInstruction() { result = getFirstInstruction(this) } - final Instruction getLastInstruction() { - result = getInstruction(getInstructionCount() - 1) - } + final Instruction getLastInstruction() { result = getInstruction(getInstructionCount() - 1) } - final int getInstructionCount() { - result = getInstructionCount(this) - } + final int getInstructionCount() { result = getInstructionCount(this) } final IRFunction getEnclosingIRFunction() { result = getFirstInstruction(this).getEnclosingIRFunction() @@ -79,40 +66,26 @@ class IRBlockBase extends TIRBlock { * instruction of another block. */ class IRBlock extends IRBlockBase { - final IRBlock getASuccessor() { - blockSuccessor(this, result) - } + final IRBlock getASuccessor() { blockSuccessor(this, result) } - final IRBlock getAPredecessor() { - blockSuccessor(result, this) - } + final IRBlock getAPredecessor() { blockSuccessor(result, this) } - final IRBlock getSuccessor(EdgeKind kind) { - blockSuccessor(this, result, kind) - } + final IRBlock getSuccessor(EdgeKind kind) { blockSuccessor(this, result, kind) } - final IRBlock getBackEdgeSuccessor(EdgeKind kind) { - backEdgeSuccessor(this, result, kind) - } + final IRBlock getBackEdgeSuccessor(EdgeKind kind) { backEdgeSuccessor(this, result, kind) } - final predicate immediatelyDominates(IRBlock block) { - blockImmediatelyDominates(this, block) - } + final predicate immediatelyDominates(IRBlock block) { blockImmediatelyDominates(this, block) } - final predicate strictlyDominates(IRBlock block) { - blockImmediatelyDominates+(this, block) - } + final predicate strictlyDominates(IRBlock block) { blockImmediatelyDominates+(this, block) } - final predicate dominates(IRBlock block) { - strictlyDominates(block) or this = block - } + final predicate dominates(IRBlock block) { strictlyDominates(block) or this = block } pragma[noinline] final IRBlock dominanceFrontier() { dominates(result.getAPredecessor()) and not strictlyDominates(result) } - + /** * Holds if this block is reachable from the entry point of its function */ @@ -125,22 +98,21 @@ class IRBlock extends IRBlockBase { private predicate startsBasicBlock(Instruction instr) { not instr instanceof PhiInstruction and ( - count(Instruction predecessor | - instr = predecessor.getASuccessor() - ) != 1 or // Multiple predecessors or no predecessor + count(Instruction predecessor | instr = predecessor.getASuccessor()) != 1 // Multiple predecessors or no predecessor + or exists(Instruction predecessor | instr = predecessor.getASuccessor() and - strictcount(Instruction other | - other = predecessor.getASuccessor() - ) > 1 - ) or // Predecessor has multiple successors + strictcount(Instruction other | other = predecessor.getASuccessor()) > 1 + ) // Predecessor has multiple successors + or exists(Instruction predecessor, EdgeKind kind | instr = predecessor.getSuccessor(kind) and not kind instanceof GotoEdge - ) or // Incoming edge is not a GotoEdge + ) // Incoming edge is not a GotoEdge + or exists(Instruction predecessor | instr = Construction::getInstructionBackEdgeSuccessor(predecessor, _) - ) // A back edge enters this instruction + ) // A back edge enters this instruction ) } @@ -148,11 +120,10 @@ private predicate isEntryBlock(TIRBlock block) { block = MkIRBlock(any(EnterFunctionInstruction enter)) } -private cached module Cached { - cached newtype TIRBlock = - MkIRBlock(Instruction firstInstr) { - startsBasicBlock(firstInstr) - } +cached +private module Cached { + cached + newtype TIRBlock = MkIRBlock(Instruction firstInstr) { startsBasicBlock(firstInstr) } /** Holds if `i2` follows `i1` in a `IRBlock`. */ private predicate adjacentInBlock(Instruction i1, Instruction i2) { @@ -165,15 +136,16 @@ private cached module Cached { shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index) /** Holds if `i` is the `index`th instruction in `block`. */ - cached Instruction getInstruction(TIRBlock block, int index) { + cached + Instruction getInstruction(TIRBlock block, int index) { result = getInstructionFromFirst(getFirstInstruction(block), index) } - cached int getInstructionCount(TIRBlock block) { - result = strictcount(getInstruction(block, _)) - } + cached + int getInstructionCount(TIRBlock block) { result = strictcount(getInstruction(block, _)) } - cached predicate blockSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { + cached + predicate blockSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { exists(Instruction predLast, Instruction succFirst | predLast = getInstruction(pred, getInstructionCount(pred) - 1) and succFirst = predLast.getSuccessor(kind) and @@ -185,7 +157,8 @@ private cached module Cached { private predicate blockIdentity(TIRBlock b1, TIRBlock b2) { b1 = b2 } pragma[noopt] - cached predicate backEdgeSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { + cached + predicate backEdgeSuccessor(TIRBlock pred, TIRBlock succ, EdgeKind kind) { backEdgeSuccessorRaw(pred, succ, kind) or // See the QLDoc on `backEdgeSuccessorRaw`. @@ -226,14 +199,12 @@ private cached module Cached { ) } - cached predicate blockSuccessor(TIRBlock pred, TIRBlock succ) { - blockSuccessor(pred, succ, _) - } + cached + predicate blockSuccessor(TIRBlock pred, TIRBlock succ) { blockSuccessor(pred, succ, _) } - cached predicate blockImmediatelyDominates(TIRBlock dominator, TIRBlock block) = + cached + predicate blockImmediatelyDominates(TIRBlock dominator, TIRBlock block) = idominance(isEntryBlock/1, blockSuccessor/2)(_, dominator, block) } -Instruction getFirstInstruction(TIRBlock block) { - block = MkIRBlock(result) -} +Instruction getFirstInstruction(TIRBlock block) { block = MkIRBlock(result) } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRFunction.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRFunction.qll index 1dd61fb9db1..1e9c2d1d913 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRFunction.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRFunction.qll @@ -2,9 +2,7 @@ private import internal.IRInternal import Instruction private newtype TIRFunction = - MkIRFunction(Language::Function func) { - Construction::functionHasIR(func) - } + MkIRFunction(Language::Function func) { Construction::functionHasIR(func) } /** * Represents the IR for a function. @@ -12,27 +10,19 @@ private newtype TIRFunction = class IRFunction extends TIRFunction { Language::Function func; - IRFunction() { - this = MkIRFunction(func) - } + IRFunction() { this = MkIRFunction(func) } - final string toString() { - result = "IR: " + func.toString() - } + final string toString() { result = "IR: " + func.toString() } /** * Gets the function whose IR is represented. */ - final Language::Function getFunction() { - result = func - } + final Language::Function getFunction() { result = func } /** * Gets the location of the function. */ - final Language::Location getLocation() { - result = func.getLocation() - } + final Language::Location getLocation() { result = func.getLocation() } /** * Gets the entry point for this function. @@ -64,38 +54,28 @@ class IRFunction extends TIRFunction { * Gets the single return instruction for this function. */ pragma[noinline] - final ReturnInstruction getReturnInstruction() { - result.getEnclosingIRFunction() = this - } + final ReturnInstruction getReturnInstruction() { result.getEnclosingIRFunction() = this } /** * Gets the variable used to hold the return value of this function. If this * function does not return a value, this predicate does not hold. */ pragma[noinline] - final IRReturnVariable getReturnVariable() { - result.getEnclosingIRFunction() = this - } - + final IRReturnVariable getReturnVariable() { result.getEnclosingIRFunction() = this } + /** * Gets the block containing the entry point of this function. - */ + */ pragma[noinline] - final IRBlock getEntryBlock() { - result.getFirstInstruction() = getEnterFunctionInstruction() - } + final IRBlock getEntryBlock() { result.getFirstInstruction() = getEnterFunctionInstruction() } /** * Gets all instructions in this function. */ - final Instruction getAnInstruction() { - result.getEnclosingIRFunction() = this - } + final Instruction getAnInstruction() { result.getEnclosingIRFunction() = this } /** * Gets all blocks in this function. */ - final IRBlock getABlock() { - result.getEnclosingIRFunction() = this - } + final IRBlock getABlock() { result.getEnclosingIRFunction() = this } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRSanity.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRSanity.qll index 9e21452c1fb..3921472dc8e 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRSanity.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRSanity.qll @@ -1,3 +1,2 @@ private import IR import InstructionSanity - diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRVariable.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRVariable.qll index b8c6af20a60..2c1b43672fc 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRVariable.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRVariable.qll @@ -37,27 +37,21 @@ abstract class IRVariable extends TIRVariable { * within the function. */ abstract string getUniqueId(); - + /** * Gets the source location of this variable. */ - final Language::Location getLocation() { - result = getAST().getLocation() - } + final Language::Location getLocation() { result = getAST().getLocation() } /** * Gets the IR for the function that references this variable. */ - final IRFunction getEnclosingIRFunction() { - result.getFunction() = func - } + final IRFunction getEnclosingIRFunction() { result.getFunction() = func } /** * Gets the function that references this variable. */ - final Language::Function getEnclosingFunction() { - result = func - } + final Language::Function getEnclosingFunction() { result = func } } /** @@ -65,34 +59,25 @@ abstract class IRVariable extends TIRVariable { */ class IRUserVariable extends IRVariable, TIRUserVariable { Language::Variable var; + Language::Type type; - IRUserVariable() { - this = TIRUserVariable(var, type, func) - } + IRUserVariable() { this = TIRUserVariable(var, type, func) } - override final string toString() { - result = getVariable().toString() - } + final override string toString() { result = getVariable().toString() } - override final Language::AST getAST() { - result = var - } + final override Language::AST getAST() { result = var } - override final string getUniqueId() { + final override string getUniqueId() { result = getVariable().toString() + " " + getVariable().getLocation().toString() } - override final Language::Type getType() { - result = type - } + final override Language::Type getType() { result = type } /** * Gets the original user-declared variable. */ - Language::Variable getVariable() { - result = var - } + Language::Variable getVariable() { result = var } } /** @@ -100,31 +85,22 @@ class IRUserVariable extends IRVariable, TIRUserVariable { * stack. This includes all parameters, non-static local variables, and * temporary variables. */ -abstract class IRAutomaticVariable extends IRVariable { -} +abstract class IRAutomaticVariable extends IRVariable { } class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable { override Language::AutomaticVariable var; - IRAutomaticUserVariable() { - Language::isVariableAutomatic(var) - } + IRAutomaticUserVariable() { Language::isVariableAutomatic(var) } - final override Language::AutomaticVariable getVariable() { - result = var - } + final override Language::AutomaticVariable getVariable() { result = var } } class IRStaticUserVariable extends IRUserVariable { override Language::StaticVariable var; - IRStaticUserVariable() { - not Language::isVariableAutomatic(var) - } + IRStaticUserVariable() { not Language::isVariableAutomatic(var) } - final override Language::StaticVariable getVariable() { - result = var - } + final override Language::StaticVariable getVariable() { result = var } } IRTempVariable getIRTempVariable(Language::AST ast, TempVariableTag tag) { @@ -134,55 +110,39 @@ IRTempVariable getIRTempVariable(Language::AST ast, TempVariableTag tag) { class IRTempVariable extends IRVariable, IRAutomaticVariable, TIRTempVariable { Language::AST ast; + TempVariableTag tag; + Language::Type type; - IRTempVariable() { - this = TIRTempVariable(func, ast, tag, type) - } + IRTempVariable() { this = TIRTempVariable(func, ast, tag, type) } - override final Language::Type getType() { - result = type - } + final override Language::Type getType() { result = type } - override final Language::AST getAST() { - result = ast - } + final override Language::AST getAST() { result = ast } - override final string getUniqueId() { + final override string getUniqueId() { result = "Temp: " + Construction::getTempVariableUniqueId(this) } - final TempVariableTag getTag() { - result = tag - } + final TempVariableTag getTag() { result = tag } override string toString() { result = getBaseString() + ast.getLocation().getStartLine().toString() + ":" + - ast.getLocation().getStartColumn().toString() + ast.getLocation().getStartColumn().toString() } - string getBaseString() { - result = "#temp" - } + string getBaseString() { result = "#temp" } } class IRReturnVariable extends IRTempVariable { - IRReturnVariable() { - tag = ReturnValueTempVar() - } + IRReturnVariable() { tag = ReturnValueTempVar() } - override final string toString() { - result = "#return" - } + final override string toString() { result = "#return" } } class IRThrowVariable extends IRTempVariable { - IRThrowVariable() { - tag = ThrowTempVar() - } + IRThrowVariable() { tag = ThrowTempVar() } - override string getBaseString() { - result = "#throw" - } + override string getBaseString() { result = "#throw" } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll index 7b621c95dcc..ea0a3f72998 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll @@ -20,27 +20,38 @@ module InstructionSanity { exists(Opcode opcode | opcode = instr.getOpcode() and ( - opcode instanceof UnaryOpcode and tag instanceof UnaryOperandTag or + opcode instanceof UnaryOpcode and tag instanceof UnaryOperandTag + or + opcode instanceof BinaryOpcode and ( - opcode instanceof BinaryOpcode and - ( - tag instanceof LeftOperandTag or - tag instanceof RightOperandTag - ) - ) or - opcode instanceof MemoryAccessOpcode and tag instanceof AddressOperandTag or - opcode instanceof BufferAccessOpcode and tag instanceof BufferSizeOperand or - opcode instanceof OpcodeWithCondition and tag instanceof ConditionOperandTag or - opcode instanceof OpcodeWithLoad and tag instanceof LoadOperandTag or - opcode instanceof Opcode::Store and tag instanceof StoreValueOperandTag or - opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag or - opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag or - opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag or - opcode instanceof Opcode::Chi and tag instanceof ChiPartialOperandTag or - ( - (opcode instanceof ReadSideEffectOpcode or opcode instanceof MayWriteSideEffectOpcode or opcode instanceof Opcode::InlineAsm) and - tag instanceof SideEffectOperandTag + tag instanceof LeftOperandTag or + tag instanceof RightOperandTag ) + or + opcode instanceof MemoryAccessOpcode and tag instanceof AddressOperandTag + or + opcode instanceof BufferAccessOpcode and tag instanceof BufferSizeOperand + or + opcode instanceof OpcodeWithCondition and tag instanceof ConditionOperandTag + or + opcode instanceof OpcodeWithLoad and tag instanceof LoadOperandTag + or + opcode instanceof Opcode::Store and tag instanceof StoreValueOperandTag + or + opcode instanceof Opcode::UnmodeledUse and tag instanceof UnmodeledUseOperandTag + or + opcode instanceof Opcode::Call and tag instanceof CallTargetOperandTag + or + opcode instanceof Opcode::Chi and tag instanceof ChiTotalOperandTag + or + opcode instanceof Opcode::Chi and tag instanceof ChiPartialOperandTag + or + ( + opcode instanceof ReadSideEffectOpcode or + opcode instanceof MayWriteSideEffectOpcode or + opcode instanceof Opcode::InlineAsm + ) and + tag instanceof SideEffectOperandTag ) ) } @@ -55,8 +66,8 @@ module InstructionSanity { operand = instr.getAnOperand() and operand.getOperandTag() = tag ) and - message = "Instruction '" + instr.getOpcode().toString() + "' is missing an expected operand with tag '" + - tag.toString() + "' in function '$@'." and + message = "Instruction '" + instr.getOpcode().toString() + + "' is missing an expected operand with tag '" + tag.toString() + "' in function '$@'." and func = instr.getEnclosingIRFunction() and funcText = Language::getIdentityString(func.getFunction()) ) @@ -68,10 +79,13 @@ module InstructionSanity { query predicate unexpectedOperand(Instruction instr, OperandTag tag) { exists(NonPhiOperand operand | operand = instr.getAnOperand() and - operand.getOperandTag() = tag) and + operand.getOperandTag() = tag + ) and not expectsOperand(instr, tag) and not (instr instanceof CallInstruction and tag instanceof ArgumentOperandTag) and - not (instr instanceof BuiltInOperationInstruction and tag instanceof PositionalArgumentOperandTag) and + not ( + instr instanceof BuiltInOperationInstruction and tag instanceof PositionalArgumentOperandTag + ) and not (instr instanceof InlineAsmInstruction and tag instanceof AsmOperandTag) } @@ -121,9 +135,7 @@ module InstructionSanity { * Holds if there are multiple (`n`) edges of kind `kind` from `source`, * where `target` is among the targets of those edges. */ - query predicate ambiguousSuccessors( - Instruction source, EdgeKind kind, int n, Instruction target - ) { + query predicate ambiguousSuccessors(Instruction source, EdgeKind kind, int n, Instruction target) { n = strictcount(Instruction t | source.getSuccessor(kind) = t) and n > 1 and source.getSuccessor(kind) = target @@ -222,11 +234,10 @@ module InstructionSanity { * of evaluation is at the end of the corresponding predecessor block. */ private predicate pointOfEvaluation(Operand operand, IRBlock block, int index) { - ( - block = operand.(PhiInputOperand).getPredecessorBlock() and - index = block.getInstructionCount() - ) or - exists (Instruction use | + block = operand.(PhiInputOperand).getPredecessorBlock() and + index = block.getInstructionCount() + or + exists(Instruction use | use = operand.(NonPhiOperand).getUse() and block.getInstruction(index) = use ) @@ -235,31 +246,28 @@ module InstructionSanity { /** * Holds if `useOperand` has a definition that does not dominate the use. */ - query predicate useNotDominatedByDefinition(Operand useOperand, string message, IRFunction func, - string funcText) { - - exists (IRBlock useBlock, int useIndex, Instruction defInstr, IRBlock defBlock, int defIndex | + query predicate useNotDominatedByDefinition( + Operand useOperand, string message, IRFunction func, string funcText + ) { + exists(IRBlock useBlock, int useIndex, Instruction defInstr, IRBlock defBlock, int defIndex | not useOperand.getUse() instanceof UnmodeledUseInstruction and pointOfEvaluation(useOperand, useBlock, useIndex) and defInstr = useOperand.getAnyDef() and ( - ( - defInstr instanceof PhiInstruction and - defBlock = defInstr.getBlock() and - defIndex = -1 - ) + defInstr instanceof PhiInstruction and + defBlock = defInstr.getBlock() and + defIndex = -1 or - defBlock.getInstruction(defIndex) = defInstr + defBlock.getInstruction(defIndex) = defInstr ) and not ( - defBlock.strictlyDominates(useBlock) or - ( - defBlock = useBlock and - defIndex < useIndex - ) + defBlock.strictlyDominates(useBlock) + or + defBlock = useBlock and + defIndex < useIndex ) and message = "Operand '" + useOperand.toString() + - "' is not dominated by its definition in function '$@'." and + "' is not dominated by its definition in function '$@'." and func = useOperand.getEnclosingIRFunction() and funcText = Language::getIdentityString(func.getFunction()) ) @@ -270,9 +278,7 @@ module InstructionSanity { * Represents a single operation in the IR. */ class Instruction extends Construction::TInstruction { - final string toString() { - result = getOpcode().toString() + ": " + getAST().toString() - } + final string toString() { result = getOpcode().toString() + ": " + getAST().toString() } /** * Gets a string showing the result, opcode, and operands of the instruction, equivalent to what @@ -291,36 +297,27 @@ class Instruction extends Construction::TInstruction { * VariableAddress[x] */ final string getOperationString() { - if exists(getImmediateString()) then - result = getOperationPrefix() + getOpcode().toString() + "[" + getImmediateString() + "]" - else - result = getOperationPrefix() + getOpcode().toString() + if exists(getImmediateString()) + then result = getOperationPrefix() + getOpcode().toString() + "[" + getImmediateString() + "]" + else result = getOperationPrefix() + getOpcode().toString() } /** * Gets a string describing the immediate value of this instruction, if any. */ - string getImmediateString() { - none() - } + string getImmediateString() { none() } private string getOperationPrefix() { - if this instanceof SideEffectInstruction then - result = "^" - else - result = "" + if this instanceof SideEffectInstruction then result = "^" else result = "" } private string getResultPrefix() { - if getResultType() instanceof Language::VoidType then - result = "v" - else if hasMemoryResult() then - if isResultModeled() then - result = "m" - else - result = "mu" + if getResultType() instanceof Language::VoidType + then result = "v" else - result = "r" + if hasMemoryResult() + then if isResultModeled() then result = "m" else result = "mu" + else result = "r" } /** @@ -335,36 +332,32 @@ class Instruction extends Construction::TInstruction { phiCount = count(block.getAPhiInstruction()) and this = block.getInstruction(index) and result = index + phiCount - ) or - ( - this instanceof PhiInstruction and - this = rank[result + 1](PhiInstruction phiInstr | - phiInstr = block.getAPhiInstruction() | + ) + or + this instanceof PhiInstruction and + this = rank[result + 1](PhiInstruction phiInstr | + phiInstr = block.getAPhiInstruction() + | phiInstr order by phiInstr.getUniqueId() ) - ) ) ) } bindingset[type] private string getValueCategoryString(string type) { - if isGLValue() then - result = "glval<" + type + ">" - else - result = type + if isGLValue() then result = "glval<" + type + ">" else result = type } string getResultTypeString() { exists(string valcat | valcat = getValueCategoryString(getResultType().toString()) and - if (getResultType() instanceof Language::UnknownType and - not isGLValue() and - exists(getResultSize())) then ( - result = valcat + "[" + getResultSize().toString() + "]" - ) - else - result = valcat + if + getResultType() instanceof Language::UnknownType and + not isGLValue() and + exists(getResultSize()) + then result = valcat + "[" + getResultSize().toString() + "]" + else result = valcat ) } @@ -377,7 +370,7 @@ class Instruction extends Construction::TInstruction { */ string getResultId() { result = getResultPrefix() + getBlock().getDisplayIndex().toString() + "_" + - getDisplayIndexInBlock().toString() + getDisplayIndexInBlock().toString() } /** @@ -387,9 +380,7 @@ class Instruction extends Construction::TInstruction { * * Example: `r1_1(int*)` */ - final string getResultString() { - result = getResultId() + "(" + getResultTypeString() + ")" - } + final string getResultString() { result = getResultId() + "(" + getResultTypeString() + ")" } /** * Gets a string describing the operands of this instruction, suitable for @@ -399,9 +390,10 @@ class Instruction extends Construction::TInstruction { */ string getOperandsString() { result = concat(Operand operand | - operand = getAnOperand() | - operand.getDumpString(), ", " order by operand.getDumpSortOrder() - ) + operand = getAnOperand() + | + operand.getDumpString(), ", " order by operand.getDumpSortOrder() + ) } /** @@ -411,16 +403,12 @@ class Instruction extends Construction::TInstruction { * This is used for sorting IR output for tests, and is likely to be * inefficient for any other use. */ - final string getUniqueId() { - result = Construction::getInstructionUniqueId(this) - } + final string getUniqueId() { result = Construction::getInstructionUniqueId(this) } /** * Gets the basic block that contains this instruction. */ - final IRBlock getBlock() { - result.getAnInstruction() = this - } + final IRBlock getBlock() { result.getAnInstruction() = this } /** * Gets the function that contains this instruction. @@ -439,31 +427,27 @@ class Instruction extends Construction::TInstruction { /** * Gets the AST that caused this instruction to be generated. */ - final Language::AST getAST() { - result = Construction::getInstructionAST(this) - } + final Language::AST getAST() { result = Construction::getInstructionAST(this) } /** * Gets the location of the source code for this instruction. */ - final Language::Location getLocation() { - result = getAST().getLocation() - } + final Language::Location getLocation() { result = getAST().getLocation() } /** * Gets the `Expr` whose result is computed by this instruction, if any. */ final Language::Expr getConvertedResultExpression() { - result = Construction::getInstructionConvertedResultExpression(this) + result = Construction::getInstructionConvertedResultExpression(this) } - + /** * Gets the unconverted `Expr` whose result is computed by this instruction, if any. */ final Language::Expr getUnconvertedResultExpression() { - result = Construction::getInstructionUnconvertedResultExpression(this) + result = Construction::getInstructionUnconvertedResultExpression(this) } - + /** * Gets the type of the result produced by this instruction. If the * instruction does not produce a result, its result type will be `VoidType`. @@ -471,9 +455,7 @@ class Instruction extends Construction::TInstruction { * If `isGLValue()` holds, then the result type of this instruction should be * thought of as "pointer to `getResultType()`". */ - final Language::Type getResultType() { - Construction::instructionHasType(this, result, _) - } + final Language::Type getResultType() { Construction::instructionHasType(this, result, _) } /** * Holds if the result produced by this instruction is a glvalue. If this @@ -493,9 +475,7 @@ class Instruction extends Construction::TInstruction { * result of the `Load` instruction is a prvalue of type `int`, representing * the integer value loaded from variable `x`. */ - final predicate isGLValue() { - Construction::instructionHasType(this, _, true) - } + final predicate isGLValue() { Construction::instructionHasType(this, _, true) } /** * Gets the size of the result produced by this instruction, in bytes. If the @@ -505,53 +485,42 @@ class Instruction extends Construction::TInstruction { * `getResultSize()` will always be the size of a pointer. */ final int getResultSize() { - if isGLValue() then ( + if isGLValue() + then // a glvalue is always pointer-sized. result = Language::getPointerSize() - ) - else if getResultType() instanceof Language::UnknownType then - result = Construction::getInstructionResultSize(this) - else ( - result = Language::getTypeSize(getResultType()) - ) + else + if getResultType() instanceof Language::UnknownType + then result = Construction::getInstructionResultSize(this) + else result = Language::getTypeSize(getResultType()) } /** * Gets the opcode that specifies the operation performed by this instruction. */ - final Opcode getOpcode() { - result = Construction::getInstructionOpcode(this) - } + final Opcode getOpcode() { result = Construction::getInstructionOpcode(this) } /** * Gets all direct uses of the result of this instruction. The result can be * an `Operand` for which `isDefinitionInexact` holds. */ - final Operand getAUse() { - result.getAnyDef() = this - } + final Operand getAUse() { result.getAnyDef() = this } /** * Gets all of this instruction's operands. */ - final Operand getAnOperand() { - result.getUse() = this - } + final Operand getAnOperand() { result.getUse() = this } /** * Holds if this instruction produces a memory result. */ - final predicate hasMemoryResult() { - exists(getResultMemoryAccess()) - } + final predicate hasMemoryResult() { exists(getResultMemoryAccess()) } /** * Gets the kind of memory access performed by this instruction's result. * Holds only for instructions with a memory result. */ - MemoryAccessKind getResultMemoryAccess() { - none() - } + MemoryAccessKind getResultMemoryAccess() { none() } /** * Gets the operand that holds the memory address to which this instruction stores its @@ -568,9 +537,7 @@ class Instruction extends Construction::TInstruction { * result, if any. For example, in `m3 = Store r1, r2`, the result of `getResultAddressOperand()` * is the instruction that defines `r1`. */ - final Instruction getResultAddress() { - result = getResultAddressOperand().getDef() - } + final Instruction getResultAddress() { result = getResultAddressOperand().getDef() } /** * Holds if the result of this instruction is precisely modeled in SSA. Always @@ -623,137 +590,89 @@ class Instruction extends Construction::TInstruction { /** * Gets all direct successors of this instruction. */ - final Instruction getASuccessor() { - result = getSuccessor(_) - } + final Instruction getASuccessor() { result = getSuccessor(_) } /** * Gets a predecessor of this instruction such that the predecessor reaches * this instruction along the control flow edge specified by `kind`. */ - final Instruction getPredecessor(EdgeKind kind) { - result.getSuccessor(kind) = this - } + final Instruction getPredecessor(EdgeKind kind) { result.getSuccessor(kind) = this } /** * Gets all direct predecessors of this instruction. */ - final Instruction getAPredecessor() { - result = getPredecessor(_) - } + final Instruction getAPredecessor() { result = getPredecessor(_) } } class VariableInstruction extends Instruction { IRVariable var; - VariableInstruction() { - var = Construction::getInstructionVariable(this) - } + VariableInstruction() { var = Construction::getInstructionVariable(this) } - override final string getImmediateString() { - result = var.toString() - } + final override string getImmediateString() { result = var.toString() } - final IRVariable getVariable() { - result = var - } + final IRVariable getVariable() { result = var } } class FieldInstruction extends Instruction { Language::Field field; - FieldInstruction() { - field = Construction::getInstructionField(this) - } + FieldInstruction() { field = Construction::getInstructionField(this) } - override final string getImmediateString() { - result = field.toString() - } + final override string getImmediateString() { result = field.toString() } - final Language::Field getField() { - result = field - } + final Language::Field getField() { result = field } } class FunctionInstruction extends Instruction { Language::Function funcSymbol; - FunctionInstruction() { - funcSymbol = Construction::getInstructionFunction(this) - } + FunctionInstruction() { funcSymbol = Construction::getInstructionFunction(this) } - override final string getImmediateString() { - result = funcSymbol.toString() - } + final override string getImmediateString() { result = funcSymbol.toString() } - final Language::Function getFunctionSymbol() { - result = funcSymbol - } + final Language::Function getFunctionSymbol() { result = funcSymbol } } class ConstantValueInstruction extends Instruction { string value; - ConstantValueInstruction() { - value = Construction::getInstructionConstantValue(this) - } + ConstantValueInstruction() { value = Construction::getInstructionConstantValue(this) } - override final string getImmediateString() { - result = value - } + final override string getImmediateString() { result = value } - final string getValue() { - result = value - } + final string getValue() { result = value } } class EnterFunctionInstruction extends Instruction { - EnterFunctionInstruction() { - getOpcode() instanceof Opcode::EnterFunction - } + EnterFunctionInstruction() { getOpcode() instanceof Opcode::EnterFunction } } class VariableAddressInstruction extends VariableInstruction { - VariableAddressInstruction() { - getOpcode() instanceof Opcode::VariableAddress - } + VariableAddressInstruction() { getOpcode() instanceof Opcode::VariableAddress } } class InitializeParameterInstruction extends VariableInstruction { - InitializeParameterInstruction() { - getOpcode() instanceof Opcode::InitializeParameter - } + InitializeParameterInstruction() { getOpcode() instanceof Opcode::InitializeParameter } - final Language::Parameter getParameter() { - result = var.(IRUserVariable).getVariable() - } + final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } } /** * An instruction that initializes the `this` pointer parameter of the enclosing function. */ class InitializeThisInstruction extends Instruction { - InitializeThisInstruction() { - getOpcode() instanceof Opcode::InitializeThis - } + InitializeThisInstruction() { getOpcode() instanceof Opcode::InitializeThis } } class FieldAddressInstruction extends FieldInstruction { - FieldAddressInstruction() { - getOpcode() instanceof Opcode::FieldAddress - } + FieldAddressInstruction() { getOpcode() instanceof Opcode::FieldAddress } - final UnaryOperand getObjectAddressOperand() { - result = getAnOperand() - } + final UnaryOperand getObjectAddressOperand() { result = getAnOperand() } - final Instruction getObjectAddress() { - result = getObjectAddressOperand().getDef() - } + final Instruction getObjectAddress() { result = getObjectAddressOperand().getDef() } } /** @@ -767,207 +686,125 @@ class FieldAddressInstruction extends FieldInstruction { * taken may want to ignore any function that contains an `ErrorInstruction`. */ class ErrorInstruction extends Instruction { - ErrorInstruction() { - getOpcode() instanceof Opcode::Error - } + ErrorInstruction() { getOpcode() instanceof Opcode::Error } } class UninitializedInstruction extends VariableInstruction { - UninitializedInstruction() { - getOpcode() instanceof Opcode::Uninitialized - } + UninitializedInstruction() { getOpcode() instanceof Opcode::Uninitialized } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } /** * Gets the variable that is uninitialized. */ - final Language::Variable getLocalVariable() { - result = var.(IRUserVariable).getVariable() - } + final Language::Variable getLocalVariable() { result = var.(IRUserVariable).getVariable() } } class NoOpInstruction extends Instruction { - NoOpInstruction() { - getOpcode() instanceof Opcode::NoOp - } + NoOpInstruction() { getOpcode() instanceof Opcode::NoOp } } class ReturnInstruction extends Instruction { - ReturnInstruction() { - getOpcode() instanceof ReturnOpcode - } + ReturnInstruction() { getOpcode() instanceof ReturnOpcode } } class ReturnVoidInstruction extends ReturnInstruction { - ReturnVoidInstruction() { - getOpcode() instanceof Opcode::ReturnVoid - } + ReturnVoidInstruction() { getOpcode() instanceof Opcode::ReturnVoid } } class ReturnValueInstruction extends ReturnInstruction { - ReturnValueInstruction() { - getOpcode() instanceof Opcode::ReturnValue - } + ReturnValueInstruction() { getOpcode() instanceof Opcode::ReturnValue } - final LoadOperand getReturnValueOperand() { - result = getAnOperand() - } - - final Instruction getReturnValue() { - result = getReturnValueOperand().getDef() - } + final LoadOperand getReturnValueOperand() { result = getAnOperand() } + + final Instruction getReturnValue() { result = getReturnValueOperand().getDef() } } class CopyInstruction extends Instruction { - CopyInstruction() { - getOpcode() instanceof CopyOpcode - } + CopyInstruction() { getOpcode() instanceof CopyOpcode } - Operand getSourceValueOperand() { - none() - } + Operand getSourceValueOperand() { none() } - final Instruction getSourceValue() { - result = getSourceValueOperand().getDef() - } + final Instruction getSourceValue() { result = getSourceValueOperand().getDef() } } class CopyValueInstruction extends CopyInstruction, UnaryInstruction { - CopyValueInstruction() { - getOpcode() instanceof Opcode::CopyValue - } + CopyValueInstruction() { getOpcode() instanceof Opcode::CopyValue } - override final UnaryOperand getSourceValueOperand() { - result = getAnOperand() - } + final override UnaryOperand getSourceValueOperand() { result = getAnOperand() } } class LoadInstruction extends CopyInstruction { - LoadInstruction() { - getOpcode() instanceof Opcode::Load - } + LoadInstruction() { getOpcode() instanceof Opcode::Load } - final AddressOperand getSourceAddressOperand() { - result = getAnOperand() - } - - final Instruction getSourceAddress() { - result = getSourceAddressOperand().getDef() - } + final AddressOperand getSourceAddressOperand() { result = getAnOperand() } - override final LoadOperand getSourceValueOperand() { - result = getAnOperand() - } + final Instruction getSourceAddress() { result = getSourceAddressOperand().getDef() } + + final override LoadOperand getSourceValueOperand() { result = getAnOperand() } } class StoreInstruction extends CopyInstruction { - StoreInstruction() { - getOpcode() instanceof Opcode::Store - } + StoreInstruction() { getOpcode() instanceof Opcode::Store } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } - final AddressOperand getDestinationAddressOperand() { - result = getAnOperand() - } - - final Instruction getDestinationAddress() { - result = getDestinationAddressOperand().getDef() - } + final AddressOperand getDestinationAddressOperand() { result = getAnOperand() } - override final StoreValueOperand getSourceValueOperand() { - result = getAnOperand() - } + final Instruction getDestinationAddress() { result = getDestinationAddressOperand().getDef() } + + final override StoreValueOperand getSourceValueOperand() { result = getAnOperand() } } class ConditionalBranchInstruction extends Instruction { - ConditionalBranchInstruction() { - getOpcode() instanceof Opcode::ConditionalBranch - } + ConditionalBranchInstruction() { getOpcode() instanceof Opcode::ConditionalBranch } - final ConditionOperand getConditionOperand() { - result = getAnOperand() - } + final ConditionOperand getConditionOperand() { result = getAnOperand() } - final Instruction getCondition() { - result = getConditionOperand().getDef() - } + final Instruction getCondition() { result = getConditionOperand().getDef() } - final Instruction getTrueSuccessor() { - result = getSuccessor(trueEdge()) - } + final Instruction getTrueSuccessor() { result = getSuccessor(trueEdge()) } - final Instruction getFalseSuccessor() { - result = getSuccessor(falseEdge()) - } + final Instruction getFalseSuccessor() { result = getSuccessor(falseEdge()) } } class ExitFunctionInstruction extends Instruction { - ExitFunctionInstruction() { - getOpcode() instanceof Opcode::ExitFunction - } + ExitFunctionInstruction() { getOpcode() instanceof Opcode::ExitFunction } } class ConstantInstruction extends ConstantValueInstruction { - ConstantInstruction() { - getOpcode() instanceof Opcode::Constant - } + ConstantInstruction() { getOpcode() instanceof Opcode::Constant } } class IntegerConstantInstruction extends ConstantInstruction { - IntegerConstantInstruction() { - getResultType() instanceof Language::IntegralType - } + IntegerConstantInstruction() { getResultType() instanceof Language::IntegralType } } class FloatConstantInstruction extends ConstantInstruction { - FloatConstantInstruction() { - getResultType() instanceof Language::FloatingPointType - } + FloatConstantInstruction() { getResultType() instanceof Language::FloatingPointType } } class StringConstantInstruction extends Instruction { Language::StringLiteral value; - StringConstantInstruction() { - value = Construction::getInstructionStringLiteral(this) - } + StringConstantInstruction() { value = Construction::getInstructionStringLiteral(this) } - override final string getImmediateString() { - result = Language::getStringLiteralText(value) - } + final override string getImmediateString() { result = Language::getStringLiteralText(value) } - final Language::StringLiteral getValue() { - result = value - } + final Language::StringLiteral getValue() { result = value } } class BinaryInstruction extends Instruction { - BinaryInstruction() { - getOpcode() instanceof BinaryOpcode - } + BinaryInstruction() { getOpcode() instanceof BinaryOpcode } - final LeftOperand getLeftOperand() { - result = getAnOperand() - } - - final RightOperand getRightOperand() { - result = getAnOperand() - } + final LeftOperand getLeftOperand() { result = getAnOperand() } - final Instruction getLeft() { - result = getLeftOperand().getDef() - } + final RightOperand getRightOperand() { result = getAnOperand() } + + final Instruction getLeft() { result = getLeftOperand().getDef() } + + final Instruction getRight() { result = getRightOperand().getDef() } - final Instruction getRight() { - result = getRightOperand().getDef() - } - /** * Holds if this instruction's operands are `op1` and `op2`, in either order. */ @@ -979,89 +816,63 @@ class BinaryInstruction extends Instruction { } class ArithmeticInstruction extends Instruction { - ArithmeticInstruction() { - getOpcode() instanceof ArithmeticOpcode - } + ArithmeticInstruction() { getOpcode() instanceof ArithmeticOpcode } } -class BinaryArithmeticInstruction extends ArithmeticInstruction, BinaryInstruction {} +class BinaryArithmeticInstruction extends ArithmeticInstruction, BinaryInstruction { } -class UnaryArithmeticInstruction extends ArithmeticInstruction, UnaryInstruction {} +class UnaryArithmeticInstruction extends ArithmeticInstruction, UnaryInstruction { } class AddInstruction extends BinaryArithmeticInstruction { - AddInstruction() { - getOpcode() instanceof Opcode::Add - } + AddInstruction() { getOpcode() instanceof Opcode::Add } } class SubInstruction extends BinaryArithmeticInstruction { - SubInstruction() { - getOpcode() instanceof Opcode::Sub - } + SubInstruction() { getOpcode() instanceof Opcode::Sub } } class MulInstruction extends BinaryArithmeticInstruction { - MulInstruction() { - getOpcode() instanceof Opcode::Mul - } + MulInstruction() { getOpcode() instanceof Opcode::Mul } } class DivInstruction extends BinaryArithmeticInstruction { - DivInstruction() { - getOpcode() instanceof Opcode::Div - } + DivInstruction() { getOpcode() instanceof Opcode::Div } } class RemInstruction extends BinaryArithmeticInstruction { - RemInstruction() { - getOpcode() instanceof Opcode::Rem - } + RemInstruction() { getOpcode() instanceof Opcode::Rem } } class NegateInstruction extends UnaryArithmeticInstruction { - NegateInstruction() { - getOpcode() instanceof Opcode::Negate - } + NegateInstruction() { getOpcode() instanceof Opcode::Negate } } class BitwiseInstruction extends Instruction { - BitwiseInstruction() { - getOpcode() instanceof BitwiseOpcode - } + BitwiseInstruction() { getOpcode() instanceof BitwiseOpcode } } -class BinaryBitwiseInstruction extends BitwiseInstruction, BinaryInstruction {} +class BinaryBitwiseInstruction extends BitwiseInstruction, BinaryInstruction { } -class UnaryBitwiseInstruction extends BitwiseInstruction, UnaryInstruction {} +class UnaryBitwiseInstruction extends BitwiseInstruction, UnaryInstruction { } class BitAndInstruction extends BinaryBitwiseInstruction { - BitAndInstruction() { - getOpcode() instanceof Opcode::BitAnd - } + BitAndInstruction() { getOpcode() instanceof Opcode::BitAnd } } class BitOrInstruction extends BinaryBitwiseInstruction { - BitOrInstruction() { - getOpcode() instanceof Opcode::BitOr - } + BitOrInstruction() { getOpcode() instanceof Opcode::BitOr } } class BitXorInstruction extends BinaryBitwiseInstruction { - BitXorInstruction() { - getOpcode() instanceof Opcode::BitXor - } + BitXorInstruction() { getOpcode() instanceof Opcode::BitXor } } class ShiftLeftInstruction extends BinaryBitwiseInstruction { - ShiftLeftInstruction() { - getOpcode() instanceof Opcode::ShiftLeft - } + ShiftLeftInstruction() { getOpcode() instanceof Opcode::ShiftLeft } } class ShiftRightInstruction extends BinaryBitwiseInstruction { - ShiftRightInstruction() { - getOpcode() instanceof Opcode::ShiftRight - } + ShiftRightInstruction() { getOpcode() instanceof Opcode::ShiftRight } } class PointerArithmeticInstruction extends BinaryInstruction { @@ -1072,57 +883,37 @@ class PointerArithmeticInstruction extends BinaryInstruction { elementSize = Construction::getInstructionElementSize(this) } - override final string getImmediateString() { - result = elementSize.toString() - } + final override string getImmediateString() { result = elementSize.toString() } - final int getElementSize() { - result = elementSize - } + final int getElementSize() { result = elementSize } } class PointerOffsetInstruction extends PointerArithmeticInstruction { - PointerOffsetInstruction() { - getOpcode() instanceof PointerOffsetOpcode - } + PointerOffsetInstruction() { getOpcode() instanceof PointerOffsetOpcode } } class PointerAddInstruction extends PointerOffsetInstruction { - PointerAddInstruction() { - getOpcode() instanceof Opcode::PointerAdd - } + PointerAddInstruction() { getOpcode() instanceof Opcode::PointerAdd } } class PointerSubInstruction extends PointerOffsetInstruction { - PointerSubInstruction() { - getOpcode() instanceof Opcode::PointerSub - } + PointerSubInstruction() { getOpcode() instanceof Opcode::PointerSub } } class PointerDiffInstruction extends PointerArithmeticInstruction { - PointerDiffInstruction() { - getOpcode() instanceof Opcode::PointerDiff - } + PointerDiffInstruction() { getOpcode() instanceof Opcode::PointerDiff } } class UnaryInstruction extends Instruction { - UnaryInstruction() { - getOpcode() instanceof UnaryOpcode - } + UnaryInstruction() { getOpcode() instanceof UnaryOpcode } - final UnaryOperand getUnaryOperand() { - result = getAnOperand() - } - - final Instruction getUnary() { - result = getUnaryOperand().getDef() - } + final UnaryOperand getUnaryOperand() { result = getAnOperand() } + + final Instruction getUnary() { result = getUnaryOperand().getDef() } } class ConvertInstruction extends UnaryInstruction { - ConvertInstruction() { - getOpcode() instanceof Opcode::Convert - } + ConvertInstruction() { getOpcode() instanceof Opcode::Convert } } /** @@ -1131,13 +922,14 @@ class ConvertInstruction extends UnaryInstruction { */ class InheritanceConversionInstruction extends UnaryInstruction { Language::Class baseClass; + Language::Class derivedClass; InheritanceConversionInstruction() { Construction::getInstructionInheritance(this, baseClass, derivedClass) } - override final string getImmediateString() { + final override string getImmediateString() { result = derivedClass.toString() + " : " + baseClass.toString() } @@ -1155,16 +947,12 @@ class InheritanceConversionInstruction extends UnaryInstruction { * base class of the derived class, or a virtual base class of the * derived class. */ - final Language::Class getBaseClass() { - result = baseClass - } + final Language::Class getBaseClass() { result = baseClass } /** * Gets the derived class of the conversion. */ - final Language::Class getDerivedClass() { - result = derivedClass - } + final Language::Class getDerivedClass() { result = derivedClass } } /** @@ -1172,9 +960,7 @@ class InheritanceConversionInstruction extends UnaryInstruction { * to the address of a direct non-virtual base class. */ class ConvertToBaseInstruction extends InheritanceConversionInstruction { - ConvertToBaseInstruction() { - getOpcode() instanceof Opcode::ConvertToBase - } + ConvertToBaseInstruction() { getOpcode() instanceof Opcode::ConvertToBase } } /** @@ -1182,9 +968,7 @@ class ConvertToBaseInstruction extends InheritanceConversionInstruction { * to the address of a virtual base class. */ class ConvertToVirtualBaseInstruction extends InheritanceConversionInstruction { - ConvertToVirtualBaseInstruction() { - getOpcode() instanceof Opcode::ConvertToVirtualBase - } + ConvertToVirtualBaseInstruction() { getOpcode() instanceof Opcode::ConvertToVirtualBase } } /** @@ -1192,48 +976,34 @@ class ConvertToVirtualBaseInstruction extends InheritanceConversionInstruction { * to the address of a direct non-virtual derived class. */ class ConvertToDerivedInstruction extends InheritanceConversionInstruction { - ConvertToDerivedInstruction() { - getOpcode() instanceof Opcode::ConvertToDerived - } + ConvertToDerivedInstruction() { getOpcode() instanceof Opcode::ConvertToDerived } } class BitComplementInstruction extends UnaryBitwiseInstruction { - BitComplementInstruction() { - getOpcode() instanceof Opcode::BitComplement - } + BitComplementInstruction() { getOpcode() instanceof Opcode::BitComplement } } class LogicalNotInstruction extends UnaryInstruction { - LogicalNotInstruction() { - getOpcode() instanceof Opcode::LogicalNot - } + LogicalNotInstruction() { getOpcode() instanceof Opcode::LogicalNot } } class CompareInstruction extends BinaryInstruction { - CompareInstruction() { - getOpcode() instanceof CompareOpcode - } + CompareInstruction() { getOpcode() instanceof CompareOpcode } } class CompareEQInstruction extends CompareInstruction { - CompareEQInstruction() { - getOpcode() instanceof Opcode::CompareEQ - } + CompareEQInstruction() { getOpcode() instanceof Opcode::CompareEQ } } class CompareNEInstruction extends CompareInstruction { - CompareNEInstruction() { - getOpcode() instanceof Opcode::CompareNE - } + CompareNEInstruction() { getOpcode() instanceof Opcode::CompareNE } } /** * Represents an instruction that does a relative comparison of two values, such as `<` or `>=`. */ class RelationalInstruction extends CompareInstruction { - RelationalInstruction() { - getOpcode() instanceof RelationalOpcode - } + RelationalInstruction() { getOpcode() instanceof RelationalOpcode } /** * Gets the operand on the "greater" (or "greater-or-equal") side @@ -1241,9 +1011,7 @@ class RelationalInstruction extends CompareInstruction { * if the overall instruction evaluates to `true`; for example on * `x <= 20` this is the `20`, and on `y > 0` it is `y`. */ - Instruction getGreater() { - none() - } + Instruction getGreater() { none() } /** * Gets the operand on the "lesser" (or "lesser-or-equal") side @@ -1251,144 +1019,88 @@ class RelationalInstruction extends CompareInstruction { * if the overall instruction evaluates to `true`; for example on * `x <= 20` this is `x`, and on `y > 0` it is the `0`. */ - Instruction getLesser() { - none() - } + Instruction getLesser() { none() } /** * Holds if this relational instruction is strict (is not an "or-equal" instruction). */ - predicate isStrict() { - none() - } + predicate isStrict() { none() } } class CompareLTInstruction extends RelationalInstruction { - CompareLTInstruction() { - getOpcode() instanceof Opcode::CompareLT - } + CompareLTInstruction() { getOpcode() instanceof Opcode::CompareLT } - override Instruction getLesser() { - result = getLeft() - } + override Instruction getLesser() { result = getLeft() } - override Instruction getGreater() { - result = getRight() - } + override Instruction getGreater() { result = getRight() } - override predicate isStrict() { - any() - } + override predicate isStrict() { any() } } class CompareGTInstruction extends RelationalInstruction { - CompareGTInstruction() { - getOpcode() instanceof Opcode::CompareGT - } + CompareGTInstruction() { getOpcode() instanceof Opcode::CompareGT } - override Instruction getLesser() { - result = getRight() - } + override Instruction getLesser() { result = getRight() } - override Instruction getGreater() { - result = getLeft() - } + override Instruction getGreater() { result = getLeft() } - override predicate isStrict() { - any() - } + override predicate isStrict() { any() } } class CompareLEInstruction extends RelationalInstruction { - CompareLEInstruction() { - getOpcode() instanceof Opcode::CompareLE - } + CompareLEInstruction() { getOpcode() instanceof Opcode::CompareLE } - override Instruction getLesser() { - result = getLeft() - } + override Instruction getLesser() { result = getLeft() } - override Instruction getGreater() { - result = getRight() - } + override Instruction getGreater() { result = getRight() } - override predicate isStrict() { - none() - } + override predicate isStrict() { none() } } class CompareGEInstruction extends RelationalInstruction { - CompareGEInstruction() { - getOpcode() instanceof Opcode::CompareGE - } + CompareGEInstruction() { getOpcode() instanceof Opcode::CompareGE } - override Instruction getLesser() { - result = getRight() - } + override Instruction getLesser() { result = getRight() } - override Instruction getGreater() { - result = getLeft() - } + override Instruction getGreater() { result = getLeft() } - override predicate isStrict() { - none() - } + override predicate isStrict() { none() } } class SwitchInstruction extends Instruction { - SwitchInstruction() { - getOpcode() instanceof Opcode::Switch - } + SwitchInstruction() { getOpcode() instanceof Opcode::Switch } - final ConditionOperand getExpressionOperand() { - result = getAnOperand() - } + final ConditionOperand getExpressionOperand() { result = getAnOperand() } - final Instruction getExpression() { - result = getExpressionOperand().getDef() - } + final Instruction getExpression() { result = getExpressionOperand().getDef() } - final Instruction getACaseSuccessor() { - exists(CaseEdge edge | - result = getSuccessor(edge) - ) - } + final Instruction getACaseSuccessor() { exists(CaseEdge edge | result = getSuccessor(edge)) } - final Instruction getDefaultSuccessor() { - result = getSuccessor(defaultEdge()) - } + final Instruction getDefaultSuccessor() { result = getSuccessor(defaultEdge()) } } /** * An instruction that calls a function. */ class CallInstruction extends Instruction { - CallInstruction() { - getOpcode() instanceof Opcode::Call - } + CallInstruction() { getOpcode() instanceof Opcode::Call } /** * Gets the operand the specifies the target function of the call. */ - final CallTargetOperand getCallTargetOperand() { - result = getAnOperand() - } + final CallTargetOperand getCallTargetOperand() { result = getAnOperand() } /** * Gets the `Instruction` that computes the target function of the call. This is usually a * `FunctionAddress` instruction, but can also be an arbitrary instruction that produces a * function pointer. */ - final Instruction getCallTarget() { - result = getCallTargetOperand().getDef() - } + final Instruction getCallTarget() { result = getCallTargetOperand().getDef() } /** * Gets all of the argument operands of the call, including the `this` pointer, if any. */ - final ArgumentOperand getAnArgumentOperand() { - result = getAnOperand() - } + final ArgumentOperand getAnArgumentOperand() { result = getAnOperand() } /** * Gets the `Function` that the call targets, if this is statically known. @@ -1400,23 +1112,17 @@ class CallInstruction extends Instruction { /** * Gets all of the arguments of the call, including the `this` pointer, if any. */ - final Instruction getAnArgument() { - result = getAnArgumentOperand().getDef() - } + final Instruction getAnArgument() { result = getAnArgumentOperand().getDef() } /** * Gets the `this` pointer argument operand of the call, if any. */ - final ThisArgumentOperand getThisArgumentOperand() { - result = getAnOperand() - } + final ThisArgumentOperand getThisArgumentOperand() { result = getAnOperand() } /** * Gets the `this` pointer argument of the call, if any. */ - final Instruction getThisArgument() { - result = getThisArgumentOperand().getDef() - } + final Instruction getThisArgument() { result = getThisArgumentOperand().getDef() } /** * Gets the argument operand at the specified index. @@ -1438,9 +1144,7 @@ class CallInstruction extends Instruction { * An instruction representing a side effect of a function call. */ class SideEffectInstruction extends Instruction { - SideEffectInstruction() { - getOpcode() instanceof SideEffectOpcode - } + SideEffectInstruction() { getOpcode() instanceof SideEffectOpcode } final Instruction getPrimaryInstruction() { result = Construction::getPrimaryInstructionForSideEffect(this) @@ -1452,11 +1156,9 @@ class SideEffectInstruction extends Instruction { * accessed by that call. */ class CallSideEffectInstruction extends SideEffectInstruction { - CallSideEffectInstruction() { - getOpcode() instanceof Opcode::CallSideEffect - } + CallSideEffectInstruction() { getOpcode() instanceof Opcode::CallSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof EscapedMayMemoryAccess } } @@ -1466,40 +1168,30 @@ class CallSideEffectInstruction extends SideEffectInstruction { * by that call. */ class CallReadSideEffectInstruction extends SideEffectInstruction { - CallReadSideEffectInstruction() { - getOpcode() instanceof Opcode::CallReadSideEffect - } + CallReadSideEffectInstruction() { getOpcode() instanceof Opcode::CallReadSideEffect } } /** * An instruction representing the read of an indirect parameter within a function call. */ class IndirectReadSideEffectInstruction extends SideEffectInstruction { - IndirectReadSideEffectInstruction() { - getOpcode() instanceof Opcode::IndirectReadSideEffect - } + IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect } } /** * An instruction representing the read of an indirect buffer parameter within a function call. */ class BufferReadSideEffectInstruction extends SideEffectInstruction { - BufferReadSideEffectInstruction() { - getOpcode() instanceof Opcode::BufferReadSideEffect - } + BufferReadSideEffectInstruction() { getOpcode() instanceof Opcode::BufferReadSideEffect } } /** * An instruction representing the write of an indirect parameter within a function call. */ class IndirectWriteSideEffectInstruction extends SideEffectInstruction { - IndirectWriteSideEffectInstruction() { - getOpcode() instanceof Opcode::IndirectWriteSideEffect - } + IndirectWriteSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMemoryAccess } } /** @@ -1507,13 +1199,9 @@ class IndirectWriteSideEffectInstruction extends SideEffectInstruction { * entire buffer is overwritten. */ class BufferWriteSideEffectInstruction extends SideEffectInstruction { - BufferWriteSideEffectInstruction() { - getOpcode() instanceof Opcode::BufferWriteSideEffect - } + BufferWriteSideEffectInstruction() { getOpcode() instanceof Opcode::BufferWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof BufferMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof BufferMemoryAccess } } /** @@ -1526,21 +1214,19 @@ class IndirectMayWriteSideEffectInstruction extends SideEffectInstruction { getOpcode() instanceof Opcode::IndirectMayWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof IndirectMayMemoryAccess } } /** - * An instruction representing the write of an indirect buffer parameter within a function call. + * An instruction representing the write of an indirect buffer parameter within a function call. * Unlike `BufferWriteSideEffectInstruction`, the buffer might not be completely overwritten. */ class BufferMayWriteSideEffectInstruction extends SideEffectInstruction { - BufferMayWriteSideEffectInstruction() { - getOpcode() instanceof Opcode::BufferMayWriteSideEffect - } + BufferMayWriteSideEffectInstruction() { getOpcode() instanceof Opcode::BufferMayWriteSideEffect } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof BufferMayMemoryAccess } } @@ -1549,11 +1235,9 @@ class BufferMayWriteSideEffectInstruction extends SideEffectInstruction { * An instruction representing a GNU or MSVC inline assembly statement. */ class InlineAsmInstruction extends Instruction { - InlineAsmInstruction() { - getOpcode() instanceof Opcode::InlineAsm - } - - override final MemoryAccessKind getResultMemoryAccess() { + InlineAsmInstruction() { getOpcode() instanceof Opcode::InlineAsm } + + final override MemoryAccessKind getResultMemoryAccess() { result instanceof EscapedMayMemoryAccess } } @@ -1562,73 +1246,55 @@ class InlineAsmInstruction extends Instruction { * An instruction that throws an exception. */ class ThrowInstruction extends Instruction { - ThrowInstruction() { - getOpcode() instanceof ThrowOpcode - } + ThrowInstruction() { getOpcode() instanceof ThrowOpcode } } /** * An instruction that throws a new exception. */ class ThrowValueInstruction extends ThrowInstruction { - ThrowValueInstruction() { - getOpcode() instanceof Opcode::ThrowValue - } + ThrowValueInstruction() { getOpcode() instanceof Opcode::ThrowValue } /** * Gets the address operand of the exception thrown by this instruction. */ - final AddressOperand getExceptionAddressOperand() { - result = getAnOperand() - } + final AddressOperand getExceptionAddressOperand() { result = getAnOperand() } /** * Gets the address of the exception thrown by this instruction. */ - final Instruction getExceptionAddress() { - result = getExceptionAddressOperand().getDef() - } + final Instruction getExceptionAddress() { result = getExceptionAddressOperand().getDef() } /** * Gets the operand for the exception thrown by this instruction. */ - final LoadOperand getExceptionOperand() { - result = getAnOperand() - } + final LoadOperand getExceptionOperand() { result = getAnOperand() } /** * Gets the exception thrown by this instruction. */ - final Instruction getException() { - result = getExceptionOperand().getDef() - } + final Instruction getException() { result = getExceptionOperand().getDef() } } /** * An instruction that re-throws the current exception. */ class ReThrowInstruction extends ThrowInstruction { - ReThrowInstruction() { - getOpcode() instanceof Opcode::ReThrow - } + ReThrowInstruction() { getOpcode() instanceof Opcode::ReThrow } } /** * An instruction that exits the current function by propagating an exception. */ class UnwindInstruction extends Instruction { - UnwindInstruction() { - getOpcode() instanceof Opcode::Unwind - } + UnwindInstruction() { getOpcode() instanceof Opcode::Unwind } } /** * An instruction that starts a `catch` handler. */ class CatchInstruction extends Instruction { - CatchInstruction() { - getOpcode() instanceof CatchOpcode - } + CatchInstruction() { getOpcode() instanceof CatchOpcode } } /** @@ -1642,33 +1308,25 @@ class CatchByTypeInstruction extends CatchInstruction { exceptionType = Construction::getInstructionExceptionType(this) } - final override string getImmediateString() { - result = exceptionType.toString() - } + final override string getImmediateString() { result = exceptionType.toString() } /** * Gets the type of exception to be caught. */ - final Language::Type getExceptionType() { - result = exceptionType - } + final Language::Type getExceptionType() { result = exceptionType } } /** * An instruction that catches any exception. */ class CatchAnyInstruction extends CatchInstruction { - CatchAnyInstruction() { - getOpcode() instanceof Opcode::CatchAny - } + CatchAnyInstruction() { getOpcode() instanceof Opcode::CatchAny } } class UnmodeledDefinitionInstruction extends Instruction { - UnmodeledDefinitionInstruction() { - getOpcode() instanceof Opcode::UnmodeledDefinition - } + UnmodeledDefinitionInstruction() { getOpcode() instanceof Opcode::UnmodeledDefinition } - override final MemoryAccessKind getResultMemoryAccess() { + final override MemoryAccessKind getResultMemoryAccess() { result instanceof UnmodeledMemoryAccess } } @@ -1677,23 +1335,15 @@ class UnmodeledDefinitionInstruction extends Instruction { * An instruction that initializes all escaped memory. */ class AliasedDefinitionInstruction extends Instruction { - AliasedDefinitionInstruction() { - getOpcode() instanceof Opcode::AliasedDefinition - } + AliasedDefinitionInstruction() { getOpcode() instanceof Opcode::AliasedDefinition } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof EscapedMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof EscapedMemoryAccess } } class UnmodeledUseInstruction extends Instruction { - UnmodeledUseInstruction() { - getOpcode() instanceof Opcode::UnmodeledUse - } + UnmodeledUseInstruction() { getOpcode() instanceof Opcode::UnmodeledUse } - override string getOperandsString() { - result = "mu*" - } + override string getOperandsString() { result = "mu*" } } /** @@ -1707,20 +1357,14 @@ class UnmodeledUseInstruction extends Instruction { * runtime. */ class PhiInstruction extends Instruction { - PhiInstruction() { - getOpcode() instanceof Opcode::Phi - } + PhiInstruction() { getOpcode() instanceof Opcode::Phi } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof PhiMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof PhiMemoryAccess } /** * Gets all of the instruction's `PhiInputOperand`s, representing the values that flow from each predecessor block. */ - final PhiInputOperand getAnInputOperand() { - result = this.getAnOperand() - } + final PhiInputOperand getAnInputOperand() { result = this.getAnOperand() } /** * Gets an instruction that defines the input to one of the operands of this @@ -1729,9 +1373,7 @@ class PhiInstruction extends Instruction { * results as `getAnInputOperand()` or fewer. */ pragma[noinline] - final Instruction getAnInput() { - result = this.getAnInputOperand().getDef() - } + final Instruction getAnInput() { result = this.getAnInputOperand().getDef() } } /** @@ -1777,43 +1419,31 @@ class PhiInstruction extends Instruction { * https://link.springer.com/content/pdf/10.1007%2F3-540-61053-7_66.pdf. */ class ChiInstruction extends Instruction { - ChiInstruction() { - getOpcode() instanceof Opcode::Chi - } + ChiInstruction() { getOpcode() instanceof Opcode::Chi } - override final MemoryAccessKind getResultMemoryAccess() { - result instanceof ChiTotalMemoryAccess - } + final override MemoryAccessKind getResultMemoryAccess() { result instanceof ChiTotalMemoryAccess } /** * Gets the operand that represents the previous state of all memory that might be aliased by the * memory write. */ - final ChiTotalOperand getTotalOperand() { - result = getAnOperand() - } + final ChiTotalOperand getTotalOperand() { result = getAnOperand() } /** * Gets the operand that represents the previous state of all memory that might be aliased by the * memory write. */ - final Instruction getTotal() { - result = getTotalOperand().getDef() - } + final Instruction getTotal() { result = getTotalOperand().getDef() } /** * Gets the operand that represents the new value written by the memory write. */ - final ChiPartialOperand getPartialOperand() { - result = getAnOperand() - } + final ChiPartialOperand getPartialOperand() { result = getAnOperand() } /** * Gets the operand that represents the new value written by the memory write. */ - final Instruction getPartial() { - result = getPartialOperand().getDef() - } + final Instruction getPartial() { result = getPartialOperand().getDef() } } /** @@ -1822,9 +1452,7 @@ class ChiInstruction extends Instruction { * infeasible. */ class UnreachedInstruction extends Instruction { - UnreachedInstruction() { - getOpcode() instanceof Opcode::Unreached - } + UnreachedInstruction() { getOpcode() instanceof Opcode::Unreached } } /** @@ -1839,9 +1467,7 @@ class BuiltInOperationInstruction extends Instruction { operation = Construction::getInstructionBuiltInOperation(this) } - final Language::BuiltInOperation getBuiltInOperation() { - result = operation - } + final Language::BuiltInOperation getBuiltInOperation() { result = operation } } /** @@ -1849,11 +1475,7 @@ class BuiltInOperationInstruction extends Instruction { * actual operation is specified by the `getBuiltInOperation()` predicate. */ class BuiltInInstruction extends BuiltInOperationInstruction { - BuiltInInstruction() { - getOpcode() instanceof Opcode::BuiltIn - } + BuiltInInstruction() { getOpcode() instanceof Opcode::BuiltIn } - override final string getImmediateString() { - result = getBuiltInOperation().toString() - } + final override string getImmediateString() { result = getBuiltInOperation().toString() } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Operand.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Operand.qll index 29af8267cb7..fda04820848 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Operand.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Operand.qll @@ -12,11 +12,15 @@ private newtype TOperand = defInstr = Construction::getRegisterOperandDefinition(useInstr, tag) and not isInCycle(useInstr) } or - TNonPhiMemoryOperand(Instruction useInstr, MemoryOperandTag tag, Instruction defInstr, Overlap overlap) { + TNonPhiMemoryOperand( + Instruction useInstr, MemoryOperandTag tag, Instruction defInstr, Overlap overlap + ) { defInstr = Construction::getMemoryOperandDefinition(useInstr, tag, overlap) and not isInCycle(useInstr) } or - TPhiOperand(PhiInstruction useInstr, Instruction defInstr, IRBlock predecessorBlock, Overlap overlap) { + TPhiOperand( + PhiInstruction useInstr, Instruction defInstr, IRBlock predecessorBlock, Overlap overlap + ) { defInstr = Construction::getPhiOperandDefinition(useInstr, predecessorBlock, overlap) } @@ -46,24 +50,16 @@ private predicate isInCycle(Instruction instr) { * A source operand of an `Instruction`. The operand represents a value consumed by the instruction. */ class Operand extends TOperand { - string toString() { - result = "Operand" - } + string toString() { result = "Operand" } - final Language::Location getLocation() { - result = getUse().getLocation() - } + final Language::Location getLocation() { result = getUse().getLocation() } - final IRFunction getEnclosingIRFunction() { - result = getUse().getEnclosingIRFunction() - } + final IRFunction getEnclosingIRFunction() { result = getUse().getEnclosingIRFunction() } /** * Gets the `Instruction` that consumes this operand. */ - Instruction getUse() { - none() - } + Instruction getUse() { none() } /** * Gets the `Instruction` whose result is the value of the operand. Unlike @@ -71,9 +67,7 @@ class Operand extends TOperand { * means that the resulting instruction may only _partially_ or _potentially_ * be the value of this operand. */ - Instruction getAnyDef() { - none() - } + Instruction getAnyDef() { none() } /** * Gets the `Instruction` whose result is the value of the operand. Unlike @@ -91,10 +85,7 @@ class Operand extends TOperand { * * Gets the `Instruction` that consumes this operand. */ - deprecated - final Instruction getUseInstruction() { - result = getUse() - } + deprecated final Instruction getUseInstruction() { result = getUse() } /** * DEPRECATED: use `getAnyDef` or `getDef`. The exact replacement for this @@ -103,31 +94,22 @@ class Operand extends TOperand { * * Gets the `Instruction` whose result is the value of the operand. */ - deprecated - final Instruction getDefinitionInstruction() { - result = getAnyDef() - } + deprecated final Instruction getDefinitionInstruction() { result = getAnyDef() } /** * Gets the overlap relationship between the operand's definition and its use. */ - Overlap getDefinitionOverlap() { - none() - } + Overlap getDefinitionOverlap() { none() } /** * Holds if the result of the definition instruction does not exactly overlap this use. */ - final predicate isDefinitionInexact() { - not getDefinitionOverlap() instanceof MustExactlyOverlap - } + final predicate isDefinitionInexact() { not getDefinitionOverlap() instanceof MustExactlyOverlap } /** * Gets a prefix to use when dumping the operand in an operand list. */ - string getDumpLabel() { - result = "" - } + string getDumpLabel() { result = "" } /** * Gets a string describing this operand, suitable for display in IR dumps. This consists of the @@ -146,18 +128,13 @@ class Operand extends TOperand { * the empty string. */ private string getInexactSpecifier() { - if isDefinitionInexact() then - result = "~" - else - result = "" + if isDefinitionInexact() then result = "~" else result = "" } /** * Get the order in which the operand should be sorted in the operand list. */ - int getDumpSortOrder() { - result = -1 - } + int getDumpSortOrder() { result = -1 } /** * Gets the type of the value consumed by this operand. This is usually the same as the @@ -166,9 +143,7 @@ class Operand extends TOperand { * the definition type, such as in the case of a partial read or a read from a pointer that * has been cast to a different type. */ - Language::Type getType() { - result = getAnyDef().getResultType() - } + Language::Type getType() { result = getAnyDef().getResultType() } /** * Holds if the value consumed by this operand is a glvalue. If this @@ -177,17 +152,13 @@ class Operand extends TOperand { * not hold, the value of the operand represents a value whose type is * given by `getResultType()`. */ - predicate isGLValue() { - getAnyDef().isGLValue() - } + predicate isGLValue() { getAnyDef().isGLValue() } /** * Gets the size of the value consumed by this operand, in bytes. If the operand does not have * a known constant size, this predicate does not hold. */ - int getSize() { - result = Language::getTypeSize(getType()) - } + int getSize() { result = Language::getTypeSize(getType()) } } /** @@ -207,9 +178,7 @@ class MemoryOperand extends Operand { /** * Gets the kind of memory access performed by the operand. */ - MemoryAccessKind getMemoryAccess() { - none() - } + MemoryAccessKind getMemoryAccess() { none() } /** * Returns the operand that holds the memory address from which the current operand loads its @@ -227,7 +196,9 @@ class MemoryOperand extends Operand { */ class NonPhiOperand extends Operand { Instruction useInstr; + Instruction defInstr; + OperandTag tag; NonPhiOperand() { @@ -235,25 +206,15 @@ class NonPhiOperand extends Operand { this = TNonPhiMemoryOperand(useInstr, tag, defInstr, _) } - override final Instruction getUse() { - result = useInstr - } + final override Instruction getUse() { result = useInstr } - override final Instruction getAnyDef() { - result = defInstr - } + final override Instruction getAnyDef() { result = defInstr } - override final string getDumpLabel() { - result = tag.getLabel() - } + final override string getDumpLabel() { result = tag.getLabel() } - override final int getDumpSortOrder() { - result = tag.getSortOrder() - } + final override int getDumpSortOrder() { result = tag.getSortOrder() } - final OperandTag getOperandTag() { - result = tag - } + final OperandTag getOperandTag() { result = tag } } /** @@ -262,7 +223,7 @@ class NonPhiOperand extends Operand { class RegisterOperand extends NonPhiOperand, TRegisterOperand { override RegisterOperandTag tag; - override final Overlap getDefinitionOverlap() { + final override Overlap getDefinitionOverlap() { // All register results overlap exactly with their uses. result instanceof MustExactlyOverlap } @@ -270,21 +231,18 @@ class RegisterOperand extends NonPhiOperand, TRegisterOperand { class NonPhiMemoryOperand extends NonPhiOperand, MemoryOperand, TNonPhiMemoryOperand { override MemoryOperandTag tag; + Overlap overlap; - NonPhiMemoryOperand() { - this = TNonPhiMemoryOperand(useInstr, tag, defInstr, overlap) - } + NonPhiMemoryOperand() { this = TNonPhiMemoryOperand(useInstr, tag, defInstr, overlap) } - override final Overlap getDefinitionOverlap() { - result = overlap - } + final override Overlap getDefinitionOverlap() { result = overlap } } class TypedOperand extends NonPhiMemoryOperand { override TypedOperandTag tag; - override final Language::Type getType() { + final override Language::Type getType() { result = Construction::getInstructionOperandType(useInstr, tag) } } @@ -296,9 +254,7 @@ class TypedOperand extends NonPhiMemoryOperand { class AddressOperand extends RegisterOperand { override AddressOperandTag tag; - override string toString() { - result = "Address" - } + override string toString() { result = "Address" } } /** @@ -308,13 +264,9 @@ class AddressOperand extends RegisterOperand { class LoadOperand extends TypedOperand { override LoadOperandTag tag; - override string toString() { - result = "Load" - } + override string toString() { result = "Load" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof IndirectMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof IndirectMemoryAccess } } /** @@ -323,9 +275,7 @@ class LoadOperand extends TypedOperand { class StoreValueOperand extends RegisterOperand { override StoreValueOperandTag tag; - override string toString() { - result = "StoreValue" - } + override string toString() { result = "StoreValue" } } /** @@ -334,9 +284,7 @@ class StoreValueOperand extends RegisterOperand { class UnaryOperand extends RegisterOperand { override UnaryOperandTag tag; - override string toString() { - result = "Unary" - } + override string toString() { result = "Unary" } } /** @@ -345,9 +293,7 @@ class UnaryOperand extends RegisterOperand { class LeftOperand extends RegisterOperand { override LeftOperandTag tag; - override string toString() { - result = "Left" - } + override string toString() { result = "Left" } } /** @@ -356,9 +302,7 @@ class LeftOperand extends RegisterOperand { class RightOperand extends RegisterOperand { override RightOperandTag tag; - override string toString() { - result = "Right" - } + override string toString() { result = "Right" } } /** @@ -367,9 +311,7 @@ class RightOperand extends RegisterOperand { class ConditionOperand extends RegisterOperand { override ConditionOperandTag tag; - override string toString() { - result = "Condition" - } + override string toString() { result = "Condition" } } /** @@ -379,13 +321,9 @@ class ConditionOperand extends RegisterOperand { class UnmodeledUseOperand extends NonPhiMemoryOperand { override UnmodeledUseOperandTag tag; - override string toString() { - result = "UnmodeledUse" - } + override string toString() { result = "UnmodeledUse" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof UnmodeledMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof UnmodeledMemoryAccess } } /** @@ -394,9 +332,7 @@ class UnmodeledUseOperand extends NonPhiMemoryOperand { class CallTargetOperand extends RegisterOperand { override CallTargetOperandTag tag; - override string toString() { - result = "CallTarget" - } + override string toString() { result = "CallTarget" } } /** @@ -415,9 +351,7 @@ class ArgumentOperand extends RegisterOperand { class ThisArgumentOperand extends ArgumentOperand { override ThisArgumentOperandTag tag; - override string toString() { - result = "ThisArgument" - } + override string toString() { result = "ThisArgument" } } /** @@ -425,32 +359,26 @@ class ThisArgumentOperand extends ArgumentOperand { */ class PositionalArgumentOperand extends ArgumentOperand { override PositionalArgumentOperandTag tag; + int argIndex; - PositionalArgumentOperand() { - argIndex = tag.getArgIndex() - } + PositionalArgumentOperand() { argIndex = tag.getArgIndex() } - override string toString() { - result = "Arg(" + argIndex + ")" - } + override string toString() { result = "Arg(" + argIndex + ")" } /** * Gets the zero-based index of the argument. */ - final int getIndex() { - result = argIndex - } + final int getIndex() { result = argIndex } } class SideEffectOperand extends TypedOperand { override SideEffectOperandTag tag; - override final int getSize() { - if getType() instanceof Language::UnknownType then - result = Construction::getInstructionOperandSize(useInstr, tag) - else - result = Language::getTypeSize(getType()) + final override int getSize() { + if getType() instanceof Language::UnknownType + then result = Construction::getInstructionOperandSize(useInstr, tag) + else result = Language::getTypeSize(getType()) } override MemoryAccessKind getMemoryAccess() { @@ -485,48 +413,35 @@ class SideEffectOperand extends TypedOperand { */ class PhiInputOperand extends MemoryOperand, TPhiOperand { PhiInstruction useInstr; + Instruction defInstr; + IRBlock predecessorBlock; + Overlap overlap; - PhiInputOperand() { - this = TPhiOperand(useInstr, defInstr, predecessorBlock, overlap) - } + PhiInputOperand() { this = TPhiOperand(useInstr, defInstr, predecessorBlock, overlap) } - override string toString() { - result = "Phi" - } + override string toString() { result = "Phi" } - override final PhiInstruction getUse() { - result = useInstr - } + final override PhiInstruction getUse() { result = useInstr } - override final Instruction getAnyDef() { - result = defInstr - } + final override Instruction getAnyDef() { result = defInstr } - override final Overlap getDefinitionOverlap() { - result = overlap - } + final override Overlap getDefinitionOverlap() { result = overlap } - override final int getDumpSortOrder() { - result = 11 + getPredecessorBlock().getDisplayIndex() - } + final override int getDumpSortOrder() { result = 11 + getPredecessorBlock().getDisplayIndex() } - override final string getDumpLabel() { + final override string getDumpLabel() { result = "from " + getPredecessorBlock().getDisplayIndex().toString() + ":" } /** * Gets the predecessor block from which this value comes. */ - final IRBlock getPredecessorBlock() { - result = predecessorBlock - } + final IRBlock getPredecessorBlock() { result = predecessorBlock } - override final MemoryAccessKind getMemoryAccess() { - result instanceof PhiMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof PhiMemoryAccess } } /** @@ -535,27 +450,18 @@ class PhiInputOperand extends MemoryOperand, TPhiOperand { class ChiTotalOperand extends NonPhiMemoryOperand { override ChiTotalOperandTag tag; - override string toString() { - result = "ChiTotal" - } + override string toString() { result = "ChiTotal" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof ChiTotalMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof ChiTotalMemoryAccess } } - /** * The partial operand of a Chi node, representing the value being written to part of the memory. */ class ChiPartialOperand extends NonPhiMemoryOperand { override ChiPartialOperandTag tag; - override string toString() { - result = "ChiPartial" - } + override string toString() { result = "ChiPartial" } - override final MemoryAccessKind getMemoryAccess() { - result instanceof ChiPartialMemoryAccess - } + final override MemoryAccessKind getMemoryAccess() { result instanceof ChiPartialMemoryAccess } } diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/PrintIR.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/PrintIR.qll index 4bad5e3fd3e..c24756a2212 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/PrintIR.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/PrintIR.qll @@ -9,23 +9,17 @@ private newtype TPrintIRConfiguration = MkPrintIRConfiguration() * The query can extend this class to control which functions are printed. */ class PrintIRConfiguration extends TPrintIRConfiguration { - string toString() { - result = "PrintIRConfiguration" - } + string toString() { result = "PrintIRConfiguration" } /** * Holds if the IR for `func` should be printed. By default, holds for all * functions. */ - predicate shouldPrintFunction(Language::Function func) { - any() - } + predicate shouldPrintFunction(Language::Function func) { any() } } private predicate shouldPrintFunction(Language::Function func) { - exists(PrintIRConfiguration config | - config.shouldPrintFunction(func) - ) + exists(PrintIRConfiguration config | config.shouldPrintFunction(func)) } /** @@ -38,27 +32,17 @@ private class FilteredIRConfiguration extends IRConfiguration { } private string getAdditionalInstructionProperty(Instruction instr, string key) { - exists(IRPropertyProvider provider | - result = provider.getInstructionProperty(instr, key) - ) + exists(IRPropertyProvider provider | result = provider.getInstructionProperty(instr, key)) } private string getAdditionalBlockProperty(IRBlock block, string key) { - exists(IRPropertyProvider provider | - result = provider.getBlockProperty(block, key) - ) + exists(IRPropertyProvider provider | result = provider.getBlockProperty(block, key)) } private newtype TPrintableIRNode = - TPrintableIRFunction(IRFunction irFunc) { - shouldPrintFunction(irFunc.getFunction()) - } or - TPrintableIRBlock(IRBlock block) { - shouldPrintFunction(block.getEnclosingFunction()) - } or - TPrintableInstruction(Instruction instr) { - shouldPrintFunction(instr.getEnclosingFunction()) - } + TPrintableIRFunction(IRFunction irFunc) { shouldPrintFunction(irFunc.getFunction()) } or + TPrintableIRBlock(IRBlock block) { shouldPrintFunction(block.getEnclosingFunction()) } or + TPrintableInstruction(Instruction instr) { shouldPrintFunction(instr.getEnclosingFunction()) } /** * A node to be emitted in the IR graph. @@ -85,29 +69,28 @@ abstract class PrintableIRNode extends TPrintableIRNode { * Gets the parent of this node. */ abstract PrintableIRNode getParent(); - + /** * Gets the kind of graph represented by this node ("graph" or "tree"). */ - string getGraphKind() { - none() - } + string getGraphKind() { none() } /** * Holds if this node should always be rendered as text, even in a graphical * viewer. */ - predicate forceText() { - none() - } + predicate forceText() { none() } /** * Gets the value of the node property with the specified key. */ string getProperty(string key) { - key = "semmle.label" and result = getLabel() or - key = "semmle.order" and result = getOrder().toString() or - key = "semmle.graphKind" and result = getGraphKind() or + key = "semmle.label" and result = getLabel() + or + key = "semmle.order" and result = getOrder().toString() + or + key = "semmle.graphKind" and result = getGraphKind() + or key = "semmle.forceText" and forceText() and result = "true" } } @@ -118,37 +101,28 @@ abstract class PrintableIRNode extends TPrintableIRNode { class PrintableIRFunction extends PrintableIRNode, TPrintableIRFunction { IRFunction irFunc; - PrintableIRFunction() { - this = TPrintableIRFunction(irFunc) - } + PrintableIRFunction() { this = TPrintableIRFunction(irFunc) } - override string toString() { - result = irFunc.toString() - } + override string toString() { result = irFunc.toString() } - override Language::Location getLocation() { - result = irFunc.getLocation() - } + override Language::Location getLocation() { result = irFunc.getLocation() } - override string getLabel() { - result = Language::getIdentityString(irFunc.getFunction()) - } + override string getLabel() { result = Language::getIdentityString(irFunc.getFunction()) } override int getOrder() { this = rank[result + 1](PrintableIRFunction orderedFunc, Language::Location location | - location = orderedFunc.getIRFunction().getLocation() | - orderedFunc order by location.getFile().getAbsolutePath(), location.getStartLine(), - location.getStartColumn(), orderedFunc.getLabel() - ) + location = orderedFunc.getIRFunction().getLocation() + | + orderedFunc + order by + location.getFile().getAbsolutePath(), location.getStartLine(), location.getStartColumn(), + orderedFunc.getLabel() + ) } - override final PrintableIRNode getParent() { - none() - } + final override PrintableIRNode getParent() { none() } - final IRFunction getIRFunction() { - result = irFunc - } + final IRFunction getIRFunction() { result = irFunc } } /** @@ -157,35 +131,21 @@ class PrintableIRFunction extends PrintableIRNode, TPrintableIRFunction { class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock { IRBlock block; - PrintableIRBlock() { - this = TPrintableIRBlock(block) - } + PrintableIRBlock() { this = TPrintableIRBlock(block) } - override string toString() { - result = getLabel() - } + override string toString() { result = getLabel() } - override Language::Location getLocation() { - result = block.getLocation() - } + override Language::Location getLocation() { result = block.getLocation() } - override string getLabel() { - result = "Block " + block.getDisplayIndex().toString() - } + override string getLabel() { result = "Block " + block.getDisplayIndex().toString() } - override int getOrder() { - result = block.getDisplayIndex() - } + override int getOrder() { result = block.getDisplayIndex() } - override final string getGraphKind() { - result = "tree" - } + final override string getGraphKind() { result = "tree" } - override final predicate forceText() { - any() - } + final override predicate forceText() { any() } - override final PrintableIRFunction getParent() { + final override PrintableIRFunction getParent() { result.getIRFunction() = block.getEnclosingIRFunction() } @@ -194,9 +154,7 @@ class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock { result = getAdditionalBlockProperty(block, key) } - final IRBlock getBlock() { - result = block - } + final IRBlock getBlock() { result = block } } /** @@ -205,45 +163,35 @@ class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock { class PrintableInstruction extends PrintableIRNode, TPrintableInstruction { Instruction instr; - PrintableInstruction() { - this = TPrintableInstruction(instr) - } + PrintableInstruction() { this = TPrintableInstruction(instr) } - override string toString() { - result = instr.toString() - } + override string toString() { result = instr.toString() } - override Language::Location getLocation() { - result = instr.getLocation() - } + override Language::Location getLocation() { result = instr.getLocation() } override string getLabel() { exists(IRBlock block | instr = block.getAnInstruction() and - exists(string resultString, string operationString, string operandsString, - int resultWidth, int operationWidth | + exists( + string resultString, string operationString, string operandsString, int resultWidth, + int operationWidth + | resultString = instr.getResultString() and operationString = instr.getOperationString() and operandsString = instr.getOperandsString() and columnWidths(block, resultWidth, operationWidth) and - result = resultString + getPaddingString(resultWidth - resultString.length()) + - " = " + operationString + getPaddingString(operationWidth - operationString.length()) + - " : " + operandsString + result = resultString + getPaddingString(resultWidth - resultString.length()) + " = " + + operationString + getPaddingString(operationWidth - operationString.length()) + " : " + + operandsString ) ) } - override int getOrder() { - result = instr.getDisplayIndexInBlock() - } + override int getOrder() { result = instr.getDisplayIndexInBlock() } - override final PrintableIRBlock getParent() { - result.getBlock() = instr.getBlock() - } + final override PrintableIRBlock getParent() { result.getBlock() = instr.getBlock() } - final Instruction getInstruction() { - result = instr - } + final Instruction getInstruction() { result = instr } override string getProperty(string key) { result = PrintableIRNode.super.getProperty(key) or @@ -253,19 +201,26 @@ class PrintableInstruction extends PrintableIRNode, TPrintableInstruction { private predicate columnWidths(IRBlock block, int resultWidth, int operationWidth) { resultWidth = max(Instruction instr | instr.getBlock() = block | instr.getResultString().length()) and - operationWidth = max(Instruction instr | instr.getBlock() = block | instr.getOperationString().length()) + operationWidth = max(Instruction instr | + instr.getBlock() = block + | + instr.getOperationString().length() + ) } private int maxColumnWidth() { result = max(Instruction instr, int width | - width = instr.getResultString().length() or - width = instr.getOperationString().length() or - width = instr.getOperandsString().length() | - width) + width = instr.getResultString().length() or + width = instr.getOperationString().length() or + width = instr.getOperandsString().length() + | + width + ) } private string getPaddingString(int n) { - n = 0 and result = "" or + n = 0 and result = "" + or n > 0 and n <= maxColumnWidth() and result = getPaddingString(n - 1) + " " } @@ -275,9 +230,10 @@ query predicate nodes(PrintableIRNode node, string key, string value) { private int getSuccessorIndex(IRBlock pred, IRBlock succ) { succ = rank[result + 1](IRBlock aSucc, EdgeKind kind | - aSucc = pred.getSuccessor(kind) | - aSucc order by kind.toString() - ) + aSucc = pred.getSuccessor(kind) + | + aSucc order by kind.toString() + ) } query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key, string value) { @@ -291,11 +247,10 @@ query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key, if predBlock.getBackEdgeSuccessor(kind) = succBlock then value = kind.toString() + " (back edge)" else value = kind.toString() - ) or - ( - key = "semmle.order" and - value = getSuccessorIndex(predBlock, succBlock).toString() ) + or + key = "semmle.order" and + value = getSuccessorIndex(predBlock, succBlock).toString() ) ) } diff --git a/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerConstant.qll b/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerConstant.qll index d6e74a28414..55546ba380f 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerConstant.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerConstant.qll @@ -3,56 +3,42 @@ class IntValue = int; /** * Returns the value of the maximum representable integer. */ -int maxValue() { - result = 2147483647 -} +int maxValue() { result = 2147483647 } /** * Returns the value of the minimum representable integer. */ -int minValue() { - result = -2147483647 -} +int minValue() { result = -2147483647 } /** * Returns a value representing an unknown integer. */ -IntValue unknown() { - result = -2147483648 -} +IntValue unknown() { result = -2147483648 } /** * Holds if `n` has a known value. */ bindingset[n] -predicate hasValue(IntValue n) { - n != unknown() -} +predicate hasValue(IntValue n) { n != unknown() } /** * Returns a string representation of `n`. If `n` does not have a known value, the result is "??". */ bindingset[n] -string intValueToString(IntValue n) { - if hasValue(n) then result = n.toString() else result = "??" -} +string intValueToString(IntValue n) { if hasValue(n) then result = n.toString() else result = "??" } /** * Holds if the value `f` is within the range of representable integers. */ -pragma[inline] bindingset[f] -private predicate isRepresentable(float f) { - (f >= minValue()) and (f <= maxValue()) -} +pragma[inline] +private predicate isRepresentable(float f) { f >= minValue() and f <= maxValue() } /** * Gets the value of `n`. Holds only if `n` has a known value. */ bindingset[n] -int getValue(IntValue n) { - hasValue(n) and result = n -} +int getValue(IntValue n) { hasValue(n) and result = n } /** * Returns `a + b`. If either input is unknown, or if the addition overflows, @@ -60,10 +46,9 @@ int getValue(IntValue n) { */ bindingset[a, b] IntValue add(IntValue a, IntValue b) { - if hasValue(a) and hasValue(b) and isRepresentable((float)a + (float)b) then - result = a + b - else - result = unknown() + if hasValue(a) and hasValue(b) and isRepresentable(a.(float) + b.(float)) + then result = a + b + else result = unknown() } /** @@ -72,10 +57,9 @@ IntValue add(IntValue a, IntValue b) { */ bindingset[a, b] IntValue sub(IntValue a, IntValue b) { - if hasValue(a) and hasValue(b) and isRepresentable((float)a - (float)b) then - result = a - b - else - result = unknown() + if hasValue(a) and hasValue(b) and isRepresentable(a.(float) - b.(float)) + then result = a - b + else result = unknown() } /** @@ -85,12 +69,12 @@ IntValue sub(IntValue a, IntValue b) { */ bindingset[a, b] IntValue mul(IntValue a, IntValue b) { - if (a = 0) or (b = 0) then - result = 0 - else if hasValue(a) and hasValue(b) and isRepresentable((float)a * (float)b) then - result = a * b + if a = 0 or b = 0 + then result = 0 else - result = unknown() + if hasValue(a) and hasValue(b) and isRepresentable(a.(float) * b.(float)) + then result = a * b + else result = unknown() } /** @@ -102,10 +86,7 @@ IntValue div(IntValue a, IntValue b) { // Normally, integer division has to worry about overflow for INT_MIN/-1. // However, since we use INT_MIN to represent an unknown value anyway, we only // have to worry about division by zero. - if hasValue(a) and hasValue(b) and (b != 0) then - result = a / b - else - result = unknown() + if hasValue(a) and hasValue(b) and b != 0 then result = a / b else result = unknown() } /** @@ -113,14 +94,9 @@ IntValue div(IntValue a, IntValue b) { */ bindingset[a, b] IntValue compareEQ(IntValue a, IntValue b) { - if hasValue(a) and hasValue(b) then ( - if a = b then - result = 1 - else - result = 0 - ) - else - result = unknown() + if hasValue(a) and hasValue(b) + then if a = b then result = 1 else result = 0 + else result = unknown() } /** @@ -128,14 +104,9 @@ IntValue compareEQ(IntValue a, IntValue b) { */ bindingset[a, b] IntValue compareNE(IntValue a, IntValue b) { - if hasValue(a) and hasValue(b) then ( - if a != b then - result = 1 - else - result = 0 - ) - else - result = unknown() + if hasValue(a) and hasValue(b) + then if a != b then result = 1 else result = 0 + else result = unknown() } /** @@ -143,14 +114,9 @@ IntValue compareNE(IntValue a, IntValue b) { */ bindingset[a, b] IntValue compareLT(IntValue a, IntValue b) { - if hasValue(a) and hasValue(b) then ( - if a < b then - result = 1 - else - result = 0 - ) - else - result = unknown() + if hasValue(a) and hasValue(b) + then if a < b then result = 1 else result = 0 + else result = unknown() } /** @@ -158,14 +124,9 @@ IntValue compareLT(IntValue a, IntValue b) { */ bindingset[a, b] IntValue compareGT(IntValue a, IntValue b) { - if hasValue(a) and hasValue(b) then ( - if a > b then - result = 1 - else - result = 0 - ) - else - result = unknown() + if hasValue(a) and hasValue(b) + then if a > b then result = 1 else result = 0 + else result = unknown() } /** @@ -173,14 +134,9 @@ IntValue compareGT(IntValue a, IntValue b) { */ bindingset[a, b] IntValue compareLE(IntValue a, IntValue b) { - if hasValue(a) and hasValue(b) then ( - if a <= b then - result = 1 - else - result = 0 - ) - else - result = unknown() + if hasValue(a) and hasValue(b) + then if a <= b then result = 1 else result = 0 + else result = unknown() } /** @@ -188,14 +144,9 @@ IntValue compareLE(IntValue a, IntValue b) { */ bindingset[a, b] IntValue compareGE(IntValue a, IntValue b) { - if hasValue(a) and hasValue(b) then ( - if a >= b then - result = 1 - else - result = 0 - ) - else - result = unknown() + if hasValue(a) and hasValue(b) + then if a >= b then result = 1 else result = 0 + else result = unknown() } /** @@ -203,53 +154,41 @@ IntValue compareGE(IntValue a, IntValue b) { */ bindingset[a] IntValue neg(IntValue a) { - result = -a // -INT_MIN = INT_MIN, so this preserves unknown + result = -a // -INT_MIN = INT_MIN, so this preserves unknown } /** * Holds if `a` is equal to `b`. Does not hold if either `a` or `b` is unknown. */ bindingset[a, b] -predicate isEQ(IntValue a, IntValue b) { - hasValue(a) and hasValue(b) and a = b -} +predicate isEQ(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a = b } /** * Holds if `a` is not equal to `b`. Does not hold if either `a` or `b` is unknown. */ bindingset[a, b] -predicate isNE(IntValue a, IntValue b) { - hasValue(a) and hasValue(b) and a != b -} +predicate isNE(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a != b } /** * Holds if `a` is less than `b`. Does not hold if either `a` or `b` is unknown. */ bindingset[a, b] -predicate isLT(IntValue a, IntValue b) { - hasValue(a) and hasValue(b) and a < b -} +predicate isLT(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a < b } /** * Holds if `a` is less than or equal to `b`. Does not hold if either `a` or `b` is unknown. */ bindingset[a, b] -predicate isLE(IntValue a, IntValue b) { - hasValue(a) and hasValue(b) and a <= b -} +predicate isLE(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a <= b } /** * Holds if `a` is greater than `b`. Does not hold if either `a` or `b` is unknown. */ bindingset[a, b] -predicate isGT(IntValue a, IntValue b) { - hasValue(a) and hasValue(b) and a > b -} +predicate isGT(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a > b } /** * Holds if `a` is greater than or equal to `b`. Does not hold if either `a` or `b` is unknown. */ bindingset[a, b] -predicate isGE(IntValue a, IntValue b) { - hasValue(a) and hasValue(b) and a >= b -} +predicate isGE(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a >= b } diff --git a/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerInterval.qll b/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerInterval.qll index 986361ada58..bc09f9c2243 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerInterval.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerInterval.qll @@ -13,14 +13,15 @@ private import IntegerConstant */ bindingset[defStart, defEnd, useStart, useEnd] Overlap getOverlap(IntValue defStart, IntValue defEnd, IntValue useStart, IntValue useEnd) { - if isEQ(defStart, useStart) and isEQ(defEnd, useEnd) then - result instanceof MustExactlyOverlap - else if isLE(defStart, useStart) and isGE(defEnd, useEnd) then - result instanceof MustTotallyOverlap - else if isLE(defEnd, useStart) or isGE(defStart, useEnd) then - none() + if isEQ(defStart, useStart) and isEQ(defEnd, useEnd) + then result instanceof MustExactlyOverlap else - result instanceof MayPartiallyOverlap + if isLE(defStart, useStart) and isGE(defEnd, useEnd) + then result instanceof MustTotallyOverlap + else + if isLE(defEnd, useStart) or isGE(defStart, useEnd) + then none() + else result instanceof MayPartiallyOverlap } /** diff --git a/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerPartial.qll b/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerPartial.qll index 45761aa7992..0e24f283b17 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerPartial.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/internal/IntegerPartial.qll @@ -6,32 +6,26 @@ /** * Gets the value of the maximum representable integer. */ -int maxValue() { - result = 2147483647 -} +int maxValue() { result = 2147483647 } /** * Gets the value of the minimum representable integer. */ -int minValue() { - result = -2147483648 -} +int minValue() { result = -2147483648 } /** * Holds if the value `f` is within the range of representable integers. */ -pragma[inline] bindingset[f] -private predicate isRepresentable(float f) { - (f >= minValue()) and (f <= maxValue()) -} +pragma[inline] +private predicate isRepresentable(float f) { f >= minValue() and f <= maxValue() } /** * Returns `a + b`. If the addition overflows, there is no result. */ bindingset[a, b] int add(int a, int b) { - isRepresentable((float)a + (float)b) and + isRepresentable(a.(float) + b.(float)) and result = a + b } @@ -40,7 +34,7 @@ int add(int a, int b) { */ bindingset[a, b] int sub(int a, int b) { - isRepresentable((float)a - (float)b) and + isRepresentable(a.(float) - b.(float)) and result = a - b } @@ -57,7 +51,7 @@ int mul(int a, int b) { b = 0 and result = 0 or - isRepresentable((float)a * (float)b) and + isRepresentable(a.(float) * b.(float)) and result = a * b } @@ -66,63 +60,34 @@ int mul(int a, int b) { */ bindingset[a, b] int div(int a, int b) { - b != 0 and (a != minValue() or b != -1) and + b != 0 and + (a != minValue() or b != -1) and result = a / b } /** Returns `a == b`. */ bindingset[a, b] -int compareEQ(int a, int b) { - if a = b then - result = 1 - else - result = 0 -} +int compareEQ(int a, int b) { if a = b then result = 1 else result = 0 } /** Returns `a != b`. */ bindingset[a, b] -int compareNE(int a, int b) { - if a != b then - result = 1 - else - result = 0 -} +int compareNE(int a, int b) { if a != b then result = 1 else result = 0 } /** Returns `a < b`. */ bindingset[a, b] -int compareLT(int a, int b) { - if a < b then - result = 1 - else - result = 0 -} +int compareLT(int a, int b) { if a < b then result = 1 else result = 0 } /** Returns `a > b`. */ bindingset[a, b] -int compareGT(int a, int b) { - if a > b then - result = 1 - else - result = 0 -} +int compareGT(int a, int b) { if a > b then result = 1 else result = 0 } /** Returns `a <= b`. */ bindingset[a, b] -int compareLE(int a, int b) { - if a <= b then - result = 1 - else - result = 0 -} +int compareLE(int a, int b) { if a <= b then result = 1 else result = 0 } /** Returns `a >= b`. */ bindingset[a, b] -int compareGE(int a, int b) { - if a >= b then - result = 1 - else - result = 0 -} +int compareGE(int a, int b) { if a >= b then result = 1 else result = 0 } /** * Returns `-a`. If the negation would overflow, there is no result. diff --git a/cpp/ql/src/semmle/code/cpp/ir/internal/Overlap.qll b/cpp/ql/src/semmle/code/cpp/ir/internal/Overlap.qll index 1680129bf22..8ce0549b2b4 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/internal/Overlap.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/internal/Overlap.qll @@ -8,19 +8,13 @@ abstract class Overlap extends TOverlap { } class MayPartiallyOverlap extends Overlap, TMayPartiallyOverlap { - override final string toString() { - result = "MayPartiallyOverlap" - } + final override string toString() { result = "MayPartiallyOverlap" } } class MustTotallyOverlap extends Overlap, TMustTotallyOverlap { - override final string toString() { - result = "MustTotallyOverlap" - } + final override string toString() { result = "MustTotallyOverlap" } } class MustExactlyOverlap extends Overlap, TMustExactlyOverlap { - override final string toString() { - result = "MustExactlyOverlap" - } + final override string toString() { result = "MustExactlyOverlap" } }