From 15dcad15df0aca75cdeb9fdff5ab8d7b4c0cdfcd Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 12 Aug 2021 14:20:56 +0100 Subject: [PATCH] Disable file/process-related code in CF for WASI --- CoreFoundation/Base.subproj/CFPriv.h | 8 +++++ CoreFoundation/Base.subproj/CFUtilities.c | 21 +++++++------ CoreFoundation/Base.subproj/CoreFoundation.h | 4 +++ .../Base.subproj/ForFoundationOnly.h | 10 +++++- .../Base.subproj/ForSwiftFoundationOnly.h | 31 +++++++++++++++++-- .../SwiftRuntime/CoreFoundation.h | 12 ++++--- 6 files changed, 69 insertions(+), 17 deletions(-) diff --git a/CoreFoundation/Base.subproj/CFPriv.h b/CoreFoundation/Base.subproj/CFPriv.h index 4c70bcaf9b..40852ce078 100644 --- a/CoreFoundation/Base.subproj/CFPriv.h +++ b/CoreFoundation/Base.subproj/CFPriv.h @@ -46,14 +46,17 @@ #include #endif +#if !TARGET_OS_WASI #include #include +#endif #include CF_EXTERN_C_BEGIN CF_EXPORT void _CFRuntimeSetCFMPresent(void *a); +#if !TARGET_OS_WASI CF_EXPORT const char *_CFProcessPath(void); CF_EXPORT const char **_CFGetProcessPath(void); CF_EXPORT const char **_CFGetProgname(void); @@ -65,6 +68,7 @@ CF_EXPORT void _CFGetUGIDs(uid_t *euid, gid_t *egid); CF_EXPORT uid_t _CFGetEUID(void); CF_EXPORT uid_t _CFGetEGID(void); #endif +#endif #if (TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_LINUX)) CF_EXPORT void _CFRunLoopSetCurrent(CFRunLoopRef rl); @@ -161,6 +165,7 @@ CF_EXPORT Boolean _CFStringGetFileSystemRepresentation(CFStringRef string, UInt8 /* If this is publicized, we might need to create a GetBytesPtr type function as well. */ CF_EXPORT CFStringRef _CFStringCreateWithBytesNoCopy(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean externalFormat, CFAllocatorRef contentsDeallocator); +#if !TARGET_OS_WASI /* These return NULL on MacOS 8 */ // This one leaks the returned string in order to be thread-safe. // CF cannot help you in this matter if you continue to use this SPI. @@ -172,6 +177,7 @@ CFStringRef CFCopyUserName(void); CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName); /* Pass NULL for the current user's home directory */ +#endif /* @@ -630,8 +636,10 @@ typedef CF_OPTIONS(CFOptionFlags, _CFBundleFilteredPlistOptions) { _CFBundleFilteredPlistMemoryMapped = 1 } API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)); +#if !TARGET_OS_WASI CF_EXPORT CFPropertyListRef _CFBundleCreateFilteredInfoPlist(CFBundleRef bundle, CFSetRef keyPaths, _CFBundleFilteredPlistOptions options) API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)); CF_EXPORT CFPropertyListRef _CFBundleCreateFilteredLocalizedInfoPlist(CFBundleRef bundle, CFSetRef keyPaths, CFStringRef localizationName, _CFBundleFilteredPlistOptions options) API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)); +#endif #if TARGET_OS_WIN32 #include diff --git a/CoreFoundation/Base.subproj/CFUtilities.c b/CoreFoundation/Base.subproj/CFUtilities.c index bc0c112d96..ece51868ab 100644 --- a/CoreFoundation/Base.subproj/CFUtilities.c +++ b/CoreFoundation/Base.subproj/CFUtilities.c @@ -95,7 +95,8 @@ #endif CF_PRIVATE os_log_t _CFOSLog(void) { - static os_log_t logger; + static os_log_t logger = NULL; + static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ logger = os_log_create("com.apple.foundation", "general"); @@ -469,7 +470,6 @@ CONST_STRING_DECL(_kCFSystemVersionProductVersionStringKey, "Version") CONST_STRING_DECL(_kCFSystemVersionBuildStringKey, "Build") #endif - CF_EXPORT Boolean _CFExecutableLinkedOnOrAfter(CFSystemVersion version) { return true; } @@ -694,7 +694,7 @@ CF_EXPORT uid_t _CFGetEGID(void) { __CFGetUGIDs(NULL, &egid); return egid; } -#endif // !TARGET_OS_WASI +#endif const char *_CFPrintForDebugger(const void *obj) { static char *result = NULL; @@ -1088,12 +1088,13 @@ CF_PRIVATE void _CFLogSimple(int32_t lev, char *format, ...) { void CFLog(int32_t lev, CFStringRef format, ...) { va_list args; - va_start(args, format); -#if TARGET_OS_WASI - _CFLogvEx3(NULL, NULL, NULL, NULL, lev, format, args, NULL); -#else + va_start(args, format); + + #if !TARGET_OS_WASI _CFLogvEx3(NULL, NULL, NULL, NULL, lev, format, args, __builtin_return_address(0)); -#endif + #else + _CFLogvEx3(NULL, NULL, NULL, NULL, lev, format, args, NULL); + #endif va_end(args); } @@ -1649,7 +1650,7 @@ CFDictionaryRef __CFGetEnvironment() { #if TARGET_OS_MAC extern char ***_NSGetEnviron(void); char **envp = *_NSGetEnviron(); -#elif TARGET_OS_BSD || TARGET_OS_CYGWIN +#elif TARGET_OS_BSD || TARGET_OS_CYGWIN || TARGET_OS_WASI extern char **environ; char **envp = environ; #elif TARGET_OS_LINUX @@ -1701,4 +1702,4 @@ int32_t __CFGetPid() { return getpid(); } -#endif // DEPLOYMENT_RUNTIME_SWIFT && !TARGET_OS_WASI +#endif diff --git a/CoreFoundation/Base.subproj/CoreFoundation.h b/CoreFoundation/Base.subproj/CoreFoundation.h index c1cee4581e..b95bf265ce 100644 --- a/CoreFoundation/Base.subproj/CoreFoundation.h +++ b/CoreFoundation/Base.subproj/CoreFoundation.h @@ -22,7 +22,9 @@ #include #include #include +#if !defined(__wasi__) #include +#endif #include #include #include @@ -56,7 +58,9 @@ #include #include #include +#if !TARGET_OS_WASI #include +#endif #include #include #include diff --git a/CoreFoundation/Base.subproj/ForFoundationOnly.h b/CoreFoundation/Base.subproj/ForFoundationOnly.h index 77b0cb126e..99407b783d 100644 --- a/CoreFoundation/Base.subproj/ForFoundationOnly.h +++ b/CoreFoundation/Base.subproj/ForFoundationOnly.h @@ -27,8 +27,11 @@ #include #include #include + +#if !TARGET_OS_WASI #include #include +#endif #include #include @@ -60,6 +63,7 @@ CF_IMPLICIT_BRIDGING_DISABLED #include #endif +#if __BLOCKS__ /* These functions implement standard error handling for reallocation. Their parameters match their unsafe variants (realloc/CFAllocatorReallocate). They differ from reallocf as they provide a chance for you to clean up a buffers contents (in addition to freeing the buffer, etc.) The optional reallocationFailureHandler is called only when the reallocation fails (with the original buffer passed in, so you can clean up the buffer/throw/abort/etc. @@ -68,8 +72,9 @@ CF_IMPLICIT_BRIDGING_DISABLED */ CF_EXPORT void *_Nonnull __CFSafelyReallocate(void * _Nullable destination, size_t newCapacity, void (^_Nullable reallocationFailureHandler)(void *_Nonnull original, bool *_Nonnull outRecovered)); CF_EXPORT void *_Nonnull __CFSafelyReallocateWithAllocator(CFAllocatorRef _Nullable, void * _Nullable destination, size_t newCapacity, CFOptionFlags options, void (^_Nullable reallocationFailureHandler)(void *_Nonnull original, bool *_Nonnull outRecovered)); +#endif - +#if !TARGET_OS_WASI #pragma mark - CFBundle #include @@ -92,6 +97,7 @@ CF_EXPORT Boolean _CFBundleLoadExecutableAndReturnError(CFBundleRef bundle, Bool CF_EXPORT CFErrorRef _CFBundleCreateError(CFAllocatorRef _Nullable allocator, CFBundleRef bundle, CFIndex code); _CF_EXPORT_SCOPE_END +#endif #pragma mark - CFUUID @@ -576,6 +582,7 @@ CF_CROSS_PLATFORM_EXPORT void _CFURLInitWithFileSystemPathRelativeToBase(CFURLRe CF_CROSS_PLATFORM_EXPORT Boolean _CFURLInitWithURLString(CFURLRef url, CFStringRef string, Boolean checkForLegalCharacters, _Nullable CFURLRef baseURL); CF_CROSS_PLATFORM_EXPORT Boolean _CFURLInitAbsoluteURLWithBytes(CFURLRef url, const UInt8 *relativeURLBytes, CFIndex length, CFStringEncoding encoding, _Nullable CFURLRef baseURL); +#if !TARGET_OS_WASI CF_EXPORT Boolean _CFRunLoopFinished(CFRunLoopRef rl, CFStringRef mode); CF_EXPORT CFTypeRef _CFRunLoopGet2(CFRunLoopRef rl); CF_EXPORT Boolean _CFRunLoopIsCurrent(CFRunLoopRef rl); @@ -586,6 +593,7 @@ CF_EXPORT void _CFWriteStreamInitialize(CFWriteStreamRef writeStream); CF_EXPORT void _CFReadStreamDeallocate(CFReadStreamRef readStream); CF_EXPORT void _CFWriteStreamDeallocate(CFWriteStreamRef writeStream); CF_EXPORT CFReadStreamRef CFReadStreamCreateWithData(_Nullable CFAllocatorRef alloc, CFDataRef data); +#endif #if TARGET_OS_MAC typedef struct { diff --git a/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h b/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h index 401f2078d2..64f8ee5892 100644 --- a/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h +++ b/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h @@ -35,7 +35,7 @@ #define VC_EXTRALEAN #define WIN32_LEAN_AND_MEAN #include -#else +#elif !TARGET_OS_WASI #include #endif #if __has_include() @@ -198,9 +198,11 @@ struct _NSMutableStringBridge { void (*_cfAppendCString)(CFTypeRef str, const char *chars, CFIndex appendLength); }; +#if !TARGET_OS_WASI struct _NSRunLoop { _Nonnull CFTypeRef (*_Nonnull _new)(CFRunLoopRef rl); }; +#endif struct _NSCharacterSetBridge { _Nullable CFCharacterSetRef (*_Nonnull _expandedCFCharacterSet)(CFTypeRef cset); @@ -276,7 +278,9 @@ struct _CFSwiftBridge { struct _NSMutableSetBridge NSMutableSet; struct _NSStringBridge NSString; struct _NSMutableStringBridge NSMutableString; +#if !TARGET_OS_WASI struct _NSRunLoop NSRunLoop; +#endif struct _NSCharacterSetBridge NSCharacterSet; struct _NSMutableCharacterSetBridge NSMutableCharacterSet; struct _NSNumberBridge NSNumber; @@ -375,7 +379,10 @@ CF_PRIVATE uint64_t __CFMemorySize(void); CF_PRIVATE CFIndex __CFActiveProcessorCount(void); CF_CROSS_PLATFORM_EXPORT CFStringRef CFCopyFullUserName(void); +#if !TARGET_OS_WASI extern CFWriteStreamRef _CFWriteStreamCreateFromFileDescriptor(CFAllocatorRef alloc, int fd); +#endif + #if !__COREFOUNDATION_FORFOUNDATIONONLY__ typedef const struct __CFKeyedArchiverUID * CFKeyedArchiverUIDRef; extern CFTypeID _CFKeyedArchiverUIDGetTypeID(void); @@ -385,7 +392,10 @@ extern uint32_t _CFKeyedArchiverUIDGetValue(CFKeyedArchiverUIDRef uid); extern CFIndex __CFBinaryPlistWriteToStream(CFPropertyListRef plist, CFTypeRef stream); CF_CROSS_PLATFORM_EXPORT CFDataRef _CFPropertyListCreateXMLDataWithExtras(CFAllocatorRef allocator, CFPropertyListRef propertyList); + +#if !TARGET_OS_WASI extern CFWriteStreamRef _CFWriteStreamCreateFromFileDescriptor(CFAllocatorRef alloc, int fd); +#endif CF_EXPORT char *_Nullable *_Nonnull _CFEnviron(void); @@ -409,7 +419,9 @@ CF_EXPORT _CFThreadRef _CFMainPThread; CF_EXPORT CFHashCode __CFHashDouble(double d); +#if __BLOCKS__ CF_CROSS_PLATFORM_EXPORT void CFSortIndexes(CFIndex *indexBuffer, CFIndex count, CFOptionFlags opts, CFComparisonResult (^cmp)(CFIndex, CFIndex)); +#endif CF_EXPORT CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key); CF_EXPORT void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value); @@ -425,6 +437,7 @@ CF_EXPORT CFCharacterSetRef _CFCharacterSetCreateCopy(CFAllocatorRef alloc, CFCh CF_EXPORT CFMutableCharacterSetRef _CFCharacterSetCreateMutableCopy(CFAllocatorRef alloc, CFCharacterSetRef theSet); CF_CROSS_PLATFORM_EXPORT void _CFCharacterSetInitCopyingSet(CFAllocatorRef alloc, CFMutableCharacterSetRef cset, CFCharacterSetRef theSet, bool isMutable, bool validateSubclasses); +#if !TARGET_OS_WASI CF_EXPORT _Nullable CFErrorRef CFReadStreamCopyError(CFReadStreamRef _Null_unspecified stream); CF_EXPORT _Nullable CFErrorRef CFWriteStreamCopyError(CFWriteStreamRef _Null_unspecified stream); @@ -433,6 +446,7 @@ CF_CROSS_PLATFORM_EXPORT CFStringRef _Nullable _CFBundleCopyExecutablePath(CFBun CF_CROSS_PLATFORM_EXPORT Boolean _CFBundleSupportsFHSBundles(void); CF_CROSS_PLATFORM_EXPORT Boolean _CFBundleSupportsFreestandingBundles(void); CF_CROSS_PLATFORM_EXPORT CFStringRef _Nullable _CFBundleCopyLoadedImagePathForAddress(const void *p); +#endif CF_CROSS_PLATFORM_EXPORT CFStringRef __CFTimeZoneCopyDataVersionString(void); @@ -494,6 +508,18 @@ static inline _Bool _resizeConditionalAllocationBuffer(_ConditionalAllocationBuf return true; } +#if TARGET_OS_WASI +static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(_ConditionalAllocationBuffer *_Nonnull)) { + _ConditionalAllocationBuffer buffer; + buffer.capacity = amount; + buffer.onStack = false; + buffer.memory = malloc(buffer.capacity); + if (buffer.memory == NULL) { return false; } + applier(&buffer); + free(buffer.memory); + return true; +} +#else static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(_ConditionalAllocationBuffer *_Nonnull)) { _ConditionalAllocationBuffer buffer; #if TARGET_OS_MAC @@ -510,6 +536,7 @@ static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((n } return true; } +#endif static inline _Bool _withStackOrHeapBufferWithResultInArguments(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(void *_Nonnull memory, size_t capacity, _Bool onStack)) { return _withStackOrHeapBuffer(amount, ^(_ConditionalAllocationBuffer *buffer) { @@ -525,7 +552,7 @@ CF_CROSS_PLATFORM_EXPORT CFIndex __CFCharDigitValue(UniChar ch); #if TARGET_OS_WIN32 CF_CROSS_PLATFORM_EXPORT int _CFOpenFileWithMode(const unsigned short *path, int opts, mode_t mode); -#else +#elif !TARGET_OS_WASI CF_CROSS_PLATFORM_EXPORT int _CFOpenFileWithMode(const char *path, int opts, mode_t mode); #endif CF_CROSS_PLATFORM_EXPORT void *_CFReallocf(void *ptr, size_t size); diff --git a/CoreFoundation/Base.subproj/SwiftRuntime/CoreFoundation.h b/CoreFoundation/Base.subproj/SwiftRuntime/CoreFoundation.h index a3ebe169bb..b373875ac2 100644 --- a/CoreFoundation/Base.subproj/SwiftRuntime/CoreFoundation.h +++ b/CoreFoundation/Base.subproj/SwiftRuntime/CoreFoundation.h @@ -31,7 +31,9 @@ #include #include #include +#if !defined(__wasi__) #include +#endif #include #include #include @@ -43,7 +45,7 @@ #include // for Host.swift #endif -#if __has_include() +#if __has_include() && !defined(__wasi__) #include // for Host.swift #endif @@ -73,7 +75,6 @@ #include #include #include -#include #include #include #include @@ -84,14 +85,17 @@ #include #include #include -#include -#include +#if !TARGET_OS_WASI +#include #include +#include +#include #include #include #include #include +#endif #include #include