| 1 | //===--- InterpHelpers.h - Interpreter Helper Functions --------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_CLANG_AST_INTERP_INTERPHELPERS_H |
| 10 | #define LLVM_CLANG_AST_INTERP_INTERPHELPERS_H |
| 11 | |
| 12 | #include "DynamicAllocator.h" |
| 13 | #include "InterpState.h" |
| 14 | #include "Pointer.h" |
| 15 | |
| 16 | namespace clang { |
| 17 | class CallExpr; |
| 18 | class OffsetOfExpr; |
| 19 | |
| 20 | namespace interp { |
| 21 | class Block; |
| 22 | struct Descriptor; |
| 23 | |
| 24 | /// Interpreter entry point. |
| 25 | bool Interpret(InterpState &S); |
| 26 | |
| 27 | /// Interpret a builtin function. |
| 28 | bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, |
| 29 | uint32_t BuiltinID); |
| 30 | |
| 31 | /// Interpret an offsetof operation. |
| 32 | bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E, |
| 33 | ArrayRef<int64_t> ArrayIndices, int64_t &Result); |
| 34 | |
| 35 | /// Checks if the array is offsetable. |
| 36 | bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr); |
| 37 | |
| 38 | /// Checks if a pointer is live and accessible. |
| 39 | bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, |
| 40 | AccessKinds AK); |
| 41 | |
| 42 | /// Checks if a pointer is a dummy pointer. |
| 43 | bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK); |
| 44 | |
| 45 | /// Checks if a pointer is in range. |
| 46 | bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, |
| 47 | AccessKinds AK); |
| 48 | |
| 49 | /// Checks if a field from which a pointer is going to be derived is valid. |
| 50 | bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, |
| 51 | CheckSubobjectKind CSK); |
| 52 | |
| 53 | /// Checks if a pointer points to a mutable field. |
| 54 | bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr); |
| 55 | |
| 56 | /// Checks if a value can be loaded from a block. |
| 57 | bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, |
| 58 | AccessKinds AK = AK_Read); |
| 59 | |
| 60 | /// Diagnose mismatched new[]/delete or new/delete[] pairs. |
| 61 | bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC, |
| 62 | DynamicAllocator::Form AllocForm, |
| 63 | DynamicAllocator::Form DeleteForm, const Descriptor *D, |
| 64 | const Expr *NewExpr); |
| 65 | |
| 66 | /// Copy the contents of Src into Dest. |
| 67 | bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest); |
| 68 | |
| 69 | UnsignedOrNone evaluateBuiltinObjectSize(const ASTContext &ASTCtx, |
| 70 | unsigned Kind, Pointer &Ptr); |
| 71 | |
| 72 | template <typename T> |
| 73 | static bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue) { |
| 74 | const Expr *E = S.Current->getExpr(PC: OpPC); |
| 75 | S.CCEDiag(E, DiagId: diag::note_constexpr_overflow) << SrcValue << E->getType(); |
| 76 | return S.noteUndefinedBehavior(); |
| 77 | } |
| 78 | |
| 79 | inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) { |
| 80 | uint64_t Limit = S.getLangOpts().ConstexprStepLimit; |
| 81 | if (Limit != 0 && NumElems > Limit) { |
| 82 | S.FFDiag(SI: S.Current->getSource(PC: OpPC), |
| 83 | DiagId: diag::note_constexpr_new_exceeds_limits) |
| 84 | << NumElems << Limit; |
| 85 | return false; |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | static inline llvm::RoundingMode getRoundingMode(FPOptions FPO) { |
| 91 | auto RM = FPO.getRoundingMode(); |
| 92 | if (RM == llvm::RoundingMode::Dynamic) |
| 93 | return llvm::RoundingMode::NearestTiesToEven; |
| 94 | return RM; |
| 95 | } |
| 96 | |
| 97 | inline bool Invalid(InterpState &S, CodePtr OpPC) { |
| 98 | const SourceLocation &Loc = S.Current->getLocation(PC: OpPC); |
| 99 | S.FFDiag(Loc, DiagId: diag::note_invalid_subexpr_in_const_expr) |
| 100 | << S.Current->getRange(PC: OpPC); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | template <typename SizeT> |
| 105 | bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements, |
| 106 | unsigned ElemSize, bool IsNoThrow) { |
| 107 | // FIXME: Both the SizeT::from() as well as the |
| 108 | // NumElements.toAPSInt() in this function are rather expensive. |
| 109 | |
| 110 | // Can't be too many elements if the bitwidth of NumElements is lower than |
| 111 | // that of Descriptor::MaxArrayElemBytes. |
| 112 | if ((NumElements->bitWidth() - NumElements->isSigned()) < |
| 113 | (sizeof(Descriptor::MaxArrayElemBytes) * 8)) |
| 114 | return true; |
| 115 | |
| 116 | // FIXME: GH63562 |
| 117 | // APValue stores array extents as unsigned, |
| 118 | // so anything that is greater that unsigned would overflow when |
| 119 | // constructing the array, we catch this here. |
| 120 | SizeT MaxElements = SizeT::from(Descriptor::MaxArrayElemBytes / ElemSize); |
| 121 | assert(MaxElements.isPositive()); |
| 122 | if (NumElements->toAPSInt().getActiveBits() > |
| 123 | ConstantArrayType::getMaxSizeBits(Context: S.getASTContext()) || |
| 124 | *NumElements > MaxElements) { |
| 125 | if (!IsNoThrow) { |
| 126 | const SourceInfo &Loc = S.Current->getSource(PC: OpPC); |
| 127 | |
| 128 | if (NumElements->isSigned() && NumElements->isNegative()) { |
| 129 | S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_new_negative) |
| 130 | << NumElements->toDiagnosticString(S.getASTContext()); |
| 131 | } else { |
| 132 | S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_new_too_large) |
| 133 | << NumElements->toDiagnosticString(S.getASTContext()); |
| 134 | } |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | } // namespace interp |
| 142 | } // namespace clang |
| 143 | |
| 144 | #endif // LLVM_CLANG_AST_INTERP_INTERPHELPERS_H |
| 145 | |