1 /*
2 * Copyright (c) 2023 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 #include "kvdbservicestub_fuzzer.h"
16
17 #include <cstddef>
18 #include <cstdint>
19
20 #include "bootstrap.h"
21 #include "ipc_skeleton.h"
22 #include "kvdb_service_impl.h"
23 #include "message_parcel.h"
24 #include "securec.h"
25
26 using namespace OHOS::DistributedData;
27 using namespace OHOS::DistributedKv;
28
29 namespace OHOS {
30 const std::u16string INTERFACE_TOKEN = u"OHOS.DistributedKv.KVFeature";
31 constexpr uint32_t CODE_MIN = 0;
32 constexpr uint32_t CODE_MAX = static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_BUTT) + 1;
33 constexpr size_t NUM_MIN = 5;
34 constexpr size_t NUM_MAX = 12;
35
OnRemoteRequestFuzz(const uint8_t * data,size_t size)36 bool OnRemoteRequestFuzz(const uint8_t *data, size_t size)
37 {
38 std::shared_ptr<KVDBServiceImpl> kvdbServiceImpl = std::make_shared<KVDBServiceImpl>();
39 std::shared_ptr<ExecutorPool> executor = std::make_shared<ExecutorPool>(NUM_MAX, NUM_MIN);
40 kvdbServiceImpl->OnBind(
41 { "KvdbServiceStubFuzz", static_cast<uint32_t>(IPCSkeleton::GetSelfTokenID()), std::move(executor) });
42
43 Bootstrap::GetInstance().LoadComponents();
44 Bootstrap::GetInstance().LoadDirectory();
45 Bootstrap::GetInstance().LoadCheckers();
46 Bootstrap::GetInstance().LoadNetworks();
47
48 uint32_t code = static_cast<uint32_t>(*data) % (CODE_MAX - CODE_MIN + 1) + CODE_MIN;
49 MessageParcel request;
50 request.WriteInterfaceToken(INTERFACE_TOKEN);
51 request.WriteBuffer(data, size);
52 request.RewindRead(0);
53 MessageParcel reply;
54 std::shared_ptr<KVDBServiceStub> kvdbServiceStub = kvdbServiceImpl;
55 kvdbServiceStub->OnRemoteRequest(code, request, reply);
56 return true;
57 }
58 } // namespace OHOS
59
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
62 {
63 if (data == nullptr) {
64 return 0;
65 }
66
67 OHOS::OnRemoteRequestFuzz(data, size);
68
69 return 0;
70 }