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

ByRef-like structs can be passed via __arglist but not retrieved #79065

Open
IS4Code opened this issue Dec 1, 2022 · 4 comments
Open

ByRef-like structs can be passed via __arglist but not retrieved #79065

IS4Code opened this issue Dec 1, 2022 · 4 comments

Comments

@IS4Code
Copy link

IS4Code commented Dec 1, 2022

Description

Calling ArgList.GetNextArg fails if the argument passed through __arglist has a ByRef-like type.

Reproduction Steps

Span<byte> s1 = stackalloc byte[1];
Span<byte> s2 = stackalloc byte[2];
Span<byte> s3 = stackalloc byte[3];
Method(__arglist(s1, s2, s3));

static void Method(__arglist)
{
    var ai = new ArgIterator(__arglist);
    while(ai.GetRemainingCount() > 0)
    {
        var tr = ai.GetNextArg();
        var val = __refvalue(tr, Span<byte>);
        Console.WriteLine(val.Length);
    }
}

Expected behavior

The value should be extracted, since C# itself allows you to pass the struct and retrieve the value without complaining, and it is not possible for the returned TypedReference to escape the method anyway.

Actual behavior

System.NotSupportedException: 'Type is not supported.' is thrown from GetNextArg.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6

Other information

No response

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 1, 2022
@IS4Code IS4Code changed the title ByRef-like structs can be passed via __arglist but not retrieves ByRef-like structs can be passed via __arglist but not retrieved Dec 1, 2022
@AaronRobinsonMSFT
Copy link
Member

This is "by-design" at present. The issue here is ByRef like types are not presently supported in this part of the stack nor in any Reflection-esque APIs. Additionally the __arglist keyword and semantics are narrowly defined to support only enough to satisfy the C++/CLI semantics as they match .NET Framework on Windows and therefore no new features are likely to be supported without broader need.

As far as why this isn't naturally supported is a TypedReference is presently unable to support a ref to a ByRef-like type in a safe manner, see https://github.com/dotnet/csharplang/blob/main/proposals/csharp-11.0/low-level-struct-improvements.md#ref-fields-to-ref-struct. However discussion on this topic and how a TypedReference could reference a ByRef-like type are being considered for .NET 8 Reflection scenarios. See #45152 and #75358.

@AaronRobinsonMSFT AaronRobinsonMSFT added area-System.Reflection and removed area-Interop-coreclr untriaged New issue has not been triaged by the area owner labels Dec 5, 2022
@ghost
Copy link

ghost commented Dec 5, 2022

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Calling ArgList.GetNextArg fails if the argument passed through __arglist has a ByRef-like type.

Reproduction Steps

Span<byte> s1 = stackalloc byte[1];
Span<byte> s2 = stackalloc byte[2];
Span<byte> s3 = stackalloc byte[3];
Method(__arglist(s1, s2, s3));

static void Method(__arglist)
{
    var ai = new ArgIterator(__arglist);
    while(ai.GetRemainingCount() > 0)
    {
        var tr = ai.GetNextArg();
        var val = __refvalue(tr, Span<byte>);
        Console.WriteLine(val.Length);
    }
}

Expected behavior

The value should be extracted, since C# itself allows you to pass the struct and retrieve the value without complaining, and it is not possible for the returned TypedReference to escape the method anyway.

Actual behavior

System.NotSupportedException: 'Type is not supported.' is thrown from GetNextArg.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6

Other information

No response

Author: IllidanS4
Assignees: -
Labels:

area-System.Reflection

Milestone: -

@AaronRobinsonMSFT AaronRobinsonMSFT added the untriaged New issue has not been triaged by the area owner label Dec 5, 2022
@steveharter steveharter added this to the Future milestone Dec 5, 2022
@steveharter steveharter removed the untriaged New issue has not been triaged by the area owner label Dec 5, 2022
@steveharter
Copy link
Member

Moving to future. When the other higher-priority byref-like issues are resolved, this will be considered.

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

No branches or pull requests

4 participants