diff --git a/.github/workflows/darwin.yaml b/.github/workflows/darwin.yaml index 8b7e44ef82ee00..04648f4da135ee 100644 --- a/.github/workflows/darwin.yaml +++ b/.github/workflows/darwin.yaml @@ -111,8 +111,8 @@ jobs: # -enableUndefinedBehaviorSanitizer instruments the code in Matter.framework, # but to instrument the code in the underlying libCHIP we need to pass CHIP_IS_UBSAN=YES TEST_RUNNER_ASAN_OPTIONS=__CURRENT_VALUE__:detect_stack_use_after_return=1 xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableAddressSanitizer YES -enableUndefinedBehaviorSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion' CHIP_IS_UBSAN=YES CHIP_IS_BLE=NO GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'> >(tee /tmp/darwin/framework-tests/darwin-tests-asan.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-asan-err.log >&2) - # And the same thing, but with MTR_PER_CONTROLLER_STORAGE_ENABLED turned on. - TEST_RUNNER_ASAN_OPTIONS=__CURRENT_VALUE__:detect_stack_use_after_return=1 xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableAddressSanitizer YES -enableUndefinedBehaviorSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion' CHIP_IS_UBSAN=YES CHIP_IS_BLE=NO GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1 MTR_PER_CONTROLLER_STORAGE_ENABLED=1' > >(tee /tmp/darwin/framework-tests/darwin-tests-asan-controller-storage.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-asan-controller-storage-err.log >&2) + # And the same thing, but with MTR_PER_CONTROLLER_STORAGE_ENABLED turned off, so we test that it does not break for now. + TEST_RUNNER_ASAN_OPTIONS=__CURRENT_VALUE__:detect_stack_use_after_return=1 xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableAddressSanitizer YES -enableUndefinedBehaviorSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion' CHIP_IS_UBSAN=YES CHIP_IS_BLE=NO GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1 MTR_PER_CONTROLLER_STORAGE_ENABLED=0' > >(tee /tmp/darwin/framework-tests/darwin-tests-asan-controller-storage.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-asan-controller-storage-err.log >&2) # And the same thing, but with MTR_ENABLE_PROVISIONAL also turned on. TEST_RUNNER_ASAN_OPTIONS=__CURRENT_VALUE__:detect_stack_use_after_return=1 xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableAddressSanitizer YES -enableUndefinedBehaviorSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion' CHIP_IS_UBSAN=YES CHIP_IS_BLE=NO GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1 MTR_PER_CONTROLLER_STORAGE_ENABLED=1 MTR_ENABLE_PROVISIONAL=1' > >(tee /tmp/darwin/framework-tests/darwin-tests-asan-provisional.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-asan-provisional-err.log >&2) # And the same thing, but with MTR_NO_AVAILABILITY not turned on. This requires -Wno-unguarded-availability-new to avoid availability errors. diff --git a/src/darwin/Framework/CHIP/MTRDefines.h b/src/darwin/Framework/CHIP/MTRDefines.h index add06898ec9080..97d918297327d5 100644 --- a/src/darwin/Framework/CHIP/MTRDefines.h +++ b/src/darwin/Framework/CHIP/MTRDefines.h @@ -71,7 +71,7 @@ #endif #ifndef MTR_PER_CONTROLLER_STORAGE_ENABLED -#define MTR_PER_CONTROLLER_STORAGE_ENABLED 0 +#define MTR_PER_CONTROLLER_STORAGE_ENABLED 1 #endif #pragma mark - Types diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.h b/src/darwin/Framework/CHIP/MTRDeviceController.h index d4e79f594181a3..e830f024d8d155 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.h +++ b/src/darwin/Framework/CHIP/MTRDeviceController.h @@ -24,7 +24,7 @@ @class MTRBaseDevice; #if MTR_PER_CONTROLLER_STORAGE_ENABLED -@class MTRDeviceControllerParameters; +@class MTRDeviceControllerAbstractParameters; #endif // MTR_PER_CONTROLLER_STORAGE_ENABLED NS_ASSUME_NONNULL_BEGIN @@ -58,7 +58,7 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS * Once this returns non-nil, it's the caller's resposibility to call shutdown * on the controller to avoid leaking it. */ -- (nullable instancetype)initWithParameters:(MTRDeviceControllerParameters *)parameters +- (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParameters *)parameters error:(NSError * __autoreleasing *)error MTR_NEWLY_AVAILABLE; #endif // MTR_PER_CONTROLLER_STORAGE_ENABLED diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index d4c5903c93077d..94b9a49173a49d 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm @@ -127,8 +127,17 @@ @interface MTRDeviceController () { @implementation MTRDeviceController -- (nullable instancetype)initWithParameters:(MTRDeviceControllerParameters *)parameters error:(NSError * __autoreleasing *)error +- (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParameters *)parameters error:(NSError * __autoreleasing *)error { + if (![parameters isKindOfClass:MTRDeviceControllerParameters.class]) { + MTR_LOG_ERROR("Unsupported type of MTRDeviceControllerAbstractParameters: %@", parameters); + + if (error) { + *error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT]; + } + return nil; + } + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; if (!factory.isRunning) { auto * params = [[MTRDeviceControllerFactoryParams alloc] initWithoutStorage]; @@ -138,7 +147,9 @@ - (nullable instancetype)initWithParameters:(MTRDeviceControllerParameters *)par } } - return [factory initializeController:self withParameters:parameters error:error]; + auto * parametersForFactory = static_cast(parameters); + + return [factory initializeController:self withParameters:parametersForFactory error:error]; } - (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerParameters.h b/src/darwin/Framework/CHIP/MTRDeviceControllerParameters.h index bcbea7902b6083..b9c72d5598f92b 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerParameters.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerParameters.h @@ -28,14 +28,29 @@ NS_ASSUME_NONNULL_BEGIN +/** + * Parameters that can be used to initialize an MTRDeviceController. Specific + * interfaces inheriting from this one should be used to actually do the + * initialization. + */ #if !MTR_PER_CONTROLLER_STORAGE_ENABLED MTR_HIDDEN #endif MTR_NEWLY_AVAILABLE -@interface MTRDeviceControllerParameters : NSObject - +@interface MTRDeviceControllerAbstractParameters : NSObject - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; +@end + +/** + * Parameters that can be used to initialize an MTRDeviceController which + * has a node identity. + */ +#if !MTR_PER_CONTROLLER_STORAGE_ENABLED +MTR_HIDDEN +#endif +MTR_NEWLY_AVAILABLE +@interface MTRDeviceControllerParameters : MTRDeviceControllerAbstractParameters /** * The Product Attestation Authority certificates that are trusted to sign diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm index 8ad185198b5330..74a9681ca3e619 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm @@ -251,6 +251,13 @@ - (instancetype)initWithOperationalKeypair:(id)operationalKeypair @end +@implementation MTRDeviceControllerAbstractParameters +- (instancetype)_initInternal +{ + return [super init]; +} +@end + @implementation MTRDeviceControllerParameters - (instancetype)initWithStorageDelegate:(id)storageDelegate storageDelegateQueue:(dispatch_queue_t)storageDelegateQueue @@ -262,7 +269,7 @@ - (instancetype)initWithStorageDelegate:(id) intermediateCertificate:(MTRCertificateDERBytes _Nullable)intermediateCertificate rootCertificate:(MTRCertificateDERBytes)rootCertificate { - if (!(self = [super init])) { + if (!(self = [super _initInternal])) { return nil; } diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h index 2850fc78258486..71e6a74f5688f3 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h @@ -52,6 +52,11 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithParams:(MTRDeviceControllerStartupParams *)params; @end +@interface MTRDeviceControllerAbstractParameters () +// Allow init from our subclasses. +- (instancetype)_initInternal; +@end + @interface MTRDeviceControllerParameters () - (instancetype)initWithStorageDelegate:(id)storageDelegate