1 /*
2  * Copyright (c) 2024 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 "distributedwant_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21 
22 #include "bool_wrapper.h"
23 #include "distributed_want.h"
24 #include "securec.h"
25 
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::DistributedSchedule;
28 
29 namespace OHOS {
30 namespace {
31 constexpr size_t FOO_MAX_LEN = 1024;
32 constexpr size_t U32_AT_SIZE = 4;
33 constexpr int32_t POS_0 = 0;
34 constexpr int32_t POS_1 = 1;
35 constexpr int32_t POS_2 = 2;
36 constexpr int32_t POS_3 = 3;
37 constexpr int32_t OFFSET_24 = 24;
38 constexpr int32_t OFFSET_16 = 16;
39 constexpr int32_t OFFSET_8 = 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[POS_0] << OFFSET_24) | (ptr[POS_1] << OFFSET_16) | (ptr[POS_2] << OFFSET_8) | ptr[POS_3];
45 }
46 
DoSomethingInterestingWithMyApiDistributedWant001(const char * data,size_t size)47 bool DoSomethingInterestingWithMyApiDistributedWant001(const char* data, size_t size)
48 {
49     std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
50     unsigned int flags = static_cast<unsigned int>(GetU32Data(data));
51     want->SetFlags(flags);
52     want->RemoveFlags(flags);
53     want->AddFlags(flags);
54     std::string entity(data, size);
55     want->AddEntity(entity);
56     want->HasEntity(entity);
57     want->RemoveEntity(entity);
58     std::string bundleName(data, size);
59     want->SetBundle(bundleName);
60     std::string deviceId(data, size);
61     want->SetDeviceId(deviceId);
62     want->SetElementName(bundleName, entity);
63     want->SetElementName(deviceId, bundleName, entity);
64     return true;
65 }
66 
DoSomethingInterestingWithMyApiDistributedWant002(const char * data,size_t size)67 bool DoSomethingInterestingWithMyApiDistributedWant002(const char* data, size_t size)
68 {
69     std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
70     std::string type(data, size);
71     want->SetType(type);
72     Uri uri(type);
73     want->SetUri(uri);
74     want->SetUriAndType(uri, type);
75     want->FormatUri(uri);
76     want->FormatUri(type);
77     want->GetLowerCaseScheme(uri);
78     return true;
79 }
80 
DoSomethingInterestingWithMyApiDistributedWant003(const char * data,size_t size)81 bool DoSomethingInterestingWithMyApiDistributedWant003(const char* data, size_t size)
82 {
83     std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
84     want->CountEntities();
85     want->GetScheme();
86     DistributedOperation operation;
87     want->SetOperation(operation);
88     std::string key(data, size);
89     want->HasParameter(key);
90     std::string content(data, size);
91     std::string prop(data, size);
92     std::string value(data, size);
93     std::string str(data, size);
94     nlohmann::json wantJson;
95     want->ReadFromJson(wantJson);
96     return true;
97 }
98 
DoSomethingInterestingWithMyApiDistributedWant004(const char * data,size_t size)99 bool DoSomethingInterestingWithMyApiDistributedWant004(const char* data, size_t size)
100 {
101     std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
102     std::string key(data, size);
103     sptr<IRemoteObject> remoteObject;
104     want->SetParam(key, remoteObject);
105     std::vector<bool> boolValue;
106     want->SetParam(key, boolValue);
107     want->GetBoolArrayParam(key);
108     byte byteValue = '\0';
109     want->SetParam(key, byteValue);
110     want->GetByteParam(key, byteValue);
111     std::vector<byte> byteVector;
112     want->SetParam(key, byteVector);
113     want->GetByteArrayParam(key);
114     zchar charValue = U'\0';
115     want->SetParam(key, charValue);
116     want->GetCharParam(key, charValue);
117     want->GetParams();
118     return true;
119 }
120 
DoSomethingInterestingWithMyApiDistributedWant005(const char * data,size_t size)121 bool DoSomethingInterestingWithMyApiDistributedWant005(const char* data, size_t size)
122 {
123     std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
124     std::string key(data, size);
125     std::vector<zchar> charVector;
126     want->SetParam(key, charVector);
127     want->GetCharArrayParam(key);
128     std::vector<int> intVector;
129     want->SetParam(key, intVector);
130     want->GetIntArrayParam(key);
131     double doubleValue = 0.0;
132     want->SetParam(key, doubleValue);
133     want->GetDoubleParam(key, doubleValue);
134     std::vector<double> doubleVector;
135     want->SetParam(key, doubleVector);
136     want->GetDoubleArrayParam(key);
137     float floatValue = 0.0;
138     want->SetParam(key, floatValue);
139     want->GetFloatParam(key, floatValue);
140     bool boolValue = true;
141     want->SetParam(key, boolValue);
142     want->GetBoolParam(key, boolValue);
143     return true;
144 }
145 
DoSomethingInterestingWithMyApiDistributedWant006(const char * data,size_t size)146 bool DoSomethingInterestingWithMyApiDistributedWant006(const char* data, size_t size)
147 {
148     std::shared_ptr<DistributedWant> want = std::make_shared<DistributedWant>();
149     std::string key(data, size);
150     std::vector<float> floatVector;
151     want->SetParam(key, floatVector);
152     want->GetFloatArrayParam(key);
153     long longValue = 0;
154     want->SetParam(key, longValue);
155     want->GetShortParam(key, longValue);
156     std::vector<long> longVector;
157     want->SetParam(key, longVector);
158     want->GetLongArrayParam(key);
159     short shortValue = 0;
160     want->SetParam(key, shortValue);
161     want->GetShortParam(key, shortValue);
162     std::vector<short> shortVector;
163     want->SetParam(key, shortVector);
164     want->GetShortArrayParam(key);
165     std::string stringValue(data, size);
166     want->SetParam(key, stringValue);
167     want->GetStringParam(key);
168     std::vector<std::string> stringVector;
169     want->SetParam(key, stringVector);
170     want->GetStringArrayParam(key);
171     want->RemoveParam(key);
172 
173     bool boolValue = true;
174     DistributedWantParams dWantParams;
175     dWantParams.SetParam(key, Boolean::Box(boolValue));
176     want->SetParams(dWantParams);
177     want->ReplaceParams(dWantParams);
178     DistributedWant dWant;
179     want->ReplaceParams(dWant);
180     want->ClearWant(&dWant);
181     return true;
182 }
183 }
184 
185 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)186 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
187 {
188     /* Run your code on data */
189     if (data == nullptr) {
190         std::cout << "invalid data" << std::endl;
191         return 0;
192     }
193 
194     /* Validate the length of size */
195     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
196         return 0;
197     }
198 
199     char* ch = reinterpret_cast<char *>(malloc(size + 1));
200     if (ch == nullptr) {
201         std::cout << "malloc failed." << std::endl;
202         return 0;
203     }
204 
205     (void)memset_s(ch, size + 1, 0x00, size + 1);
206     if (memcpy_s(ch, size + 1, data, size) != EOK) {
207         std::cout << "copy failed." << std::endl;
208         free(ch);
209         ch = nullptr;
210         return 0;
211     }
212 
213     OHOS::DoSomethingInterestingWithMyApiDistributedWant001(ch, size);
214     OHOS::DoSomethingInterestingWithMyApiDistributedWant002(ch, size);
215     OHOS::DoSomethingInterestingWithMyApiDistributedWant003(ch, size);
216     OHOS::DoSomethingInterestingWithMyApiDistributedWant004(ch, size);
217     OHOS::DoSomethingInterestingWithMyApiDistributedWant005(ch, size);
218     OHOS::DoSomethingInterestingWithMyApiDistributedWant006(ch, size);
219     free(ch);
220     ch = nullptr;
221     return 0;
222 }
223