From 4dfac82254f0bc18c4a2e3b3a1824aaeeffd6bab Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 30 Jun 2023 01:44:50 +0000 Subject: [PATCH 01/14] =?UTF-8?q?fuzz=E8=BF=9E=E6=8E=A5=E6=89=93=E6=A1=A9?= =?UTF-8?q?=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I3059c5b77546cafe99e10da8b405f815f363bec4 --- .../interface/include/sensor_hdi_connection.h | 1 + .../interface/src/sensor_hdi_connection.cpp | 15 +++++++ services/sensor/include/sensor_service.h | 3 +- services/sensor/src/sensor_service.cpp | 44 +++++++++++++++++-- .../sensoronremoterequest_fuzzer.cpp | 1 + 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h index 926cc747..4311a8a4 100644 --- a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -33,6 +33,7 @@ public: int32_t SetMode(int32_t sensorId, int32_t mode) override; int32_t RegisterDataReport(ReportDataCb cb, sptr reportDataCallback) override; int32_t DestroyHdiConnection() override; + int32_t ConnectCompatible(); private: DISALLOW_COPY_AND_MOVE(SensorHdiConnection); diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index ddf5681a..101badfd 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -51,6 +51,20 @@ int32_t SensorHdiConnection::ConnectHdi() return ERR_OK; } +int32_t SensorHdiConnection::ConnectCompatible() +{ + iSensorHdiConnection_ = std::make_unique(); + int32_t ret = ConnectHdiService(); + if (ret != ERR_OK) { + SEN_HILOGE("Connect compatible failed"); + } + ret = ConnectCompatibleHdi(); + if (ret != ERR_OK) { + SEN_HILOGE("Connect compatible hdi failed, ret:%{public}d", ret); + } + return ERR_OK; +} + int32_t SensorHdiConnection::ConnectHdiService() { int32_t ret = iSensorHdiConnection_->ConnectHdi(); @@ -58,6 +72,7 @@ int32_t SensorHdiConnection::ConnectHdiService() SEN_HILOGE("Connect hdi service failed"); return CONNECT_SENSOR_HDF_ERR; } + sensorList_.clear(); ret = iSensorHdiConnection_->GetSensorList(sensorList_); if (ret != 0) { SEN_HILOGE("Get sensor list failed"); diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 758e3deb..782f42ff 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -64,6 +64,7 @@ public: ErrCode EnableActiveInfoCB() override; ErrCode DisableActiveInfoCB() override; ErrCode ResetSensors() override; + void OnStartFuzzer(); private: DISALLOW_COPY_AND_MOVE(SensorService); @@ -80,7 +81,7 @@ private: void RegisterClientDeathRecipient(sptr sensorClient, int32_t pid); void UnregisterClientDeathRecipient(sptr sensorClient); - bool InitInterface(); + bool InitInterface(bool isConnectHdi); bool InitDataCallback(); bool InitSensorList(); bool InitSensorPolicy(); diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index a911e968..eac583be 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -57,7 +57,7 @@ void SensorService::OnStart() SEN_HILOGW("SensorService has already started"); return; } - if (!InitInterface()) { + if (!InitInterface(true)) { SEN_HILOGE("Init interface error"); return; } @@ -82,9 +82,45 @@ void SensorService::OnStart() state_ = SensorServiceState::STATE_RUNNING; } -bool SensorService::InitInterface() +void SensorService::OnStartFuzzer() { - auto ret = sensorHdiConnection_.ConnectHdi(); + CALL_LOG_ENTER; + if (state_ == SensorServiceState::STATE_RUNNING) { + SEN_HILOGW("SensorService has already started"); + return; + } + if (!InitInterface(false)) { + SEN_HILOGE("Init interface error"); + return; + } + if (!InitDataCallback()) { + SEN_HILOGE("Init data callback error"); + return; + } + if (!InitSensorList()) { + SEN_HILOGE("Init sensor list error"); + return; + } + sensorDataProcesser_ = new (std::nothrow) SensorDataProcesser(sensorMap_); + CHKPV(sensorDataProcesser_); + if (!InitSensorPolicy()) { + SEN_HILOGE("Init sensor policy error"); + } + if (!SystemAbility::Publish(this)) { + SEN_HILOGE("Publish SensorService error"); + return; + } + sensorManager_.InitSensorMap(sensorMap_, sensorDataProcesser_, reportDataCallback_); + state_ = SensorServiceState::STATE_RUNNING; +} + +bool SensorService::InitInterface(bool isConnectHdi) +{ + if (isConnectHdi) { + auto ret = sensorHdiConnection_.ConnectHdi(); + } else { + auto ret = sensorHdiConnection_.ConnectCompatible(); + } if (ret != ERR_OK) { SEN_HILOGE("Connect hdi failed"); return false; @@ -108,6 +144,7 @@ bool SensorService::InitDataCallback() bool SensorService::InitSensorList() { std::lock_guard sensorLock(sensorsMutex_); + sensors_.clear(); int32_t ret = sensorHdiConnection_.GetSensorList(sensors_); if (ret != 0) { SEN_HILOGE("GetSensorList is failed"); @@ -115,6 +152,7 @@ bool SensorService::InitSensorList() } { std::lock_guard sensorMapLock(sensorMapMutex_); + sensorMap_.clear(); for (const auto &it : sensors_) { if (!(sensorMap_.insert(std::make_pair(it.GetSensorId(), it)).second)) { SEN_HILOGW("sensorMap_ insert failed"); diff --git a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp index 92527388..9a944fce 100644 --- a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp +++ b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp @@ -49,6 +49,7 @@ bool OnRemoteRequestFuzzTest(const char* data, size_t size) datas.RewindRead(0); MessageParcel reply; MessageOption option; + sensorServicePtr->OnStartFuzzer(); sensorServicePtr->OnRemoteRequest(code, datas, reply, option); return true; } -- Gitee From 6a444206bf5774d96a255a47f79eb71df3835c42 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 30 Jun 2023 02:31:19 +0000 Subject: [PATCH 02/14] =?UTF-8?q?fuzz=E8=BF=9E=E6=8E=A5=E6=89=93=E6=A1=A9?= =?UTF-8?q?=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I3bbbc40413640f936ac3b6c512fa8de2ef7ef4f7 --- .../hdi_connection/interface/src/sensor_hdi_connection.cpp | 1 + services/sensor/include/sensor_service.h | 2 +- services/sensor/src/sensor_service.cpp | 6 +----- .../sensoronremoterequest_fuzzer.cpp | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index 101badfd..14414e5a 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -58,6 +58,7 @@ int32_t SensorHdiConnection::ConnectCompatible() if (ret != ERR_OK) { SEN_HILOGE("Connect compatible failed"); } + SEN_HILOGE("Connect compatible success"); ret = ConnectCompatibleHdi(); if (ret != ERR_OK) { SEN_HILOGE("Connect compatible hdi failed, ret:%{public}d", ret); diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 782f42ff..ba861ea2 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -64,7 +64,7 @@ public: ErrCode EnableActiveInfoCB() override; ErrCode DisableActiveInfoCB() override; ErrCode ResetSensors() override; - void OnStartFuzzer(); + void OnStartFuzz(); private: DISALLOW_COPY_AND_MOVE(SensorService); diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index eac583be..47beb407 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -82,7 +82,7 @@ void SensorService::OnStart() state_ = SensorServiceState::STATE_RUNNING; } -void SensorService::OnStartFuzzer() +void SensorService::OnStartFuzz() { CALL_LOG_ENTER; if (state_ == SensorServiceState::STATE_RUNNING) { @@ -106,10 +106,6 @@ void SensorService::OnStartFuzzer() if (!InitSensorPolicy()) { SEN_HILOGE("Init sensor policy error"); } - if (!SystemAbility::Publish(this)) { - SEN_HILOGE("Publish SensorService error"); - return; - } sensorManager_.InitSensorMap(sensorMap_, sensorDataProcesser_, reportDataCallback_); state_ = SensorServiceState::STATE_RUNNING; } diff --git a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp index 9a944fce..85117a9d 100644 --- a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp +++ b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp @@ -49,7 +49,7 @@ bool OnRemoteRequestFuzzTest(const char* data, size_t size) datas.RewindRead(0); MessageParcel reply; MessageOption option; - sensorServicePtr->OnStartFuzzer(); + sensorServicePtr->OnStartFuzz(); sensorServicePtr->OnRemoteRequest(code, datas, reply, option); return true; } -- Gitee From acb9e82253739dd2f37692030e89be8cdf241b1b Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 30 Jun 2023 09:30:19 +0000 Subject: [PATCH 03/14] update Signed-off-by: hui1975 Change-Id: I91e07911f8bbe95d00a98b0702a8301c6151aede --- services/sensor/src/sensor_service.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 47beb407..51fd3e6c 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -112,10 +112,11 @@ void SensorService::OnStartFuzz() bool SensorService::InitInterface(bool isConnectHdi) { + int32_t ret = -1; if (isConnectHdi) { - auto ret = sensorHdiConnection_.ConnectHdi(); + ret = sensorHdiConnection_.ConnectHdi(); } else { - auto ret = sensorHdiConnection_.ConnectCompatible(); + ret = sensorHdiConnection_.ConnectCompatible(); } if (ret != ERR_OK) { SEN_HILOGE("Connect hdi failed"); -- Gitee From f245f4a16d532fd4e6e61a33cd94cbabef6f5de2 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 30 Jun 2023 09:37:41 +0000 Subject: [PATCH 04/14] update Signed-off-by: hui1975 Change-Id: Ibd55d56bfc8594cb462418e73500ea5e4d0a3b71 --- .../hdi_connection/interface/src/sensor_hdi_connection.cpp | 1 - services/sensor/src/sensor_service.cpp | 2 -- 2 files changed, 3 deletions(-) diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index 14414e5a..1684f653 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -73,7 +73,6 @@ int32_t SensorHdiConnection::ConnectHdiService() SEN_HILOGE("Connect hdi service failed"); return CONNECT_SENSOR_HDF_ERR; } - sensorList_.clear(); ret = iSensorHdiConnection_->GetSensorList(sensorList_); if (ret != 0) { SEN_HILOGE("Get sensor list failed"); diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 51fd3e6c..e380fa14 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -141,7 +141,6 @@ bool SensorService::InitDataCallback() bool SensorService::InitSensorList() { std::lock_guard sensorLock(sensorsMutex_); - sensors_.clear(); int32_t ret = sensorHdiConnection_.GetSensorList(sensors_); if (ret != 0) { SEN_HILOGE("GetSensorList is failed"); @@ -149,7 +148,6 @@ bool SensorService::InitSensorList() } { std::lock_guard sensorMapLock(sensorMapMutex_); - sensorMap_.clear(); for (const auto &it : sensors_) { if (!(sensorMap_.insert(std::make_pair(it.GetSensorId(), it)).second)) { SEN_HILOGW("sensorMap_ insert failed"); -- Gitee From 8f70f0242acc812ccbc4da7c297229a1b92b5851 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 3 Jul 2023 03:08:05 +0000 Subject: [PATCH 05/14] update Signed-off-by: hui1975 Change-Id: I6585e2fde5089d81e3247ed6324fc975c7b045ba --- .../hdi_connection/interface/src/sensor_hdi_connection.cpp | 1 - services/sensor/include/sensor_service.h | 2 +- services/sensor/src/sensor_service.cpp | 6 +++--- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index 1684f653..113a6d8a 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -58,7 +58,6 @@ int32_t SensorHdiConnection::ConnectCompatible() if (ret != ERR_OK) { SEN_HILOGE("Connect compatible failed"); } - SEN_HILOGE("Connect compatible success"); ret = ConnectCompatibleHdi(); if (ret != ERR_OK) { SEN_HILOGE("Connect compatible hdi failed, ret:%{public}d", ret); diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index ba861ea2..70b034ca 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -81,7 +81,7 @@ private: void RegisterClientDeathRecipient(sptr sensorClient, int32_t pid); void UnregisterClientDeathRecipient(sptr sensorClient); - bool InitInterface(bool isConnectHdi); + bool InitInterface(bool status); bool InitDataCallback(); bool InitSensorList(); bool InitSensorPolicy(); diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index e380fa14..eaf1ac57 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -86,7 +86,7 @@ void SensorService::OnStartFuzz() { CALL_LOG_ENTER; if (state_ == SensorServiceState::STATE_RUNNING) { - SEN_HILOGW("SensorService has already started"); + SEN_HILOGD("SensorService has already started"); return; } if (!InitInterface(false)) { @@ -110,10 +110,10 @@ void SensorService::OnStartFuzz() state_ = SensorServiceState::STATE_RUNNING; } -bool SensorService::InitInterface(bool isConnectHdi) +bool SensorService::InitInterface(bool status) { int32_t ret = -1; - if (isConnectHdi) { + if (status) { ret = sensorHdiConnection_.ConnectHdi(); } else { ret = sensorHdiConnection_.ConnectCompatible(); -- Gitee From ff4b1195f396087ac3d34d54ff4d800d15fbba8d Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 6 Jul 2023 02:07:02 +0000 Subject: [PATCH 06/14] update Signed-off-by: hui1975 Change-Id: I0a1bab4b94b9abf58aff1f0d70c78e4a5ea1e3cf --- .../include/sensors_ipc_interface_code.h | 2 - services/sensor/include/sensor_service.h | 2 +- .../sensoronremoterequest_fuzzer/BUILD.gn | 2 + .../sensoronremoterequest_fuzzer.cpp | 41 +++++++++++++++++-- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/frameworks/native/sensor/include/sensors_ipc_interface_code.h b/frameworks/native/sensor/include/sensors_ipc_interface_code.h index 5cdd9860..70c1e4e7 100644 --- a/frameworks/native/sensor/include/sensors_ipc_interface_code.h +++ b/frameworks/native/sensor/include/sensors_ipc_interface_code.h @@ -22,8 +22,6 @@ namespace Sensors { enum class SensorInterfaceCode { ENABLE_SENSOR = 0, DISABLE_SENSOR, - GET_SENSOR_STATE, - RUN_COMMAND, GET_SENSOR_LIST, TRANSFER_DATA_CHANNEL, DESTROY_SENSOR_CHANNEL, diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 70b034ca..c4abb112 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -48,6 +48,7 @@ public: void OnDump() override; void OnStart() override; void OnStop() override; + void OnStartFuzz(); int Dump(int fd, const std::vector &args) override; ErrCode EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; ErrCode DisableSensor(int32_t sensorId) override; @@ -64,7 +65,6 @@ public: ErrCode EnableActiveInfoCB() override; ErrCode DisableActiveInfoCB() override; ErrCode ResetSensors() override; - void OnStartFuzz(); private: DISALLOW_COPY_AND_MOVE(SensorService); diff --git a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/BUILD.gn index 03708698..b079fe3c 100644 --- a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/BUILD.gn +++ b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/BUILD.gn @@ -50,6 +50,8 @@ ohos_fuzztest("SensorOnRemoteRequestFuzzTest") { external_deps = [ "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", diff --git a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp index 85117a9d..dcb280ad 100644 --- a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp +++ b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp @@ -19,33 +19,66 @@ #include #include +#include "accesstoken_kit.h" #include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + #include "sensor.h" #include "sensor_service.h" - namespace OHOS { namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; namespace { constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t FOO_MIN_LEN = 96; constexpr size_t U32_AT_SIZE = 4; +constexpr uint32_t IPC_CODE_COUNT = 13; std::shared_ptr sensorServicePtr = std::make_shared(3601, false); const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; } // namespace +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "SensorOnRemoteRequestFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + uint32_t GetU32Data(const char* ptr) { // convert fuzz input data to an integer - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; + return ((ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]) % IPC_CODE_COUNT; } bool OnRemoteRequestFuzzTest(const char* data, size_t size) { + SetUpTestCase(); uint32_t code = GetU32Data(data); MessageParcel datas; datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); - datas.WriteBuffer(data, size); + datas.WriteBuffer(data + U32_AT_SIZE, size - U32_AT_SIZE); datas.RewindRead(0); MessageParcel reply; MessageOption option; @@ -65,7 +98,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) } /* Validate the length of size */ - if (size > OHOS::Sensors::FOO_MAX_LEN || size < OHOS::Sensors::U32_AT_SIZE) { + if (size > OHOS::Sensors::FOO_MAX_LEN || size < OHOS::Sensors::FOO_MIN_LEN) { return 0; } -- Gitee From 3e4a0230792c412b96e3d01c2a3205ead842e754 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 20 Jul 2023 06:59:44 +0000 Subject: [PATCH 07/14] update Signed-off-by: hui1975 Change-Id: Icca5c4e9453cb04eb9a5c85a17ffb526cebd004e --- .../hdi_connection/interface/src/sensor_hdi_connection.cpp | 7 ++++--- .../sensoronremoterequest_fuzzer.cpp | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index 8dc381a9..a55c52a2 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -68,13 +68,14 @@ int32_t SensorHdiConnection::ConnectCompatible() iSensorHdiConnection_ = std::make_unique(); int32_t ret = ConnectHdiService(); if (ret != ERR_OK) { - SEN_HILOGE("Connect compatible failed"); + SEN_HILOGE("Connect compatible connection failed, ret:%{public}d", ret); + return ret; } ret = ConnectCompatibleHdi(); if (ret != ERR_OK) { - SEN_HILOGE("Connect compatible hdi failed, ret:%{public}d", ret); + SEN_HILOGE("Connect color and sar compatible connection failed, ret:%{public}d", ret); } - return ERR_OK; + return ret; } int32_t SensorHdiConnection::ConnectHdiService() diff --git a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp index dcb280ad..1ddb501b 100644 --- a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp +++ b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp @@ -40,7 +40,7 @@ constexpr uint32_t IPC_CODE_COUNT = 13; std::shared_ptr sensorServicePtr = std::make_shared(3601, false); const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; -} // namespace +} // namespace void SetUpTestCase() { -- Gitee From af6fcb240e29adb365fe5b50bc11cfa55db82cf9 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 20 Jul 2023 07:38:36 +0000 Subject: [PATCH 08/14] update Signed-off-by: hui1975 Change-Id: Icce475d262995530344a924f54d67218f783031e --- .../hdi_connection/hardware/include/hdi_service_impl.h | 2 +- .../hdi_connection/hardware/src/hdi_service_impl.cpp | 8 ++++++++ services/sensor/include/sensor_manager.h | 1 + services/sensor/src/sensor_manager.cpp | 8 ++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h index a2b7ebd7..4b97c90d 100644 --- a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h +++ b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h @@ -27,7 +27,7 @@ namespace Sensors { class HdiServiceImpl : public Singleton { public: HdiServiceImpl() = default; - virtual ~HdiServiceImpl() {} + ~HdiServiceImpl(); int32_t GetSensorList(std::vector& sensorList); int32_t EnableSensor(int32_t sensorId); int32_t DisableSensor(int32_t sensorId); diff --git a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp index a7167eef..5e11c12a 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -56,6 +56,14 @@ int64_t HdiServiceImpl::samplingInterval_ = -1; int64_t HdiServiceImpl::reportInterval_ = -1; std::atomic_bool HdiServiceImpl::isStop_ = false; +HdiServiceImpl::~HdiServiceImpl() +{ + if (dataReportThread_.joinable()) { + pthread_cancel(dataReportThread_.native_handle()); + dataReportThread_.join(); + } +} + int32_t HdiServiceImpl::GetSensorList(std::vector& sensorList) { CALL_LOG_ENTER; diff --git a/services/sensor/include/sensor_manager.h b/services/sensor/include/sensor_manager.h index b4f3dc03..b80db1ac 100644 --- a/services/sensor/include/sensor_manager.h +++ b/services/sensor/include/sensor_manager.h @@ -30,6 +30,7 @@ namespace Sensors { using namespace Security::AccessToken; class SensorManager : public Singleton { public: + ~SensorManager(); bool SetBestSensorParams(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); bool ResetBestSensorParams(int32_t sensorId); ErrCode SaveSubscriber(int32_t sensorId, uint32_t pid, int64_t samplingPeriodNs, int64_t maxReportDelayNs); diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index 329b156a..70961fd7 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -32,6 +32,14 @@ constexpr uint32_t PROXIMITY_SENSOR_ID = 50331904; constexpr float PROXIMITY_FAR = 5.0; } // namespace +SensorManager::~SensorManager() +{ + if (dataThread_.joinable()) { + pthread_cancel(dataThread_.native_handle()); + dataThread_.join(); + } +} + void SensorManager::InitSensorMap(std::unordered_map &sensorMap, sptr dataProcesser, sptr dataCallback) { -- Gitee From daab3073a5810635ce7c376ad9c97e0f14b5026d Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 21 Jul 2023 05:43:15 +0000 Subject: [PATCH 09/14] update Signed-off-by: hui1975 Change-Id: Ib722a5b8f43a702cbe8e440c60620153a0b576fd --- .../sensor/hdi_connection/hardware/src/hdi_service_impl.cpp | 3 +-- services/sensor/src/sensor_manager.cpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp index 5e11c12a..0477957e 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -59,8 +59,7 @@ std::atomic_bool HdiServiceImpl::isStop_ = false; HdiServiceImpl::~HdiServiceImpl() { if (dataReportThread_.joinable()) { - pthread_cancel(dataReportThread_.native_handle()); - dataReportThread_.join(); + dataReportThread_.detach(); } } diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index 70961fd7..fa16783f 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -35,8 +35,7 @@ constexpr float PROXIMITY_FAR = 5.0; SensorManager::~SensorManager() { if (dataThread_.joinable()) { - pthread_cancel(dataThread_.native_handle()); - dataThread_.join(); + dataThread_.detach(); } } -- Gitee From 0ca5490a4bfb5f0fbbd69bb0a9145a3ae1371f2a Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 25 Jul 2023 04:11:25 +0000 Subject: [PATCH 10/14] update Signed-off-by: hui1975 Change-Id: I512359afd21df80d9800fd273ae08d41a16036d0 --- services/sensor/src/sensor_service.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index f386d8e0..2d8cde52 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -116,12 +116,7 @@ void SensorService::OnStartFuzz() bool SensorService::InitInterface(bool status) { - int32_t ret = -1; - if (status) { - ret = sensorHdiConnection_.ConnectHdi(); - } else { - ret = sensorHdiConnection_.ConnectCompatible(); - } + int32_t ret = status ? sensorHdiConnection_.ConnectHdi() : sensorHdiConnection_.ConnectCompatible(); if (ret != ERR_OK) { SEN_HILOGE("Connect hdi failed"); return false; -- Gitee From 1f978b64573377ff7db8f182a1855ba7d50c7ddf Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 31 Jul 2023 04:07:29 +0000 Subject: [PATCH 11/14] update Signed-off-by: hui1975 Change-Id: I874502cbb4c21fe262c3f72592b50df673508366 --- services/sensor/src/sensor_manager.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index ca36e4ea..41ca4863 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -37,13 +37,15 @@ constexpr float PROXIMITY_FAR = 5.0; SensorManager::~SensorManager() { +#ifdef HDF_DRIVERS_INTERFACE_SENSOR if (dataThread_.joinable()) { dataThread_.detach(); } +#endif // HDF_DRIVERS_INTERFACE_SENSOR } #ifdef HDF_DRIVERS_INTERFACE_SENSOR -void SensorManager::InitSensorMap(std::unordered_map &sensorMap, +void SensorManager::InitSensorMap(const std::unordered_map &sensorMap, sptr dataProcesser, sptr dataCallback) { std::lock_guard sensorLock(sensorMapMutex_); -- Gitee From 05d8c7af7701f3ade4da001a39b7456d9b4e135c Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 1 Aug 2023 12:38:20 +0000 Subject: [PATCH 12/14] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E8=AE=A9=E7=BA=BF=E7=A8=8B=E6=AD=A3=E5=B8=B8=E7=BB=93?= =?UTF-8?q?=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: Ic2b60a620bc346e3927b2139480649ae92d51ae7 --- .../hdi_connection/hardware/src/hdi_service_impl.cpp | 10 +++++++++- services/sensor/include/sensor_data_processer.h | 3 +++ services/sensor/src/sensor_data_processer.cpp | 10 ++++++++++ services/sensor/src/sensor_manager.cpp | 8 +++++++- utils/common/include/sensors_errors.h | 1 + 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp index 0477957e..a4666d6a 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -58,8 +58,10 @@ std::atomic_bool HdiServiceImpl::isStop_ = false; HdiServiceImpl::~HdiServiceImpl() { + CALL_LOG_ENTER; + isStop_ = true; if (dataReportThread_.joinable()) { - dataReportThread_.detach(); + dataReportThread_.join(); } } @@ -167,6 +169,12 @@ int32_t HdiServiceImpl::SetMode(int32_t sensorId, int32_t mode) int32_t HdiServiceImpl::Register(RecordSensorCallback cb) { CHKPR(cb, ERROR); + for (auto it = callbacks_.begin(); it != callbacks_.end(); it++) { + if (*it == cb) { + SEN_HILOGW("Same recordSensorCallback has been registered"); + return ERR_OK; + } + } callbacks_.push_back(cb); return ERR_OK; } diff --git a/services/sensor/include/sensor_data_processer.h b/services/sensor/include/sensor_data_processer.h index bd229494..e8ec187c 100644 --- a/services/sensor/include/sensor_data_processer.h +++ b/services/sensor/include/sensor_data_processer.h @@ -16,6 +16,7 @@ #ifndef SENSORS_DATA_PROCESSER_H #define SENSORS_DATA_PROCESSER_H +#include #include #include @@ -41,6 +42,7 @@ public: int32_t SendEvents(sptr &channel, SensorData &data); static int DataThread(sptr dataProcesser, sptr dataCallback); int32_t CacheSensorEvent(const SensorData &data, sptr &channel); + void StopThread(); private: DISALLOW_COPY_AND_MOVE(SensorDataProcesser); @@ -61,6 +63,7 @@ private: std::unordered_map>> dataCountMap_; std::mutex sensorMutex_; std::unordered_map sensorMap_; + static std::atomic_bool isStop_; }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 38acab08..18784f26 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -32,6 +32,7 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorDataProcesser" }; } // namespace +std::atomic_bool SensorDataProcesser::isStop_ = false; SensorDataProcesser::SensorDataProcesser(const std::unordered_map &sensorMap) { @@ -45,6 +46,11 @@ SensorDataProcesser::~SensorDataProcesser() sensorMap_.clear(); } +void SensorDataProcesser::StopThread() +{ + isStop_ = true; +} + void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map &cacheBuf, sptr &channel, SensorData &data, uint64_t periodCount) @@ -296,6 +302,10 @@ int32_t SensorDataProcesser::DataThread(sptr dataProcesser, SEN_HILOGE("Callback cannot be null"); return INVALID_POINTER; } + if (dataProcesser->isStop_) { + SEN_HILOGI("Thread stop"); + return THREAD_STOP; + } } while (1); } } // namespace Sensors diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index 41ca4863..de7ffc2f 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -37,9 +37,15 @@ constexpr float PROXIMITY_FAR = 5.0; SensorManager::~SensorManager() { + CALL_LOG_ENTER; #ifdef HDF_DRIVERS_INTERFACE_SENSOR + sensorDataProcesser_->StopThread(); if (dataThread_.joinable()) { - dataThread_.detach(); + { + std::unique_lock lk(ISensorHdiConnection::dataMutex_); + ISensorHdiConnection::dataCondition_.notify_one(); + } + dataThread_.join(); } #endif // HDF_DRIVERS_INTERFACE_SENSOR } diff --git a/utils/common/include/sensors_errors.h b/utils/common/include/sensors_errors.h index 859a8562..4848d4b8 100644 --- a/utils/common/include/sensors_errors.h +++ b/utils/common/include/sensors_errors.h @@ -96,6 +96,7 @@ enum { SUSPEND_ERR = REGIST_CALLBACK_ERR + 1, RESUME_ERR = SUSPEND_ERR + 1, RESET_ERR = RESUME_ERR + 1, + THREAD_STOP = RESET_ERR + 1, }; // Error code for Sensor utils -- Gitee From 07a013ce2a4d9b9e09f8ba9dac8fd587b194664c Mon Sep 17 00:00:00 2001 From: hui1975 Date: Wed, 30 Aug 2023 06:20:28 +0000 Subject: [PATCH 13/14] update Signed-off-by: hui1975 Change-Id: Ib9c00973cfa0bf448cb41b1c5796451cd56195c8 --- frameworks/native/sensor/src/sensor_service_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index 7a7586b1..afc46e79 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -214,7 +214,7 @@ void SensorServiceClient::ProcessDeathObserver(const wptr &object // STEP3 : Clear sensorlist and sensorServer_ sensorList_.clear(); sensorServer_ = nullptr; - // STEP4 : ReGet sensors 3601 service + // STEP4 : ReGet sensors 3601 service int32_t ret = InitServiceClient(); if (ret != ERR_OK) { SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); -- Gitee From 6d852d30ee083fde72f770fb2c7ed9d90b729808 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Wed, 30 Aug 2023 06:21:14 +0000 Subject: [PATCH 14/14] update Signed-off-by: hui1975 Change-Id: I42955b131871872358f136cc142dfb9f701cb368 --- frameworks/native/sensor/src/sensor_service_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index afc46e79..7a7586b1 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -214,7 +214,7 @@ void SensorServiceClient::ProcessDeathObserver(const wptr &object // STEP3 : Clear sensorlist and sensorServer_ sensorList_.clear(); sensorServer_ = nullptr; - // STEP4 : ReGet sensors 3601 service + // STEP4 : ReGet sensors 3601 service int32_t ret = InitServiceClient(); if (ret != ERR_OK) { SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); -- Gitee