-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mono ignores non-public custom attributes in dynamic assemblies #60650
Comments
Tagging subscribers to this area: Issue DetailsDescriptionWhen building a dynamic assembly via reflection with the various In runtime/src/mono/mono/metadata/custom-attrs.c Lines 738 to 741 in 6122bba
There's a comment about it matching .NET, but that is not what I see with coreclr or .NET Framework. runtime/src/mono/mono/metadata/custom-attrs.c Line 723 in 6122bba
Reproduction StepsDynamic assembly with an assembly-level non-public attribute: AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("DynamicAssembly"), AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");
// Create internal attribute
TypeBuilder typeBuilder = moduleBuilder.DefineType("InternalAttribute", TypeAttributes.NotPublic, typeof(Attribute));
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
constructorBuilder.GetILGenerator().Emit(OpCodes.Ret);
// Add assembly-level attribute
ConstructorInfo constructorInfo = typeBuilder.CreateTypeInfo().GetConstructor(Array.Empty<Type>());
CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(constructorInfo, Array.Empty<object>());
assemblyBuilder.SetCustomAttribute(customAttributeBuilder);
IList<CustomAttributeData> attrs = assemblyBuilder.GetCustomAttributesData();
Console.WriteLine($"{assemblyBuilder.GetName().Name} - Attributes (count: {attrs.Count})");
foreach (CustomAttributeData attr in attrs)
{
Console.WriteLine($" {attr}");
} Expected behaviorThe internal attribute is listed. On coreclr (and also .NET Framework 4.6.2), the above prints:
Actual behaviorThe internal attribute is not listed. On mono, the above prints:
Regression?No response Known WorkaroundsNo response ConfigurationThis results in the test infrastructure for Roslyn analyzers (in dotnet/roslyn-sdk) being unable to run on mono. They rely on emitting a non-public
Other informationNo response
|
I doubt it. That comment is from 2005 from mono/mono@6c9333e Perhaps it's matching a bug from some old version of .NET Framework. |
Targetting net7 to enable tests to run to possibly discover other issues |
@lambdageek this seems to be limited by a call to the local function runtime/src/mono/mono/metadata/custom-attrs.c Line 951 in 9f8f653
as it covers only public attribute visibility: runtime/src/mono/mono/metadata/custom-attrs.c Lines 107 to 108 in 9f8f653
do you see any major implications if we would just try to loosen the limitation? |
@ivanpovazan I think you're onto something with runtime/src/mono/mono/metadata/custom-attrs.c Line 105 in 18c6495
But note that actually the attribute is defined in the |
@lambdageek do you expect this to be fixed on the short term (or for .NET 8)? This issue also affects the aspnetcore tests, so we're looking into skipping the tests there too (dotnet/aspnetcore#48710). |
@tmds we are currently looking into this and will try to get the fix upstreamed by preview 6 snap |
Just an update: the fix will most probably not be ready for preview 6 snap, but we actively working on resolving the issue. EDIT : Aiming to resolve the issue for .NET8 |
Description
When building a dynamic assembly via reflection with the various
*Builder
classes, non-public attributes are explicitly ignored in mono.In
mono_custom_attrs_from_builders_handle
:runtime/src/mono/mono/metadata/custom-attrs.c
Lines 738 to 741 in 6122bba
There's a comment about it matching .NET, but that is not what I see with coreclr or .NET Framework.
runtime/src/mono/mono/metadata/custom-attrs.c
Line 723 in 6122bba
Not sure if there's another reason mono skips non-public attributes?
Reproduction Steps
Dynamic assembly with an assembly-level non-public attribute:
Expected behavior
The internal attribute is listed. On coreclr (and also .NET Framework 4.6.2), the above prints:
Actual behavior
The internal attribute is not listed. On mono, the above prints:
Other information
This results in the test infrastructure for Roslyn analyzers (in dotnet/roslyn-sdk) being unable to run on mono.
https://github.com/dotnet/roslyn-sdk/blob/27fe3d6060150031b532cc7255259c40a37a5fda/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerInfo.cs
They rely on emitting a non-public
IgnoresAccessChecksToAttribute
to skip access checks. Since mono ends up ignoring the attribute, using their test infrastructure results in:cc @lambdageek
The text was updated successfully, but these errors were encountered: