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 "bgtaskSubscriber_fuzzer.h"
17 #include "ibackground_task_subscriber.h"
18 #include "securec.h"
19
20 #define private public
21 #include "background_task_subscriber_stub.h"
22 #include "background_task_subscriber.h"
23
24 namespace OHOS {
25 namespace BackgroundTaskMgr {
26 constexpr int32_t U32_AT_SIZE = 4;
27 constexpr int32_t MAX_CODE = 15;
28 constexpr uint8_t TWENTYFOUR = 24;
29 constexpr uint8_t SIXTEEN = 16;
30 constexpr uint8_t EIGHT = 8;
31 constexpr int32_t THREE = 3;
32 constexpr int32_t TWO = 2;
33 const std::u16string BACKGROUND_TASK_MGR_SUBSCRIBER_TOKEN = u"ohos.resourceschedule.IBackgroundTaskSubscriber";
34 class TestBackgroundTaskSubscriber : public BackgroundTaskSubscriber {
35 public:
TestBackgroundTaskSubscriber()36 TestBackgroundTaskSubscriber() : BackgroundTaskSubscriber() {}
37 };
38
GetU32Data(const char * ptr)39 uint32_t GetU32Data(const char* ptr)
40 {
41 return (ptr[0] << TWENTYFOUR) | (ptr[1] << SIXTEEN) | (ptr[TWO] << EIGHT) | (ptr[THREE]);
42 }
43
DoSomethingInterestingWithMyAPI(const char * data,size_t size)44 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
45 {
46 uint32_t code = GetU32Data(data);
47 MessageParcel datas;
48 datas.WriteInterfaceToken(BACKGROUND_TASK_MGR_SUBSCRIBER_TOKEN);
49 datas.WriteBuffer(data, size);
50 datas.RewindRead(0);
51 MessageParcel reply;
52 MessageOption option;
53 auto subscriber = TestBackgroundTaskSubscriber();
54 auto subscriberImpl = std::make_shared<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl>(subscriber);
55 subscriberImpl->HandleOnConnected();
56 subscriberImpl->HandleOnDisconnected();
57 subscriberImpl->HandleOnTransientTaskStart(datas);
58 subscriberImpl->HandleOnTransientTaskEnd(datas);
59 subscriberImpl->HandleOnAppTransientTaskStart(datas);
60 subscriberImpl->HandleOnAppTransientTaskEnd(datas);
61 subscriberImpl->HandleOnContinuousTaskStart(datas);
62 subscriberImpl->HandleOnContinuousTaskCancel(datas);
63 subscriberImpl->HandleOnAppContinuousTaskStop(datas);
64 subscriberImpl->HandleOnAppEfficiencyResourcesApply(datas);
65 subscriberImpl->HandleOnAppEfficiencyResourcesReset(datas);
66 subscriberImpl->HandleOnProcEfficiencyResourcesApply(datas);
67 subscriberImpl->HandleOnProcEfficiencyResourcesReset(datas);
68 subscriberImpl->OnRemoteRequest(code % MAX_CODE, datas, reply, option);
69 return true;
70 }
71 } // BackgroundTaskMgr
72 } // OHOS
73
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77 /* Run your code on data */
78 if (data == nullptr) {
79 return 0;
80 }
81
82 if (size < OHOS::BackgroundTaskMgr::U32_AT_SIZE) {
83 return 0;
84 }
85
86 char* ch = (char *)malloc(size + 1);
87 if (ch == nullptr) {
88 return 0;
89 }
90
91 (void)memset_s(ch, size + 1, 0x00, size+1);
92 if (memcpy_s(ch, size, data, size) != EOK) {
93 free(ch);
94 ch = nullptr;
95 return 0;
96 }
97
98 OHOS::BackgroundTaskMgr::DoSomethingInterestingWithMyAPI(ch, size);
99 free(ch);
100 ch = nullptr;
101 return 0;
102 }
103