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