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