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 "abilityrunningrecord_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "ability_running_record.h"
22 #include "ability_record.h"
23 #include "parcel.h"
24 #include "securec.h"
25 
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AppExecFwk;
28 
29 namespace OHOS {
30 namespace {
31 constexpr int INPUT_ZERO = 0;
32 constexpr int INPUT_ONE = 1;
33 constexpr int INPUT_THREE = 3;
34 constexpr size_t FOO_MAX_LEN = 1024;
35 constexpr size_t U32_AT_SIZE = 4;
36 constexpr uint8_t ENABLE = 2;
37 constexpr size_t OFFSET_ZERO = 24;
38 constexpr size_t OFFSET_ONE = 16;
39 constexpr size_t OFFSET_TWO = 8;
40 }
GetU32Data(const char * ptr)41 uint32_t GetU32Data(const char* ptr)
42 {
43     // convert fuzz input data to an integer
44     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
45         ptr[INPUT_THREE];
46 }
GetFuzzAbilityToken()47 sptr<Token> GetFuzzAbilityToken()
48 {
49     sptr<Token> token = nullptr;
50 
51     AbilityRequest abilityRequest;
52     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
53     abilityRequest.abilityInfo.name = "MainAbility";
54     abilityRequest.abilityInfo.type = AbilityType::PAGE;
55     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
56     if (abilityRecord) {
57         token = abilityRecord->GetToken();
58     }
59 
60     return token;
61 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)62 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
63 {
64     std::shared_ptr<AbilityInfo> info;
65     sptr<IRemoteObject> token = GetFuzzAbilityToken();
66     int32_t abilityRecordId = static_cast<int32_t>(GetU32Data(data));
67     AbilityRunningRecord abilityRecord(info, token, abilityRecordId);
68     Parcel wantParcel;
69     std::shared_ptr<Want> want = nullptr;
70     abilityRecord.SetWant(want);
71     AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_READY;
72     abilityRecord.SetState(state);
73     sptr<IRemoteObject> pretoken = GetFuzzAbilityToken();
74     abilityRecord.SetPreToken(pretoken);
75     int32_t visibility = static_cast<int32_t>(GetU32Data(data));
76     abilityRecord.SetVisibility(visibility);
77     int32_t perceptibility = static_cast<int32_t>(GetU32Data(data));
78     abilityRecord.SetPerceptibility(perceptibility);
79     int32_t connectionState = static_cast<int32_t>(GetU32Data(data));
80     abilityRecord.SetConnectionState(connectionState);
81     int64_t eventId = static_cast<int64_t>(GetU32Data(data));
82     abilityRecord.SetEventId(eventId);
83     int32_t ownerUserId = static_cast<int64_t>(GetU32Data(data));
84     abilityRecord.SetOwnerUserId(ownerUserId);
85     bool flag = *data % ENABLE;
86     abilityRecord.SetIsSingleUser(flag);
87     bool isFocus = *data % ENABLE;
88     abilityRecord.UpdateFocusState(isFocus);
89     abilityRecord.GetName();
90     abilityRecord.GetAbilityInfo();
91     abilityRecord.GetWant();
92     abilityRecord.GetToken();
93     abilityRecord.GetState();
94     abilityRecord.GetLastLaunchTime();
95     abilityRecord.GetPreToken();
96     abilityRecord.GetVisibility();
97     abilityRecord.GetPerceptibility();
98     abilityRecord.GetConnectionState();
99     abilityRecord.GetEventId();
100     abilityRecord.SetTerminating();
101     abilityRecord.IsTerminating();
102     abilityRecord.GetOwnerUserId();
103     abilityRecord.IsSingleUser();
104     abilityRecord.GetFocusFlag();
105     return abilityRecord.IsSameState(state);
106 }
107 }
108 
109 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)110 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
111 {
112     /* Run your code on data */
113     if (data == nullptr) {
114         std::cout << "invalid data" << std::endl;
115         return 0;
116     }
117 
118     /* Validate the length of size */
119     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
120         return 0;
121     }
122 
123     char* ch = static_cast<char*>(malloc(size + 1));
124     if (ch == nullptr) {
125         std::cout << "malloc failed." << std::endl;
126         return 0;
127     }
128 
129     (void)memset_s(ch, size + 1, 0x00, size + 1);
130     if (memcpy_s(ch, size, data, size) != EOK) {
131         std::cout << "copy failed." << std::endl;
132         free(ch);
133         ch = nullptr;
134         return 0;
135     }
136 
137     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
138     free(ch);
139     ch = nullptr;
140     return 0;
141 }
142 
143