Skip to content

Conversation

@medhatiwari
Copy link
Contributor

[Mono] Fix ParameterInfo.Name to return null for unnamed parameters

Summary

Fixes runtime inconsistency where Mono's ParameterInfo.Name returns empty string ("") for unnamed parameters while CoreCLR correctly returns null.

Problem

Failing Tests:

  • OpenApiOperationGeneratorTests.ThrowsInvalidOperationExceptionGivenUnnamedParameter
  • RequestDelegateFactoryTests.CreateThrowsInvalidOperationExceptionGivenUnnamedArgument

Root Cause:

// CoreCLR behavior (correct)
var param = lambda.Compile().Method.GetParameters()[0];
param.Name == null  // true

// Mono behavior (bug)
param.Name == ""    //  empty string, not null

ASP.NET Core validation checks if (parameter.Name is null) - this works on CoreCLR but fails on Mono.

Reproduction

using System;
using System.Linq.Expressions;

var unnamed = Expression.Parameter(typeof(int));
var lambda = Expression.Lambda(Expression.Block(), unnamed);
var param = lambda.Compile().Method.GetParameters()[0];

// CoreCLR: param.Name is null 
// Mono: param.Name is "" 
Console.WriteLine($"Name is null: {param.Name is null}");

Test Results:

  • With only loader.c fix: param.Name still returns ""
  • With both fixes: param.Name correctly returns null

Why

ParameterInfo.Name is nullable (string?) per API Contract)

Related Issues

Fixes failures in dotnet/aspnetcore test suite when running on Mono runtime.

cc: @giritrivedi

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Oct 27, 2025
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 27, 2025
@huoyaoyuan huoyaoyuan added area-VM-reflection-mono Reflection issues specific to MonoVM and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Oct 27, 2025
@steveisok steveisok requested a review from BrzVlad October 27, 2025 11:08

for (i = 0; i < signature->param_count; ++i)
names [i] = "";
names [i] = NULL;
Copy link
Member

Choose a reason for hiding this comment

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

mono_method_get_param_names_internal is called by a couple of other methods. We need to make sure that each of them expects and handles NULL value. This seems to produce quite a few crashes on CI, primarily around aot compilation, but it looks like debugger could also suffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BrzVlad Thank you for catching this! I've audited all callers of mono_method_get_param_names_internal and found 2 unsafe callers
debugger-agent.c:9327 - Added NULL check: names[i] ? names[i] : ""
dwarfwriter.c:1868 - Added NULL check: !pname || pname[0] == '\0'

and other callers (mini-llvm.c (lines 4168, 13298) - checks if (names[i]), debug-mini.c:680 - uses ternary names[i] ? names[i] : "unknown name"

already loosk safe
Please let me know if I'm missing something here or if there are other callers I should check.

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

Labels

area-VM-reflection-mono Reflection issues specific to MonoVM 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.

3 participants