• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "playvibratorcustom_fuzzer.h"
17 
18 #include "securec.h"
19 
20 #include "vibrator_agent.h"
21 
22 namespace OHOS {
23 namespace Sensors {
24 namespace {
25 constexpr size_t DATA_MIN_SIZE = 20;
26 } // namespace
27 
28 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)29 size_t GetObject(const uint8_t *data, size_t size, T &object)
30 {
31     size_t objectSize = sizeof(object);
32     if (objectSize > size) {
33         return 0;
34     }
35     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
36     if (ret != EOK) {
37         return 0;
38     }
39     return objectSize;
40 }
41 
PlayVibratorCustomFuzzTest(const uint8_t * data,size_t size)42 void PlayVibratorCustomFuzzTest(const uint8_t *data, size_t size)
43 {
44     if (data == nullptr || size < DATA_MIN_SIZE) {
45         return;
46     }
47     size_t startPos = 0;
48     int32_t fd { 0 };
49     startPos += GetObject<int32_t>(data + startPos, size - startPos, fd);
50     int64_t offset { 0 };
51     startPos += GetObject<int64_t>(data + startPos, size - startPos, offset);
52     int64_t length { 0 };
53     GetObject<int64_t>(data + startPos, size - startPos, length);
54     OHOS::Sensors::PlayVibratorCustom(fd, offset, length);
55 }
56 } // namespace Sensors
57 } // namespace OHOS
58 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
60 {
61     OHOS::Sensors::PlayVibratorCustomFuzzTest(data, size);
62     return 0;
63 }