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