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