/src/fluent-bit/tests/internal/fuzzers/fstore_fuzzer.c
Line | Count | Source |
1 | | /* Fluent Bit |
2 | | * ========== |
3 | | * Copyright (C) 2019-2022 The Fluent Bit Authors |
4 | | * Copyright (C) 2015-2018 Treasure Data Inc. |
5 | | * |
6 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | * you may not use this file except in compliance with the License. |
8 | | * You may obtain a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, software |
13 | | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | * See the License for the specific language governing permissions and |
16 | | * limitations under the License. |
17 | | */ |
18 | | |
19 | | #include <stdint.h> |
20 | | #include <string.h> |
21 | | #include <stdlib.h> |
22 | | #include <assert.h> |
23 | | |
24 | | #include <fluent-bit/flb_info.h> |
25 | | #include <fluent-bit/flb_fstore.h> |
26 | | #include <fluent-bit/flb_mem.h> |
27 | | #include <fluent-bit/flb_compat.h> |
28 | | |
29 | | #include <chunkio/chunkio.h> |
30 | | #include <chunkio/cio_utils.h> |
31 | | |
32 | | #include <sys/types.h> |
33 | | #include <sys/stat.h> |
34 | | |
35 | | |
36 | 294 | #define FSF_STORE_PATH "/tmp/flb-fstore" |
37 | | |
38 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
39 | 149 | { |
40 | 149 | int ret; |
41 | 149 | void *out_buf; |
42 | 149 | size_t out_size; |
43 | 149 | struct stat st_data; |
44 | 149 | struct flb_fstore *fs; |
45 | 149 | struct flb_fstore_stream *st; |
46 | 149 | struct flb_fstore_file *fsf; |
47 | | |
48 | | /* Set flb_malloc_mod to be fuzzer-data dependent */ |
49 | 149 | if (size < 4) { |
50 | 2 | return 0; |
51 | 2 | } |
52 | 147 | flb_malloc_p = 0; |
53 | 147 | flb_malloc_mod = *(int*)data; |
54 | 147 | data += 4; |
55 | 147 | size -= 4; |
56 | | |
57 | | /* Avoid division by zero for modulo operations */ |
58 | 147 | if (flb_malloc_mod == 0) { |
59 | 3 | flb_malloc_mod = 1; |
60 | 3 | } |
61 | | |
62 | 147 | cio_utils_recursive_delete(FSF_STORE_PATH); |
63 | 147 | fs = flb_fstore_create(FSF_STORE_PATH, FLB_FSTORE_FS); |
64 | 147 | if (fs == NULL) { |
65 | 5 | return 0; |
66 | 5 | } |
67 | 142 | st = flb_fstore_stream_create(fs, "abc"); |
68 | 142 | if (st != NULL) { |
69 | 139 | fsf = flb_fstore_file_create(fs, st, "example.txt", size); |
70 | | |
71 | 139 | if (fsf != NULL) { |
72 | 135 | ret = flb_fstore_file_append(fsf, data, size); |
73 | 135 | if (ret == 0) { |
74 | 135 | ret = flb_fstore_file_content_copy(fs, fsf, &out_buf, &out_size); |
75 | 135 | if (ret == 0) { |
76 | 135 | assert(memcmp(out_buf, data, size) == 0); |
77 | 135 | } |
78 | 135 | flb_free(out_buf); |
79 | 135 | } |
80 | 135 | } |
81 | 139 | } |
82 | | |
83 | 142 | flb_fstore_dump(fs); |
84 | 142 | flb_fstore_destroy(fs); |
85 | 142 | return 0; |
86 | 142 | } |