FuzzParsePath.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "fuzz/Fuzz.h"
  8. #include "include/core/SkString.h"
  9. #include "include/utils/SkParsePath.h"
  10. #include <stdlib.h>
  11. // Most of this is taken from random_parse_path.cpp and adapted to use the Fuzz
  12. // instead of SKRandom
  13. static const struct Legal {
  14. char fSymbol;
  15. int fScalars;
  16. } gLegal[] = {
  17. { 'M', 2 },
  18. { 'H', 1 },
  19. { 'V', 1 },
  20. { 'L', 2 },
  21. { 'Q', 4 },
  22. { 'T', 2 },
  23. { 'C', 6 },
  24. { 'S', 4 },
  25. { 'A', 4 },
  26. { 'Z', 0 },
  27. };
  28. static bool gEasy = false; // set to true while debugging to suppress unusual whitespace
  29. // mostly do nothing, then bias towards spaces
  30. static const char gWhiteSpace[] = { 0, 0, 0, 0, 0, 0, 0, 0, ' ', ' ', ' ', ' ', 0x09, 0x0D, 0x0A };
  31. static void add_white(Fuzz* fuzz, SkString* atom) {
  32. if (gEasy) {
  33. atom->append(" ");
  34. return;
  35. }
  36. // Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint"
  37. // smaller, which leads to more efficient fuzzing.
  38. uint8_t reps;
  39. fuzz->nextRange(&reps, 0, 2);
  40. for (uint8_t rep = 0; rep < reps; ++rep) {
  41. uint8_t index;
  42. fuzz->nextRange(&index, 0, (int) SK_ARRAY_COUNT(gWhiteSpace) - 1);
  43. if (gWhiteSpace[index]) {
  44. atom->append(&gWhiteSpace[index], 1);
  45. }
  46. }
  47. }
  48. static void add_some_white(Fuzz* fuzz, SkString* atom) {
  49. for(int i = 0; i < 10; i++) {
  50. add_white(fuzz, atom);
  51. }
  52. }
  53. static void add_comma(Fuzz* fuzz, SkString* atom) {
  54. if (gEasy) {
  55. atom->append(",");
  56. return;
  57. }
  58. add_white(fuzz, atom);
  59. bool b;
  60. fuzz->next(&b);
  61. if (b) {
  62. atom->append(",");
  63. }
  64. add_some_white(fuzz, atom);
  65. }
  66. SkString MakeRandomParsePathPiece(Fuzz* fuzz) {
  67. SkString atom;
  68. uint8_t index;
  69. fuzz->nextRange(&index, 0, (int) SK_ARRAY_COUNT(gLegal) - 1);
  70. const Legal& legal = gLegal[index];
  71. gEasy ? atom.append("\n") : add_white(fuzz, &atom);
  72. bool b;
  73. fuzz->next(&b);
  74. char symbol = legal.fSymbol | (b ? 0x20 : 0);
  75. atom.append(&symbol, 1);
  76. uint8_t reps;
  77. fuzz->nextRange(&reps, 1, 3);
  78. for (int rep = 0; rep < reps; ++rep) {
  79. for (int index = 0; index < legal.fScalars; ++index) {
  80. SkScalar coord;
  81. fuzz->nextRange(&coord, 0.0f, 100.0f);
  82. add_white(fuzz, &atom);
  83. atom.appendScalar(coord);
  84. if (rep < reps - 1 && index < legal.fScalars - 1) {
  85. add_comma(fuzz, &atom);
  86. } else {
  87. add_some_white(fuzz, &atom);
  88. }
  89. if ('A' == legal.fSymbol && 1 == index) {
  90. SkScalar s;
  91. fuzz->nextRange(&s, -720.0f, 720.0f);
  92. atom.appendScalar(s);
  93. add_comma(fuzz, &atom);
  94. fuzz->next(&b);
  95. atom.appendU32(b);
  96. add_comma(fuzz, &atom);
  97. fuzz->next(&b);
  98. atom.appendU32(b);
  99. add_comma(fuzz, &atom);
  100. }
  101. }
  102. }
  103. return atom;
  104. }
  105. DEF_FUZZ(ParsePath, fuzz) {
  106. SkPath path;
  107. SkString spec;
  108. uint8_t count;
  109. fuzz->nextRange(&count, 0, 40);
  110. for (uint8_t i = 0; i < count; ++i) {
  111. spec.append(MakeRandomParsePathPiece(fuzz));
  112. }
  113. SkDebugf("SkParsePath::FromSVGString(%s, &path);\n",spec.c_str());
  114. if (!SkParsePath::FromSVGString(spec.c_str(), &path)){
  115. SkDebugf("Could not decode path\n");
  116. }
  117. }