opus_decode_fuzzer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright (c) 2017 Google Inc. */
  2. /*
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  12. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  13. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  15. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include <stdint.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "opus.h"
  30. #include "opus_types.h"
  31. #define MAX_FRAME_SAMP 5760
  32. #define MAX_PACKET 1500
  33. /* 4 bytes: packet length, 4 bytes: encoder final range */
  34. #define SETUP_BYTE_COUNT 8
  35. #define MAX_DECODES 12
  36. typedef struct {
  37. int fs;
  38. int channels;
  39. } TocInfo;
  40. static void ParseToc(const uint8_t *toc, TocInfo *const info) {
  41. const int samp_freqs[5] = {8000, 12000, 16000, 24000, 48000};
  42. const int bandwidth = opus_packet_get_bandwidth(toc);
  43. info->fs = samp_freqs[bandwidth - OPUS_BANDWIDTH_NARROWBAND];
  44. info->channels = opus_packet_get_nb_channels(toc);
  45. }
  46. /* Treats the input data as concatenated packets encoded by opus_demo,
  47. * structured as
  48. * bytes 0..3: packet length
  49. * bytes 4..7: encoder final range
  50. * bytes 8+ : Opus packet, including ToC
  51. */
  52. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  53. OpusDecoder *dec;
  54. opus_int16 *pcm;
  55. uint8_t *temp_data;
  56. TocInfo toc;
  57. int i = 0;
  58. int err = OPUS_OK;
  59. int num_decodes = 0;
  60. /* Not enough data to setup the decoder (+1 for the ToC) */
  61. if (size < SETUP_BYTE_COUNT + 1) {
  62. return 0;
  63. }
  64. /* Create decoder based on info from the first ToC available */
  65. ParseToc(&data[SETUP_BYTE_COUNT], &toc);
  66. dec = opus_decoder_create(toc.fs, toc.channels, &err);
  67. if (err != OPUS_OK || dec == NULL) {
  68. return 0;
  69. }
  70. pcm = (opus_int16*) malloc(sizeof(*pcm) * MAX_FRAME_SAMP * toc.channels);
  71. while (i + SETUP_BYTE_COUNT < size && num_decodes++ < MAX_DECODES) {
  72. int len, fec;
  73. len = (opus_uint32) data[i ] << 24 |
  74. (opus_uint32) data[i + 1] << 16 |
  75. (opus_uint32) data[i + 2] << 8 |
  76. (opus_uint32) data[i + 3];
  77. if (len > MAX_PACKET || len < 0 || i + SETUP_BYTE_COUNT + len > size) {
  78. break;
  79. }
  80. /* Bytes 4..7 represent encoder final range, but are unused here.
  81. * Instead, byte 4 is repurposed to determine if FEC is used. */
  82. fec = data[i + 4] & 1;
  83. if (len == 0) {
  84. /* Lost packet */
  85. int frame_size;
  86. opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&frame_size));
  87. (void) opus_decode(dec, NULL, len, pcm, frame_size, fec);
  88. } else {
  89. temp_data = (uint8_t*) malloc(len);
  90. memcpy(temp_data, &data[i + SETUP_BYTE_COUNT], len);
  91. (void) opus_decode(dec, temp_data, len, pcm, MAX_FRAME_SAMP, fec);
  92. free(temp_data);
  93. }
  94. i += SETUP_BYTE_COUNT + len;
  95. }
  96. opus_decoder_destroy(dec);
  97. free(pcm);
  98. return 0;
  99. }