1 /*
2 * Copyright (c) 2024 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 "eventserversocket_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <securec.h>
21
22 #include "hiview_platform.h"
23 #include "sysevent_source.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace {
28 constexpr size_t BUFFER_SIZE = 384 * 1024 + 1;
29 }
ConverRawData(char * source)30 std::shared_ptr<EventRaw::RawData> ConverRawData(char* source)
31 {
32 if (source == nullptr) {
33 return nullptr;
34 }
35 uint32_t sourceLen = *(reinterpret_cast<uint32_t*>(source));
36 uint32_t desLen = sourceLen + sizeof(uint8_t);
37 uint8_t* des = reinterpret_cast<uint8_t*>(malloc(desLen));
38 if (des == nullptr) {
39 return nullptr;
40 }
41 uint32_t sourceHeaderLen = sizeof(int32_t) + sizeof(EventRaw::HiSysEventHeader) - sizeof(uint8_t);
42 if (memcpy_s(des, desLen, source, sourceHeaderLen) != EOK) {
43 free(des);
44 return nullptr;
45 }
46 *(reinterpret_cast<uint8_t*>(des + sourceHeaderLen)) = 0; // init header.log flag
47 uint32_t desPos = sourceHeaderLen + sizeof(uint8_t);
48 if (memcpy_s(des + desPos, desLen - desPos, source + sourceHeaderLen, sourceLen - sourceHeaderLen) != EOK) {
49 free(des);
50 return nullptr;
51 }
52 *(reinterpret_cast<int32_t*>(des)) = desLen;
53 auto rawData = std::make_shared<EventRaw::RawData>(des, desLen);
54 free(des);
55 return rawData;
56 }
57
SysEventServerSocketFuzzTest(const uint8_t * data,size_t size)58 void SysEventServerSocketFuzzTest(const uint8_t* data, size_t size)
59 {
60 if (size < static_cast<int32_t>(EventRaw::GetValidDataMinimumByteCount())) {
61 return;
62 }
63 char* buffer = new char[BUFFER_SIZE]();
64 if (memcpy_s(buffer, BUFFER_SIZE, data, size) != EOK) {
65 delete[] buffer;
66 return;
67 }
68 buffer[size] = 0;
69 HiviewPlatform platform;
70 SysEventSource source;
71 source.SetHiviewContext(&platform);
72 source.OnLoad();
73 std::shared_ptr<SysEventReceiver> receiver = std::make_shared<SysEventReceiver>(source);
74 SocketDevice device;
75 if (!device.IsValidMsg(buffer, size)) {
76 delete[] buffer;
77 return;
78 }
79 receiver->HandlerEvent(ConverRawData(buffer));
80 }
81 } // namespace HiviewDFX
82 } // namespace OHOS
83
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87 /* Run your code on data */
88 OHOS::HiviewDFX::SysEventServerSocketFuzzTest(data, size);
89 return 0;
90 }
91
92