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 "ForwardReadStream_fuzzer.h"
17 #include <uv.h>
18 #include "forward.h"
19
20 namespace Hdc {
21 class HdcForwardFuzzer : public HdcForwardBase {
22 public:
HdcForwardFuzzer(HTaskInfo hTaskInfo)23 explicit HdcForwardFuzzer(HTaskInfo hTaskInfo) : HdcForwardBase(hTaskInfo) {}
24
Instance(HTaskInfo hTaskInfo)25 static std::unique_ptr<HdcForwardFuzzer> Instance(HTaskInfo hTaskInfo)
26 {
27 std::unique_ptr<HdcForwardFuzzer> forward = std::make_unique<HdcForwardFuzzer>(hTaskInfo);
28 return forward;
29 }
30 };
31
FuzzForwardReadStream(const uint8_t * data,size_t size)32 bool FuzzForwardReadStream(const uint8_t *data, size_t size)
33 {
34 HTaskInfo hTaskInfo = new TaskInformation();
35 HdcSessionBase *daemon = new HdcSessionBase(false);
36 hTaskInfo->ownerSessionClass = daemon;
37 auto forward = HdcForwardFuzzer::Instance(hTaskInfo);
38 if (forward == nullptr) {
39 WRITE_LOG(LOG_FATAL, "FuzzForwardReadStream forward is null");
40 return false;
41 }
42 HdcForwardBase::HCtxForward ctx = (HdcForwardBase::HCtxForward)forward->MallocContext(true);
43 if (ctx == nullptr) {
44 WRITE_LOG(LOG_FATAL, "FuzzForwardReadStream ctx is null");
45 return false;
46 }
47 uv_pipe_t pipe;
48 pipe.data = ctx;
49 uv_stream_t *stream = (uv_stream_t *)&pipe;
50 uv_buf_t *rbf = (uv_buf_t*)malloc(sizeof(uv_buf_t));
51 if (rbf == nullptr) {
52 return false;
53 }
54 if (size <= 0) {
55 return true;
56 }
57 rbf->base = new char[size];
58 (void)memcpy_s(rbf->base, size, reinterpret_cast<char *>(const_cast<uint8_t *>(data)), size);
59 forward->ReadForwardBuf(stream, (ssize_t)size, rbf);
60 delete ctx;
61 delete hTaskInfo;
62 delete daemon;
63 return true;
64 }
65 } // namespace Hdc
66
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
69 {
70 /* Run your code on data */
71 Hdc::FuzzForwardReadStream(data, size);
72 return 0;
73 }
74