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
FuzzForwardReadStream(const uint8_t * data,size_t size)22 bool FuzzForwardReadStream(const uint8_t *data, size_t size)
23 {
24 if (size <= 0) {
25 return true;
26 }
27 HTaskInfo hTaskInfo = new TaskInformation();
28 HdcSessionBase *daemon = new HdcSessionBase(false);
29 hTaskInfo->ownerSessionClass = daemon;
30 HdcForwardBase *forward = new(std::nothrow) HdcForwardBase(hTaskInfo);
31 if (forward == nullptr) {
32 delete daemon;
33 delete hTaskInfo;
34 WRITE_LOG(LOG_FATAL, "FuzzForwardReadStream forward is null");
35 return false;
36 }
37 HdcForwardBase::HCtxForward ctx = (HdcForwardBase::HCtxForward)forward->MallocContext(true);
38 if (ctx == nullptr) {
39 delete daemon;
40 delete forward;
41 delete hTaskInfo;
42 WRITE_LOG(LOG_FATAL, "FuzzForwardReadStream ctx is null");
43 return false;
44 }
45 uv_stream_t *stream = (uv_stream_t *)&ctx->tcp;
46 uint8_t *base = new uint8_t[size];
47 if (memcpy_s(base, size, const_cast<uint8_t *>(data), size) == EOK) {
48 uv_buf_t rbf = uv_buf_init(reinterpret_cast<char *>(base), size);
49 forward->ReadForwardBuf(stream, (ssize_t)size, &rbf);
50 } else {
51 delete[] base;
52 }
53 delete ctx;
54 delete daemon;
55 delete forward;
56 delete hTaskInfo;
57 return true;
58 }
59 } // namespace Hdc
60
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
63 {
64 /* Run your code on data */
65 Hdc::FuzzForwardReadStream(data, size);
66 return 0;
67 }
68