/src/cms_transform_extended_fuzzer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2022 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | |
13 | | #include <stdint.h> |
14 | | #include "lcms2.h" |
15 | | |
16 | | // An extended cmsDoTransform fuzzer. The idea is to include a range of |
17 | | // input/output source formats. |
18 | | |
19 | | void |
20 | | run_test(const uint8_t *data, |
21 | | size_t size, |
22 | | uint32_t intent, |
23 | 5.52k | uint32_t flags, int dstVal) { |
24 | 5.52k | if (size < 2) { |
25 | 1 | return; |
26 | 1 | } |
27 | | |
28 | 5.51k | cmsHPROFILE srcProfile = cmsOpenProfileFromMem(data, size); |
29 | 5.51k | if (!srcProfile) return; |
30 | | |
31 | | // Select dstProfile and dstFormat |
32 | 4.88k | cmsHPROFILE dstProfile; |
33 | 4.88k | uint32_t dstFormat; |
34 | 4.88k | if (dstVal == 1) { |
35 | 391 | dstProfile = cmsCreateLab4Profile(NULL); |
36 | 391 | dstFormat = TYPE_Lab_8; |
37 | 391 | } |
38 | 4.49k | else if (dstVal == 2) { |
39 | 501 | dstProfile = cmsCreateLab2Profile(NULL); |
40 | 501 | dstFormat = TYPE_LabV2_8; |
41 | 501 | } |
42 | 3.99k | else if (dstVal == 3) { |
43 | 166 | cmsToneCurve* gamma18; |
44 | 166 | gamma18 = cmsBuildGamma(0, 1.8); |
45 | 166 | dstProfile = cmsCreateGrayProfile(NULL, gamma18); |
46 | 166 | cmsFreeToneCurve(gamma18); |
47 | 166 | dstFormat = TYPE_GRAY_FLT | EXTRA_SH(1); |
48 | 166 | } |
49 | 3.83k | else if (dstVal == 4) { |
50 | 513 | dstProfile = cmsCreateXYZProfile(); |
51 | 513 | dstFormat = TYPE_XYZ_16; |
52 | 513 | } |
53 | 3.31k | else if (dstVal == 5) { |
54 | 306 | dstProfile = cmsCreateXYZProfile(); |
55 | 306 | dstFormat = TYPE_XYZ_DBL; |
56 | 306 | } |
57 | 3.01k | else if (dstVal == 6) { |
58 | 608 | dstProfile = cmsCreateLab4Profile(NULL); |
59 | 608 | dstFormat = TYPE_Lab_DBL; |
60 | 608 | } |
61 | 2.40k | else if (dstVal == 7) { |
62 | 314 | dstProfile = cmsCreateLab4Profile(NULL); |
63 | 314 | dstFormat = TYPE_Lab_DBL; |
64 | 314 | } |
65 | 2.08k | else if (dstVal == 8){ |
66 | 244 | dstProfile = cmsCreate_OkLabProfile(NULL); |
67 | 244 | dstFormat = (FLOAT_SH(1)|COLORSPACE_SH(PT_MCH3)|CHANNELS_SH(3)|BYTES_SH(0)); |
68 | 244 | } |
69 | 1.84k | else if (dstVal == 9){ |
70 | 221 | dstProfile = cmsCreateNULLProfile(); |
71 | 221 | dstFormat = 0; |
72 | 221 | } |
73 | 1.62k | else if (dstVal == 10){ |
74 | 470 | dstProfile = cmsCreateBCHSWabstractProfile(17, 0, 1.2, 0, 3, 5000, 5000); |
75 | 470 | dstFormat = TYPE_Lab_DBL; |
76 | 470 | } |
77 | 1.15k | else { |
78 | 1.15k | dstProfile = cmsCreate_sRGBProfile(); |
79 | 1.15k | dstFormat = TYPE_RGB_8; |
80 | 1.15k | } |
81 | | |
82 | 4.88k | if (!dstProfile) { |
83 | 0 | cmsCloseProfile(srcProfile); |
84 | 0 | return; |
85 | 0 | } |
86 | | |
87 | | // Extract srcFormat from the random src profile |
88 | 4.88k | cmsColorSpaceSignature srcCS = cmsGetColorSpace(srcProfile); |
89 | 4.88k | cmsUInt32Number nSrcComponents = cmsChannelsOf(srcCS); |
90 | 4.88k | cmsUInt32Number srcFormat; |
91 | 4.88k | if (srcCS == cmsSigLabData) { |
92 | 205 | if (dstVal != 7) { |
93 | 195 | srcFormat = |
94 | 195 | COLORSPACE_SH(PT_Lab) | CHANNELS_SH(nSrcComponents) | BYTES_SH(0); |
95 | 195 | } |
96 | 10 | else { |
97 | 10 | srcFormat = |
98 | 10 | COLORSPACE_SH(PT_Lab) | CHANNELS_SH(nSrcComponents) | BYTES_SH(0) | FLOAT_SH(1); |
99 | 10 | } |
100 | 4.68k | } else { |
101 | 4.68k | srcFormat = |
102 | 4.68k | COLORSPACE_SH(PT_ANY) | CHANNELS_SH(nSrcComponents) | BYTES_SH(1); |
103 | 4.68k | } |
104 | | |
105 | | // Create the transform |
106 | 4.88k | cmsContext ctx = cmsCreateContext(NULL, NULL); |
107 | 4.88k | cmsHTRANSFORM hTransform = cmsCreateTransformTHR( |
108 | 4.88k | ctx, |
109 | 4.88k | srcProfile, |
110 | 4.88k | srcFormat, |
111 | 4.88k | dstProfile, |
112 | 4.88k | dstFormat, |
113 | 4.88k | intent, |
114 | 4.88k | flags); |
115 | | |
116 | 4.88k | cmsCloseProfile(srcProfile); |
117 | 4.88k | cmsCloseProfile(dstProfile); |
118 | 4.88k | if (!hTransform) return; |
119 | | |
120 | | |
121 | | // Do transformation. |
122 | | // The output buffer type depends on the dstFormat |
123 | | // The input buffer type depends on the srcFormat. |
124 | 982 | if (T_BYTES(srcFormat) == 0) { // 0 means double |
125 | | // Ensure output is large enough |
126 | 4 | long long output[nSrcComponents*4]; |
127 | 4 | double input[nSrcComponents]; |
128 | 16 | for (uint32_t i = 0; i < nSrcComponents; i++) input[i] = 0.5f; |
129 | 4 | cmsDoTransform(hTransform, input, output, 1); |
130 | 4 | } |
131 | 978 | else { |
132 | 978 | uint8_t input[nSrcComponents]; |
133 | 4.53k | for (uint32_t i = 0; i < nSrcComponents; i++) input[i] = 128; |
134 | | |
135 | 978 | if (dstFormat == TYPE_XYZ_16) { |
136 | 82 | cmsCIEXYZ output_XYZ = { 0, 0, 0 }; |
137 | 82 | cmsDoTransform(hTransform, input, &output_XYZ, 1); |
138 | 82 | } |
139 | 896 | else if (dstFormat == TYPE_XYZ_DBL) { |
140 | 32 | cmsCIEXYZTRIPLE out[4]; |
141 | 32 | cmsDoTransform(hTransform, input, out, 1); |
142 | 32 | } |
143 | 864 | else if (dstFormat == TYPE_Lab_DBL || dstFormat == (FLOAT_SH(1)|COLORSPACE_SH(PT_MCH3)|CHANNELS_SH(3)|BYTES_SH(0))) { |
144 | 262 | cmsCIELab Lab1; |
145 | 262 | cmsDoTransform(hTransform, input, &Lab1, 1); |
146 | 262 | } |
147 | 602 | else { |
148 | 602 | uint8_t output[4]; |
149 | 602 | cmsDoTransform(hTransform, input, output, 1); |
150 | 602 | } |
151 | 978 | } |
152 | 982 | cmsDeleteTransform(hTransform); |
153 | 982 | } |
154 | | |
155 | | |
156 | 12.2k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
157 | 12.2k | if (size < 12) { |
158 | 11 | return 0; |
159 | 11 | } |
160 | | |
161 | 12.2k | uint32_t flags = *((const uint32_t *)data+0); |
162 | 12.2k | uint32_t intent = *((const uint32_t *)data+1) % 16; |
163 | 12.2k | int decider = *((int*)data+2) % 11; |
164 | 12.2k | data += 12; |
165 | 12.2k | size -= 12; |
166 | | |
167 | | // Transform using various output formats. |
168 | 12.2k | run_test(data, size, intent, flags, decider); |
169 | | |
170 | 12.2k | return 0; |
171 | 12.2k | } |