• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 uint8_t * data,size_t size)57 bool DoSomethingInterestingWithMyApiDistributedWantParams001(const uint8_t* data, size_t size)
58 {
59     if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
60         return false;
61     }
62 
63     DistributedWantParams wantOther;
64     std::string key(reinterpret_cast<const char*>(data), size);
65     std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
66     sptr<AAFwk::IArray> array = new (std::nothrow) AAFwk::Array(0, DistributedSchedule::g_IID_IDistributedWantParams);
67     wantParams->SetParam(key, array);
68     IArray *iarray = IArray::Query(array);
69     sptr<IArray> destAO = nullptr;
70     wantParams->NewArrayData(iarray, destAO);
71     sptr<IInterface> stringIt = String::Box(key);
72     wantParams->Remove(key);
73     wantParams->SetParam(key, stringIt);
74     wantParams->KeySet();
75     wantParams->HasParam(key);
76     wantParams->IsEmpty();
77     wantParams->GetParams();
78     return true;
79 }
80 
DoSomethingInterestingWithMyApiDistributedWantParams002(const uint8_t * data,size_t size)81 bool DoSomethingInterestingWithMyApiDistributedWantParams002(const uint8_t* data, size_t size)
82 {
83     if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
84         return false;
85     }
86 
87     DistributedWantParams wantOther;
88     std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
89     Parcel parcel;
90     IArray* ao = nullptr;
91     wantParams->WriteArrayToParcelChar(parcel, ao);
92     wantParams->WriteArrayToParcelShort(parcel, ao);
93     wantParams->WriteArrayToParcelString(parcel, ao);
94     wantParams->WriteArrayToParcelBool(parcel, ao);
95     wantParams->WriteArrayToParcelByte(parcel, ao);
96     wantParams->WriteArrayToParcelInt(parcel, ao);
97     wantParams->WriteArrayToParcelLong(parcel, ao);
98     wantParams->WriteArrayToParcelFloat(parcel, ao);
99     wantParams->WriteArrayToParcelDouble(parcel, ao);
100     long longValue = static_cast<long>(GetU32Data(reinterpret_cast<const char*>(data)));
101     sptr<IInterface> longIt = Long::Box(longValue);
102     wantParams->WriteToParcelLong(parcel, longIt);
103     float floatValue = static_cast<float>(GetU32Data(reinterpret_cast<const char*>(data)));
104     sptr<IInterface> floatIt = Float::Box(floatValue);
105     wantParams->WriteToParcelFloat(parcel, floatIt);
106 
107     sptr<IArray> array;
108     wantParams->ReadFromParcelArrayChar(parcel, array);
109     wantParams->ReadFromParcelArrayShort(parcel, array);
110     wantParams->ReadFromParcelArrayString(parcel, array);
111     wantParams->ReadFromParcelArrayBool(parcel, array);
112     wantParams->ReadFromParcelArrayByte(parcel, array);
113     wantParams->ReadFromParcelArrayInt(parcel, array);
114     wantParams->ReadFromParcelArrayLong(parcel, array);
115     wantParams->ReadFromParcelArrayFloat(parcel, array);
116     wantParams->ReadFromParcelArrayDouble(parcel, array);
117     std::string key(reinterpret_cast<const char*>(data), size);
118     wantParams->ReadFromParcelLong(parcel, key);
119     wantParams->ReadFromParcelFloat(parcel, key);
120 
121     wantParams->ReadArrayToParcel(parcel, GetU32Data(reinterpret_cast<const char*>(data)) % OFFSET_16, array);
122     return true;
123 }
124 
DoSomethingInterestingWithMyApiDistributedWant003(const uint8_t * data,size_t size)125 bool DoSomethingInterestingWithMyApiDistributedWant003(const uint8_t* data, size_t size)
126 {
127     if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
128         return false;
129     }
130 
131     DistributedWantParams wantOther;
132     std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
133     Parcel parcel;
134     std::string key(reinterpret_cast<const char*>(data), size);
135     int8_t byteValue = static_cast<int8_t>(GetU32Data(reinterpret_cast<const char*>(data)));
136     sptr<IInterface> byteIt = Byte::Box(byteValue);
137     wantParams->WriteToParcelByte(parcel, byteIt);
138     sptr<IInterface> stringIt = String::Box(key);
139     wantParams->WriteToParcelString(parcel, stringIt);
140     bool boolValue = static_cast<bool>(GetU32Data(reinterpret_cast<const char*>(data)) > FOO_MAX_LEN);
141     sptr<IInterface> boolIt = Boolean::Box(boolValue);
142     wantParams->WriteToParcelBool(parcel, boolIt);
143     char charValue = *data;
144     sptr<IInterface> charIt = Char::Box(charValue);
145     wantParams->WriteToParcelChar(parcel, charIt);
146     short shortValue = static_cast<short>(GetU32Data(reinterpret_cast<const char*>(data)));
147     sptr<IInterface> shortIt = Short::Box(shortValue);
148     wantParams->WriteToParcelShort(parcel, shortIt);
149     double doubleValue = static_cast<double>(GetU32Data(reinterpret_cast<const char*>(data)));
150     sptr<IInterface> doubleIt = Double::Box(doubleValue);
151     wantParams->WriteToParcelDouble(parcel, doubleIt);
152     int32_t intValue = static_cast<int32_t>(GetU32Data(reinterpret_cast<const char*>(data)));
153     sptr<IInterface> intIt = Integer::Box(intValue);
154     wantParams->WriteToParcelInt(parcel, intIt);
155     wantParams->WriteToParcelFD(parcel, wantOther);
156     wantParams->WriteToParcelRemoteObject(parcel, wantOther);
157 
158     int type = static_cast<int>(GetU32Data(reinterpret_cast<const char*>(data)));
159     wantParams->ReadFromParcelInt8(parcel, key);
160     wantParams->ReadFromParcelString(parcel, key);
161     wantParams->ReadFromParcelBool(parcel, key);
162     wantParams->ReadFromParcelChar(parcel, key);
163     wantParams->ReadFromParcelShort(parcel, key);
164     wantParams->ReadFromParcelDouble(parcel, key);
165     wantParams->ReadFromParcelInt(parcel, key);
166     wantParams->ReadFromParcelFD(parcel, key);
167     wantParams->ReadFromParcelRemoteObject(parcel, key);
168     wantParams->ReadFromParcelWantParamWrapper(parcel, key, type);
169 
170     wantParams->WriteToParcelByte(parcel, byteIt);
171     wantParams->ReadFromParcel(parcel);
172 
173     DistributedUnsupportedData uData;
174     std::shared_ptr<DistributedUnsupportedData> unsupportedData = std::make_shared<DistributedUnsupportedData>(uData);
175     wantParams->cachedUnsupportedData_.emplace_back(std::move(uData));
176     wantParams->DoMarshalling(parcel);
177     wantParams->cachedUnsupportedData_.clear();
178     return true;
179 }
180 
DoSomethingInterestingWithMyApiDistributedWantParams004(const uint8_t * data,size_t size)181 bool DoSomethingInterestingWithMyApiDistributedWantParams004(const uint8_t* data, size_t size)
182 {
183     if (data == nullptr || size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
184         return false;
185     }
186 
187     DistributedWantParams wantOther;
188     std::shared_ptr<DistributedWantParams> wantParams = std::make_shared<DistributedWantParams>(wantOther);
189 
190     std::string value(reinterpret_cast<const char*>(data), size);
191     sptr<IInterface> stringObj =
192         DistributedWantParams::GetInterfaceByType(DistributedWantParams::VALUE_TYPE_STRING, value);
193     wantParams->CompareInterface(stringObj, stringObj, DistributedWantParams::VALUE_TYPE_STRING);
194 
195     std::string keyStr = "12345667";
196     wantParams->SetParam(keyStr, String::Box(value));
197     Parcel in;
198     wantParams->Marshalling(in);
199     std::shared_ptr<DistributedWantParams> wantParamsNew(DistributedWantParams::Unmarshalling(in));
200 
201     wantParams->ToWantParams();
202     return true;
203 }
204 }
205 
206 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)207 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
208 {
209     OHOS::DoSomethingInterestingWithMyApiDistributedWantParams001(data, size);
210     OHOS::DoSomethingInterestingWithMyApiDistributedWantParams002(data, size);
211     OHOS::DoSomethingInterestingWithMyApiDistributedWant003(data, size);
212     OHOS::DoSomethingInterestingWithMyApiDistributedWantParams004(data, size);
213     return 0;
214 }
215