• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"OHOS.DeviceUsageStats.IAppGroupCallback";
35 
36     class TestAppGroupChangeCallback : public AppGroupCallbackStub {
37     public:
38         ErrCode OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo) override;
39     };
40 
OnAppGroupChanged(const AppGroupCallbackInfo & appGroupCallbackInfo)41     ErrCode TestAppGroupChangeCallback::OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo)
42     {
43         return ERR_OK;
44     }
45 
GetU32Data(const char * ptr)46     uint32_t GetU32Data(const char* ptr)
47     {
48         return (ptr[0] << TWENTYFOUR) | (ptr[1] << SIXTEEN) | (ptr[TWO] << EIGHT) | (ptr[THREE]);
49     }
50 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)51     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
52     {
53         DelayedSingleton<FuzztestHelper>::GetInstance()->NativeTokenGet();
54         uint32_t code = GetU32Data(data);
55         MessageParcel datas;
56         datas.WriteInterfaceToken(APP_GOUNP_ACTIVE_TOKEN);
57         datas.WriteBuffer(data, size);
58         datas.RewindRead(0);
59         MessageParcel reply;
60         MessageOption option;
61         DelayedSingleton<TestAppGroupChangeCallback>::GetInstance()->OnRemoteRequest(code % MAX_CODE,
62             datas, reply, option);
63         return true;
64     }
65 } // namespace DeviceUsageStats
66 } // namespace OHOS
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::DeviceUsageStats::U32_AT_SIZE) {
77         return 0;
78     }
79     char* ch = (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 
91     OHOS::DeviceUsageStats::DoSomethingInterestingWithMyAPI(ch, size);
92     free(ch);
93     ch = nullptr;
94     return 0;
95 }
96 
97