| 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 |
|
| 24 |
#include <cstddef> |
| 25 |
#include <cstdint> |
| 26 |
#include <limits> |
| 27 |
|
| 28 |
#include <fuzzing/datasource/datasource.hpp> |
| 29 |
#include <fuzzing/memory.hpp> |
| 30 |
|
| 31 |
#include "FLAC++/encoder.h" |
| 32 |
#include "common.h" |
| 33 |
|
| 34 |
namespace FLAC { |
| 35 |
namespace Encoder { |
| 36 |
class FuzzerStream : public Stream { |
| 37 |
private: |
| 38 |
// fuzzing::datasource::Datasource& ds; |
| 39 |
public: |
| 40 |
FuzzerStream(fuzzing::datasource::Datasource&) : |
| 41 |
Stream() { } |
| 42 |
|
| 43 |
::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, uint32_t /* samples */, uint32_t /* current_frame */) override { |
| 44 |
fuzzing::memory::memory_test(buffer, bytes); |
| 45 |
#if 0 |
| 46 |
try { |
| 47 |
if ( ds.Get<bool>() == true ) { |
| 48 |
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 49 |
} |
| 50 |
} catch ( ... ) { } |
| 51 |
#endif |
| 52 |
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; |
| 53 |
} |
| 54 |
}; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 59 |
fuzzing::datasource::Datasource ds(data, size); |
| 60 |
FLAC::Encoder::FuzzerStream encoder(ds); |
| 61 |
|
| 62 |
try { |
| 63 |
const int channels = ds.Get<uint8_t>(); |
| 64 |
const int bps = ds.Get<uint8_t>(); |
| 65 |
encoder.set_channels(channels); |
| 66 |
encoder.set_bits_per_sample(bps); |
| 67 |
|
| 68 |
{ |
| 69 |
const bool res = encoder.set_streamable_subset(ds.Get<bool>()); |
| 70 |
fuzzing::memory::memory_test(res); |
| 71 |
} |
| 72 |
{ |
| 73 |
const bool res = encoder.set_ogg_serial_number(ds.Get<long>()); |
| 74 |
fuzzing::memory::memory_test(res); |
| 75 |
} |
| 76 |
{ |
| 77 |
const bool res = encoder.set_verify(ds.Get<bool>()); |
| 78 |
fuzzing::memory::memory_test(res); |
| 79 |
} |
| 80 |
{ |
| 81 |
const bool res = encoder.set_compression_level(ds.Get<uint8_t>()); |
| 82 |
fuzzing::memory::memory_test(res); |
| 83 |
} |
| 84 |
{ |
| 85 |
const bool res = encoder.set_do_exhaustive_model_search(ds.Get<bool>()); |
| 86 |
fuzzing::memory::memory_test(res); |
| 87 |
} |
| 88 |
{ |
| 89 |
const bool res = encoder.set_do_mid_side_stereo(ds.Get<bool>()); |
| 90 |
fuzzing::memory::memory_test(res); |
| 91 |
} |
| 92 |
{ |
| 93 |
const bool res = encoder.set_loose_mid_side_stereo(ds.Get<bool>()); |
| 94 |
fuzzing::memory::memory_test(res); |
| 95 |
} |
| 96 |
{ |
| 97 |
const auto s = ds.Get<std::string>(); |
| 98 |
const bool res = encoder.set_apodization(s.data()); |
| 99 |
fuzzing::memory::memory_test(res); |
| 100 |
} |
| 101 |
{ |
| 102 |
const bool res = encoder.set_max_lpc_order(ds.Get<uint8_t>()); |
| 103 |
fuzzing::memory::memory_test(res); |
| 104 |
} |
| 105 |
{ |
| 106 |
const bool res = encoder.set_qlp_coeff_precision(ds.Get<uint32_t>()); |
| 107 |
fuzzing::memory::memory_test(res); |
| 108 |
} |
| 109 |
{ |
| 110 |
const bool res = encoder.set_do_qlp_coeff_prec_search(ds.Get<bool>()); |
| 111 |
fuzzing::memory::memory_test(res); |
| 112 |
} |
| 113 |
{ |
| 114 |
const bool res = encoder.set_do_escape_coding(ds.Get<bool>()); |
| 115 |
fuzzing::memory::memory_test(res); |
| 116 |
} |
| 117 |
{ |
| 118 |
const bool res = encoder.set_min_residual_partition_order(ds.Get<uint32_t>()); |
| 119 |
fuzzing::memory::memory_test(res); |
| 120 |
} |
| 121 |
{ |
| 122 |
const bool res = encoder.set_max_residual_partition_order(ds.Get<uint32_t>()); |
| 123 |
fuzzing::memory::memory_test(res); |
| 124 |
} |
| 125 |
{ |
| 126 |
const bool res = encoder.set_rice_parameter_search_dist(ds.Get<uint32_t>()); |
| 127 |
fuzzing::memory::memory_test(res); |
| 128 |
} |
| 129 |
{ |
| 130 |
const bool res = encoder.set_total_samples_estimate(ds.Get<uint64_t>()); |
| 131 |
fuzzing::memory::memory_test(res); |
| 132 |
} |
| 133 |
{ |
| 134 |
const bool res = encoder.set_blocksize(ds.Get<uint16_t>()); |
| 135 |
fuzzing::memory::memory_test(res); |
| 136 |
} |
| 137 |
{ |
| 138 |
const bool res = encoder.set_limit_min_bitrate(ds.Get<bool>()); |
| 139 |
fuzzing::memory::memory_test(res); |
| 140 |
} |
| 141 |
{ |
| 142 |
const bool res = encoder.set_sample_rate(ds.Get<uint32_t>()); |
| 143 |
fuzzing::memory::memory_test(res); |
| 144 |
} |
| 145 |
{ |
| 146 |
const bool res = encoder.set_num_threads(ds.Get<uint32_t>()); |
| 147 |
fuzzing::memory::memory_test(res); |
| 148 |
} |
| 149 |
|
| 150 |
if ( size > 2 * 65535 * 4 ) { |
| 151 |
/* With large inputs and expensive options enabled, the fuzzer can get *really* slow. |
| 152 |
* Some combinations can make the fuzzer timeout (>60 seconds). However, while combining |
| 153 |
* options makes the fuzzer slower, most options do not expose new code when combined. |
| 154 |
* Therefore, combining slow options is disabled for large inputs. Any input containing |
| 155 |
* more than 65536 * 2 samples of 32 bits each (max blocksize, stereo) is considered large |
| 156 |
*/ |
| 157 |
encoder.set_do_qlp_coeff_prec_search(false); |
| 158 |
encoder.set_do_exhaustive_model_search(false); |
| 159 |
} |
| 160 |
if ( size > 2 * 4096 * 4 + 250 ) { |
| 161 |
/* With subdivide_tukey in the mix testing apodizations can get really expensive. Therefore |
| 162 |
* this is disabled for inputs of more than one whole stereo block of 32-bit inputs plus a |
| 163 |
* bit of overhead */ |
| 164 |
encoder.set_apodization(""); |
| 165 |
} |
| 166 |
|
| 167 |
{ |
| 168 |
::FLAC__StreamEncoderInitStatus ret; |
| 169 |
if ( ds.Get<bool>() ) { |
| 170 |
ret = encoder.init(); |
| 171 |
} else { |
| 172 |
ret = encoder.init_ogg(); |
| 173 |
} |
| 174 |
|
| 175 |
if ( ret != FLAC__STREAM_ENCODER_INIT_STATUS_OK ) { |
| 176 |
goto end; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/* These sets must fail, because encoder is already initialized */ |
| 181 |
{ |
| 182 |
bool res = false; |
| 183 |
res = res || encoder.set_streamable_subset(true); |
| 184 |
res = res || encoder.set_ogg_serial_number(0); |
| 185 |
res = res || encoder.set_verify(true); |
| 186 |
res = res || encoder.set_compression_level(0); |
| 187 |
res = res || encoder.set_do_exhaustive_model_search(true); |
| 188 |
res = res || encoder.set_do_mid_side_stereo(true); |
| 189 |
res = res || encoder.set_loose_mid_side_stereo(true); |
| 190 |
res = res || encoder.set_apodization("test"); |
| 191 |
res = res || encoder.set_max_lpc_order(0); |
| 192 |
res = res || encoder.set_qlp_coeff_precision(0); |
| 193 |
res = res || encoder.set_do_qlp_coeff_prec_search(true); |
| 194 |
res = res || encoder.set_do_escape_coding(true); |
| 195 |
res = res || encoder.set_min_residual_partition_order(0); |
| 196 |
res = res || encoder.set_max_residual_partition_order(0); |
| 197 |
res = res || encoder.set_rice_parameter_search_dist(0); |
| 198 |
res = res || encoder.set_total_samples_estimate(0); |
| 199 |
res = res || encoder.set_channels(channels); |
| 200 |
res = res || encoder.set_bits_per_sample(16); |
| 201 |
res = res || encoder.set_limit_min_bitrate(true); |
| 202 |
res = res || encoder.set_blocksize(3021); |
| 203 |
res = res || encoder.set_sample_rate(44100); |
| 204 |
res = res || (encoder.set_num_threads(4) == FLAC__STREAM_ENCODER_SET_NUM_THREADS_OK); |
| 205 |
fuzzing::memory::memory_test(res); |
| 206 |
if(res) |
| 207 |
abort(); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
{ |
| 212 |
/* XORing values as otherwise compiler will optimize, apparently */ |
| 213 |
bool res = false; |
| 214 |
res = res != encoder.get_streamable_subset(); |
| 215 |
res = res != encoder.get_verify(); |
| 216 |
res = res != encoder.get_do_exhaustive_model_search(); |
| 217 |
res = res != encoder.get_do_mid_side_stereo(); |
| 218 |
res = res != encoder.get_loose_mid_side_stereo(); |
| 219 |
res = res != encoder.get_max_lpc_order(); |
| 220 |
res = res != encoder.get_qlp_coeff_precision(); |
| 221 |
res = res != encoder.get_do_qlp_coeff_prec_search(); |
| 222 |
res = res != encoder.get_do_escape_coding(); |
| 223 |
res = res != encoder.get_min_residual_partition_order(); |
| 224 |
res = res != encoder.get_max_residual_partition_order(); |
| 225 |
res = res != encoder.get_rice_parameter_search_dist(); |
| 226 |
res = res != encoder.get_total_samples_estimate(); |
| 227 |
res = res != encoder.get_channels(); |
| 228 |
res = res != encoder.get_bits_per_sample(); |
| 229 |
res = res != encoder.get_limit_min_bitrate(); |
| 230 |
res = res != encoder.get_blocksize(); |
| 231 |
res = res != encoder.get_sample_rate(); |
| 232 |
res = res != encoder.get_num_threads(); |
| 233 |
fuzzing::memory::memory_test(res); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
while ( ds.Get<bool>() ) { |
| 238 |
{ |
| 239 |
auto dat = ds.GetVector<FLAC__int32>(); |
| 240 |
|
| 241 |
if( ds.Get<bool>() ) |
| 242 |
/* Mask */ |
| 243 |
for (size_t i = 0; i < dat.size(); i++) |
| 244 |
/* If we get here, bps is 4 or larger, or init will have failed */ |
| 245 |
dat[i] = (int32_t)(((uint32_t)(dat[i]) << (32-bps)) >> (32-bps)); |
| 246 |
|
| 247 |
const uint32_t samples = dat.size() / channels; |
| 248 |
if ( samples > 0 ) { |
| 249 |
const int32_t* ptr = dat.data(); |
| 250 |
const bool res = encoder.process_interleaved(ptr, samples); |
| 251 |
fuzzing::memory::memory_test(res); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} catch ( ... ) { } |
| 256 |
|
| 257 |
end: |
| 258 |
{ |
| 259 |
const bool res = encoder.finish(); |
| 260 |
fuzzing::memory::memory_test(res); |
| 261 |
} |
| 262 |
return 0; |
| 263 |
} |