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 "formproviderstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "form_provider_stub.h"
22 #include "message_parcel.h"
23 #include "securec.h"
24
25 using namespace OHOS::AppExecFwk;
26
27 namespace OHOS {
28 constexpr size_t U32_AT_SIZE = 4;
29 const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.appexecfwk.FormMgr";
30
31 class FormProviderStubFuzzTest : public FormProviderStub {
32 public:
33 FormProviderStubFuzzTest() = default;
34 virtual ~FormProviderStubFuzzTest() = default;
AcquireProviderFormInfo(const FormJsInfo & formJsInfo,const Want & want,const sptr<IRemoteObject> & callerToken)35 int AcquireProviderFormInfo(const FormJsInfo &formJsInfo, const Want &want,
36 const sptr<IRemoteObject> &callerToken) override
37 {
38 return 0;
39 }
NotifyFormDelete(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)40 int NotifyFormDelete(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken) override
41 {
42 return 0;
43 }
NotifyFormsDelete(const std::vector<int64_t> & formIds,const Want & want,const sptr<IRemoteObject> & callerToken)44 int NotifyFormsDelete(const std::vector<int64_t> &formIds, const Want &want,
45 const sptr<IRemoteObject> &callerToken) override
46 {
47 return 0;
48 }
NotifyFormUpdate(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)49 int NotifyFormUpdate(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken) override
50 {
51 return 0;
52 }
EventNotify(const std::vector<int64_t> & formIds,const int32_t formVisibleType,const Want & want,const sptr<IRemoteObject> & callerToken)53 int EventNotify(const std::vector<int64_t> &formIds, const int32_t formVisibleType,
54 const Want &want, const sptr<IRemoteObject> &callerToken) override
55 {
56 return 0;
57 }
NotifyFormCastTempForm(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)58 int NotifyFormCastTempForm(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken) override
59 {
60 return 0;
61 }
FireFormEvent(const int64_t formId,const std::string & message,const Want & want,const sptr<IRemoteObject> & callerToken)62 int FireFormEvent(const int64_t formId, const std::string &message, const Want &want,
63 const sptr<IRemoteObject> &callerToken) override
64 {
65 return 0;
66 }
67
AcquireState(const Want & wantArg,const std::string & provider,const Want & want,const sptr<IRemoteObject> & callerToken)68 int AcquireState(const Want &wantArg, const std::string &provider, const Want &want,
69 const sptr<IRemoteObject> &callerToken) override
70 {
71 return 0;
72 }
AcquireShareFormData(int64_t formId,const std::string & remoteDeviceId,const sptr<IRemoteObject> & formSupplyCallback,int64_t requestCode)73 int32_t AcquireShareFormData(int64_t formId, const std::string &remoteDeviceId,
74 const sptr<IRemoteObject> &formSupplyCallback, int64_t requestCode) override
75 {
76 return 0;
77 }
AcquireFormData(int64_t formId,const sptr<IRemoteObject> & formSupplyCallback,int64_t requestCode)78 int32_t AcquireFormData(int64_t formId, const sptr<IRemoteObject> &formSupplyCallback,
79 int64_t requestCode) override
80 {
81 return 0;
82 }
83 };
84
GetU32Data(const char * ptr)85 uint32_t GetU32Data(const char* ptr)
86 {
87 if (ptr == nullptr) {
88 return 0;
89 }
90 // 将第0个数字左移24位,将第1个数字左移16位,将第2个数字左移8位,第3个数字不左移
91 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
92 }
93
DoSomethingInterestingWithMyAPI(const char * data,size_t size)94 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
95 {
96 uint32_t code = GetU32Data(data);
97 MessageParcel datas;
98 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
99 datas.WriteBuffer(data, size);
100 datas.RewindRead(0);
101 MessageParcel reply;
102 MessageOption option;
103 std::shared_ptr<FormProviderStub> formproviderstub = std::make_shared<FormProviderStubFuzzTest>();
104 formproviderstub->OnRemoteRequest(code, datas, reply, option);
105 return true;
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 return 0;
115 }
116
117 if (size < OHOS::U32_AT_SIZE) {
118 return 0;
119 }
120
121 char* ch = static_cast<char*>(malloc(size + 1));
122 if (ch == nullptr) {
123 return 0;
124 }
125
126 (void)memset_s(ch, size + 1, 0x00, size + 1);
127 if (memcpy_s(ch, size + 1, data, size) != EOK) {
128 free(ch);
129 ch = nullptr;
130 return 0;
131 }
132
133 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
134 free(ch);
135 ch = nullptr;
136 return 0;
137 }
138
139