/src/fluent-bit/tests/internal/fuzzers/engine_fuzzer.c
Line | Count | Source |
1 | | #include <fluent-bit.h> |
2 | | #include <fluent-bit/flb_parser.h> |
3 | | #include <fluent-bit/flb_parser.h> |
4 | | #include <fluent-bit/flb_input_chunk.h> |
5 | | #include <fluent-bit/flb_metrics.h> |
6 | | #include "flb_fuzz_header.h" |
7 | | |
8 | | #include <stdio.h> |
9 | | #include <monkey/mk_core.h> |
10 | | |
11 | | struct flb_parser *parser; |
12 | | flb_ctx_t *ctx; |
13 | | int in_ffd; |
14 | | int out_ffd; |
15 | | int filter_ffd; |
16 | | |
17 | 642 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
18 | 642 | if (size < 100) { |
19 | 13 | return 0; |
20 | 13 | } |
21 | | /* Set fuzzer-malloc chance of failure */ |
22 | 629 | flb_malloc_p = 0; |
23 | 629 | flb_malloc_mod = 25000; |
24 | | |
25 | 629 | uint8_t ud = data[0]; |
26 | 629 | MOVE_INPUT(1); |
27 | 635 | #define NM_SIZE 50 |
28 | 629 | char *null_terminated = get_null_terminated(NM_SIZE, &data, &size); |
29 | | |
30 | 629 | char *nm3 = get_null_terminated(10, &data, &size); |
31 | 629 | char *nm4 = get_null_terminated(10, &data, &size); |
32 | 629 | int random_i1 = *(int *)data; |
33 | 629 | MOVE_INPUT(4); |
34 | 629 | int random_i2 = *(int *)data; |
35 | | |
36 | 629 | #define FUNC_NUMS 10 |
37 | 629 | switch (ud % FUNC_NUMS) { |
38 | 191 | case 0: |
39 | 191 | flb_output(ctx, null_terminated, nm3); |
40 | 191 | break; |
41 | 134 | case 1: |
42 | 134 | flb_filter(ctx, null_terminated, nm3); |
43 | 134 | break; |
44 | 138 | case 2: |
45 | 138 | flb_input(ctx, null_terminated, nm3); |
46 | 138 | break; |
47 | 1 | case 3: |
48 | 1 | flb_output_check(ctx->config); |
49 | 1 | break; |
50 | 103 | case 4: { |
51 | 103 | struct mk_list *head; |
52 | 103 | struct flb_input_instance *entry; |
53 | 931 | mk_list_foreach(head, &ctx->config->inputs) { |
54 | 931 | entry = mk_list_entry(head, struct flb_input_instance, _head); |
55 | 931 | flb_input_name_exists(nm3, ctx->config); |
56 | 931 | flb_input_get_property(nm3, entry); |
57 | 931 | flb_input_name(entry); |
58 | 931 | flb_input_collector_running(0, entry); |
59 | 931 | flb_input_collector_pause(random_i1, entry); |
60 | 931 | flb_input_collector_resume(random_i2, entry); |
61 | 931 | flb_input_net_default_listener(nm4, random_i1, entry); |
62 | 931 | flb_input_collector_start(random_i2, entry); |
63 | 931 | } |
64 | 103 | } |
65 | 103 | break; |
66 | 11 | case 5: { |
67 | 11 | struct mk_list *head; |
68 | 11 | struct flb_input_instance *entry; |
69 | 72 | mk_list_foreach(head, &ctx->config->inputs) { |
70 | 72 | entry = mk_list_entry(head, struct flb_input_instance, _head); |
71 | 72 | if (entry->storage != NULL) { |
72 | 0 | char bufbuf[100]; |
73 | 0 | flb_input_chunk_append_raw(entry, FLB_INPUT_LOGS, 0, "A", |
74 | 0 | 1, "\0", 0); |
75 | |
|
76 | 0 | struct flb_input_chunk *ic = NULL; |
77 | 0 | ic = flb_input_chunk_create(entry, FLB_INPUT_LOGS, nm3, 10); |
78 | 0 | if (ic != NULL) { |
79 | 0 | flb_input_chunk_get_size(ic); |
80 | 0 | flb_input_chunk_set_up_down(ic); |
81 | 0 | flb_input_chunk_down(ic); |
82 | 0 | flb_input_chunk_set_up(ic); |
83 | 0 | flb_input_chunk_get_name(ic); |
84 | 0 | char *tag_buf; |
85 | 0 | int tag_len; |
86 | 0 | flb_input_chunk_get_tag(ic, &tag_buf, &tag_len); |
87 | 0 | size_t flushed; |
88 | 0 | flb_input_chunk_flush(ic, &flushed); |
89 | 0 | } |
90 | 0 | } |
91 | 72 | } |
92 | 11 | } |
93 | 11 | break; |
94 | 13 | case 6: |
95 | 13 | flb_input_check(ctx->config); |
96 | 13 | flb_input_pause_all(ctx->config); |
97 | 13 | break; |
98 | 32 | case 7: { |
99 | 32 | struct mk_list *head; |
100 | 32 | struct flb_output_instance *entry; |
101 | 1.09k | mk_list_foreach(head, &ctx->config->outputs) { |
102 | 1.09k | entry = mk_list_entry(head, struct flb_output_instance, _head); |
103 | 1.09k | flb_output_net_default(nm4, random_i1, entry); |
104 | 1.09k | flb_output_name(entry); |
105 | 1.09k | } |
106 | 32 | } |
107 | 32 | break; |
108 | 6 | default: |
109 | 6 | flb_lib_push(ctx, in_ffd, null_terminated, NM_SIZE); |
110 | 6 | break; |
111 | 629 | } |
112 | | |
113 | 629 | flb_free(null_terminated); |
114 | 629 | flb_free(nm3); |
115 | 629 | flb_free(nm4); |
116 | 629 | return 0; |
117 | 629 | } |
118 | | |
119 | | int callback_test(void* data, size_t size, void* cb_data) |
120 | 0 | { |
121 | 0 | return 0; |
122 | 0 | } |
123 | | |
124 | | struct flb_lib_out_cb cb; |
125 | | |
126 | | |
127 | 2 | int LLVMFuzzerInitialize(int *argc, char ***argv) { |
128 | | /* Set fuzzer-malloc chance of failure */ |
129 | 2 | flb_malloc_p = 0; |
130 | 2 | flb_malloc_mod = 25000; |
131 | | |
132 | 2 | ctx = flb_create(); |
133 | 2 | flb_service_set(ctx, "Flush", "0", "Grace", |
134 | 2 | "0", "Log_Level", "debug", NULL); |
135 | | |
136 | 2 | in_ffd = flb_input(ctx, (char *) "lib", NULL); |
137 | 2 | flb_input_set(ctx, in_ffd, (char *) "test", NULL); |
138 | 2 | flb_input_set(ctx, in_ffd, (char *) "BBBB", NULL); |
139 | 2 | flb_input_set(ctx, in_ffd, (char *) "AAAA", NULL); |
140 | 2 | flb_input_set(ctx, in_ffd, (char *) "AAAAA", NULL); |
141 | 2 | flb_input_set(ctx, in_ffd, (char *) "CC", NULL); |
142 | 2 | flb_input_set(ctx, in_ffd, (char *) "A", NULL); |
143 | | |
144 | 2 | parser = flb_parser_create("timestamp", "regex", "^(?<time>.*)$", FLB_TRUE, |
145 | 2 | "%s.%L", "time", NULL, MK_FALSE, 0, FLB_FALSE, |
146 | 2 | FLB_FALSE, NULL, 0, NULL, ctx->config); |
147 | 2 | filter_ffd = flb_filter(ctx, (char *) "parser", NULL); |
148 | 2 | int ret; |
149 | 2 | ret = flb_filter_set(ctx, filter_ffd, "Match", "test", |
150 | 2 | "Key_Name", "@timestamp", |
151 | 2 | "Parser", "timestamp", |
152 | 2 | "Reserve_Data", "On", |
153 | 2 | NULL); |
154 | | |
155 | 2 | cb.cb = callback_test; |
156 | 2 | cb.data = NULL; |
157 | 2 | out_ffd = flb_output(ctx, (char *) "lib", &cb); |
158 | 2 | flb_output_set(ctx, out_ffd, "Match", "*", |
159 | 2 | "format", "json", NULL); |
160 | | |
161 | 2 | flb_output_set(ctx, out_ffd,"match", "test", NULL); |
162 | 2 | flb_output_set(ctx, out_ffd,"region", "us-west-2", NULL); |
163 | 2 | flb_output_set(ctx, out_ffd,"log_group_name", "fluent", NULL); |
164 | 2 | flb_output_set(ctx, out_ffd,"log_stream_prefix", "from-fluent-", NULL); |
165 | 2 | flb_output_set(ctx, out_ffd,"auto_create_group", "On", NULL); |
166 | 2 | flb_output_set(ctx, out_ffd,"net.keepalive", "Off", NULL); |
167 | 2 | flb_output_set(ctx, out_ffd,"Retry_Limit", "1", NULL); |
168 | | |
169 | | /* start the engine */ |
170 | 2 | flb_start(ctx); |
171 | 2 | } |