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 "appgroupcallbackstub_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "app_mgr_interface.h"
20
21 #define private public
22 #include "app_group_callback_stub.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace DeviceUsageStats {
28 constexpr uint32_t U32_AT_SIZE = 4;
29 constexpr uint32_t MAX_CODE = 2; // current max code is 2
30 constexpr uint8_t TWENTYFOUR = 24;
31 constexpr uint8_t SIXTEEN = 16;
32 constexpr uint8_t EIGHT = 8;
33 constexpr uint8_t TWO = 2;
34 constexpr uint8_t THREE = 3;
35 const std::u16string APP_GOUNP_ACTIVE_TOKEN = u"Resourceschedule.IAppGroupCallback";
GetU32Data(const char * ptr)36 uint32_t GetU32Data(const char* ptr)
37 {
38 return (ptr[0] << TWENTYFOUR) | (ptr[1] << SIXTEEN) | (ptr[TWO] << EIGHT) | (ptr[THREE]);
39 }
40
DoSomethingInterestingWithMyAPI(const char * data,size_t size)41 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
42 {
43 DelayedSingleton<FuzztestHelper>::GetInstance()->NativeTokenGet();
44 uint32_t code = GetU32Data(data);
45 MessageParcel datas;
46 datas.WriteInterfaceToken(APP_GOUNP_ACTIVE_TOKEN);
47 datas.WriteBuffer(data, size);
48 datas.RewindRead(0);
49 MessageParcel reply;
50 MessageOption option;
51 DelayedSingleton<AppGroupCallbackStub>::GetInstance()->OnRemoteRequest(code % MAX_CODE, datas, reply, option);
52 return true;
53 }
54 } // namespace DeviceUsageStats
55 } // namespace OHOS
56
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
59 {
60 /* Run your code on data */
61 if (data == nullptr) {
62 return 0;
63 }
64
65 if (size < OHOS::DeviceUsageStats::U32_AT_SIZE) {
66 return 0;
67 }
68 char* ch = (char *)malloc(size + 1);
69 if (ch == nullptr) {
70 return 0;
71 }
72
73 (void)memset_s(ch, size + 1, 0x00, size + 1);
74 if (memcpy_s(ch, size, data, size) != EOK) {
75 free(ch);
76 ch = nullptr;
77 return 0;
78 }
79
80 OHOS::DeviceUsageStats::DoSomethingInterestingWithMyAPI(ch, size);
81 free(ch);
82 ch = nullptr;
83 return 0;
84 }
85
86