• 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 "scheduleconnectabilitydone_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "ability_manager_client.h"
22 #include "ability_record.h"
23 #include "ability_connect_manager.h"
24 
25 using namespace OHOS::AAFwk;
26 using namespace OHOS::AppExecFwk;
27 
28 namespace OHOS {
29 namespace {
30 constexpr size_t FOO_MAX_LEN = 1024;
31 constexpr size_t U32_AT_SIZE = 4;
32 constexpr int32_t UID_TEST = 100;
33 constexpr int OFFSET_ZERO = 24;
34 }
GetFuzzAbilityToken(AbilityType type)35 sptr<Token> GetFuzzAbilityToken(AbilityType type)
36 {
37     sptr<Token> token = nullptr;
38 
39     AbilityRequest abilityRequest;
40     abilityRequest.uid = UID_TEST;
41     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
42     abilityRequest.abilityInfo.name = "MainAbility";
43     abilityRequest.abilityInfo.type = type;
44     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
45     if (abilityRecord) {
46         token = abilityRecord->GetToken();
47     }
48 
49     return token;
50 }
GetU32Data(const char * ptr)51 uint32_t GetU32Data(const char* ptr)
52 {
53     // convert fuzz input data to an integer
54     return (ptr[0] << OFFSET_ZERO) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
55 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)56 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
57 {
58     auto abilitymgr = AbilityManagerClient::GetInstance();
59     int userId = static_cast<int>(GetU32Data(data));
60     auto connectManager = new AbilityConnectManager(userId);
61     sptr<IRemoteObject> remoteObject;
62     if (!abilitymgr) {
63         return false;
64     }
65 
66     // get token
67     sptr<IRemoteObject> token = GetFuzzAbilityToken(AbilityType::PAGE);
68     if (!token) {
69         std::cout << "Get ability token failed." << std::endl;
70         return false;
71     }
72 
73     // get serviceToken
74     sptr<IRemoteObject> serviceToken = GetFuzzAbilityToken(AbilityType::SERVICE);
75     if (!serviceToken) {
76         std::cout << "Get service ability token failed." << std::endl;
77         return false;
78     }
79 
80     if (connectManager) {
81         connectManager->ScheduleConnectAbilityDoneLocked(serviceToken, remoteObject);
82     }
83 
84     if (abilitymgr->ScheduleConnectAbilityDone(token, remoteObject) != 0) {
85         return false;
86     }
87 
88     return true;
89 }
90 }
91 
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94 {
95     /* Run your code on data */
96     if (data == nullptr) {
97         std::cout << "invalid data" << std::endl;
98         return 0;
99     }
100 
101     /* Validate the length of size */
102     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
103         return 0;
104     }
105 
106     char* ch = (char *)malloc(size + 1);
107     if (ch == nullptr) {
108         std::cout << "malloc failed." << std::endl;
109         return 0;
110     }
111 
112     (void)memset_s(ch, size + 1, 0x00, size + 1);
113     if (memcpy_s(ch, size, data, size) != EOK) {
114         std::cout << "copy failed." << std::endl;
115         free(ch);
116         ch = nullptr;
117         return 0;
118     }
119 
120     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
121     free(ch);
122     ch = nullptr;
123     return 0;
124 }
125 
126