• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 U32_AT_SIZE = 4;
27 constexpr size_t MESSAGE_SIZE = 4;
28 constexpr size_t DCAMERA_SHIFT_24 = 24;
29 constexpr size_t DCAMERA_SHIFT_16 = 16;
30 constexpr size_t DCAMERA_SHIFT_8 = 8;
31 class ServiceCenterStatusCallbackStubFuzzTest : public ServiceCenterStatusCallbackStub {
32 public:
33     ServiceCenterStatusCallbackStubFuzzTest() = default;
34     virtual ~ServiceCenterStatusCallbackStubFuzzTest() = default;
OnInstallFinished(std::string installResult)35     virtual int32_t OnInstallFinished(std::string installResult) override
36     {
37         return 0;
38     }
OnDelayedHeartbeat(std::string installResult)39     virtual int32_t OnDelayedHeartbeat(std::string installResult) override
40     {
41         return 0;
42     }
OnServiceCenterReceived(std::string installResultStr)43     virtual int32_t OnServiceCenterReceived(std::string installResultStr) override
44     {
45         return 0;
46     }
47 };
GetU32Data(const char * ptr)48 uint32_t GetU32Data(const char* ptr)
49 {
50     return (ptr[0] << DCAMERA_SHIFT_24) | (ptr[1] << DCAMERA_SHIFT_16) | (ptr[2] << DCAMERA_SHIFT_8) | (ptr[3]);
51 }
52 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)53 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
54 {
55     ServiceCenterStatusCallbackStubFuzzTest serviceCenterStatusCallbackStub;
56     MessageParcel datas;
57     datas.WriteBuffer(data, size);
58     datas.RewindRead(0);
59     int32_t code = reinterpret_cast<uintptr_t>(data);
60     MessageParcel reply;
61     MessageOption option;
62     serviceCenterStatusCallbackStub.OnRemoteRequest(code, datas, reply, option);
63     return true;
64 }
65 }
66 
67 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70     /* Run your code on data */
71     if (data == nullptr) {
72         return 0;
73     }
74 
75     if (size < OHOS::U32_AT_SIZE) {
76         return 0;
77     }
78 
79     char* ch = static_cast<char*>(malloc(size + 1));
80     if (ch == nullptr) {
81         return 0;
82     }
83 
84     (void)memset_s(ch, size + 1, 0x00, size + 1);
85     if (memcpy_s(ch, size, data, size) != EOK) {
86         free(ch);
87         ch = nullptr;
88         return 0;
89     }
90     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
91     free(ch);
92     ch = nullptr;
93     return 0;
94 }