1 /*
2 * Copyright (c) 2022 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 "abilitymanagerserviceseventh_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #define protected public
23 #include "ability_manager_service.h"
24 #undef protected
25 #undef private
26
27 #include "ability_record.h"
28
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31
32 namespace OHOS {
33 namespace {
34 constexpr size_t FOO_MAX_LEN = 1024;
35 constexpr size_t U32_AT_SIZE = 4;
36 class MyAbilityConnection : public IAbilityConnection {
37 public:
38 MyAbilityConnection() = default;
39 virtual ~MyAbilityConnection() = default;
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)40 void OnAbilityConnectDone(
41 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override
42 {}
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)43 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override
44 {}
AsObject()45 sptr<IRemoteObject> AsObject() override
46 {
47 return {};
48 }
49 };
50 }
51
GetU32Data(const char * ptr)52 uint32_t GetU32Data(const char* ptr)
53 {
54 // convert fuzz input data to an integer
55 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
56 }
57
GetFuzzAbilityToken()58 sptr<Token> GetFuzzAbilityToken()
59 {
60 sptr<Token> token = nullptr;
61 AbilityRequest abilityRequest;
62 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
63 abilityRequest.abilityInfo.name = "MainAbility";
64 abilityRequest.abilityInfo.type = AbilityType::DATA;
65 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
66 if (abilityRecord) {
67 token = abilityRecord->GetToken();
68 }
69 return token;
70 }
71
DoSomethingInterestingWithMyAPI(const char * data,size_t size)72 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
73 {
74 int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
75 std::string stringParam(data, size);
76 Parcel wantParcel;
77 Want *want = nullptr;
78 if (wantParcel.WriteBuffer(data, size)) {
79 want = Want::Unmarshalling(wantParcel);
80 if (!want) {
81 return false;
82 }
83 }
84 sptr<IRemoteObject> token = GetFuzzAbilityToken();
85 sptr<IAbilityConnection> connect = new MyAbilityConnection();
86
87 // fuzz for AbilityManagerService
88 auto abilityms = std::make_shared<AbilityManagerService>();
89 MissionSnapshot missionSnapshot;
90 abilityms->GetRemoteMissionSnapshotInfo(stringParam, int32Param, missionSnapshot);
91 abilityms->GetDataAbilityManagerByToken(token);
92 abilityms->ConnectBmsService();
93 sptr<IWantSender> target;
94 std::shared_ptr<WantSenderInfo> wantSenderInfo;
95 abilityms->GetWantSenderInfo(target, wantSenderInfo);
96 abilityms->GetAppMemorySize();
97 abilityms->IsRamConstrainedDevice();
98 abilityms->GetMissionSaveTime();
99 abilityms->GetMissionIdByAbilityToken(token);
100 abilityms->GetAbilityTokenByMissionId(int32Param);
101 abilityms->StartRemoteAbilityByCall(*want, token, token);
102 AppExecFwk::ElementName element;
103 abilityms->ReleaseRemoteAbility(token, element);
104 abilityms->StartAbilityByCall(*want, connect, token);
105 abilityms->ReleaseCall(connect, element);
106
107 return true;
108 }
109 }
110
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114 /* Run your code on data */
115 if (data == nullptr) {
116 return 0;
117 }
118
119 /* Validate the length of size */
120 if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
121 return 0;
122 }
123
124 char* ch = (char *)malloc(size + 1);
125 if (ch == nullptr) {
126 std::cout << "malloc failed." << std::endl;
127 return 0;
128 }
129
130 (void)memset_s(ch, size + 1, 0x00, size + 1);
131 if (memcpy_s(ch, size, data, size) != EOK) {
132 std::cout << "copy failed." << std::endl;
133 free(ch);
134 ch = nullptr;
135 return 0;
136 }
137
138 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
139 free(ch);
140 ch = nullptr;
141 return 0;
142 }
143
144