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 "eventserver_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <securec.h>
21 
22 #include "decoded/decoded_event.h"
23 #include "event_server.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace {
28 constexpr size_t BUFFER_SIZE = 384 * 1024 + 1;
29 }
SysEventServerFuzzTest(const uint8_t * data,size_t size)30 static void SysEventServerFuzzTest(const uint8_t* data, size_t size)
31 {
32     if (size < static_cast<int32_t>(EventRaw::GetValidDataMinimumByteCount())) {
33         return;
34     }
35     char* buffer = new char[BUFFER_SIZE]();
36     if (memcpy_s(buffer, BUFFER_SIZE, data, size) != EOK) {
37         delete[] buffer;
38         return;
39     }
40     buffer[size] = 0;
41     int32_t dataByteCnt = *(reinterpret_cast<int32_t*>(buffer));
42     if (dataByteCnt != static_cast<int32_t>(size)) {
43         delete[] buffer;
44         return;
45     }
46     EventRaw::DecodedEvent event(reinterpret_cast<uint8_t*>(buffer));
47     (void)event.AsJsonStr();
48     delete[] buffer;
49 }
50 } // namespace HiviewDFX
51 } // namespace OHOS
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     /* Run your code on data */
57     OHOS::HiviewDFX::SysEventServerFuzzTest(data, size);
58     return 0;
59 }
60 
61