aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/java/com/example/TurboJpegFuzzer.java
blob: b9ea715b1904e5ba7cd855bba01d78da75af0d16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2021 Code Intelligence GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.example;

import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import org.libjpegturbo.turbojpeg.TJ;
import org.libjpegturbo.turbojpeg.TJDecompressor;
import org.libjpegturbo.turbojpeg.TJException;
import org.libjpegturbo.turbojpeg.TJTransform;
import org.libjpegturbo.turbojpeg.TJTransformer;

public class TurboJpegFuzzer {
  static byte[] buffer = new byte[128 * 128 * 4];

  public static void fuzzerInitialize() throws TJException {
    // Trigger an early load of the native library to show the coverage counters stats in libFuzzer.
    new TJDecompressor();
  }

  public static void fuzzerTestOneInput(FuzzedDataProvider data) {
    try {
      int flagsDecompress = data.consumeInt();
      int flagsTransform = data.consumeInt();
      int pixelFormat = data.consumeInt(TJ.PF_RGB, TJ.PF_CMYK);
      // Specify explicit small target width/height so that we can reuse a
      // fixed-size buffer.
      int desiredWidth = data.consumeInt(1, 128);
      int desiredHeight = data.consumeInt(1, 128);
      int transformOp = data.consumeInt(TJTransform.OP_NONE, TJTransform.OP_ROT270);
      int transformOptions = data.consumeInt();
      int transformWidth = data.consumeBoolean() ? 128 : 64;
      int transformHeight = data.consumeBoolean() ? 128 : 64;
      TJDecompressor tjd;
      if (data.consumeBoolean()) {
        TJTransformer tjt = new TJTransformer(data.consumeRemainingAsBytes());
        TJTransform tjf = new TJTransform(
            0, 0, transformWidth, transformHeight, transformOp, transformOptions, null);
        tjd = tjt.transform(new TJTransform[] {tjf}, flagsTransform)[0];
      } else {
        tjd = new TJDecompressor(data.consumeRemainingAsBytes());
      }
      tjd.decompress(buffer, 0, 0, desiredWidth, 0, desiredHeight, pixelFormat, flagsDecompress);
    } catch (Exception ignored) {
      // We are not looking for Java exceptions, but segfaults and ASan reports.
    }
  }
}