https://github.com/alexey-bataev commented:

Sema assertion crash / null deref on mixed shaped + subscript/section to/from
```
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -25114,9 +25114,6 @@ static bool checkMapConflicts(
           } else if (const auto *OASE = dyn_cast<ArraySectionExpr>(
                          SI->getAssociatedExpression())) {
             const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
             Type = ArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
-          } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>(
-                         SI->getAssociatedExpression())) {
-            Type = OASE->getBase()->getType()->getPointeeType();
           }
           if (Type.isNull() || Type->isAnyPointerType() ||
               checkArrayExpressionDoesNotReferToWholeSize(

```
Unsound VLA contiguity check silently miscompiles mutated VLA bounds

```
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -24535,15 +24535,7 @@ static bool checkArrayExpressionRefersToWholeSize(Sema 
&SemaRef,
   if (const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
     return EvaluatesTo(Length, CATy->getSExtSize());
 
-  // For a variable length array the length has to be the size expression of
-  // the dimension, e.g. c[0:n] for double c[n][m].
-  const auto *VATy = dyn_cast<VariableArrayType>(BaseQTy.getTypePtr());
-  if (!VATy || !VATy->getSizeExpr() || Length->HasSideEffects(Ctx))
-    return false;
-  llvm::FoldingSetNodeID LengthID, SizeID;
-  Length->IgnoreParenImpCasts()->Profile(LengthID, Ctx, /*Canonical=*/true);
-  VATy->getSizeExpr()->IgnoreParenImpCasts()->Profile(SizeID, Ctx,
-                                                      /*Canonical=*/true);
-  return LengthID == SizeID;
+  return false;
 }

```
Wrong ArgsBase and spurious descriptor dimensions for non-contiguous shaped 
sections on non-simple pointer bases
```
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -8456,6 +8456,16 @@ class MappableExprsHandler {
           dyn_cast<OMPArrayShapingExpr>(I->getAssociatedExpression());
       const auto *UO = dyn_cast<UnaryOperator>(I->getAssociatedExpression());
       const auto *BO = dyn_cast<BinaryOperator>(I->getAssociatedExpression());
+      if (OAShE && Next != CE && IsNonContiguous) {
+        BP = Address(CGF.EmitScalarExpr(OAShE->getBase()),
+                     CGF.ConvertTypeForMem(
+                         OAShE->getBase()->getType()->getPointeeType()),
+                     CGF.getContext().getTypeAlignInChars(
+                         OAShE->getBase()->getType()));
+        EncounteredME = nullptr;
+        ShouldBeMemberOf = false;
+        FirstPointerInComplexData = false;
+        continue;
+      }
       // An array shaping expression maps the shaped storage only as the whole
       // list item; as the base of array sections it just reinterprets the
       // pointer.
@@ -8757,17 +8767,22 @@ class MappableExprsHandler {
     MapValuesArrayTy DimSizes{llvm::ConstantInt::get(CGF.CGM.Int64Ty, 1)};
     uint64_t ElementTypeSize;
 
-    // Only array sections and subscripts add a dimension.
+    // Components past an array shaping expression belong to the shaped pointer
+    // base, not to dimensions of the shaped array.
+    auto ArrayDimsEnd = llvm::find_if(
+        Components,
+        [](const OMPClauseMappableExprCommon::MappableComponent &Component) {
+          return isa<OMPArrayShapingExpr>(Component.getAssociatedExpression());
+        });
+    auto ArrayDimComponents =
+        llvm::make_range(Components.begin(), ArrayDimsEnd);
     unsigned NumArrayDims = llvm::count_if(
-        Components,
+        ArrayDimComponents,
         [](const OMPClauseMappableExprCommon::MappableComponent &Component) {
           return isa<ArraySectionExpr, ArraySubscriptExpr>(
               Component.getAssociatedExpression());
         });
 
     // Collect Size information for each dimension and get the element size as
     // the first Stride. For example, for `int arr[10][10]`, the DimSizes
     // should be [10, 10] and the first stride is 4 btyes.
-    for (const OMPClauseMappableExprCommon::MappableComponent &Component :
-         Components) {
+    for (const OMPClauseMappableExprCommon::MappableComponent &Component :
+         ArrayDimComponents) {
       const Expr *AssocExpr = Component.getAssociatedExpression();
       const auto *OASE = dyn_cast<ArraySectionExpr>(AssocExpr);
@@ -8848,8 +8863,8 @@ class MappableExprsHandler {
     // Also, the size of offset, count, and stride are not the same as
     // pointers, base_pointers, sizes, or dims. Instead, the size of offset,
     // count, and stride are the same as the number of non-contiguous
     // declaration in target update to/from clause.
-    for (const OMPClauseMappableExprCommon::MappableComponent &Component :
-         Components) {
+    for (const OMPClauseMappableExprCommon::MappableComponent &Component :
+         ArrayDimComponents) {

```
ASTContext bit-size overflow crash / 32-bit truncation to 0 on huge constant 
dimensions, plus illegal array-of-function ShapedTy
```
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -28604,7 +28604,8 @@ ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
                                        RParenLoc, Dims, Brackets);
   if (!BaseTy->isPointerType() ||
       (!Base->isTypeDependent() &&
-       BaseTy->getPointeeType()->isIncompleteType()))
+       (BaseTy->getPointeeType()->isIncompleteType() ||
+        BaseTy->getPointeeType()->isFunctionType())))
     return ExprError(Diag(Base->getExprLoc(),
                           diag::err_omp_non_pointer_type_array_shaping_base)
                      << Base->getSourceRange());
@@ -28656,7 +28657,8 @@ ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
   // OpenMP 5.0 [2.1.4, Array Shaping]: the result has n-dimensional array type
   // s1 x ... x sn of T; build it like the type of a declared array.
   QualType ShapedTy = BaseTy->getPointeeType();
-  for (Expr *Dim : llvm::reverse(NewDims)) {
+  for (size_t I = NewDims.size(); I > 0; --I) {
+    Expr *Dim = NewDims[I - 1];
     if (Dim->isTypeDependent() || Dim->isValueDependent()) {
       ShapedTy = Context.getDependentSizedArrayType(
           ShapedTy, Dim, ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
@@ -28666,11 +28668,14 @@ ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
     // An array of variable length arrays is itself a variable length array.
     if (ShapedTy->isDependentType() || ShapedTy->isConstantSizeType())
       Size = Dim->getIntegerConstantExpr(Context);
-    if (Size)
+    if (Size) {
+      if (SemaRef.checkArrayTooLarge(ShapedTy, *Size, Dim->getExprLoc(),
+                                     Brackets[I - 1]))
+        return ExprError();
       ShapedTy = Context.getConstantArrayType(ShapedTy, *Size, Dim,
                                               ArraySizeModifier::Normal,
                                               /*IndexTypeQuals=*/0);
-    else
+    } else
       ShapedTy = Context.getVariableArrayType(
           ShapedTy, Dim, ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
   }

```
APInt::getSExtValue() assert on >64-bit constants and IncompleteArrayType 
mishandling in checkArrayExpressionRefersToWholeSize
```
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -24505,15 +24505,16 @@ static bool 
checkArrayExpressionRefersToWholeSize(Sema &SemaRef,
                                                   const ArraySectionExpr *OASE,
                                                   QualType BaseQTy) {
   ASTContext &Ctx = SemaRef.getASTContext();
-  auto EvaluatesTo = [&Ctx](const Expr *E, int64_t Value) {
+  auto EvaluatesTo = [&Ctx](const Expr *E, const llvm::APSInt &Value) {
     Expr::EvalResult Result;
     return !E->isValueDependent() && E->EvaluateAsInt(Result, Ctx) &&
-           Result.Val.getInt().getSExtValue() == Value;
+           llvm::APSInt::isSameValue(Result.Val.getInt(), Value);
   };
 
   if (OASE->getColonLocFirst().isInvalid()) {
     const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
-    return CATy && CATy->getSExtSize() == 1;
+    return CATy && CATy->getSize().isOne();
   }
 
   const Expr *Stride = OASE->getStride();
-  if (Stride && !EvaluatesTo(Stride, 1))
+  if (Stride && !EvaluatesTo(Stride, llvm::APSInt::get(1)))
     return false;
   const Expr *LowerBound = OASE->getLowerBound();
-  if (LowerBound && !EvaluatesTo(LowerBound, 0))
+  if (LowerBound && !EvaluatesTo(LowerBound, llvm::APSInt::get(0)))
     return false;
 
-  if (BaseQTy->isPointerType())
+  if (!BaseQTy->isConstantArrayType() && !BaseQTy->isVariableArrayType())
     return false;
 
   // Without a length the section covers the rest of the dimension.
   const Expr *Length = OASE->getLength();
   if (!Length)
     return true;
 
   if (const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
-    return EvaluatesTo(Length, CATy->getSExtSize());
+    return EvaluatesTo(Length,
+                       llvm::APSInt(CATy->getSize(), /*isUnsigned=*/true));

```


https://github.com/llvm/llvm-project/pull/244591
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to