• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "rdbservicestub_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "ipc_skeleton.h"
22 #include "rdb_service_impl.h"
23 #include "message_parcel.h"
24 #include "securec.h"
25 
26 using namespace OHOS::DistributedRdb;
27 
28 namespace OHOS {
29 const std::u16string INTERFACE_TOKEN = u"OHOS.DistributedRdb.IRdbService";
30 constexpr uint32_t CODE_MIN = 0;
31 constexpr uint32_t CODE_MAX = static_cast<uint32_t>(RdbServiceCode::RDB_SERVICE_CMD_MAX) + 1;
32 constexpr size_t NUM_MIN = 5;
33 constexpr size_t NUM_MAX = 12;
34 
OnRemoteRequestFuzz(const uint8_t * data,size_t size)35 bool OnRemoteRequestFuzz(const uint8_t *data, size_t size)
36 {
37     std::shared_ptr<RdbServiceImpl> rdbServiceImpl = std::make_shared<RdbServiceImpl>();
38     std::shared_ptr<ExecutorPool> executor = std::make_shared<ExecutorPool>(NUM_MAX, NUM_MIN);
39     rdbServiceImpl->OnBind(
40         { "RdbServiceStubFuzz", static_cast<uint32_t>(IPCSkeleton::GetSelfTokenID()), std::move(executor) });
41 
42     uint32_t code = static_cast<uint32_t>(*data) % (CODE_MAX - CODE_MIN + 1) + CODE_MIN;
43     MessageParcel request;
44     request.WriteInterfaceToken(INTERFACE_TOKEN);
45     request.WriteBuffer(data, size);
46     request.RewindRead(0);
47     MessageParcel reply;
48     std::shared_ptr<RdbServiceStub> rdbServiceStub = rdbServiceImpl;
49     rdbServiceStub->OnRemoteRequest(code, request, reply);
50     return true;
51 }
52 } // namespace OHOS
53 
54 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
56 {
57     if (data == nullptr) {
58         return 0;
59     }
60 
61     OHOS::OnRemoteRequestFuzz(data, size);
62 
63     return 0;
64 }