Skip to content

Some enums have different values between x64 and arm64 on macOS and Mac Catalyst #12111

Closed

Description

@rolfbjarne

Apple has defined a TARGET_ABI_USES_IOS_VALUES constant in their headers:

#define TARGET_ABI_USES_IOS_VALUES  (!TARGET_CPU_X86_64 || (TARGET_OS_IPHONE && !TARGET_OS_MACCATALYST))

which means TARGET_ABI_USES_IOS_VALUES is true on:

  • iOS, tvOS, watchOS (all abis): because TARGET_OS_IPHONE is 1 on these platforms
  • Mac Catalyst: arm64 (because !TARGET_CPU_X86_64)
  • macOS: arm64 (because !TARGET_CPU_X86_64)

and TARGET_ABI_USES_IOS_VALUES is false on:

  • Mac Catalyst: x64
  • macOS: x64

Searching Apple's headers, this shows up for the following enums:

typedef NS_ENUM(NSInteger, NSTextAlignment) {
    NSTextAlignmentLeft      = 0,    // Visually left aligned
#if TARGET_ABI_USES_IOS_VALUES
    NSTextAlignmentCenter    = 1,    // Visually centered
    NSTextAlignmentRight     = 2,    // Visually right aligned
#else /* !TARGET_ABI_USES_IOS_VALUES */
    NSTextAlignmentRight     = 1,    // Visually right aligned
    NSTextAlignmentCenter    = 2,    // Visually centered
#endif
    NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.
    NSTextAlignmentNatural   = 4     // Indicates the default alignment for script
} API_AVAILABLE(macos(10.0), ios(6.0), watchos(2.0), tvos(9.0));
typedef NS_ENUM(NSInteger, NSImageResizingMode) {
#if !TARGET_ABI_USES_IOS_VALUES
    NSImageResizingModeStretch = 0,
    NSImageResizingModeTile = 1,
#else /* !TARGET_ABI_USES_IOS_VALUES */
    NSImageResizingModeTile = 0,
    NSImageResizingModeStretch = 1,
#endif /* !TARGET_ABI_USES_IOS_VALUES */
} API_AVAILABLE(macos(10.10));
typedef NS_ENUM(NSInteger, UIImageResizingMode) {
#if TARGET_ABI_USES_IOS_VALUES
    UIImageResizingModeTile = 0,
    UIImageResizingModeStretch = 1,
#else /* TARGET_ABI_USES_IOS_VALUES */
    UIImageResizingModeStretch = 0,
    UIImageResizingModeTile = 1,
#endif /* TARGET_ABI_USES_IOS_VALUES */
};

Activity

  1. added
    bug
    dotnet
    dotnet-pri0
    on Jul 14, 2021
  2. added this to the .NET 6 milestone on Jul 14, 2021
  3. akoeplinger commented on Jul 14, 2021

    @akoeplinger
    Member

    I assume this is a variation of #11498 ?

  4. filipnavara commented on Jul 14, 2021

    @filipnavara
    Member

    It is a similar issue. The difference with SSLCipherSuite is that it's a deprecated API and thus not very performance sensitive. Some of the same constraints apply though:

    • Existing code and libraries embed the (managed) enum values and thus it's less likely to break existing code if the old values are kept
    • For macOS the ARM64 is new platform so there's no existing code for it and the "reference" values are the x64 ones
    • Mac Catalyst is technically also new platform on the managed side but there's compatibility story with iOS and thus the enums would have to follow the iOS numbers
  5. changed the title [-]Some enums have different values between x64 and arm64 on macOS[/-] [+]Some enums have different values between x64 and arm64 on macOS and Mac Catalyst[/+] on Jul 14, 2021
  6. spouliot commented on Jul 20, 2021

    @spouliot
    Contributor

    It's a breaking change to modify the values - and they differ between Xamarin.[iOS|tvOS|watchOS].dll and Xamarin.Mac.dll assemblies.

    Xamarin.MacCatalyst.dll could be set to anything (not a breaking change yet) but it makes sense to keep it like Xamarin.iOS.dll. However the values needs to differ by architecture :-(

    The values are also baked directly into (3rd parties) assemblies that refers to them... so that limit what we can do to fix this.

    Proposal

    I suggest adding extension methods, on all affected enum types, that gives the correct value. They can be optimized/removed later (e.g. by the linker) so they won't represent a large runtime cost.

    The generator will also need to be aware of those types, i.e. to use the extension methods, not the enum values, e.g.

    void SetAlignment (NSTextAlignment alignment)
    {
       long value = alignment.GetArchSpecificValue ();
       Pinvoke (value);
    }

    Also manual p/invokes will need to use the (public) extensions methods to work properly. That will need to be documented.

    Adding a new analyzer could also help in spotting incorrect usage inside user projects.

  7. rolfbjarne commented on Jul 20, 2021

    @rolfbjarne
    MemberAuthor

    I have a slightly different proposal :)

    Proposal #2

    We already have some code to convert values for these enums, because they're [Native] enums. We could augment that code so that we can do something like this:

    [Native (ConvertToNative="NSTextAlignmentExtensions.ConvertToNative", ConvertToManaged="NSTextAlignmentExtension.ConvertToManaged")]
    
    public static NSTextAlignment ConvertToNative (NSTextAlignment value)
    {
        if (Runtime.IsARM64CallingConvention) {
           switch (value) {
           case NSTextAlignment.Center: return NSTextAlignment.Right;
           case NSTextAlignment.Right: return NSTextAlignment.Center;
           }
        }
    
        return value;
    }

    We'd have a managed enum that would define the values for managed code, and then we convert to the corresponding value whenever we enter/exit native code (exactly like we already do for the existing [Native] support).

    The advantage would be that we'd not have to remember to use the extension functions, it would all be automatic (the only manual code would be the extension methods themselves).

    It would likely be a bit more work to implement though.

  8. spouliot commented on Jul 20, 2021

    @spouliot
    Contributor

    Love it! The only downside is that [Native] already has some semantics wrt [u]int-[u]long and would not work for SslCipherSuite enum (or similar cases). Hopefully the list is complete and this is not something that should happen anymore...

  9. self-assigned this
    on Aug 17, 2021
  10. added a commit that references this issue on Aug 19, 2021
    f803b21
  11. added a commit that references this issue on Aug 20, 2021
    2d4b68b
  12. ghost locked as resolved and limited conversation to collaborators on Apr 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugdotnetdotnet-pri0

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions