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