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 "dialogsessionmanager_fuzzer.h"
17 #include <cstddef>
18 #include <cstdint>
19 #define private public
20 #include "dialog_session_manager.h"
21 #undef private
22 #include "ability_record.h"
23
24 using namespace OHOS::AAFwk;
25 using namespace OHOS::AppExecFwk;
26
27 namespace OHOS {
28 namespace {
29 constexpr int INPUT_ZERO = 0;
30 constexpr int INPUT_ONE = 1;
31 constexpr int INPUT_TWO = 2;
32 constexpr int INPUT_THREE = 3;
33 constexpr size_t FOO_MAX_LEN = 1024;
34 constexpr size_t U32_AT_SIZE = 4;
35 constexpr size_t OFFSET_ZERO = 24;
36 constexpr size_t OFFSET_ONE = 16;
37 constexpr size_t OFFSET_TWO = 8;
38 constexpr uint8_t ENABLE = 2;
39 } // namespace
40
GetU32Data(const char* ptr)41 uint32_t GetU32Data(const char* ptr)
42 {
43 // convert fuzz input data to an integer
44 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) |
45 ptr[INPUT_THREE];
46 }
47
GetFuzzAbilityToken()48 sptr<Token> GetFuzzAbilityToken()
49 {
50 sptr<Token> token = nullptr;
51 AbilityRequest abilityRequest;
52 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
53 abilityRequest.abilityInfo.name = "MainAbility";
54 abilityRequest.abilityInfo.type = AbilityType::DATA;
55 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
56 if (abilityRecord) {
57 token = abilityRecord->GetToken();
58 }
59 return token;
60 }
61
DoSomethingInterestingWithMyAPI(const char *data, size_t size)62 bool DoSomethingInterestingWithMyAPI(const char *data, size_t size)
63 {
64 std::string bundleName(data, size);
65 std::string dialogSessionId(data, size);
66 std::string replaceWant(data, size);
67 bool isSelector = *data % ENABLE;
68 AAFwk::WantParams wantParams;
69 int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
70 AbilityRequest abilityRequest;
71 std::vector<DialogAppInfo> dialogAppInfos;
72 std::vector<DialogAbilityInfo> targetAbilityInfos;
73 sptr<IRemoteObject> callerToken = GetFuzzAbilityToken();
74 Parcel wantParcel;
75 Want want;
76 auto dialogSessionInfo = std::make_shared<DialogSessionInfo>();
77 if (dialogSessionInfo == nullptr) {
78 return false;
79 }
80 std::shared_ptr<DialogCallerInfo> dialogCallerInfo = std::make_shared<DialogCallerInfo>();
81 if (dialogCallerInfo == nullptr) {
82 return false;
83 }
84 std::shared_ptr<DialogSessionManager> dialogSessionManager = std::make_shared<DialogSessionManager>();
85 if (dialogSessionManager == nullptr) {
86 return false;
87 }
88 dialogSessionManager->GenerateDialogSessionId();
89 dialogSessionManager->SetStartupSessionInfo(dialogSessionId, abilityRequest);
90 DialogAbilityInfo callerAbilityInfo;
91 dialogSessionManager->GenerateCallerAbilityInfo(abilityRequest, callerAbilityInfo);
92 dialogSessionManager->GenerateSelectorTargetAbilityInfos(dialogAppInfos, targetAbilityInfos);
93 dialogSessionManager->GenerateDialogCallerInfo(abilityRequest, int32Param, dialogCallerInfo, isSelector);
94 dialogSessionManager->NotifySCBToRecoveryAfterInterception(dialogSessionId, abilityRequest);
95 dialogSessionManager->GenerateDialogSessionRecordCommon(abilityRequest, int32Param, wantParams,
96 dialogAppInfos, isSelector);
97 dialogSessionManager->CreateJumpModalDialog(abilityRequest, int32Param, want);
98 dialogSessionManager->CreateImplicitSelectorModalDialog(abilityRequest, want, int32Param, dialogAppInfos);
99 dialogSessionManager->CreateCloneSelectorModalDialog(abilityRequest, want, int32Param, dialogAppInfos,
100 replaceWant);
101 dialogSessionManager->CreateModalDialogCommon(want, callerToken, dialogSessionId);
102 dialogSessionManager->HandleErmsResult(abilityRequest, int32Param, want);
103 dialogSessionManager->HandleErmsResultBySCB(abilityRequest, want);
104 dialogSessionManager->IsCreateCloneSelectorDialog(bundleName, int32Param);
105 return true;
106 }
107 } // namespace OHOS
108
109 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)110 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
111 {
112 /* Run your code on data */
113 if (data == nullptr) {
114 std::cout << "invalid data" << std::endl;
115 return 0;
116 }
117
118 /* Validate the length of size */
119 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
120 return 0;
121 }
122
123 char *ch = (char *)malloc(size + 1);
124 if (ch == nullptr) {
125 std::cout << "malloc failed." << std::endl;
126 return 0;
127 }
128
129 (void)memset_s(ch, size + 1, 0x00, size + 1);
130 if (memcpy_s(ch, size + 1, data, size) != EOK) {
131 std::cout << "copy failed." << std::endl;
132 free(ch);
133 ch = nullptr;
134 return 0;
135 }
136
137 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
138 free(ch);
139 ch = nullptr;
140 return 0;
141 }