Skip to content

Commit 79d2b8d

Browse files
authored
[Sema] Change data type in MaxDispatchGrid validation calculation to uint64_t to prevent overflow (microsoft#6090)
Previously, in order to determine whether the three arguments provided to the MaxDispatchGrid attribute exceeded the maximum product value (which is 16,777,215 (2^24-1)), each of the three components would be multiplied together and compared to this maximum. However, the three arguments would be read in as unsigned ints. In certain conditions, the multiplication would cause an overflow, which would programmatically result in a number less than the maximum product limit, despite mathematically being above this limit. The diagnostic should trigger in this case, but it doesn't. This PR changes the data type of the three arguments to unsigned long long, which can hold the maximum possible product that can be achieved without triggering the per-multiplicand limit of 65535, and won't overflow in this case. Now, the compiler will trigger this diagnostic. Regression test has been added. Fixes microsoft#5988
1 parent f171ae8 commit 79d2b8d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13170,7 +13170,8 @@ void ValidateDispatchGridValues(DiagnosticsEngine &Diags,
1317013170
<< A.getName() << "Z" << A.getRange();
1317113171
z = 0;
1317213172
}
13173-
if (x * y * z > MaxProductValue)
13173+
uint64_t product = (uint64_t)x * (uint64_t)y * (uint64_t)z;
13174+
if (product > MaxProductValue)
1317413175
Diags.Report(A.getLoc(), diag::err_hlsl_dispatchgrid_product)
1317513176
<< A.getName() << A.getRange();
1317613177
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %dxc -T lib_6_8 -verify %s
2+
3+
[Shader("node")]
4+
[NodeLaunch("broadcasting")]
5+
[NodeDispatchGrid(65535, 65535, 65535)] // expected-error {{'NodeDispatchGrid' X * Y * Z product may not exceed 16,777,215 (2^24-1)}}
6+
[NumThreads(1, 1, 1)]
7+
void myNode() { }

0 commit comments

Comments
 (0)