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