Repository navigation
Use blittable Byte for C bool and Int32 for hb_bool_t delegate callbacks - #5479
Conversation
|
📖 Documentation Preview Add the Removing the label removes the preview. While the label is present, each new commit updates it. This comment is automatically updated by the documentation staging workflow. |
📦 Try the packages from this PRWarning Do not run these scripts without first reviewing the code in this PR. Step 1 — Download the packages bash / macOS / Linux: curl -fsSL https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.sh | bash -s -- 5479PowerShell / Windows: iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 5479"Step 2 — Add the local NuGet source dotnet nuget add source ~/.skiasharp/hives/pr-5479/packages --name skiasharp-pr-5479More options
Or download manually from Azure Pipelines — look for the Remove the source when you're done: dotnet nuget remove source skiasharp-pr-5479 |
📊 SkiaSharp benchmarks — PR #5479⭐ this PR (full source build) vs 🌙 nightly · Linux · Windows · macOS
Highlights⏱️ Time — 🔴 19 slower · 🟢 65 faster
📦 Allocations — 🔴 1 more alloc · 🟢 0 less alloc
Full per-OS benchmark deltasLinux⏱️ Time (vs 🌙 nightly
📦 Allocations (vs 🌙 nightly
Windows⏱️ Time (vs 🌙 nightly
macOS⏱️ Time (vs 🌙 nightly
|
|
@dalexsoto review |
There was a problem hiding this comment.
🟡 Changes recommended
The tests do not cover recovery after callback exceptions, leaving error-state resets on successful Rewind/Seek/Move and preservation on failed repositioning unverified.
1 open issue
What changed in this PR
This PR hardens SkiaSharp and HarfBuzzSharp native callbacks by replacing callback bool values with blittable bytes, containing managed stream exceptions, and simplifying callback state storage.
Changes:
- Updates the generator, generated callback signatures, and proxy implementations to use
byte. - Adds stream exception containment and error-state tracking, with regression tests for stream failures and false-returning HarfBuzz callbacks.
- Replaces closure-based callback state with direct user-data handles and a shared state object.
| File | Description |
|---|---|
| utils/SkiaSharpGenerator/Generate/Generator.cs | Removes callback boolean marshalling attributes. |
| utils/SkiaSharpGenerator/BaseTool.cs | Maps callback booleans to bytes. |
| tests/Tests/SkiaSharp/SKManagedWStreamTest.cs | Tests throwing write-stream callbacks. |
| tests/Tests/SkiaSharp/SKManagedStreamTest.cs | Tests callback failures and disposed-stream termination. |
| tests/Tests/HarfBuzzSharp/HBUnicodeFuncsTest.cs | Tests false-returning Unicode callbacks. |
| tests/Tests/HarfBuzzSharp/HBFontFuncsTest.cs | Tests false-returning font callbacks. |
| binding/SkiaSharp/SKAbstractManagedStream.cs | Adds callback error-state storage. |
| binding/SkiaSharp/Generated/SKManagedWStreamWriteProxyDelegate.generated.cs | Changes write delegate return to byte. |
| binding/SkiaSharp/Generated/SKManagedWStreamDelegates.generated.cs | Updates write function-pointer signature. |
| binding/SkiaSharp/Generated/SKManagedStreamSeekProxyDelegate.generated.cs | Changes seek delegate return to byte. |
| binding/SkiaSharp/Generated/SKManagedStreamRewindProxyDelegate.generated.cs | Changes rewind delegate return to byte. |
| binding/SkiaSharp/Generated/SKManagedStreamMoveProxyDelegate.generated.cs | Changes move delegate return to byte. |
| binding/SkiaSharp/Generated/SKManagedStreamIsAtEndProxyDelegate.generated.cs | Changes EOF delegate return to byte. |
| binding/SkiaSharp/Generated/SKManagedStreamHasPositionProxyDelegate.generated.cs | Changes position-capability return to byte. |
| binding/SkiaSharp/Generated/SKManagedStreamHasLengthProxyDelegate.generated.cs | Changes length-capability return to byte. |
| binding/SkiaSharp/Generated/SKManagedStreamDelegates.generated.cs | Updates stream function-pointer signatures. |
| binding/SkiaSharp/Generated/SKGraphiteImageProviderProxyDelegate.generated.cs | Changes mipmapping parameter to byte. |
| binding/SkiaSharp/Generated/DelegateProxies_SKManagedWStreamWriteProxyDelegate.generated.cs | Updates write proxy declarations. |
| binding/SkiaSharp/Generated/DelegateProxies_SKManagedStreamSeekProxyDelegate.generated.cs | Updates seek proxy declarations. |
| binding/SkiaSharp/Generated/DelegateProxies_SKManagedStreamRewindProxyDelegate.generated.cs | Updates rewind proxy declarations. |
| binding/SkiaSharp/Generated/DelegateProxies_SKManagedStreamMoveProxyDelegate.generated.cs | Updates move proxy declarations. |
| binding/SkiaSharp/Generated/DelegateProxies_SKManagedStreamIsAtEndProxyDelegate.generated.cs | Updates EOF proxy declarations. |
| binding/SkiaSharp/Generated/DelegateProxies_SKManagedStreamHasPositionProxyDelegate.generated.cs | Updates position-capability proxy declarations. |
| binding/SkiaSharp/Generated/DelegateProxies_SKManagedStreamHasLengthProxyDelegate.generated.cs | Updates length-capability proxy declarations. |
| binding/SkiaSharp/Generated/DelegateProxies_SKGraphiteImageProviderProxyDelegate.generated.cs | Updates image-provider proxy declarations. |
| binding/SkiaSharp/DelegateProxies.wstream.cs | Contains write-stream callback exceptions. |
| binding/SkiaSharp/DelegateProxies.stream.cs | Contains exceptions and tracks stream error recovery. |
| binding/SkiaSharp/DelegateProxies.cs | Converts mipmapping byte to managed boolean. |
| binding/HarfBuzzSharp/Generated/UnicodeDecomposeProxyDelegate.generated.cs | Changes decomposition return to byte. |
| binding/HarfBuzzSharp/Generated/UnicodeComposeProxyDelegate.generated.cs | Changes composition return to byte. |
| binding/HarfBuzzSharp/Generated/FontGetVariationGlyphProxyDelegate.generated.cs | Changes variation-glyph return to byte. |
| binding/HarfBuzzSharp/Generated/FontGetNominalGlyphProxyDelegate.generated.cs | Changes nominal-glyph return to byte. |
| binding/HarfBuzzSharp/Generated/FontGetGlyphProxyDelegate.generated.cs | Changes glyph lookup return to byte. |
| binding/HarfBuzzSharp/Generated/FontGetGlyphOriginProxyDelegate.generated.cs | Changes glyph-origin return to byte. |
| binding/HarfBuzzSharp/Generated/FontGetGlyphNameProxyDelegate.generated.cs | Changes glyph-name return to byte. |
| binding/HarfBuzzSharp/Generated/FontGetGlyphFromNameProxyDelegate.generated.cs | Changes name lookup return to byte. |
| binding/HarfBuzzSharp/Generated/FontGetGlyphExtentsProxyDelegate.generated.cs | Changes glyph-extents return to byte. |
| binding/HarfBuzzSharp/Generated/FontGetGlyphContourPointProxyDelegate.generated.cs | Changes contour-point return to byte. |
| binding/HarfBuzzSharp/Generated/FontGetFontExtentsProxyDelegate.generated.cs | Changes font-extents return to byte. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_UnicodeDecomposeProxyDelegate.generated.cs | Updates decomposition proxy declarations. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_UnicodeComposeProxyDelegate.generated.cs | Updates composition proxy declarations. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_FontGetVariationGlyphProxyDelegate.generated.cs | Updates variation-glyph proxy declarations. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_FontGetNominalGlyphProxyDelegate.generated.cs | Updates nominal-glyph proxy declarations. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_FontGetGlyphOriginProxyDelegate.generated.cs | Updates glyph-origin proxy declarations. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_FontGetGlyphNameProxyDelegate.generated.cs | Updates glyph-name proxy declarations. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_FontGetGlyphFromNameProxyDelegate.generated.cs | Updates name lookup proxy declarations. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_FontGetGlyphExtentsProxyDelegate.generated.cs | Updates glyph-extents proxy declarations. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_FontGetGlyphContourPointProxyDelegate.generated.cs | Updates contour-point proxy declarations. |
| binding/HarfBuzzSharp/Generated/DelegateProxies_FontGetFontExtentsProxyDelegate.generated.cs | Updates font-extents proxy declarations. |
| binding/HarfBuzzSharp/Generated/BufferMessageProxyDelegate.generated.cs | Changes buffer-message return to byte. |
| binding/HarfBuzzSharp/DelegateProxies.unicode.cs | Converts Unicode callback results to bytes. |
| binding/HarfBuzzSharp/DelegateProxies.font.cs | Converts font results and short-circuits failed name lookups. |
| binding/Binding.Shared/DelegateProxies.shared.cs | Replaces closures with direct callback state storage. |
Review effort: Balanced
baee8fe to
146011d
Compare
dalexsoto
left a comment
There was a problem hiding this comment.
Reviewed all 53 changed generator, generated-binding, callback, user-data and test files with their native ABI, stream error-state and ownership consumers, including a separate final blocker search. The callback representations, stream error containment and recovery, and handle ownership match the reviewed native and generated contracts. The intended unmanaged-callback exception containment and resolved recovery-test concerns are not being re-blocked; no author-reported runtime or performance validation is claimed. No independent high-confidence blocker remains in this bounded static review; no executed runtime or whole-pipeline certification is claimed.
8800cc0 to
97fe612
Compare
dalexsoto
left a comment
There was a problem hiding this comment.
Reviewed all 53 changed generator, generated-binding, callback, user-data and test files with their native ABI, stream error-state and ownership consumers, including a separate final blocker search.
Release a partially created fork before converting its exception to null (binding/SkiaSharp/DelegateProxies.stream.cs:24-25)
The new catch makes a failed native Fork return null, but SKManagedStream.OnFork can already have allocated its native duplicate before the exception. After one successful Duplicate has cached snapshotData, disposing the underlying MemoryStream and invoking Fork on the still-live SKManagedStream enters the cache fast path, allocates a new SkMemoryStream, and then throws while reading stream.Position. The catch discards that duplicate pointer. Each failed Fork leaks the native stream and its SKData reference; disposing the original wrapper cannot release the retained snapshot.
Fix: Read the underlying position before allocating the duplicate, or destroy the just-created native stream in SKManagedStream.OnFork on every exception before rethrowing to this containment proxy. Add coverage with an already-cached snapshot and a subsequently failing Position getter.
The intended unmanaged-callback exception containment and resolved recovery-test concerns are not being re-blocked; no author-reported runtime or performance validation is claimed.
This is a static source-contract finding, not an executed runtime reproduction.
Fixed in |
51417bc to
c92a9c8
Compare
There was a problem hiding this comment.
🟡 Changes recommended
A throwing Stream.Dispose() can leave the wrapper holding a freed native handle and stale dictionary entry, while Fork() can swallow a position error and return an incorrectly positioned copy.
2 open issues
1 resolved since last review
Review effort: Max
- Update SkiaSharpGenerator to emit blittable Byte for C bool and Int32 for HarfBuzz hb_bool_t (and omit [MarshalAs(UnmanagedType.I1)]) on generated delegate types, unmanaged function pointers, and DelegateProxies implementations, avoiding non-blittable bool return ABI issues under [UnmanagedCallersOnly] and Mono's ARM64 interpreter (dotnet/runtime#136578). - Update mechanical boolean conversions in SkiaSharp and HarfBuzzSharp DelegateProxies implementations (including early-returning 0 on failed FontGetGlyphName lookups before copying the name span) and add false-returning HarfBuzz callback tests. Co-authored-by: Copilot App <[email protected]>
533564d to
13a7906
Compare
|
@mattleibow I cannot complete this review request in this iteration: the PR was restacked and its review scope changed while the prior exact-revision review was in progress. That result cannot serve as a review of the new revision. I am withholding a formal decision until the complete current scope and moved integration paths have been rechecked; this request remains tracked. |
|
@dalexsoto review |
dalexsoto
left a comment
There was a problem hiding this comment.
Reviewed all 49 changed files in the ABI-only layer, including the generated bindings and the relevant native callback signatures. Skia Boolean and HarfBuzz hb_bool_t representations and calling conventions agree across the marshalled and direct unmanaged callback paths; no high-confidence blocker remains in this scope.
Broader stream exception containment, recovery, fork and native-close cleanup intentionally belong to the upper #5464 layer and are not certified by this ABI review. This is a bounded static-source review, not an executed runtime, produced-output or whole-pipeline certification.



Description
Match each unmanaged callback's actual native Boolean representation: one-byte
Bytefor Skia C/C++bool, four-byteInt32for HarfBuzzhb_bool_t. Generated callback delegates, unmanaged function pointers and their implementations use the same blittable ABI types. Public managed Boolean delegate/API types remain unchanged. This is 49 changed files, including 40 generated files.What was wrong before
The pre-change generated HarfBuzz callback declaration already had an attribute; this was not a missing-attribute problem:
But HarfBuzz 14.4.0's
src/hb-common.hdeclares:I1means a signed one-byte native value. It does not mean “use a four-byte int and then convert to bool.” In the traditional delegate-marshalling path, a managedboolnormally marshals as a four-byte WindowsBOOL;I1explicitly requests one byte instead. That was the wrong native width for HarfBuzz'sinttypedef.boolbool+MarshalAs(I1)requested the correct widthbool; the attribute cannot repair this no-marshalling signatureByte/bytehb_bool_t(typedef int)bool+MarshalAs(I1)requested the wrong, one-byte widthboolalso did not match the native four-byteintInt32/int[UnmanagedCallersOnly]anddelegate* unmanaged[Cdecl]do not perform Boolean marshalling. Putting[MarshalAs]on the method does not insert a conversion stub.boolwith four-byteMarshalAs(UnmanagedType.Bool)can represent a native four-byte Boolean on the traditional marshalled path, but it does not solve the direct unmanaged callback path. Explicit blittable integers give both paths the correct native representation.The Mono ARM64 interpreter investigation exposed a distinct runtime problem with raw managed
bool: theMONO_TYPE_BOOLEANreturn-copy path used a one-bytestrbstore, leaving upper bits of return-register storage unchanged. Apple ARM64 C++ callers can test the wholew0register, so a managed false could be observed as true. See dotnet/runtime#136578. Native declarations still need to be matched independently of that bug: usingbytefor HarfBuzz merely because register extension can make it appear to work would remain an ABI mismatch.The old declarations could appear correct for common
0/1results when a compiler/runtime happened to extend return registers appropriately. That incidental behavior did not make the HarfBuzz signature correct.Why this is correct
GetManagedFunctionArgumentsandGetFunctionPointerTypeinBaseTool.cs, together withGenerator.cs, map native C/C++booltoByteand the configured HarfBuzzhb_bool_t("Boolean"mapping) toInt32. Generator changes and regenerated declarations stay together.[UnmanagedFunctionPointer]delegates and modern[UnmanagedCallersOnly]/unmanaged function pointers use these same blittable types.MarshalAs(I1)is removed from the integer callback signatures because no Boolean marshalling is required.0or1using the correct return type; native Boolean inputs are converted with!= 0. Public managed callers still usebool.mipmappedinput, eight HarfBuzz font callbacks and two Unicode callbacks. False-returning font/Unicode tests check the results.FontGetGlyphNameProxyImplementationguard returns0when the user's delegate returns false, before copying its output name. This is an explicit callback-correctness change, not hidden as a mechanical type edit.Shared user-data/closure allocation work is separately reviewable in #5518. Stream callback exception containment, EOF error state, fork/native-destruction cleanup and their tests are preserved in #5464, not dropped.
Related issues
Related to #3377 and dotnet/runtime#136578.
Native stack 5513: #5475 (merged) → #5479 → #5483 → #5464 → #5476 → #5518. The allocation-only last layer is not a prerequisite for the earlier fixes.
Required skia PR
None.
Areas affected
binding/)externals/skia/src/c,include/c)Changes
No public managed signatures or native C/C++ declarations change. Internal callback signatures now match those existing native declarations.
Behavior
Bytefor Skia nativebooland four-byteInt32for HarfBuzz nativehb_bool_t, avoiding the former HarfBuzz width mismatch and unsupported reliance on Boolean marshalling at direct unmanaged entry points.FontGetGlyphNameProxyImplementationreturns failure without copying the output name when the managed delegate returns false.Testing
Existing validation evidence for ABI-only commit
13a79064199:pwsh -NoLogo -NoProfile -File ./utils/generate.ps1— passed, regeneration produced 0 diff.dotnet build binding/SkiaSharp/SkiaSharp.csproj— passed, all configured target frameworks, 0 errors.dotnet build binding/HarfBuzzSharp/HarfBuzzSharp.csproj— passed, all configured target frameworks, 0 errors.dotnet test tests/SkiaSharp.Tests.Console.slnx -p:TargetFramework=net10.0 -p:TargetFrameworks=net10.0— passed on macOS ARM64 using source-matched Skia748bc0c0116cnative binaries: core 6,475; gallery 50; singleton 1; Direct3D 2; Vulkan 4 passed; 0 failures, existing skips unchanged.HBFontFuncsTest.FalseReturningDelegatesShouldReturnFalseandHBUnicodeFuncsTest.FalseReturningComposeAndDecomposeDelegatesShouldReturnFalse.The native
hb_bool_ttypedef and pre-change generatedbool/I1declarations were checked directly for this explanation. Traditional SkiaI1marshalling is not being described as a width bug. The historical ARM64 interpreter failure is distinct from the native-width mismatch; the local macOS suite is not a claim that every runtime/architecture was executed again. This description update changes no code.Checklist
Changesabove lists all public API and behavioral changes (or "None.")///documentation and regenerated bindings when applicablemono/skiaPR linked above and bindings regenerated