• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "formabilityconnection_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "form_ability_connection.h"
22 #define private public
23 #define protected public
24 #include "form_ams_helper.h"
25 #undef private
26 #undef protected
27 #include "securec.h"
28 
29 using namespace OHOS::AppExecFwk;
30 
31 namespace OHOS {
32 constexpr size_t FOO_MAX_LEN = 1024;
33 constexpr size_t U32_AT_SIZE = 4;
34 constexpr uint8_t ENABLE = 2;
GetU32Data(const char * ptr)35 uint32_t GetU32Data(const char* ptr)
36 {
37     // convert fuzz input data to an integer
38     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
39 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)40 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
41 {
42     FormAbilityConnection formAbilityConnection;
43     AppExecFwk::ElementName element;
44     sptr<IRemoteObject> remoteObjects = nullptr;
45     int resultCode = static_cast<int>(GetU32Data(data));
46     formAbilityConnection.OnAbilityConnectDone(element, remoteObjects, resultCode);
47     formAbilityConnection.GetProviderKey();
48     std::string bundleName(data, size);
49     std::string abilityName(data, size);
50     formAbilityConnection.SetProviderKey(bundleName, abilityName);
51     bool isFreeInstall = *data % ENABLE;
52     formAbilityConnection.SetFreeInstall(isFreeInstall);
53     int64_t formId = static_cast<int64_t>(GetU32Data(data));
54     formAbilityConnection.SetFormId(formId);
55     formAbilityConnection.GetFormId();
56     sptr<IRemoteObject> hostToken = nullptr;
57     formAbilityConnection.SetHostToken(hostToken);
58     formAbilityConnection.GetHostToken();
59     sptr<IRemoteObject> providerToken = nullptr;
60     formAbilityConnection.SetProviderToken(providerToken);
61     formAbilityConnection.GetProviderToken();
62     int32_t connectId = static_cast<int32_t>(GetU32Data(data));
63     formAbilityConnection.SetConnectId(connectId);
64     FormAmsHelper formAmsHelper;
65     formAmsHelper.GetAbilityManager();
66     Want want;
67     sptr<AAFwk::IAbilityConnection> connect = nullptr;
68     formAmsHelper.ConnectServiceAbility(want, connect);
69     formAmsHelper.DisconnectServiceAbility(connect);
70     formAmsHelper.DisconnectServiceAbilityDelay(connect);
71     sptr<AAFwk::IAbilityManager> abilityManager = nullptr;
72     formAmsHelper.SetAbilityManager(abilityManager);
73     formAmsHelper.DisconnectAbilityTask(connect);
74     int32_t userId = static_cast<int32_t>(GetU32Data(data));
75     formAmsHelper.StartAbility(want, userId);
76     return formAbilityConnection.GetConnectId();
77 }
78 }
79 
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83     /* Run your code on data */
84     if (data == nullptr) {
85         return 0;
86     }
87 
88     if (size < OHOS::U32_AT_SIZE) {
89         return 0;
90     }
91 
92     /* Validate the length of size */
93     if (size == 0 || size > OHOS::FOO_MAX_LEN) {
94         return 0;
95     }
96 
97     char* ch = (char *)malloc(size + 1);
98     if (ch == nullptr) {
99         return 0;
100     }
101 
102     (void)memset_s(ch, size + 1, 0x00, size + 1);
103     if (memcpy_s(ch, size, data, size) != EOK) {
104         free(ch);
105         ch = nullptr;
106         return 0;
107     }
108 
109     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
110     free(ch);
111     ch = nullptr;
112     return 0;
113 }
114 
115