onreadable判断fd合法性增强 Signed-off-by: m00472246 <majingtao1@huawei.com>

Signed-off-by: m00472246 <majingtao1@huawei.com>
This commit is contained in:
m00472246 2024-11-18 20:01:16 +08:00
parent ff44778db8
commit f9565074b7
3 changed files with 63 additions and 3 deletions

View File

@ -35,6 +35,7 @@ public:
using VSyncCallback = std::function<void(int64_t, void*)>;
using VSyncCallbackWithId = std::function<void(int64_t, int64_t, void*)>;
using FdShutDownCallback = std::function<void(int32_t)>;
using ReadableCallback = std::function<bool(int32_t)>;
struct FrameCallback {
void *userData_;
VSyncCallback callback_;
@ -101,6 +102,7 @@ public:
}
void RegisterFdShutDownCallback(FdShutDownCallback cb);
void RegisterReadableCallback(ReadableCallback cb);
void SetFdClosedFlagLocked(bool fdClosed);
@ -124,6 +126,7 @@ private:
std::vector<FrameCallback> frameCallbacks_ = {};
bool fdClosed_ = false;
FdShutDownCallback fdShutDownCallback_ = nullptr;
ReadableCallback readableCallback_ = nullptr;
std::mutex cbMutex_;
};

View File

@ -38,9 +38,12 @@ constexpr int32_t INVALID_FD = -1;
void VSyncCallBackListener::OnReadable(int32_t fileDescriptor)
{
HitracePerfScoped perfTrace(ScopedDebugTrace::isEnabled(), HITRACE_TAG_GRAPHIC_AGP, "OnReadablePerfCount");
if (fileDescriptor < 0) {
VLOGE("OnReadable Invalid fileDescriptor:%{public}d", fileDescriptor);
return;
{
std::lock_guard<std::mutex> locker(cbMutex_);
if (fileDescriptor < 0 || (readableCallback_ != nullptr && !readableCallback_(fileDescriptor))) {
VLOGE("OnReadable Invalid fileDescriptor:%{public}d", fileDescriptor);
return;
}
}
// 3 is array size.
int64_t data[3];
@ -160,6 +163,12 @@ void VSyncCallBackListener::RegisterFdShutDownCallback(FdShutDownCallback cb)
fdShutDownCallback_ = cb;
}
void VSyncCallBackListener::RegisterReadableCallback(ReadableCallback cb)
{
std::lock_guard<std::mutex> locker(cbMutex_);
readableCallback_ = cb;
}
VSyncReceiver::VSyncReceiver(const sptr<IVSyncConnection>& conn,
const sptr<IRemoteObject>& token,
const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper,
@ -215,6 +224,14 @@ VsyncError VSyncReceiver::Init()
}
RemoveAndCloseFdLocked();
});
listener_->RegisterReadableCallback([this](int32_t fileDescriptor) -> bool {
std::lock_guard<std::mutex> locker(initMutex_);
if (fileDescriptor != fd_) {
VLOGE("OnReadable Invalid fileDescriptor:%{public}d, fd_:%{public}d", fileDescriptor, fd_);
return false;
}
return true;
});
looper_->AddFileDescriptorListener(fd_, AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener_, "vSyncTask");
init_ = true;
@ -239,6 +256,7 @@ void VSyncReceiver::ThreadCreateNotify()
VSyncReceiver::~VSyncReceiver()
{
listener_->RegisterFdShutDownCallback(nullptr);
listener_->RegisterReadableCallback(nullptr);
std::lock_guard<std::mutex> locker(initMutex_);
RemoveAndCloseFdLocked();
DestroyLocked();

View File

@ -384,6 +384,45 @@ HWTEST_F(VsyncReceiverTest, SetUiDvsyncConfigTest, Function | MediumTest| Level3
ASSERT_EQ(vsyncReceiver->SetUiDvsyncConfig(1), VSYNC_ERROR_OK);
vsyncDistributor->RemoveConnection(conn);
}
/*
* Function: OnReadable001
* Type: Function
* Rank: Important(2)
* EnvConditions: N/A
* CaseDescription: 1. test OnReadable
*/
HWTEST_F(VsyncReceiverTest, OnReadable001, Function | MediumTest| Level3)
{
onVsyncCount = 0;
auto& rsClient = RSInterfaces::GetInstance();
auto rsReceiver = rsClient.CreateVSyncReceiver("VsyncReceiverTest");
ASSERT_EQ(rsReceiver->Init(), VSYNC_ERROR_OK);
VSyncReceiver::FrameCallback fcb = {
.userData_ = this,
.callback_ = OnVSync,
};
ASSERT_EQ(rsReceiver->RequestNextVSync(fcb), VSYNC_ERROR_OK);
while (onVsyncCount == 0) {
sleep(1);
std::cout<< "OnVsync called count: " << onVsyncCount << std::endl;
}
onVsyncCount = 0;
ASSERT_EQ(rsReceiver->SetVsyncCallBackForEveryFrame(fcb, true), VSYNC_ERROR_OK);
int64_t period = 0;
ASSERT_EQ(rsReceiver->GetVSyncPeriod(period), VSYNC_ERROR_OK);
usleep(period / 10);
ASSERT_EQ(rsReceiver->SetVsyncCallBackForEveryFrame(fcb, false), VSYNC_ERROR_OK);
sleep(1);
std::cout<< "OnVsync called count: " << onVsyncCount << " period: " << period << std::endl;
ASSERT_EQ(abs(onVsyncCount - 100) <= 5, true);
rsReceiver->listener_->OnReadable(-1);
rsReceiver->listener_->OnReadable(999);
}
} // namespace
} // namespace Rosen
} // namespace OHOS