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 <cstddef>
17 #include <cstdint>
18 #include <string>
19 
20 #include "faultlogger_client.h"
21 #include "faultlogger_client_fuzzer.h"
22 #include "faultlogger_fuzzertest_common.h"
23 
24 namespace OHOS {
25 const int FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH = 50;
26 
FuzzInterfaceAddFaultLog(const uint8_t * data,size_t size)27 void FuzzInterfaceAddFaultLog(const uint8_t* data, size_t size)
28 {
29     FaultLogInfoInner inner;
30     int32_t faultLogType {0};
31     int offsetTotalLength = sizeof(inner.time) + sizeof(inner.id) + sizeof(inner.pid) + sizeof(faultLogType) +
32                             (4 * FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH); // 4 : Offset by 4 string length
33     if (offsetTotalLength > size) {
34         return;
35     }
36 
37     STREAM_TO_VALUEINFO(data, inner.time);
38     STREAM_TO_VALUEINFO(data, inner.id);
39     STREAM_TO_VALUEINFO(data, inner.pid);
40     STREAM_TO_VALUEINFO(data, faultLogType);
41     inner.faultLogType = abs(faultLogType % 10); // 10 : get the absolute value of the last digit of the number
42 
43     std::string module(reinterpret_cast<const char*>(data), FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH);
44     data += FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH;
45     inner.module = module;
46     std::string reason(reinterpret_cast<const char*>(data), FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH);
47     data += FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH;
48     inner.reason = reason;
49     std::string summary(reinterpret_cast<const char*>(data), FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH);
50     data += FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH;
51     inner.summary = summary;
52     std::string logPath(reinterpret_cast<const char*>(data), FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH);
53     data += FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH;
54     inner.logPath = logPath;
55     HiviewDFX::AddFaultLog(inner);
56     HiviewDFX::AddFaultLog(inner.time, inner.faultLogType, inner.module, inner.summary);
57 }
58 
FuzzInterfaceQuerySelfFaultLog(const uint8_t * data,size_t size)59 void FuzzInterfaceQuerySelfFaultLog(const uint8_t* data, size_t size)
60 {
61     int32_t faultLogType;
62     int32_t count;
63     int offsetTotalLength = sizeof(faultLogType) + sizeof(count);
64     if (offsetTotalLength > size) {
65         return;
66     }
67 
68     STREAM_TO_VALUEINFO(data, faultLogType);
69     faultLogType = abs(faultLogType % 10); // 10 : get the absolute value of the last digit of the number
70     STREAM_TO_VALUEINFO(data, count);
71 
72     HiviewDFX::FaultLogType type = static_cast<HiviewDFX::FaultLogType>(faultLogType);
73     auto result = HiviewDFX::QuerySelfFaultLog(type, count);
74     if (result != nullptr) {
75         while (result->HasNext()) {
76             result->Next();
77         }
78     }
79 }
80 
FuzzFaultloggerClientInterface(const uint8_t * data,size_t size)81 void FuzzFaultloggerClientInterface(const uint8_t* data, size_t size)
82 {
83     FuzzInterfaceAddFaultLog(data, size);
84     FuzzInterfaceQuerySelfFaultLog(data, size);
85 }
86 }
87 
88 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
90 {
91     if (data == nullptr || size == 0) {
92         return 0;
93     }
94     OHOS::FuzzFaultloggerClientInterface(data, size);
95     return 0;
96 }