encoder.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Copyright 2019 Guido Vranken
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining
  4. * a copy of this software and associated documentation files (the
  5. * "Software"), to deal in the Software without restriction, including
  6. * without limitation the rights to use, copy, modify, merge, publish,
  7. * distribute, sublicense, and/or sell copies of the Software, and to
  8. * permit persons to whom the Software is furnished to do so, subject
  9. * to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include <cstddef>
  24. #include <cstdint>
  25. #include <limits>
  26. #include <fuzzing/datasource/datasource.hpp>
  27. #include <fuzzing/memory.hpp>
  28. #include "FLAC++/encoder.h"
  29. #include "common.h"
  30. namespace FLAC {
  31. namespace Encoder {
  32. class FuzzerStream : public Stream {
  33. private:
  34. // fuzzing::datasource::Datasource& ds;
  35. public:
  36. FuzzerStream(fuzzing::datasource::Datasource&) :
  37. Stream() { }
  38. ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, uint32_t /* samples */, uint32_t /* current_frame */) override {
  39. fuzzing::memory::memory_test(buffer, bytes);
  40. #if 0
  41. try {
  42. if ( ds.Get<bool>() == true ) {
  43. return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
  44. }
  45. } catch ( ... ) { }
  46. #endif
  47. return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  48. }
  49. };
  50. }
  51. }
  52. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  53. fuzzing::datasource::Datasource ds(data, size);
  54. FLAC::Encoder::FuzzerStream encoder(ds);
  55. try {
  56. const int channels = ds.Get<uint8_t>();
  57. const int bps = ds.Get<uint8_t>();
  58. encoder.set_channels(channels);
  59. encoder.set_bits_per_sample(bps);
  60. {
  61. const bool res = encoder.set_streamable_subset(ds.Get<bool>());
  62. fuzzing::memory::memory_test(res);
  63. }
  64. {
  65. const bool res = encoder.set_ogg_serial_number(ds.Get<long>());
  66. fuzzing::memory::memory_test(res);
  67. }
  68. {
  69. const bool res = encoder.set_verify(ds.Get<bool>());
  70. fuzzing::memory::memory_test(res);
  71. }
  72. {
  73. const bool res = encoder.set_compression_level(ds.Get<uint8_t>());
  74. fuzzing::memory::memory_test(res);
  75. }
  76. {
  77. const bool res = encoder.set_do_exhaustive_model_search(ds.Get<bool>());
  78. fuzzing::memory::memory_test(res);
  79. }
  80. {
  81. const bool res = encoder.set_do_mid_side_stereo(ds.Get<bool>());
  82. fuzzing::memory::memory_test(res);
  83. }
  84. {
  85. const bool res = encoder.set_loose_mid_side_stereo(ds.Get<bool>());
  86. fuzzing::memory::memory_test(res);
  87. }
  88. {
  89. const auto s = ds.Get<std::string>();
  90. const bool res = encoder.set_apodization(s.data());
  91. fuzzing::memory::memory_test(res);
  92. }
  93. {
  94. const bool res = encoder.set_max_lpc_order(ds.Get<uint8_t>());
  95. fuzzing::memory::memory_test(res);
  96. }
  97. {
  98. const bool res = encoder.set_qlp_coeff_precision(ds.Get<uint32_t>());
  99. fuzzing::memory::memory_test(res);
  100. }
  101. {
  102. const bool res = encoder.set_do_qlp_coeff_prec_search(ds.Get<bool>());
  103. fuzzing::memory::memory_test(res);
  104. }
  105. {
  106. const bool res = encoder.set_do_escape_coding(ds.Get<bool>());
  107. fuzzing::memory::memory_test(res);
  108. }
  109. {
  110. const bool res = encoder.set_min_residual_partition_order(ds.Get<uint32_t>());
  111. fuzzing::memory::memory_test(res);
  112. }
  113. {
  114. const bool res = encoder.set_max_residual_partition_order(ds.Get<uint32_t>());
  115. fuzzing::memory::memory_test(res);
  116. }
  117. {
  118. const bool res = encoder.set_rice_parameter_search_dist(ds.Get<uint32_t>());
  119. fuzzing::memory::memory_test(res);
  120. }
  121. {
  122. const bool res = encoder.set_total_samples_estimate(ds.Get<uint64_t>());
  123. fuzzing::memory::memory_test(res);
  124. }
  125. {
  126. const bool res = encoder.set_blocksize(ds.Get<uint16_t>());
  127. fuzzing::memory::memory_test(res);
  128. }
  129. {
  130. const bool res = encoder.set_limit_min_bitrate(ds.Get<bool>());
  131. fuzzing::memory::memory_test(res);
  132. }
  133. {
  134. const bool res = encoder.set_sample_rate(ds.Get<uint32_t>());
  135. fuzzing::memory::memory_test(res);
  136. }
  137. if ( size > 2 * 65535 * 4 ) {
  138. /* With large inputs and expensive options enabled, the fuzzer can get *really* slow.
  139. * Some combinations can make the fuzzer timeout (>60 seconds). However, while combining
  140. * options makes the fuzzer slower, most options do not expose new code when combined.
  141. * Therefore, combining slow options is disabled for large inputs. Any input containing
  142. * more than 65536 * 2 samples of 32 bits each (max blocksize, stereo) is considered large
  143. */
  144. encoder.set_do_qlp_coeff_prec_search(false);
  145. encoder.set_do_exhaustive_model_search(false);
  146. }
  147. if ( size > 2 * 4096 * 4 + 250 ) {
  148. /* With subdivide_tukey in the mix testing apodizations can get really expensive. Therefore
  149. * this is disabled for inputs of more than one whole stereo block of 32-bit inputs plus a
  150. * bit of overhead */
  151. encoder.set_apodization("");
  152. }
  153. {
  154. ::FLAC__StreamEncoderInitStatus ret;
  155. if ( ds.Get<bool>() ) {
  156. ret = encoder.init();
  157. } else {
  158. ret = encoder.init_ogg();
  159. }
  160. if ( ret != FLAC__STREAM_ENCODER_INIT_STATUS_OK ) {
  161. goto end;
  162. }
  163. }
  164. /* These sets must fail, because encoder is already initialized */
  165. {
  166. bool res = false;
  167. res = res || encoder.set_streamable_subset(true);
  168. res = res || encoder.set_ogg_serial_number(0);
  169. res = res || encoder.set_verify(true);
  170. res = res || encoder.set_compression_level(0);
  171. res = res || encoder.set_do_exhaustive_model_search(true);
  172. res = res || encoder.set_do_mid_side_stereo(true);
  173. res = res || encoder.set_loose_mid_side_stereo(true);
  174. res = res || encoder.set_apodization("test");
  175. res = res || encoder.set_max_lpc_order(0);
  176. res = res || encoder.set_qlp_coeff_precision(0);
  177. res = res || encoder.set_do_qlp_coeff_prec_search(true);
  178. res = res || encoder.set_do_escape_coding(true);
  179. res = res || encoder.set_min_residual_partition_order(0);
  180. res = res || encoder.set_max_residual_partition_order(0);
  181. res = res || encoder.set_rice_parameter_search_dist(0);
  182. res = res || encoder.set_total_samples_estimate(0);
  183. res = res || encoder.set_channels(channels);
  184. res = res || encoder.set_bits_per_sample(16);
  185. res = res || encoder.set_limit_min_bitrate(true);
  186. res = res || encoder.set_blocksize(3021);
  187. res = res || encoder.set_sample_rate(44100);
  188. fuzzing::memory::memory_test(res);
  189. if(res)
  190. abort();
  191. }
  192. {
  193. /* XORing values as otherwise compiler will optimize, apparently */
  194. bool res = false;
  195. res = res != encoder.get_streamable_subset();
  196. res = res != encoder.get_verify();
  197. res = res != encoder.get_do_exhaustive_model_search();
  198. res = res != encoder.get_do_mid_side_stereo();
  199. res = res != encoder.get_loose_mid_side_stereo();
  200. res = res != encoder.get_max_lpc_order();
  201. res = res != encoder.get_qlp_coeff_precision();
  202. res = res != encoder.get_do_qlp_coeff_prec_search();
  203. res = res != encoder.get_do_escape_coding();
  204. res = res != encoder.get_min_residual_partition_order();
  205. res = res != encoder.get_max_residual_partition_order();
  206. res = res != encoder.get_rice_parameter_search_dist();
  207. res = res != encoder.get_total_samples_estimate();
  208. res = res != encoder.get_channels();
  209. res = res != encoder.get_bits_per_sample();
  210. res = res != encoder.get_limit_min_bitrate();
  211. res = res != encoder.get_blocksize();
  212. res = res != encoder.get_sample_rate();
  213. fuzzing::memory::memory_test(res);
  214. }
  215. while ( ds.Get<bool>() ) {
  216. {
  217. auto dat = ds.GetVector<FLAC__int32>();
  218. if( ds.Get<bool>() )
  219. /* Mask */
  220. for (size_t i = 0; i < dat.size(); i++)
  221. /* If we get here, bps is 4 or larger, or init will have failed */
  222. dat[i] = (int32_t)(((uint32_t)(dat[i]) << (32-bps)) >> (32-bps));
  223. const uint32_t samples = dat.size() / channels;
  224. if ( samples > 0 ) {
  225. const int32_t* ptr = dat.data();
  226. const bool res = encoder.process_interleaved(ptr, samples);
  227. fuzzing::memory::memory_test(res);
  228. }
  229. }
  230. }
  231. } catch ( ... ) { }
  232. end:
  233. {
  234. const bool res = encoder.finish();
  235. fuzzing::memory::memory_test(res);
  236. }
  237. return 0;
  238. }