1*f1c4c3daSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*f1c4c3daSCy Schubert /* tests/fuzzing/fuzz_profile.c */
3*f1c4c3daSCy Schubert /*
4*f1c4c3daSCy Schubert * Copyright (C) 2024 by Arjun. All rights reserved.
5*f1c4c3daSCy Schubert *
6*f1c4c3daSCy Schubert * Redistribution and use in source and binary forms, with or without
7*f1c4c3daSCy Schubert * modification, are permitted provided that the following conditions
8*f1c4c3daSCy Schubert * are met:
9*f1c4c3daSCy Schubert *
10*f1c4c3daSCy Schubert * * Redistributions of source code must retain the above copyright
11*f1c4c3daSCy Schubert * notice, this list of conditions and the following disclaimer.
12*f1c4c3daSCy Schubert *
13*f1c4c3daSCy Schubert * * Redistributions in binary form must reproduce the above copyright
14*f1c4c3daSCy Schubert * notice, this list of conditions and the following disclaimer in
15*f1c4c3daSCy Schubert * the documentation and/or other materials provided with the
16*f1c4c3daSCy Schubert * distribution.
17*f1c4c3daSCy Schubert *
18*f1c4c3daSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*f1c4c3daSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*f1c4c3daSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*f1c4c3daSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22*f1c4c3daSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23*f1c4c3daSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*f1c4c3daSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25*f1c4c3daSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*f1c4c3daSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27*f1c4c3daSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*f1c4c3daSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29*f1c4c3daSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE.
30*f1c4c3daSCy Schubert */
31*f1c4c3daSCy Schubert
32*f1c4c3daSCy Schubert /*
33*f1c4c3daSCy Schubert * Fuzzing harness implementation for profile_parse_file.
34*f1c4c3daSCy Schubert */
35*f1c4c3daSCy Schubert
36*f1c4c3daSCy Schubert #include "autoconf.h"
37*f1c4c3daSCy Schubert #include <prof_int.h>
38*f1c4c3daSCy Schubert
39*f1c4c3daSCy Schubert void dump_profile(struct profile_node *root, int level);
40*f1c4c3daSCy Schubert
41*f1c4c3daSCy Schubert #define kMinInputLength 2
42*f1c4c3daSCy Schubert #define kMaxInputLength 1024
43*f1c4c3daSCy Schubert
44*f1c4c3daSCy Schubert extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
45*f1c4c3daSCy Schubert
46*f1c4c3daSCy Schubert int
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)47*f1c4c3daSCy Schubert LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48*f1c4c3daSCy Schubert {
49*f1c4c3daSCy Schubert errcode_t ret;
50*f1c4c3daSCy Schubert FILE *fp_w, *fp_r;
51*f1c4c3daSCy Schubert char file_name[256], *output;
52*f1c4c3daSCy Schubert struct profile_node *root;
53*f1c4c3daSCy Schubert
54*f1c4c3daSCy Schubert if (size < kMinInputLength || size > kMaxInputLength)
55*f1c4c3daSCy Schubert return 0;
56*f1c4c3daSCy Schubert
57*f1c4c3daSCy Schubert snprintf(file_name, sizeof(file_name), "/tmp/libfuzzer.%d", getpid());
58*f1c4c3daSCy Schubert
59*f1c4c3daSCy Schubert /* Write data into the file. */
60*f1c4c3daSCy Schubert fp_w = fopen(file_name, "w");
61*f1c4c3daSCy Schubert if (!fp_w)
62*f1c4c3daSCy Schubert return 1;
63*f1c4c3daSCy Schubert fwrite(data, 1, size, fp_w);
64*f1c4c3daSCy Schubert fclose(fp_w);
65*f1c4c3daSCy Schubert
66*f1c4c3daSCy Schubert /* Provide the file pointer to the parser. */
67*f1c4c3daSCy Schubert fp_r = fopen(file_name, "r");
68*f1c4c3daSCy Schubert if (!fp_r)
69*f1c4c3daSCy Schubert return 1;
70*f1c4c3daSCy Schubert
71*f1c4c3daSCy Schubert initialize_prof_error_table();
72*f1c4c3daSCy Schubert
73*f1c4c3daSCy Schubert ret = profile_parse_file(fp_r, &root, NULL);
74*f1c4c3daSCy Schubert if (!ret) {
75*f1c4c3daSCy Schubert ret = profile_write_tree_to_buffer(root, &output);
76*f1c4c3daSCy Schubert if (!ret)
77*f1c4c3daSCy Schubert free(output);
78*f1c4c3daSCy Schubert
79*f1c4c3daSCy Schubert profile_verify_node(root);
80*f1c4c3daSCy Schubert profile_free_node(root);
81*f1c4c3daSCy Schubert }
82*f1c4c3daSCy Schubert
83*f1c4c3daSCy Schubert fclose(fp_r);
84*f1c4c3daSCy Schubert unlink(file_name);
85*f1c4c3daSCy Schubert
86*f1c4c3daSCy Schubert return 0;
87*f1c4c3daSCy Schubert }
88