Skip to content
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

Ensure threadsafe access to mutable MTRDeviceController_XPC state. #37875

Merged
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
20 changes: 19 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ @interface MTRDeviceController_XPC ()

// #define MTR_HAVE_MACH_SERVICE_NAME_CONSTRUCTOR

@implementation MTRDeviceController_XPC
@implementation MTRDeviceController_XPC {
// Protects access to the data set in controllerConfigurationUpdated:
os_unfair_lock _configurationLock;
}

#pragma mark - Node ID Management

Expand Down Expand Up @@ -119,8 +122,20 @@ - (void)forgetDeviceWithNodeID:(NSNumber *)nodeID
}

#pragma mark - XPC

@synthesize controllerNodeID = _controllerNodeID;
- (nullable NSNumber *)controllerNodeID
{
std::lock_guard lock(_configurationLock);
return _controllerNodeID;
}

@synthesize compressedFabricID = _compressedFabricID;
- (nullable NSNumber *)compressedFabricID
{
std::lock_guard lock(_configurationLock);
return _compressedFabricID;
}

+ (NSMutableSet *)_allowedClasses
{
Expand Down Expand Up @@ -345,6 +360,7 @@ - (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParamete
self.uniqueIdentifier = UUID;
self.xpcParameters = xpcParameters;
_workQueue = dispatch_queue_create("MTRDeviceController_XPC_queue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
_configurationLock = OS_UNFAIR_LOCK_INIT;

if (![self _setupXPCConnection]) {
return nil;
Expand Down Expand Up @@ -490,6 +506,8 @@ - (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(N

NSDictionary * controllerContext = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationControllerContextKey], NSDictionary);
if (controllerContext) {
std::lock_guard lock(_configurationLock);

NSNumber * controllerNodeID = MTR_SAFE_CAST(controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey], NSNumber);
if (controllerNodeID) {
_controllerNodeID = controllerNodeID;
Expand Down
Loading