• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.FormProvider";
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     }
NotifyConfigurationUpdate(const AppExecFwk::Configuration & configuration,const Want & want,const sptr<IRemoteObject> & callerToken)62     int NotifyConfigurationUpdate(const AppExecFwk::Configuration &configuration,
63         const Want &want, const sptr<IRemoteObject> &callerToken) override
64     {
65         return 0;
66     }
FireFormEvent(const int64_t formId,const std::string & message,const Want & want,const sptr<IRemoteObject> & callerToken)67     int FireFormEvent(const int64_t formId, const std::string &message, const Want &want,
68         const sptr<IRemoteObject> &callerToken) override
69     {
70         return 0;
71     }
72 
NotifyFormLocationUpdate(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)73     int NotifyFormLocationUpdate(const int64_t formId, const Want &want,
74         const sptr<IRemoteObject> &callerToken) override
75     {
76         return 0;
77     }
78 
AcquireState(const Want & wantArg,const std::string & provider,const Want & want,const sptr<IRemoteObject> & callerToken)79     int AcquireState(const Want &wantArg, const std::string &provider, const Want &want,
80         const sptr<IRemoteObject> &callerToken) override
81     {
82         return 0;
83     }
AcquireShareFormData(int64_t formId,const std::string & remoteDeviceId,const sptr<IRemoteObject> & formSupplyCallback,int64_t requestCode)84     int32_t AcquireShareFormData(int64_t formId, const std::string &remoteDeviceId,
85         const sptr<IRemoteObject> &formSupplyCallback, int64_t requestCode) override
86     {
87         return 0;
88     }
AcquireFormData(int64_t formId,const sptr<IRemoteObject> & formSupplyCallback,int64_t requestCode)89     int32_t AcquireFormData(int64_t formId, const sptr<IRemoteObject> &formSupplyCallback,
90         int64_t requestCode) override
91     {
92         return 0;
93     }
94 };
95 
GetU32Data(const char * ptr)96 uint32_t GetU32Data(const char* ptr)
97 {
98     if (ptr == nullptr) {
99         return 0;
100     }
101     // 将第0个数字左移24位,将第1个数字左移16位,将第2个数字左移8位,第3个数字不左移
102     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
103 }
104 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)105 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
106 {
107     uint32_t code = GetU32Data(data);
108     MessageParcel datas;
109     datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
110     datas.WriteBuffer(data, size);
111     datas.RewindRead(0);
112     MessageParcel reply;
113     MessageOption option;
114     std::shared_ptr<FormProviderStub> formproviderstub = std::make_shared<FormProviderStubFuzzTest>();
115     formproviderstub->OnRemoteRequest(code, datas, reply, option);
116     return true;
117 }
118 }
119 
120 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)121 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
122 {
123     /* Run your code on data */
124     if (data == nullptr) {
125         return 0;
126     }
127 
128     if (size < OHOS::U32_AT_SIZE) {
129         return 0;
130     }
131 
132     char* ch = static_cast<char*>(malloc(size + 1));
133     if (ch == nullptr) {
134         return 0;
135     }
136 
137     (void)memset_s(ch, size + 1, 0x00, size + 1);
138     if (memcpy_s(ch, size + 1, data, size) != EOK) {
139         free(ch);
140         ch = nullptr;
141         return 0;
142     }
143 
144     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
145     free(ch);
146     ch = nullptr;
147     return 0;
148 }
149 
150