Skip to content

Fix PE timestamp for deterministic resource designer assemblies - #13144

Merged
simonrozsival merged 2 commits into
mainfrom
copilot/fix-microsoft-android-designer-issue
Oct 15, 2026
Merged

simonrozsival merged 2 commits into
mainfrom
copilot/fix-microsoft-android-designer-issue

Conversation

Copilot AI commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Cecil’s wall-clock PE timestamp changes _Microsoft.Android.Resource.Designer.dll between identical builds, even with Deterministic=true. The timestamp also changes the content-derived MVID, cascading differences into application assemblies and downstream native artifacts.

  • Generation: Set WriterParameters.Timestamp to zero for deterministic output; retain Cecil’s default timestamp otherwise.
    DeterministicMvid = Deterministic,
    Timestamp = Deterministic ? 0u : null,
  • Regression coverage: Cover application and library designers in both modes, checking deterministic byte equality and matching MVIDs, plus preservation of non-deterministic timestamp and MVID behavior.

Copilot AI changed the title [WIP] Fix reproducibility issue in Microsoft.Android.Resource.Designer.dll Fix PE timestamp for deterministic resource designer assemblies Oct 14, 2026
Copilot AI requested a review from jonathanpeppers October 14, 2026 18:31
@jonathanpeppers
jonathanpeppers marked this pull request as ready for review October 14, 2026 18:39
Copilot AI balanced review requested due to automatic review settings October 14, 2026 18: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.

🔵 Needs a closer look

No blocking defects were identified, but successful build and regression-test validation remains unconfirmed.

0 open findings

What changed in this PR

Fixes #13140 by making resource designer assemblies reproducible when deterministic builds are enabled.

Changes:

  • Sets the deterministic PE timestamp to zero while preserving Cecil’s default otherwise.
  • Adds application and library regression tests for timestamps, byte equality, and module identifiers.
File Description
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​Tasks/​AndroidResourceTests.cs Tests designer output in both determinism modes.
src/​Xamarin.Android.Build.Tasks/​Tasks/​GenerateResourceDesignerAssembly.cs Fixes the timestamp for deterministic output.

🧠 Review effort: Balanced

StrongNameAssembly (assembly.Name);
var wp = new WriterParameters () {
DeterministicMvid = Deterministic,
Timestamp = Deterministic ? 0u : null,

@jonathanpeppers jonathanpeppers Oct 14, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FYI for reviewers wondering why this uses 0 instead of a content-hashed timestamp:

  • Roslyn / ManagedPEBuilder / ilasm compute a SHA-256 over the PE content (BlobContentId.FromHash), using bytes 0-15 for the MVID and bytes 16-19 (| 0x80000000) for the COFF TimeDateStamp.
  • ILLink / Cecil rewriters preserve the input assembly's existing image.Timestamp and only set DeterministicMvid = true.
  • Cecil CreateAssembly (used here) defaults timestamp to DateTime.UtcNow and computes DeterministicMvid by hashing the already-written PE stream (including the COFF header timestamp) without patching the header timestamp afterward. Setting Timestamp = 0u before Write gives Cecil a stable stream for DeterministicMvid and is explicitly valid per ECMA-335 II.25.2.2 ("seconds since January 1st 1970 00:00:00 or 0").

@jonathanpeppers

Copy link
Copy Markdown
Member

@dalexsoto review

@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Oct 14, 2026

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed the complete two-file change at 54800097907e06e612212d9d410755712258775d, including a separate integration pass against actual main. With the pinned Cecil implementation, an explicit zero timestamp removes the time-dependent input before deterministic MVID hashing; null preserves the existing non-deterministic timestamp and MVID behavior. The accepted zero-timestamp design is compatible with ECMA-335.

The four application/library and determinism-mode regression cases force regeneration and check the actual designer output. The exact-zero assertion detects the old defect even within one clock second. Relevant producer, compiler, publish, rewrite and signing contracts remain consistent; no high-confidence blocker remains.

This was a read-only source review. Repository tests were not executed, and exact-head CI was still pending with no observed failures at the closing check; this is not a merge-readiness assertion.

@simonrozsival
simonrozsival merged commit b98cccd into main Oct 15, 2026
42 checks passed
@simonrozsival
simonrozsival deleted the copilot/fix-microsoft-android-designer-issue branch October 15, 2026 06:34
@jonathanpeppers

Copy link
Copy Markdown
Member

/backport to release/11.0.1xx

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0.1xx (link to workflow run)

jonathanpeppers added a commit that referenced this pull request Oct 21, 2026
Port the linker-cache fix from #13158 to main.

When Android build properties change, `build.props` changes, but `_RunILLink` does not track it. On the release branch, switching from CoreCLR to Mono can therefore reuse incompatible linked assemblies and fail Mono AOT with `Runtime critical type System.Diagnostics.MonoStackFrame not found`.

Add the Android-owned `_AndroidInvalidateLinkerCache` target from commit 3dd8d4e. Before linking, it deletes each RID's semaphore if `build.props` is newer. Unchanged builds still skip linking. No changes to `MSBuildAllProjects`. Main already has the runtime cache entries and the timestamp fix from #13144.

Extend `PostTrimmingPipelineIsSkippedOnSecondBuild` in `IncrementalBuildTest.cs` to check cached property changes and subsequent unchanged builds for arm64 and x64. No new test file or test helpers.

Co-authored-by: Copilot App <[email protected]>
simonrozsival added a commit that referenced this pull request Nov 11, 2026
…#13150)

Context: [#13110](#13110) preserves the resource designer's historical public-key identity without signing the generated assembly.  [#13144](#13144) fixes deterministic PE timestamps.  Both changes are already merged; preserve their behavior when replacing the emitter.

Generating a resource designer with Mono.Cecil constructs an assembly object graph and resolves framework assemblies even though the output has a fixed, small metadata contract.  For applications with large resource sets, this adds allocation and execution cost to resource generation.

Replace `Xamarin.Android.Tasks.GenerateResourceDesignerAssembly` with a .NET-only task in `Microsoft.Android.Build.Tasks` that writes metadata, IL, and the PE image directly with `System.Reflection.Metadata`.  Use it for both normal and design-time builds, without an opt-in property or Cecil fallback.  The emitter does not need Cecil or framework assembly resolution.

### Preserve the designer contract

Keep resource parsing and case mapping, application properties and constants, library reference-assembly metadata, array initialization, and copy-if-changed behavior.  Preserve assembly version `1.0.0.0` and public-key token `1e9360d6629057ee` so existing binary libraries continue to compile against the generated designer.  Read the public key from the existing embedded `Resource.Designer.snk`; do not sign the generated assembly or move the key file.

Generate `TargetFrameworkAttribute` from the project's `$(TargetFrameworkIdentifier)` and `$(TargetFrameworkVersion)`, and scope framework type references to the matching `System.Runtime` version instead of retaining the old `netstandard2.1` baseline.  Remove the R.txt-only timestamp shortcut so retargeting the same output cannot retain stale framework metadata.  Deterministic copy-if-changed still preserves unchanged output timestamps.

Retain deterministic MVIDs and zero PE timestamps for deterministic builds, and fresh MVIDs and current PE timestamps otherwise.  Consolidate all four application/library and determinism cases in the modern task test suite, retaining the coverage introduced by #13144.

Source-link the shared resource parsing utilities and consolidate line-oriented map parsing in `MonoAndroidHelper.Basic.cs`, keeping that shared file free of MSBuild dependencies.  Resolve R.txt, map, and output paths through the task's `TaskEnvironment`; use the project-scoped copy/retry overload rather than cached process-wide environment settings.  Use `CultureInfo.InvariantCulture` for resource IDs, styleable indices, map keys, and parser diagnostics.  Culture tests exercise `ar-SA` and `tr-TR` with a custom negative sign; remove the artificial negative-base exception-message assertion.

### Register the .NET task for both MSBuild hosts

Follow the existing conditional `<UsingTask/>` pattern in both designer targets: declare `Runtime="NET"` for both hosts, select `TaskFactory="TaskHostFactory"` only when `$(MSBuildRuntimeType)` is `Full`, and leave .NET MSBuild's `Core` registration in-process.  Runtime selection belongs on the registration so Framework MSBuild does not try to discover the .NET-only task in-process before selecting its host.  Remove the redundant invocation-level `MSBuildRuntime="NET"` override.

Derive the task from `MultithreadedAndroidTask` and declare `[MSBuildMultiThreadableTask]` directly on it.  Cover separate projects with relative paths and different resource mappings, including ordinary and strict multithreaded execution through both shipped registration surfaces.

### Recorded performance comparison

A captured MAUI `R.txt` with 6,817 lines was measured on an Apple M5 Max running macOS 26.7.1 and .NET 11 RC2 Arm64, using Release builds and BenchmarkDotNet 0.15.8 in-process with three warmups and eight measured iterations.  These measurements were collected while both emitters and the temporary benchmark harness were present; they predate the subsequent framework-reference and task-environment changes.

| Fresh generation | Cecil mean ± SD | Metadata mean ± SD | Speedup | Managed allocation reduction |
|---|---:|---:|---:|---:|
| Library | 20.817 ± 0.328 ms | 8.115 ± 0.182 ms | 2.56× | 57% |
| Application | 25.492 ± 0.689 ms | 8.639 ± 0.091 ms | 2.95× | 60% |

These are warm-process task timings, not end-to-end build speedups.  Before removing the baseline, benchmark setup compared metadata contracts, public-key identity, normalized getter IL, and application property/field values.  The development-only benchmark project and harness are not part of this PR.
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 15, 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

5 participants