...
 
Commits (16)
    https://gitcode.net/openharmony/communication_ipc/-/commit/00688b024c628efa5ee7e45851415de5237248a6 回退 'Pull Request !742 : 解决NAPIRemoteObject内存泄露问题' 2023-07-31T11:16:22+00:00 马根堂 magentang4@huawei.com https://gitcode.net/openharmony/communication_ipc/-/commit/d0b257395cfa228ba91b11715311ba731d0f0de8 !748 回退 'Pull Request !742 : 解决NAPIRemoteObject内存泄露问题' 2023-07-31T12:33:05+00:00 openharmony_ci 120357966@qq.com Merge pull request !748 from 马根堂/revert-merge-742-master https://gitcode.net/openharmony/communication_ipc/-/commit/26166028db7a972c6dd92077663e93cbedfbd473 fix ability acts crash when env destucted ahead 2023-07-31T20:36:54+08:00 18392170496 magentang4@huawei.com Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com"></a><a href="javascript:void(0)" class="avatar s16 avatar-inline identicon bg6" style="text-decoration: none">N</a><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">18392170496</a> &lt;<a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">magentang4@huawei.com</a>&gt;</span> https://gitcode.net/openharmony/communication_ipc/-/commit/e431c96ca57ea07f92e118fd5e68766c9b55707a !749 fix ability acts crash when env destucted ahead 2023-07-31T13:43:20+00:00 openharmony_ci 120357966@qq.com Merge pull request !749 from 马根堂/master https://gitcode.net/openharmony/communication_ipc/-/commit/842931b5c83a13d878fb5079988f4401c758a56c fix mem leak 2023-07-31T21:50:05+08:00 18392170496 magentang4@huawei.com Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com"></a><a href="javascript:void(0)" class="avatar s16 avatar-inline identicon bg6" style="text-decoration: none">N</a><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">18392170496</a> &lt;<a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">magentang4@huawei.com</a>&gt;</span> https://gitcode.net/openharmony/communication_ipc/-/commit/7f0820765a121680f139ef30be9958cf65eb781a !750 解决NAPIRemoteObject内存泄露问题 2023-07-31T14:43:09+00:00 openharmony_ci 120357966@qq.com Merge pull request !750 from 马根堂/master https://gitcode.net/openharmony/communication_ipc/-/commit/9c7413f2babfbb982a0b0912ca24044fe7f8a1c0 update interfaces/innerkits/ipc_core/include/iremote_object.h. 2023-08-03T06:26:38+00:00 dengyutao dengyutao2@huawei.com 虚继承修改 Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:dengyutao2@huawei.com" title="dengyutao2@huawei.com"></a><a href="javascript:void(0)" class="avatar s16 avatar-inline identicon bg2" style="text-decoration: none">N</a><a href="mailto:dengyutao2@huawei.com" title="dengyutao2@huawei.com">dengyutao</a> &lt;<a href="mailto:dengyutao2@huawei.com" title="dengyutao2@huawei.com">dengyutao2@huawei.com</a>&gt;</span> https://gitcode.net/openharmony/communication_ipc/-/commit/7a4a434debc295c66aa6d63ec13e9434ebbee48d remove cleanup hook 2023-08-07T15:22:32+08:00 18392170496 magentang4@huawei.com Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com"></a><a href="javascript:void(0)" class="avatar s16 avatar-inline identicon bg1" style="text-decoration: none">N</a><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">18392170496</a> &lt;<a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">magentang4@huawei.com</a>&gt;</span> https://gitcode.net/openharmony/communication_ipc/-/commit/65d74acd0108b48547f51685c3d5f57eae00ea1d remove cleanup hook 2023-08-07T18:57:31+08:00 18392170496 magentang4@huawei.com Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com"></a><a href="javascript:void(0)" class="avatar s16 avatar-inline identicon bg1" style="text-decoration: none">N</a><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">18392170496</a> &lt;<a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">magentang4@huawei.com</a>&gt;</span> https://gitcode.net/openharmony/communication_ipc/-/commit/2b8dd22be52a335bcfef728ec547e0c9731837e2 remove cleanup hook 2023-08-07T20:53:19+08:00 18392170496 magentang4@huawei.com Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com"></a><a href="javascript:void(0)" class="avatar s16 avatar-inline identicon bg6" style="text-decoration: none">N</a><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">18392170496</a> &lt;<a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">magentang4@huawei.com</a>&gt;</span> https://gitcode.net/openharmony/communication_ipc/-/commit/04835041102a3621256b6b494cd434966a65fb42 remove cleanup hook 2023-08-08T14:22:58+08:00 18392170496 magentang4@huawei.com Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com"></a><a href="javascript:void(0)" class="avatar s16 avatar-inline identicon bg3" style="text-decoration: none">N</a><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">18392170496</a> &lt;<a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">magentang4@huawei.com</a>&gt;</span> https://gitcode.net/openharmony/communication_ipc/-/commit/e9337e32b47dbb9deb0b4bd8d4c1b775ed960548 remove cleanup hook 2023-08-08T17:48:31+08:00 18392170496 magentang4@huawei.com Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com"></a><a href="javascript:void(0)" class="avatar s16 avatar-inline identicon bg6" style="text-decoration: none">N</a><a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">18392170496</a> &lt;<a href="mailto:magentang4@huawei.com" title="magentang4@huawei.com">magentang4@huawei.com</a>&gt;</span> https://gitcode.net/openharmony/communication_ipc/-/commit/00a54245d841e179f93821a355b2ba5714f82b44 !756 remove cleanup hook 2023-08-09T06:50:06+00:00 openharmony_ci 120357966@qq.com Merge pull request !756 from 马根堂/master https://gitcode.net/openharmony/communication_ipc/-/commit/108b3ada24e2bcf897796f8eca3309ef28e74130 !754 虚继承修改 2023-08-10T03:14:53+00:00 openharmony_ci 120357966@qq.com Merge pull request !754 from dengyutao/master https://gitcode.net/openharmony/communication_ipc/-/commit/2cc91be209ba272bebb88d9f98eec0353b8a54c6 fix:stoll crash 2023-08-11T09:03:23+08:00 wuchengwen wuchengwen4@huawei.com Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:wuchengwen4@huawei.com" title="wuchengwen4@huawei.com"></a><a href="javascript:void(0)" class="avatar s16 avatar-inline identicon bg4" style="text-decoration: none">N</a><a href="mailto:wuchengwen4@huawei.com" title="wuchengwen4@huawei.com">wuchengwen</a> &lt;<a href="mailto:wuchengwen4@huawei.com" title="wuchengwen4@huawei.com">wuchengwen4@huawei.com</a>&gt;</span> https://gitcode.net/openharmony/communication_ipc/-/commit/c1dea793506d62a6de9bad6e95772e1e9b4eb14f !757 fix:stoll crash 2023-08-13T11:30:13+00:00 openharmony_ci 120357966@qq.com Merge pull request !757 from 吴成文/master
...@@ -45,7 +45,7 @@ public: ...@@ -45,7 +45,7 @@ public:
enum { enum {
DATABUS_TYPE, DATABUS_TYPE,
}; };
class DeathRecipient : public RefBase { class DeathRecipient : public virtual RefBase {
public: public:
/** /**
* @brief Methods that enumerate death notifications. * @brief Methods that enumerate death notifications.
......
...@@ -30,6 +30,7 @@ static NapiError napiErr; ...@@ -30,6 +30,7 @@ static NapiError napiErr;
static const size_t ARGV_INDEX_0 = 0; static const size_t ARGV_INDEX_0 = 0;
static const size_t ARGV_INDEX_1 = 1; static const size_t ARGV_INDEX_1 = 1;
static constexpr size_t UINT64_STRING_MAX_LEN = 20;
napi_value NAPI_IPCSkeleton_getContextObject(napi_env env, napi_callback_info info) napi_value NAPI_IPCSkeleton_getContextObject(napi_env env, napi_callback_info info)
{ {
...@@ -249,6 +250,20 @@ napi_value NAPI_IPCSkeleton_resetCallingIdentity(napi_env env, napi_callback_inf ...@@ -249,6 +250,20 @@ napi_value NAPI_IPCSkeleton_resetCallingIdentity(napi_env env, napi_callback_inf
} }
} }
static bool IsValidIdentity(const std::string &identity)
{
// 20 represents the maximum string length uint64_t can represent
if (identity.empty() || identity.length() > UINT64_STRING_MAX_LEN) {
return false;
}
auto predicate = [](char c) {
return isdigit(c) == 0;
};
auto it = std::find_if(identity.begin(), identity.end(), predicate);
return it == identity.end();
}
napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info info) napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info info)
{ {
napi_value global = nullptr; napi_value global = nullptr;
...@@ -291,7 +306,7 @@ napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info ...@@ -291,7 +306,7 @@ napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info
napi_get_value_bool(env, napiIsLocalCalling, &isLocalCalling); napi_get_value_bool(env, napiIsLocalCalling, &isLocalCalling);
napi_value result; napi_value result;
if (isLocalCalling) { if (isLocalCalling) {
if (identity.empty()) { if (!IsValidIdentity(identity)) {
napi_get_boolean(env, false, &result); napi_get_boolean(env, false, &result);
return result; return result;
} }
...@@ -314,7 +329,12 @@ napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info ...@@ -314,7 +329,12 @@ napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info
} }
std::string deviceId = identity.substr(0, DEVICEID_LENGTH); std::string deviceId = identity.substr(0, DEVICEID_LENGTH);
int64_t token = std::stoll(identity.substr(DEVICEID_LENGTH, identity.length() - DEVICEID_LENGTH)); const std::string readIdentity = identity.substr(DEVICEID_LENGTH, identity.length() - DEVICEID_LENGTH);
if (!IsValidIdentity(readIdentity)) {
napi_get_boolean(env, false, &result);
return result;
}
int64_t token = std::stoll(readIdentity);
int callerUid = static_cast<int>((static_cast<uint64_t>(token)) >> PID_LEN); int callerUid = static_cast<int>((static_cast<uint64_t>(token)) >> PID_LEN);
int callerPid = static_cast<int>(token); int callerPid = static_cast<int>(token);
napi_value napiCallingPid; napi_value napiCallingPid;
...@@ -343,7 +363,7 @@ static napi_value NAPI_IPCSkeleton_restoreCallingIdentitySetProperty(napi_env en ...@@ -343,7 +363,7 @@ static napi_value NAPI_IPCSkeleton_restoreCallingIdentitySetProperty(napi_env en
napi_value result; napi_value result;
napi_get_undefined(env, &result); napi_get_undefined(env, &result);
if (isLocalCalling) { if (isLocalCalling) {
if (identity.empty()) { if (!IsValidIdentity(identity)) {
ZLOGE(LOG_LABEL, "identity is empty"); ZLOGE(LOG_LABEL, "identity is empty");
return result; return result;
} }
...@@ -365,7 +385,12 @@ static napi_value NAPI_IPCSkeleton_restoreCallingIdentitySetProperty(napi_env en ...@@ -365,7 +385,12 @@ static napi_value NAPI_IPCSkeleton_restoreCallingIdentitySetProperty(napi_env en
} }
std::string deviceId = identity.substr(0, DEVICEID_LENGTH); std::string deviceId = identity.substr(0, DEVICEID_LENGTH);
int64_t token = std::stoll(identity.substr(DEVICEID_LENGTH, identity.length() - DEVICEID_LENGTH)); const std::string readIdentity = identity.substr(DEVICEID_LENGTH, identity.length() - DEVICEID_LENGTH);
if (!IsValidIdentity(readIdentity)) {
ZLOGE(LOG_LABEL, "readIdentity is invalid");
return result;
}
int64_t token = std::stoll(readIdentity);
int callerUid = static_cast<int>((static_cast<uint64_t>(token)) >> PID_LEN); int callerUid = static_cast<int>((static_cast<uint64_t>(token)) >> PID_LEN);
int callerPid = static_cast<int>(token); int callerPid = static_cast<int>(token);
napi_value napiCallingPid; napi_value napiCallingPid;
......
...@@ -27,7 +27,7 @@ namespace OHOS { ...@@ -27,7 +27,7 @@ namespace OHOS {
class NAPIRemoteObjectHolder : public RefBase { class NAPIRemoteObjectHolder : public RefBase {
public: public:
explicit NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar); explicit NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar);
~NAPIRemoteObjectHolder() = default; ~NAPIRemoteObjectHolder();
sptr<IRemoteObject> Get(); sptr<IRemoteObject> Get();
void Set(sptr<IRemoteObject> object); void Set(sptr<IRemoteObject> object);
void attachLocalInterface(napi_value localInterface, std::string &descriptor); void attachLocalInterface(napi_value localInterface, std::string &descriptor);
......
...@@ -181,17 +181,6 @@ static NativeValue *RemoteObjectAttachCb(NativeEngine *engine, void *value, void ...@@ -181,17 +181,6 @@ static NativeValue *RemoteObjectAttachCb(NativeEngine *engine, void *value, void
return reinterpret_cast<NativeValue *>(jsRemoteObject); return reinterpret_cast<NativeValue *>(jsRemoteObject);
} }
static void OnEnvCleanUp(void *data)
{
if (data == nullptr) {
ZLOGE(LOG_LABEL, "data is null");
return;
}
NAPIRemoteObjectHolder *holder = reinterpret_cast<NAPIRemoteObjectHolder *>(data);
// js env has been destrcted, clear saved env info, and check befor use it
holder->CleanJsEnv();
}
napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info) napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info)
{ {
// new napi remote object // new napi remote object
...@@ -224,9 +213,6 @@ napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info) ...@@ -224,9 +213,6 @@ napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info)
// connect native object to js thisVar // connect native object to js thisVar
napi_status status = napi_wrap(env, thisVar, holder, RemoteObjectHolderFinalizeCb, nullptr, nullptr); napi_status status = napi_wrap(env, thisVar, holder, RemoteObjectHolderFinalizeCb, nullptr, nullptr);
NAPI_ASSERT(env, status == napi_ok, "wrap js RemoteObject and native holder failed"); NAPI_ASSERT(env, status == napi_ok, "wrap js RemoteObject and native holder failed");
// register listener for env destruction
status = napi_add_env_cleanup_hook(env, OnEnvCleanUp, holder);
NAPI_ASSERT(env, status == napi_ok, "add cleanup hook failed");
return thisVar; return thisVar;
} }
......
...@@ -23,10 +23,16 @@ ...@@ -23,10 +23,16 @@
namespace OHOS { namespace OHOS {
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "napi_remoteObject_holder" }; static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "napi_remoteObject_holder" };
struct DeleteJsRefParam { static void OnEnvCleanUp(void *data)
napi_env env; {
napi_ref thisVarRef; if (data == nullptr) {
}; ZLOGE(LOG_LABEL, "data is null");
return;
}
NAPIRemoteObjectHolder *holder = reinterpret_cast<NAPIRemoteObjectHolder *>(data);
// js env has been destrcted, clear saved env info, and check befor use it
holder->CleanJsEnv();
}
NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar) NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar)
: env_(env), descriptor_(descriptor), sptrCachedObject_(nullptr), wptrCachedObject_(nullptr), : env_(env), descriptor_(descriptor), sptrCachedObject_(nullptr), wptrCachedObject_(nullptr),
...@@ -36,6 +42,22 @@ NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16strin ...@@ -36,6 +42,22 @@ NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16strin
// create weak ref, do not need to delete, // create weak ref, do not need to delete,
// increase ref count when the JS object will transfer to another thread or process. // increase ref count when the JS object will transfer to another thread or process.
napi_create_reference(env, thisVar, 0, &jsObjectRef_); napi_create_reference(env, thisVar, 0, &jsObjectRef_);
// register listener for env destruction
napi_status status = napi_add_env_cleanup_hook(env, OnEnvCleanUp, this);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "add cleanup hook failed");
}
}
NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder()
{
if (env_ != nullptr) {
napi_status status = napi_remove_env_cleanup_hook(env_, OnEnvCleanUp, this);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "remove cleanup hook failed");
}
}
} }
sptr<IRemoteObject> NAPIRemoteObjectHolder::Get() sptr<IRemoteObject> NAPIRemoteObjectHolder::Get()
......