Debian Codegraph @

tests/fuzzing/synth_fuzzer.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2018 Sascha Brawer
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write see:
16 * <http://www.gnu.org/licenses/>.
17 */
18
19 #include "config.h"
20
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <libgen.h>
25 #include <time.h>
26
27 #include <espeak-ng/espeak_ng.h>
28 #define BOLDRED(x) "\x1b[31m\x1b[1m" x "\x1b[0m"
29
30 static int initialized = 0;
31
32 static int
33 espeak_callback(short *data, int samples, espeak_EVENT *events)
34 {
35 (void)data;
36 (void)samples;
37 (void)events;
38
39 return 0;
40 }
41
42 /* See http://llvm.org/docs/LibFuzzer.html */
43 extern int LLVMFuzzerRunDriver(int *argc, char ***argv,
44 int (*UserCb)(const uint8_t *Data, size_t Size));
45
46 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
47 {
48 int buflength = size+1;
49 if (!initialized)
50 {
51 int options = espeakINITIALIZE_DONT_EXIT;
52 espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, buflength, PATH_ESPEAK_DATA, options);
53 espeak_SetSynthCallback(espeak_callback);
54 const char *lang = getenv("FUZZ_VOICE");
55 if (lang == NULL)
56 {
57 fprintf(stderr, "\n" BOLDRED("[Please set up FUZZ_VOICE env var before starting fuzzer]") "\n\n");
58 exit(1);
59 }
60 if (espeak_SetVoiceByName(lang) != EE_OK)
61 {
62 fprintf(stderr, "\n" BOLDRED("[Please supply a valid voice in FUZZ_VOICE]") "\n\n");
63 exit(1);
64 }
65 initialized = 1;
66 fprintf(stderr, "VOICE FUZZED = %s\n", lang);
67 }
68 char *mutable_data = strndup((char *)data, size);
69 if (!mutable_data)
70 {
71 perror("malloc");
72 exit(1);
73 }
74 unsigned int position = 0, position_type = POS_CHARACTER, end_position = 0 , synth_flags = espeakCHARS_AUTO;
75 espeak_Synth(mutable_data, buflength, position, position_type, end_position,
76 synth_flags, NULL, NULL);
77 free(mutable_data);
78
79 return 0;
80 }
81
82 int main(int argc, char *argv[]) {
83 LLVMFuzzerRunDriver(&argc, &argv, LLVMFuzzerTestOneInput);
84 }