enc_dec_fuzzer.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2018 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. ////////////////////////////////////////////////////////////////////////////////
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "./fuzz_utils.h"
  19. #include "src/webp/decode.h"
  20. #include "src/webp/encode.h"
  21. namespace {
  22. const VP8CPUInfo default_VP8GetCPUInfo = VP8GetCPUInfo;
  23. } // namespace
  24. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) {
  25. uint32_t bit_pos = 0;
  26. ExtractAndDisableOptimizations(default_VP8GetCPUInfo, data, size, &bit_pos);
  27. // Init the source picture.
  28. WebPPicture pic;
  29. if (!WebPPictureInit(&pic)) {
  30. fprintf(stderr, "WebPPictureInit failed.\n");
  31. abort();
  32. }
  33. pic.use_argb = Extract(1, data, size, &bit_pos);
  34. // Read the source picture.
  35. if (!ExtractSourcePicture(&pic, data, size, &bit_pos)) {
  36. const WebPEncodingError error_code = pic.error_code;
  37. WebPPictureFree(&pic);
  38. if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return 0;
  39. fprintf(stderr, "Can't read input image. Error code: %d\n", error_code);
  40. abort();
  41. }
  42. // Crop and scale.
  43. if (!ExtractAndCropOrScale(&pic, data, size, &bit_pos)) {
  44. const WebPEncodingError error_code = pic.error_code;
  45. WebPPictureFree(&pic);
  46. if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return 0;
  47. fprintf(stderr, "ExtractAndCropOrScale failed. Error code: %d\n",
  48. error_code);
  49. abort();
  50. }
  51. // Extract a configuration from the packed bits.
  52. WebPConfig config;
  53. if (!ExtractWebPConfig(&config, data, size, &bit_pos)) {
  54. fprintf(stderr, "ExtractWebPConfig failed.\n");
  55. abort();
  56. }
  57. // Skip slow settings on big images, it's likely to timeout.
  58. if (pic.width * pic.height > 32 * 32) {
  59. if (config.lossless) {
  60. if (config.quality > 99.0f && config.method >= 5) {
  61. config.quality = 99.0f;
  62. config.method = 5;
  63. }
  64. } else {
  65. if (config.quality > 99.0f && config.method == 6) {
  66. config.quality = 99.0f;
  67. }
  68. }
  69. if (config.alpha_quality == 100 && config.method == 6) {
  70. config.alpha_quality = 99;
  71. }
  72. }
  73. // Encode.
  74. WebPMemoryWriter memory_writer;
  75. WebPMemoryWriterInit(&memory_writer);
  76. pic.writer = WebPMemoryWrite;
  77. pic.custom_ptr = &memory_writer;
  78. if (!WebPEncode(&config, &pic)) {
  79. const WebPEncodingError error_code = pic.error_code;
  80. WebPMemoryWriterClear(&memory_writer);
  81. WebPPictureFree(&pic);
  82. if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY ||
  83. error_code == VP8_ENC_ERROR_BAD_WRITE) {
  84. return 0;
  85. }
  86. fprintf(stderr, "WebPEncode failed. Error code: %d\n", error_code);
  87. abort();
  88. }
  89. // Try decoding the result.
  90. const uint8_t* const out_data = memory_writer.mem;
  91. const size_t out_size = memory_writer.size;
  92. WebPDecoderConfig dec_config;
  93. if (!WebPInitDecoderConfig(&dec_config)) {
  94. fprintf(stderr, "WebPInitDecoderConfig failed.\n");
  95. WebPMemoryWriterClear(&memory_writer);
  96. WebPPictureFree(&pic);
  97. abort();
  98. }
  99. dec_config.output.colorspace = MODE_BGRA;
  100. const VP8StatusCode status = WebPDecode(out_data, out_size, &dec_config);
  101. if ((status != VP8_STATUS_OK && status != VP8_STATUS_OUT_OF_MEMORY &&
  102. status != VP8_STATUS_USER_ABORT) ||
  103. (status == VP8_STATUS_OK && (dec_config.output.width != pic.width ||
  104. dec_config.output.height != pic.height))) {
  105. fprintf(stderr, "WebPDecode failed. status: %d.\n", status);
  106. WebPFreeDecBuffer(&dec_config.output);
  107. WebPMemoryWriterClear(&memory_writer);
  108. WebPPictureFree(&pic);
  109. abort();
  110. }
  111. if (status == VP8_STATUS_OK) {
  112. const uint8_t* const rgba = dec_config.output.u.RGBA.rgba;
  113. const int w = dec_config.output.width;
  114. const int h = dec_config.output.height;
  115. // Compare the results if exact encoding.
  116. if (pic.use_argb && config.lossless && config.near_lossless == 100) {
  117. const uint32_t* src1 = (const uint32_t*)rgba;
  118. const uint32_t* src2 = pic.argb;
  119. for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) {
  120. for (int x = 0; x < w; ++x) {
  121. uint32_t v1 = src1[x], v2 = src2[x];
  122. if (!config.exact) {
  123. if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) {
  124. // Only keep alpha for comparison of fully transparent area.
  125. v1 &= 0xff000000u;
  126. v2 &= 0xff000000u;
  127. }
  128. }
  129. if (v1 != v2) {
  130. fprintf(stderr, "Lossless compression failed pixel-exactness.\n");
  131. WebPFreeDecBuffer(&dec_config.output);
  132. WebPMemoryWriterClear(&memory_writer);
  133. WebPPictureFree(&pic);
  134. abort();
  135. }
  136. }
  137. }
  138. }
  139. }
  140. WebPFreeDecBuffer(&dec_config.output);
  141. WebPMemoryWriterClear(&memory_writer);
  142. WebPPictureFree(&pic);
  143. return 0;
  144. }