-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[HLSL] error on out of bounds vector accesses #128952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
656d6e8
f44e697
8449ac1
14f60af
8488243
2fa2c72
7ef3654
d49eb7c
73d4dd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14054,6 +14054,23 @@ void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { | |
<< TRange << Op->getSourceRange(); | ||
} | ||
|
||
void Sema::CheckVectorAccess(const Expr *BaseExpr, const Expr *IndexExpr) { | ||
const auto *VTy = BaseExpr->getType()->getAs<VectorType>(); | ||
if (!VTy) | ||
return; | ||
|
||
Expr::EvalResult Result; | ||
if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally, if a value is not required to be a constant, we don't want different semantic rules depending on whether the value is in fact constant. (There are a few places that do in fact work like this, like array bounds, but it causes weird results. Especially when people do stuff with macros.) So I don't think we want to reject here. A DiagRuntimeBehavior() is probably appropriate, though. |
||
return; | ||
|
||
unsigned DiagID = diag::err_vector_index_out_of_range; | ||
|
||
llvm::APSInt index = Result.Val.getInt(); | ||
if (index.isNegative() || index >= VTy->getNumElements()) | ||
Diag(BaseExpr->getBeginLoc(), DiagID) << toString(index, 10, true); | ||
return; | ||
} | ||
|
||
void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, | ||
const ArraySubscriptExpr *ASE, | ||
bool AllowOnePastEnd, bool IndexNegated) { | ||
|
@@ -14068,6 +14085,12 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, | |
const Type *EffectiveType = | ||
BaseExpr->getType()->getPointeeOrArrayElementType(); | ||
BaseExpr = BaseExpr->IgnoreParenCasts(); | ||
|
||
if (BaseExpr->getType()->isVectorType()) { | ||
CheckVectorAccess(BaseExpr, IndexExpr); | ||
return; | ||
} | ||
|
||
const ConstantArrayType *ArrayTy = | ||
Context.getAsConstantArrayType(BaseExpr->getType()); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -verify | ||
|
||
export void fn1() { | ||
int2 A = {1,2}; | ||
int X = A[-1]; | ||
// expected-error@-1 {{vector element index -1 is out of bounds}} | ||
} | ||
|
||
export void fn2() { | ||
int4 A = {1,2,3,4}; | ||
int X = A[4]; | ||
// expected-error@-1 {{vector element index 4 is out of bounds}} | ||
} | ||
|
||
export void fn3() { | ||
bool2 A = {true,true}; | ||
bool X = A[-1]; | ||
// expected-error@-1 {{vector element index -1 is out of bounds}} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.