Henrik Lundin | fe32a76 | 2015-12-08 10:27:27 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "test/fuzzers/audio_decoder_fuzzer.h" |
Henrik Lundin | fe32a76 | 2015-12-08 10:27:27 | [diff] [blame] | 12 | |
henrik.lundin | fd2be27 | 2016-02-09 08:00:23 | [diff] [blame] | 13 | #include <limits> |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 14 | #include <optional> |
henrik.lundin | fd2be27 | 2016-02-09 08:00:23 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "api/audio_codecs/audio_decoder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "modules/rtp_rtcp/source/byte_io.h" |
| 18 | #include "rtc_base/checks.h" |
Henrik Lundin | fe32a76 | 2015-12-08 10:27:27 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace { |
henrik.lundin | fd2be27 | 2016-02-09 08:00:23 | [diff] [blame] | 22 | template <typename T, unsigned int B = sizeof(T)> |
| 23 | bool ParseInt(const uint8_t** data, size_t* remaining_size, T* value) { |
| 24 | static_assert(std::numeric_limits<T>::is_integer, "Type must be an integer."); |
| 25 | static_assert(sizeof(T) <= sizeof(uint64_t), |
| 26 | "Cannot read wider than uint64_t."); |
| 27 | static_assert(B <= sizeof(T), "T must be at least B bytes wide."); |
| 28 | if (B > *remaining_size) |
| 29 | return false; |
| 30 | uint64_t val = ByteReader<uint64_t, B>::ReadBigEndian(*data); |
| 31 | *data += B; |
| 32 | *remaining_size -= B; |
| 33 | *value = static_cast<T>(val); |
| 34 | return true; |
Henrik Lundin | fe32a76 | 2015-12-08 10:27:27 | [diff] [blame] | 35 | } |
| 36 | } // namespace |
| 37 | |
Artem Titov | 1ee563d | 2021-07-27 10:46:29 | [diff] [blame] | 38 | // This function reads two bytes from the beginning of `data`, interprets them |
Henrik Lundin | fe32a76 | 2015-12-08 10:27:27 | [diff] [blame] | 39 | // as the first packet length, and reads this many bytes if available. The |
| 40 | // payload is inserted into the decoder, and the process continues until no more |
henrik.lundin | fd2be27 | 2016-02-09 08:00:23 | [diff] [blame] | 41 | // data is available. Either AudioDecoder::Decode or |
| 42 | // AudioDecoder::DecodeRedundant is used, depending on the value of |
Artem Titov | 1ee563d | 2021-07-27 10:46:29 | [diff] [blame] | 43 | // `decode_type`. |
henrik.lundin | fd2be27 | 2016-02-09 08:00:23 | [diff] [blame] | 44 | void FuzzAudioDecoder(DecoderFunctionType decode_type, |
| 45 | const uint8_t* data, |
Henrik Lundin | fe32a76 | 2015-12-08 10:27:27 | [diff] [blame] | 46 | size_t size, |
| 47 | AudioDecoder* decoder, |
| 48 | int sample_rate_hz, |
| 49 | size_t max_decoded_bytes, |
| 50 | int16_t* decoded) { |
| 51 | const uint8_t* data_ptr = data; |
| 52 | size_t remaining_size = size; |
henrik.lundin | fd2be27 | 2016-02-09 08:00:23 | [diff] [blame] | 53 | size_t packet_len; |
Sam Zackrisson | 35c773d | 2018-07-13 14:00:31 | [diff] [blame] | 54 | constexpr size_t kMaxNumFuzzedPackets = 200; |
| 55 | for (size_t num_packets = 0; num_packets < kMaxNumFuzzedPackets; |
| 56 | ++num_packets) { |
| 57 | if (!(ParseInt<size_t, 2>(&data_ptr, &remaining_size, &packet_len) && |
| 58 | packet_len <= remaining_size)) { |
| 59 | break; |
| 60 | } |
Henrik Lundin | fe32a76 | 2015-12-08 10:27:27 | [diff] [blame] | 61 | AudioDecoder::SpeechType speech_type; |
henrik.lundin | fd2be27 | 2016-02-09 08:00:23 | [diff] [blame] | 62 | switch (decode_type) { |
| 63 | case DecoderFunctionType::kNormalDecode: |
| 64 | decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes, |
| 65 | decoded, &speech_type); |
| 66 | break; |
| 67 | case DecoderFunctionType::kRedundantDecode: |
| 68 | decoder->DecodeRedundant(data_ptr, packet_len, sample_rate_hz, |
| 69 | max_decoded_bytes, decoded, &speech_type); |
| 70 | break; |
| 71 | } |
Henrik Lundin | fe32a76 | 2015-12-08 10:27:27 | [diff] [blame] | 72 | data_ptr += packet_len; |
| 73 | remaining_size -= packet_len; |
henrik.lundin | fd2be27 | 2016-02-09 08:00:23 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Henrik Lundin | fe32a76 | 2015-12-08 10:27:27 | [diff] [blame] | 77 | } // namespace webrtc |