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