• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 
3 Copyright (c) 2021 Huawei Device Co., Ltd.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 http://www.apache.org/licenses/LICENSE-2.0
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13 */
14 #include "hilogclient_fuzzer.h"
15 
16 #include <cstddef>
17 #include <cstdint>
18 #include <ctime>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <sys/syscall.h>
22 #include "hilog/log.h"
23 #include "hilog_common.h"
24 #include "hilog_input_socket_client.h"
25 
26 namespace OHOS {
27     static const char FUZZ_TEST[] = "fuzz test";
28     static const uint32_t FUZZ_DOMAIN = 0xD002D01;
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)29     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
30     {
31         HilogMsg header = {0};
32         struct timespec ts = {0};
33         (void)clock_gettime(CLOCK_REALTIME, &ts);
34         struct timespec tsMono = {0};
35         (void)clock_gettime(CLOCK_MONOTONIC, &tsMono);
36         header.tv_sec = static_cast<uint32_t>(ts.tv_sec);
37         header.tv_nsec = static_cast<uint32_t>(ts.tv_nsec);
38         header.mono_sec = static_cast<uint32_t>(tsMono.tv_sec);
39         header.pid = getpid();
40         header.tid = static_cast<uint32_t>(syscall(SYS_gettid));
41         header.type = LOG_CORE;
42         header.level = LOG_INFO;
43         header.domain = FUZZ_DOMAIN;
44         int ret = HilogWriteLogMessage(&header, FUZZ_TEST,
45             strlen(FUZZ_TEST) + 1, reinterpret_cast<const char*>(data), size);
46         if (ret < 0) {
47             return false;
48         }
49         return true;
50     }
51 }
52 
53 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)54 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
55 {
56     if (data == nullptr || size == 0) {
57         std::cout << "invalid data" << std::endl;
58         return 0;
59     }
60     /* Run your code on data */
61     OHOS::DoSomethingInterestingWithMyAPI(data, size);
62     return 0;
63 }