Coverage Report

Created: 2025-08-26 06:21

/src/libyaml_dumper_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2020 Google LLC
2
//
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
#include "yaml.h"
16
#include "yaml_write_handler.h"
17
#include <assert.h>
18
#include <stdbool.h>
19
#include <stdint.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
24
#ifdef NDEBUG
25
#undef NDEBUG
26
#endif
27
28
10.3k
#define MAX_DOCUMENTS 16
29
30
bool nodes_equal(yaml_document_t *document1, int index1,
31
10.7M
                 yaml_document_t *document2, int index2, int level) {
32
10.7M
  const bool equal = true;
33
34
10.7M
  if (level++ > 1000)
35
79
    return !equal;
36
10.7M
  yaml_node_t *node1 = yaml_document_get_node(document1, index1);
37
38
10.7M
  if (!node1)
39
0
    return !equal;
40
41
10.7M
  yaml_node_t *node2 = yaml_document_get_node(document2, index2);
42
43
10.7M
  if (!node2)
44
0
    return !equal;
45
46
10.7M
  if (node1->type != node2->type)
47
0
    return !equal;
48
49
10.7M
  if (strcmp((char *)node1->tag, (char *)node2->tag) != 0)
50
0
    return !equal;
51
52
10.7M
  switch (node1->type) {
53
7.50M
  case YAML_SCALAR_NODE:
54
7.50M
    if (node1->data.scalar.length != node2->data.scalar.length)
55
14
      return !equal;
56
7.50M
    if (strncmp((char *)node1->data.scalar.value,
57
7.50M
                (char *)node2->data.scalar.value,
58
7.50M
                node1->data.scalar.length) != 0)
59
20
      return !equal;
60
7.50M
    break;
61
7.50M
  case YAML_SEQUENCE_NODE:
62
576k
    if ((node1->data.sequence.items.top - node1->data.sequence.items.start) !=
63
576k
        (node2->data.sequence.items.top - node2->data.sequence.items.start))
64
0
      return !equal;
65
1.55M
    for (int k = 0; k < (node1->data.sequence.items.top -
66
1.55M
                         node1->data.sequence.items.start);
67
988k
         k++) {
68
988k
      if (!nodes_equal(document1, node1->data.sequence.items.start[k],
69
988k
                       document2, node2->data.sequence.items.start[k], level))
70
14.8k
        return !equal;
71
988k
    }
72
561k
    break;
73
2.68M
  case YAML_MAPPING_NODE:
74
2.68M
    if ((node1->data.mapping.pairs.top - node1->data.mapping.pairs.start) !=
75
2.68M
        (node2->data.mapping.pairs.top - node2->data.mapping.pairs.start))
76
0
      return !equal;
77
2.68M
    for (int k = 0;
78
7.53M
         k < (node1->data.mapping.pairs.top - node1->data.mapping.pairs.start);
79
4.91M
         k++) {
80
4.91M
      if (!nodes_equal(document1, node1->data.mapping.pairs.start[k].key,
81
4.91M
                       document2, node2->data.mapping.pairs.start[k].key,
82
4.91M
                       level))
83
56.1k
        return !equal;
84
4.85M
      if (!nodes_equal(document1, node1->data.mapping.pairs.start[k].value,
85
4.85M
                       document2, node2->data.mapping.pairs.start[k].value,
86
4.85M
                       level))
87
8.73k
        return !equal;
88
4.85M
    }
89
2.61M
    break;
90
2.61M
  default:
91
0
    return !equal;
92
10.7M
  }
93
10.6M
  return equal;
94
10.7M
}
95
96
4.34k
bool documents_equal(yaml_document_t *document1, yaml_document_t *document2) {
97
98
4.34k
  const bool equal = true;
99
100
4.34k
  if ((document1->version_directive && !document2->version_directive) ||
101
4.34k
      (!document1->version_directive && document2->version_directive) ||
102
4.34k
      (document1->version_directive && document2->version_directive &&
103
4.34k
       (document1->version_directive->major !=
104
119
            document2->version_directive->major ||
105
119
        document1->version_directive->minor !=
106
119
            document2->version_directive->minor)))
107
0
    return !equal;
108
109
4.34k
  if ((document1->tag_directives.end - document1->tag_directives.start) !=
110
4.34k
      (document2->tag_directives.end - document2->tag_directives.start))
111
0
    return !equal;
112
4.34k
  for (int k = 0;
113
6.29k
       k < (document1->tag_directives.end - document1->tag_directives.start);
114
4.34k
       k++) {
115
1.95k
    if ((strcmp((char *)document1->tag_directives.start[k].handle,
116
1.95k
                (char *)document2->tag_directives.start[k].handle) != 0) ||
117
1.95k
        (strcmp((char *)document1->tag_directives.start[k].prefix,
118
1.95k
                (char *)document2->tag_directives.start[k].prefix) != 0))
119
0
      return !equal;
120
1.95k
  }
121
122
4.34k
  if ((document1->nodes.top - document1->nodes.start) !=
123
4.34k
      (document2->nodes.top - document2->nodes.start))
124
2
    return !equal;
125
126
4.34k
  if (document1->nodes.top != document1->nodes.start) {
127
4.34k
    if (!nodes_equal(document1, 1, document2, 1, 0))
128
113
      return !equal;
129
4.34k
  }
130
131
4.22k
  return equal;
132
4.34k
}
133
134
bool copy_document(yaml_document_t *document_to,
135
10.3k
                   yaml_document_t *document_from) {
136
10.3k
  bool error = true;
137
138
10.3k
  yaml_node_t *node;
139
10.3k
  yaml_node_item_t *item;
140
10.3k
  yaml_node_pair_t *pair;
141
142
10.3k
  if (!yaml_document_initialize(document_to, document_from->version_directive,
143
10.3k
                                document_from->tag_directives.start,
144
10.3k
                                document_from->tag_directives.end,
145
10.3k
                                document_from->start_implicit,
146
10.3k
                                document_from->end_implicit))
147
8
    return !error;
148
149
10.1M
  for (node = document_from->nodes.start; node < document_from->nodes.top;
150
10.1M
       node++) {
151
10.1M
    switch (node->type) {
152
7.58M
    case YAML_SCALAR_NODE:
153
7.58M
      if (!yaml_document_add_scalar(
154
7.58M
              document_to, node->tag, node->data.scalar.value,
155
7.58M
              node->data.scalar.length, node->data.scalar.style))
156
21
        goto out;
157
7.58M
      break;
158
7.58M
    case YAML_SEQUENCE_NODE:
159
115k
      if (!yaml_document_add_sequence(document_to, node->tag,
160
115k
                                      node->data.sequence.style))
161
1
        goto out;
162
115k
      break;
163
2.45M
    case YAML_MAPPING_NODE:
164
2.45M
      if (!yaml_document_add_mapping(document_to, node->tag,
165
2.45M
                                     node->data.mapping.style))
166
1
        goto out;
167
2.45M
      break;
168
2.45M
    default:
169
0
      goto out;
170
10.1M
    }
171
10.1M
  }
172
173
10.1M
  for (node = document_from->nodes.start; node < document_from->nodes.top;
174
10.1M
       node++) {
175
10.1M
    switch (node->type) {
176
115k
    case YAML_SEQUENCE_NODE:
177
115k
      for (item = node->data.sequence.items.start;
178
1.38M
           item < node->data.sequence.items.top; item++) {
179
1.26M
        if (!yaml_document_append_sequence_item(
180
1.26M
                document_to, node - document_from->nodes.start + 1, *item))
181
0
          goto out;
182
1.26M
      }
183
115k
      break;
184
2.45M
    case YAML_MAPPING_NODE:
185
2.45M
      for (pair = node->data.mapping.pairs.start;
186
7.23M
           pair < node->data.mapping.pairs.top; pair++) {
187
4.78M
        if (!yaml_document_append_mapping_pair(
188
4.78M
                document_to, node - document_from->nodes.start + 1, pair->key,
189
4.78M
                pair->value))
190
0
          goto out;
191
4.78M
      }
192
2.45M
      break;
193
7.58M
    default:
194
7.58M
      break;
195
10.1M
    }
196
10.1M
  }
197
10.3k
  return error;
198
199
23
out:
200
23
  yaml_document_delete(document_to);
201
23
  return !error;
202
10.3k
}
203
204
11.0k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
205
11.0k
  if (size < 2)
206
1
    return 0;
207
208
11.0k
  yaml_parser_t parser;
209
11.0k
  yaml_emitter_t emitter;
210
211
11.0k
  yaml_document_t document;
212
11.0k
  yaml_document_t documents[MAX_DOCUMENTS];
213
11.0k
  size_t document_number = 0;
214
11.0k
  int count = 0;
215
11.0k
  bool done = false;
216
11.0k
  bool equal = false;
217
11.0k
  bool is_canonical = data[0] & 1;
218
11.0k
  bool is_unicode = data[1] & 1;
219
11.0k
  data += 2;
220
11.0k
  size -= 2;
221
222
11.0k
  if (!yaml_parser_initialize(&parser))
223
0
    return 0;
224
225
11.0k
  yaml_parser_set_input_string(&parser, data, size);
226
11.0k
  if (!yaml_emitter_initialize(&emitter))
227
0
    return 0;
228
229
11.0k
  yaml_emitter_set_canonical(&emitter, is_canonical);
230
11.0k
  yaml_emitter_set_unicode(&emitter, is_unicode);
231
232
11.0k
  yaml_output_buffer_t out = {/*buf=*/NULL, /*size=*/0, /*capacity=*/1000};
233
11.0k
  yaml_emitter_set_output(&emitter, yaml_write_handler, &out);
234
11.0k
  yaml_emitter_open(&emitter);
235
236
18.9k
  while (!done) {
237
16.5k
    if (!yaml_parser_load(&parser, &document)) {
238
3.83k
      equal = 1;
239
3.83k
      break;
240
3.83k
    }
241
242
12.7k
    done = (!yaml_document_get_root_node(&document));
243
12.7k
    if (!done) {
244
10.3k
      if (document_number >= MAX_DOCUMENTS) {
245
17
        yaml_document_delete(&document);
246
17
        equal = true;
247
17
        break;
248
17
      }
249
250
10.3k
      if (!copy_document(&documents[document_number++], &document)) {
251
31
        yaml_document_delete(&document);
252
31
        equal = true;
253
31
        break;
254
31
      }
255
10.3k
      if (!(yaml_emitter_dump(&emitter, &document) ||
256
10.3k
            (yaml_emitter_flush(&emitter) && 0))) {
257
4.81k
        equal = true;
258
4.81k
        break;
259
4.81k
      }
260
261
5.52k
      count++;
262
5.52k
    } else {
263
2.34k
      yaml_document_delete(&document);
264
2.34k
    }
265
12.7k
  }
266
267
11.0k
  yaml_parser_delete(&parser);
268
11.0k
  yaml_emitter_close(&emitter);
269
11.0k
  yaml_emitter_delete(&emitter);
270
271
11.0k
  if (!equal) {
272
2.34k
    count = 0;
273
2.34k
    done = false;
274
2.34k
    if (!yaml_parser_initialize(&parser))
275
0
      goto error;
276
277
2.34k
    if (!out.buf) {
278
172
      yaml_parser_delete(&parser);
279
172
      goto error;
280
172
    }
281
282
2.17k
    yaml_parser_set_input_string(&parser, out.buf, out.size);
283
284
8.43k
    while (!done) {
285
6.40k
      if (!yaml_parser_load(&parser, &document)) {
286
22
        yaml_parser_delete(&parser);
287
22
        goto error;
288
22
      }
289
290
6.37k
      done = (!yaml_document_get_root_node(&document));
291
6.37k
      if (!done) {
292
4.34k
        if (!documents_equal(documents + count, &document)) {
293
115
          yaml_parser_delete(&parser);
294
115
          yaml_document_delete(&document);
295
115
          goto error;
296
115
        }
297
4.22k
        count++;
298
4.22k
      }
299
6.26k
      yaml_document_delete(&document);
300
6.26k
    }
301
2.03k
    yaml_parser_delete(&parser);
302
2.03k
  }
303
304
305
11.0k
error:
306
307
21.4k
  for (int k = 0; k < document_number; k++) {
308
10.3k
    yaml_document_delete(documents + k);
309
10.3k
  }
310
11.0k
  free(out.buf);
311
11.0k
  return 0;
312
11.0k
}