Skip to content

Commit c5b61f6

Browse files
authored
Merge pull request #3037 from apple/maxd/cf-disable-for-wasi
Disable file/process-related code in CF for WASI
2 parents 3934385 + 15dcad1 commit c5b61f6

File tree

6 files changed

+69
-17
lines changed

6 files changed

+69
-17
lines changed

CoreFoundation/Base.subproj/CFPriv.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@
4646
#include <CoreFoundation/CFMessagePort.h>
4747
#endif
4848

49+
#if !TARGET_OS_WASI
4950
#include <CoreFoundation/CFRunLoop.h>
5051
#include <CoreFoundation/CFSocket.h>
52+
#endif
5153
#include <CoreFoundation/CFBundlePriv.h>
5254

5355
CF_EXTERN_C_BEGIN
5456

5557
CF_EXPORT void _CFRuntimeSetCFMPresent(void *a);
5658

59+
#if !TARGET_OS_WASI
5760
CF_EXPORT const char *_CFProcessPath(void);
5861
CF_EXPORT const char **_CFGetProcessPath(void);
5962
CF_EXPORT const char **_CFGetProgname(void);
@@ -65,6 +68,7 @@ CF_EXPORT void _CFGetUGIDs(uid_t *euid, gid_t *egid);
6568
CF_EXPORT uid_t _CFGetEUID(void);
6669
CF_EXPORT uid_t _CFGetEGID(void);
6770
#endif
71+
#endif
6872

6973
#if (TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_LINUX))
7074
CF_EXPORT void _CFRunLoopSetCurrent(CFRunLoopRef rl);
@@ -161,6 +165,7 @@ CF_EXPORT Boolean _CFStringGetFileSystemRepresentation(CFStringRef string, UInt8
161165
/* If this is publicized, we might need to create a GetBytesPtr type function as well. */
162166
CF_EXPORT CFStringRef _CFStringCreateWithBytesNoCopy(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean externalFormat, CFAllocatorRef contentsDeallocator);
163167

168+
#if !TARGET_OS_WASI
164169
/* These return NULL on MacOS 8 */
165170
// This one leaks the returned string in order to be thread-safe.
166171
// CF cannot help you in this matter if you continue to use this SPI.
@@ -172,6 +177,7 @@ CFStringRef CFCopyUserName(void);
172177

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

176182

177183
/*
@@ -630,8 +636,10 @@ typedef CF_OPTIONS(CFOptionFlags, _CFBundleFilteredPlistOptions) {
630636
_CFBundleFilteredPlistMemoryMapped = 1
631637
} API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
632638

639+
#if !TARGET_OS_WASI
633640
CF_EXPORT CFPropertyListRef _CFBundleCreateFilteredInfoPlist(CFBundleRef bundle, CFSetRef keyPaths, _CFBundleFilteredPlistOptions options) API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
634641
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));
642+
#endif
635643

636644
#if TARGET_OS_WIN32
637645
#include <CoreFoundation/CFNotificationCenter.h>

CoreFoundation/Base.subproj/CFUtilities.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
#endif
9696

9797
CF_PRIVATE os_log_t _CFOSLog(void) {
98-
static os_log_t logger;
98+
static os_log_t logger = NULL;
99+
99100
static dispatch_once_t onceToken;
100101
dispatch_once(&onceToken, ^{
101102
logger = os_log_create("com.apple.foundation", "general");
@@ -469,7 +470,6 @@ CONST_STRING_DECL(_kCFSystemVersionProductVersionStringKey, "Version")
469470
CONST_STRING_DECL(_kCFSystemVersionBuildStringKey, "Build")
470471
#endif
471472

472-
473473
CF_EXPORT Boolean _CFExecutableLinkedOnOrAfter(CFSystemVersion version) {
474474
return true;
475475
}
@@ -694,7 +694,7 @@ CF_EXPORT uid_t _CFGetEGID(void) {
694694
__CFGetUGIDs(NULL, &egid);
695695
return egid;
696696
}
697-
#endif // !TARGET_OS_WASI
697+
#endif
698698

699699
const char *_CFPrintForDebugger(const void *obj) {
700700
static char *result = NULL;
@@ -1088,12 +1088,13 @@ CF_PRIVATE void _CFLogSimple(int32_t lev, char *format, ...) {
10881088

10891089
void CFLog(int32_t lev, CFStringRef format, ...) {
10901090
va_list args;
1091-
va_start(args, format);
1092-
#if TARGET_OS_WASI
1093-
_CFLogvEx3(NULL, NULL, NULL, NULL, lev, format, args, NULL);
1094-
#else
1091+
va_start(args, format);
1092+
1093+
#if !TARGET_OS_WASI
10951094
_CFLogvEx3(NULL, NULL, NULL, NULL, lev, format, args, __builtin_return_address(0));
1096-
#endif
1095+
#else
1096+
_CFLogvEx3(NULL, NULL, NULL, NULL, lev, format, args, NULL);
1097+
#endif
10971098
va_end(args);
10981099
}
10991100

@@ -1649,7 +1650,7 @@ CFDictionaryRef __CFGetEnvironment() {
16491650
#if TARGET_OS_MAC
16501651
extern char ***_NSGetEnviron(void);
16511652
char **envp = *_NSGetEnviron();
1652-
#elif TARGET_OS_BSD || TARGET_OS_CYGWIN
1653+
#elif TARGET_OS_BSD || TARGET_OS_CYGWIN || TARGET_OS_WASI
16531654
extern char **environ;
16541655
char **envp = environ;
16551656
#elif TARGET_OS_LINUX
@@ -1701,4 +1702,4 @@ int32_t __CFGetPid() {
17011702
return getpid();
17021703
}
17031704

1704-
#endif // DEPLOYMENT_RUNTIME_SWIFT && !TARGET_OS_WASI
1705+
#endif

CoreFoundation/Base.subproj/CoreFoundation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include <limits.h>
2323
#include <locale.h>
2424
#include <math.h>
25+
#if !defined(__wasi__)
2526
#include <setjmp.h>
27+
#endif
2628
#include <signal.h>
2729
#include <stddef.h>
2830
#include <stdio.h>
@@ -56,7 +58,9 @@
5658
#include <CoreFoundation/CFLocale.h>
5759
#include <CoreFoundation/CFNumber.h>
5860
#include <CoreFoundation/CFNumberFormatter.h>
61+
#if !TARGET_OS_WASI
5962
#include <CoreFoundation/CFPreferences.h>
63+
#endif
6064
#include <CoreFoundation/CFPropertyList.h>
6165
#include <CoreFoundation/CFSet.h>
6266
#include <CoreFoundation/CFString.h>

CoreFoundation/Base.subproj/ForFoundationOnly.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727
#include <CoreFoundation/CFNumberFormatter.h>
2828
#include <CoreFoundation/CFBag.h>
2929
#include <CoreFoundation/CFCalendar.h>
30+
31+
#if !TARGET_OS_WASI
3032
#include <CoreFoundation/CFStreamPriv.h>
3133
#include <CoreFoundation/CFRuntime.h>
34+
#endif
3235
#include <math.h>
3336
#include <limits.h>
3437

@@ -60,6 +63,7 @@ CF_IMPLICIT_BRIDGING_DISABLED
6063
#include <objc/message.h>
6164
#endif
6265

66+
#if __BLOCKS__
6367
/* 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.)
6468
6569
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
6872
*/
6973
CF_EXPORT void *_Nonnull __CFSafelyReallocate(void * _Nullable destination, size_t newCapacity, void (^_Nullable reallocationFailureHandler)(void *_Nonnull original, bool *_Nonnull outRecovered));
7074
CF_EXPORT void *_Nonnull __CFSafelyReallocateWithAllocator(CFAllocatorRef _Nullable, void * _Nullable destination, size_t newCapacity, CFOptionFlags options, void (^_Nullable reallocationFailureHandler)(void *_Nonnull original, bool *_Nonnull outRecovered));
75+
#endif
7176

72-
77+
#if !TARGET_OS_WASI
7378
#pragma mark - CFBundle
7479

7580
#include <CoreFoundation/CFBundlePriv.h>
@@ -92,6 +97,7 @@ CF_EXPORT Boolean _CFBundleLoadExecutableAndReturnError(CFBundleRef bundle, Bool
9297
CF_EXPORT CFErrorRef _CFBundleCreateError(CFAllocatorRef _Nullable allocator, CFBundleRef bundle, CFIndex code);
9398

9499
_CF_EXPORT_SCOPE_END
100+
#endif
95101

96102
#pragma mark - CFUUID
97103

@@ -576,6 +582,7 @@ CF_CROSS_PLATFORM_EXPORT void _CFURLInitWithFileSystemPathRelativeToBase(CFURLRe
576582
CF_CROSS_PLATFORM_EXPORT Boolean _CFURLInitWithURLString(CFURLRef url, CFStringRef string, Boolean checkForLegalCharacters, _Nullable CFURLRef baseURL);
577583
CF_CROSS_PLATFORM_EXPORT Boolean _CFURLInitAbsoluteURLWithBytes(CFURLRef url, const UInt8 *relativeURLBytes, CFIndex length, CFStringEncoding encoding, _Nullable CFURLRef baseURL);
578584

585+
#if !TARGET_OS_WASI
579586
CF_EXPORT Boolean _CFRunLoopFinished(CFRunLoopRef rl, CFStringRef mode);
580587
CF_EXPORT CFTypeRef _CFRunLoopGet2(CFRunLoopRef rl);
581588
CF_EXPORT Boolean _CFRunLoopIsCurrent(CFRunLoopRef rl);
@@ -586,6 +593,7 @@ CF_EXPORT void _CFWriteStreamInitialize(CFWriteStreamRef writeStream);
586593
CF_EXPORT void _CFReadStreamDeallocate(CFReadStreamRef readStream);
587594
CF_EXPORT void _CFWriteStreamDeallocate(CFWriteStreamRef writeStream);
588595
CF_EXPORT CFReadStreamRef CFReadStreamCreateWithData(_Nullable CFAllocatorRef alloc, CFDataRef data);
596+
#endif
589597

590598
#if TARGET_OS_MAC
591599
typedef struct {

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#define VC_EXTRALEAN
3636
#define WIN32_LEAN_AND_MEAN
3737
#include <Windows.h>
38-
#else
38+
#elif !TARGET_OS_WASI
3939
#include <fts.h>
4040
#endif
4141
#if __has_include(<unistd.h>)
@@ -198,9 +198,11 @@ struct _NSMutableStringBridge {
198198
void (*_cfAppendCString)(CFTypeRef str, const char *chars, CFIndex appendLength);
199199
};
200200

201+
#if !TARGET_OS_WASI
201202
struct _NSRunLoop {
202203
_Nonnull CFTypeRef (*_Nonnull _new)(CFRunLoopRef rl);
203204
};
205+
#endif
204206

205207
struct _NSCharacterSetBridge {
206208
_Nullable CFCharacterSetRef (*_Nonnull _expandedCFCharacterSet)(CFTypeRef cset);
@@ -276,7 +278,9 @@ struct _CFSwiftBridge {
276278
struct _NSMutableSetBridge NSMutableSet;
277279
struct _NSStringBridge NSString;
278280
struct _NSMutableStringBridge NSMutableString;
281+
#if !TARGET_OS_WASI
279282
struct _NSRunLoop NSRunLoop;
283+
#endif
280284
struct _NSCharacterSetBridge NSCharacterSet;
281285
struct _NSMutableCharacterSetBridge NSMutableCharacterSet;
282286
struct _NSNumberBridge NSNumber;
@@ -375,7 +379,10 @@ CF_PRIVATE uint64_t __CFMemorySize(void);
375379
CF_PRIVATE CFIndex __CFActiveProcessorCount(void);
376380
CF_CROSS_PLATFORM_EXPORT CFStringRef CFCopyFullUserName(void);
377381

382+
#if !TARGET_OS_WASI
378383
extern CFWriteStreamRef _CFWriteStreamCreateFromFileDescriptor(CFAllocatorRef alloc, int fd);
384+
#endif
385+
379386
#if !__COREFOUNDATION_FORFOUNDATIONONLY__
380387
typedef const struct __CFKeyedArchiverUID * CFKeyedArchiverUIDRef;
381388
extern CFTypeID _CFKeyedArchiverUIDGetTypeID(void);
@@ -385,7 +392,10 @@ extern uint32_t _CFKeyedArchiverUIDGetValue(CFKeyedArchiverUIDRef uid);
385392

386393
extern CFIndex __CFBinaryPlistWriteToStream(CFPropertyListRef plist, CFTypeRef stream);
387394
CF_CROSS_PLATFORM_EXPORT CFDataRef _CFPropertyListCreateXMLDataWithExtras(CFAllocatorRef allocator, CFPropertyListRef propertyList);
395+
396+
#if !TARGET_OS_WASI
388397
extern CFWriteStreamRef _CFWriteStreamCreateFromFileDescriptor(CFAllocatorRef alloc, int fd);
398+
#endif
389399

390400
CF_EXPORT char *_Nullable *_Nonnull _CFEnviron(void);
391401

@@ -409,7 +419,9 @@ CF_EXPORT _CFThreadRef _CFMainPThread;
409419

410420
CF_EXPORT CFHashCode __CFHashDouble(double d);
411421

422+
#if __BLOCKS__
412423
CF_CROSS_PLATFORM_EXPORT void CFSortIndexes(CFIndex *indexBuffer, CFIndex count, CFOptionFlags opts, CFComparisonResult (^cmp)(CFIndex, CFIndex));
424+
#endif
413425

414426
CF_EXPORT CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key);
415427
CF_EXPORT void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value);
@@ -425,6 +437,7 @@ CF_EXPORT CFCharacterSetRef _CFCharacterSetCreateCopy(CFAllocatorRef alloc, CFCh
425437
CF_EXPORT CFMutableCharacterSetRef _CFCharacterSetCreateMutableCopy(CFAllocatorRef alloc, CFCharacterSetRef theSet);
426438
CF_CROSS_PLATFORM_EXPORT void _CFCharacterSetInitCopyingSet(CFAllocatorRef alloc, CFMutableCharacterSetRef cset, CFCharacterSetRef theSet, bool isMutable, bool validateSubclasses);
427439

440+
#if !TARGET_OS_WASI
428441
CF_EXPORT _Nullable CFErrorRef CFReadStreamCopyError(CFReadStreamRef _Null_unspecified stream);
429442

430443
CF_EXPORT _Nullable CFErrorRef CFWriteStreamCopyError(CFWriteStreamRef _Null_unspecified stream);
@@ -433,6 +446,7 @@ CF_CROSS_PLATFORM_EXPORT CFStringRef _Nullable _CFBundleCopyExecutablePath(CFBun
433446
CF_CROSS_PLATFORM_EXPORT Boolean _CFBundleSupportsFHSBundles(void);
434447
CF_CROSS_PLATFORM_EXPORT Boolean _CFBundleSupportsFreestandingBundles(void);
435448
CF_CROSS_PLATFORM_EXPORT CFStringRef _Nullable _CFBundleCopyLoadedImagePathForAddress(const void *p);
449+
#endif
436450

437451
CF_CROSS_PLATFORM_EXPORT CFStringRef __CFTimeZoneCopyDataVersionString(void);
438452

@@ -494,6 +508,18 @@ static inline _Bool _resizeConditionalAllocationBuffer(_ConditionalAllocationBuf
494508
return true;
495509
}
496510

511+
#if TARGET_OS_WASI
512+
static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(_ConditionalAllocationBuffer *_Nonnull)) {
513+
_ConditionalAllocationBuffer buffer;
514+
buffer.capacity = amount;
515+
buffer.onStack = false;
516+
buffer.memory = malloc(buffer.capacity);
517+
if (buffer.memory == NULL) { return false; }
518+
applier(&buffer);
519+
free(buffer.memory);
520+
return true;
521+
}
522+
#else
497523
static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(_ConditionalAllocationBuffer *_Nonnull)) {
498524
_ConditionalAllocationBuffer buffer;
499525
#if TARGET_OS_MAC
@@ -510,6 +536,7 @@ static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((n
510536
}
511537
return true;
512538
}
539+
#endif
513540

514541
static inline _Bool _withStackOrHeapBufferWithResultInArguments(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(void *_Nonnull memory, size_t capacity, _Bool onStack)) {
515542
return _withStackOrHeapBuffer(amount, ^(_ConditionalAllocationBuffer *buffer) {
@@ -525,7 +552,7 @@ CF_CROSS_PLATFORM_EXPORT CFIndex __CFCharDigitValue(UniChar ch);
525552

526553
#if TARGET_OS_WIN32
527554
CF_CROSS_PLATFORM_EXPORT int _CFOpenFileWithMode(const unsigned short *path, int opts, mode_t mode);
528-
#else
555+
#elif !TARGET_OS_WASI
529556
CF_CROSS_PLATFORM_EXPORT int _CFOpenFileWithMode(const char *path, int opts, mode_t mode);
530557
#endif
531558
CF_CROSS_PLATFORM_EXPORT void *_CFReallocf(void *ptr, size_t size);

CoreFoundation/Base.subproj/SwiftRuntime/CoreFoundation.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
#include <limits.h>
3232
#include <locale.h>
3333
#include <math.h>
34+
#if !defined(__wasi__)
3435
#include <setjmp.h>
36+
#endif
3537
#include <signal.h>
3638
#include <stddef.h>
3739
#include <stdio.h>
@@ -43,7 +45,7 @@
4345
#include <netdb.h> // for Host.swift
4446
#endif
4547

46-
#if __has_include(<ifaddrs.h>)
48+
#if __has_include(<ifaddrs.h>) && !defined(__wasi__)
4749
#include <ifaddrs.h> // for Host.swift
4850
#endif
4951

@@ -73,7 +75,6 @@
7375
#include <CoreFoundation/CFLocale.h>
7476
#include <CoreFoundation/CFNumber.h>
7577
#include <CoreFoundation/CFNumberFormatter.h>
76-
#include <CoreFoundation/CFPreferences.h>
7778
#include <CoreFoundation/CFPropertyList.h>
7879
#include <CoreFoundation/CFSet.h>
7980
#include <CoreFoundation/CFString.h>
@@ -84,14 +85,17 @@
8485
#include <CoreFoundation/CFURLAccess.h>
8586
#include <CoreFoundation/CFUUID.h>
8687
#include <CoreFoundation/CFUtilities.h>
87-
#include <CoreFoundation/CFBundle.h>
8888

89-
#include <CoreFoundation/CFMessagePort.h>
89+
#if !TARGET_OS_WASI
90+
#include <CoreFoundation/CFBundle.h>
9091
#include <CoreFoundation/CFPlugIn.h>
92+
#include <CoreFoundation/CFMessagePort.h>
93+
#include <CoreFoundation/CFPreferences.h>
9194
#include <CoreFoundation/CFRunLoop.h>
9295
#include <CoreFoundation/CFStream.h>
9396
#include <CoreFoundation/CFSocket.h>
9497
#include <CoreFoundation/CFMachPort.h>
98+
#endif
9599

96100
#include <CoreFoundation/CFAttributedString.h>
97101
#include <CoreFoundation/CFNotificationCenter.h>

0 commit comments

Comments
 (0)