Skip to content

Prefer ExactSpelling=true on [DllImport] for known APIs #35695

Closed

Description

@elinor-fung

On Windows, setting ExactSpelling=true on the DllImport attribute prevents the runtime from looking for alternate function names (suffix based on CharSet), giving a slight performance benefit by avoiding this search.

There is a fair amount of nuance associated with the behaviour of ExactSpelling and under which scenarios there would actually be a performance benefit. In order to avoid noise, this rule can be scoped to check for a database of Win32 APIs that use the suffixes and provide a warning and fix for those cases where it matters.

// Flag ExactSpelling=false
[DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
private static extern bool SetEnvironmentVariableW(string name, string? value);

// OK - not in database of APIs
[DllImport("MyLibrary.dll", CharSet=CharSet.Unicode)]
private static extern void Foo();

Recommend:

[DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling = true)]
private static extern bool SetEnvironmentVariableW(string name, string? value);

Category: Performance
Default: Enabled

cc @terrajobst @stephentoub @AaronRobinsonMSFT @jkoritzinsky

Activity

  1. added
    api-suggestion
    code-analyzer
    on May 1, 2020
  2. added
    untriaged
    on May 1, 2020
  3. stephentoub commented on May 1, 2020

    @stephentoub
    Member

    Is there any concern here that someone following this guidance will introduce bugs, e.g. because they were actually relying on the behavior? Can an auto-fixer do more than just add "ExactSpelling = true", e.g. would it be able to update the function name appropriately in case adding ExactSpelling would be introducing a bug?

    I expect we have a bunch of places where we don't follow this guidance in dotnet/runtime. What does a fix look like there, and how much overhead should we expect to see removed? If it's meaningful enough to add an analyzer for, it would more so seem to be something we should fix in dotnet/runtime for .NET 5.

  4. AaronRobinsonMSFT commented on May 1, 2020

    @AaronRobinsonMSFT
    Member

    Is there any concern here that someone following this guidance will introduce bugs, e.g. because they were actually relying on the behavior?

    The biggest one is for Win32 APIs that use the A/W suffix. Detecting that might be possible, but the heuristic would be fragile. We could have an exhaustive Win32 API list where this applies. I haven't observed new Win32 APIs applying these suffixes anymore so it might actually be stable.

  5. EgorBo commented on May 1, 2020

    @EgorBo
    Member

    I believe there is an issue somewhere to turn off A/W probing for non-windows OSs so ExactSpelling=true will be just a noise for them.

  6. elinor-fung commented on May 5, 2020

    @elinor-fung
    MemberAuthor

    Good call about non-Windows - certainly would not want that noise.

    Looking more at how we look for an entry point, it does have a bit more subtlety than I had realized. On Windows, if ExactSpelling=false and:

    • CharSet=CharSet.Unicode, we always look for both the specified name and the name with the W suffix
      • Even if the specified name is found as an exact match to an entry point, we look for the W suffix. If found, the W suffix function is used.
      • It may not be clear to the user that they are actually relying on the suffix-appending behaviour, so ExactSpelling=true, would also require adding the W suffix,
    • CharSet=CharSet.Ansi, we look for the specified name (no suffix) first and only try the suffix if it is not found
      • If the specified name is already the exact entry point name, setting ExactSpelling=true wouldn't make any difference.
      • Setting ExactSpelling=true would only matter if the user is actually relying on the suffix-appending behaviour - and it would require updating to add the A suffix.

    Given the above, I'm a little less convinced that this should warrant a rule/fixer.

    Rule or not, I'd agree we should do a pass over dotnet/runtime. Aside from places that just don't need the suffix behaviour, I also see some places where functions explicitly have the W suffix in the entry point name (e.g. FooW), but don't set ExactSpelling=true, so the runtime always looks for a duplicated suffix (e.g. FooWW)...

  7. jkotas commented on May 5, 2020

    @jkotas
    Member

    Given the above, I'm a little less convinced that this should warrant a rule/fixer.

    You build the database of OS APIs and do the rule/fixer for those only.

  8. changed the title [-]Prefer ExactSpelling=true on [DllImport][/-] [+]Prefer ExactSpelling=true on [DllImport] for known APIs[/+] on May 27, 2020
  9. removed
    untriaged
    on Jun 8, 2020
  10. added this to the 5.0 milestone on Jun 8, 2020
  11. added and removed
    api-suggestion
    on Jun 26, 2020
  12. 18 remaining items

  13. riverar commented on Feb 5, 2021

    @riverar
    Contributor
  14. Mrnikbobjeff commented on Feb 26, 2021

    @Mrnikbobjeff

    @riverar I used a small utility exe I wrote to parse dumpbin output for all dll files in the System32 directory. I uploaded the gist but it really is just a hacked together script and nothing production ready :)

  15. riverar commented on Feb 26, 2021

    @riverar
    Contributor

    @Mrnikbobjeff Thanks, I think my concern when I originally asked was that we should be using win32metadata as the authoritative source and not any ad-hoc scans. Scanning System32 is not a great idea due to windows componentization, you'll definitely miss something.

  16. jkoritzinsky commented on Jul 13, 2021

    @jkoritzinsky
    Member

    @Mrnikbobjeff how are you doing with this work? Do you need any assistance?

  17. modified the milestones: 6.0.0, 7.0.0 on Jul 30, 2021
  18. carlossanlop commented on Jul 6, 2022

    @carlossanlop
    Contributor

    @Mrnikbobjeff I'll unassign this issue and move it to 8.0. If you still want to work on it, let us know.

  19. added
    help wanted
    on Jul 6, 2022
  20. modified the milestones: 7.0.0, 8.0.0 on Jul 6, 2022
  21. jkotas commented on Jul 6, 2022

    @jkotas
    Member

    This was superseded by the interop source generator and LibraryImportAttribute that sets ExactSpelling=true by default.

    @AaronRobinsonMSFT @jkoritzinsky Should we close this?

  22. AaronRobinsonMSFT commented on Jul 6, 2022

    @AaronRobinsonMSFT
    Member

    I think so. I'm of the opinion that for .NET 7+, any analyzers for DllImport, unless security sensitive, should be retargeted to LibraryImport.

  23. ghost locked as resolved and limited conversation to collaborators on Aug 6, 2022
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-approvedarea-System.Runtime.InteropServicescode-analyzerhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions