Skip to content

Disable file/process-related code in CF for WASI #3037

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

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CoreFoundation/Base.subproj/CFPriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@
#include <CoreFoundation/CFMessagePort.h>
#endif

#if !TARGET_OS_WASI
#include <CoreFoundation/CFRunLoop.h>
#include <CoreFoundation/CFSocket.h>
#endif
#include <CoreFoundation/CFBundlePriv.h>

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);
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -172,6 +177,7 @@ CFStringRef CFCopyUserName(void);

CF_EXPORT
CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName); /* Pass NULL for the current user's home directory */
#endif


/*
Expand Down Expand Up @@ -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 <CoreFoundation/CFNotificationCenter.h>
Expand Down
21 changes: 11 additions & 10 deletions CoreFoundation/Base.subproj/CFUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -469,7 +470,6 @@ CONST_STRING_DECL(_kCFSystemVersionProductVersionStringKey, "Version")
CONST_STRING_DECL(_kCFSystemVersionBuildStringKey, "Build")
#endif


CF_EXPORT Boolean _CFExecutableLinkedOnOrAfter(CFSystemVersion version) {
return true;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1701,4 +1702,4 @@ int32_t __CFGetPid() {
return getpid();
}

#endif // DEPLOYMENT_RUNTIME_SWIFT && !TARGET_OS_WASI
#endif
4 changes: 4 additions & 0 deletions CoreFoundation/Base.subproj/CoreFoundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include <limits.h>
#include <locale.h>
#include <math.h>
#if !defined(__wasi__)
#include <setjmp.h>
#endif
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
Expand Down Expand Up @@ -56,7 +58,9 @@
#include <CoreFoundation/CFLocale.h>
#include <CoreFoundation/CFNumber.h>
#include <CoreFoundation/CFNumberFormatter.h>
#if !TARGET_OS_WASI
#include <CoreFoundation/CFPreferences.h>
#endif
#include <CoreFoundation/CFPropertyList.h>
#include <CoreFoundation/CFSet.h>
#include <CoreFoundation/CFString.h>
Expand Down
10 changes: 9 additions & 1 deletion CoreFoundation/Base.subproj/ForFoundationOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
#include <CoreFoundation/CFNumberFormatter.h>
#include <CoreFoundation/CFBag.h>
#include <CoreFoundation/CFCalendar.h>

#if !TARGET_OS_WASI
#include <CoreFoundation/CFStreamPriv.h>
#include <CoreFoundation/CFRuntime.h>
#endif
#include <math.h>
#include <limits.h>

Expand Down Expand Up @@ -60,6 +63,7 @@ CF_IMPLICIT_BRIDGING_DISABLED
#include <objc/message.h>
#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.
Expand All @@ -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 <CoreFoundation/CFBundlePriv.h>
Expand All @@ -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

Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
31 changes: 29 additions & 2 deletions CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#elif !TARGET_OS_WASI
#include <fts.h>
#endif
#if __has_include(<unistd.h>)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions CoreFoundation/Base.subproj/SwiftRuntime/CoreFoundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
#include <limits.h>
#include <locale.h>
#include <math.h>
#if !defined(__wasi__)
#include <setjmp.h>
#endif
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
Expand All @@ -43,7 +45,7 @@
#include <netdb.h> // for Host.swift
#endif

#if __has_include(<ifaddrs.h>)
#if __has_include(<ifaddrs.h>) && !defined(__wasi__)
#include <ifaddrs.h> // for Host.swift
#endif

Expand Down Expand Up @@ -73,7 +75,6 @@
#include <CoreFoundation/CFLocale.h>
#include <CoreFoundation/CFNumber.h>
#include <CoreFoundation/CFNumberFormatter.h>
#include <CoreFoundation/CFPreferences.h>
#include <CoreFoundation/CFPropertyList.h>
#include <CoreFoundation/CFSet.h>
#include <CoreFoundation/CFString.h>
Expand All @@ -84,14 +85,17 @@
#include <CoreFoundation/CFURLAccess.h>
#include <CoreFoundation/CFUUID.h>
#include <CoreFoundation/CFUtilities.h>
#include <CoreFoundation/CFBundle.h>

#include <CoreFoundation/CFMessagePort.h>
#if !TARGET_OS_WASI
#include <CoreFoundation/CFBundle.h>
#include <CoreFoundation/CFPlugIn.h>
#include <CoreFoundation/CFMessagePort.h>
#include <CoreFoundation/CFPreferences.h>
#include <CoreFoundation/CFRunLoop.h>
#include <CoreFoundation/CFStream.h>
#include <CoreFoundation/CFSocket.h>
#include <CoreFoundation/CFMachPort.h>
#endif

#include <CoreFoundation/CFAttributedString.h>
#include <CoreFoundation/CFNotificationCenter.h>
Expand Down