Skip to content

Commit 9ad8ec8

Browse files
Make MTRServerAttribute threadsafe. (project-chip#31970)
* Make MTRServerAttribute threadsafe. If two API clients are both touching the same instance of MTRServerAttribute on different threads, we should handle that correctly. * Address review comments.
1 parent 5a44923 commit 9ad8ec8

File tree

5 files changed

+109
-9
lines changed

5 files changed

+109
-9
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* RAII wrapper around os_unfair_lock.
19+
*/
20+
21+
#import <os/lock.h>
22+
23+
#include <mutex>
24+
25+
template <>
26+
class std::lock_guard<os_unfair_lock>
27+
{
28+
public:
29+
explicit lock_guard(os_unfair_lock & lock) : mLock(lock) { os_unfair_lock_lock(&mLock); }
30+
~lock_guard() { os_unfair_lock_unlock(&mLock); }
31+
32+
lock_guard(const lock_guard &) = delete;
33+
void operator=(const lock_guard &) = delete;
34+
35+
private:
36+
os_unfair_lock & mLock;
37+
};

src/darwin/Framework/CHIP/ServerEndpoint/MTRServerAttribute.mm

+49-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
#import "MTRLogging_Internal.h"
2121
#import "MTRServerAttribute_Internal.h"
2222
#import "MTRServerEndpoint_Internal.h"
23+
#import "MTRUnfairLock.h"
2324
#import "NSDataSpanConversion.h"
25+
2426
#import <Matter/MTRServerAttribute.h>
2527

2628
#include <app/reporting/reporting.h>
@@ -32,7 +34,14 @@
3234

3335
MTR_DIRECT_MEMBERS
3436
@implementation MTRServerAttribute {
37+
// _lock always protects access to _deviceController, _value, and
38+
// _parentCluster. _serializedValue is protected when we are modifying it
39+
// directly while we have no _deviceController. Once we have one,
40+
// _serializedValue is only modified on the Matter thread.
41+
os_unfair_lock _lock;
3542
MTRDeviceController * __weak _deviceController;
43+
NSDictionary<NSString *, id> * _value;
44+
app::ConcreteClusterPath _parentCluster;
3645
}
3746

3847
- (nullable instancetype)initAttributeWithID:(NSNumber *)attributeID initialValue:(NSDictionary<NSString *, id> *)value requiredReadPrivilege:(MTRAccessControlEntryPrivilege)requiredReadPrivilege writable:(BOOL)writable
@@ -66,6 +75,7 @@ - (nullable instancetype)initWithAttributeID:(NSNumber *)attributeID value:(NSDi
6675
return nil;
6776
}
6877

78+
_lock = OS_UNFAIR_LOCK_INIT;
6979
_attributeID = attributeID;
7080
_requiredReadPrivilege = requiredReadPrivilege;
7181
_writable = writable;
@@ -121,7 +131,10 @@ - (BOOL)setValueInternal:(NSDictionary<NSString *, id> *)value logIfNotAssociate
121131
}
122132
}
123133

124-
// We serialized properly, so should be good to go on the value.
134+
// We serialized properly, so should be good to go on the value. Lock
135+
// around our ivar accesses.
136+
std::lock_guard lock(_lock);
137+
125138
_value = [value copy];
126139

127140
MTRDeviceController * deviceController = _deviceController;
@@ -147,8 +160,16 @@ - (BOOL)setValueInternal:(NSDictionary<NSString *, id> *)value logIfNotAssociate
147160
return YES;
148161
}
149162

163+
- (NSDictionary<NSString *, id> *)value
164+
{
165+
std::lock_guard lock(_lock);
166+
return [_value copy];
167+
}
168+
150169
- (BOOL)associateWithController:(nullable MTRDeviceController *)controller
151170
{
171+
std::lock_guard lock(_lock);
172+
152173
MTRDeviceController * existingController = _deviceController;
153174
if (existingController != nil) {
154175
#if MTR_PER_CONTROLLER_STORAGE_ENABLED
@@ -167,7 +188,34 @@ - (BOOL)associateWithController:(nullable MTRDeviceController *)controller
167188

168189
- (void)invalidate
169190
{
191+
std::lock_guard lock(_lock);
192+
170193
_deviceController = nil;
171194
}
172195

196+
- (BOOL)addToCluster:(const app::ConcreteClusterPath &)cluster
197+
{
198+
std::lock_guard lock(_lock);
199+
200+
if (_parentCluster.mClusterId != kInvalidClusterId) {
201+
MTR_LOG_ERROR("Cannot add attribute to cluster " ChipLogFormatMEI "; already added to cluster " ChipLogFormatMEI, ChipLogValueMEI(cluster.mClusterId), ChipLogValueMEI(_parentCluster.mClusterId));
202+
return NO;
203+
}
204+
205+
_parentCluster = cluster;
206+
return YES;
207+
}
208+
209+
- (void)updateParentCluster:(const app::ConcreteClusterPath &)cluster
210+
{
211+
std::lock_guard lock(_lock);
212+
_parentCluster = cluster;
213+
}
214+
215+
- (const chip::app::ConcreteClusterPath &)parentCluster
216+
{
217+
std::lock_guard lock(_lock);
218+
return _parentCluster;
219+
}
220+
173221
@end

src/darwin/Framework/CHIP/ServerEndpoint/MTRServerAttribute_Internal.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ NS_ASSUME_NONNULL_BEGIN
3737
*/
3838
- (void)invalidate;
3939

40+
/**
41+
* Add the attribute to a cluster with the given cluster path. Will return NO
42+
* if the attribute is already added to a cluster.
43+
*/
44+
- (BOOL)addToCluster:(const chip::app::ConcreteClusterPath &)cluster;
45+
46+
/**
47+
* Update the parent cluster path of the attribute. Should only be done for
48+
* attributes that are already added to a cluster, when the endpoint id needs to
49+
* be updated.
50+
*/
51+
- (void)updateParentCluster:(const chip::app::ConcreteClusterPath &)cluster;
52+
4053
/**
4154
* serializedValue is either an NSData or an NSArray<NSData *>, depending on
4255
* whether the attribute is list-typed.
@@ -47,7 +60,7 @@ NS_ASSUME_NONNULL_BEGIN
4760
* parentCluster will have kInvalidClusterId for the cluster ID until the
4861
* attribute is added to a cluster.
4962
*/
50-
@property (nonatomic, assign) chip::app::ConcreteClusterPath parentCluster;
63+
@property (nonatomic, assign, readonly) const chip::app::ConcreteClusterPath & parentCluster;
5164

5265
@end
5366

src/darwin/Framework/CHIP/ServerEndpoint/MTRServerCluster.mm

+5-7
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,6 @@ - (BOOL)addAttribute:(MTRServerAttribute *)attribute
176176
return NO;
177177
}
178178

179-
if (attribute.parentCluster.mClusterId != kInvalidClusterId) {
180-
MTR_LOG_ERROR("Cannot add attribute to cluster %llu; already added to cluster %" PRIu32, _clusterID.unsignedLongLongValue, attribute.parentCluster.mClusterId);
181-
return NO;
182-
}
183-
184179
auto attributeID = attribute.attributeID.unsignedLongLongValue;
185180
if (attributeID == MTRAttributeIDTypeGlobalAttributeAttributeListID || attributeID == MTRAttributeIDTypeGlobalAttributeAcceptedCommandListID || attributeID == MTRAttributeIDTypeGlobalAttributeGeneratedCommandListID || attributeID == MTRAttributeIDTypeGlobalAttributeClusterRevisionID) {
186181
MTR_LOG_ERROR("Cannot add global attribute %llx on cluster %llx", attributeID, _clusterID.unsignedLongLongValue);
@@ -201,8 +196,11 @@ - (BOOL)addAttribute:(MTRServerAttribute *)attribute
201196
}
202197
}
203198

199+
if (![attribute addToCluster:ConcreteClusterPath(_parentEndpoint, static_cast<ClusterId>(_clusterID.unsignedLongLongValue))]) {
200+
return NO;
201+
}
202+
204203
[_attributes addObject:attribute];
205-
attribute.parentCluster = ConcreteClusterPath(_parentEndpoint, static_cast<ClusterId>(_clusterID.unsignedLongLongValue));
206204
return YES;
207205
}
208206

@@ -374,7 +372,7 @@ - (void)setParentEndpoint:(EndpointId)endpoint
374372
// Update it on all the attributes, in case the attributes were added to us
375373
// before we were added to the endpoint.
376374
for (MTRServerAttribute * attr in _attributes) {
377-
attr.parentCluster = ConcreteClusterPath(endpoint, static_cast<ClusterId>(_clusterID.unsignedLongLongValue));
375+
[attr updateParentCluster:ConcreteClusterPath(endpoint, static_cast<ClusterId>(_clusterID.unsignedLongLongValue))];
378376
}
379377
}
380378

src/darwin/Framework/Matter.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
51565CB22A7AD77600469F18 /* MTRDeviceControllerDataStore.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51565CB02A7AD77600469F18 /* MTRDeviceControllerDataStore.mm */; };
171171
51565CB42A7AD78D00469F18 /* MTRDeviceControllerStorageDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 51565CB32A7AD78D00469F18 /* MTRDeviceControllerStorageDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
172172
51565CB62A7B0D6600469F18 /* MTRDeviceControllerParameters.h in Headers */ = {isa = PBXBuildFile; fileRef = 51565CB52A7B0D6600469F18 /* MTRDeviceControllerParameters.h */; settings = {ATTRIBUTES = (Public, ); }; };
173+
515BE4ED2B72C0C5000BC1FD /* MTRUnfairLock.h in Headers */ = {isa = PBXBuildFile; fileRef = 515BE4EC2B72C0C5000BC1FD /* MTRUnfairLock.h */; };
173174
515C1C6F284F9FFB00A48F0C /* MTRFramework.mm in Sources */ = {isa = PBXBuildFile; fileRef = 515C1C6D284F9FFB00A48F0C /* MTRFramework.mm */; };
174175
515C1C70284F9FFB00A48F0C /* MTRFramework.h in Headers */ = {isa = PBXBuildFile; fileRef = 515C1C6E284F9FFB00A48F0C /* MTRFramework.h */; };
175176
516411312B6BF70300E67C05 /* DataModelHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 516415FE2B6B132200D5CE11 /* DataModelHandler.cpp */; };
@@ -567,6 +568,7 @@
567568
51565CB02A7AD77600469F18 /* MTRDeviceControllerDataStore.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDeviceControllerDataStore.mm; sourceTree = "<group>"; };
568569
51565CB32A7AD78D00469F18 /* MTRDeviceControllerStorageDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRDeviceControllerStorageDelegate.h; sourceTree = "<group>"; };
569570
51565CB52A7B0D6600469F18 /* MTRDeviceControllerParameters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRDeviceControllerParameters.h; sourceTree = "<group>"; };
571+
515BE4EC2B72C0C5000BC1FD /* MTRUnfairLock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRUnfairLock.h; sourceTree = "<group>"; };
570572
515C1C6D284F9FFB00A48F0C /* MTRFramework.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRFramework.mm; sourceTree = "<group>"; };
571573
515C1C6E284F9FFB00A48F0C /* MTRFramework.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRFramework.h; sourceTree = "<group>"; };
572574
516415FA2B6ACA8300D5CE11 /* MTRServerAccessControl.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRServerAccessControl.mm; sourceTree = "<group>"; };
@@ -1358,6 +1360,7 @@
13581360
5173A47229C0E2ED00F67F48 /* MTRFabricInfo_Internal.h */,
13591361
5173A47429C0E2ED00F67F48 /* MTRFabricInfo.h */,
13601362
5173A47329C0E2ED00F67F48 /* MTRFabricInfo.mm */,
1363+
515BE4EC2B72C0C5000BC1FD /* MTRUnfairLock.h */,
13611364
3D69867E29382E58007314E7 /* Resources */,
13621365
);
13631366
path = CHIP;
@@ -1622,6 +1625,7 @@
16221625
51E51FC0282AD37A00FC978D /* MTRDeviceControllerStartupParams_Internal.h in Headers */,
16231626
3DECCB702934AECD00585AEC /* MTRLogging.h in Headers */,
16241627
1E4D654E29C208DD00BC3478 /* MTRCommissionableBrowserResult.h in Headers */,
1628+
515BE4ED2B72C0C5000BC1FD /* MTRUnfairLock.h in Headers */,
16251629
998F286F26D55EC5001846C6 /* MTRP256KeypairBridge.h in Headers */,
16261630
2C222ADF255C811800E446B9 /* MTRBaseDevice_Internal.h in Headers */,
16271631
514C7A022B64223400DD6D7B /* MTRServerEndpoint_Internal.h in Headers */,

0 commit comments

Comments
 (0)