| 1 |
From ba3ee9b265f06ed1f16e4d7402d29ad7ac583e7a Mon Sep 17 00:00:00 2001 |
| 2 |
From: alexisdavidc <[email protected]> |
| 3 |
Date: Fri, 12 Jun 2026 13:28:34 -0400 |
| 4 |
Subject: [PATCH] Resolved a Data Race on fStream in SkTypeface_Mac |
| 5 |
|
| 6 |
There was a data race in SkTypeface_Mac where `onOpenStream` |
| 7 |
and `onOpenExistingStream` would race to read/write `fStream`. While `onOpenStream` would begin initializing `fStream` on one thread, a separate thread could be calling `onOpenExistingStream` to try to read `fStream` before it was done initializing. |
| 8 |
|
| 9 |
The issue was resolved by applying a mutex on `fStream`. |
| 10 |
|
| 11 |
The cl introducing this bug (https://skia-review.git.corp.google.com/c/skia/+/204720) was focused on caching the typefaces received with a global process wide `gTFCache` to save on performance and memory. The issue arose in that since the SkTypeface_Mac could be accessed across threads, it became thread unsafe. |
| 12 |
|
| 13 |
A test was added to this CL but removed as it was too large and took too long. It helps us keep it in the patch history for reference. |
| 14 |
|
| 15 |
Bug: https://issues.chromium.org/issues/520535595 |
| 16 |
Change-Id: Id28aeed3d67a5a5246d22681f2c7ab0e6c133558 |
| 17 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1264296 |
| 18 |
Commit-Queue: Alexis Cruz-Ayala <[email protected]> |
| 19 |
Reviewed-by: Kaylee Lubick <[email protected]> |
| 20 |
--- |
| 21 |
src/ports/SkTypeface_mac_ct.cpp | 8 ++++---- |
| 22 |
src/ports/SkTypeface_mac_ct.h | 3 ++- |
| 23 |
2 files changed, 6 insertions(+), 5 deletions(-) |
| 24 |
|
| 25 |
--- a/src/ports/SkTypeface_mac_ct.cpp |
| 26 |
+++ b/src/ports/SkTypeface_mac_ct.cpp |
| 27 |
@@ -604,9 +604,9 @@ |
| 28 |
std::unique_ptr<SkStreamAsset> SkTypeface_Mac::onOpenStream(int* ttcIndex) const { |
| 29 |
*ttcIndex = 0; |
| 30 |
|
| 31 |
- fInitStream([this]{ |
| 32 |
+ SkAutoSharedMutexExclusive sm(fStreamMutex); |
| 33 |
if (fStream) { |
| 34 |
- return; |
| 35 |
+ return fStream->duplicate(); |
| 36 |
} |
| 37 |
|
| 38 |
SK_SFNT_ULONG fontType = get_font_type_tag(fFontRef.get()); |
| 39 |
@@ -704,12 +704,12 @@ |
| 40 |
++entry; |
| 41 |
} |
| 42 |
fStream = std::make_unique<SkMemoryStream>(std::move(streamData)); |
| 43 |
- }); |
| 44 |
return fStream->duplicate(); |
| 45 |
} |
| 46 |
|
| 47 |
std::unique_ptr<SkStreamAsset> SkTypeface_Mac::onOpenExistingStream(int* ttcIndex) const { |
| 48 |
*ttcIndex = 0; |
| 49 |
+ SkAutoSharedMutexShared sm(fStreamMutex); |
| 50 |
return fStream ? fStream->duplicate() : nullptr; |
| 51 |
} |
| 52 |
|
| 53 |
@@ -1221,7 +1221,7 @@ |
| 54 |
if (!ctVariant) { |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
- |
| 58 |
+ SkAutoSharedMutexShared sm(fStreamMutex); |
| 59 |
return SkTypeface_Mac::Make(std::move(ctVariant), ctVariation.opsz, |
| 60 |
fStream ? fStream->duplicate() : nullptr); |
| 61 |
} |
| 62 |
--- a/src/ports/SkTypeface_mac_ct.h |
| 63 |
+++ b/src/ports/SkTypeface_mac_ct.h |
| 64 |
@@ -19,6 +19,7 @@ |
| 65 |
#include "include/core/SkStream.h" |
| 66 |
#include "include/core/SkTypeface.h" |
| 67 |
#include "include/private/base/SkOnce.h" |
| 68 |
+#include "src/base/SkSharedMutex.h" |
| 69 |
#include "src/utils/mac/SkUniqueCFRef.h" |
| 70 |
|
| 71 |
#ifdef SK_BUILD_FOR_MAC |
| 72 |
@@ -129,8 +130,8 @@ |
| 73 |
private: |
| 74 |
mutable std::unique_ptr<SkStreamAsset> fStream; |
| 75 |
mutable SkUniqueCFRef<CFArrayRef> fVariationAxes; |
| 76 |
+ mutable SkSharedMutex fStreamMutex; |
| 77 |
bool fIsFromStream; |
| 78 |
- mutable SkOnce fInitStream; |
| 79 |
mutable SkOnce fInitVariationAxes; |
| 80 |
|
| 81 |
using INHERITED = SkTypeface; |