1 /*
2 * Copyright (c) 2022 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 "vibrator_fuzzer.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include "hdf_log.h"
20 #include "v2_0/vibrator_interface_proxy.h"
21 #include "v2_0/vibrator_interface_stub.h"
22
23 using namespace OHOS::HDI::Vibrator::V2_0;
24
25 namespace OHOS {
26 constexpr size_t THRESHOLD = 10;
27 constexpr int32_t OFFSET = 4;
28 constexpr int32_t ARRAY0 = 0;
29 constexpr int32_t ARRAY1 = 1;
30 constexpr int32_t ARRAY2 = 2;
31 constexpr int32_t ARRAY3 = 3;
32 constexpr int32_t DIGIT8 = 8;
33 constexpr int32_t DIGIT16 = 16;
34 constexpr int32_t DIGIT24 = 24;
35 const std::u16string VIBRATOR_INTERFACE_TOKEN = u"ohos.hdi.vibrator.v1_1.IVibratorInterface";
36
Convert2Uint32(const uint8_t * ptr)37 uint32_t Convert2Uint32(const uint8_t* ptr)
38 {
39 if (ptr == nullptr) {
40 return 0;
41 }
42 /*
43 * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
44 * and the third digit no left
45 */
46 return (ptr[ARRAY0] << DIGIT24) | (ptr[ARRAY1] << DIGIT16) | (ptr[ARRAY2] << DIGIT8) | (ptr[ARRAY3]);
47 }
48
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)49 bool DoSomethingInterestingWithMyAPI(const uint8_t* rawData, size_t size)
50 {
51 if (rawData == nullptr) {
52 return false;
53 }
54 uint32_t code = Convert2Uint32(rawData);
55 rawData = rawData + OFFSET;
56 size = size - OFFSET;
57
58 MessageParcel data;
59 data.WriteInterfaceToken(VIBRATOR_INTERFACE_TOKEN);
60 data.WriteBuffer(rawData, size);
61 data.RewindRead(0);
62 MessageParcel reply;
63 MessageOption option;
64 sptr<IVibratorInterface> g_vibratorInterface = IVibratorInterface::Get(false);
65 if (g_vibratorInterface == nullptr) {
66 HDF_LOGE("%{public}s:IVibratorInterface::Get() failed.", __func__);
67 return false;
68 }
69 sptr<VibratorInterfaceStub> vibratorInterface = new VibratorInterfaceStub(g_vibratorInterface);
70 if (vibratorInterface == nullptr) {
71 HDF_LOGE("%{public}s:new VibratorInterfaceStub failed.", __func__);
72 return false;
73 }
74 vibratorInterface->OnRemoteRequest(code, data, reply, option);
75
76 return true;
77 }
78 }
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 if (size < OHOS::THRESHOLD) {
84 return 0;
85 }
86
87 if (data == nullptr) {
88 return 0;
89 }
90
91 if (size < sizeof(int32_t)) {
92 return 0;
93 }
94 /* Run your code on data */
95 OHOS::DoSomethingInterestingWithMyAPI(data, size);
96 return 0;
97 }
98
99