cmd_fuzz.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2022 Google, Inc.
  4. * Written by Andrew Scull <ascull@google.com>
  5. */
  6. #include <command.h>
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <fuzzing_engine.h>
  10. #include <test/fuzz.h>
  11. static struct fuzz_test *find_fuzz_test(const char *name)
  12. {
  13. struct fuzz_test *fuzzer = FUZZ_TEST_START();
  14. size_t count = FUZZ_TEST_COUNT();
  15. size_t i;
  16. for (i = 0; i < count; ++i) {
  17. if (strcmp(name, fuzzer->name) == 0)
  18. return fuzzer;
  19. ++fuzzer;
  20. }
  21. return NULL;
  22. }
  23. static struct udevice *find_fuzzing_engine(void)
  24. {
  25. struct udevice *dev;
  26. if (uclass_first_device_err(UCLASS_FUZZING_ENGINE, &dev))
  27. return NULL;
  28. return dev;
  29. }
  30. static int do_fuzz(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  31. {
  32. struct fuzz_test *fuzzer;
  33. struct udevice *dev;
  34. if (argc != 2)
  35. return CMD_RET_USAGE;
  36. fuzzer = find_fuzz_test(argv[1]);
  37. if (!fuzzer) {
  38. printf("Could not find fuzzer: %s\n", argv[1]);
  39. return 1;
  40. }
  41. dev = find_fuzzing_engine();
  42. if (!dev) {
  43. puts("No fuzzing engine available\n");
  44. return 1;
  45. }
  46. while (1) {
  47. const uint8_t *data;
  48. size_t size;
  49. if (dm_fuzzing_engine_get_input(dev, &data, &size)) {
  50. puts("Fuzzing engine failed\n");
  51. return 1;
  52. }
  53. fuzzer->func(data, size);
  54. }
  55. return 1;
  56. }
  57. #ifdef CONFIG_SYS_LONGHELP
  58. static char fuzz_help_text[] =
  59. "[fuzz-test-name] - execute the named fuzz test\n"
  60. ;
  61. #endif /* CONFIG_SYS_LONGHELP */
  62. U_BOOT_CMD(
  63. fuzz, CONFIG_SYS_MAXARGS, 1, do_fuzz,
  64. "fuzz tests", fuzz_help_text
  65. );