• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "startvibratoronceenhanced_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21 
22 #include "securec.h"
23 #include "vibrator_agent.h"
24 
25 namespace {
26 constexpr int32_t MAX_VIBRATOR_TIME = 1800000;
27 constexpr int32_t MIN_VIBRATOR_TIME = 0;
28 constexpr size_t U32_AT_SIZE = 12;
29 }  // namespace
30 
31 namespace OHOS {
32 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)33 size_t GetObject(const uint8_t *data, size_t size, T &object)
34 {
35     size_t objectSize = sizeof(object);
36     if (objectSize > size) {
37         return 0;
38     }
39     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
40     if (ret != EOK) {
41         return 0;
42     }
43     return objectSize;
44 }
45 
StartVibratorOnceEnhancedFuzzTest(const uint8_t * data,size_t size)46 bool StartVibratorOnceEnhancedFuzzTest(const uint8_t *data, size_t size)
47 {
48     if (data == nullptr || size < U32_AT_SIZE) {
49         return false;
50     }
51     VibratorIdentifier identifier;
52     size_t startPos = 0;
53     int32_t duration = 0;
54     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.deviceId);
55     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.vibratorId);
56     GetObject<int32_t>(data + startPos, size - startPos, duration);
57 
58     int32_t ret = OHOS::Sensors::StartVibratorOnceEnhanced(identifier, duration);
59     if (((duration <= MIN_VIBRATOR_TIME || duration > MAX_VIBRATOR_TIME) && ret == 0) ||
60         ((duration > MIN_VIBRATOR_TIME && duration <= MAX_VIBRATOR_TIME) && ret != 0)) {
61         return false;
62     }
63     return true;
64 }
65 } // namespace OHOS
66 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69     OHOS::StartVibratorOnceEnhancedFuzzTest(data, size);
70     return 0;
71 }