1 /*
2 * Copyright (c) 2025 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 #include "handlevibratordata_fuzzer.h"
17
18 #include "securec.h"
19 #include "vibrator_service_client.h"
20
21 namespace OHOS {
22 namespace Sensors {
23 namespace {
24 constexpr size_t DATA_MIN_SIZE = 8;
25 } // namespace
26
27 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)28 static size_t GetObject(const uint8_t *data, size_t size, T &object)
29 {
30 size_t objectSize = sizeof(object);
31 if (objectSize > size) {
32 return 0;
33 }
34 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
35 if (ret != EOK) {
36 return 0;
37 }
38 return objectSize;
39 }
40
HandleVibratorDataFuzzTest(const uint8_t * data,size_t size)41 void HandleVibratorDataFuzzTest(const uint8_t *data, size_t size)
42 {
43 if (data == nullptr || size < DATA_MIN_SIZE) {
44 return;
45 }
46 VibratorStatusEvent statusEvent;
47 size_t startPos = GetObject<int32_t>(data, size, statusEvent.deviceId);
48 GetObject<int32_t>(data + startPos, size - startPos, statusEvent.vibratorCnt);
49 VibratorServiceClient::GetInstance().HandleVibratorData(statusEvent);
50 }
51 } // namespace Sensors
52 } // namespace OHOS
53
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)54 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
55 {
56 OHOS::Sensors::HandleVibratorDataFuzzTest(data, size);
57 return 0;
58 }