• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     Parcel wantParcel;
76     Want* want = nullptr;
77     if (wantParcel.WriteBuffer(data, size)) {
78         want = Want::Unmarshalling(wantParcel);
79         if (!want) {
80             return false;
81         }
82     }
83     sptr<IRemoteObject> token = GetFuzzAbilityToken();
84     sptr<IAbilityConnection> connect = new MyAbilityConnection();
85 
86     // fuzz for AbilityManagerService
87     auto abilityms = std::make_shared<AbilityManagerService>();
88     abilityms->GetDataAbilityManagerByToken(token);
89     abilityms->ConnectBmsService();
90     sptr<IWantSender> target;
91     std::shared_ptr<WantSenderInfo> wantSenderInfo;
92     abilityms->GetWantSenderInfo(target, wantSenderInfo);
93     abilityms->GetAppMemorySize();
94     abilityms->IsRamConstrainedDevice();
95     AmsConfigurationParameter::GetInstance().GetMissionSaveTime();
96     abilityms->GetMissionIdByAbilityToken(token);
97     abilityms->GetAbilityTokenByMissionId(int32Param);
98     abilityms->StartRemoteAbilityByCall(*want, token, token);
99     AppExecFwk::ElementName element;
100     abilityms->ReleaseRemoteAbility(token, element);
101     abilityms->StartAbilityByCall(*want, connect, token);
102     abilityms->ReleaseCall(connect, element);
103 
104     return true;
105 }
106 }
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         return 0;
114     }
115 
116     /* Validate the length of size */
117     if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
118         return 0;
119     }
120 
121     char* ch = (char*)malloc(size + 1);
122     if (ch == nullptr) {
123         std::cout << "malloc failed." << std::endl;
124         return 0;
125     }
126 
127     (void)memset_s(ch, size + 1, 0x00, size + 1);
128     if (memcpy_s(ch, size, data, size) != EOK) {
129         std::cout << "copy failed." << std::endl;
130         free(ch);
131         ch = nullptr;
132         return 0;
133     }
134 
135     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
136     free(ch);
137     ch = nullptr;
138     return 0;
139 }
140 
141