1 /* 2 * Copyright (C) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_IPC_DBINDER_BASE_INVOKER_DEFINE_H 17 #define OHOS_IPC_DBINDER_BASE_INVOKER_DEFINE_H 18 19 #include <unistd.h> 20 #include <cinttypes> 21 #include <memory> 22 #include <sys/types.h> 23 #include "securec.h" 24 #include "sys_binder.h" 25 #include "iremote_invoker.h" 26 #include "invoker_factory.h" 27 28 #include "ipc_object_stub.h" 29 #include "ipc_object_proxy.h" 30 #include "ipc_process_skeleton.h" 31 #include "ipc_thread_skeleton.h" 32 #include "ipc_skeleton.h" 33 #include "ipc_debug.h" 34 #include "hitrace_invoker.h" 35 #include "dbinder_error_code.h" 36 #include "log_tags.h" 37 38 namespace OHOS { 39 using namespace OHOS::HiviewDFX; 40 41 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC_DBINDER_INVOKER, 42 "DBinderBaseInvoker" }; 43 44 template <class T> class DBinderBaseInvoker : public IRemoteInvoker { 45 public: 46 class DBinderSendAllocator : public DefaultAllocator { 47 void Dealloc(void *data) override; 48 49 friend DBinderBaseInvoker; 50 }; 51 52 class DBinderRecvAllocator : public DefaultAllocator { 53 void Dealloc(void *data) override; 54 55 friend DBinderBaseInvoker; 56 }; 57 58 virtual ~DBinderBaseInvoker() = default; 59 virtual std::shared_ptr<T> QueryServerSessionObject(uint32_t handle) = 0; 60 virtual bool UpdateClientSession(std::shared_ptr<T> sessionObject) = 0; 61 62 virtual int SendRequest(int32_t handle, uint32_t code, MessageParcel &data, MessageParcel &reply, 63 MessageOption &option) override; 64 virtual bool AddDeathRecipient(int32_t handle, void *cookie) override; 65 virtual bool RemoveDeathRecipient(int32_t handle, void *cookie) override; 66 virtual bool SetMaxWorkThread(int maxThreadNum) override; 67 virtual int SendReply(MessageParcel &reply, uint32_t flags, int32_t result) override; 68 virtual bool PingService(int32_t handle) override; 69 virtual sptr<IRemoteObject> GetSAMgrObject() override; 70 virtual bool SetRegistryObject(sptr<IRemoteObject> &object) override; 71 virtual void FreeBuffer(void *data) override; 72 virtual std::shared_ptr<T> WriteTransaction(int cmd, uint32_t flags, int32_t handle, int32_t socketId, 73 uint32_t code, MessageParcel &data, uint64_t &seqNumber, int status); 74 virtual int SendOrWaitForCompletion(int userWaitTime, uint64_t seqNumber, std::shared_ptr<T> sessionOfPeer, 75 MessageParcel *reply = nullptr); 76 virtual void OnTransaction(std::shared_ptr<ThreadProcessInfo> processInfo); 77 virtual void StartProcessLoop(int32_t socketId, const char *buffer, uint32_t size); 78 virtual uint32_t QueryHandleBySession(std::shared_ptr<T> session) = 0; 79 virtual std::shared_ptr<T> QueryClientSessionObject(uint32_t databusHandle) = 0; 80 virtual std::shared_ptr<T> NewSessionOfBinderProxy(uint32_t handle, std::shared_ptr<T> session) = 0; 81 virtual std::shared_ptr<T> QuerySessionOfBinderProxy(uint32_t handle, std::shared_ptr<T> session) = 0; 82 virtual std::shared_ptr<T> CreateServerSessionObject(binder_uintptr_t binder, std::shared_ptr<T> sessionObject) = 0; 83 virtual uint32_t FlattenSession(char *sessionOffset, const std::shared_ptr<T> connectSession, 84 uint32_t binderVersion) = 0; 85 virtual std::shared_ptr<T> UnFlattenSession(char *sessionOffset, uint32_t binderVersion) = 0; 86 virtual int OnSendMessage(std::shared_ptr<T> sessionOfPeer) = 0; 87 virtual bool CreateProcessThread() = 0; 88 virtual uint64_t GetSeqNum() const = 0; 89 virtual void SetSeqNum(uint64_t seq) = 0; 90 virtual int32_t GetClientFd() const = 0; 91 virtual void SetClientFd(int32_t fd) = 0; 92 virtual void SetCallerPid(pid_t pid) = 0; 93 virtual void SetCallerUid(pid_t uid) = 0; 94 virtual void SetStatus(uint32_t status) = 0; 95 virtual void SetCallerDeviceID(const std::string &deviceId) = 0; 96 virtual void SetCallerTokenID(const uint32_t tokenId) = 0; 97 virtual int CheckAndSetCallerInfo(int32_t socketId, uint64_t stubIndex) = 0; 98 virtual int OnSendRawData(std::shared_ptr<T> session, const void *data, size_t size) = 0; 99 bool CheckTransactionData(const dbinder_transaction_data *tr) const; 100 std::mutex &GetObjectMutex(); 101 void PrintDBinderTransaction(const char *funcName, const char *titleName, const dbinder_transaction_data *tr); 102 void PrintBuffer(const char *funcName, const char *titleName, const uint8_t *data, size_t length); 103 104 private: 105 uint32_t TranslateBinderType(flat_binder_object *binderObject, char *sessionOffset, std::shared_ptr<T> session); 106 uint32_t TranslateHandleType(flat_binder_object *binderObject, char *sessionOffset, std::shared_ptr<T> session); 107 void ClearBinderType(flat_binder_object *binderObject); 108 void ClearHandleType(flat_binder_object *binderObject); 109 bool TranslateRemoteHandleType(flat_binder_object *binderObject, char *sessionOffset, uint32_t binderVersion); 110 int HandleReply(uint64_t seqNumber, MessageParcel *reply, std::shared_ptr<ThreadMessageInfo> messageInfo); 111 int WaitForReply(uint64_t seqNumber, MessageParcel *reply, uint32_t handle, int userWaitTime); 112 void ProcessTransaction(dbinder_transaction_data *tr, int32_t listenFd); 113 void ProcessReply(dbinder_transaction_data *tr, int32_t listenFd); 114 bool IRemoteObjectTranslateWhenSend(char *dataBuffer, binder_size_t bufferSize, MessageParcel &data, 115 uint32_t socketId, std::shared_ptr<T> sessionObject); 116 bool IRemoteObjectTranslateWhenRcv(char *dataBuffer, binder_size_t bufferSize, MessageParcel &data, 117 uint32_t socketId, std::shared_ptr<T> sessionObject); 118 bool TranslateRawData(char *dataBuffer, MessageParcel &data, uint32_t socketId); 119 std::shared_ptr<T> GetSessionObject(uint32_t handle, uint32_t socketId); 120 uint64_t GetUniqueSeqNumber(int cmd); 121 void ConstructTransData(MessageParcel &data, dbinder_transaction_data &transData, size_t totalSize, 122 uint64_t seqNum, int cmd, __u32 code, __u32 flags); 123 bool ProcessRawData(std::shared_ptr<T> sessionObject, MessageParcel &data, uint64_t seqNum); 124 std::shared_ptr<dbinder_transaction_data> ProcessNormalData(std::shared_ptr<T> sessionObject, MessageParcel &data, 125 int32_t handle, int32_t socketId, uint64_t seqNum, int cmd, __u32 code, __u32 flags, int status); 126 bool MoveTransData2Buffer(std::shared_ptr<T> sessionObject, std::shared_ptr<dbinder_transaction_data> transData); 127 bool RemoveDBinderPtrData(std::shared_ptr<dbinder_transaction_data> transData, uint32_t &cutCount); 128 void OverrideMessageParcelData(std::shared_ptr<dbinder_transaction_data> tr, MessageParcel &data); 129 bool MoveMessageParcel2TransData(MessageParcel &data, std::shared_ptr<T> sessionObject, 130 std::shared_ptr<dbinder_transaction_data> transData, int32_t socketId, int status); 131 std::shared_ptr<ThreadProcessInfo> MakeThreadProcessInfo(int32_t socketId, const char *buffer, uint32_t size); 132 std::shared_ptr<ThreadMessageInfo> MakeThreadMessageInfo(int32_t socketId); 133 uint32_t MakeRemoteHandle(std::shared_ptr<T> session); 134 135 private: 136 std::mutex objectMutex_; 137 static constexpr int32_t REPLY_RETRY_COUNT = 5; 138 static constexpr int32_t REPLY_RETRY_WAIT_MS = 10; 139 }; 140 } // namespace OHOS 141 #endif // OHOS_IPC_DBINDER_BASE_INVOKER_DEFINE_H 142