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 "distributedwantparams_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21 
22 #include "array_wrapper.h"
23 #include "bool_wrapper.h"
24 #include "byte_wrapper.h"
25 #include "distributed_want_params.h"
26 #include "distributed_want_params_wrapper.h"
27 #include "double_wrapper.h"
28 #include "float_wrapper.h"
29 #include "int_wrapper.h"
30 #include "long_wrapper.h"
31 #include "securec.h"
32 #include "short_wrapper.h"
33 #include "string_wrapper.h"
34 #include "zchar_wrapper.h"
35 
36 using namespace OHOS::AAFwk;
37 using namespace OHOS::DistributedSchedule;
38 
39 namespace OHOS {
40 namespace {
41 constexpr size_t FOO_MAX_LEN = 1024;
42 constexpr size_t U32_AT_SIZE = 4;
43 constexpr int32_t POS_0 = 0;
44 constexpr int32_t POS_1 = 1;
45 constexpr int32_t POS_2 = 2;
46 constexpr int32_t POS_3 = 3;
47 constexpr int32_t OFFSET_24 = 24;
48 constexpr int32_t OFFSET_16 = 16;
49 constexpr int32_t OFFSET_8 = 8;
50 }
GetU32Data(const char * ptr)51 uint32_t GetU32Data(const char* ptr)
52 {
53     // convert fuzz input data to an integer
54     return (ptr[POS_0] << OFFSET_24) | (ptr[POS_1] << OFFSET_16) | (ptr[POS_2] << OFFSET_8) | ptr[POS_3];
55 }
56 
DoSomethingInterestingWithMyApiDistributedWantParams001(const char * data,size_t size)57 bool DoSomethingInterestingWithMyApiDistributedWantParams001(const char* data, size_t size)
58 {
59     DistributedWantParams wantOther;
60     std::string key(data, size);
61     std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
62     sptr<AAFwk::IArray> array = new (std::nothrow) AAFwk::Array(0, DistributedSchedule::g_IID_IDistributedWantParams);
63     wantParams->SetParam(key, array);
64     IArray *iarray = IArray::Query(array);
65     sptr<IArray> destAO = nullptr;
66     wantParams->NewArrayData(iarray, destAO);
67     sptr<IInterface> stringIt = String::Box(key);
68     wantParams->Remove(key);
69     wantParams->SetParam(key, stringIt);
70     wantParams->KeySet();
71     wantParams->HasParam(key);
72     wantParams->IsEmpty();
73     wantParams->GetParams();
74 
75     Parcel parcel;
76 
77     return true;
78 }
79 
DoSomethingInterestingWithMyApiDistributedWantParams002(const char * data,size_t size)80 bool DoSomethingInterestingWithMyApiDistributedWantParams002(const char* data, size_t size)
81 {
82     DistributedWantParams wantOther;
83     std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
84     Parcel parcel;
85     IArray* ao = nullptr;
86     wantParams->WriteArrayToParcelChar(parcel, ao);
87     wantParams->WriteArrayToParcelShort(parcel, ao);
88     wantParams->WriteArrayToParcelString(parcel, ao);
89     wantParams->WriteArrayToParcelBool(parcel, ao);
90     wantParams->WriteArrayToParcelByte(parcel, ao);
91     wantParams->WriteArrayToParcelInt(parcel, ao);
92     wantParams->WriteArrayToParcelLong(parcel, ao);
93     wantParams->WriteArrayToParcelFloat(parcel, ao);
94     wantParams->WriteArrayToParcelDouble(parcel, ao);
95     long longValue = static_cast<long>(GetU32Data(data));
96     sptr<IInterface> longIt = Long::Box(longValue);
97     wantParams->WriteToParcelLong(parcel, longIt);
98     float floatValue = static_cast<float>(GetU32Data(data));
99     sptr<IInterface> floatIt = Float::Box(floatValue);
100     wantParams->WriteToParcelFloat(parcel, floatIt);
101 
102     sptr<IArray> array;
103     wantParams->ReadFromParcelArrayChar(parcel, array);
104     wantParams->ReadFromParcelArrayShort(parcel, array);
105     wantParams->ReadFromParcelArrayString(parcel, array);
106     wantParams->ReadFromParcelArrayBool(parcel, array);
107     wantParams->ReadFromParcelArrayByte(parcel, array);
108     wantParams->ReadFromParcelArrayInt(parcel, array);
109     wantParams->ReadFromParcelArrayLong(parcel, array);
110     wantParams->ReadFromParcelArrayFloat(parcel, array);
111     wantParams->ReadFromParcelArrayDouble(parcel, array);
112     std::string key(data, size);
113     wantParams->ReadFromParcelLong(parcel, key);
114     wantParams->ReadFromParcelFloat(parcel, key);
115 
116     wantParams->ReadArrayToParcel(parcel, GetU32Data(data) % OFFSET_16, array);
117     return true;
118 }
119 
DoSomethingInterestingWithMyApiDistributedWantParams003(const char * data,size_t size)120 bool DoSomethingInterestingWithMyApiDistributedWantParams003(const char* data, size_t size)
121 {
122     DistributedWantParams wantOther;
123     std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
124     Parcel parcel;
125     std::string key(data, size);
126     int8_t byteValue = static_cast<int8_t>(GetU32Data(data));
127     sptr<IInterface> byteIt = Byte::Box(byteValue);
128     wantParams->WriteToParcelByte(parcel, byteIt);
129     sptr<IInterface> stringIt = String::Box(key);
130     wantParams->WriteToParcelString(parcel, stringIt);
131     bool boolValue = static_cast<bool>(GetU32Data(data) > FOO_MAX_LEN);
132     sptr<IInterface> boolIt = Boolean::Box(boolValue);
133     wantParams->WriteToParcelBool(parcel, boolIt);
134     char charValue = *data;
135     sptr<IInterface> charIt = Char::Box(charValue);
136     wantParams->WriteToParcelChar(parcel, charIt);
137     short shortValue = static_cast<short>(GetU32Data(data));
138     sptr<IInterface> shortIt = Short::Box(shortValue);
139     wantParams->WriteToParcelShort(parcel, shortIt);
140     double doubleValue = static_cast<double>(GetU32Data(data));
141     sptr<IInterface> doubleIt = Double::Box(doubleValue);
142     wantParams->WriteToParcelDouble(parcel, doubleIt);
143     int32_t intValue = static_cast<int32_t>(GetU32Data(data));
144     sptr<IInterface> intIt = Integer::Box(intValue);
145     wantParams->WriteToParcelInt(parcel, intIt);
146     wantParams->WriteToParcelFD(parcel, wantOther);
147     wantParams->WriteToParcelRemoteObject(parcel, wantOther);
148 
149     int type = static_cast<int>(GetU32Data(data));
150     wantParams->ReadFromParcelInt8(parcel, key);
151     wantParams->ReadFromParcelString(parcel, key);
152     wantParams->ReadFromParcelBool(parcel, key);
153     wantParams->ReadFromParcelChar(parcel, key);
154     wantParams->ReadFromParcelShort(parcel, key);
155     wantParams->ReadFromParcelDouble(parcel, key);
156     wantParams->ReadFromParcelInt(parcel, key);
157     wantParams->ReadFromParcelFD(parcel, key);
158     wantParams->ReadFromParcelRemoteObject(parcel, key);
159     wantParams->ReadFromParcelWantParamWrapper(parcel, key, type);
160 
161     wantParams->WriteToParcelByte(parcel, byteIt);
162     wantParams->ReadFromParcel(parcel);
163 
164     DistributedUnsupportedData uData;
165     std::shared_ptr<DistributedUnsupportedData> unsupportedData = std::make_shared<DistributedUnsupportedData>(uData);
166     wantParams->cachedUnsupportedData_.emplace_back(std::move(uData));
167     wantParams->DoMarshalling(parcel);
168     wantParams->cachedUnsupportedData_.clear();
169     return true;
170 }
171 
DoSomethingInterestingWithMyApiDistributedWantParams004(const char * data,size_t size)172 bool DoSomethingInterestingWithMyApiDistributedWantParams004(const char* data, size_t size)
173 {
174     DistributedWantParams wantOther;
175     std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
176 
177     std::string value(data, size);
178     sptr<IInterface> stringObj =
179         DistributedWantParams::GetInterfaceByType(DistributedWantParams::VALUE_TYPE_STRING, value);
180     wantParams->CompareInterface(stringObj, stringObj, DistributedWantParams::VALUE_TYPE_STRING);
181 
182     std::string keyStr = "12345667";
183     wantParams->SetParam(keyStr, String::Box(value));
184     Parcel in;
185     wantParams->Marshalling(in);
186     std::shared_ptr<DistributedWantParams> wantParamsNew(DistributedWantParams::Unmarshalling(in));
187 
188     wantParams->ToWantParams();
189     return true;
190 }
191 }
192 
193 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)194 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
195 {
196     /* Run your code on data */
197     if (data == nullptr) {
198         std::cout << "invalid data" << std::endl;
199         return 0;
200     }
201 
202     /* Validate the length of size */
203     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
204         return 0;
205     }
206 
207     char* ch = (char *)malloc(size + 1);
208     if (ch == nullptr) {
209         std::cout << "malloc failed." << std::endl;
210         return 0;
211     }
212 
213     (void)memset_s(ch, size + 1, 0x00, size + 1);
214     if (memcpy_s(ch, size + 1, data, size) != EOK) {
215         std::cout << "copy failed." << std::endl;
216         free(ch);
217         ch = nullptr;
218         return 0;
219     }
220 
221     OHOS::DoSomethingInterestingWithMyApiDistributedWantParams001(ch, size);
222     OHOS::DoSomethingInterestingWithMyApiDistributedWantParams002(ch, size);
223     OHOS::DoSomethingInterestingWithMyApiDistributedWantParams003(ch, size);
224     OHOS::DoSomethingInterestingWithMyApiDistributedWantParams004(ch, size);
225     free(ch);
226     ch = nullptr;
227     return 0;
228 }
229