Skip to content

Commit a54abe8

Browse files
authored
Add earlier check for invalid SV_Target[n] (microsoft#6771)
According to the HLSL semantics documentation, the valid semantic indices for SV_Target[n] are 0 <= n <= 7: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics A check for this already exists in DXIL validation, but for large values of n, crashes and/or buffer overruns may occur during compilation before validation, so an earlier check is needed. Fixes microsoft#6115
1 parent b18fe87 commit a54abe8

File tree

7 files changed

+49
-2
lines changed

7 files changed

+49
-2
lines changed

tools/clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7962,6 +7962,8 @@ def warn_hlsl_barrier_no_mem_with_required_device_scope: Warning<
79627962
def warn_hlsl_legacy_integer_literal_signedness: Warning<
79637963
"literal value is treated as signed in HLSL 2021 and earlier, and unsigned in later language versions">,
79647964
InGroup<HLSLLegacyLiterals>, DefaultIgnore;
7965+
def err_hlsl_unsupported_semantic_index: Error<
7966+
"'%0' is defined with semantic index %1, but only values 0 through %2 are supported">;
79657967
// HLSL Change Ends
79667968

79677969
// SPIRV Change Starts

tools/clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9108,6 +9108,8 @@ class Sema {
91089108
bool isImplicit);
91099109
QualType getHLSLDefaultSpecialization(TemplateDecl *Decl);
91109110
// HLSL Change End - adjust this from T* to T&-like
9111+
9112+
void DiagnoseSemanticDecl(hlsl::SemanticDecl *Decl); // HLSL Change
91119113
};
91129114

91139115
/// \brief RAII object that enters a new expression evaluation context.

tools/clang/lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ bool Parser::MaybeParseHLSLAttributes(std::vector<hlsl::UnusualAnnotation *> &ta
630630
}
631631
hlsl::SemanticDecl *pUA = new (context) hlsl::SemanticDecl(semanticName);
632632
pUA->Loc = Tok.getLocation();
633+
Actions.DiagnoseSemanticDecl(pUA);
633634
ConsumeToken(); // consume semantic
634635

635636
target.push_back(pUA);

tools/clang/lib/Sema/SemaDecl.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "clang/Sema/SemaInternal.h"
1514
#include "TypeLocBuilder.h"
15+
#include "dxc/DXIL/DxilSemantic.h" // HLSL Change
1616
#include "clang/AST/ASTConsumer.h"
1717
#include "clang/AST/ASTContext.h"
1818
#include "clang/AST/ASTLambda.h"
@@ -42,13 +42,14 @@
4242
#include "clang/Sema/ParsedTemplate.h"
4343
#include "clang/Sema/Scope.h"
4444
#include "clang/Sema/ScopeInfo.h"
45+
#include "clang/Sema/SemaHLSL.h" // HLSL Change
46+
#include "clang/Sema/SemaInternal.h"
4547
#include "clang/Sema/Template.h"
4648
#include "llvm/ADT/SmallString.h"
4749
#include "llvm/ADT/Triple.h"
4850
#include <algorithm>
4951
#include <cstring>
5052
#include <functional>
51-
#include "clang/Sema/SemaHLSL.h" // HLSL Change
5253
using namespace clang;
5354
using namespace sema;
5455

@@ -14629,3 +14630,20 @@ AvailabilityResult Sema::getCurContextAvailability() const {
1462914630
return D ? D->getAvailability() : AR_Available;
1463014631
}
1463114632

14633+
// HLSL Change Begin
14634+
void Sema::DiagnoseSemanticDecl(hlsl::SemanticDecl *Decl) {
14635+
StringRef SemName = Decl->SemanticName;
14636+
14637+
StringRef BaseSemName; // The 'FOO' in 'FOO1'
14638+
uint32_t SemIndex; // The '1' in 'FOO1'
14639+
14640+
// Split name and index.
14641+
hlsl::Semantic::DecomposeNameAndIndex(SemName, &BaseSemName, &SemIndex);
14642+
14643+
// The valid semantic indices for SV_Target[n] are 0 <= n <= 7.
14644+
if (BaseSemName.equals("SV_Target") && SemIndex > 7) {
14645+
Diag(Decl->Loc, diag::err_hlsl_unsupported_semantic_index)
14646+
<< SemName << SemIndex << "7";
14647+
}
14648+
}
14649+
// HLSL Change Ends
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %dxc -T ps_6_0 -E zero -verify %s
2+
// RUN: %dxc -T ps_6_0 -E seven -verify %s
3+
4+
float4 zero() : SV_Target0 { /* expected-no-diagnostics */
5+
return float4(0, 1, 2, 3);
6+
}
7+
8+
float4 seven() : SV_Target7 { /* expected-no-diagnostics */
9+
return float4(0, 1, 2, 3);
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %dxc -T ps_6_0 -verify %s
2+
3+
float4 bad() : SV_Target8 { /* expected-error{{'SV_Target8' is defined with semantic index 8, but only values 0 through 7 are supported}} */
4+
return float4(0, 1, 2, 3);
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %dxc -T ps_6_0 -verify %s
2+
3+
struct S {
4+
float4 t : SV_Target12345; /* expected-error{{'SV_Target12345' is defined with semantic index 12345, but only values 0 through 7 are supported}} */
5+
};
6+
7+
S main() {
8+
return S(float4(0, 1, 2, 3));
9+
}

0 commit comments

Comments
 (0)