Repository navigation
Fix PE timestamp for deterministic resource designer assemblies - #13144
Conversation
Co-authored-by: jonathanpeppers <[email protected]>
There was a problem hiding this comment.
🔵 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, |
There was a problem hiding this comment.
FYI for reviewers wondering why this uses 0 instead of a content-hashed timestamp:
- Roslyn /
ManagedPEBuilder/ilasmcompute a SHA-256 over the PE content (BlobContentId.FromHash), using bytes 0-15 for the MVID and bytes 16-19 (| 0x80000000) for the COFFTimeDateStamp. - ILLink / Cecil rewriters preserve the input assembly's existing
image.Timestampand only setDeterministicMvid = true. - Cecil
CreateAssembly(used here) defaultstimestamptoDateTime.UtcNowand computesDeterministicMvidby hashing the already-written PE stream (including the COFF header timestamp) without patching the header timestamp afterward. SettingTimestamp = 0ubeforeWritegives Cecil a stable stream forDeterministicMvidand is explicitly valid per ECMA-335 II.25.2.2 ("seconds since January 1st 1970 00:00:00 or 0").
|
@dalexsoto review |
dalexsoto
left a comment
There was a problem hiding this comment.
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.
|
/backport to release/11.0.1xx |
|
Started backporting to |
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]>
…#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.
Cecil’s wall-clock PE timestamp changes
_Microsoft.Android.Resource.Designer.dllbetween identical builds, even withDeterministic=true. The timestamp also changes the content-derived MVID, cascading differences into application assemblies and downstream native artifacts.WriterParameters.Timestampto zero for deterministic output; retain Cecil’s default timestamp otherwise.