|
| 1 | +{- |
| 2 | +Module : UCCrux.LLVM.Bug.UndefinedBehaviorTag |
| 3 | +Description : Representation of undefined behavior |
| 4 | +Copyright : (c) Galois, Inc 2021 |
| 5 | +License : BSD3 |
| 6 | +Maintainer : Langston Barrett <[email protected]> |
| 7 | +Stability : provisional |
| 8 | +-} |
| 9 | + |
| 10 | +{-# LANGUAGE GADTs #-} |
| 11 | +{-# LANGUAGE FlexibleContexts #-} |
| 12 | +{-# LANGUAGE LambdaCase #-} |
| 13 | + |
| 14 | +module UCCrux.LLVM.Bug.UndefinedBehaviorTag |
| 15 | + ( UndefinedBehaviorTag, |
| 16 | + getUndefinedBehaviorTag, |
| 17 | + makeUndefinedBehaviorTag, |
| 18 | + ) |
| 19 | +where |
| 20 | + |
| 21 | +{- ORMOLU_DISABLE -} |
| 22 | +import Data.Type.Equality (testEquality) |
| 23 | + |
| 24 | +import Lang.Crucible.Simulator.RegValue (RegValue'(unRV)) |
| 25 | +import Lang.Crucible.Types (BVType, TypeRepr, baseToType) |
| 26 | + |
| 27 | +import Data.Parameterized.Classes (compareF) |
| 28 | +import Data.Parameterized.ClassesC (testEqualityC, compareC) |
| 29 | + |
| 30 | +import What4.Interface (IsExpr, SymExpr, exprType) |
| 31 | + |
| 32 | +import Lang.Crucible.LLVM.Errors.Poison (Poison(..)) |
| 33 | +import Lang.Crucible.LLVM.Errors.UndefinedBehavior (UndefinedBehavior(..)) |
| 34 | +import Lang.Crucible.LLVM.MemModel.Pointer (LLVMPointerType, llvmPointerType) |
| 35 | +{- ORMOLU_ENABLE -} |
| 36 | + |
| 37 | +-- | A simplification of 'UndefinedBehavior' that keeps less information around. |
| 38 | +-- Used for deduplicating reports about possible bugs/errors in programs and |
| 39 | +-- explaining the provenance of inferred function preconditions. |
| 40 | +newtype UndefinedBehaviorTag = |
| 41 | + UndefinedBehaviorTag { getUndefinedBehaviorTag :: UndefinedBehavior TypeRepr } |
| 42 | + |
| 43 | +makeUndefinedBehaviorTag :: |
| 44 | + IsExpr (SymExpr sym) => |
| 45 | + UndefinedBehavior (RegValue' sym) -> |
| 46 | + UndefinedBehaviorTag |
| 47 | +makeUndefinedBehaviorTag = |
| 48 | + UndefinedBehaviorTag . |
| 49 | + \case |
| 50 | + FreeBadOffset ptr -> |
| 51 | + FreeBadOffset (pointer ptr) |
| 52 | + FreeUnallocated ptr -> |
| 53 | + FreeUnallocated (pointer ptr) |
| 54 | + DoubleFree ptr -> |
| 55 | + DoubleFree (pointer ptr) |
| 56 | + MemsetInvalidRegion ptr val len -> |
| 57 | + MemsetInvalidRegion (pointer ptr) (bv val) (bv len) |
| 58 | + ReadBadAlignment ptr a -> |
| 59 | + ReadBadAlignment (pointer ptr) a |
| 60 | + WriteBadAlignment ptr a -> |
| 61 | + WriteBadAlignment (pointer ptr) a |
| 62 | + PtrAddOffsetOutOfBounds ptr off -> |
| 63 | + PtrAddOffsetOutOfBounds (pointer ptr) (bv off) |
| 64 | + CompareInvalidPointer op p1 p2 -> |
| 65 | + CompareInvalidPointer op (pointer p1) (pointer p2) |
| 66 | + CompareDifferentAllocs p1 p2 -> |
| 67 | + CompareDifferentAllocs (pointer p1) (pointer p2) |
| 68 | + PtrSubDifferentAllocs p1 p2 -> |
| 69 | + PtrSubDifferentAllocs (pointer p1) (pointer p2) |
| 70 | + PointerFloatCast ptr tp -> |
| 71 | + PointerFloatCast (pointer ptr) tp |
| 72 | + PointerIntCast ptr tp -> |
| 73 | + PointerIntCast (pointer ptr) tp |
| 74 | + PointerUnsupportedOp ptr msg -> |
| 75 | + PointerUnsupportedOp (pointer ptr) msg |
| 76 | + ComparePointerToBV ptr val -> |
| 77 | + ComparePointerToBV (pointer ptr) (bv val) |
| 78 | + UDivByZero v1 v2 -> |
| 79 | + UDivByZero (bv v1) (bv v2) |
| 80 | + SDivByZero v1 v2 -> |
| 81 | + SDivByZero (bv v1) (bv v2) |
| 82 | + URemByZero v1 v2 -> |
| 83 | + URemByZero (bv v1) (bv v2) |
| 84 | + SRemByZero v1 v2 -> |
| 85 | + SRemByZero (bv v1) (bv v2) |
| 86 | + SDivOverflow v1 v2 -> |
| 87 | + SDivOverflow (bv v1) (bv v2) |
| 88 | + SRemOverflow v1 v2 -> |
| 89 | + SRemOverflow (bv v1) (bv v2) |
| 90 | + AbsIntMin v -> |
| 91 | + AbsIntMin (bv v) |
| 92 | + PoisonValueCreated p -> |
| 93 | + PoisonValueCreated (poison p) |
| 94 | + where |
| 95 | + pointer :: |
| 96 | + IsExpr (SymExpr sym) => |
| 97 | + RegValue' sym (LLVMPointerType w) -> |
| 98 | + TypeRepr (LLVMPointerType w) |
| 99 | + pointer = llvmPointerType . unRV |
| 100 | + |
| 101 | + bv :: |
| 102 | + IsExpr (SymExpr sym) => |
| 103 | + RegValue' sym (BVType w) -> |
| 104 | + TypeRepr (BVType w) |
| 105 | + bv = baseToType . exprType . unRV |
| 106 | + |
| 107 | + poison :: |
| 108 | + IsExpr (SymExpr sym) => |
| 109 | + Poison (RegValue' sym) -> |
| 110 | + Poison TypeRepr |
| 111 | + poison = |
| 112 | + \case |
| 113 | + AddNoUnsignedWrap v1 v2 -> |
| 114 | + AddNoUnsignedWrap (bv v1) (bv v2) |
| 115 | + AddNoSignedWrap v1 v2 -> |
| 116 | + AddNoSignedWrap (bv v1) (bv v2) |
| 117 | + SubNoUnsignedWrap v1 v2 -> |
| 118 | + SubNoUnsignedWrap (bv v1) (bv v2) |
| 119 | + SubNoSignedWrap v1 v2 -> |
| 120 | + SubNoSignedWrap (bv v1) (bv v2) |
| 121 | + MulNoUnsignedWrap v1 v2 -> |
| 122 | + MulNoUnsignedWrap(bv v1) (bv v2) |
| 123 | + MulNoSignedWrap v1 v2 -> |
| 124 | + MulNoSignedWrap (bv v1) (bv v2) |
| 125 | + UDivExact v1 v2 -> |
| 126 | + UDivExact (bv v1) (bv v2) |
| 127 | + SDivExact v1 v2 -> |
| 128 | + SDivExact (bv v1) (bv v2) |
| 129 | + ShlOp2Big v1 v2 -> |
| 130 | + ShlOp2Big (bv v1) (bv v2) |
| 131 | + ShlNoUnsignedWrap v1 v2 -> |
| 132 | + ShlNoUnsignedWrap (bv v1) (bv v2) |
| 133 | + ShlNoSignedWrap v1 v2 -> |
| 134 | + ShlNoSignedWrap (bv v1) (bv v2) |
| 135 | + LshrExact v1 v2 -> |
| 136 | + LshrExact (bv v1) (bv v2) |
| 137 | + LshrOp2Big v1 v2 -> |
| 138 | + LshrOp2Big (bv v1) (bv v2) |
| 139 | + AshrExact v1 v2 -> |
| 140 | + AshrExact (bv v1) (bv v2) |
| 141 | + AshrOp2Big v1 v2 -> |
| 142 | + AshrOp2Big (bv v1) (bv v2) |
| 143 | + ExtractElementIndex v -> |
| 144 | + ExtractElementIndex (bv v) |
| 145 | + InsertElementIndex v -> |
| 146 | + InsertElementIndex (bv v) |
| 147 | + GEPOutOfBounds p v -> |
| 148 | + GEPOutOfBounds (pointer p) (bv v) |
| 149 | + LLVMAbsIntMin v -> |
| 150 | + LLVMAbsIntMin (bv v) |
| 151 | + |
| 152 | +-- | This instance is a coarsening of that for 'UndefinedBehavior'. In |
| 153 | +-- particular, there may be instances of 'UndefinedBehavior' that do not compare |
| 154 | +-- equal, but their projections under 'makeUndefinedBehaviorTag' do compare |
| 155 | +-- equal. |
| 156 | +instance Eq UndefinedBehaviorTag where |
| 157 | + UndefinedBehaviorTag t1 == UndefinedBehaviorTag t2 = |
| 158 | + testEqualityC testEquality t1 t2 |
| 159 | + |
| 160 | +-- | See comment on 'Eq'. |
| 161 | +-- |
| 162 | +-- Under the hood, this uses 'unsafeCoerce'. |
| 163 | +instance Ord UndefinedBehaviorTag where |
| 164 | + compare (UndefinedBehaviorTag t1) (UndefinedBehaviorTag t2) = |
| 165 | + compareC compareF t1 t2 |
0 commit comments