1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "abilitymanagerservicefirst_fuzzer.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include <cstddef> 19eace7efcSopenharmony_ci#include <cstdint> 20eace7efcSopenharmony_ci 21eace7efcSopenharmony_ci#define private public 22eace7efcSopenharmony_ci#define protected public 23eace7efcSopenharmony_ci#include "ability_manager_service.h" 24eace7efcSopenharmony_ci#undef protected 25eace7efcSopenharmony_ci#undef private 26eace7efcSopenharmony_ci 27eace7efcSopenharmony_ci#include "ability_connect_callback_interface.h" 28eace7efcSopenharmony_ci#include "ability_connect_callback_stub.h" 29eace7efcSopenharmony_ci#include "ability_record.h" 30eace7efcSopenharmony_ci 31eace7efcSopenharmony_ciusing namespace OHOS::AAFwk; 32eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk; 33eace7efcSopenharmony_ci 34eace7efcSopenharmony_cinamespace OHOS { 35eace7efcSopenharmony_cinamespace { 36eace7efcSopenharmony_ciconstexpr int INPUT_ZERO = 0; 37eace7efcSopenharmony_ciconstexpr int INPUT_ONE = 1; 38eace7efcSopenharmony_ciconstexpr int INPUT_THREE = 3; 39eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024; 40eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4; 41eace7efcSopenharmony_ciconstexpr uint8_t ENABLE = 2; 42eace7efcSopenharmony_ciconstexpr size_t OFFSET_ZERO = 24; 43eace7efcSopenharmony_ciconstexpr size_t OFFSET_ONE = 16; 44eace7efcSopenharmony_ciconstexpr size_t OFFSET_TWO = 8; 45eace7efcSopenharmony_ciclass MyAbilityConnection : public IAbilityConnection { 46eace7efcSopenharmony_cipublic: 47eace7efcSopenharmony_ci MyAbilityConnection() = default; 48eace7efcSopenharmony_ci virtual ~MyAbilityConnection() = default; 49eace7efcSopenharmony_ci void OnAbilityConnectDone( 50eace7efcSopenharmony_ci const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode) override 51eace7efcSopenharmony_ci {} 52eace7efcSopenharmony_ci void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode) override 53eace7efcSopenharmony_ci {} 54eace7efcSopenharmony_ci sptr<IRemoteObject> AsObject() override 55eace7efcSopenharmony_ci { 56eace7efcSopenharmony_ci return {}; 57eace7efcSopenharmony_ci } 58eace7efcSopenharmony_ci}; 59eace7efcSopenharmony_ci} 60eace7efcSopenharmony_ci 61eace7efcSopenharmony_ciuint32_t GetU32Data(const char* ptr) 62eace7efcSopenharmony_ci{ 63eace7efcSopenharmony_ci // convert fuzz input data to an integer 64eace7efcSopenharmony_ci return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) | 65eace7efcSopenharmony_ci ptr[INPUT_THREE]; 66eace7efcSopenharmony_ci} 67eace7efcSopenharmony_ci 68eace7efcSopenharmony_cisptr<Token> GetFuzzAbilityToken() 69eace7efcSopenharmony_ci{ 70eace7efcSopenharmony_ci sptr<Token> token = nullptr; 71eace7efcSopenharmony_ci AbilityRequest abilityRequest; 72eace7efcSopenharmony_ci abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; 73eace7efcSopenharmony_ci abilityRequest.abilityInfo.name = "MainAbility"; 74eace7efcSopenharmony_ci abilityRequest.abilityInfo.type = AbilityType::DATA; 75eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); 76eace7efcSopenharmony_ci if (abilityRecord) { 77eace7efcSopenharmony_ci token = abilityRecord->GetToken(); 78eace7efcSopenharmony_ci } 79eace7efcSopenharmony_ci return token; 80eace7efcSopenharmony_ci} 81eace7efcSopenharmony_ci 82eace7efcSopenharmony_civoid DoSomethingInterestingWithMyAPI1(AbilityManagerService &abilityms, Want& want, 83eace7efcSopenharmony_ci sptr<IRemoteObject> token, const char* data, size_t size) 84eace7efcSopenharmony_ci{ 85eace7efcSopenharmony_ci bool boolParam = *data % ENABLE; 86eace7efcSopenharmony_ci int intParam = static_cast<int>(GetU32Data(data)); 87eace7efcSopenharmony_ci int32_t int32Param = static_cast<int32_t>(GetU32Data(data)); 88eace7efcSopenharmony_ci int64_t int64Param = static_cast<int64_t>(GetU32Data(data)); 89eace7efcSopenharmony_ci uint32_t uint32Param = GetU32Data(data); 90eace7efcSopenharmony_ci std::string stringParam(data, size); 91eace7efcSopenharmony_ci sptr<IAbilityConnection> connect(new MyAbilityConnection()); 92eace7efcSopenharmony_ci abilityms.InitStartupFlag(); 93eace7efcSopenharmony_ci abilityms.QueryServiceState(); 94eace7efcSopenharmony_ci AbilityRequest abilityRequest; 95eace7efcSopenharmony_ci AppExecFwk::ExtensionAbilityType extensionType = ExtensionAbilityType::SERVICE; 96eace7efcSopenharmony_ci abilityms.CheckOptExtensionAbility(want, abilityRequest, int32Param, extensionType); 97eace7efcSopenharmony_ci AppExecFwk::AbilityInfo abilityInfo; 98eace7efcSopenharmony_ci abilityms.ReportAbilityStartInfoToRSS(abilityInfo); 99eace7efcSopenharmony_ci abilityms.ReportEventToRSS(abilityInfo, token); 100eace7efcSopenharmony_ci abilityms.StartExtensionAbility(want, token, int32Param, extensionType); 101eace7efcSopenharmony_ci abilityms.StopExtensionAbility(want, token, int32Param, extensionType); 102eace7efcSopenharmony_ci abilityms.TerminateAbility(token, intParam, &want); 103eace7efcSopenharmony_ci abilityms.CloseAbility(token, intParam, &want); 104eace7efcSopenharmony_ci abilityms.TerminateAbilityWithFlag(token, intParam, &want, boolParam); 105eace7efcSopenharmony_ci abilityms.SendResultToAbility(intParam, intParam, want); 106eace7efcSopenharmony_ci abilityms.StartRemoteAbility(want, intParam, int32Param, token); 107eace7efcSopenharmony_ci abilityms.CheckIfOperateRemote(want); 108eace7efcSopenharmony_ci abilityms.AnonymizeDeviceId(stringParam); 109eace7efcSopenharmony_ci abilityms.MinimizeAbility(token, boolParam); 110eace7efcSopenharmony_ci abilityms.ConnectAbility(want, connect, token, int32Param); 111eace7efcSopenharmony_ci abilityms.ConnectAbilityCommon(want, connect, token, extensionType, int32Param); 112eace7efcSopenharmony_ci abilityms.BuildEventInfo(want, int32Param); 113eace7efcSopenharmony_ci abilityms.DisconnectAbility(connect); 114eace7efcSopenharmony_ci abilityms.ConnectLocalAbility(want, int32Param, connect, token, extensionType); 115eace7efcSopenharmony_ci abilityms.ConnectRemoteAbility(want, token, token); 116eace7efcSopenharmony_ci abilityms.DisconnectLocalAbility(connect); 117eace7efcSopenharmony_ci abilityms.DisconnectRemoteAbility(token); 118eace7efcSopenharmony_ci AAFwk::WantParams wantParams; 119eace7efcSopenharmony_ci abilityms.ContinueMission(stringParam, stringParam, int32Param, token, wantParams); 120eace7efcSopenharmony_ci abilityms.ContinueAbility(stringParam, int32Param, uint32Param); 121eace7efcSopenharmony_ci abilityms.StartContinuation(want, token, int32Param); 122eace7efcSopenharmony_ci abilityms.NotifyCompleteContinuation(stringParam, int32Param, boolParam); 123eace7efcSopenharmony_ci abilityms.NotifyContinuationResult(int32Param, int32Param); 124eace7efcSopenharmony_ci abilityms.StartSyncRemoteMissions(stringParam, boolParam, int64Param); 125eace7efcSopenharmony_ci abilityms.StopSyncRemoteMissions(stringParam); 126eace7efcSopenharmony_ci} 127eace7efcSopenharmony_ci 128eace7efcSopenharmony_civoid DoSomethingInterestingWithMyAPI2(AbilityManagerService &abilityms, Want& want, 129eace7efcSopenharmony_ci sptr<IRemoteObject> token, const char* data, size_t size) 130eace7efcSopenharmony_ci{ 131eace7efcSopenharmony_ci int32_t int32Param = static_cast<int32_t>(GetU32Data(data)); 132eace7efcSopenharmony_ci int64_t int64Param = static_cast<int64_t>(GetU32Data(data)); 133eace7efcSopenharmony_ci std::string stringParam(data, size); 134eace7efcSopenharmony_ci sptr<AbilityRuntime::IConnectionObserver> observer; 135eace7efcSopenharmony_ci abilityms.RegisterObserver(observer); 136eace7efcSopenharmony_ci abilityms.UnregisterObserver(observer); 137eace7efcSopenharmony_ci#ifdef WITH_DLP 138eace7efcSopenharmony_ci std::vector<AbilityRuntime::DlpConnectionInfo> infos; 139eace7efcSopenharmony_ci abilityms.GetDlpConnectionInfos(infos); 140eace7efcSopenharmony_ci#endif // WITH_DLP 141eace7efcSopenharmony_ci std::vector<AbilityRuntime::ConnectionData> connectionData; 142eace7efcSopenharmony_ci abilityms.GetConnectionData(connectionData); 143eace7efcSopenharmony_ci 144eace7efcSopenharmony_ci sptr<SessionInfo> extensionSessionInfo; 145eace7efcSopenharmony_ci abilityms.StartUIExtensionAbility(extensionSessionInfo, int32Param); 146eace7efcSopenharmony_ci extensionSessionInfo = sptr<SessionInfo>(new (std::nothrow) SessionInfo()); 147eace7efcSopenharmony_ci extensionSessionInfo->sessionToken = token; 148eace7efcSopenharmony_ci extensionSessionInfo->callerToken = token; 149eace7efcSopenharmony_ci extensionSessionInfo->persistentId = int32Param; 150eace7efcSopenharmony_ci extensionSessionInfo->resultCode = int32Param; 151eace7efcSopenharmony_ci extensionSessionInfo->requestCode = int32Param; 152eace7efcSopenharmony_ci extensionSessionInfo->errorReason = stringParam; 153eace7efcSopenharmony_ci extensionSessionInfo->errorCode = int32Param; 154eace7efcSopenharmony_ci extensionSessionInfo->uiAbilityId = int64Param; 155eace7efcSopenharmony_ci extensionSessionInfo->userId = int32Param; 156eace7efcSopenharmony_ci extensionSessionInfo->state = CallToState::FOREGROUND; 157eace7efcSopenharmony_ci extensionSessionInfo->want = want; 158eace7efcSopenharmony_ci abilityms.StartUIExtensionAbility(extensionSessionInfo, int32Param); 159eace7efcSopenharmony_ci} 160eace7efcSopenharmony_ci 161eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 162eace7efcSopenharmony_ci{ 163eace7efcSopenharmony_ci Parcel wantParcel; 164eace7efcSopenharmony_ci Want* want = nullptr; 165eace7efcSopenharmony_ci if (wantParcel.WriteBuffer(data, size)) { 166eace7efcSopenharmony_ci want = Want::Unmarshalling(wantParcel); 167eace7efcSopenharmony_ci if (!want) { 168eace7efcSopenharmony_ci return false; 169eace7efcSopenharmony_ci } 170eace7efcSopenharmony_ci } 171eace7efcSopenharmony_ci sptr<IRemoteObject> token = GetFuzzAbilityToken(); 172eace7efcSopenharmony_ci 173eace7efcSopenharmony_ci // fuzz for AbilityManagerService 174eace7efcSopenharmony_ci auto abilityms = std::make_shared<AbilityManagerService>(); 175eace7efcSopenharmony_ci DoSomethingInterestingWithMyAPI1(*abilityms, *want, token, data, size); 176eace7efcSopenharmony_ci DoSomethingInterestingWithMyAPI2(*abilityms, *want, token, data, size); 177eace7efcSopenharmony_ci if (!want) { 178eace7efcSopenharmony_ci delete want; 179eace7efcSopenharmony_ci want = nullptr; 180eace7efcSopenharmony_ci } 181eace7efcSopenharmony_ci 182eace7efcSopenharmony_ci return true; 183eace7efcSopenharmony_ci} 184eace7efcSopenharmony_ci} 185eace7efcSopenharmony_ci 186eace7efcSopenharmony_ci/* Fuzzer entry point */ 187eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 188eace7efcSopenharmony_ci{ 189eace7efcSopenharmony_ci /* Run your code on data */ 190eace7efcSopenharmony_ci if (data == nullptr) { 191eace7efcSopenharmony_ci return 0; 192eace7efcSopenharmony_ci } 193eace7efcSopenharmony_ci 194eace7efcSopenharmony_ci /* Validate the length of size */ 195eace7efcSopenharmony_ci if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { 196eace7efcSopenharmony_ci return 0; 197eace7efcSopenharmony_ci } 198eace7efcSopenharmony_ci 199eace7efcSopenharmony_ci char* ch = (char*)malloc(size + 1); 200eace7efcSopenharmony_ci if (ch == nullptr) { 201eace7efcSopenharmony_ci std::cout << "malloc failed." << std::endl; 202eace7efcSopenharmony_ci return 0; 203eace7efcSopenharmony_ci } 204eace7efcSopenharmony_ci 205eace7efcSopenharmony_ci (void)memset_s(ch, size + 1, 0x00, size + 1); 206eace7efcSopenharmony_ci if (memcpy_s(ch, size, data, size) != EOK) { 207eace7efcSopenharmony_ci std::cout << "copy failed." << std::endl; 208eace7efcSopenharmony_ci free(ch); 209eace7efcSopenharmony_ci ch = nullptr; 210eace7efcSopenharmony_ci return 0; 211eace7efcSopenharmony_ci } 212eace7efcSopenharmony_ci 213eace7efcSopenharmony_ci OHOS::DoSomethingInterestingWithMyAPI(ch, size); 214eace7efcSopenharmony_ci free(ch); 215eace7efcSopenharmony_ci ch = nullptr; 216eace7efcSopenharmony_ci return 0; 217eace7efcSopenharmony_ci} 218eace7efcSopenharmony_ci 219