!19756 merge dispatcher-0703 into master

dispatcher

Created-by: xialiangwei
Commit-by: xialiangwei1@huawei.com
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**:

**XTS Result**:

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


See merge request: openharmony/ability_ability_runtime!19756
This commit is contained in:
openharmony_ci
2026-07-04 17:02:59 +08:00
6 changed files with 72 additions and 1 deletions
@@ -110,6 +110,7 @@ public:
static bool VariantEquals(const MoVariantStorage& lhs, const OH_AbilityRuntime_ModObjDispatcher_Variant* rhs);
static void RegisterStructMetadata(const std::vector<MoStructMeta>& structs);
static void UnregisterStructMetadata(const std::vector<MoStructMeta>& structs);
static bool GetStructFieldType(const std::string& structName, const std::string& fieldName,
std::shared_ptr<MoTypeInfo>* fieldType);
static bool GetStructFieldNames(const std::string& structName, std::vector<std::string>* fieldNames);
@@ -34,6 +34,11 @@ public:
AbilityRuntime_ErrorCode EnsureLoaded(OHOS::IRemoteObject* proxy);
// Clear all cached metadata and reset loaded state.
// Called when the remote proxy dies to release cached memory.
// The metadataManager object itself remains valid; only its cached data is released.
void ClearCache();
AbilityRuntime_ErrorCode QueryMainServiceInterfaceMemberIds(const char** names, uint32_t count,
uint32_t* memberIds) const;
@@ -130,6 +130,7 @@ private:
struct OH_AbilityRuntime_ModularObjectDispatcher {
OHOS::sptr<OHOS::IRemoteObject> proxy = nullptr;
std::shared_ptr<OHOS::AbilityRuntime::ModObjDispatcherMetadataManager> metadataManager;
OHOS::sptr<OHOS::IRemoteObject::DeathRecipient> deathRecipient;
};
struct OH_AbilityRuntime_ModularObjectDispatcher_TypeDescriptor {
@@ -1722,6 +1722,15 @@ void ModObjDispatcherComplexTypeManager::RegisterStructMetadata(const std::vecto
}
}
void ModObjDispatcherComplexTypeManager::UnregisterStructMetadata(const std::vector<MoStructMeta>& structs)
{
std::lock_guard<std::mutex> lock(g_structMetaMutex);
for (const auto& structMeta : structs) {
g_structFieldTypes.erase(structMeta.name);
g_structFieldOrder.erase(structMeta.name);
}
}
bool ModObjDispatcherComplexTypeManager::GetStructFieldType(const std::string& structName, const std::string& fieldName,
std::shared_ptr<MoTypeInfo>* fieldType)
{
@@ -281,6 +281,27 @@ AbilityRuntime_ErrorCode ModObjDispatcherMetadataManager::EnsureLoaded(OHOS::IRe
return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
}
// -------- ClearCache --------
void ModObjDispatcherMetadataManager::ClearCache()
{
std::lock_guard<std::mutex> lock(mutex_);
if (!loaded_) {
return;
}
ModObjDispatcherComplexTypeManager::UnregisterStructMetadata(structs_);
loaded_ = false;
version_.clear();
mainServiceInterface_.clear();
interfaces_.clear();
enums_.clear();
structs_.clear();
nameToMemberId_.clear();
memberIdToName_.clear();
memberIdToMethod_.clear();
TAG_LOGI(AAFwkTag::EXT, "ClearCache: metadata cache cleared");
}
// -------- RequestMetadataJson --------
AbilityRuntime_ErrorCode ModObjDispatcherMetadataManager::RequestMetadataJson(OHOS::IRemoteObject* proxy,
@@ -24,6 +24,28 @@ using OHOS::AbilityRuntime::ModObjDispatcherParamCodec;
using OHOS::MessageOption;
using OHOS::MessageParcel;
namespace OHOS::AbilityRuntime {
// Death recipient that clears cached metadata when the remote proxy dies.
// Holds weak_ptr to avoid extending metadataManager lifetime beyond the dispatcher.
class ProxyDeathRecipient : public IRemoteObject::DeathRecipient {
public:
explicit ProxyDeathRecipient(std::weak_ptr<ModObjDispatcherMetadataManager> metadataManager)
: metadataManager_(std::move(metadataManager)) {}
~ProxyDeathRecipient() override = default;
void OnRemoteDied(const wptr<IRemoteObject> &remote) override
{
auto mgr = metadataManager_.lock();
if (mgr != nullptr) {
TAG_LOGI(AAFwkTag::EXT, "Remote proxy died, clearing metadata cache");
mgr->ClearCache();
}
}
private:
std::weak_ptr<ModObjDispatcherMetadataManager> metadataManager_;
};
} // namespace OHOS::AbilityRuntime
namespace {
AbilityRuntime_ErrorCode CopyStringToBuffer(const std::string& src, char* dst, uint32_t max)
{
@@ -53,6 +75,13 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ModObjDispatcher_CreateMainServiceIns
}
dispatcher->proxy = remoteProxy->remote;
dispatcher->metadataManager = std::make_shared<ModObjDispatcherMetadataManager>();
// Register death recipient to auto-release cached metadata when the remote peer dies.
// Uses weak_ptr so the recipient does not extend metadataManager's lifetime.
dispatcher->deathRecipient = OHOS::sptr<OHOS::AbilityRuntime::ProxyDeathRecipient>::MakeSptr(
dispatcher->metadataManager);
if (dispatcher->proxy != nullptr && dispatcher->deathRecipient != nullptr) {
dispatcher->proxy->AddDeathRecipient(dispatcher->deathRecipient);
}
*ppModObjDispatcher = dispatcher;
return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
}
@@ -82,7 +111,12 @@ void OH_AbilityRuntime_ModObjDispatcher_Release(OH_AbilityRuntime_ModObjDispatch
if (ppModObjDispatcher == nullptr || *ppModObjDispatcher == nullptr) {
return;
}
delete *ppModObjDispatcher;
auto* dispatcher = *ppModObjDispatcher;
if (dispatcher->proxy != nullptr && dispatcher->deathRecipient != nullptr) {
dispatcher->proxy->RemoveDeathRecipient(dispatcher->deathRecipient);
}
dispatcher->deathRecipient = nullptr;
delete dispatcher;
*ppModObjDispatcher = nullptr;
}