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