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