Skip to content

Analyzer for MarshalAs usage #34831

Closed
Closed

Description

@AaronRobinsonMSFT

Defining how types should be marshaled via a P/Invoke or COM interop is difficult to get correct and many bad practices have been developed over the years. The Native interoperability best practices page should be converted into an analyzer to help developers do the right thing.

Related to #33808

Additional thoughts:

/cc @terrajobst @jkoritzinsky @elinor-fung

Activity

  1. added
    api-suggestion
    code-analyzer
    on Apr 10, 2020
  2. added
    untriaged
    on Apr 10, 2020
  3. danmoseley commented on Apr 10, 2020

    @danmoseley
    Contributor
  4. elinor-fung commented on Apr 21, 2020

    @elinor-fung
    Member

    There is an existing P/Invoke analyzer, which could be a fitting home for some of this.

    Some marshalling-related items that could be reasonable rules from the guidance doc:

    • Specify CharSet on DllImport when strings/characters are present
      • CA2101 seems to partly cover this (does string and StringBuilder, not char), although it is under Globalization(?) and the doc talks about security / partial trust
    • Prefer ExactSpelling=true on DllImport (Performance)
    • Do not use [Out] string (Interoperability)
    • Avoid StringBuilder parameters (Performance)
    • Specify marshalling for booleans (Interoperability)
      • CA1414 exists, but is disabled for Core (also only for p/invokes, so won't hit fields on types used in p/invokes)
    • Specify SizeConst when marshalling field as ByValArray (Interoperability)
    • Do not use Delegate or MulticastDelegate fields in marshalled structs (Interoperability).
  5. AaronRobinsonMSFT commented on Apr 23, 2020

    @AaronRobinsonMSFT
    MemberAuthor

    @elinor-fung Here is another issue that should get a rule: #9944

  6. JeremyKuhne commented on May 5, 2020

    @JeremyKuhne
    Member

    Some initial thoughts:

    • Having both [ComVisible] and [ComImport] on an interface should be incorrect, right?
    • For COM interfaces we should consider always requiring [MarshalAs] for string and object as they are often not BSTR and VARIANT
  7. removed
    untriaged
    on May 11, 2020
  8. added this to the 5.0 milestone on May 11, 2020
  9. AaronRobinsonMSFT commented on May 11, 2020

    @AaronRobinsonMSFT
    MemberAuthor

    @elinor-fung Is this aggregated issue being handled by the various other issues being filed or should we keep this one open?

  10. elinor-fung commented on May 27, 2020

    @elinor-fung
    Member

    Closing this and using #37039 as the tracking issue that will point to the separate issues we file for each rule.

  11. ghost locked as resolved and limited conversation to collaborators on Dec 9, 2020
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

    api-suggestionarea-Interop-coreclrcode-analyzer

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions