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 "motion_fuzzer.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include "hdf_log.h"
20 #include "v1_1/motion_interface_stub.h"
21 
22 using namespace OHOS::HDI::Motion::V1_1;
23 
24 namespace OHOS {
25 constexpr size_t THRESHOLD = 10;
26 constexpr int32_t OFFSET = 4;
27 constexpr int32_t ARRAY0 = 0;
28 constexpr int32_t ARRAY1 = 1;
29 constexpr int32_t ARRAY2 = 2;
30 constexpr int32_t ARRAY3 = 3;
31 constexpr int32_t DIGIT8 = 8;
32 constexpr int32_t DIGIT16 = 16;
33 constexpr int32_t DIGIT24 = 24;
34 
35 const std::u16string MOTION_INTERFACE_TOKEN = u"ohos.hdi.motion.v1_1.IMotionInterface";
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(MOTION_INTERFACE_TOKEN);
60     data.WriteBuffer(rawData, size);
61     data.RewindRead(0);
62     MessageParcel reply;
63     MessageOption option;
64     sptr<OHOS::HDI::Motion::V1_1::IMotionInterface> g_motionInterface =
65         OHOS::HDI::Motion::V1_1::IMotionInterface::Get(false);
66     if (g_motionInterface == nullptr) {
67         HDF_LOGE("%{public}s:IMotionInterface::Get() failed.", __func__);
68         return false;
69     }
70     sptr<OHOS::HDI::Motion::V1_1::MotionInterfaceStub> motionInterface =
71         new OHOS::HDI::Motion::V1_1::MotionInterfaceStub(g_motionInterface);
72     if (motionInterface == nullptr) {
73         HDF_LOGE("%{public}s:new MotionInterfaceStub failed.", __func__);
74         return false;
75     }
76     motionInterface->OnRemoteRequest(code, data, reply, option);
77 
78     return true;
79 }
80 }
81 
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85     if (size < OHOS::THRESHOLD) {
86         return 0;
87     }
88 
89     /* Run your code on data */
90     OHOS::DoSomethingInterestingWithMyAPI(data, size);
91     return 0;
92 }
93 
94