• 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 "socperf_fuzzer.h"
17 
18 #define private public
19 #include "socperf_server.h"
20 #include "socperf_stub.h"
21 
22 namespace OHOS {
23 namespace SOCPERF {
24     constexpr int32_t MIN_LEN = 4;
25     static bool isInited = false;
26 
onRemoteRequest(uint32_t code,MessageParcel & data)27     int32_t onRemoteRequest(uint32_t code, MessageParcel& data)
28     {
29         int32_t ret = -1;
30         MessageParcel reply;
31         MessageOption option;
32         if (!isInited) {
33             if (!DelayedSingleton<SocPerfServer>::GetInstance()->socPerf.Init()) {
34                 return ret;
35             }
36             isInited = true;
37         }
38         ret = DelayedSingleton<SocPerfServer>::GetInstance()->OnRemoteRequest(code, data, reply, option);
39         return ret;
40     }
41 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)42     void DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
43     {
44         if (size <= MIN_LEN) {
45             return;
46         }
47 
48         MessageParcel dataMessageParcel;
49         if (!dataMessageParcel.WriteInterfaceToken(SocPerfStub::GetDescriptor())) {
50             return;
51         }
52 
53         uint32_t code = *(reinterpret_cast<const uint32_t*>(data));
54         size -= sizeof(uint32_t);
55 
56         dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size);
57         dataMessageParcel.RewindRead(0);
58 
59         onRemoteRequest(code, dataMessageParcel);
60     }
61 } // namespace SOCPERF
62 } // namespace OHOS
63 
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
66 {
67     /* Run your code on data */
68     OHOS::SOCPERF::DoSomethingInterestingWithMyAPI(data, size);
69     return 0;
70 }
71 
72