Bug 206055

Summary: Decoding an SVG off the main thread causes a crash
Product: WebKit Reporter: Chris Lord <clord>
Component: SVGAssignee: Chris Lord <clord>
Status: RESOLVED FIXED    
Severity: Normal CC: ap, cdumez, changseok, darin, dino, esprehn+autocc, ews-watchlist, fmalita, gyuyoung.kim, pdr, sabouhallawa, schenney, sergio, simon.fraser, webkit-bug-importer, zimmermann
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on:    
Bug Blocks: 183720    
Attachments:
Description Flags
Patch
none
Patch none

Description Chris Lord 2020-01-10 02:26:14 PST
If you decode an SVG off the main thread via ImageBitmap, this results in a crash. You can test this by enabling offscreen-canvas and running either of these two tests:

imported/w3c/web-platform-tests/offscreen-canvas/drawing-images-to-the-canvas/2d.drawImage.svg.worker.html
imported/w3c/web-platform-tests/offscreen-canvas/drawing-images-to-the-canvas/2d.drawImage.zerosource.image.worker.html
Comment 1 Simon Fraser (smfr) 2020-01-13 11:05:20 PST
None of the SVG code is thread safe. You'll need to bounce to the main thread for SVG.
Comment 2 Chris Lord 2021-04-30 05:09:54 PDT
Created attachment 427409 [details]
Patch
Comment 3 Chris Lord 2021-04-30 06:40:10 PDT
Comment on attachment 427409 [details]
Patch

ImageBitmap obviously doesn't need an actual SVG document, it was only using Image to get the bitmap data. Instead, use BitmapImage directly and bypass the SVG and PDF document image loaders.
Comment 4 Darin Adler 2021-04-30 10:33:11 PDT
Comment on attachment 427409 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=427409&action=review

> Source/WebCore/html/ImageBitmap.cpp:739
> +    auto image = BitmapImage::create(&observer.get());

The better idiom here is observer.ptr().

> Source/WebCore/html/ImageBitmap.cpp:760
> +    bitmapData->context().drawImage(image.get(), destRect, sourceRectangle.releaseReturnValue(), { interpolationQualityForResizeQuality(options.resizeQuality), imageOrientationForOrientation(options.imageOrientation) });

Not sure we need ".get()" here. Ref typically converts itself to a reference when passing it as an argument.

> Source/WebCore/svg/graphics/SVGImage.cpp:83
> +    ASSERT(isMainThread());

Consider also having this assertion in Image::create itself?

The assertion without a comment seems a little mysterious; not that I’d want a long comment.
Comment 5 Chris Lord 2021-05-03 01:33:46 PDT
Created attachment 427544 [details]
Patch
Comment 6 EWS 2021-05-03 02:29:58 PDT
Committed r276895 (237241@main): <https://commits.webkit.org/237241@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 427544 [details].
Comment 7 Radar WebKit Bug Importer 2021-05-03 02:30:18 PDT
<rdar://problem/77448836>