Skip to content

.NET: Use UnmanagedCallersOnlyAttribute instead of (or in addition to) MonoPInvokeCallbackAttribute #10470

Closed

Description

@rolfbjarne

.NET 5/C# 9 has a new UnmanagedCallersOnlyAttribute, which we should start using instead of the MonoPInvokeCallbackAttribute (the main advantage is that the compiler will tell you if you use it wrong, as opposed to the MonoPInvokeCallbackAttribute, where the AOT compiler will crash and not tell you why if you use it wrong).

Ref: dotnet/runtime#47177

Using UnmanagedCallersOnlyAttribute for block callbacks is rather complex, and has been moved to a separate issue: #15783 (see #10470 (comment)).

Activity

  1. added
    macOSIssues affecting macOS
    iOSIssues affecting iOS
    dotnetAn issue or pull request related to .NET
    on Jan 20, 2021
  2. added this to the .NET 6 milestone on Jan 20, 2021
  3. spouliot commented on Jan 20, 2021

    @spouliot
    Contributor

    I'd rather have it instead of (than in addition to) so we're sure we're testing the new attribute and behaviour.
    It also reduce the amount of final metadata inside the app - even if that should not be a huge size :)

  4. added
    dotnet-pri1.NET 6: important for stable release
    enhancementThe issue or pull request is an enhancement
    on Feb 3, 2021
  5. spouliot commented on Feb 3, 2021

    @spouliot
    Contributor

    The old/existing attribute won't be in CoreCLR, which is pri0 #10569
    That makes it a pre-requirement to fix this earlier than pri1

  6. rolfbjarne commented on Feb 3, 2021

    @rolfbjarne
    MemberAuthor

    The old/existing attribute won't be in CoreCLR, which is pri0 #10569
    That makes it a pre-requirement to fix this earlier than pri1

    The MonoPInvokeCallbackAttribute is only required on platforms where we run the AOT compiler.
    CoreCLR won't be available (at least for now) on any platforms where we need the AOT compiler.
    Thus I don't think we need to implement this before CoreCLR support (and we define the attribute ourselves).

  7. spouliot commented on Feb 3, 2021

    @spouliot
    Contributor

    I don't recall if all those existing attributes are under #if !MONOMAC or similar conditionals...
    If so then it's fine, if not then we have to exclude them (which is about the same work as replacing them)

  8. rolfbjarne commented on Oct 4, 2021

    @rolfbjarne
    MemberAuthor

    It turns out this is a rather involved task, in particular when it comes to blocks:

    1. Add support to the BlockLiteral struct to accept function pointers. This is not trivial (might even be easier to create a new type instead of adding support to the existing type).
    2. Add support to the generator for the new block implementation.
    3. Add support to our optimizations to handle the new block implementation.
    4. Update all our manual binding code to use the new block implementation.

    In the normal case (where the callback is supposed to be just a plain C function), things are a bit simpler, in that we can port our manual binding code incrementally.

    I've ported a single callback (which at least makes sure that using function pointers work): #12920

    Since the work for blocks is not trivial, and requires some design and thought, it can be postponed until .NET 7.

  9. modified the milestones: .NET 6, .NET 7 on Oct 4, 2021
  10. 32 remaining items

  11. stephen-hawley commented on Sep 8, 2022

    @stephen-hawley
    Contributor

    Adding to keep track of incomplete work for using [UnmanagedCallersOnly]
    The following files use SetupBlockUnsafe (this is not exhaustive)
    AddressBook
    CoreText
    CoreGraphics/PDFArray.cs

  12. stephen-hawley commented on Sep 13, 2022

    @stephen-hawley
    Contributor

    The following packages use SetupBlock or SetupBlockUnsafe:
    AddressBook - SetupBlockUnsafe
    CoreText - SetupBlockUnsafe
    CoreGraphics - PDFArray.cs, SetupBlockUnsafe
    Metal
    Network
    ObjCRuntime
    UIKit
    VideoToolbox

    ImageIO - mixed usage of callback as delegate and an unmanaged delegate. This is
    weird.

  13. rolfbjarne commented on Mar 15, 2023

    @rolfbjarne
    MemberAuthor

    @stephen-hawley is this all done now?

  14. rolfbjarne commented on Aug 10, 2023

    @rolfbjarne
    MemberAuthor

    @stephen-hawley is this all done now?

    The generator needs to be updated too: #18685

  15. modified the milestones: .NET 8, .NET 9 on Sep 20, 2023
  16. rolfbjarne commented on May 2, 2024

    @rolfbjarne
    MemberAuthor

    I believe this is complete now, we're only missing manual P/Invokes (neither the generator nor blocks), which is handled in #15684.

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

Metadata

Metadata

Labels

dotnetAn issue or pull request related to .NETenhancementThe issue or pull request is an enhancementiOSIssues affecting iOSmacOSIssues affecting macOSperformanceIf an issue or pull request is related to performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions