未验证 提交 01633e0d 编写于 作者: O openharmony_ci 提交者: Gitee

!249 read remote object bugfix

Merge pull request !249 from pilipala195/bugfix_remote_object
...@@ -103,16 +103,16 @@ public: ...@@ -103,16 +103,16 @@ public:
virtual void SetCallerTokenID(const uint32_t tokenId) = 0; virtual void SetCallerTokenID(const uint32_t tokenId) = 0;
virtual int CheckAndSetCallerInfo(uint32_t listenFd, uint64_t stubIndex) = 0; virtual int CheckAndSetCallerInfo(uint32_t listenFd, uint64_t stubIndex) = 0;
virtual int OnSendRawData(std::shared_ptr<T> session, const void *data, size_t size) = 0; virtual int OnSendRawData(std::shared_ptr<T> session, const void *data, size_t size) = 0;
virtual bool SetTokenId(const dbinder_transaction_data *tr, uint32_t listenFd) = 0; virtual bool SetTokenId(const dbinder_transaction_data *tr, std::shared_ptr<T> sessionObject) = 0;
bool CheckTransactionData(const dbinder_transaction_data *tr) const; bool CheckTransactionData(const dbinder_transaction_data *tr) const;
private: private:
uint32_t TranslateBinderType(flat_binder_object *binderObject, char *sessionOffset, std::shared_ptr<T> session); uint32_t TranslateBinderType(flat_binder_object *binderObject, char *sessionOffset, std::shared_ptr<T> session);
uint32_t TranslateHandleType(flat_binder_object *binderObject, char *sessionOffset, std::shared_ptr<T> session); uint32_t TranslateHandleType(flat_binder_object *binderObject, char *sessionOffset, std::shared_ptr<T> session);
bool TranslateRemoteHandleType(flat_binder_object *binderObject, char *sessionOffset); bool TranslateRemoteHandleType(flat_binder_object *binderObject, char *sessionOffset, std::shared_ptr<T> session);
int HandleReply(uint64_t seqNumber, MessageParcel *reply); int HandleReply(uint64_t seqNumber, MessageParcel *reply, std::shared_ptr<T> sessionObject);
bool SetSenderStubIndex(std::shared_ptr<dbinder_transaction_data> transData, uint32_t handle); bool SetSenderStubIndex(std::shared_ptr<dbinder_transaction_data> transData, uint32_t handle);
int WaitForReply(uint64_t seqNumber, MessageParcel *reply, uint32_t handle, int userWaitTime); int WaitForReply(uint64_t seqNumber, MessageParcel *reply, std::shared_ptr<T> sessionObject, int userWaitTime);
void ProcessTransaction(dbinder_transaction_data *tr, uint32_t listenFd); void ProcessTransaction(dbinder_transaction_data *tr, uint32_t listenFd);
void ProcessReply(dbinder_transaction_data *tr, uint32_t listenFd); void ProcessReply(dbinder_transaction_data *tr, uint32_t listenFd);
bool IRemoteObjectTranslate(char *dataBuffer, binder_size_t buffer_size, MessageParcel &data, uint32_t socketId, bool IRemoteObjectTranslate(char *dataBuffer, binder_size_t buffer_size, MessageParcel &data, uint32_t socketId,
...@@ -201,7 +201,8 @@ template <class T> uint32_t DBinderBaseInvoker<T>::MakeRemoteHandle(std::shared_ ...@@ -201,7 +201,8 @@ template <class T> uint32_t DBinderBaseInvoker<T>::MakeRemoteHandle(std::shared_
} }
template <class T> template <class T>
bool DBinderBaseInvoker<T>::TranslateRemoteHandleType(flat_binder_object *binderObject, char *sessionOffset) bool DBinderBaseInvoker<T>::TranslateRemoteHandleType(flat_binder_object *binderObject, char *sessionOffset,
std::shared_ptr<T> session)
{ {
std::shared_ptr<T> sessionOfPeer = nullptr; std::shared_ptr<T> sessionOfPeer = nullptr;
uint64_t stubIndex = 0; uint64_t stubIndex = 0;
...@@ -214,6 +215,7 @@ bool DBinderBaseInvoker<T>::TranslateRemoteHandleType(flat_binder_object *binder ...@@ -214,6 +215,7 @@ bool DBinderBaseInvoker<T>::TranslateRemoteHandleType(flat_binder_object *binder
DBINDER_BASE_LOGE("send a wrong dbinder object"); DBINDER_BASE_LOGE("send a wrong dbinder object");
return false; return false;
} }
sessionOfPeer->SetFeatureSet(session->GetFeatureSet());
uint32_t handle = QueryHandleBySession(sessionOfPeer, stubIndex); uint32_t handle = QueryHandleBySession(sessionOfPeer, stubIndex);
if (handle == 0) { if (handle == 0) {
...@@ -267,7 +269,8 @@ bool DBinderBaseInvoker<T>::IRemoteObjectTranslate(char *dataBuffer, binder_size ...@@ -267,7 +269,8 @@ bool DBinderBaseInvoker<T>::IRemoteObjectTranslate(char *dataBuffer, binder_size
} }
case BINDER_TYPE_REMOTE_HANDLE: { case BINDER_TYPE_REMOTE_HANDLE: {
if (TranslateRemoteHandleType(binderObject, flatOffset + i * T::GetFlatSessionLen()) != true) { if (TranslateRemoteHandleType(binderObject, flatOffset + i * T::GetFlatSessionLen(),
sessionObject) != true) {
DBINDER_BASE_LOGE("send a wrong dbinder object"); DBINDER_BASE_LOGE("send a wrong dbinder object");
return false; return false;
} }
...@@ -561,7 +564,8 @@ std::shared_ptr<T> DBinderBaseInvoker<T>::WriteTransaction(int cmd, uint32_t fla ...@@ -561,7 +564,8 @@ std::shared_ptr<T> DBinderBaseInvoker<T>::WriteTransaction(int cmd, uint32_t fla
return sessionObject; return sessionObject;
} }
template <class T> int DBinderBaseInvoker<T>::HandleReply(uint64_t seqNumber, MessageParcel *reply) template <class T> int DBinderBaseInvoker<T>::HandleReply(uint64_t seqNumber, MessageParcel *reply,
std::shared_ptr<T> sessionObject)
{ {
if (reply == nullptr) { if (reply == nullptr) {
DBINDER_BASE_LOGE("no need reply, free the buffer"); DBINDER_BASE_LOGE("no need reply, free the buffer");
...@@ -606,7 +610,7 @@ template <class T> int DBinderBaseInvoker<T>::HandleReply(uint64_t seqNumber, Me ...@@ -606,7 +610,7 @@ template <class T> int DBinderBaseInvoker<T>::HandleReply(uint64_t seqNumber, Me
} }
if (!IRemoteObjectTranslate(reinterpret_cast<char *>(messageInfo->buffer), messageInfo->bufferSize, *reply, if (!IRemoteObjectTranslate(reinterpret_cast<char *>(messageInfo->buffer), messageInfo->bufferSize, *reply,
messageInfo->socketId, nullptr)) { messageInfo->socketId, sessionObject)) {
DBINDER_BASE_LOGE("translate object failed"); DBINDER_BASE_LOGE("translate object failed");
return RPC_BASE_INVOKER_INVALID_REPLY_ERR; return RPC_BASE_INVOKER_INVALID_REPLY_ERR;
} }
...@@ -634,14 +638,15 @@ template <class T> void DBinderBaseInvoker<T>::DBinderRecvAllocator::Dealloc(voi ...@@ -634,14 +638,15 @@ template <class T> void DBinderBaseInvoker<T>::DBinderRecvAllocator::Dealloc(voi
} }
template <class T> template <class T>
int DBinderBaseInvoker<T>::WaitForReply(uint64_t seqNumber, MessageParcel *reply, uint32_t handle, int userWaitTime) int DBinderBaseInvoker<T>::WaitForReply(uint64_t seqNumber, MessageParcel *reply,
std::shared_ptr<T> sessionObject, int userWaitTime)
{ {
/* if reply == nullptr, this is a one way message */ /* if reply == nullptr, this is a one way message */
if (reply == nullptr) { if (reply == nullptr) {
return NO_ERROR; return NO_ERROR;
} }
std::shared_ptr<ThreadMessageInfo> messageInfo = MakeThreadMessageInfo(handle); std::shared_ptr<ThreadMessageInfo> messageInfo = MakeThreadMessageInfo(sessionObject->GetSessionHandle());
if (messageInfo == nullptr) { if (messageInfo == nullptr) {
DBINDER_BASE_LOGE("make thread message info failed, no memory"); DBINDER_BASE_LOGE("make thread message info failed, no memory");
return RPC_BASE_INVOKER_WAIT_REPLY_ERR; return RPC_BASE_INVOKER_WAIT_REPLY_ERR;
...@@ -658,7 +663,7 @@ int DBinderBaseInvoker<T>::WaitForReply(uint64_t seqNumber, MessageParcel *reply ...@@ -658,7 +663,7 @@ int DBinderBaseInvoker<T>::WaitForReply(uint64_t seqNumber, MessageParcel *reply
return RPC_BASE_INVOKER_WAIT_REPLY_ERR; return RPC_BASE_INVOKER_WAIT_REPLY_ERR;
} }
int32_t err = HandleReply(seqNumber, reply); int32_t err = HandleReply(seqNumber, reply, sessionObject);
current->EraseThreadBySeqNumber(seqNumber); current->EraseThreadBySeqNumber(seqNumber);
messageInfo->buffer = nullptr; messageInfo->buffer = nullptr;
return err; return err;
...@@ -681,7 +686,7 @@ int DBinderBaseInvoker<T>::SendOrWaitForCompletion(int userWaitTime, uint64_t se ...@@ -681,7 +686,7 @@ int DBinderBaseInvoker<T>::SendOrWaitForCompletion(int userWaitTime, uint64_t se
DBINDER_BASE_LOGE("fail to send to remote session with error = %{public}d", returnLen); DBINDER_BASE_LOGE("fail to send to remote session with error = %{public}d", returnLen);
// no return, for msg send failed maybe not mine // no return, for msg send failed maybe not mine
} }
return WaitForReply(seqNumber, reply, sessionOfPeer->GetSessionHandle(), userWaitTime); return WaitForReply(seqNumber, reply, sessionOfPeer, userWaitTime);
} }
template <class T> template <class T>
...@@ -865,7 +870,12 @@ template <class T> void DBinderBaseInvoker<T>::ProcessTransaction(dbinder_transa ...@@ -865,7 +870,12 @@ template <class T> void DBinderBaseInvoker<T>::ProcessTransaction(dbinder_transa
DBINDER_BASE_LOGE("set user info error, maybe cookie is NOT belong to current caller"); DBINDER_BASE_LOGE("set user info error, maybe cookie is NOT belong to current caller");
return; return;
} }
if (SetTokenId(tr, listenFd) != true) { std::shared_ptr<T> sessionObject = QueryClientSessionObject(listenFd);
if (sessionObject == nullptr) {
DBINDER_BASE_LOGE("session is not exist for listenFd = %u", listenFd);
return;
}
if (SetTokenId(tr, sessionObject) != true) {
DBINDER_BASE_LOGE("set tokenid failed"); DBINDER_BASE_LOGE("set tokenid failed");
return; return;
} }
...@@ -883,7 +893,8 @@ template <class T> void DBinderBaseInvoker<T>::ProcessTransaction(dbinder_transa ...@@ -883,7 +893,8 @@ template <class T> void DBinderBaseInvoker<T>::ProcessTransaction(dbinder_transa
DBINDER_BASE_LOGE("stubIndex is invalid"); DBINDER_BASE_LOGE("stubIndex is invalid");
return; return;
} }
if (!IRemoteObjectTranslate(reinterpret_cast<char *>(tr->buffer), tr->buffer_size, data, listenFd, nullptr)) { if (!IRemoteObjectTranslate(reinterpret_cast<char *>(tr->buffer), tr->buffer_size, data,
listenFd, sessionObject)) {
DBINDER_BASE_LOGE("translate object failed"); DBINDER_BASE_LOGE("translate object failed");
return; return;
} }
......
...@@ -98,7 +98,7 @@ private: ...@@ -98,7 +98,7 @@ private:
bool ConnectRemoteObject2Session(IRemoteObject *stubObject, uint64_t stubIndex, bool ConnectRemoteObject2Session(IRemoteObject *stubObject, uint64_t stubIndex,
const std::shared_ptr<DBinderSessionObject> sessionObject); const std::shared_ptr<DBinderSessionObject> sessionObject);
bool AuthSession2Proxy(uint32_t handle, const std::shared_ptr<DBinderSessionObject> Session); bool AuthSession2Proxy(uint32_t handle, const std::shared_ptr<DBinderSessionObject> Session);
bool SetTokenId(const dbinder_transaction_data *tr, uint32_t listenFd) override; bool SetTokenId(const dbinder_transaction_data *tr, std::shared_ptr<DBinderSessionObject> sessionObject) override;
private: private:
DISALLOW_COPY_AND_MOVE(DBinderDatabusInvoker); DISALLOW_COPY_AND_MOVE(DBinderDatabusInvoker);
......
...@@ -683,15 +683,11 @@ bool DBinderDatabusInvoker::IsLocalCalling() ...@@ -683,15 +683,11 @@ bool DBinderDatabusInvoker::IsLocalCalling()
return false; return false;
} }
bool DBinderDatabusInvoker::SetTokenId(const dbinder_transaction_data *tr, uint32_t listenFd) bool DBinderDatabusInvoker::SetTokenId(const dbinder_transaction_data *tr,
std::shared_ptr<DBinderSessionObject> sessionObject)
{ {
if (tr == nullptr) {
DBINDER_LOGE("set tokenid tr is null");
return false;
}
std::shared_ptr<DBinderSessionObject> sessionObject = QueryClientSessionObject(listenFd);
if (sessionObject == nullptr) { if (sessionObject == nullptr) {
DBINDER_LOGE("session is not exist for listenFd = %u", listenFd); DBINDER_LOGE("sessionObject is null");
return false; return false;
} }
std::shared_ptr<FeatureSetData> feature = sessionObject->GetFeatureSet(); std::shared_ptr<FeatureSetData> feature = sessionObject->GetFeatureSet();
......
...@@ -31,10 +31,14 @@ public: ...@@ -31,10 +31,14 @@ public:
SEND_ASYNC_REPLY = 1, SEND_ASYNC_REPLY = 1,
SEND_WRONG_REPLY = 2, SEND_WRONG_REPLY = 2,
GET_TOKENID = 3, GET_TOKENID = 3,
TEST_REMOTE_OBJECT = 4,
TEST_ADD = 5,
}; };
std::string GetFooName(void); std::string GetFooName(void);
virtual std::string TestGetFooName(void) = 0; virtual std::string TestGetFooName(void) = 0;
virtual int32_t TestAccessToken(MessageParcel &data, MessageParcel &reply) = 0; virtual int32_t TestAccessToken(MessageParcel &data, MessageParcel &reply) = 0;
virtual int32_t TestRemoteObject(MessageParcel &data, MessageParcel &reply) = 0;
virtual int32_t TestAdd(MessageParcel &data, MessageParcel &reply) = 0;
public: public:
DECLARE_INTERFACE_DESCRIPTOR(u"test.rpc.IRpcFooTest"); DECLARE_INTERFACE_DESCRIPTOR(u"test.rpc.IRpcFooTest");
private: private:
...@@ -47,6 +51,8 @@ public: ...@@ -47,6 +51,8 @@ public:
MessageParcel &data, MessageParcel &reply, MessageOption &option) override; MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
std::string TestGetFooName(void) override; std::string TestGetFooName(void) override;
int32_t TestAccessToken(MessageParcel &data, MessageParcel &reply) override; int32_t TestAccessToken(MessageParcel &data, MessageParcel &reply) override;
int32_t TestRemoteObject(MessageParcel &data, MessageParcel &reply) override;
int32_t TestAdd(MessageParcel &data, MessageParcel &reply) override;
}; };
class RpcFooProxy : public IRemoteProxy<IRpcFooTest> { class RpcFooProxy : public IRemoteProxy<IRpcFooTest> {
...@@ -55,6 +61,8 @@ public: ...@@ -55,6 +61,8 @@ public:
~RpcFooProxy() = default; ~RpcFooProxy() = default;
std::string TestGetFooName(void) override; std::string TestGetFooName(void) override;
int32_t TestAccessToken(MessageParcel &data, MessageParcel &reply) override; int32_t TestAccessToken(MessageParcel &data, MessageParcel &reply) override;
int32_t TestRemoteObject(MessageParcel &data, MessageParcel &reply) override;
int32_t TestAdd(MessageParcel &data, MessageParcel &reply) override;
private: private:
static inline BrokerDelegator<RpcFooProxy> delegator_; static inline BrokerDelegator<RpcFooProxy> delegator_;
}; };
......
...@@ -30,6 +30,7 @@ using namespace OHOS; ...@@ -30,6 +30,7 @@ using namespace OHOS;
#define TEST_NUMS 1000 #define TEST_NUMS 1000
static std::string g_deviceId; static std::string g_deviceId;
static sptr<IRpcFooTest> g_rpcTestProxy; static sptr<IRpcFooTest> g_rpcTestProxy;
static sptr<IRpcFooTest> g_ipcTestProxy;
class RpcClientTest : public testing::Test { class RpcClientTest : public testing::Test {
public: public:
static constexpr char DBINDER_PKG_NAME[] = "DBinderService"; static constexpr char DBINDER_PKG_NAME[] = "DBinderService";
...@@ -105,4 +106,25 @@ HWTEST_F(RpcClientTest, function_test_004, TestSize.Level1) ...@@ -105,4 +106,25 @@ HWTEST_F(RpcClientTest, function_test_004, TestSize.Level1)
} }
} }
} }
HWTEST_F(RpcClientTest, function_test_005, TestSize.Level1)
{
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
ASSERT_FALSE(saMgr == nullptr);
sptr<IRemoteObject> object = saMgr->GetSystemAbility(RPC_TEST_SERVICE2);
ASSERT_TRUE(object != nullptr);
MessageParcel dataParcel, replyParcel;
dataParcel.WriteRemoteObject(object);
dataParcel.WriteInt32(1);
dataParcel.WriteInt32(1);
dataParcel.WriteInt32(1);
dataParcel.WriteInt32(1);
dataParcel.WriteInt32(1);
int32_t err = g_rpcTestProxy->TestRemoteObject(dataParcel, replyParcel);
ASSERT_EQ(err, ERR_NONE);
err = replyParcel.ReadInt32();
ASSERT_EQ(err, ERR_NONE);
}
} }
\ No newline at end of file
...@@ -32,7 +32,10 @@ public: ...@@ -32,7 +32,10 @@ public:
void RpcServerTest::SetUpTestCase() {} void RpcServerTest::SetUpTestCase() {}
void RpcServerTest::TearDownTestCase() {} void RpcServerTest::TearDownTestCase()
{
IPCSkeleton::JoinWorkThread();
}
HWTEST_F(RpcServerTest, function_test_001, TestSize.Level1) HWTEST_F(RpcServerTest, function_test_001, TestSize.Level1)
{ {
...@@ -43,7 +46,14 @@ HWTEST_F(RpcServerTest, function_test_001, TestSize.Level1) ...@@ -43,7 +46,14 @@ HWTEST_F(RpcServerTest, function_test_001, TestSize.Level1)
saExtra.isDistributed = true; saExtra.isDistributed = true;
int result = saMgr->AddSystemAbility(RPC_TEST_SERVICE, new RpcFooStub(), saExtra); int result = saMgr->AddSystemAbility(RPC_TEST_SERVICE, new RpcFooStub(), saExtra);
ASSERT_EQ(result, 0); ASSERT_EQ(result, 0);
}
IPCSkeleton::JoinWorkThread(); HWTEST_F(RpcServerTest, function_test_002, TestSize.Level1)
{
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
ASSERT_TRUE(saMgr != nullptr);
int result = saMgr->AddSystemAbility(RPC_TEST_SERVICE2, new RpcFooStub());
ASSERT_EQ(result, 0);
} }
} }
\ No newline at end of file
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
*/ */
#include "rpc_test.h" #include "rpc_test.h"
#include "iremote_object.h"
#include "ipc_skeleton.h" #include "ipc_skeleton.h"
#include "ipc_types.h" #include "ipc_types.h"
...@@ -37,6 +38,14 @@ int RpcFooStub::OnRemoteRequest(uint32_t code, ...@@ -37,6 +38,14 @@ int RpcFooStub::OnRemoteRequest(uint32_t code,
result = TestAccessToken(data, reply); result = TestAccessToken(data, reply);
break; break;
} }
case TEST_REMOTE_OBJECT: {
result = TestRemoteObject(data, reply);
break;
}
case TEST_ADD: {
result = TestAdd(data, reply);
break;
}
default: default:
return IPCObjectStub::OnRemoteRequest(code, data, reply, option); return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
} }
...@@ -61,6 +70,27 @@ int32_t RpcFooStub::TestAccessToken(MessageParcel &data, MessageParcel &reply) ...@@ -61,6 +70,27 @@ int32_t RpcFooStub::TestAccessToken(MessageParcel &data, MessageParcel &reply)
return ERR_NONE; return ERR_NONE;
} }
int32_t RpcFooStub::TestRemoteObject(MessageParcel &data, MessageParcel &reply)
{
sptr<RpcFooProxy> rpcProxy = iface_cast<RpcFooProxy>(data.ReadRemoteObject());
if (rpcProxy == nullptr) {
reply.WriteInt32(ERR_NULL_OBJECT);
return ERR_NULL_OBJECT;
}
MessageParcel dataParcel, replyParcel;
int32_t err = rpcProxy->TestAdd(dataParcel, replyParcel);
reply.WriteInt32(err);
return err;
}
int32_t RpcFooStub::TestAdd(MessageParcel &data, MessageParcel &reply)
{
int32_t a = data.ReadInt32();
int32_t b = data.ReadInt32();
reply.WriteInt32(a + b);
return ERR_NONE;
}
RpcFooProxy::RpcFooProxy(const sptr<IRemoteObject> &impl) RpcFooProxy::RpcFooProxy(const sptr<IRemoteObject> &impl)
: IRemoteProxy<IRpcFooTest>(impl) : IRemoteProxy<IRpcFooTest>(impl)
{ {
...@@ -83,4 +113,25 @@ int32_t RpcFooProxy::TestAccessToken(MessageParcel &data, MessageParcel &reply) ...@@ -83,4 +113,25 @@ int32_t RpcFooProxy::TestAccessToken(MessageParcel &data, MessageParcel &reply)
int32_t err = Remote()->SendRequest(GET_TOKENID, data, reply, option); int32_t err = Remote()->SendRequest(GET_TOKENID, data, reply, option);
return err; return err;
} }
int32_t RpcFooProxy::TestRemoteObject(MessageParcel &data, MessageParcel &reply)
{
MessageOption option;
int32_t err = Remote()->SendRequest(TEST_REMOTE_OBJECT, data, reply, option);
return err;
}
int32_t RpcFooProxy::TestAdd(MessageParcel &data, MessageParcel &reply)
{
int32_t a = 223;
int32_t b = 513;
data.WriteInt32(a);
data.WriteInt32(b);
MessageOption option;
int32_t err = Remote()->SendRequest(TEST_ADD, data, reply, option);
if (err != ERR_NONE) {
return err;
}
return reply.ReadInt32() == (a + b) ? ERR_NONE : ERR_INVALID_DATA;
}
} // namespace OHOS } // namespace OHOS
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册