xref: /aosp_15_r20/external/webp/tests/fuzzer/huffman_fuzzer.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2023 Google Inc.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*b2055c35SXin Li // you may not use this file except in compliance with the License.
5*b2055c35SXin Li // You may obtain a copy of the License at
6*b2055c35SXin Li //
7*b2055c35SXin Li //      http://www.apache.org/licenses/LICENSE-2.0
8*b2055c35SXin Li //
9*b2055c35SXin Li // Unless required by applicable law or agreed to in writing, software
10*b2055c35SXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*b2055c35SXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*b2055c35SXin Li // See the License for the specific language governing permissions and
13*b2055c35SXin Li // limitations under the License.
14*b2055c35SXin Li //
15*b2055c35SXin Li ////////////////////////////////////////////////////////////////////////////////
16*b2055c35SXin Li 
17*b2055c35SXin Li #include <stdint.h>
18*b2055c35SXin Li #include <string.h>
19*b2055c35SXin Li 
20*b2055c35SXin Li #include "src/dec/vp8li_dec.h"
21*b2055c35SXin Li #include "src/utils/bit_reader_utils.h"
22*b2055c35SXin Li #include "src/utils/huffman_utils.h"
23*b2055c35SXin Li #include "src/utils/utils.h"
24*b2055c35SXin Li #include "src/webp/format_constants.h"
25*b2055c35SXin Li 
LLVMFuzzerTestOneInput(const uint8_t * const data,size_t size)26*b2055c35SXin Li int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) {
27*b2055c35SXin Li   // Number of bits to initialize data.
28*b2055c35SXin Li   static const int kColorCacheBitsBits = 4;
29*b2055c35SXin Li   // 'num_htree_groups' is contained in the RG channel, hence 16 bits.
30*b2055c35SXin Li   static const int kNumHtreeGroupsBits = 16;
31*b2055c35SXin Li   if (size * sizeof(*data) < kColorCacheBitsBits + kNumHtreeGroupsBits) {
32*b2055c35SXin Li     return 0;
33*b2055c35SXin Li   }
34*b2055c35SXin Li 
35*b2055c35SXin Li   // A non-NULL mapping brings minor changes that are tested by the normal
36*b2055c35SXin Li   // fuzzer.
37*b2055c35SXin Li   int* const mapping = NULL;
38*b2055c35SXin Li   HuffmanTables huffman_tables;
39*b2055c35SXin Li   memset(&huffman_tables, 0, sizeof(huffman_tables));
40*b2055c35SXin Li   HTreeGroup* htree_groups = NULL;
41*b2055c35SXin Li 
42*b2055c35SXin Li   VP8LDecoder* dec = VP8LNew();
43*b2055c35SXin Li   if (dec == NULL) goto Error;
44*b2055c35SXin Li   VP8LBitReader* const br = &dec->br_;
45*b2055c35SXin Li   VP8LInitBitReader(br, data, size);
46*b2055c35SXin Li 
47*b2055c35SXin Li   const int color_cache_bits = VP8LReadBits(br, kColorCacheBitsBits);
48*b2055c35SXin Li   if (color_cache_bits < 1 || color_cache_bits > MAX_CACHE_BITS) goto Error;
49*b2055c35SXin Li 
50*b2055c35SXin Li   const int num_htree_groups = VP8LReadBits(br, kNumHtreeGroupsBits);
51*b2055c35SXin Li   // 'num_htree_groups' cannot be 0 as it is built from a non-empty image.
52*b2055c35SXin Li   if (num_htree_groups == 0) goto Error;
53*b2055c35SXin Li   // This variable is only useful when mapping is not NULL.
54*b2055c35SXin Li   const int num_htree_groups_max = num_htree_groups;
55*b2055c35SXin Li   (void)ReadHuffmanCodesHelper(color_cache_bits, num_htree_groups,
56*b2055c35SXin Li                                num_htree_groups_max, mapping, dec,
57*b2055c35SXin Li                                &huffman_tables, &htree_groups);
58*b2055c35SXin Li 
59*b2055c35SXin Li  Error:
60*b2055c35SXin Li   WebPSafeFree(mapping);
61*b2055c35SXin Li   VP8LHtreeGroupsFree(htree_groups);
62*b2055c35SXin Li   VP8LHuffmanTablesDeallocate(&huffman_tables);
63*b2055c35SXin Li   VP8LDelete(dec);
64*b2055c35SXin Li   return 0;
65*b2055c35SXin Li }
66