demangler-fuzzer.c 2.6 KB

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