• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "vibrator_interface_impl.h"
21 #include "v1_1/vibrator_interface_stub.h"
22 
23 using namespace OHOS::HDI::Vibrator::V1_1;
24 
25 namespace OHOS {
26 constexpr size_t THRESHOLD = 10;
27 constexpr int32_t OFFSET = 4;
28 const std::u16string VIBRATOR_INTERFACE_TOKEN = u"ohos.hdi.vibrator.v1_1.IVibratorInterface";
29 
Convert2Uint32(const uint8_t * ptr)30 uint32_t Convert2Uint32(const uint8_t* ptr)
31 {
32     if (ptr == nullptr) {
33         return 0;
34     }
35     /*
36      * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
37      * and the third digit no left
38      */
39     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
40 }
41 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)42 bool DoSomethingInterestingWithMyAPI(const uint8_t* rawData, size_t size)
43 {
44     if (rawData == nullptr) {
45         return false;
46     }
47     uint32_t code = Convert2Uint32(rawData);
48     rawData = rawData + OFFSET;
49     size = size - OFFSET;
50 
51     MessageParcel data;
52     data.WriteInterfaceToken(VIBRATOR_INTERFACE_TOKEN);
53     data.WriteBuffer(rawData, size);
54     data.RewindRead(0);
55     MessageParcel reply;
56     MessageOption option;
57     sptr<IVibratorInterface> g_vibratorInterface = IVibratorInterface::Get(false);
58     if (g_vibratorInterface == nullptr) {
59         HDF_LOGE("%{public}s:IVibratorInterface::Get() failed.", __func__);
60         return false;
61     }
62     sptr<VibratorInterfaceStub> vibratorInterface = new VibratorInterfaceStub(g_vibratorInterface);
63     if (vibratorInterface == nullptr) {
64         HDF_LOGE("%{public}s:new VibratorInterfaceStub failed.", __func__);
65         return false;
66     }
67     vibratorInterface->OnRemoteRequest(code, data, reply, option);
68 
69     return true;
70 }
71 }
72 
73 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76     if (size < OHOS::THRESHOLD) {
77         return 0;
78     }
79 
80     /* Run your code on data */
81     OHOS::DoSomethingInterestingWithMyAPI(data, size);
82     return 0;
83 }
84 
85