Skip to content

ILVerify: verify methods that use calli, mkrefany, refanyval, refanytype or arglist instead of aborting them #136111

Open

Description

@pcshrosbree

TL;DR: ILVerify stops checking a method when it reaches any of the five instructions in the title, so later errors go unreported. I propose to implement their checks; calli would be reported as unverifiable, and checking would continue within its basic block. The prototype has about 180 lines of code and 51 tests, and gives normal verification results instead of the exception in 149 methods from published packages. I would like your view on six short questions about the rules, error codes and PR scope before I open a PR.

What happens today

ImportCalli throws NotImplementedException. ImportMkRefAny, ImportRefAnyVal, ImportRefAnyType and ImportArgList throw PlatformNotSupportedException ("TypedReference not supported in .NET Core", "RuntimeArgumentHandle not supported in .NET Core"). Verifier turns each exception into one result with no error code and no offset, and the rest of the method is not verified.

A minimal example, with a real error after the instruction:

.method public static void Target(int32 x) cil managed { ret }

.method public static void Calli_ThenUnderflow() cil managed
{
    ldc.i4.1
    ldftn void P::Target(int32)
    calli void(int32)
    pop          // stack underflow
    ret
}

.method public static void Mkrefany_ThenUnderflow() cil managed
{
    .locals init (int32 V_0)
    ldloca.s V_0
    mkrefany [System.Runtime]System.Int32
    pop
    pop          // stack underflow
    ret
}

ILVerify built from main (26da606) reports:

[IL]: Error []: [premise.dll : P::Calli_ThenUnderflow()] ImportCalli not implemented
[IL]: Error []: [premise.dll : P::Mkrefany_ThenUnderflow()] TypedReference not supported in .NET Core

The same pop with no such instruction before it is reported as StackUnderflow. An error before the instruction is still reported. Everything after it is lost, including errors in other basic blocks.

On .NET 10.0.12 and 11.0 preview 7 (linux-x64), the JIT compiles valid uses of calli, mkrefany, refanyval and refanytype. arglist could not be exercised there: the runtime refused the vararg method with "Vararg calling convention not supported.", because CoreCLR supports vararg methods only on Windows. That is a platform limit, not a .NET Core-wide one, and not a reason for the verifier to stop.

Proposed rules

All section numbers are from ECMA-335, 6th edition (June 2012). I also checked Ecma-335-Augments.md. It changes the type rules around these instructions, but not the instructions themselves.

calli (III.3.20):

  • ECMA makes calli verifiable when the tracked type of the method pointer is a method signature.
  • ILVerify tracks a function-pointer value as a plain native int (ILImporter.StackValue.cs), so I propose to report Unverifiable, as the old native verifier did, and then check the call site:
    • The operand must be a stand-alone signature token.
    • The method pointer must be a native int.
    • The arguments must be assignable to the call-site signature.
    • For an instance site without explicit, the signature gives no type for this, so the call-site check only pops it. When the target is known, the target checks described below also check its type.
    • An uninitialized this cannot be passed as an argument (III.1.8.1.4), which is stricter than ImportCall.
    • tail. is handled as for call.
    • The return type is pushed.
  • When the pointer came straight from ldftn or ldvirtftn, I also check the call site against that method (III.3.20, Correctness):
    • The calling convention must match (I.8.7.1 says "match exactly").
    • Exception: metadata gives an UnmanagedCallersOnly method a managed signature, but it is called through an unmanaged site. For such a target the prototype only checks that the site is unmanaged; it does not compare the attribute's CallConvs, because the type system code that reads them is not part of ILVerification. ECMA-335 and the augments say nothing about this case.
    • The argument count must match.
    • III.3.20 compares the target signature with the call-site signature. Read literally, I.8.7.1 would require target parameter types to be assignable to call-site parameter types. The existing delegate check uses the reverse direction. I use that check for parameter and return types, following ILVerify rather than the letter of the spec. In the delegate check, distinct value types are never assignable, so an enum and its underlying type, or int32 and uint32, do not match. That follows III.3.20's "all other items must be identical".

The prototype still leaves some blocks unchecked after a calli. ILVerify does not carry the stack into the successors of a basic block that reported any error (PropagateControlFlow), and Unverifiable counts as an error. An error after a calli in the same block is reported, but a block reached only through that block is still not verified. localloc and cpblk behave the same way today.

mkrefany (III.4.19; III.1.8.1.2.2):

  • The pointer must be a managed pointer to exactly the class (III.4.19 says "exactly that type"). Under this rule, int32& with uint32 is reported. I.8.7.2 would instead compare verification types.
  • It must not be a readonly pointer.
  • The class must not be byref-like. ECMA-335 and the augments are silent on this; the augments even allow a managed pointer to a byref-like type. The rule comes from the native verifier, which rejected mkrefany on TypedReference, ArgHandle and ArgIterator (VER_E_RA_PTR_TO_STACK); the prototype extends it to all byref-like types, as the old JIT verifier did.
  • The class must be accessible, as the JIT checks.
  • Push TypedReference.

refanyval (III.4.23): the operand must be a TypedReference; push a managed pointer to the type. It is not a permanent home, so returning it is reported.

refanytype (III.4.22): the operand must be a TypedReference; push RuntimeTypeHandle, as the JIT does.

arglist (III.3.4): valid only in a vararg method; push RuntimeArgumentHandle. ILVerify checks the spec, so the non-Windows limitation is not reported.

The prototype adds five error codes, appended at the end of VerifierError and named after the native verifier's messages where one existed:

  • ExpectedSignatureToken
  • CalliSignatureMismatch
  • Arglist
  • ReadOnlyInMkrefany
  • MkrefanyByRefLike

Vararg call sites that pass extra arguments, so that the signature has a sentinel, still fail in the shared type-system signature parser. That also affects call. The prototype does not address this parser limitation, and none of the measurements below exercises vararg call sites.

Measured impact

The shipped .NET 10.0.12 shared framework (172 assemblies, 130,146 method bodies) has 55 methods with calli and 3 with refanytype. It has none with mkrefany, refanyval or arglist.

Today With the prototype
Methods aborted by these instructions 31 (28 calli, 3 refanytype) 0
Results changed in any other method 0 of 130,115
New findings in the 31 methods, besides Unverifiable 37: InitLocals 33, UnmanagedPointer 2, StackUnexpected 2

All 37 new findings come after the calli. The same methods already report those kinds of errors earlier, mostly InitLocals in code compiled with SkipLocalsInit. The new calli signature check fired nowhere in the framework.

On real C# output (SDK 10.0.401), function-pointer calls now report only Unverifiable at the calli. Methods using __makeref, __refvalue, __reftype and __arglist verify cleanly.

I also compared both builds, against .NET 10.0.12, on two larger sets of method bodies. Across both, no method without one of the five instructions changed result, and none of the five new codes fired.

F# and rewritten IL (834,164 bodies).

The 811,964 F# bodies come from FSharp.Core 4.5.4 to 11.0.101-rc2, eight F# libraries with their dependencies, the F# compiler's own assemblies from SDKs 6.0 to 11.0 RC1, and a probe built by 11 SDKs. None contains any of the five instructions.

The 104,868 tail. prefixes in these F# bodies, all on call or callvirt, give the same results as before, so moving the tail. checks into the shared helper changed no result in this corpus.

A five-method C# probe, rewritten by eight Fody weavers, by coverlet and by a static IL instrumenter, gives the same verification results as before rewriting: only Unverifiable at the calli, wherever the rewrite moved it.

Published packages (388,443 bodies).

These are hand-written IL packages, ten high-download packages found to use InlineIL.Fody, 300 of the most-downloaded packages on nuget.org, and Castle DynamicProxy, Moq and NSubstitute proxies saved to disk. 942 methods contain one of the five: 940 calli, one mkrefany and one refanyval. In 793 of them both builds report the same earlier error before the instruction, so these results do not show the effect of the new checks.

The other 149 (147 calli, the mkrefany and the refanyval) are aborted today; the prototype checks those instructions and gives normal verification results. They come from InlineIL.Fody users and hand-written IL such as DotNext, ImpromptuNinjas.ZStd, MonoMod.ILHelpers and Datadog.Trace, and from Npgsql. Besides Unverifiable, they gained six findings, all at or after the instruction.

In ImpromptuNinjas.ZStd 1.4.5.5's ZStdDCtx::Copy, two arguments are pushed for a calli whose signature takes three, and the prototype reports a stack underflow that the base build could not see. I have not checked what the JIT does with it.

In those 300 packages, 774 methods contain calli, but ILVerify reaches the calli in only 3 of them. None contains the other four instructions.

Roslyn's tests depend on today's behaviour:

  • Four tests quote the abort messages: ExtensionTests.AddressOf_TypeReceiver, LocalStateTracingTests.RefArguments_FunctionPointerInvocation, FunctionPointerTests.FunctionPointerInference_ExactInferenceThroughArray_RefKindMatch (in a comment), and CodeGenTests.Issue51228 (both TypedReference messages).
  • The two helpers in CodeGenFunctionPointersTests skip verification at more than 100 call sites. That file does not say why, so I do not attribute all of those to calli.

Prototype

  • Code: ILImporter.Verify.cs, VerifierError.cs and Strings.resx, 181 lines added and 31 removed. Most of the removal is the tail. return checks, moved into a helper that call and calli share.
  • Tests: two IL test files, each with an accompanying project file; 32 calli tests and 19 tests for the other four instructions, with one rule per test where possible. ILVerification.Tests goes from 513 to 564 tests, all passing.
  • I ran 43 mutations, each disabling or inverting one new check. Every mutant built and failed at least one test, and the source was restored byte-identically after each.

Questions

  1. Should calli be reported as unverifiable with its call site still checked, as proposed? Or should ILVerify follow ECMA and track function-pointer types on the stack, so that a calli through a known signature can verify?
  2. Is checking the call site against an ldftn target (CalliSignatureMismatch) wanted? The C# compiler spills the pointer to a local before pushing arguments, so it mainly catches hand-written or rewritten IL.
  3. For an UnmanagedCallersOnly target, is "any unmanaged site" acceptable, or should the attribute's CallConvs be compared, as I.8.7.1's exact match would require?
  4. Should mkrefany keep the native verifier's rejection of byref-like classes? Should its pointer-type check require an exact type match or compare verification types?
  5. Are the five proposed codes, one per new message, and their names right: ExpectedSignatureToken, CalliSignatureMismatch, Arglist, ReadOnlyInMkrefany, MkrefanyByRefLike? I can reuse existing codes instead if you prefer.
  6. One PR, or two (calli, then the TypedReference family with arglist)?

Note

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

Activity

  1. added
    untriaged
    on Oct 19, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 19, 2026
  3. dotnet-policy-service commented on Oct 19, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. added
    area-Tools-ILVerification
    on Oct 20, 2026
  5. dotnet-policy-service commented on Oct 20, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  6. dotnet-policy-service commented on Oct 20, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/jit-contrib
    See info in area-owners.md if you want to be subscribed.

  7. jkotas commented on Oct 20, 2026

    @jkotas
    Member

    calli

    It is impossible to make calli actually verifiable since the runtime does not track function pointer lifetimes. The function pointer can point to a code that was unloaded. calli would fall under #37391 . Errors like mismatched number of arguments can be still reported, even in validation-only mode.

    arglist

    The shared type system supports varargs. Varargs support in modern .NET is limited to Windows and just to what's needed for managed C++. I do not think we want to spend any time on making varargs better.

  8. removed
    area-CodeGen-coreclr
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILVerificationuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions