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