未验证 提交 20f4ff43 编写于 作者: A Alexander Köplinger 提交者: GitHub

Enable Automatic Reference Counting (ARC) for Objective-C code that needs it (#90379)

We noticed that ARC isn't enabled for System.Globalization.Native code but we aren't properly doing manual memory management by calling `release`.

Enable ARC for the System.Globalization.Native ObjC code so it is handled automatically by the compiler.
Disable it for System.Native code since that one is less complex and we can manually manage it.

We need to wrap all public entrypoints to the library in `@autoreleasepool` blocks so that the releasing of autoreleased objects occurs at the end of the block, rather than at some undetermined point in the future (and we don't even know if we have an autorelease pool in the thread).
上级 55828b9a
......@@ -83,7 +83,7 @@ if(HAVE_SYS_ICU)
set_source_files_properties(${icu_shim_sources} PROPERTIES COMPILE_DEFINITIONS OSX_ICU_LIBRARY_PATH="${OSX_ICU_LIBRARY_PATH}")
set_source_files_properties(${icu_shim_sources} PROPERTIES COMPILE_FLAGS "-I\"${ICU_INCLUDEDIR}\" -I\"${CLR_SRC_NATIVE_DIR}/libs/System.Globalization.Native/\" -I\"${CLR_SRC_NATIVE_DIR}/libs/Common/\" ${ICU_FLAGS}")
if(TARGET_DARWIN)
set_property(SOURCE ${icu_shim_darwin_sources} APPEND_STRING PROPERTY COMPILE_FLAGS "${CLR_CMAKE_COMMON_OBJC_FLAGS}")
set_property(SOURCE ${icu_shim_darwin_sources} APPEND_STRING PROPERTY COMPILE_FLAGS " -fobjc-arc ${CLR_CMAKE_COMMON_OBJC_FLAGS}")
endif()
if(TARGET_WIN32)
set_source_files_properties(${icu_shim_sources} PROPERTIES LANGUAGE CXX)
......
......@@ -93,8 +93,13 @@ else()
endif()
if (CLR_CMAKE_TARGET_APPLE)
set(NATIVEGLOBALIZATION_SOURCES ${NATIVEGLOBALIZATION_SOURCES} pal_locale.m pal_collation.m pal_casing.m pal_calendarData.m)
set_source_files_properties(pal_locale.m pal_collation.m pal_casing.m pal_calendarData.m PROPERTIES COMPILE_FLAGS "${CLR_CMAKE_COMMON_OBJC_FLAGS}")
set(NATIVEGLOBALIZATION_SOURCES_OBJC
pal_locale.m
pal_collation.m
pal_casing.m
pal_calendarData.m)
set_source_files_properties(${NATIVEGLOBALIZATION_SOURCES_OBJC} PROPERTIES COMPILE_FLAGS "-fobjc-arc ${CLR_CMAKE_COMMON_OBJC_FLAGS}")
set(NATIVEGLOBALIZATION_SOURCES ${NATIVEGLOBALIZATION_SOURCES} ${NATIVEGLOBALIZATION_SOURCES_OBJC})
endif()
# time zone names are filtered out of icu data for the browser and associated functionality is disabled
......
......@@ -6,6 +6,10 @@
#include "pal_calendarData.h"
#import <Foundation/Foundation.h>
#if !__has_feature(objc_arc)
#error This file relies on ARC for memory management, but ARC is not enabled.
#endif
#if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS)
/*
......@@ -55,6 +59,8 @@ with the requested value.
*/
const char* GlobalizationNative_GetCalendarInfoNative(const char* localeName, CalendarId calendarId, CalendarDataType dataType)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
......@@ -133,5 +139,6 @@ const char* GlobalizationNative_GetCalendarInfoNative(const char* localeName, Ca
NSString *arrayToString = [[result valueForKey:@"description"] componentsJoinedByString:@"||"];
return arrayToString ? strdup([arrayToString UTF8String]) : NULL;
}
}
#endif
......@@ -7,6 +7,10 @@
#import <Foundation/Foundation.h>
#if !__has_feature(objc_arc)
#error This file relies on ARC for memory management, but ARC is not enabled.
#endif
#if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS)
/**
......@@ -97,6 +101,8 @@ Returns 0 for success, non-zero on failure see ErrorCodes.
int32_t GlobalizationNative_ChangeCaseNative(const uint16_t* localeName, int32_t lNameLength,
const uint16_t* lpSrc, int32_t cwSrcLength, uint16_t* lpDst, int32_t cwDstLength, int32_t bToUpper)
{
@autoreleasepool
{
NSLocale *currentLocale;
if(localeName == NULL || lNameLength == 0)
{
......@@ -130,6 +136,7 @@ int32_t GlobalizationNative_ChangeCaseNative(const uint16_t* localeName, int32_t
return isError;
}
return Success;
}
}
/*
......@@ -146,6 +153,8 @@ Returns 0 for success, non-zero on failure see ErrorCodes.
*/
int32_t GlobalizationNative_ChangeCaseInvariantNative(const uint16_t* lpSrc, int32_t cwSrcLength, uint16_t* lpDst, int32_t cwDstLength, int32_t bToUpper)
{
@autoreleasepool
{
int32_t srcIdx = 0, dstIdx = 0, isError = 0;
uint16_t dstCodepoint;
while (srcIdx < cwSrcLength)
......@@ -168,6 +177,7 @@ int32_t GlobalizationNative_ChangeCaseInvariantNative(const uint16_t* lpSrc, int
return isError;
}
return Success;
}
}
#endif
......@@ -7,6 +7,10 @@
#import <Foundation/Foundation.h>
#if !__has_feature(objc_arc)
#error This file relies on ARC for memory management, but ARC is not enabled.
#endif
#if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS)
// Enum that corresponds to C# CompareOptions
......@@ -70,6 +74,8 @@ CompareString
int32_t GlobalizationNative_CompareStringNative(const uint16_t* localeName, int32_t lNameLength, const uint16_t* lpSource, int32_t cwSourceLength,
const uint16_t* lpTarget, int32_t cwTargetLength, int32_t comparisonOptions)
{
@autoreleasepool
{
NSLocale *currentLocale = GetCurrentLocale(localeName, lNameLength);
NSString *sourceString = [NSString stringWithCharacters: lpSource length: cwSourceLength];
NSString *sourceStrPrecomposed = sourceString.precomposedStringWithCanonicalMapping;
......@@ -87,6 +93,7 @@ int32_t GlobalizationNative_CompareStringNative(const uint16_t* localeName, int3
options:options
range:comparisonRange
locale:currentLocale];
}
}
static NSString* RemoveWeightlessCharacters(NSString* source)
......@@ -120,6 +127,8 @@ Find detailed explanation how this function works in https://github.com/dotnet/r
Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNameLength, const uint16_t* lpTarget, int32_t cwTargetLength,
const uint16_t* lpSource, int32_t cwSourceLength, int32_t comparisonOptions, int32_t fromBeginning)
{
@autoreleasepool
{
assert(cwTargetLength >= 0);
Range result = {ERROR_INDEX_NOT_FOUND, 0};
NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions);
......@@ -223,6 +232,7 @@ Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNam
}
return result;
}
}
/*
......@@ -231,6 +241,8 @@ Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNam
int32_t GlobalizationNative_StartsWithNative(const uint16_t* localeName, int32_t lNameLength, const uint16_t* lpPrefix, int32_t cwPrefixLength,
const uint16_t* lpSource, int32_t cwSourceLength, int32_t comparisonOptions)
{
@autoreleasepool
{
NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions);
// in case mapping is not found
......@@ -250,6 +262,7 @@ int32_t GlobalizationNative_StartsWithNative(const uint16_t* localeName, int32_t
range:sourceRange
locale:currentLocale];
return result == NSOrderedSame ? 1 : 0;
}
}
/*
......@@ -258,6 +271,8 @@ int32_t GlobalizationNative_StartsWithNative(const uint16_t* localeName, int32_t
int32_t GlobalizationNative_EndsWithNative(const uint16_t* localeName, int32_t lNameLength, const uint16_t* lpSuffix, int32_t cwSuffixLength,
const uint16_t* lpSource, int32_t cwSourceLength, int32_t comparisonOptions)
{
@autoreleasepool
{
NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions);
// in case mapping is not found
......@@ -277,6 +292,7 @@ int32_t GlobalizationNative_EndsWithNative(const uint16_t* localeName, int32_t l
range:sourceRange
locale:currentLocale];
return result == NSOrderedSame ? 1 : 0;
}
}
#endif
......@@ -10,6 +10,10 @@
#import <Foundation/Foundation.h>
#import <Foundation/NSFormatter.h>
#if !__has_feature(objc_arc)
#error This file relies on ARC for memory management, but ARC is not enabled.
#endif
char* DetectDefaultAppleLocaleName(void)
{
NSLocale *currentLocale = [NSLocale currentLocale];
......@@ -36,10 +40,13 @@ char* DetectDefaultAppleLocaleName(void)
const char* GlobalizationNative_GetLocaleNameNative(const char* localeName)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
const char* value = [currentLocale.localeIdentifier UTF8String];
return strdup(value);
}
}
/**
......@@ -88,6 +95,8 @@ static void GetParent(const char* localeID, char* parent, int32_t parentCapacity
const char* GlobalizationNative_GetLocaleInfoStringNative(const char* localeName, LocaleStringData localeStringData)
{
@autoreleasepool
{
const char* value;
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
......@@ -222,6 +231,7 @@ const char* GlobalizationNative_GetLocaleInfoStringNative(const char* localeName
}
return value ? strdup(value) : "";
}
}
// invariant character definitions
......@@ -463,6 +473,8 @@ static int32_t GetValueForNumberFormat(NSLocale *currentLocale, LocaleNumberData
int32_t GlobalizationNative_GetLocaleInfoIntNative(const char* localeName, LocaleNumberData localeNumberData)
{
@autoreleasepool
{
#ifndef NDEBUG
bool isSuccess = true;
#endif
......@@ -561,6 +573,7 @@ int32_t GlobalizationNative_GetLocaleInfoIntNative(const char* localeName, Local
assert(isSuccess);
return value;
}
}
/*
......@@ -571,6 +584,8 @@ Returns primary grouping size for decimal and currency
*/
int32_t GlobalizationNative_GetLocaleInfoPrimaryGroupingSizeNative(const char* localeName, LocaleNumberData localeGroupingData)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
......@@ -589,6 +604,7 @@ int32_t GlobalizationNative_GetLocaleInfoPrimaryGroupingSizeNative(const char* l
break;
}
return [numberFormatter groupingSize];
}
}
/*
......@@ -599,6 +615,8 @@ Returns secondary grouping size for decimal and currency
*/
int32_t GlobalizationNative_GetLocaleInfoSecondaryGroupingSizeNative(const char* localeName, LocaleNumberData localeGroupingData)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
......@@ -618,6 +636,7 @@ int32_t GlobalizationNative_GetLocaleInfoSecondaryGroupingSizeNative(const char*
}
return [numberFormatter secondaryGroupingSize];
}
}
/*
......@@ -628,6 +647,8 @@ Returns time format information (in native format, it needs to be converted to .
*/
const char* GlobalizationNative_GetLocaleTimeFormatNative(const char* localeName, int shortFormat)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
......@@ -643,6 +664,7 @@ const char* GlobalizationNative_GetLocaleTimeFormatNative(const char* localeName
}
return strdup([[dateFormatter dateFormat] UTF8String]);
}
}
#endif
......@@ -650,15 +672,21 @@ const char* GlobalizationNative_GetLocaleTimeFormatNative(const char* localeName
#if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS)
const char* GlobalizationNative_GetICUDataPathRelativeToAppBundleRoot(const char* path)
{
@autoreleasepool
{
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *dataPath = [bundlePath stringByAppendingPathComponent: [NSString stringWithFormat:@"%s", path]];
return strdup([dataPath UTF8String]);
}
}
const char* GlobalizationNative_GetICUDataPathFallback(void)
{
@autoreleasepool
{
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"icudt" ofType:@"dat"];
return strdup([dataPath UTF8String]);
}
}
#endif
......@@ -67,56 +67,45 @@ else()
endif()
if (CLR_CMAKE_TARGET_APPLE)
list (APPEND NATIVE_SOURCES pal_autoreleasepool.m)
set_source_files_properties(pal_autoreleasepool.m PROPERTIES COMPILE_FLAGS "-fno-objc-arc ${CLR_CMAKE_COMMON_OBJC_FLAGS}")
else()
list (APPEND NATIVE_SOURCES pal_autoreleasepool.c)
endif()
if (CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
list (APPEND NATIVE_SOURCES pal_environment.m)
set_source_files_properties(pal_environment.m PROPERTIES COMPILE_FLAGS "${CLR_CMAKE_COMMON_OBJC_FLAGS}")
else()
list (APPEND NATIVE_SOURCES pal_environment.c)
endif()
if (CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
set(NATIVE_SOURCES ${NATIVE_SOURCES}
list (APPEND NATIVE_SOURCES_OBJC_NO_ARC
pal_autoreleasepool.m
pal_environment.m
pal_searchpath.m
pal_datetime.m)
set_source_files_properties(pal_datetime.m PROPERTIES COMPILE_FLAGS "${CLR_CMAKE_COMMON_OBJC_FLAGS}")
endif()
if (CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
set(NATIVE_SOURCES ${NATIVE_SOURCES}
pal_log.m
pal_searchpath.m)
set_source_files_properties(pal_log.m pal_searchpath.m PROPERTIES COMPILE_FLAGS "${CLR_CMAKE_COMMON_OBJC_FLAGS}")
elseif (CLR_CMAKE_TARGET_OSX)
list (APPEND NATIVE_SOURCES
pal_searchpath.m
pal_console.c
pal_log.c)
set_source_files_properties(pal_searchpath.m PROPERTIES COMPILE_FLAGS "${CLR_CMAKE_COMMON_OBJC_FLAGS}")
elseif (CLR_CMAKE_TARGET_WASI)
list (APPEND NATIVE_SOURCES
pal_searchpath.c
pal_console_wasi.c
pal_log.c)
else ()
if (CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
list (APPEND NATIVE_SOURCES_OBJC_NO_ARC pal_log.m)
if (CLR_CMAKE_TARGET_MACCATALYST)
list (APPEND NATIVE_SOURCES_OBJC_NO_ARC pal_iossupportversion.m)
else()
list (APPEND NATIVE_SOURCES pal_iossupportversion.c)
endif ()
elseif (CLR_CMAKE_TARGET_OSX)
list (APPEND NATIVE_SOURCES
pal_searchpath.c
pal_console.c
pal_log.c)
endif ()
pal_log.c
pal_iossupportversion.c)
else()
message(FATAL_ERROR "Unsupported Apple platform")
endif ()
if (CLR_CMAKE_TARGET_MACCATALYST)
set(NATIVE_SOURCES ${NATIVE_SOURCES}
pal_iossupportversion.m)
set_source_files_properties(pal_iossupportversion.m PROPERTIES COMPILE_FLAGS "${CLR_CMAKE_COMMON_OBJC_FLAGS}")
else ()
set_source_files_properties(${NATIVE_SOURCES_OBJC_NO_ARC} PROPERTIES COMPILE_FLAGS "-fno-objc-arc ${CLR_CMAKE_COMMON_OBJC_FLAGS}")
list (APPEND NATIVE_SOURCES ${NATIVE_SOURCES_OBJC_NO_ARC})
else()
list (APPEND NATIVE_SOURCES
pal_autoreleasepool.c
pal_environment.c
pal_searchpath.c
pal_log.c
pal_iossupportversion.c)
endif ()
if (CLR_CMAKE_TARGET_WASI)
list (APPEND NATIVE_SOURCES pal_console_wasi.c)
else()
list (APPEND NATIVE_SOURCES pal_console.c)
endif()
endif()
if (NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI)
list (APPEND NATIVE_SOURCES pal_networkchange.c)
......
......@@ -48,6 +48,7 @@ static const Entry s_sysNative[] =
DllImportEntry(SystemNative_GetSignalForBreak)
DllImportEntry(SystemNative_SetSignalForBreak)
DllImportEntry(SystemNative_GetSystemTimeAsTicks)
DllImportEntry(SystemNative_GetDefaultTimeZone)
DllImportEntry(SystemNative_GetTimeZoneData)
DllImportEntry(SystemNative_ConvertErrorPlatformToPal)
DllImportEntry(SystemNative_ConvertErrorPalToPlatform)
......
......@@ -5,6 +5,10 @@
#include <Foundation/Foundation.h>
#include <objc/runtime.h>
#if __has_feature(objc_arc)
#error This file uses manual memory management and must not use ARC, but ARC is enabled.
#endif
void EnsureNSThreadIsMultiThreaded(void)
{
if (![NSThread isMultiThreaded])
......
......@@ -61,6 +61,12 @@ char* SystemNative_GetDefaultTimeZone(void)
return NULL;
}
}
#elif !defined(__APPLE__)
char* SystemNative_GetDefaultTimeZone(void)
{
assert_err(false, "This function is not supported on this platform.", EINVAL);
return NULL;
}
#endif
const char* SystemNative_GetTimeZoneData(const char* name, int* length)
......
......@@ -8,8 +8,6 @@
PALEXPORT int64_t SystemNative_GetSystemTimeAsTicks(void);
#if defined(TARGET_ANDROID) || defined(__APPLE__)
PALEXPORT char* SystemNative_GetDefaultTimeZone(void);
#endif
PALEXPORT const char* SystemNative_GetTimeZoneData(const char* name, int* length);
......@@ -4,6 +4,10 @@
#include "pal_datetime.h"
#import <Foundation/Foundation.h>
#if __has_feature(objc_arc)
#error This file uses manual memory management and must not use ARC, but ARC is enabled.
#endif
char* SystemNative_GetDefaultTimeZone()
{
NSTimeZone *tz = [NSTimeZone localTimeZone];
......
......@@ -9,6 +9,10 @@
#include <objc/runtime.h>
#include <objc/message.h>
#if __has_feature(objc_arc)
#error This file uses manual memory management and must not use ARC, but ARC is enabled.
#endif
char* SystemNative_GetEnv(const char* variable)
{
return getenv(variable);
......
......@@ -5,14 +5,16 @@
#include "pal_autoreleasepool.h"
#import <Foundation/Foundation.h>
#if __has_feature(objc_arc)
#error This file uses manual memory management and must not use ARC, but ARC is enabled.
#endif
const char* SystemNative_iOSSupportVersion()
{
EnsureNSThreadIsMultiThreaded();
@autoreleasepool
{
NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
NSDictionary *plist = [[NSDictionary alloc] initWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
NSString *iOSSupportVersion = (NSString *)[plist objectForKey:@"iOSSupportVersion"];
return strdup([iOSSupportVersion UTF8String]);
}
const char* version = strdup([iOSSupportVersion UTF8String]);
[plist release];
return version;
}
......@@ -4,6 +4,10 @@
#include "pal_log.h"
#import <Foundation/Foundation.h>
#if __has_feature(objc_arc)
#error This file uses manual memory management and must not use ARC, but ARC is enabled.
#endif
void SystemNative_Log (uint8_t* buffer, int32_t length)
{
NSString *msg = [[NSString alloc] initWithBytes: buffer length: length encoding: NSUTF16LittleEndianStringEncoding];
......
......@@ -4,6 +4,10 @@
#include "pal_searchpath.h"
#import <Foundation/Foundation.h>
#if __has_feature(objc_arc)
#error This file uses manual memory management and must not use ARC, but ARC is enabled.
#endif
const char* SystemNative_SearchPath(int32_t folderId)
{
NSSearchPathDirectory spd = (NSSearchPathDirectory) folderId;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册