Skip to content

Fix typemap proxy metadata field ownership - #12497

Merged
jonathanpeppers merged 4 commits into
mainfrom
simonrozsival/fix-trimmable-typemap-retention
Aug 25, 2026
Merged

jonathanpeppers merged 4 commits into
mainfrom
simonrozsival/fix-trimmable-typemap-retention

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

Summary

  • emit deduplicated UTF-8 registration fields before proxy types
  • ensure every RVA field is owned by a dedicated __utf8_* helper type
  • prevent metadata field ownership from creating accidental ILLink dependencies between otherwise unrelated proxies
  • add a metadata regression covering field ownership and string deduplication

Root cause

UTF-8 RVA fields and proxy TypeDef rows were emitted interleaved. ECMA-335 represents field ownership using contiguous TypeDef.FieldList ranges, so fields emitted after another proxy type could become owned by that proxy even though they were intended for <PrivateImplementationDetails>.

When a live proxy referenced a shared registration field, ILLink correctly marked the field's accidental declaring proxy, its constructor, and its managed listener implementor. This caused unrelated framework implementors to survive trimming.

The emitter now prepares all registration strings and emits their fields contiguously on dedicated __utf8_* helper types before emitting any proxy type. Typemap attributes, conditionality, associations, proxy APIs, and UTF-8 string deduplication remain unchanged.

Testing

  • Microsoft.Android.Sdk.TrimmableTypeMap.Tests: 800 passed

Make ordinary Java peers conditional on managed reachability while preserving explicit external Java roots. Emit UTF-8 registration fields before proxy types so metadata field ownership cannot create accidental proxy-to-proxy linker dependencies.

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

Copilot-Session: a6de09d8-fc09-4560-a976-ffcccd21ba70
Copilot AI lite review requested due to automatic review settings August 24, 2026 20:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 fixes a trimmable typemap codegen/rooting issue that could accidentally retain proxy types and their managed implementors during trimming, and expands rooting to correctly preserve external Java roots (including XML-only custom views).

Changes:

  • Updates scanner/model rules so ordinary ACW peers are conditional on managed reachability unless explicitly rooted (attributes, manifests, custom views).
  • Fixes typemap assembly emission by preparing and emitting deduplicated UTF-8 RVA fields on dedicated __utf8_* helper types before proxy TypeDef rows are emitted.
  • Adds/updates test coverage, including new build-level CoreCLR trimming tests for used/unused binding listener implementors and external Java roots.
Show a summary per file
File Description
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Scanner/JavaPeerScannerTests.cs Updates scanner assertions around unconditional rooting rules.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/TypeMapModelBuilderTests.cs Adjusts model expectations for conditional vs unconditional entries.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/TypeMapAssemblyGeneratorTests.cs Adds validation that RVA fields are declared on __utf8_* helper types.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/TrimmableTypeMapGeneratorTests.cs Adds unit coverage for rooting only custom-view referenced managed types.
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/TrimmableTypeMapBuildTests.cs Adds end-to-end CoreCLR trimming tests and helpers to verify retention/trim behavior.
src/Xamarin.Android.Build.Tasks/Tasks/GenerateTrimmableTypeMap.cs Plumbs custom view map inputs into typemap generation.
src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.targets Adds custom view map to incremental inputs and passes it to the generation task.
src/Microsoft.Android.Sdk.TrimmableTypeMap/TrimmableTypeMapGenerator.cs Adds custom-view rooting step prior to manifest-based rooting.
src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerScanner.cs Refines unconditional rooting logic (framework vs non-framework + JNI signature roots).
src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/TypeMapAssemblyEmitter.cs Prepares UTF-8 fields up-front instead of interleaving with proxy emission.
src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/PEAssemblyBuilder.cs Implements grouped preparation for contiguous UTF-8 RVA field emission.
src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/ModelBuilder.cs Removes unconditional default for user ACW types so reachability governs unless explicitly rooted.

Review details

  • Files reviewed: 12/12 changed files
  • Comments generated: 2
  • Review effort level: Lite

Move the independent conditional ACW and external Java root changes to a separate pull request.

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

Copilot-Session: a6de09d8-fc09-4560-a976-ffcccd21ba70
@simonrozsival simonrozsival changed the title Fix accidental retention in trimmable typemaps Fix typemap proxy metadata field ownership Aug 24, 2026
Reuse the encoded size collected while grouping registration strings when allocating their RVA data.

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

Copilot-Session: a6de09d8-fc09-4560-a976-ffcccd21ba70
@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@jonathanpeppers

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #12497

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ LGTM

Findings: 0 errors · 0 warnings · 1 suggestion

The eager, size-grouped emission preserves contiguous ECMA-335 field ownership before proxy TypeDef rows are created, while retaining UTF-8 deduplication. The regression assertion directly verifies that RVA fields resolve to the intended __utf8_* declaring types. All 44 CI checks passed.

Generated by Android PR Reviewer for #12497 · gpt56 · 186 AIC · ⌖ 19 AIC · ⊞ 25.7K
Comment /review to run again

Rename the lookup API now that UTF-8 fields must be prepared before proxy emission.

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

Copilot-Session: a6de09d8-fc09-4560-a976-ffcccd21ba70
@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Aug 25, 2026
@jonathanpeppers
jonathanpeppers merged commit 34ab0fd into main Aug 25, 2026
44 checks passed
@jonathanpeppers
jonathanpeppers deleted the simonrozsival/fix-trimmable-typemap-retention branch August 25, 2026 17:09
jonathanpeppers pushed a commit that referenced this pull request Sep 1, 2026
## Summary

- make ordinary Java peers conditional on managed reachability instead of preserving every ACW
- preserve explicit external Java roots from component and JNI-provider attributes
- preserve peers referenced by the manifest and custom views instantiated from Android XML resources
- add standalone and trimmed CoreCLR build coverage for used/unused binding listener implementors and XML-only custom views

## Why custom views are part of this change

Removing the blanket unconditional ACW rule allows ILLink to trim ordinary Java peers. Types instantiated externally by Android still need explicit roots even when managed code does not reference them. Custom views used only in layout XML are one such root, so the custom-view map is consumed by the typemap generator before trimming.

## Dependency

Stacked on #12497 so the trimmed integration coverage runs with correct UTF-8 metadata field ownership. The diff in this PR contains only conditionality and external-root behavior.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants