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