Skip to content

Should we mark MethodBase.GetCurrentMethod() and friends RequiresUnreferencedCode? #53242

Closed

Description

@MichalStrehovsky

Illink started trimming bits of metadata off things that are not seen as reflected on. We have APIs that allow reflecting on basically everything in the program (e.g. MethodBase.GetCurrentMethod()). One of the bits illinker strips is parameter names.

While MethodBase.GetCurrentMethod is not "trim unsafe" per se, the MethodInfos returned from it might be "damaged" and will result in different runtime behavior. We have been tagging places like that so that the developer can decide whether it's acceptable. Tagging ParameterInfo.Name as trim-unsafe feels like unnecessarily big hammer - it will work if the MethodBase was retrieved through more standard means. It feels more appropriate to just tag MethodBase.GetCurrentMethod and friends as RequiresUnreferenced.

E.g. the Illinker optimization is going to cause different behavior in this code. I expect we're fine with a different behavior there, but it should be a conscious decision, not a random result of an unsafe optimization. E.g. we might want to revise what happens when the parameter name was null in that codepath.

// arguments printing
sb.Append('(');
bool fFirstParam = true;
for (int j = 0; j < pi.Length; j++)
{
if (!fFirstParam)
sb.Append(", ");
else
fFirstParam = false;
string typeName = "<UnknownType>";
if (pi[j].ParameterType != null)
typeName = pi[j].ParameterType.Name;
sb.Append(typeName);
sb.Append(' ');
sb.Append(pi[j].Name);
}
sb.Append(')');

This would apply to APIs like: Exception.TargetSite and StackFrame.GetMethod too.

Activity

  1. added
    linkable-framework
    on May 25, 2021
  2. added this to the 6.0.0 milestone on May 25, 2021
  3. ghost added
    untriaged
    on May 25, 2021
  4. vitek-karas commented on May 26, 2021

    @vitek-karas
    Member

    I think it makes sense to annotate these. We'll see how widespread the usage is. If it's too bad we may consider supporting some of these view instrinsics if possible to avoid warnings (For example the GetCurrentMethod is something linker can figure out and make "safe" always).

  5. removed
    untriaged
    on Jul 6, 2021
  6. modified the milestones: 6.0.0, 7.0.0 on Sep 9, 2021
  7. added a commit that references this issue on Oct 1, 2021
    daea18b
  8. ghost added
    in-pr
    on Oct 1, 2021
  9. added a commit that references this issue on Oct 4, 2021
    b72b8a2
  10. ghost removed
    in-pr
    on Oct 4, 2021
  11. ghost locked as resolved and limited conversation to collaborators on Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-AssemblyLoader-coreclrlinkable-framework

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions