!19670 merge fix into master

fix mem leak of obs manager

Created-by: zqz979
Commit-by: zqz979
Merged-by: openharmony_ci
Description: **IssueNo**:

**Description**:

**稳定性自检:**
| 自检项                                                       | 自检结果  |
| ------------------------------------------------------------ | -------- |
| 涉及跨进程调用的相关操作需要抛至主线程或加锁防止并发              |          |
| 成员变量进行赋值或创建需要排查并发                               |          |
| 谨慎在lambda表达式中使用引用捕获                                |          |
| 谨慎在未经拷贝的情况下使用外部传入的string、C字符串               |          |
| map\vector\list\set等stl模板类使用时需要排查并发                |          |
| 谨慎考虑加锁范围                                               |          |
| 在IPC通信中谨慎使用同步通信方式                                 |          |
| 禁止传递this指针至其他模块或线程(特别是eventhandler任务)        |          |
| 禁止将外部传入的裸指针在内部直接构造智能指针                      |          |
| 禁止多个独立创建的智能指针管理同一地址                           |          |
| 禁止在析构函数中抛异步任务                                      |          |
| 禁止js对象在非js线程(例如在IPC线程)创建、使用或销毁             |          |
| 禁止在对外接口中未经判空直接使用外部传入的指针                    |          |
| 禁止接口返回局部变量引用                                        |          |
| 禁止在信号函数中加锁                                            |          |
| 禁止在关键流程(SA启动、应用启动等主流程)执行耗时的操作           |          |
| 禁止将同一个cpp编译在不同的so中                                 |          |

**安全编码自检:**
| 自检项                                                          | 自检结果 |
| -------------------------------------------------------------- | -------- |
| 裸指针避免通过隐式转换构造为sptr                                 |          |
| json对象在取值之前必须先判断类型,避免类型不匹配                   |          |
| 序列化时必须对传入的数组大小进行校验,避免出现超大数组              |          |
| 避免使用未明确位宽的整型,选择使用int8_t、uint8_t等类型            |          |
| 外部传入的路径要做规范化校验,对路径中的.、..、../等特殊字符严格校验 |          |
| 指针变量、表示资源描述符的变量、bool变量必须赋初值                  |          |
| readParcelable获取的对象使用前需要判空                            |          |
| 分配和释放内存的函数需要成对出现                                   |          |
| 申请内存后异常退出前需要及时进行内存释放                            |          |
| 内存申请前必须对内存大小进行合法性校验                              |          |
| 内存分配后必须判断是否成功                                         |          |
| 禁止使用realloc、alloca函数                                       |          |
| 禁止打印文件路径、口令等敏感信息,如有需要,使用private修饰          |          |
| 禁止打印内存地址                                                  |          |
| 整数之间运算时必须严格检查,确保不会出现溢出、反转、除0               |          |
| 禁止对有符号整数进行位操作符运算                                    |          |
| 禁止对指针进行逻辑或位运算                                         |          |
| 循环次数如果收外部数据控制,需要检验其合法性                         |          |
| 禁止使用内存操作类危险函数,需要使用安全函数                         |          |
| 谨慎使用不可重入函数                                               |          |
| 必须检查安全函数的返回值,并进行正确处理                             |          |
| 禁止仅通过TokenType类型判断绕过权限校验                             |          |

**TDD Result**:
![image.png](https://raw.gitcode.com/user-images/assets/4421702/27e481e2-1f6b-473a-a7a9-f87802ffa8be/image.png 'image.png')
**XTS Result**:

### 是否已执行L0用例
- [x] 已验证
- [ ] 不涉及。如不涉及,请写明理由


See merge request: openharmony/ability_ability_runtime!19670
This commit is contained in:
openharmony_ci
2026-06-23 21:13:13 +08:00
5 changed files with 64 additions and 14 deletions
@@ -43,6 +43,7 @@ struct ChangeInfo {
uint32_t size_ = 0;
VBuckets valueBuckets_ = {};
static constexpr int LIST_MAX_COUNT = 3000;
static constexpr uint32_t MAX_DATA_SIZE = 200 * 1024;
};
} // namespace AAFwk
} // namespace OHOS
@@ -125,6 +125,7 @@ private:
uint32_t tokenId, int32_t userId);
void SubmitNotifyChangeTask(Uri &uri, int32_t userId, std::string readPermission,
ObserverInfo &info);
void ReduceTaskCount();
private:
static constexpr std::uint32_t TASK_COUNT_MAX = 50;
ffrt::mutex taskCountMutex_;
@@ -89,9 +89,9 @@ bool ChangeInfo::Unmarshalling(ChangeInfo &output, MessageParcel &parcel)
return false;
}
const uint8_t *data = size > 0 ? parcel.ReadBuffer(size) : nullptr;
const uint8_t *data = (size > 0 && size <= ChangeInfo::MAX_DATA_SIZE) ? parcel.ReadBuffer(size) : nullptr;
if (size > 0 && data == nullptr) {
LOG_ERROR("Failed to read buffer from parcel.");
LOG_ERROR("Failed to read buffer from parcel. size: %{public}u", size);
return false;
}
VBuckets buckets;
+17 -12
View File
@@ -517,6 +517,12 @@ bool DataObsMgrService::IsTaskOverLimit()
return false;
}
void DataObsMgrService::ReduceTaskCount()
{
std::lock_guard<ffrt::mutex> lck(taskCountMutex_);
--taskCount_;
}
void DataObsMgrService::SubmitNotifyChangeTask(Uri &uri, int32_t userId, std::string readPermission, ObserverInfo &info)
{
ChangeInfo changeInfo = { ChangeInfo::ChangeType::OTHER, { uri } };
@@ -744,13 +750,10 @@ std::pair<Status, std::vector<NotifyInfo>> DataObsMgrService::MakeNotifyInfos(Ch
Status DataObsMgrService::NotifyChangeExt(const ChangeInfo &changeInfo, DataObsOption opt)
{
if (handler_ == nullptr) {
TAG_LOGE(AAFwkTag::DBOBSMGR, "null handler");
return DATAOBS_SERVICE_HANDLER_IS_NULL;
}
if (dataObsMgrInner_ == nullptr || dataObsMgrInnerExt_ == nullptr) {
LOG_ERROR("dataObsMgrInner_:%{public}d or null dataObsMgrInnerExt", dataObsMgrInner_ == nullptr);
return DATAOBS_SERVICE_INNER_IS_NULL;
if (handler_ == nullptr || dataObsMgrInner_ == nullptr || dataObsMgrInnerExt_ == nullptr) {
LOG_ERROR("handler_: %{public}d, dataObsMgrInner: %{public}d, dataObsMgrInnerExt: %{public}d",
handler_ == nullptr, dataObsMgrInner_ == nullptr, dataObsMgrInnerExt_ == nullptr);
return handler_ == nullptr ? DATAOBS_SERVICE_HANDLER_IS_NULL : DATAOBS_SERVICE_INNER_IS_NULL;
}
if (!IsCallingPermissionValid(opt)) {
return DATAOBS_NOT_SYSTEM_APP;
@@ -761,20 +764,23 @@ Status DataObsMgrService::NotifyChangeExt(const ChangeInfo &changeInfo, DataObsO
LOG_ERROR("GetCallingUserId fail, type:%{public}d, userId:%{public}d", changeInfo.changeType_, userId);
return DATAOBS_INVALID_USERID;
}
if (IsTaskOverLimit()) {
return DATAOBS_SERVICE_TASK_LIMMIT;
}
ChangeInfo changes;
Status result = DeepCopyChangeInfo(changeInfo, changes);
if (result != SUCCESS) {
LOG_ERROR("copy data failed,changeType:%{public}ud,uris num:%{public}zu,null data:%{public}d,size:%{public}ud",
changeInfo.changeType_, changeInfo.uris_.size(), changeInfo.data_ == nullptr, changeInfo.size_);
ReduceTaskCount();
return result;
}
if (IsTaskOverLimit()) {
return DATAOBS_SERVICE_TASK_LIMMIT;
}
std::vector<NotifyInfo> notifyInfo;
std::tie (result, notifyInfo) = MakeNotifyInfos(changes, opt, tokenId, userId);
if (changes.uris_.empty()) {
TAG_LOGE(AAFwkTag::DBOBSMGR, "uris_ is empty");
delete [] static_cast<uint8_t *>(changes.data_);
ReduceTaskCount();
return result;
}
handler_->SubmitTask([this, changes, userId, tokenId, notifyInfo]() {
@@ -787,8 +793,7 @@ Status DataObsMgrService::NotifyChangeExt(const ChangeInfo &changeInfo, DataObsO
count++;
}
delete [] static_cast<uint8_t *>(changes.data_);
std::lock_guard<ffrt::mutex> lck(taskCountMutex_);
--taskCount_;
ReduceTaskCount();
});
return SUCCESS;
}
@@ -272,6 +272,49 @@ HWTEST_F(DataObsMgrServiceSecondTest, DataObsMgrServiceSecondTest_NotifyChangeEx
TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceSecondTest_NotifyChangeExt_0400 end");
}
/*
* Feature: DataObsMgrService
* Function: NotifyChangeExt
* SubFunction: NA
* FunctionPoints: DataObsMgrService NotifyChangeExt
* EnvConditions: NA
* CaseDescription: Verify that the DataObsMgrService NotifyChangeExt is abnormal.
*/
HWTEST_F(DataObsMgrServiceSecondTest, DataObsMgrServiceSecondTest_NotifyChangeExt_0500, TestSize.Level1)
{
TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceSecondTest_NotifyChangeExt_0500 start");
const int testVal = static_cast<int>(DATAOBS_SERVICE_HANDLER_IS_NULL);
Uri uri("dataobs://authority/com.domainname.dataability.persondata/ person/10");
auto dataObsMgrServer = std::make_shared<DataObsMgrService>();
dataObsMgrServer->handler_ = nullptr;
EXPECT_EQ(testVal, dataObsMgrServer->NotifyChangeExt({ ChangeInfo::ChangeType::UPDATE, { uri } }));
TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceSecondTest_NotifyChangeExt_0500 end");
}
/*
* Feature: DataObsMgrService
* Function: NotifyChangeExt
* SubFunction: NA
* FunctionPoints: DataObsMgrService NotifyChangeExt
* EnvConditions: NA
* CaseDescription: Verify that the DataObsMgrService NotifyChangeExt is abnormal.
*/
HWTEST_F(DataObsMgrServiceSecondTest, DataObsMgrServiceSecondTest_NotifyChangeExt_0600, TestSize.Level1)
{
TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceSecondTest_NotifyChangeExt_0600 start");
const int testVal = static_cast<int>(DATAOBS_SERVICE_INNER_IS_NULL);
Uri uri("dataobs://authority/com.domainname.dataability.persondata/ person/10");
auto dataObsMgrServer = std::make_shared<DataObsMgrService>();
dataObsMgrServer->Init();
auto tmp = dataObsMgrServer->dataObsMgrInner_;
dataObsMgrServer->dataObsMgrInner_ = nullptr;
EXPECT_EQ(testVal, dataObsMgrServer->NotifyChangeExt({ ChangeInfo::ChangeType::UPDATE, { uri } }));
dataObsMgrServer->dataObsMgrInner_ = tmp;
TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceSecondTest_NotifyChangeExt_0600 end");
}
/*
* Feature: DataObsMgrService
* Function: NotifyProcessObserver