Skip to content

[bgen] Remove support for ZeroCopyStrings#25515

Open
rolfbjarne wants to merge 4 commits into
mainfrom
dev/rolf/no-zero-copy-strings
Open

[bgen] Remove support for ZeroCopyStrings#25515
rolfbjarne wants to merge 4 commits into
mainfrom
dev/rolf/no-zero-copy-strings

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

@rolfbjarne rolfbjarne commented May 25, 2026

Remove the dead zero-copy string marshaling code from the binding generator.

This feature was never fully working, because it never really worked well (it ran into a number of bugs in other places, causing crashes because APIs would retain or copy NSStrings when they shouldn't and this optimization would run head-first into those).

Note that this option never did anything in .NET, it was always forcefully disabled if someone tried to enable it.

Changes

  • Remove ZeroCopyStrings field, type_wants_zero_copy, ZeroCopyStringMarshal, CollectFastStringMarshalParameters, and all zero-copy code paths from the generator
  • Simplify GenerateMarshalString/GenerateDisposeString (always use the copy path)
  • Keep ZeroCopyStringsAttribute and DisableZeroCopyAttribute as no-op stubs wrapped in #if !XAMCORE_5_0 with [Obsolete] for source compatibility
  • Guard --use-zero-copy CLI option with #if !XAMCORE_5_0 (emits warning BI1027)
  • Mark ObjCBindings.Property.DisableZeroCopy enum value as [Obsolete]
  • Remove [DisableZeroCopy] usages from binding sources (glkit, corebluetooth)
  • Update documentation to mark both attributes as obsolete

🤖 Pull request created by Copilot

rolfbjarne and others added 2 commits May 25, 2026 10:50
The zero-copy string marshaling feature was never fully implemented
(it always emitted a warning and disabled itself). Remove the dead
code paths and simplify the string marshaling logic.

- Remove ZeroCopyStrings field and type_wants_zero_copy from Generator
- Remove ZeroCopyStringMarshal from MarshalInfo
- Remove CollectFastStringMarshalParameters and the fixed char* block
- Simplify GenerateMarshalString/GenerateDisposeString (always copies)
- Keep ZeroCopyStringsAttribute and DisableZeroCopyAttribute as no-op
  stubs for source compatibility
- Update --use-zero-copy to emit a warning instead of setting a flag
- Update documentation to mark both attributes as obsolete

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…utes

- Add [Obsolete] and #if !XAMCORE_5_0 to DisableZeroCopyAttribute and
  ZeroCopyStringsAttribute so they're removed in the next breaking change
- Guard --use-zero-copy option with #if !XAMCORE_5_0
- Mark ObjCBindings.Property.DisableZeroCopy enum value as [Obsolete]
- Remove [DisableZeroCopy] usages from binding sources (glkit, corebluetooth)
  since the attribute is now a no-op

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

Add #pragma warning disable CS0618 to suppress obsolete warnings in
rgen source and test files that still reference the now-obsolete
ObjCBindings.Property.DisableZeroCopy enum value.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

The cecil ObsoleteTest requires newly obsoleted APIs to also have
[EditorBrowsable(EditorBrowsableState.Never)] to hide them from
IntelliSense.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne marked this pull request as ready for review May 25, 2026 16:23
@rolfbjarne rolfbjarne requested a review from dalexsoto as a code owner May 25, 2026 16:23
Copilot AI review requested due to automatic review settings May 25, 2026 16:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes the unused/defunct “zero-copy” string marshaling feature from bgen (the binding generator), while keeping ZeroCopyStringsAttribute / DisableZeroCopyAttribute as obsolete no-op stubs for source compatibility and updating related warnings/docs.

Changes:

  • Remove all zero-copy string marshaling state and code paths from bgen (including CLI/config plumbing).
  • Mark DisableZeroCopy flags/attributes as obsolete/no-op and adjust rgen/analyzer/tests to tolerate the obsolete symbol.
  • Update BI1027 text and binding documentation to reflect that --use-zero-copy is ignored.

Reviewed changes

Copilot reviewed 17 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/rgen/Microsoft.Macios.Bindings.Analyzer.Tests/Validators/PropertyOrFieldValidatorTests.cs Disable obsolete-warning noise in tests referencing DisableZeroCopy.
tests/rgen/Microsoft.Macios.Bindings.Analyzer.Tests/Validators/FieldValidatorTests.cs Disable obsolete-warning noise in tests referencing DisableZeroCopy.
tests/rgen/Microsoft.Macios.Bindings.Analyzer.Tests/Validators/ArrayValidatorTests.cs Disable obsolete-warning noise in tests referencing DisableZeroCopy.
src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs Update attribute documentation to state DisableZeroCopy is obsolete/no-op.
src/rgen/Microsoft.Macios.Generator/DataModel/Property.Generator.cs Keep DisableZeroCopy flag readable while suppressing CS0618.
src/rgen/Microsoft.Macios.Bindings.Analyzer/Validators/FieldValidator.cs Suppress CS0618 where the obsolete flag is enumerated as “ignored”.
src/Resources.resx Update BI1027 warning text for --use-zero-copy.
src/Resources.Designer.cs Regenerate BI1027 designer comment to match updated resource.
src/ObjCBindings/ExportTag.cs Mark ObjCBindings.Property.DisableZeroCopy as obsolete/no-op and hide from IntelliSense.
src/glkit.cs Remove [DisableZeroCopy] usage from bindings.
src/corebluetooth.cs Remove [DisableZeroCopy] usage from bindings.
src/bgen/Models/MarshalInfo.cs Remove ZeroCopyStringMarshal tracking and related attribute/flag checks.
src/bgen/Models/BindingTouchConfig.cs Remove UseZeroCopy configuration knob.
src/bgen/Generator.cs Remove zero-copy marshaling generation and simplify to always use the copy path.
src/bgen/BindingTouch.cs Keep parsing --use-zero-copy (when enabled) but only emit BI1027 and ignore it.
src/bgen/Attributes.cs Keep DisableZeroCopyAttribute / ZeroCopyStringsAttribute as obsolete stubs (conditional).
docs/website/generator-errors.md Update BI1027 documentation text (currently slightly inconsistent with resources).
docs/website/binding_types_reference_guide.md Mark the two attributes as obsolete/no-op in the binding docs.
Files not reviewed (1)
  • src/Resources.Designer.cs: Language not supported

### <a name='BI1026'/>BI1026: `*`: Enums attributed with [\*] must have an underlying type of `long` or `ulong`

### <a name='BI1027'/>BI1027: Support for ZeroCopy strings is not implemented. Strings will be marshalled as NSStrings.
### <a name='BI1027'/>BI1027: Support for ZeroCopy strings is not implemented. The --use-zero-copy option is not supported and will be ignored.
Comment thread src/bgen/Generator.cs
Comment on lines 3226 to +3233
// @probe_null: determines whether null is allowed, and
// whether we need to generate code to handle this
//
// @must_copy: determines whether to create a new NSString, necessary
// for NSString properties that are flagged with "retain" instead of "copy"
//
// @prefix: prefix to prepend on each line
//
// @property: the name of the property
//
public string GenerateMarshalString (bool probe_null, bool must_copy)
{
if (must_copy) {
return "var ns{0} = CFString.CreateNative ({1});\n";
}
return
"ObjCRuntime.NSStringStruct _s{0}; Console.WriteLine (\"" + CurrentMethod + ": Marshalling: {{1}}\", {1}); \n" +
"_s{0}.ClassPtr = ObjCRuntime.NSStringStruct.ReferencePtr;\n" +
"_s{0}.Flags = 0x010007d1; // RefCount=1, Unicode, InlineContents = 0, DontFreeContents\n" +
"_s{0}.UnicodePtr = _p{0};\n" +
"_s{0}.Length = " + (probe_null ? "{1} is null ? 0 : {1}.Length;" : "{1}.Length;\n");
}

public string GenerateDisposeString (bool probe_null, bool must_copy)
public string GenerateMarshalString (bool probe_null)
{
if (must_copy) {
return "CFString.ReleaseNative (ns{0});\n";
} else
return "if (_s{0}.Flags != 0x010007d1) throw new Exception (\"String was retained, not copied\");";
return "var ns{0} = CFString.CreateNative ({1});\n";
@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #c15dad3] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: c15dad3e5aa2d759c34cffbeb54698bf2e57fe17 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #c15dad3] Build passed (Build packages) ✅

Pipeline on Agent
Hash: c15dad3e5aa2d759c34cffbeb54698bf2e57fe17 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: c15dad3e5aa2d759c34cffbeb54698bf2e57fe17 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #c15dad3] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: c15dad3e5aa2d759c34cffbeb54698bf2e57fe17 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🔥 [CI Build #c15dad3] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 22 tests failed, 161 tests passed.

Failures

❌ fsharp tests

2 tests failed, 2 tests passed.

Failed tests

  • fsharp/iOS - simulator/Debug: LaunchTimedOut
  • fsharp/tvOS - simulator/Debug: LaunchTimedOut

Html Report (VSDrops) Download

❌ interdependent-binding-projects tests

2 tests failed, 2 tests passed.

Failed tests

  • interdependent-binding-projects/iOS - simulator/Debug: LaunchTimedOut
  • interdependent-binding-projects/tvOS - simulator/Debug: LaunchTimedOut

Html Report (VSDrops) Download

❌ introspection tests

4 tests failed, 2 tests passed.

Failed tests

  • introspection/iOS - simulator/Debug: LaunchTimedOut
  • introspection/iOS - simulator/Debug (ARM64): LaunchTimedOut
  • introspection/tvOS - simulator/Debug: LaunchTimedOut
  • introspection/tvOS - simulator/Debug (ARM64): LaunchTimedOut

Html Report (VSDrops) Download

❌ linker tests (tvOS)

11 tests failed, 0 tests passed.

Failed tests

  • dont link/tvOS - simulator/Debug: LaunchTimedOut
  • dont link/tvOS - simulator/Release: LaunchTimedOut
  • link sdk/tvOS - simulator/Debug: LaunchTimedOut
  • link sdk/tvOS - simulator/Release: LaunchTimedOut
  • link all/tvOS - simulator/Debug: LaunchTimedOut
  • link all/tvOS - simulator/Release: LaunchTimedOut
  • link all/tvOS - simulator/Debug (don't bundle original resources): LaunchTimedOut
  • trimmode copy/tvOS - simulator/Debug: LaunchTimedOut
  • trimmode copy/tvOS - simulator/Release: LaunchTimedOut
  • trimmode link/tvOS - simulator/Debug: LaunchTimedOut
  • trimmode link/tvOS - simulator/Release: LaunchTimedOut

Html Report (VSDrops) Download

❌ windows tests

1 tests failed, 2 tests passed.

Failed tests

  • Remote .NET tests/Xamarin.Tests.DotNetProjectTest.BuildProjectsWithExtensionsOnRemoteWindows(iOS,"ios-arm64",False): Failed: 'dotnet build' timed out after 00:10:00

  • Remote .NET tests/Xamarin.Tests.PackTest.BindingXcFrameworksProjectOnRemoteWindowsUsingFallback(iOS,True,False,True): Failed: 'dotnet pack' timed out after 00:10:00

Html Report (VSDrops) Download

❌ xcframework tests

2 tests failed, 2 tests passed.

Failed tests

  • xcframework-test/iOS - simulator/Debug: LaunchTimedOut
  • xcframework-test/tvOS - simulator/Debug: LaunchTimedOut

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. (⚠️ Html Report Publish failed ⚠️) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 11 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 18 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. (⚠️ Html Report Publish failed ⚠️) Download
✅ sharpie: All 1 tests passed. (⚠️ Html Report Publish failed ⚠️) Download
✅ xtro: All 1 tests passed. (⚠️ Html Report Publish failed ⚠️) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: c15dad3e5aa2d759c34cffbeb54698bf2e57fe17 [PR build]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants