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 protected:
CheckPermission()62 int32_t CheckPermission() override
63 {
64 return ERR_OK;
65 }
66 };
67
GetU32Data(const char * ptr)68 uint32_t GetU32Data(const char* ptr)
69 {
70 if (ptr == nullptr) {
71 return 0;
72 }
73 // 将第0个数字左移24位,将第1个数字左移16位,将第2个数字左移8位,第3个数字不左移
74 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
75 }
76
DoSomethingInterestingWithMyAPI(const char * data,size_t size)77 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
78 {
79 uint32_t code = GetU32Data(data);
80 MessageParcel datas;
81 datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
82 datas.WriteBuffer(data, size);
83 datas.RewindRead(0);
84 MessageParcel reply;
85 MessageOption option;
86 std::shared_ptr<FormRenderStub> formRenderStub = std::make_shared<FormRenderStubFuzzTest>();
87 formRenderStub->OnRemoteRequest(code, datas, reply, option);
88 formRenderStub->HandleRenderForm(datas, reply);
89 formRenderStub->HandleCleanFormHost(datas, reply);
90 formRenderStub->HandleReloadForm(datas, reply);
91 formRenderStub->HandleStopRenderingForm(datas, reply);
92 formRenderStub->HandleOnUnlock(datas, reply);
93 formRenderStub->HandleSetVisibleChange(datas, reply);
94 formRenderStub->HandleRecoverForm(datas, reply);
95 return true;
96 }
97 }
98
99 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
101 {
102 /* Run your code on data */
103 if (data == nullptr) {
104 return 0;
105 }
106
107 if (size < OHOS::U32_AT_SIZE) {
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