Coverage Report

Created: 2018-09-25 13:22

/src/fuzz_advanced_api.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2018 Google Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
17
#include "fuzz.h"
18
#include "webp/decode.h"
19
20
5.27k
int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) {
21
5.27k
  WebPDecoderConfig config;
22
5.27k
  if (!WebPInitDecoderConfig(&config)) return 0;
23
5.27k
  if (WebPGetFeatures(data, size, &config.input) != VP8_STATUS_OK) return 0;
24
4.82k
  if ((size_t)config.input.width * config.input.height > kFuzzPxLimit) return 0;
25
4.78k
26
4.78k
  // Using two independent criteria ensures that all combinations of options
27
4.78k
  // can reach each path at the decoding stage, with meaningful differences.
28
4.78k
29
4.78k
  const uint8_t value = FuzzHash(data, size);
30
4.78k
  const float factor = value / 255.f;  // 0-1
31
4.78k
32
4.78k
  config.options.flip = value & 1;
33
4.78k
  config.options.bypass_filtering = value & 2;
34
4.78k
  config.options.no_fancy_upsampling = value & 4;
35
4.78k
  config.options.use_threads = value & 8;
36
4.78k
  if (size & 1) {
37
1.71k
    config.options.use_cropping = 1;
38
1.71k
    config.options.crop_width = (int)(config.input.width * (1 - factor));
39
1.71k
    config.options.crop_height = (int)(config.input.height * (1 - factor));
40
1.71k
    config.options.crop_left = config.input.width - config.options.crop_width;
41
1.71k
    config.options.crop_top = config.input.height - config.options.crop_height;
42
1.71k
  }
43
4.78k
  if (size & 2) {
44
2.26k
    int strength = (int)(factor * 100);
45
2.26k
    config.options.dithering_strength = strength;
46
2.26k
    config.options.alpha_dithering_strength = 100 - strength;
47
2.26k
  }
48
4.78k
  if (size & 4) {
49
2.73k
    config.options.use_scaling = 1;
50
2.73k
    config.options.scaled_width = (int)(config.input.width * factor * 2);
51
2.73k
    config.options.scaled_height = (int)(config.input.height * factor * 2);
52
2.73k
  }
53
4.78k
54
4.78k
  config.output.colorspace = (WEBP_CSP_MODE)(value % MODE_LAST);
55
4.78k
56
4.78k
  if (size % 3) {
57
3.48k
    // Decodes incrementally in chunks of increasing size.
58
3.48k
    WebPIDecoder* idec = WebPIDecode(NULL, 0, &config);
59
3.48k
    if (!idec) return 0;
60
3.48k
    VP8StatusCode status;
61
3.48k
    if (size & 8) {
62
1.74k
      size_t available_size = value + 1;
63
3.59k
      while (1) {
64
3.59k
        if (available_size > size) available_size = size;
65
3.59k
        status = WebPIUpdate(idec, data, available_size);
66
3.59k
        if (status != VP8_STATUS_SUSPENDED || available_size == size) break;
67
1.85k
        available_size *= 2;
68
1.85k
      }
69
1.74k
    } else {
70
1.74k
      // WebPIAppend expects new data and its size with each call.
71
1.74k
      // Implemented here by simply advancing the pointer into data.
72
1.74k
      const uint8_t* new_data = data;
73
1.74k
      size_t new_size = value + 1;
74
6.01k
      while (1) {
75
6.01k
        if (new_data + new_size > data + size) {
76
2.47k
          new_size = data + size - new_data;
77
2.47k
        }
78
6.01k
        status = WebPIAppend(idec, new_data, new_size);
79
6.01k
        if (status != VP8_STATUS_SUSPENDED || new_size == 0) break;
80
4.27k
        new_data += new_size;
81
4.27k
        new_size *= 2;
82
4.27k
      }
83
1.74k
    }
84
3.48k
    WebPIDelete(idec);
85
3.48k
  } else {
86
1.30k
    WebPDecode(data, size, &config);
87
1.30k
  }
88
4.78k
89
4.78k
  WebPFreeDecBuffer(&config.output);
90
4.78k
  return 0;
91
4.78k
}