Skip to content
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] Mono support for InlineArrayAttribute #83776

Merged
merged 18 commits into from
Mar 29, 2023

Conversation

kotlarmilos
Copy link
Member

This PR Implements support for InlineArrayAtribute as outlined in #61135 and tracked in #80798.

It implements structure layout, updates GC class bitmap to consider the struct field as an array, disallows marshalling structs with the attribute in JS Interop generator and adds a corresponding test.

@kotlarmilos kotlarmilos self-assigned this Mar 22, 2023
@kotlarmilos kotlarmilos added this to the 8.0.0 milestone Mar 22, 2023
Copy link
Member

@lambdageek lambdageek left a comment

Choose a reason for hiding this comment

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

Most of the structure is looking pretty reasonable

src/mono/mono/metadata/class-init.c Outdated Show resolved Hide resolved
src/mono/mono/metadata/class-init.c Outdated Show resolved Hide resolved
@kotlarmilos kotlarmilos marked this pull request as ready for review March 24, 2023 09:35
@kotlarmilos
Copy link
Member Author

Inline array attribute tests have passed and the failure shouldn't be related.

@SamMonoRT
Copy link
Member

/cc @mangod9

@mangod9
Copy link
Member

mangod9 commented Mar 27, 2023

@VSadov too.

Copy link
Member

@lambdageek lambdageek left a comment

Choose a reason for hiding this comment

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

LGTM.

The most important things to address:

  1. we should make sure that [InlineArray(-1)] fails for the right reasons (because we correctly detect and disallow negative values) not because it wraps around to a huge unsigned value.
  2. the callback that extracts the value shoudl be named more specifically for InlineArrayAttribute - I don't think it's a good idea to accidentally reuse it for another attribute that might be defined with more constructors, arguments or properties.

src/mono/mono/metadata/class-init.c Outdated Show resolved Hide resolved
size *= m_class_inlinearray_value (klass);
inlined_fields++;
if(size <= 0 || size > struct_max_size) {
mono_class_set_type_load_failure (klass, "Inline array struct size out of bounds.");
Copy link
Member

Choose a reason for hiding this comment

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

Is this an error that we share with coreclr? Should we mention the maximum size?

Copy link
Member

Choose a reason for hiding this comment

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

Should we mention the maximum size?

I am not sure the limit is very informative to the user.

The size is computed from element size and will be impacted by aligning and padding. The limit was rather arbitrarily chosen as there would be some limit anyways.
1 MiB seems to be enough for most uses. It can be increased if there is a need for more, but likely this error will be seen only in erroneous cases.

We basically want to say "Hey, the array is abnormally large. Is everything alright with the code?"

Copy link
Member Author

Choose a reason for hiding this comment

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

It might be valuable for users to provide more information on the error, either maximum size or indication that the struct is abnormally large. Would it be wrong to mention maximum size as it could be impacted by aligning and padding?

src/mono/mono/metadata/class-private-definition.h Outdated Show resolved Hide resolved
src/mono/mono/metadata/class-init.c Outdated Show resolved Hide resolved
src/mono/mono/metadata/custom-attrs-internals.h Outdated Show resolved Hide resolved
@@ -931,6 +932,15 @@ compute_class_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int
g_error ("compute_class_bitmap: Invalid type %x for field %s:%s\n", type->type, mono_type_get_full_name (m_field_get_parent (field)), field->name);
break;
}

// If a struct has inline array attribute, consider it as an array
Copy link
Member

@VSadov VSadov Mar 28, 2023

Choose a reason for hiding this comment

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

I am not familiar with how GC/pointer maps work in Mono. The rest of the change looks good to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

A struct with the inline array attribute is handled in the same way as an array.

@kotlarmilos
Copy link
Member Author

Failures shouldn't be related.

@kotlarmilos kotlarmilos merged commit e14174b into dotnet:main Mar 29, 2023
@kotlarmilos kotlarmilos deleted the feature/inline-array-attribute branch March 29, 2023 12:02
@ghost ghost locked as resolved and limited conversation to collaborators Apr 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants