[clang] [clang] add array out-of-bounds access constraints using llvm.assume (PR #159046)
Eli Friedman via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 6 14:17:00 PDT 2025
================
@@ -4559,6 +4559,152 @@ void CodeGenFunction::EmitCountedByBoundsChecking(
}
}
+/// Emit array bounds constraints using llvm.assume for optimization hints.
+///
+/// C Standard (ISO/IEC 9899:2011 - C11)
+/// Section J.2 (Undefined behavior): An array subscript is out of range, even
+/// if an object is apparently accessible with the given subscript (as in the
+/// lvalue expression a[1][7] given the declaration int a[4][5]) (6.5.6).
+///
+/// Section 6.5.6 (Additive operators): If both the pointer operand and the
+/// result point to elements of the same array object, or one past the last
+/// element of the array object, the evaluation shall not produce an overflow;
+/// otherwise, the behavior is undefined.
+///
+/// C++ Standard (ISO/IEC 14882 - 2017)
+/// Section 8.7 (Additive operators):
+/// 4 When an expression that has integral type is added to or subtracted from a
+/// pointer, the result has the type of the pointer operand. If the expression
+/// P points to element x[i] of an array object x with n elements,^86 the
+/// expressions P + J and J + P (where J has the value j) point to the
+/// (possibly-hypothetical) element x[i + j] if 0 ≤ i + j ≤ n; otherwise, the
+/// behavior is undefined. Likewise, the expression P - J points to the
+/// (possibly-hypothetical) element x[i − j] if 0 ≤ i − j ≤ n; otherwise, the
+/// behavior is undefined.
+/// ^86 A pointer past the last element of an array x of n elements is
+/// considered to be equivalent to a pointer to a hypothetical element x[n]
+/// for this purpose; see 6.9.2.
+///
+/// This function emits llvm.assume statements to inform the optimizer that
+/// array subscripts are within bounds, enabling better optimization without
+/// duplicating side effects from the subscript expression. The IndexVal
+/// parameter should be the already-emitted index value to avoid re-evaluation.
+///
+/// Code that intentionally accesses out-of-bounds (UB) may break with
+/// optimizations. Only applies to constant-size arrays (not pointers, VLAs, or
+/// flexible arrays.) Disabled when -fsanitize=array-bounds is active.
+///
+void CodeGenFunction::EmitArrayBoundsConstraints(const ArraySubscriptExpr *E,
+ llvm::Value *IndexVal) {
+ // Disable with -fno-assume-array-bounds.
+ if (!CGM.getCodeGenOpts().AssumeArrayBounds)
+ return;
+
+ // Disable at -O0.
+ if (CGM.getCodeGenOpts().OptimizationLevel == 0)
+ return;
+
+ // Disable with array-bounds sanitizer.
+ if (SanOpts.has(SanitizerKind::ArrayBounds))
+ return;
+
+ // Use the provided IndexVal to avoid duplicating side effects.
+ // The caller has already emitted the index expression once.
+ if (!IndexVal)
+ return;
+
+ const Expr *Base = E->getBase();
+ const Expr *Idx = E->getIdx();
+ QualType BaseType = Base->getType();
+
+ if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Base)) {
+ if (ICE->getCastKind() == CK_ArrayToPointerDecay) {
+ BaseType = ICE->getSubExpr()->getType();
+ }
+ }
+
+ // Handle both constant arrays and VLAs (variable-length arrays.)
+ const ConstantArrayType *CAT = getContext().getAsConstantArrayType(BaseType);
+ llvm::Value *VLASize = nullptr;
+
+ if (!CAT) {
+ if (const VariableArrayType *VAT =
+ getContext().getAsVariableArrayType(BaseType))
+ VLASize = getVLASize(VAT).NumElts;
+ else
+ return; // Not a constant or VLA.
+ }
+
+ llvm::APInt ArraySize;
+ if (CAT)
+ ArraySize = CAT->getSize();
+
+ // Don't generate assumes for flexible array member pattern.
+ // Size-1 arrays: "struct { int len; char data[1]; }" (pre-C99 idiom.)
+ // Zero-length arrays: "struct { int len; char data[0]; }" (GCC extension
+ // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html)
+ // Both patterns use arrays as placeholders for variable-length data.
+ if (CAT && (ArraySize == 0 || ArraySize == 1)) {
----------------
efriedma-quic wrote:
This should respect StrictFlexArraysLevel, probably.
https://github.com/llvm/llvm-project/pull/159046
More information about the cfe-commits
mailing list