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 "dbinderservice_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include "dbinder_service.h"
21 #include "dbinder_service_stub.h"
22 #include "string_ex.h"
23 
24 namespace OHOS {
OnRemoteMessageTaskTest(const uint8_t * data,size_t size)25     void OnRemoteMessageTaskTest(const uint8_t* data, size_t size)
26     {
27         #define VERSION_NUM 1
28         #define MESSAGE_INVALID 5
29         if ((data == nullptr) || (size == 0)) {
30             return;
31         }
32 
33         OHOS::DBinderService dBinderService;
34         std::shared_ptr<struct DHandleEntryTxRx> handleEntry = std::make_shared<struct DHandleEntryTxRx>();
35         handleEntry->head.len = sizeof(DHandleEntryTxRx);
36         handleEntry->head.version = VERSION_NUM;
37         handleEntry->dBinderCode = MESSAGE_INVALID;
38 
39         dBinderService.OnRemoteMessageTask(handleEntry);
40     }
41 
QuerySessionObjectTest(const uint8_t * data,size_t size)42     void QuerySessionObjectTest(const uint8_t* data, size_t size)
43     {
44         #define INVALID 0
45         if ((data == nullptr) || (size == 0)) {
46             return;
47         }
48         OHOS::DBinderService dBinderService;
49         dBinderService.QuerySessionObject(INVALID);
50     }
51 
RegisterRemoteProxy1Test(const uint8_t * data,size_t size)52     void RegisterRemoteProxy1Test(const uint8_t* data, size_t size)
53     {
54         if ((data == nullptr) || (size == 0)) {
55             return;
56         }
57 
58         OHOS::DBinderService dBinderService;
59         std::string sessionNameTmp(reinterpret_cast<const char*>(data), size);
60         std::u16string serviceName = Str8ToStr16(sessionNameTmp);
61         sptr<IRemoteObject> binderObject = NULL;
62         dBinderService.RegisterRemoteProxy(serviceName, binderObject);
63     }
64 
RegisterRemoteProxy2Test(const uint8_t * data,size_t size)65     void RegisterRemoteProxy2Test(const uint8_t* data, size_t size)
66     {
67         if ((data == nullptr) || (size < sizeof(int32_t))) {
68             return;
69         }
70         OHOS::DBinderService dBinderService;
71         std::string sessionNameTmp(reinterpret_cast<const char*>(data), size);
72         std::u16string serviceName = Str8ToStr16(sessionNameTmp);
73         int32_t abilityId = *(reinterpret_cast<const int32_t*>(data));
74         dBinderService.RegisterRemoteProxy(serviceName, abilityId);
75     }
76 }
77 
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81     /* Run your code on data */
82     OHOS::OnRemoteMessageTaskTest(data, size);
83     OHOS::QuerySessionObjectTest(data, size);
84     OHOS::RegisterRemoteProxy1Test(data, size);
85     OHOS::RegisterRemoteProxy2Test(data, size);
86     return 0;
87 }
88 
89