1 /*
2 * Copyright (c) 2024 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 "servicecenterstatuscallbackstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #include "service_center_status_callback_stub.h"
23 #include "securec.h"
24 using namespace OHOS::AppExecFwk;
25 namespace OHOS {
26 constexpr size_t FOO_MAX_LEN = 1024;
27 constexpr size_t U32_AT_SIZE = 4;
28 constexpr size_t MESSAGE_SIZE = 4;
29 constexpr size_t DCAMERA_SHIFT_24 = 24;
30 constexpr size_t DCAMERA_SHIFT_16 = 16;
31 constexpr size_t DCAMERA_SHIFT_8 = 8;
32 class ServiceCenterStatusCallbackStubFuzzTest : public ServiceCenterStatusCallbackStub {
33 public:
34 ServiceCenterStatusCallbackStubFuzzTest() = default;
35 virtual ~ServiceCenterStatusCallbackStubFuzzTest() = default;
OnInstallFinished(std::string installResult)36 virtual int32_t OnInstallFinished(std::string installResult) override
37 {
38 return 0;
39 }
OnDelayedHeartbeat(std::string installResult)40 virtual int32_t OnDelayedHeartbeat(std::string installResult) override
41 {
42 return 0;
43 }
OnServiceCenterReceived(std::string installResultStr)44 virtual int32_t OnServiceCenterReceived(std::string installResultStr) override
45 {
46 return 0;
47 }
48 };
GetU32Data(const char * ptr)49 uint32_t GetU32Data(const char* ptr)
50 {
51 return (ptr[0] << DCAMERA_SHIFT_24) | (ptr[1] << DCAMERA_SHIFT_16) | (ptr[2] << DCAMERA_SHIFT_8) | (ptr[3]);
52 }
53
DoSomethingInterestingWithMyAPI(const char * data,size_t size)54 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
55 {
56 ServiceCenterStatusCallbackStubFuzzTest serviceCenterStatusCallbackStub;
57 MessageParcel datas;
58 datas.WriteBuffer(data, size);
59 datas.RewindRead(0);
60 int32_t code = reinterpret_cast<uintptr_t>(data);
61 MessageParcel reply;
62 MessageOption option;
63 serviceCenterStatusCallbackStub.OnRemoteRequest(code, datas, reply, option);
64 return true;
65 }
66 }
67
68 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
70 {
71 /* Run your code on data */
72 if (data == nullptr) {
73 return 0;
74 }
75
76 if (size < OHOS::U32_AT_SIZE) {
77 return 0;
78 }
79
80 /* Validate the length of size */
81 if (size > OHOS::FOO_MAX_LEN) {
82 return 0;
83 }
84
85 char* ch = static_cast<char*>(malloc(size + 1));
86 if (ch == nullptr) {
87 return 0;
88 }
89
90 (void)memset_s(ch, size + 1, 0x00, size + 1);
91 if (memcpy_s(ch, size, data, size) != EOK) {
92 free(ch);
93 ch = nullptr;
94 return 0;
95 }
96 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
97 free(ch);
98 ch = nullptr;
99 return 0;
100 }