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