b8a7516d978f8453f67383d47a7b5ad1164316f1
[platform/upstream/libjpeg-turbo.git] / fuzz / transform.cc
1 /*
2  * Copyright (C)2021 D. R. Commander.  All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of the libjpeg-turbo Project nor the names of its
13  *   contributors may be used to endorse or promote products derived from this
14  *   software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <turbojpeg.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33
34
35 #define NUMXFORMS  3
36
37
38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
39 {
40   tjhandle handle = NULL;
41   unsigned char *dstBufs[NUMXFORMS] = { NULL, NULL, NULL };
42   unsigned long dstSizes[NUMXFORMS] = { 0, 0, 0 }, maxBufSize;
43   int width = 0, height = 0, jpegSubsamp, jpegColorspace, i, t;
44   tjtransform transforms[NUMXFORMS];
45 #if defined(__has_feature) && __has_feature(memory_sanitizer)
46   char env[18] = "JSIMD_FORCENONE=1";
47
48   /* The libjpeg-turbo SIMD extensions produce false positives with
49      MemorySanitizer. */
50   putenv(env);
51 #endif
52
53   if ((handle = tjInitTransform()) == NULL)
54     goto bailout;
55
56   /* We ignore the return value of tjDecompressHeader3(), because some JPEG
57      images may have unusual subsampling configurations that the TurboJPEG API
58      cannot identify but can still transform. */
59   tjDecompressHeader3(handle, data, size, &width, &height, &jpegSubsamp,
60                       &jpegColorspace);
61
62   /* Ignore 0-pixel images and images larger than 1 Megapixel.  Casting width
63      to (uint64_t) prevents integer overflow if width * height > INT_MAX. */
64   if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
65     goto bailout;
66
67   if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP)
68     jpegSubsamp = TJSAMP_444;
69
70   for (t = 0; t < NUMXFORMS; t++)
71     memset(&transforms[t], 0, sizeof(tjtransform));
72
73   transforms[0].op = TJXOP_NONE;
74   transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE;
75   dstBufs[0] = (unsigned char *)malloc(tjBufSize(width, height, jpegSubsamp));
76   if (!dstBufs[0])
77     goto bailout;
78
79   transforms[1].r.w = (width + 1) / 2;
80   transforms[1].r.h = (height + 1) / 2;
81   transforms[1].op = TJXOP_TRANSPOSE;
82   transforms[1].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE;
83   dstBufs[1] =
84     (unsigned char *)malloc(tjBufSize((width + 1) / 2, (height + 1) / 2,
85                                       TJSAMP_GRAY));
86   if (!dstBufs[1])
87     goto bailout;
88
89   transforms[2].op = TJXOP_ROT90;
90   transforms[2].options = TJXOPT_TRIM | TJXOPT_COPYNONE;
91   dstBufs[2] = (unsigned char *)malloc(tjBufSize(height, width, jpegSubsamp));
92   if (!dstBufs[2])
93     goto bailout;
94
95   maxBufSize = tjBufSize(width, height, jpegSubsamp);
96
97   if (tjTransform(handle, data, size, NUMXFORMS, dstBufs, dstSizes, transforms,
98                   TJFLAG_LIMITSCANS | TJFLAG_NOREALLOC) == 0) {
99     /* Touch all of the output pixels in order to catch uninitialized reads
100        when using MemorySanitizer. */
101     for (t = 0; t < NUMXFORMS; t++) {
102       int sum = 0;
103
104       for (i = 0; i < dstSizes[t]; i++)
105         sum += dstBufs[t][i];
106
107       /* Prevent the code above from being optimized out.  This test should
108          never be true, but the compiler doesn't know that. */
109       if (sum > 255 * maxBufSize)
110         goto bailout;
111     }
112   }
113
114   transforms[0].options &= ~TJXOPT_COPYNONE;
115   free(dstBufs[0]);
116   dstBufs[0] = NULL;
117   dstSizes[0] = 0;
118
119   if (tjTransform(handle, data, size, 1, dstBufs, dstSizes, transforms,
120                   TJFLAG_LIMITSCANS) == 0) {
121     int sum = 0;
122
123     for (i = 0; i < dstSizes[0]; i++)
124       sum += dstBufs[0][i];
125
126     if (sum > 255 * maxBufSize)
127       goto bailout;
128   }
129
130 bailout:
131   for (t = 0; t < NUMXFORMS; t++)
132     free(dstBufs[t]);
133   if (handle) tjDestroy(handle);
134   return 0;
135 }