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 "extensionrecordmanager_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #include "extension_record_manager.h"
23 #undef private
24 
25 #include "ability_record.h"
26 
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::AppExecFwk;
29 
30 namespace OHOS {
31 namespace {
32 constexpr int INPUT_ZERO = 0;
33 constexpr int INPUT_ONE = 1;
34 constexpr int INPUT_TWO = 2;
35 constexpr int INPUT_THREE = 3;
36 constexpr size_t FOO_MAX_LEN = 1024;
37 constexpr size_t U32_AT_SIZE = 4;
38 constexpr size_t OFFSET_ZERO = 24;
39 constexpr size_t OFFSET_ONE = 16;
40 constexpr size_t OFFSET_TWO = 8;
41 constexpr uint8_t ENABLE = 2;
42 } // namespace
43 
GetU32Data(const char * ptr)44 uint32_t GetU32Data(const char* ptr)
45 {
46     // convert fuzz input data to an integer
47     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) |
48         ptr[INPUT_THREE];
49 }
50 
GetFuzzAbilityToken()51 sptr<Token> GetFuzzAbilityToken()
52 {
53     sptr<Token> token = nullptr;
54     AbilityRequest abilityRequest;
55     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
56     abilityRequest.abilityInfo.name = "MainAbility";
57     abilityRequest.abilityInfo.type = AbilityType::DATA;
58     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
59     if (abilityRecord) {
60         token = abilityRecord->GetToken();
61     }
62     return token;
63 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)64 bool DoSomethingInterestingWithMyAPI(const char *data, size_t size)
65 {
66     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
67     auto extensionRecordManager = std::make_shared<AbilityRuntime::ExtensionRecordManager>(int32Param);
68     extensionRecordManager->GenerateExtensionRecordId(int32Param);
69     std::shared_ptr<AbilityRuntime::ExtensionRecord> record;
70     extensionRecordManager->AddExtensionRecord(int32Param, record);
71     extensionRecordManager->RemoveExtensionRecord(int32Param);
72     extensionRecordManager->AddExtensionRecordToTerminatedList(int32Param);
73     AppExecFwk::AbilityInfo abilityInfo;
74     extensionRecordManager->IsBelongToManager(abilityInfo);
75     auto focusToken = GetFuzzAbilityToken();
76     extensionRecordManager->IsFocused(int32Param, focusToken, focusToken);
77     std::vector<std::string> extensionList;
78     extensionRecordManager->GetActiveUIExtensionList(int32Param, extensionList);
79     std::string strParam(data, size);
80     extensionRecordManager->GetActiveUIExtensionList(strParam, extensionList);
81     AAFwk::AbilityRequest abilityRequest;
82     extensionRecordManager->StartAbility(abilityRequest);
83     std::shared_ptr<AbilityRuntime::ExtensionRecord> extensionRecord;
84     extensionRecordManager->CreateExtensionRecord(abilityRequest, strParam, extensionRecord, int32Param);
85     bool boolParam = *data % ENABLE;
86     extensionRecordManager->IsPreloadExtensionRecord(abilityRequest, strParam, extensionRecord, boolParam);
87     std::shared_ptr<AAFwk::AbilityRecord> abilityRecord;
88     extensionRecordManager->AddPreloadUIExtensionRecord(abilityRecord);
89     AbilityRuntime::ExtensionRecordManager::PreLoadUIExtensionMapKey preLoadUIExtensionInfo;
90     extensionRecordManager->RemoveAllPreloadUIExtensionRecord(preLoadUIExtensionInfo);
91     std::tuple<std::string, std::string, std::string, std::string> extensionRecordMapKey;
92     extensionRecordManager->RemovePreloadUIExtensionRecord(extensionRecordMapKey);
93     extensionRecordManager->RemovePreloadUIExtensionRecordById(extensionRecordMapKey, int32Param);
94     extensionRecordManager->GetOrCreateExtensionRecord(abilityRequest, strParam, abilityRecord, boolParam);
95     sptr<AAFwk::SessionInfo> sessionInfo;
96     extensionRecordManager->GetAbilityRecordBySessionInfo(sessionInfo);
97     auto token = GetFuzzAbilityToken();
98     extensionRecordManager->GetUIExtensionRootHostInfo(token);
99     UIExtensionSessionInfo uiExtensionSessionInfo;
100     extensionRecordManager->GetUIExtensionSessionInfo(token, uiExtensionSessionInfo);
101     extensionRecordManager->LoadTimeout(int32Param);
102     extensionRecordManager->ForegroundTimeout(int32Param);
103     extensionRecordManager->BackgroundTimeout(int32Param);
104     extensionRecordManager->TerminateTimeout(int32Param);
105     extensionRecordManager->GetHostBundleNameForExtensionId(int32Param, strParam);
106     extensionRecordManager->GetRootCallerTokenLocked(int32Param);
107     extensionRecordManager->GetOrCreateExtensionRecordInner(abilityRequest, strParam, extensionRecord, boolParam);
108     extensionRecordManager->IsHostSpecifiedProcessValid(abilityRequest, record, strParam);
109     std::list<sptr<IRemoteObject>> callerList;
110     mgr->GetCallerTokenList(int32Param, callerList);
111 
112     return true;
113 }
114 } // namespace OHOS
115 
116 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)117 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
118 {
119     /* Run your code on data */
120     if (data == nullptr) {
121         std::cout << "invalid data" << std::endl;
122         return 0;
123     }
124 
125     /* Validate the length of size */
126     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
127         return 0;
128     }
129 
130     char *ch = static_cast<char*>(malloc(size + 1));
131     if (ch == nullptr) {
132         std::cout << "malloc failed." << std::endl;
133         return 0;
134     }
135 
136     (void)memset_s(ch, size + 1, 0x00, size + 1);
137     if (memcpy_s(ch, size + 1, data, size) != EOK) {
138         std::cout << "copy failed." << std::endl;
139         free(ch);
140         ch = nullptr;
141         return 0;
142     }
143 
144     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
145     free(ch);
146     ch = nullptr;
147     return 0;
148 }