Debian Codegraph @

debian/patches/CVE-2026-9998

raw
1 From 5f4f454b9662b83facd71dcbc6e8ac105660e938 Mon Sep 17 00:00:00 2001
2 From: Kaylee Lubick <[email protected]>
3 Date: Wed, 20 May 2026 08:23:28 -0400
4 Subject: [PATCH] Fix for integer wraparound in sksl
5
6 I found the ES 2 spec [1] helpful for reference here.
7
8 The calculate_count_neq_int is not strictly necessary (I was unable
9 to find a case that tricked the existing floats with ints), but
10 I like the refactoring and it mirrors the gt/lt cases nicely.
11
12 [1] https://registry.khronos.org/OpenGL/specs/es/2.0/GLSL_ES_Specification_1.00.pdf
13
14 Change-Id: I0b9117f347e4b7d5d336de0f14337b9bec510ff2
15 Bug: https://issues.chromium.org/issues/513337118
16 Fixed: 513337118
17 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1236656
18 Commit-Queue: Florin Malita <[email protected]>
19 Reviewed-by: Florin Malita <[email protected]>
20 Commit-Queue: Kaylee Lubick <[email protected]>
21 Auto-Submit: Kaylee Lubick <[email protected]>
22 ---
23 gn/sksl_tests.gni | 2 +
24 resources/sksl/BUILD.bazel | 2 +
25 resources/sksl/errors/ForLoopNEQOverflow.rts | 11 ++
26 resources/sksl/errors/ForLoopOverflow.rts | 11 ++
27 src/base/SkSafeMath.h | 9 ++
28 src/sksl/analysis/SkSLGetLoopUnrollInfo.cpp | 130 ++++++++++++++++---
29 tests/sksl/errors/ForLoopNEQOverflow.glsl | 6 +
30 tests/sksl/errors/ForLoopOverflow.glsl | 6 +
31 8 files changed, 157 insertions(+), 20 deletions(-)
32 create mode 100644 resources/sksl/errors/ForLoopNEQOverflow.rts
33 create mode 100644 resources/sksl/errors/ForLoopOverflow.rts
34 create mode 100644 tests/sksl/errors/ForLoopNEQOverflow.glsl
35 create mode 100644 tests/sksl/errors/ForLoopOverflow.glsl
36
37 diff --git a/gn/sksl_tests.gni b/gn/sksl_tests.gni
38 index 1cd9317f04..1a1804f842 100644
39 --- a/gn/sksl_tests.gni
40 +++ b/gn/sksl_tests.gni
41 @@ -142,6 +142,8 @@ sksl_error_tests = [
42 "errors/FloatRemainder.rts",
43 "errors/ForInitStmt.sksl",
44 "errors/ForLoopInductionVariableScope.sksl",
45 + "errors/ForLoopNEQOverflow.rts",
46 + "errors/ForLoopOverflow.rts",
47 "errors/ForTypeMismatch.rts",
48 "errors/FunctionParamBadType.rts",
49 "errors/FunctionParamShadowedByLocal.rts",
50 diff --git a/src/base/SkSafeMath.h b/src/base/SkSafeMath.h
51 index d8f9fbc965..590098a5ca 100644
52 --- a/src/base/SkSafeMath.h
53 +++ b/src/base/SkSafeMath.h
54 @@ -41,6 +41,7 @@ public:
55 * be set to false, and it is undefined what this returns.
56 */
57 int addInt(int a, int b) {
58 + static_assert(sizeof(int) == 4, "int is not 4 bytes");
59 if (b < 0 && a < std::numeric_limits<int>::min() - b) {
60 fOK = false;
61 return a;
62 @@ -51,6 +52,14 @@ public:
63 return a + b;
64 }
65
66 + int subInt(int a, int b) {
67 + if (b == std::numeric_limits<int>::min()) {
68 + fOK = false;
69 + return a;
70 + }
71 + return addInt(a, -b);
72 + }
73 +
74 int mulInt(int x, int y) {
75 int64_t result = (int64_t)x * (int64_t)y;
76 if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
77 diff --git a/src/sksl/analysis/SkSLGetLoopUnrollInfo.cpp b/src/sksl/analysis/SkSLGetLoopUnrollInfo.cpp
78 index 70a2170ba3..974db63e36 100644
79 --- a/src/sksl/analysis/SkSLGetLoopUnrollInfo.cpp
80 +++ b/src/sksl/analysis/SkSLGetLoopUnrollInfo.cpp
81 @@ -7,6 +7,7 @@
82
83 #include "include/core/SkTypes.h"
84 #include "include/private/base/SkFloatingPoint.h"
85 +#include "src/base/SkSafeMath.h"
86 #include "src/sksl/SkSLAnalysis.h"
87 #include "src/sksl/SkSLConstantFolder.h"
88 #include "src/sksl/SkSLErrorReporter.h"
89 @@ -35,25 +36,106 @@ class Context;
90 // Loops that run for 100000+ iterations will exceed our program size limit.
91 static constexpr int kLoopTerminationLimit = 100000;
92
93 -static int calculate_count(double start, double end, double delta, bool forwards, bool inclusive) {
94 - if ((forwards && start > end) || (!forwards && start < end)) {
95 +enum class Direction {
96 + kBackwards,
97 + kForwards,
98 +};
99 +
100 +enum class Inclusive : bool {
101 + kNo = false,
102 + kYes = true,
103 +};
104 +
105 +enum class LoopType {
106 + kFloat,
107 + kInt,
108 +};
109 +
110 +static int calculate_count_float(double start, double end, double delta,
111 + Inclusive inclusive) {
112 + double iterations = sk_ieee_double_divide(end - start, delta);
113 + double count = std::ceil(iterations);
114 + if (inclusive == Inclusive::kYes && (count == iterations)) {
115 + count += 1.0;
116 + }
117 + if (count > kLoopTerminationLimit || !std::isfinite(count)) {
118 + // The loop runs for more iterations than we can safely unroll.
119 + return kLoopTerminationLimit;
120 + }
121 + return sk_double_saturate2int(count);
122 +}
123 +
124 +static int calculate_count_int(int32_t start, int32_t end, int32_t delta,
125 + Inclusive inclusive) {
126 + if (delta == 0) {
127 + return kLoopTerminationLimit;
128 + }
129 + SkSafeMath math;
130 + int roundUp = delta > 0 ? math.subInt(delta, 1) : math.addInt(delta, 1);
131 + int width = math.subInt(end, start);
132 + int iterations = math.addInt(width, roundUp) / delta;
133 + if (inclusive == Inclusive::kYes && width % delta == 0) {
134 + iterations = math.addInt(iterations, 1);
135 + }
136 + // Check that we won't overflow while looping
137 + math.addInt(start, math.mulInt(delta, iterations));
138 + if (!math || iterations < 0 || iterations > kLoopTerminationLimit) {
139 + return kLoopTerminationLimit;
140 + }
141 + return iterations;
142 +}
143 +
144 +static int calculate_count(double start, double end, double delta, Direction dir,
145 + Inclusive inclusive, LoopType loop) {
146 + if ((dir == Direction::kForwards && start > end) ||
147 + (dir == Direction::kBackwards && start < end)) {
148 // The loop starts in a completed state (the start has already advanced past the end).
149 return 0;
150 }
151 - if ((delta == 0.0) || forwards != (delta > 0.0)) {
152 + if ((delta == 0.0) ||
153 + (delta > 0.0 && dir == Direction::kBackwards) ||
154 + (delta < 0.0 && dir == Direction::kForwards)) {
155 // The loop does not progress toward a completed state, and will never terminate.
156 return kLoopTerminationLimit;
157 }
158 + if (loop == LoopType::kInt) {
159 + return calculate_count_int((int32_t)start, (int32_t)end, (int32_t)delta, inclusive);
160 + }
161 + return calculate_count_float(start, end, delta, inclusive);
162 +}
163 +
164 +static int calculate_count_neq_int(int32_t start, int32_t end, int32_t delta) {
165 + if (delta == 0) {
166 + return kLoopTerminationLimit;
167 + }
168 + SkSafeMath math;
169 + int iterations = math.subInt(end, start) / delta;
170 + // Check that we won't overflow while looping and that we actually hit end.
171 + int lastValue = math.addInt(start, math.mulInt(delta, iterations));
172 + if (!math || lastValue != end || iterations < 0 || iterations > kLoopTerminationLimit) {
173 + return kLoopTerminationLimit;
174 + }
175 + return iterations;
176 +}
177 +
178 +static int calculate_count_neq_float(double start, double end, double delta) {
179 + if (delta == 0.0) {
180 + return kLoopTerminationLimit;
181 + }
182 double iterations = sk_ieee_double_divide(end - start, delta);
183 double count = std::ceil(iterations);
184 - if (inclusive && (count == iterations)) {
185 - count += 1.0;
186 - }
187 - if (count > kLoopTerminationLimit || !std::isfinite(count)) {
188 - // The loop runs for more iterations than we can safely unroll.
189 + if (count < 0 || count != iterations || !std::isfinite(iterations)) {
190 + // The loop doesn't reach the exact endpoint and so will never terminate.
191 return kLoopTerminationLimit;
192 }
193 - return (int)count;
194 + return sk_double_saturate2int(count);
195 +}
196 +
197 +static int calculate_count_neq(double start, double end, double delta, LoopType loop) {
198 + if (loop == LoopType::kInt) {
199 + return calculate_count_neq_int((int32_t)start, (int32_t)end, (int32_t)delta);
200 + }
201 + return calculate_count_neq_float(start, end, delta);
202 }
203
204 std::unique_ptr<LoopUnrollInfo> Analysis::GetLoopUnrollInfo(const Context& context,
205 @@ -226,35 +308,43 @@ std::unique_ptr<LoopUnrollInfo> Analysis::GetLoopUnrollInfo(const Context& conte
206 // Finally, compute the iteration count, based on the bounds, and the termination operator.
207 loopInfo->fCount = 0;
208
209 + // Strict ES2 requires loop induction variables to be either 'int' or 'float'. For 'int'
210 + // variables, we simulate the loop using 32-bit signed math to correctly detect the integer
211 + // wraparound behavior that would occur at runtime on the GPU. (For 'float' variables,
212 + // the existing double-precision calculation is sufficient.)
213 + LoopType loop;
214 + if (initDecl.baseType().isSigned()) {
215 + SkASSERT(initDecl.baseType().bitWidth() == 32);
216 + loop = LoopType::kInt;
217 + } else {
218 + SkASSERT(initDecl.baseType().isFloat());
219 + loop = LoopType::kFloat;
220 + }
221 +
222 switch (cond->getOperator().kind()) {
223 case Operator::Kind::LT:
224 loopInfo->fCount = calculate_count(loopInfo->fStart, loopEnd, loopInfo->fDelta,
225 - /*forwards=*/true, /*inclusive=*/false);
226 + Direction::kForwards, Inclusive::kNo, loop);
227 break;
228
229 case Operator::Kind::GT:
230 loopInfo->fCount = calculate_count(loopInfo->fStart, loopEnd, loopInfo->fDelta,
231 - /*forwards=*/false, /*inclusive=*/false);
232 + Direction::kBackwards, Inclusive::kNo, loop);
233 break;
234
235 case Operator::Kind::LTEQ:
236 loopInfo->fCount = calculate_count(loopInfo->fStart, loopEnd, loopInfo->fDelta,
237 - /*forwards=*/true, /*inclusive=*/true);
238 + Direction::kForwards, Inclusive::kYes, loop);
239 break;
240
241 case Operator::Kind::GTEQ:
242 loopInfo->fCount = calculate_count(loopInfo->fStart, loopEnd, loopInfo->fDelta,
243 - /*forwards=*/false, /*inclusive=*/true);
244 + Direction::kBackwards, Inclusive::kYes, loop);
245 break;
246
247 case Operator::Kind::NEQ: {
248 - float iterations = sk_ieee_double_divide(loopEnd - loopInfo->fStart, loopInfo->fDelta);
249 - loopInfo->fCount = std::ceil(iterations);
250 - if (loopInfo->fCount < 0 || loopInfo->fCount != iterations ||
251 - !std::isfinite(iterations)) {
252 - // The loop doesn't reach the exact endpoint and so will never terminate.
253 - loopInfo->fCount = kLoopTerminationLimit;
254 - }
255 + loopInfo->fCount = calculate_count_neq(loopInfo->fStart, loopEnd, loopInfo->fDelta,
256 + loop);
257 if (loopInfo->fIndex->type().componentType().isFloat()) {
258 // Rewrite `x != n` tests as `x < n` or `x > n` depending on the loop direction.
259 // Less-than and greater-than tests avoid infinite loops caused by rounding error.
260 --
261 2.43.0
262