Skip to content

Document ILVerify.Verifier thread safety, PEReader lifetime and the rest of the public API - #135989

Merged
jkotas merged 8 commits into
dotnet:mainfrom
pcshrosbree:docs/ilverify-verifier-contract
Oct 19, 2026
Merged

jkotas merged 8 commits into
dotnet:mainfrom
pcshrosbree:docs/ilverify-verifier-contract

Conversation

@pcshrosbree

@pcshrosbree pcshrosbree commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

I add XML doc comments to ILVerify.Verifier and IResolver describing their existing behavior and requirements: whether a Verifier can be used from several threads, when verification runs, how long its PEReaders must stay usable, and why the resolver must return the same PEReader instance each time it is asked for the same assembly or module. The IResolver summaries now say "must" rather than "should". Behavior does not change. I also add XML doc comments to the rest of the public API declared in src/coreclr/tools/ILVerification: VerificationResult, ErrorArgument, VerifierOptions, the VerifierError enum, and the Verifier constructors and methods. Following review, the VerifierError values other than None have no summaries of their own, because each would repeat the message the library reports for it. The package README describes what the package is for and shows a short example; following review, I removed its detailed section on thread safety, reader lifetime and resolver requirements. ILVerification/README.md now points to that example and to the XML doc comments in the source, and says that the shared type system compiled into the library is not part of its API, apart from ExceptionStringID.

What the implementation does:

  • ILVerifyTypeSystemContext caches one EcmaModule per PEReader in a Dictionary (ILVerifyTypeSystemContext.cs:24). It adds entries without a lock and never removes them (:101-112).
  • The public Verify methods return sequences produced by iterators (Verifier.cs:145, :214, :267), so verification and the resolver calls it needs run during enumeration. SetSystemModuleName resolves the system module immediately (Verifier.cs:84-92).
  • Each EcmaModule keeps its PEReader and a MetadataReader obtained from it (EcmaModule.cs:189-221). System.Reflection.Metadata requires the PEReader to stay undisposed for the lifetime of that MetadataReader (PEReaderExtensions.cs:73). Nothing in ILVerification disposes a PEReader.
  • Modules are keyed by PEReader instance, so a resolver that returns a second instance for an assembly creates a second module with distinct types. This also applies when the assembly being verified is resolved by name. For example, System.Private.CoreLib is resolved by name when it is both the system module and the assembly being verified.

Validation

I ran these probes against ILVerification built from main at 11ce222 (linux-x64, Release), verifying System.Collections.Specialized, System.Text.Json and System.Private.CoreLib from Microsoft.NETCore.App 10.0.12. For each method, I compared the results' diagnostic codes, exception IDs and IL offsets.

Probe Result
8 threads share one Verifier, each call with its own PEReader; 10 runs of 2400 calls every run threw on 2 to 506 of the calls: InvalidOperationException ("Operations that change non-concurrent collections must have exclusive access…") or ArgumentException ("An item with the same key has already been added")
the same calls on 1 thread no exceptions
600 calls that do not overlap, each on a new thread same results as single-threaded verification; no exceptions
a resolver that returns a new PEReader on every call, against one that caches them 41 of 200 methods in System.Collections.Specialized and 147 of 300 in System.Text.Json get different results, for example an added StackUnexpected
verify a rebuilt System.Private.CoreLib, with CoreLib as the system module I obtained each method body's reference result by verifying it in its own rebuild, with the resolver returning that rebuild. When the resolver returned the on-disk CoreLib, 72 of 300 methods differed from their reference results. When it returned the instance passed to Verify, 1000 of 1000 methods matched their reference results.
Verify(peReader, method) the call itself makes no resolver call; they happen when the result is enumerated
dispose a PEReader whose module the verifier has cached, then verify more of its methods array-backed reader: ObjectDisposedException; file-backed reader: AccessViolationException, and the process aborts

ILVerification.Tests, Release, on this branch: 501/501.

Note

This PR was prepared with AI assistance (Anthropic Claude and OpenAI Codex), under my direction. I reviewed it before posting.

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

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.

2 participants