1 /*
2  * Copyright (c) 2023 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 "formrenderstub_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "form_render_stub.h"
24 #undef private
25 #undef protected
26 #include "message_parcel.h"
27 #include "securec.h"
28 
29 using namespace OHOS::AppExecFwk;
30 
31 namespace OHOS {
32 constexpr size_t FOO_MAX_LEN = 1024;
33 constexpr size_t U32_AT_SIZE = 4;
34 const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.appexecfwk.FormRender";
35 
36 class FormRenderStubFuzzTest : public FormRenderStub {
37 public:
38     FormRenderStubFuzzTest() = default;
39     virtual ~FormRenderStubFuzzTest() = default;
RenderForm(const FormJsInfo & formJsInfo,const Want & want,sptr<IRemoteObject> callerToken)40     int32_t RenderForm(const FormJsInfo &formJsInfo, const Want &want,
41         sptr<IRemoteObject> callerToken) override
42     {
43         return 0;
44     }
45 
StopRenderingForm(const FormJsInfo & formJsInfo,const Want & want,const sptr<IRemoteObject> & callerToken)46     int32_t StopRenderingForm(
47         const FormJsInfo &formJsInfo, const Want &want, const sptr<IRemoteObject> &callerToken) override
48     {
49         return 0;
50     }
51 
CleanFormHost(const sptr<IRemoteObject> & hostToken)52     int32_t CleanFormHost(const sptr<IRemoteObject> &hostToken) override
53     {
54         return 0;
55     }
56 
ReloadForm(const std::vector<FormJsInfo> && formJsInfos,const Want & want)57     int32_t ReloadForm(const std::vector<FormJsInfo> &&formJsInfos, const Want &want) override
58     {
59         return 0;
60     }
61 };
62 
GetU32Data(const char * ptr)63 uint32_t GetU32Data(const char* ptr)
64 {
65     if (ptr == nullptr) {
66         return 0;
67     }
68     // 将第0个数字左移24位,将第1个数字左移16位,将第2个数字左移8位,第3个数字不左移
69     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
70 }
71 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)72 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
73 {
74     uint32_t code = GetU32Data(data);
75     MessageParcel datas;
76     datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
77     datas.WriteBuffer(data, size);
78     datas.RewindRead(0);
79     MessageParcel reply;
80     MessageOption option;
81     std::shared_ptr<FormRenderStub> formRenderStub = std::make_shared<FormRenderStubFuzzTest>();
82     formRenderStub->OnRemoteRequest(code, datas, reply, option);
83     formRenderStub->HandleRenderForm(datas, reply);
84     formRenderStub->HandleCleanFormHost(datas, reply);
85     formRenderStub->HandleReloadForm(datas, reply);
86     formRenderStub->HandleStopRenderingForm(datas, reply);
87     formRenderStub->HandleOnUnlock(datas, reply);
88     formRenderStub->HandleSetVisibleChange(datas, reply);
89     formRenderStub->HandleRecoverForm(datas, reply);
90     return true;
91 }
92 }
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
96 {
97     /* Run your code on data */
98     if (data == nullptr) {
99         return 0;
100     }
101 
102     if (size < OHOS::U32_AT_SIZE) {
103         return 0;
104     }
105 
106     /* Validate the length of size */
107     if (size == 0 || size > OHOS::FOO_MAX_LEN) {
108         return 0;
109     }
110 
111     char* ch = static_cast<char*>(malloc(size + 1));
112     if (ch == nullptr) {
113         return 0;
114     }
115 
116     (void)memset_s(ch, size + 1, 0x00, size + 1);
117     if (memcpy_s(ch, size + 1, data, size) != EOK) {
118         free(ch);
119         ch = nullptr;
120         return 0;
121     }
122 
123     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
124     free(ch);
125     ch = nullptr;
126     return 0;
127 }
128 
129