Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__CFAllocatorRespectsHintZeroWhenAllocating not consistently declared extern C #5125

Closed
lhoward opened this issue Oct 30, 2024 · 9 comments · Fixed by #5126
Closed

__CFAllocatorRespectsHintZeroWhenAllocating not consistently declared extern C #5125

lhoward opened this issue Oct 30, 2024 · 9 comments · Fixed by #5126

Comments

@lhoward
Copy link
Contributor

lhoward commented Oct 30, 2024

when ForFoundationOnly.h and ForSwiftFoundationOnly.h end up being imported, compile fails due to different linkage of __CFAllocatorRespectsHintZeroWhenAllocating().

suggest removing it from ForFoundationOnly.h or adding CF_PRIVATE.

<module-includes>:1:10: note: in file included from <module-includes>:1:
1 | #include "CoreFoundation.h"
  |          `- note: in file included from <module-includes>:1:
2 | 

/opt/swift-6.0.2-RELEASE-ubuntu24.04-aarch64/usr/lib/swift/CoreFoundation/CoreFoundation.h:77:10: note: in file included from /opt/swift-6.0.2-RELEASE-ubuntu24.04-aarch64/usr/lib/swift/CoreFoundation/CoreFoundation.h:77:
 75 | #include "CFConstantKeys.h"
 76 | 
 77 | #include "ForSwiftFoundationOnly.h"
    |          `- note: in file included from /opt/swift-6.0.2-RELEASE-ubuntu24.04-aarch64/usr/lib/swift/CoreFoundation/CoreFoundation.h:77:
 78 | 
 79 | #if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX

<unknown>:0: error: could not build C module 'CoreFoundation'
/opt/swift-6.0.2-RELEASE-ubuntu24.04-aarch64/usr/lib/swift/CoreFoundation/ForSwiftFoundationOnly.h:108:20: error: declaration of '__CFAllocatorRespectsHintZeroWhenAllocating' has a different language linkage
106 | _CF_EXPORT_SCOPE_BEGIN
107 | 
108 | CF_PRIVATE Boolean __CFAllocatorRespectsHintZeroWhenAllocating(CFAllocatorRef _Nullable allocator);
    |                    `- error: declaration of '__CFAllocatorRespectsHintZeroWhenAllocating' has a different language linkage
109 | 
110 | struct __CFSwiftObject {

/opt/swift-6.0.2-RELEASE-ubuntu24.04-aarch64/usr/lib/swift/CoreFoundation/ForFoundationOnly.h:71:9: note: previous declaration is here
 69 | #endif
 70 | 
 71 | Boolean __CFAllocatorRespectsHintZeroWhenAllocating(CFAllocatorRef _Nullable allocator);
    |         `- note: previous declaration is here
 72 | typedef CF_ENUM(CFOptionFlags, _CFAllocatorHint) {
 73 |     _CFAllocatorHintZeroWhenAllocating = 1
@lhoward
Copy link
Contributor Author

lhoward commented Oct 30, 2024

Hmm, still seeing this when adding CF_PRIVATE to ForFoundationOnly.h for reasons I haven't yet determined (although if CF_PRIVATE is already defined, ForSwiftFoundationOnly.h won't define it, so that could be it).

Is there a reason it needs to be in ForFoundationOnly.h?

@parkera
Copy link
Contributor

parkera commented Oct 30, 2024

It is there for historical reasons. We can move it to CFInternal.h and make it CF_PRIVATE.

parkera added a commit to parkera/swift-corelibs-foundation that referenced this issue Oct 30, 2024
@parkera
Copy link
Contributor

parkera commented Oct 30, 2024

@lhoward are you saying this is new to 6.0.2 (vs 6.0)?

@lhoward
Copy link
Contributor Author

lhoward commented Oct 30, 2024

@lhoward are you saying this is new to 6.0.2 (vs 6.0)?

I am, although it doesn't really make sense (given the prototype in ForFoundationOnly.h was committed in 2021). I'll try to find out the real reason.

Edit: seeing this on 6.0 as well now.

@lhoward
Copy link
Contributor Author

lhoward commented Oct 30, 2024

I can't easily explain why it's just started happening with one particular package I'm trying to build.

But from first principles it seems that this always should have raised an error:

  • CoreFoundation.h (presumably the umbrella header for the CoreFoundation module) includes ForSwiftFoundationOnly.h
  • ForSwiftFoundationOnly.h defines CF_PRIVATE, if undefined, to extern __attribute__((__visibility__("hidden")))
  • ForSwiftFoundationOnly.h includes ForFoundationOnly.h
  • ForFoundationOnly.h declares __CFAllocatorRespectsHintZeroWhenAllocating() without CF_PRIVATE
  • ForSwiftFoundationOnly.h declares __CFAllocatorRespectsHintZeroWhenAllocating() ) with CF_PRIVATE

The only reasons I can think of as to why this didn't raise an error by default:

  • somehow CF_PRIVATE was being defined to the empty string (seems unlikely)
  • something in the compiler flags (perhaps related to C++ interoperability?) raised the language linkage error in this particular case

@lhoward
Copy link
Contributor Author

lhoward commented Oct 30, 2024

Actually, it's nothing to do with the visibility attribute. The issue is that __CFAllocatorRespectsHintZeroWhenAllocating in ForFoundationOnly.h is not within extern "C" (via _CF_EXPORT_SCOPE_BEGIN).

Moving __CFAllocatorRespectsHintZeroWhenAllocating after _CF_EXPORT_SCOPE_BEGIN in ForFoundationOnly.h also fixes it. Of course #5126 looks like a better fix.

Why did I only see this now? Perhaps it was there all along since Swift 6 and something has changed WRT C++ interoperability (which I am using)?

@lhoward lhoward changed the title linkage build errors on Linux since upgrading to 6.0.2 __CFAllocatorRespectsHintZeroWhenAllocating not consistently declared extern C Oct 30, 2024
@parkera
Copy link
Contributor

parkera commented Oct 30, 2024

It makes sense that this could have started in 6.0 vs 6.0.2. Thanks for the investigation.

parkera added a commit that referenced this issue Oct 30, 2024
…der to avoid confusion over its linkage. Resolves #5125 (#5126)
parkera added a commit to parkera/swift-corelibs-foundation that referenced this issue Oct 30, 2024
…der to avoid confusion over its linkage. Resolves swiftlang#5125 (swiftlang#5126)

(cherry picked from commit b1b4603)
@lhoward
Copy link
Contributor Author

lhoward commented Oct 30, 2024

Tangential: __CFSafelyReallocate() and __CFSafelyReallocateWithAllocator() are also declare with incorrect (but not conflicting) linkage when imported as C++. Suggest wrapping them in _CF_EXPORT_SCOPE_BEGIN/END.

parkera added a commit that referenced this issue Nov 1, 2024
…der to avoid confusion over its linkage. Resolves #5125 (#5126) (#5127)

(cherry picked from commit b1b4603)
@parkera
Copy link
Contributor

parkera commented Nov 6, 2024

Let's file a new bug/get a new PR for reallocate ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants