• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "event_runner.h"
17 #include "eventrunner_fuzzer.h"
18 #include "securec.h"
19 
20 namespace OHOS {
21 namespace {
22     constexpr size_t U32_AT_SIZE = 4;
23     static auto runner = AppExecFwk::EventRunner::Create(true);
24     [[maybe_unused]] static auto runner2 = AppExecFwk::EventRunner::Create("EventRunnerFuzzTest");
25 }
26 
27 class DumperTest : public AppExecFwk::Dumper {
28 public:
29     DumperTest() = default;
~DumperTest()30     virtual ~DumperTest()
31     {};
Dump(const std::string & message)32     void Dump(const std::string &message) override
33     {}
GetTag()34     std::string GetTag() override
35     {
36         return {};
37     }
38 };
39 
40 class LoggerTest : public AppExecFwk::Logger {
41 public:
42     LoggerTest() = default;
~LoggerTest()43     virtual ~LoggerTest()
44     {};
Log(const std::string & line)45     void Log(const std::string &line) override
46     {}
47 };
48 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)49 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
50 {
51     DumperTest dumper;
52     std::string stringData(data);
53     std::shared_ptr<LoggerTest> logger = std::make_shared<LoggerTest>();
54     runner->Dump(dumper);
55     runner->DumpRunnerInfo(stringData);
56     runner->SetLogger(logger);
57     runner->GetCurrentEventQueue();
58     runner->GetThreadId();
59     runner->IsCurrentRunnerThread();
60     runner->Current();
61     runner->Run();
62     runner->Stop();
63     runner->GetEventQueue();
64     runner->GetMainEventRunner();
65     return true;
66 }
67 }
68 
69 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
71 {
72     /* Run your code on data */
73     if (data == nullptr) {
74         return 0;
75     }
76 
77     if (size < OHOS::U32_AT_SIZE) {
78         return 0;
79     }
80 
81     char* ch = (char *)malloc(size + 1);
82     if (ch == nullptr) {
83         return 0;
84     }
85 
86     (void)memset_s(ch, size + 1, 0x00, size + 1);
87     if (memcpy_s(ch, size, data, size) != EOK) {
88         free(ch);
89         ch = nullptr;
90         return 0;
91     }
92 
93     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
94     free(ch);
95     ch = nullptr;
96     return 0;
97 }
98