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 }
24
25 class DumperTest : public AppExecFwk::Dumper {
26 public:
27 DumperTest() = default;
~DumperTest()28 virtual ~DumperTest()
29 {};
Dump(const std::string & message)30 void Dump(const std::string &message) override
31 {}
GetTag()32 std::string GetTag() override
33 {
34 return {};
35 }
36 };
37
38 class LoggerTest : public AppExecFwk::Logger {
39 public:
40 LoggerTest() = default;
~LoggerTest()41 virtual ~LoggerTest()
42 {};
Log(const std::string & line)43 void Log(const std::string &line) override
44 {}
45 };
46
DoSomethingInterestingWithMyAPI(const char * data,size_t size)47 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
48 {
49 DumperTest dumper;
50 std::string stringData(data);
51 std::shared_ptr<LoggerTest> logger = std::make_shared<LoggerTest>();
52 auto runner = AppExecFwk::EventRunner::Create(true);
53 runner->Dump(dumper);
54 runner->DumpRunnerInfo(stringData);
55 runner->SetLogger(logger);
56 runner->GetCurrentEventQueue();
57 runner->GetThreadId();
58 runner->IsCurrentRunnerThread();
59 return true;
60 }
61 }
62
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66 /* Run your code on data */
67 if (data == nullptr) {
68 return 0;
69 }
70
71 if (size < OHOS::U32_AT_SIZE) {
72 return 0;
73 }
74
75 char* ch = (char *)malloc(size + 1);
76 if (ch == nullptr) {
77 return 0;
78 }
79
80 (void)memset_s(ch, size + 1, 0x00, size + 1);
81 if (memcpy_s(ch, size, data, size) != EOK) {
82 free(ch);
83 ch = nullptr;
84 return 0;
85 }
86
87 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
88 free(ch);
89 ch = nullptr;
90 return 0;
91 }
92