/src/opensips/test/fuzz/fuzz_standalone.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023 Sippy Software, Inc., http://www.sippysoft.com |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
15 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
18 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 | | * SUCH DAMAGE. |
25 | | * |
26 | | */ |
27 | | |
28 | | #pragma once |
29 | | |
30 | | #include <assert.h> |
31 | | #if defined(FUZZ_STANDALONE) |
32 | | #include <fcntl.h> |
33 | | #include <string.h> |
34 | | #include <unistd.h> |
35 | | #endif /* FUZZ_STANDALONE */ |
36 | | |
37 | | #include "../../context.h" |
38 | | #include "../../core_stats.h" |
39 | | #include "../../dset.h" |
40 | | |
41 | | /* Dummy */ |
42 | | const struct scm_version core_scm_ver; |
43 | | |
44 | | #if defined(__linux__) |
45 | | static int optreset; /* Not present in linux */ |
46 | | #endif |
47 | | |
48 | | static struct opt_save { |
49 | | char *optarg; |
50 | | int optind; |
51 | | int optopt; |
52 | | int opterr; |
53 | | int optreset; |
54 | | } opt_save; |
55 | | |
56 | | #define OPT_SAVE() (opt_save = (struct opt_save){optarg, optind, optopt, opterr, optreset}) |
57 | | #define OPT_RESTORE() ({ \ |
58 | | optarg = opt_save.optarg; \ |
59 | | optind = opt_save.optind; \ |
60 | | optopt = opt_save.optopt; \ |
61 | | opterr = opt_save.opterr; \ |
62 | | optreset = opt_save.optreset; \ |
63 | | }) |
64 | | |
65 | | __attribute__((constructor (101))) void |
66 | | opensips_fuzz_init() |
67 | 8 | { |
68 | 8 | static stat_val bad_URIs_c; |
69 | 8 | static stat_val bad_msg_hdr_c; |
70 | 8 | static stat_var _bad_URIs = {.u.val = &bad_URIs_c}; |
71 | 8 | static stat_var _bad_msg_hdr = {.u.val = &bad_msg_hdr_c}; |
72 | | |
73 | 8 | bad_URIs = &_bad_URIs; |
74 | 8 | bad_msg_hdr = &_bad_msg_hdr; |
75 | | |
76 | 8 | assert(init_dset() == 0); |
77 | 8 | assert(ensure_global_context() == 0); |
78 | 8 | *log_level = L_CRIT; |
79 | 8 | } |
80 | | |
81 | | #if defined(FUZZ_STANDALONE) |
82 | | int LLVMFuzzerTestOneInput(const char *data, size_t size); |
83 | | |
84 | | int |
85 | | main(int argc, char *argv[]) |
86 | | { |
87 | | int fflag, ch, fd; |
88 | | char buf[1024], *cp; |
89 | | size_t size; |
90 | | |
91 | | fflag = 0; |
92 | | OPT_SAVE(); |
93 | | while ((ch = getopt(argc, argv, "f")) != -1) { |
94 | | switch (ch) { |
95 | | case 'f': |
96 | | fflag = 1; |
97 | | break; |
98 | | default: |
99 | | return (-1); |
100 | | } |
101 | | } |
102 | | argc -= optind; |
103 | | argv += optind; |
104 | | OPT_RESTORE(); |
105 | | |
106 | | assert(argc == 1); |
107 | | if (fflag) { |
108 | | fd = open(argv[0], O_RDONLY, 0); |
109 | | assert(fd >= 0); |
110 | | size = read(fd, buf, sizeof(buf)); |
111 | | assert(size > 0); |
112 | | close(fd); |
113 | | cp = buf; |
114 | | } else { |
115 | | cp = argv[0]; |
116 | | size = strlen(cp); |
117 | | } |
118 | | LLVMFuzzerTestOneInput(cp, size); |
119 | | return (0); |
120 | | } |
121 | | #endif /* FUZZ_STANDALONE */ |