• 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 "formsupplystub_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "form_supply_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.FormMgr";
31 
32 class FormSupplyStubFuzzTest : public FormSupplyStub {
33 public:
34     FormSupplyStubFuzzTest() = default;
35     virtual ~FormSupplyStubFuzzTest() = default;
OnAcquire(const FormProviderInfo & formInfo,const Want & want)36     int OnAcquire(const FormProviderInfo &formInfo, const Want &want) override
37     {
38         return 0;
39     }
OnEventHandle(const Want & want)40     int OnEventHandle(const Want &want) override
41     {
42         return 0;
43     }
OnAcquireStateResult(FormState state,const std::string & provider,const Want & wantArg,const Want & want)44     int OnAcquireStateResult(FormState state, const std::string &provider,
45         const Want &wantArg, const Want &want) override
46     {
47         return 0;
48     }
OnShareAcquire(int64_t formId,const std::string & remoteDeviceId,const AAFwk::WantParams & wantParams,int64_t requestCode,const bool & result)49     void OnShareAcquire(int64_t formId, const std::string &remoteDeviceId,
50         const AAFwk::WantParams &wantParams, int64_t requestCode, const bool &result) override
51     {}
OnRenderTaskDone(int64_t formId,const Want & want)52     int32_t OnRenderTaskDone(int64_t formId, const Want &want) override
53     {
54         return ERR_OK;
55     }
OnStopRenderingTaskDone(int64_t formId,const Want & want)56     int32_t OnStopRenderingTaskDone(int64_t formId, const Want &want) override
57     {
58         return ERR_OK;
59     }
OnAcquireDataResult(const AAFwk::WantParams & wantParams,int64_t requestCode)60     int OnAcquireDataResult(const AAFwk::WantParams &wantParams, int64_t requestCode) override
61     {
62         return ERR_OK;
63     }
64 };
65 
Convert2Uint32(const char * ptr)66 uint32_t Convert2Uint32(const char* ptr)
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 = Convert2Uint32(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<FormSupplyStub> formsupplystub = std::make_shared<FormSupplyStubFuzzTest>();
82     formsupplystub->OnRemoteRequest(code, datas, reply, option);
83     return true;
84 }
85 }
86 
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89 {
90     /* Run your code on data */
91     if (data == nullptr) {
92         return 0;
93     }
94 
95     if (size < OHOS::U32_AT_SIZE) {
96         return 0;
97     }
98 
99     /* Validate the length of size */
100     if (size == 0 || size > OHOS::FOO_MAX_LEN) {
101         return 0;
102     }
103 
104     char* ch = static_cast<char*>(malloc(size + 1));
105     if (ch == nullptr) {
106         return 0;
107     }
108 
109     (void)memset_s(ch, size + 1, 0x00, size + 1);
110     if (memcpy_s(ch, size + 1, data, size) != EOK) {
111         free(ch);
112         ch = nullptr;
113         return 0;
114     }
115 
116     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
117     free(ch);
118     ch = nullptr;
119     return 0;
120 }
121 
122