| 1 |
/* Demangler fuzzer. |
| 2 |
|
| 3 |
Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of GNU libiberty. |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <unistd.h> |
| 23 |
#include <time.h> |
| 24 |
#include "demangle.h" |
| 25 |
|
| 26 |
#define MAXLEN 253 |
| 27 |
#define ALPMIN 33 |
| 28 |
#define ALPMAX 127 |
| 29 |
|
| 30 |
static char *program_name; |
| 31 |
|
| 32 |
#define DEFAULT_MAXCOUNT 7500000 |
| 33 |
|
| 34 |
static void |
| 35 |
print_usage (FILE *fp, int exit_value) |
| 36 |
{ |
| 37 |
fprintf (fp, "Usage: %s [OPTION]...\n", program_name); |
| 38 |
fprintf (fp, "Options:\n"); |
| 39 |
fprintf (fp, " -h Display this message.\n"); |
| 40 |
fprintf (fp, " -s SEED Select the random seed to be used.\n"); |
| 41 |
fprintf (fp, " The default is to base one on the"); |
| 42 |
fprintf (fp, " current time.\n"); |
| 43 |
fprintf (fp, " -m MAXCOUNT Exit after MAXCOUNT symbols.\n"); |
| 44 |
fprintf (fp, " The default is %d.", DEFAULT_MAXCOUNT); |
| 45 |
fprintf (fp, " Set to `-1' for no limit.\n"); |
| 46 |
|
| 47 |
exit (exit_value); |
| 48 |
} |
| 49 |
|
| 50 |
int |
| 51 |
main (int argc, char *argv[]) |
| 52 |
{ |
| 53 |
char symbol[2 + MAXLEN + 1] = "_Z"; |
| 54 |
int seed = -1, seed_set = 0; |
| 55 |
int count = 0, maxcount = DEFAULT_MAXCOUNT; |
| 56 |
int optchr; |
| 57 |
|
| 58 |
program_name = argv[0]; |
| 59 |
|
| 60 |
do |
| 61 |
{ |
| 62 |
optchr = getopt (argc, argv, "hs:m:t:"); |
| 63 |
switch (optchr) |
| 64 |
{ |
| 65 |
case '?': /* Unrecognized option. */ |
| 66 |
print_usage (stderr, 1); |
| 67 |
break; |
| 68 |
|
| 69 |
case 'h': |
| 70 |
print_usage (stdout, 0); |
| 71 |
break; |
| 72 |
|
| 73 |
case 's': |
| 74 |
seed = atoi (optarg); |
| 75 |
seed_set = 1; |
| 76 |
break; |
| 77 |
|
| 78 |
case 'm': |
| 79 |
maxcount = atoi (optarg); |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
while (optchr != -1); |
| 84 |
|
| 85 |
if (!seed_set) |
| 86 |
seed = time (NULL); |
| 87 |
srand (seed); |
| 88 |
printf ("%s: seed = %d\n", program_name, seed); |
| 89 |
|
| 90 |
while (maxcount < 0 || count < maxcount) |
| 91 |
{ |
| 92 |
char *buffer = symbol + 2; |
| 93 |
int length, i; |
| 94 |
|
| 95 |
length = rand () % MAXLEN; |
| 96 |
for (i = 0; i < length; i++) |
| 97 |
*buffer++ = (rand () % (ALPMAX - ALPMIN)) + ALPMIN; |
| 98 |
|
| 99 |
*buffer++ = '\0'; |
| 100 |
|
| 101 |
cplus_demangle (symbol, DMGL_AUTO | DMGL_ANSI | DMGL_PARAMS); |
| 102 |
|
| 103 |
count++; |
| 104 |
} |
| 105 |
|
| 106 |
printf ("%s: successfully demangled %d symbols\n", program_name, count); |
| 107 |
exit (0); |
| 108 |
} |