FreeRDP
Loading...
Searching...
No Matches
TestFuzzClearCompress.c
1
19
20#include <stddef.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <winpr/crt.h>
26#include <winpr/wlog.h>
27
28#include <freerdp/codec/color.h>
29#include <freerdp/codec/clear.h>
30
31/* Each input is a few frames compressed by one encoder and decompressed by one decoder, so the
32 * V-Bar caches of both sides must stay in sync from frame to frame.
33 *
34 * Input layout:
35 * [0..1] width - 1 (9 bits)
36 * [2] height - 1
37 * [3] source pixel format index
38 * [4] bits 0-1: number of frames - 1, bits 2-4: extra bytes per source line,
39 * bit 5: pass a 0 stride (only without extra bytes)
40 * [5..] pixel data, repeated to fill the image, shifted by one byte per frame
41 */
42
43#define FUZZ_CLEAR_MAX_PIXELS 65536u /* 256x256 */
44
45/* NSCodec subcodec: color loss level 3 */
46#define FUZZ_CLEAR_NSC_TOLERANCE 16
47
48static const UINT32 FUZZ_CLEAR_FORMATS[] = { PIXEL_FORMAT_BGRX32, PIXEL_FORMAT_BGRA32,
49 PIXEL_FORMAT_XRGB32, PIXEL_FORMAT_ARGB32,
50 PIXEL_FORMAT_RGBX32, PIXEL_FORMAT_RGBA32,
51 PIXEL_FORMAT_BGR24, PIXEL_FORMAT_RGB24 };
52
53static UINT32 fuzz_clear_subcodec_size(const BYTE* pCompressed, UINT32 compressedSize)
54{
55 if (compressedSize < 14)
56 return 0;
57 return (UINT32)pCompressed[10] | ((UINT32)pCompressed[11] << 8) |
58 ((UINT32)pCompressed[12] << 16) | ((UINT32)pCompressed[13] << 24);
59}
60
61WINPR_NORETURN(static void fuzz_clear_fail(const char* what, UINT32 frame, UINT32 width,
62 UINT32 height, UINT32 format));
63
64static void fuzz_clear_fail(const char* what, UINT32 frame, UINT32 width, UINT32 height,
65 UINT32 format)
66{
67 (void)fprintf(stderr, "clear compressor: %s, frame %" PRIu32 " %" PRIu32 "x%" PRIu32 " %s\n",
68 what, frame, width, height, FreeRDPGetColorFormatName(format));
69 abort();
70}
71
72static void fuzz_clear_check(const BYTE* pSrc, size_t srcSize, UINT32 SrcFormat, UINT32 nSrcStep,
73 const BYTE* pDst, size_t dstSize, UINT32 nDstStep, UINT32 width,
74 UINT32 height, UINT32 tolerance, UINT32 frame)
75{
76 const UINT32 srcBpp = FreeRDPGetBytesPerPixel(SrcFormat);
77
78 for (UINT32 y = 0; y < height; y++)
79 {
80 for (UINT32 x = 0; x < width; x++)
81 {
82 BYTE src[3] = WINPR_C_ARRAY_INIT;
83 BYTE dst[3] = WINPR_C_ARRAY_INIT;
84
85 const size_t srcOffset = 1ull * y * nSrcStep + 1ull * x * srcBpp;
86 const size_t dstOffset = 1ull * y * nDstStep + 4ull * x;
87 if ((srcOffset >= srcSize) || (srcSize - srcOffset < srcBpp) ||
88 (dstOffset >= dstSize) || (dstSize - dstOffset < 4))
89 fuzz_clear_fail("pixel outside of the image", frame, width, height, SrcFormat);
90
91 const UINT32 srcColor = FreeRDPReadColor(&pSrc[srcOffset], SrcFormat);
92 FreeRDPSplitColor(srcColor, SrcFormat, &src[0], &src[1], &src[2], nullptr, nullptr);
93
94 const UINT32 dstColor = FreeRDPReadColor(&pDst[dstOffset], PIXEL_FORMAT_BGRX32);
95 FreeRDPSplitColor(dstColor, PIXEL_FORMAT_BGRX32, &dst[0], &dst[1], &dst[2], nullptr,
96 nullptr);
97
98 for (size_t c = 0; c < ARRAYSIZE(src); c++)
99 {
100 const int diff = (int)src[c] - (int)dst[c];
101 if ((UINT32)abs(diff) > tolerance)
102 fuzz_clear_fail("decoded image differs", frame, width, height, SrcFormat);
103 }
104 }
105 }
106}
107
108int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
109{
110 static BOOL loggingInitialized = FALSE;
111
112 if (!loggingInitialized)
113 {
114 (void)WLog_SetLogLevel(WLog_GetRoot(), WLOG_OFF);
115 loggingInitialized = TRUE;
116 }
117
118 if (size < 6)
119 return 0;
120
121 const UINT32 width = 1u + (data[0] | ((data[1] & 0x01u) << 8));
122 UINT32 height = 1u + data[2];
123 if (1ull * width * height > FUZZ_CLEAR_MAX_PIXELS)
124 height = FUZZ_CLEAR_MAX_PIXELS / width;
125
126 const UINT32 SrcFormat = FUZZ_CLEAR_FORMATS[data[3] % ARRAYSIZE(FUZZ_CLEAR_FORMATS)];
127 const UINT32 frames = 1u + (data[4] & 0x03u);
128 const UINT32 extra = (data[4] >> 2) & 0x07u;
129 const BOOL zeroStride = (extra == 0) && ((data[4] & 0x20u) != 0);
130 const uint8_t* pixels = &data[5];
131 const size_t pixelsSize = size - 5;
132
133 const UINT32 nSrcStep = width * FreeRDPGetBytesPerPixel(SrcFormat) + extra;
134 const UINT32 nDstStep = width * 4;
135 const size_t srcSize = 1ull * nSrcStep * height;
136 const size_t dstSize = 1ull * nDstStep * height;
137 BYTE* pSrc = malloc(srcSize);
138 BYTE* pDst = calloc(dstSize, 1);
139 CLEAR_CONTEXT* encoder = clear_context_new(TRUE);
140 CLEAR_CONTEXT* decoder = clear_context_new(FALSE);
141
142 if (!pSrc || !pDst || !encoder || !decoder)
143 goto fail;
144
145 for (UINT32 frame = 0; frame < frames; frame++)
146 {
147 for (size_t i = 0; i < srcSize; i++)
148 pSrc[i] = pixels[(i + frame) % pixelsSize];
149
150 BYTE* pCompressed = nullptr;
151 UINT32 compressedSize = 0;
152 if (!clear_compress(encoder, pSrc, SrcFormat, zeroStride ? 0 : nSrcStep, width, height,
153 &pCompressed, &compressedSize))
154 fuzz_clear_fail("clear_compress failed", frame, width, height, SrcFormat);
155
156 if (clear_decompress(decoder, pCompressed, compressedSize, width, height, pDst,
157 PIXEL_FORMAT_BGRX32, nDstStep, 0, 0, width, height, nullptr) != 0)
158 fuzz_clear_fail("clear_decompress failed", frame, width, height, SrcFormat);
159
160 const UINT32 tolerance = (fuzz_clear_subcodec_size(pCompressed, compressedSize) > 0)
161 ? FUZZ_CLEAR_NSC_TOLERANCE
162 : 0;
163 fuzz_clear_check(pSrc, srcSize, SrcFormat, nSrcStep, pDst, dstSize, nDstStep, width, height,
164 tolerance, frame);
165 }
166
167fail:
168 clear_context_free(encoder);
169 clear_context_free(decoder);
170 free(pSrc);
171 free(pDst);
172 return 0;
173}