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