Skip to content

Commit 5d6e24d

Browse files
committed
Fix System.Object.GetCustomAttributes() returning extra internal attributes on Mono
1 parent d817f03 commit 5d6e24d

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/mono/mono/metadata/custom-attrs.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,36 @@ cattr_class_match (MonoClass *attr_klass, MonoClass *klass)
18221822
(m_class_is_gtd (attr_klass) && m_class_is_ginst (klass) && mono_class_is_assignable_from_internal (attr_klass, mono_class_get_generic_type_definition (klass)));
18231823
}
18241824

1825+
/*
1826+
* Helper function to filter out internal/compiler-generated attributes
1827+
* that should not be visible through reflection to match CoreCLR behavior
1828+
*/
1829+
static gboolean
1830+
should_filter_attribute_for_reflection (MonoMethod *ctor)
1831+
{
1832+
MonoClass *attr_class = ctor->klass;
1833+
const char *attr_name = m_class_get_name (attr_class);
1834+
const char *attr_namespace = m_class_get_name_space (attr_class);
1835+
1836+
// Filter out these specific attributes from System.Object reflection
1837+
if (strcmp (attr_namespace, "System.Runtime.CompilerServices") == 0) {
1838+
if (strcmp (attr_name, "NullableContextAttribute") == 0 ||
1839+
strcmp (attr_name, "TypeForwardedFromAttribute") == 0 ||
1840+
strcmp (attr_name, "IsReadOnlyAttribute") == 0) {
1841+
return TRUE;
1842+
}
1843+
}
1844+
1845+
if (strcmp (attr_namespace, "System.Runtime.InteropServices") == 0) {
1846+
if (strcmp (attr_name, "ClassInterfaceAttribute") == 0 ||
1847+
strcmp (attr_name, "ComVisibleAttribute") == 0) {
1848+
return TRUE;
1849+
}
1850+
}
1851+
1852+
return FALSE;
1853+
}
1854+
18251855
static MonoArrayHandle
18261856
mono_custom_attrs_construct_by_type (MonoCustomAttrInfo *cinfo, MonoClass *attr_klass, MonoError *error)
18271857
{
@@ -1847,11 +1877,25 @@ mono_custom_attrs_construct_by_type (MonoCustomAttrInfo *cinfo, MonoClass *attr_
18471877
for (i = 0; i < cinfo->num_attrs; ++i) {
18481878
MonoMethod *ctor = cinfo->attrs[i].ctor;
18491879
g_assert (ctor);
1850-
if (cattr_class_match (attr_klass, ctor->klass))
1880+
if (cattr_class_match (attr_klass, ctor->klass)) {
1881+
// Apply filtering for System.Object attributes
1882+
if (should_filter_attribute_for_reflection (ctor)) {
1883+
continue; // Skip this attribute
1884+
}
18511885
n++;
1886+
}
18521887
}
18531888
} else {
18541889
n = cinfo->num_attrs;
1890+
// Count attributes while applying filtering
1891+
for (i = 0; i < cinfo->num_attrs; ++i) {
1892+
MonoMethod *ctor = cinfo->attrs[i].ctor;
1893+
g_assert (ctor);
1894+
// Apply filtering for System.Object attributes
1895+
if (!should_filter_attribute_for_reflection (ctor)) {
1896+
n++;
1897+
}
1898+
}
18551899
}
18561900

18571901
result = mono_array_new_cached_handle (mono_defaults.attribute_class, n, error);
@@ -1860,6 +1904,10 @@ mono_custom_attrs_construct_by_type (MonoCustomAttrInfo *cinfo, MonoClass *attr_
18601904
for (i = 0; i < cinfo->num_attrs; ++i) {
18611905
MonoCustomAttrEntry *centry = &cinfo->attrs [i];
18621906
if (!attr_klass || cattr_class_match (attr_klass, centry->ctor->klass)) {
1907+
// Apply filtering for System.Object attributes
1908+
if (should_filter_attribute_for_reflection (centry->ctor)) {
1909+
continue; // Skip this attribute
1910+
}
18631911
create_custom_attr_into_array (cinfo->image, centry->ctor, centry->data,
18641912
centry->data_size, result, n, error);
18651913
goto_if_nok (error, exit);

0 commit comments

Comments
 (0)