• 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 "startvibratorenhanced_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include <string>
22 
23 #include "securec.h"
24 #include "vibrator_agent.h"
25 
26 namespace OHOS {
27 namespace {
28 constexpr size_t U32_AT_SIZE = 28;
29 constexpr char END_CHAR = '\0';
30 constexpr size_t LEN = 20;
31 } // namespace
32 
33 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)34 size_t GetObject(const uint8_t *data, size_t size, T &object)
35 {
36     size_t objectSize = sizeof(object);
37     if (objectSize > size) {
38         return 0;
39     }
40     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
41     if (ret != EOK) {
42         return 0;
43     }
44     return objectSize;
45 }
46 
StartVibratorEnhancedFuzzTest(const uint8_t * data,size_t size)47 bool StartVibratorEnhancedFuzzTest(const uint8_t *data, size_t size)
48 {
49     if (data == nullptr || size < U32_AT_SIZE) {
50         return false;
51     }
52     VibratorIdentifier identifier;
53     size_t startPos = 0;
54     char effectId[LEN + 1];
55     effectId[LEN] = END_CHAR;
56     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.deviceId);
57     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.vibratorId);
58     for (size_t i = 0; i < LEN; ++i) {
59         startPos += GetObject<char>(data + startPos, size - startPos, effectId[i]);
60     }
61 
62     int32_t ret = OHOS::Sensors::StartVibratorEnhanced(identifier, effectId);
63     int32_t ret2 = std::strcmp(effectId, "haptic.clock.timer");
64     if ((ret2 != 0 && ret == 0) || (ret2 == 0 && ret != 0)) {
65         return false;
66     }
67     return true;
68 }
69 } // namespace OHOS
70 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
72 {
73     OHOS::StartVibratorEnhancedFuzzTest(data, size);
74     return 0;
75 }