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