| 1 |
From 33b70be027e80ccc2e1c36dd60db32791a7f34ef Mon Sep 17 00:00:00 2001 |
| 2 |
From: Kaylee Lubick <[email protected]> |
| 3 |
Date: Mon, 18 May 2026 16:07:23 +0000 |
| 4 |
Subject: [PATCH] Fix pathops bug with linked lists in SkOpCoincidence |
| 5 |
|
| 6 |
I added some logging to SkOpCoincidence::fixUp and ::release and, |
| 7 |
with the new test case (which has multiple coincidences [1]), observed |
| 8 |
|
| 9 |
``` |
| 10 |
fixUp: release(0x...7378, 0x...7378) |
| 11 |
release: head_arg=0x...7378, remove=0x...7378, global fHead=...7378 |
| 12 |
release: updated global fHead to 0x...72b8 |
| 13 |
fixUp: after release global fHead=0x...72b8 |
| 14 |
fixUp: release(0x...7378, 0x...6ec8) |
| 15 |
``` |
| 16 |
|
| 17 |
As per the linked bug, this leads to a faulty state because the |
| 18 |
old head pointer 0x...7378 is being used after it was "released". |
| 19 |
I could not get ASAN to fire on this but it is worth fixing for |
| 20 |
correctness and consistency. |
| 21 |
|
| 22 |
My change fixes that particular issue and adds some asserts to avoid |
| 23 |
this happening again. |
| 24 |
|
| 25 |
I also rewrote some do while loops to |
| 26 |
avoid iffy behavior where we were releasing coin and then for |
| 27 |
the next iteration still calling coin->next(). This worked because |
| 28 |
we had an SkArenaAlloc and that memory wasn't "fully" released. |
| 29 |
|
| 30 |
[1] a coincidence is an overlap between two path segments that |
| 31 |
lasts for more than a single point. e.g. two squares sharing an edge. |
| 32 |
|
| 33 |
Bug: https://issues.chromium.org/issues/513001309 |
| 34 |
Fixed: 513001309 |
| 35 |
Change-Id: Ie3f6cea575acf9575e984b2b0d2eb788bb4b2052 |
| 36 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1238176 |
| 37 |
Reviewed-by: Thomas Smith <[email protected]> |
| 38 |
Commit-Queue: Kaylee Lubick <[email protected]> |
| 39 |
--- |
| 40 |
src/pathops/SkOpCoincidence.cpp | 94 +++++++++++++++++---------------- |
| 41 |
src/pathops/SkOpCoincidence.h | 8 +-- |
| 42 |
4 files changed, 88 insertions(+), 63 deletions(-) |
| 43 |
|
| 44 |
diff --git a/src/pathops/SkOpCoincidence.cpp b/src/pathops/SkOpCoincidence.cpp |
| 45 |
index 4a8bcec1d8..a8a604ed36 100644 |
| 46 |
--- a/src/pathops/SkOpCoincidence.cpp |
| 47 |
+++ b/src/pathops/SkOpCoincidence.cpp |
| 48 |
@@ -695,8 +695,8 @@ bool SkOpCoincidence::addOrOverlap(SkOpSegment* coinSeg, SkOpSegment* oppSeg, |
| 49 |
: overlap->oppPtTEnd()->fT < test->oppPtTEnd()->fT) { |
| 50 |
overlap->setOppPtTEnd(test->oppPtTEnd()); |
| 51 |
} |
| 52 |
- if (!fHead || !this->release(fHead, test)) { |
| 53 |
- SkAssertResult(this->release(fTop, test)); |
| 54 |
+ if (!fHead || !this->release(&fHead, test)) { |
| 55 |
+ SkAssertResult(this->release(&fTop, test)); |
| 56 |
} |
| 57 |
} |
| 58 |
const SkOpPtT* cs = coinSeg->existing(coinTs, oppSeg); |
| 59 |
@@ -1157,57 +1157,54 @@ bool SkOpCoincidence::apply(DEBUG_COIN_DECLARE_ONLY_PARAMS()) { |
| 60 |
} |
| 61 |
|
| 62 |
// Please keep this in sync with debugRelease() |
| 63 |
-bool SkOpCoincidence::release(SkCoincidentSpans* coin, SkCoincidentSpans* remove) { |
| 64 |
- SkCoincidentSpans* head = coin; |
| 65 |
+bool SkOpCoincidence::release(SkCoincidentSpans** headPtr, SkCoincidentSpans* remove) { |
| 66 |
+ SkASSERT(headPtr == &fHead || headPtr == &fTop); |
| 67 |
+ SkCoincidentSpans* coin = *headPtr; |
| 68 |
SkCoincidentSpans* prev = nullptr; |
| 69 |
SkCoincidentSpans* next; |
| 70 |
- do { |
| 71 |
+ while (coin) { |
| 72 |
next = coin->next(); |
| 73 |
if (coin == remove) { |
| 74 |
if (prev) { |
| 75 |
prev->setNext(next); |
| 76 |
- } else if (head == fHead) { |
| 77 |
- fHead = next; |
| 78 |
} else { |
| 79 |
- fTop = next; |
| 80 |
+ *headPtr = next; |
| 81 |
} |
| 82 |
break; |
| 83 |
} |
| 84 |
prev = coin; |
| 85 |
- } while ((coin = next)); |
| 86 |
+ coin = next; |
| 87 |
+ } |
| 88 |
return coin != nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
-void SkOpCoincidence::releaseDeleted(SkCoincidentSpans* coin) { |
| 92 |
- if (!coin) { |
| 93 |
- return; |
| 94 |
- } |
| 95 |
- SkCoincidentSpans* head = coin; |
| 96 |
+void SkOpCoincidence::releaseDeleted(SkCoincidentSpans** headPtr) { |
| 97 |
+ SkASSERT(headPtr == &fHead || headPtr == &fTop); |
| 98 |
+ SkCoincidentSpans* coin = *headPtr; |
| 99 |
SkCoincidentSpans* prev = nullptr; |
| 100 |
SkCoincidentSpans* next; |
| 101 |
- do { |
| 102 |
+ while (coin) { |
| 103 |
next = coin->next(); |
| 104 |
if (coin->coinPtTStart()->deleted()) { |
| 105 |
SkOPASSERT(coin->flipped() ? coin->oppPtTEnd()->deleted() : |
| 106 |
coin->oppPtTStart()->deleted()); |
| 107 |
if (prev) { |
| 108 |
prev->setNext(next); |
| 109 |
- } else if (head == fHead) { |
| 110 |
- fHead = next; |
| 111 |
} else { |
| 112 |
- fTop = next; |
| 113 |
+ *headPtr = next; |
| 114 |
} |
| 115 |
} else { |
| 116 |
SkOPASSERT(coin->flipped() ? !coin->oppPtTEnd()->deleted() : |
| 117 |
!coin->oppPtTStart()->deleted()); |
| 118 |
prev = coin; |
| 119 |
} |
| 120 |
- } while ((coin = next)); |
| 121 |
+ coin = next; |
| 122 |
+ } |
| 123 |
} |
| 124 |
|
| 125 |
void SkOpCoincidence::releaseDeleted() { |
| 126 |
- this->releaseDeleted(fHead); |
| 127 |
- this->releaseDeleted(fTop); |
| 128 |
+ this->releaseDeleted(&fHead); |
| 129 |
+ this->releaseDeleted(&fTop); |
| 130 |
} |
| 131 |
|
| 132 |
void SkOpCoincidence::restoreHead() { |
| 133 |
@@ -1248,7 +1245,7 @@ bool SkOpCoincidence::expand(DEBUG_COIN_DECLARE_ONLY_PARAMS()) { |
| 134 |
} |
| 135 |
if (coin->coinPtTStart() == test->coinPtTStart() |
| 136 |
&& coin->oppPtTStart() == test->oppPtTStart()) { |
| 137 |
- this->release(fHead, test); |
| 138 |
+ this->release(&fHead, test); |
| 139 |
break; |
| 140 |
} |
| 141 |
} while ((test = test->next())); |
| 142 |
@@ -1297,45 +1294,52 @@ bool SkOpCoincidence::findOverlaps(SkOpCoincidence* overlaps DEBUG_COIN_DECLARE |
| 143 |
void SkOpCoincidence::fixUp(SkOpPtT* deleted, const SkOpPtT* kept) { |
| 144 |
SkOPASSERT(deleted != kept); |
| 145 |
if (fHead) { |
| 146 |
- this->fixUp(fHead, deleted, kept); |
| 147 |
+ this->fixUp(&fHead, deleted, kept); |
| 148 |
} |
| 149 |
if (fTop) { |
| 150 |
- this->fixUp(fTop, deleted, kept); |
| 151 |
+ this->fixUp(&fTop, deleted, kept); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
-void SkOpCoincidence::fixUp(SkCoincidentSpans* coin, SkOpPtT* deleted, const SkOpPtT* kept) { |
| 156 |
- SkCoincidentSpans* head = coin; |
| 157 |
- do { |
| 158 |
+void SkOpCoincidence::fixUp(SkCoincidentSpans** headPtr, SkOpPtT* deleted, const SkOpPtT* kept) { |
| 159 |
+ SkASSERT(headPtr == &fHead || headPtr == &fTop); |
| 160 |
+ SkCoincidentSpans* coin = *headPtr; |
| 161 |
+ while (coin) { |
| 162 |
+ SkCoincidentSpans* next = coin->next(); |
| 163 |
if (coin->coinPtTStart() == deleted) { |
| 164 |
if (coin->coinPtTEnd()->span() == kept->span()) { |
| 165 |
- this->release(head, coin); |
| 166 |
+ this->release(headPtr, coin); |
| 167 |
+ coin = next; |
| 168 |
continue; |
| 169 |
} |
| 170 |
coin->setCoinPtTStart(kept); |
| 171 |
} |
| 172 |
if (coin->coinPtTEnd() == deleted) { |
| 173 |
if (coin->coinPtTStart()->span() == kept->span()) { |
| 174 |
- this->release(head, coin); |
| 175 |
+ this->release(headPtr, coin); |
| 176 |
+ coin = next; |
| 177 |
continue; |
| 178 |
} |
| 179 |
coin->setCoinPtTEnd(kept); |
| 180 |
} |
| 181 |
if (coin->oppPtTStart() == deleted) { |
| 182 |
if (coin->oppPtTEnd()->span() == kept->span()) { |
| 183 |
- this->release(head, coin); |
| 184 |
+ this->release(headPtr, coin); |
| 185 |
+ coin = next; |
| 186 |
continue; |
| 187 |
} |
| 188 |
coin->setOppPtTStart(kept); |
| 189 |
} |
| 190 |
if (coin->oppPtTEnd() == deleted) { |
| 191 |
if (coin->oppPtTStart()->span() == kept->span()) { |
| 192 |
- this->release(head, coin); |
| 193 |
+ this->release(headPtr, coin); |
| 194 |
+ coin = next; |
| 195 |
continue; |
| 196 |
} |
| 197 |
coin->setOppPtTEnd(kept); |
| 198 |
} |
| 199 |
- } while ((coin = coin->next())); |
| 200 |
+ coin = next; |
| 201 |
+ } |
| 202 |
} |
| 203 |
|
| 204 |
// Please keep this in sync with debugMark() |
| 205 |
@@ -1386,9 +1390,11 @@ bool SkOpCoincidence::mark(DEBUG_COIN_DECLARE_ONLY_PARAMS()) { |
| 206 |
} |
| 207 |
|
| 208 |
// Please keep in sync with debugMarkCollapsed() |
| 209 |
-void SkOpCoincidence::markCollapsed(SkCoincidentSpans* coin, SkOpPtT* test) { |
| 210 |
- SkCoincidentSpans* head = coin; |
| 211 |
+void SkOpCoincidence::markCollapsed(SkCoincidentSpans** headPtr, SkOpPtT* test) { |
| 212 |
+ SkASSERT(headPtr == &fHead || headPtr == &fTop); |
| 213 |
+ SkCoincidentSpans* coin = *headPtr; |
| 214 |
while (coin) { |
| 215 |
+ SkCoincidentSpans* next = coin->next(); |
| 216 |
if (coin->collapsed(test)) { |
| 217 |
if (zero_or_one(coin->coinPtTStart()->fT) && zero_or_one(coin->coinPtTEnd()->fT)) { |
| 218 |
coin->coinPtTStartWritable()->segment()->markAllDone(); |
| 219 |
@@ -1396,16 +1402,16 @@ void SkOpCoincidence::markCollapsed(SkCoincidentSpans* coin, SkOpPtT* test) { |
| 220 |
if (zero_or_one(coin->oppPtTStart()->fT) && zero_or_one(coin->oppPtTEnd()->fT)) { |
| 221 |
coin->oppPtTStartWritable()->segment()->markAllDone(); |
| 222 |
} |
| 223 |
- this->release(head, coin); |
| 224 |
+ this->release(headPtr, coin); |
| 225 |
} |
| 226 |
- coin = coin->next(); |
| 227 |
+ coin = next; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
// Please keep in sync with debugMarkCollapsed() |
| 232 |
void SkOpCoincidence::markCollapsed(SkOpPtT* test) { |
| 233 |
- markCollapsed(fHead, test); |
| 234 |
- markCollapsed(fTop, test); |
| 235 |
+ markCollapsed(&fHead, test); |
| 236 |
+ markCollapsed(&fTop, test); |
| 237 |
} |
| 238 |
|
| 239 |
bool SkOpCoincidence::Ordered(const SkOpSegment* coinSeg, const SkOpSegment* oppSeg) { |
| 240 |
@@ -1439,18 +1445,16 @@ bool SkOpCoincidence::overlap(const SkOpPtT* coin1s, const SkOpPtT* coin1e, |
| 241 |
return *overS < *overE; |
| 242 |
} |
| 243 |
|
| 244 |
-// Commented-out lines keep this in sync with debugRelease() |
| 245 |
void SkOpCoincidence::release(const SkOpSegment* deleted) { |
| 246 |
SkCoincidentSpans* coin = fHead; |
| 247 |
- if (!coin) { |
| 248 |
- return; |
| 249 |
- } |
| 250 |
- do { |
| 251 |
+ while (coin) { |
| 252 |
+ SkCoincidentSpans* next = coin->next(); |
| 253 |
if (coin->coinPtTStart()->segment() == deleted |
| 254 |
|| coin->coinPtTEnd()->segment() == deleted |
| 255 |
|| coin->oppPtTStart()->segment() == deleted |
| 256 |
|| coin->oppPtTEnd()->segment() == deleted) { |
| 257 |
- this->release(fHead, coin); |
| 258 |
+ this->release(&fHead, coin); |
| 259 |
} |
| 260 |
- } while ((coin = coin->next())); |
| 261 |
+ coin = next; |
| 262 |
+ } |
| 263 |
} |
| 264 |
diff --git a/src/pathops/SkOpCoincidence.h b/src/pathops/SkOpCoincidence.h |
| 265 |
index cf8bb18d78..3f4b26371d 100644 |
| 266 |
--- a/src/pathops/SkOpCoincidence.h |
| 267 |
+++ b/src/pathops/SkOpCoincidence.h |
| 268 |
@@ -282,13 +282,13 @@ private: |
| 269 |
void debugAddEndMovedSpans(SkPathOpsDebug::GlitchLog* , |
| 270 |
const SkOpPtT* ptT) const; |
| 271 |
#endif |
| 272 |
- void fixUp(SkCoincidentSpans* coin, SkOpPtT* deleted, const SkOpPtT* kept); |
| 273 |
- void markCollapsed(SkCoincidentSpans* head, SkOpPtT* test); |
| 274 |
+ void fixUp(SkCoincidentSpans** headPtr, SkOpPtT* deleted, const SkOpPtT* kept); |
| 275 |
+ void markCollapsed(SkCoincidentSpans** headPtr, SkOpPtT* test); |
| 276 |
bool overlap(const SkOpPtT* coinStart1, const SkOpPtT* coinEnd1, |
| 277 |
const SkOpPtT* coinStart2, const SkOpPtT* coinEnd2, |
| 278 |
double* overS, double* overE) const; |
| 279 |
- bool release(SkCoincidentSpans* coin, SkCoincidentSpans* ); |
| 280 |
- void releaseDeleted(SkCoincidentSpans* ); |
| 281 |
+ bool release(SkCoincidentSpans** headPtr, SkCoincidentSpans* ); |
| 282 |
+ void releaseDeleted(SkCoincidentSpans** headPtr); |
| 283 |
void restoreHead(); |
| 284 |
// return coinPtT->segment()->t mapped from overS->fT <= t <= overE->fT |
| 285 |
static double TRange(const SkOpPtT* overS, double t, const SkOpSegment* coinPtT |
| 286 |
-- |
| 287 |
2.43.0 |
| 288 |
|