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 "JdwpReadStream_fuzzer.h"
17 #include <uv.h>
18 #include <uv_status.h>
19
20 namespace Hdc {
21 class HdcJdwpFuzzer : public HdcJdwp {
22 public:
HdcJdwpFuzzer(uv_loop_t * loop,LoopStatus * loopStatus)23 explicit HdcJdwpFuzzer(uv_loop_t *loop, LoopStatus *loopStatus) : HdcJdwp(loop, loopStatus) {}
24
Instance(uv_loop_t * loop,LoopStatus * loopStatus)25 static std::unique_ptr<HdcJdwpFuzzer> Instance(uv_loop_t *loop, LoopStatus *loopStatus)
26 {
27 std::unique_ptr<HdcJdwpFuzzer> jdwp = std::make_unique<HdcJdwpFuzzer>(loop, loopStatus);
28 return jdwp;
29 }
30 };
31
FuzzJdwpReadStream(const uint8_t * data,size_t size)32 bool FuzzJdwpReadStream(const uint8_t *data, size_t size)
33 {
34 uv_loop_t loop;
35 uv_loop_init(&loop);
36 LoopStatus ls(&loop, "not support");
37 auto jdwp = HdcJdwpFuzzer::Instance(&loop, &ls);
38 if (jdwp == nullptr) {
39 WRITE_LOG(LOG_FATAL, "FuzzJdwpReadStream jdwp is null");
40 return false;
41 }
42 HdcJdwp::HCtxJdwp ctx = (HdcJdwp::HCtxJdwp)jdwp->MallocContext();
43 if (ctx == nullptr) {
44 WRITE_LOG(LOG_FATAL, "FuzzJdwpReadStream ctx is null");
45 return false;
46 }
47 ctx->finish = true;
48 uv_pipe_t pipe;
49 pipe.data = ctx;
50 uv_stream_t *stream = (uv_stream_t *)&pipe;
51 uv_buf_t rbf = uv_buf_init(reinterpret_cast<char *>(const_cast<uint8_t *>(data)), size);
52 jdwp->ReadStream(stream, (ssize_t)size, &rbf);
53 delete ctx;
54 uv_stop(&loop);
55 uv_loop_close(&loop);
56 return true;
57 }
58 } // namespace Hdc
59
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
62 {
63 /* Run your code on data */
64 Hdc::FuzzJdwpReadStream(data, size);
65 return 0;
66 }
67