diff --git a/interfaces/innerkits/ipc_core/include/peer_holder.h b/interfaces/innerkits/ipc_core/include/peer_holder.h index 81a6fdd6..c24747fe 100644 --- a/interfaces/innerkits/ipc_core/include/peer_holder.h +++ b/interfaces/innerkits/ipc_core/include/peer_holder.h @@ -19,6 +19,10 @@ #include "iremote_object.h" namespace OHOS { + +constexpr uint32_t BEFORE_MAGIC = 0x5A5A5A5A; +constexpr uint32_t AFTER_MAGIC = 0xA5A5A5A5; + class PeerHolder { protected: PeerHolder(const sptr &object); @@ -33,7 +37,19 @@ protected: sptr Remote(); private: + uint32_t beforeMagic_ = BEFORE_MAGIC; const sptr remoteObject_; + uint32_t afterMagic_ = AFTER_MAGIC; + + uint32_t GetBeforeMagic() + { + return beforeMagic_; + } + + uint32_t GetAfterMagic() + { + return afterMagic_; + } }; } // namespace OHOS #endif // OHOS_IPC_PEER_HOLDER_H diff --git a/ipc/native/src/core/source/peer_holder.cpp b/ipc/native/src/core/source/peer_holder.cpp index f5824b6a..ec017fa2 100644 --- a/ipc/native/src/core/source/peer_holder.cpp +++ b/ipc/native/src/core/source/peer_holder.cpp @@ -15,14 +15,23 @@ #include "peer_holder.h" +#include "ipc_debug.h" #include "iremote_object.h" +#include "log_tags.h" #include "refbase.h" namespace OHOS { +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC_PEER_HOLDER, "PeerHolder" }; + PeerHolder::PeerHolder(const sptr &object) : remoteObject_(object) {} sptr PeerHolder::Remote() { + if (GetBeforeMagic() != BEFORE_MAGIC || GetAfterMagic() != AFTER_MAGIC) { + ZLOGE(LOG_LABEL, "remoteObject invalid, beforeMagic:%{public}x afterMagic:%{public}x", + beforeMagic_, afterMagic_); + return nullptr; + } return remoteObject_; } } // namespace OHOS diff --git a/utils/include/log_tags.h b/utils/include/log_tags.h index ff3c625e..04bdc951 100644 --- a/utils/include/log_tags.h +++ b/utils/include/log_tags.h @@ -43,5 +43,6 @@ const unsigned int LOG_ID_RPC_DBINDER_INVOKER = LOG_ID_IPC_BASE | 0x14; const unsigned int LOG_ID_RPC_REMOTE_LISTENER = LOG_ID_IPC_BASE | 0x15; const unsigned int LOG_ID_RPC_DBINDER_CB_STUB = LOG_ID_IPC_BASE | 0x16; const unsigned int LOG_ID_IPC_DBINDER_SOFTBUS_CLIENT = LOG_ID_IPC_BASE | 0x17; +const unsigned int LOG_ID_IPC_PEER_HOLDER = LOG_ID_IPC_BASE | 0x18; } // namespace OHOS #endif // OHOS_COMMUNICATION_LOG_TAGS_H