xml_parse_fuzzer.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <assert.h>
  17. #include <stdint.h>
  18. #include "expat.h"
  19. #include "siphash.h"
  20. // Macros to convert preprocessor macros to string literals. See
  21. // https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html
  22. #define xstr(s) str(s)
  23. #define str(s) #s
  24. // The encoder type that we wish to fuzz should come from the compile-time
  25. // definition `ENCODING_FOR_FUZZING`. This allows us to have a separate fuzzer
  26. // binary for
  27. #ifndef ENCODING_FOR_FUZZING
  28. # error "ENCODING_FOR_FUZZING was not provided to this fuzz target."
  29. #endif
  30. // 16-byte deterministic hash key.
  31. static unsigned char hash_key[16] = "FUZZING IS FUN!";
  32. static void XMLCALL
  33. start(void *userData, const XML_Char *name, const XML_Char **atts) {
  34. (void)userData;
  35. (void)name;
  36. (void)atts;
  37. }
  38. static void XMLCALL
  39. end(void *userData, const XML_Char *name) {
  40. (void)userData;
  41. (void)name;
  42. }
  43. int
  44. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  45. XML_Parser p = XML_ParserCreate(xstr(ENCODING_FOR_FUZZING));
  46. assert(p);
  47. // Set the hash salt using siphash to generate a deterministic hash.
  48. struct sipkey *key = sip_keyof(hash_key);
  49. XML_SetHashSalt(p, (unsigned long)siphash24(data, size, key));
  50. XML_SetElementHandler(p, start, end);
  51. XML_Parse(p, (const XML_Char *)data, size, 0);
  52. XML_Parse(p, (const XML_Char *)data, size, 1);
  53. XML_ParserFree(p);
  54. return 0;
  55. }