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