Parse custom attribute value blobs for dependencies and rewrite type name strings#127596
Merged
Conversation
…ame strings Implement custom attribute blob parsing in CustomAttributeNode: 1. GetStaticDependencies now decodes the custom attribute value blob and reports dependencies for typeof() arguments, enum types, property setters, and fields referenced in the blob. 2. WriteInternal now re-encodes the blob with type name strings resolved to their definitions, handling cases where trimming drops type forwarders (e.g. re-encoding "System.Object, System.Runtime" to "System.Object, System.Private.CoreLib"). Remove 24 tests from ILTrimExpectedFailures.txt that now pass. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/f4173cc7-fd45-4a62-9a1c-1a31c06cc85b Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
… remove unused parameter - Catch TypeSystemException and BadImageFormatException instead of base Exception - Add DependencyNode type alias to reduce verbose fully-qualified type names - Remove unused isFixedArg parameter from WriteArgumentValue - Hoist constructor resolution out of named argument loop - Fix duplicate CustomAttributeTypeNameFormatter allocations Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/f4173cc7-fd45-4a62-9a1c-1a31c06cc85b Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
MichalStrehovsky
April 30, 2026 05:21
View session
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/illink |
… BlobBuilder Use BlobEncoder.CustomAttributeSignature with FixedArgumentsEncoder and CustomAttributeNamedArgumentsEncoder instead of manual byte writing. Use writeContext.GetSharedBlobBuilder() following established patterns. Removed byte[] materialization - writes directly to shared BlobBuilder. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d078b767-9d76-41e5-9168-8f6651786108 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
…bute blobs Remove CheckArgumentsForRewrite/CheckArgumentValueForRewrite that skipped rewriting when no typeof() references were found. Always rewrite to exercise the encoding logic and surface bugs. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/46e2e54d-5313-4538-8274-c5cfe241e547 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
…pe.Category - Replace early returns with cascading if/else if/else in EncodeNamedArgumentType, WriteLiteralValue, and WriteScalarValue. - Switch on type.Category instead of type.UnderlyingType.Category in EncodeElementType, handling IsEnum and System.Type in the default case. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/683a21bb-4e07-4219-81e8-b0a320967bd3 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/6129cc2c-9d66-4f42-876a-056fcef7f711 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/20d7918c-7976-4a3d-882f-8aac8f4ade8b Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/f536fc71-012c-4ae8-b43e-0abb314fd4c0 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/e2e5e7e2-e344-4277-b320-fb781514a11c Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/47730547-daf1-4d8d-aa61-66dda42e8d3e Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/6f52143b-ce97-4011-8d73-52d18ba9700c Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
MichalStrehovsky
approved these changes
May 11, 2026
…ewrite Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5aad31ca-3463-4605-90a7-54c2be91deb6 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Contributor
Author
|
@MichalStrehovsky I've opened a new pull request, #128122, to work on those changes. Once the pull request is ready, I'll request review from you. |
sbomer
approved these changes
May 15, 2026
Member
sbomer
left a comment
There was a problem hiding this comment.
I wonder whether some of the is checks throughout this code should fail fast instead? Seems like hard casts could catch future holes if we don't expect these checks to fail. Otherwise LGTM!
… hard casts instead of is-checks Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/bba68476-9f61-4c82-9f8b-156ddb87c9ba Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/bba68476-9f61-4c82-9f8b-156ddb87c9ba Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
CustomAttributeNodehad two TODOs: it wasn't parsing the custom attribute value blob for dependencies, and it wasn't rewriting type name strings that could become stale after trimming removes type forwarders.Dependency analysis (
GetStaticDependencies)Decodes the custom attribute blob via
CustomAttributeTypeProviderand reports dependencies for:typeof()argumentsConstructor resolution uses
TryGetMethodinstead of try/catch, with a null constructor triggering an early return before named argument processing.GetDependenciesFromPropertySetterusesGetTypeDefinition()to correctly resolve property setters on generic attribute types. Dependency decode failures set an instance_isCorruptedflag so rewrite can avoid re-processing known-bad blobs.Blob rewriting (
WriteInternal)RewriteCustomAttributeBlobreads the custom attribute blob manually withBlobReader, writes intowriteContext.GetSharedBlobBuilder(), and rewrites only serialized type-name strings where needed (System.Typepayloads and enum type names). Type-name resolution usesGetTypeByCustomAttributeTypeName(..., throwIfNotFound: false)and rewrites only when resolution succeeds, preserving unresolved serialized type names as-is.Rewrite early-outs to copying the original blob when
_isCorruptedis set or when the constructor cannot be resolved.Type safety improvements
NodeFactory.ReflectedType,ReflectedMethod, andReflectedFieldnow returnDependencyNodedirectly instead ofobject, since all return paths produce aDependencyNode(either a real definition node orNullDependencyNode.Instance). This removes the need foris DependencyNodepattern checks at all call sites. Similarly, thevalue is ImmutableArray<CustomAttributeTypedArgument<TypeDesc>>pattern check inGetDependenciesFromCustomAttributeArgumenthas been replaced with a hard cast, relying on theSystem.Reflection.Metadatacontract thatDecodeValuealways producesImmutableArray<...>forSzArray-typed arguments (thevalue is nullcase is already handled earlier).Validation
dotnet build src/coreclr/tools/ILTrim.Core/ILTrim.Core.csprojdotnet test src/coreclr/tools/ILTrim.Tests/ILTrim.Tests.csproj --filter "FullyQualifiedName~AttributesTests"parallel_validation(Code Review + CodeQL)