-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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] Do not ignore non-public custom attributes in dynamic assemblies #87406
Conversation
@lambdageek looking at the test failures with coreCLR, it seems like explicitly setting an internal attribute defined in one dynamic assembly on another dynamic assembly: https://github.com/dotnet/runtime/pull/87406/files#diff-fd74227f1b0f0310d5980748bee0599f868713d789a4ddf80c9cc9eb7f86c2b3R313 works - as if custom attribute visibility is not taken into account. |
I'm not sure. @steveharter @buyaa-n Do you know if the CoreCLR behavior here is intentional or a bug: When there are two dynamic assemblies and one of them defines a non- |
From the test failures in Core, GetCustomAttributeData() returns non-empty, but a bit of additional testing locally shows GetCustomAttributes() returns empty. A visibility check is done by calling IsCAVisibleFromDecoratedType. |
public override object[] GetCustomAttributes(bool inherit) | ||
{ | ||
return GetCustomAttributes(null!, inherit); // FIXME: coreclr doesn't allow null attributeType | ||
} | ||
|
||
public override object[] GetCustomAttributes(Type attributeType, bool inherit) | ||
{ | ||
if (cattrs == null || cattrs.Length == 0) | ||
return Array.Empty<object>(); | ||
|
||
if (attributeType is TypeBuilder) | ||
throw new InvalidOperationException(SR.InvalidOperation_CannotHaveFirstArgumentAsTypeBuilder); | ||
public override object[] GetCustomAttributes(bool inherit) => CustomAttribute.GetCustomAttributes(this, inherit); | ||
|
||
List<object> results = new List<object>(); | ||
for (int i = 0; i < cattrs.Length; i++) | ||
{ | ||
Type t = cattrs[i].Ctor.GetType(); | ||
|
||
if (t is TypeBuilder) | ||
throw new InvalidOperationException(SR.InvalidOperation_CannotConstructCustomAttributeForTypeBuilderType); | ||
|
||
if (attributeType == null || attributeType.IsAssignableFrom(t)) | ||
results.Add(cattrs[i].Invoke()); | ||
} | ||
|
||
return results.ToArray(); | ||
} | ||
public override object[] GetCustomAttributes(Type attributeType, bool inherit) => | ||
CustomAttribute.GetCustomAttributes(this, attributeType, inherit); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes are aligning how GetCustomAttributes
is implemented for RuntimeAssemblyBuilder.Mono
and RuntimeModuleBuilder.Mono
by now sharing the same logic.
This is required as by adding the test cases:
- https://github.com/dotnet/runtime/pull/87406/files#diff-666a23992bc09b4499c853602019f69bdad7f3dc4cf247a32a5e40a2d76616d1R77
- https://github.com/dotnet/runtime/pull/87406/files#diff-666a23992bc09b4499c853602019f69bdad7f3dc4cf247a32a5e40a2d76616d1R110
a System.NullReferenceException
bug was discovered in the old implementation.
The problem was coming from the fact that when invoking SetCustomAttributes
on the RuntimeModuleBuilder.Mono
:
runtime/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.Mono.cs
Line 429 in 1600677
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute) |
The code inits the custom attribute builders without initializing their
namedFields
and namedProperties
fields, this later fails when calling GetCustomAttributes
on a module builder and invoking custom attribute builder via: https://github.com/dotnet/runtime/pull/87406/files#diff-ccf091eb73b1e9e47985dd5763e288a55cdb682be2ae8a1f394c004d37e7bd17L879 as the Invoke expects that those fields are adequately set:runtime/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.Mono.cs
Lines 71 to 75 in 1600677
for (int i = 0; i < namedFields.Length; i++) | |
namedFields[i].SetValue(result, fieldValues[i]); | |
for (int i = 0; i < namedProperties.Length; i++) | |
namedProperties[i].SetValue(result, propertyValues[i]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks Ivan!
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
Failures seem unrelated. |
This PR:
Fixes #60650