• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     SelectorType type = SelectorType::WITHOUT_SELECTOR;
93     dialogSessionManager->GenerateDialogSessionRecordCommon(abilityRequest, int32Param, wantParams,
94         dialogAppInfos, type, isSelector);
95     type = SelectorType::IMPLICIT_START_SELECTOR;
96     dialogSessionManager->GenerateDialogSessionRecordCommon(abilityRequest, int32Param, wantParams,
97         dialogAppInfos, type, isSelector);
98     type = SelectorType::APP_CLONE_SELECTOR;
99     dialogSessionManager->GenerateDialogSessionRecordCommon(abilityRequest, int32Param, wantParams,
100         dialogAppInfos, type, isSelector);
101     type = SelectorType::INTERCEPTOR_SELECTOR;
102     dialogSessionManager->GenerateDialogSessionRecordCommon(abilityRequest, int32Param, wantParams,
103         dialogAppInfos, type, isSelector);
104     dialogSessionManager->GenerateDialogCallerInfo(abilityRequest, int32Param, dialogCallerInfo, type, isSelector);
105     dialogSessionManager->NotifySCBToRecoveryAfterInterception(dialogSessionId, abilityRequest);
106     dialogSessionManager->CreateJumpModalDialog(abilityRequest, int32Param, want);
107     dialogSessionManager->CreateImplicitSelectorModalDialog(abilityRequest, want, int32Param, dialogAppInfos);
108     dialogSessionManager->CreateCloneSelectorModalDialog(abilityRequest, want, int32Param, dialogAppInfos,
109         replaceWant);
110     dialogSessionManager->CreateModalDialogCommon(want, callerToken, dialogSessionId);
111     dialogSessionManager->HandleErmsResult(abilityRequest, int32Param, want);
112     dialogSessionManager->HandleErmsResultBySCB(abilityRequest, want);
113     dialogSessionManager->IsCreateCloneSelectorDialog(bundleName, int32Param);
114     return true;
115 }
116 } // namespace OHOS
117 
118 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)119 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
120 {
121     /* Run your code on data */
122     if (data == nullptr) {
123         std::cout << "invalid data" << std::endl;
124         return 0;
125     }
126 
127     /* Validate the length of size */
128     if (size < OHOS::U32_AT_SIZE) {
129         return 0;
130     }
131 
132     char *ch = static_cast<char*>(malloc(size + 1));
133     if (ch == nullptr) {
134         std::cout << "malloc failed." << std::endl;
135         return 0;
136     }
137 
138     (void)memset_s(ch, size + 1, 0x00, size + 1);
139     if (memcpy_s(ch, size + 1, data, size) != EOK) {
140         std::cout << "copy failed." << std::endl;
141         free(ch);
142         ch = nullptr;
143         return 0;
144     }
145 
146     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
147     free(ch);
148     ch = nullptr;
149     return 0;
150 }