Skip to content

Add ILVerify option to validate all metadata references - #132362

Open
pkuyo wants to merge 4 commits into
dotnet:mainfrom
pkuyo:feat-ilverify-96458-validate-metadata
Open

Add ILVerify option to validate all metadata references#132362
pkuyo wants to merge 4 commits into
dotnet:mainfrom
pkuyo:feat-ilverify-96458-validate-metadata

Conversation

@pkuyo

@pkuyo pkuyo commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #96458

Adds an opt-in --verify-all-dependencies option to ILVerify.

With this option enabled, ILVerify performs an additional pass over supported metadata references, resolving each one independently so that all failures are reported. (The pass skips P/Invoke ModuleRefs, since they name native libraries rather than managed netmodules.)

Diagnostics use the most specific available error identifier and include metadata tokens when --tokens is specified.
For example, running with both options produces output like this:

[MD]: Error [FileLoadErrorGeneric]: [X.dll][token 0x23000002] Unable to resolve metadata reference (AssemblyReference): Failed to load assembly 'ILVerifyAssemblyThatDoesNotExist'
[MD]: Error [TokenResolve]: [X.dll][token 0x1A000001] Unable to resolve metadata reference (ModuleReference): Assembly or module not found: ILVerifyManagedModuleThatDoesNotExist.netmodule
[MD]: Error [FileLoadErrorGeneric]: [X.dll][token 0x27000001] Unable to resolve metadata reference (ExportedType): Failed to load assembly 'ILVerifyAssemblyThatDoesNotExist'

Tests cover all supported reference kinds, including unused invalid references and valid assembly, type, and managed netmodule references.

@github-actions github-actions Bot added the area-Tools-ILVerification Issues related to ilverify tool and IL verification in general label Aug 15, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 15, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread src/coreclr/tools/ILVerify/Program.cs Outdated
VerifyTypes(peReader, module, path, ref numErrors, ref verifiedTypeCounter, ref typeCounter);

if (Get(_command.VerifyAllDependencies))
VerifyMetadataReferences(peReader, path, ref numErrors);

@jkotas jkotas Aug 16, 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.

The feature request was to validate references and nothing else.

It is fine and desirable to validate all metadata references by default.

Comment thread src/coreclr/tools/ILVerify/Program.cs Outdated
VerifyTypes(peReader, module, path, ref numErrors, ref verifiedTypeCounter, ref typeCounter);

if (Get(_command.VerifyAllDependencies))
VerifyMetadataReferences(peReader, path, ref numErrors);

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.

Many of the missing metadata references are going to be duplicate in VerifyMethods and VerifyTypes. Would it make sense to do the metadata reference validation first, and then omit the duplicate errors?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just realized the approach in 12d8343 isn't quite right — suppressing metadata resolution errors from VerifyMethods / VerifyTypes can hide errors that aren't actually duplicates. I'll rework this to deduplicate the errors instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated this in 0e8249b

public Option<bool> Tokens { get; } =
new("--tokens", "-t") { Description = "Include metadata tokens in error messages" };
public Option<bool> MetadataOnly { get; } =
new("--metadata-only") { Description = "Only validate metadata references" };

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.

Suggested change
new("--metadata-only") { Description = "Only validate metadata references" };
new("--metadata-references-only") { Description = "Only validate metadata references" };

Or just --references-only

Comment thread src/coreclr/tools/ILVerify/Program.cs Outdated
else if (metadataOnly)
WriteLine("All metadata references in " + path + " resolved.");
else
WriteLine("All Classes and Methods in " + path + " Verified.");

@jkotas jkotas Aug 16, 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.

Suggested change
WriteLine("All Classes and Methods in " + path + " Verified.");
WriteLine("All types and methods in " + path + " verified.");

(Nit) pre-existing

Comment thread src/coreclr/tools/ILVerify/Program.cs Outdated
{
WriteLine($"Types found: {typeCounter}");
WriteLine($"Types verified: {verifiedTypeCounter}");
WriteLine($"Metadata errors: {metadataErrorCounter}");

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.

I do not think printing this number is useful. We do not have to print any detailed statics at all when we are validating references only.

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

Labels

area-Tools-ILVerification Issues related to ilverify tool and IL verification in general community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ILVerify] Add option for validation of all dependencies

2 participants