libFuzzerLocal.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (C) 2017 Tobias Brunner
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <library.h>
  19. extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size);
  20. __attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv);
  21. /**
  22. * This is a simple driver for the fuzz targets to verify test inputs outside
  23. * of OSS-Fuzz.
  24. *
  25. * Failures will usually cause crashes.
  26. */
  27. int main(int argc, char **argv)
  28. {
  29. chunk_t *data;
  30. int i, res = 0;
  31. fprintf(stderr, "%s: running %d inputs\n", argv[0], argc - 1);
  32. if (LLVMFuzzerInitialize)
  33. {
  34. LLVMFuzzerInitialize(&argc, &argv);
  35. }
  36. for (i = 1; i < argc; i++)
  37. {
  38. fprintf(stderr, "running: %s\n", argv[i]);
  39. data = chunk_map(argv[i], FALSE);
  40. if (!data)
  41. {
  42. fprintf(stderr, "opening %s failed: %s\n", argv[i], strerror(errno));
  43. return 1;
  44. }
  45. res = LLVMFuzzerTestOneInput(data->ptr, data->len);
  46. fprintf(stderr, "done: %s: (%zd bytes)\n", argv[i], data->len);
  47. chunk_unmap(data);
  48. if (res)
  49. {
  50. break;
  51. }
  52. }
  53. fprintf(stderr, "%s: completed %d inputs\n", argv[0], i-1);
  54. return res;
  55. }