| 1 |
/* |
| 2 |
* Copyright 2016 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "fuzz/Fuzz.h" |
| 9 |
#include "include/codec/SkCodec.h" |
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkData.h" |
| 13 |
#include "include/core/SkFontMgr.h" |
| 14 |
#include "include/core/SkImage.h" |
| 15 |
#include "include/core/SkMallocPixelRef.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkPath.h" |
| 18 |
#include "include/core/SkStream.h" |
| 19 |
#include "include/core/SkSurface.h" |
| 20 |
#include "include/core/SkTextBlob.h" |
| 21 |
#include "src/core/SkOSFile.h" |
| 22 |
#include "src/core/SkReadBuffer.h" |
| 23 |
#include "src/utils/SkOSPath.h" |
| 24 |
#include "tools/ToolUtils.h" |
| 25 |
#include "tools/flags/CommandLineFlags.h" |
| 26 |
#include "tools/fonts/FontToolUtils.h" |
| 27 |
|
| 28 |
#if defined(SK_CODEC_DECODES_PNG_WITH_RUST) |
| 29 |
#include "include/encode/SkPngRustEncoder.h" |
| 30 |
#else |
| 31 |
#include "include/encode/SkPngEncoder.h" |
| 32 |
#endif |
| 33 |
|
| 34 |
#include <iostream> |
| 35 |
#include <map> |
| 36 |
#include <regex> |
| 37 |
#include <signal.h> |
| 38 |
|
| 39 |
static DEFINE_string2(bytes, b, "", "A path to a file or a directory. If a file, the " |
| 40 |
"contents will be used as the fuzz bytes. If a directory, all files " |
| 41 |
"in the directory will be used as fuzz bytes for the fuzzer, one at a " |
| 42 |
"time."); |
| 43 |
static DEFINE_string2(name, n, "", "If --type is 'api', fuzz the API with this name."); |
| 44 |
static DEFINE_string2(dump, d, "", "If not empty, dump 'image*' or 'skp' types as a " |
| 45 |
"PNG with this name."); |
| 46 |
static DEFINE_int(loops, 1, "Run the fuzzer on each input this many times."); |
| 47 |
DEFINE_bool2(verbose, v, false, "Print more information while fuzzing."); |
| 48 |
|
| 49 |
// This cannot be inlined in DEFINE_string2 due to interleaved ifdefs |
| 50 |
static constexpr char g_type_message[] = "How to interpret --bytes, one of:\n" |
| 51 |
"android_codec\n" |
| 52 |
"animated_image_decode\n" |
| 53 |
"api\n" |
| 54 |
"color_deserialize\n" |
| 55 |
"colrv1\n" |
| 56 |
"filter_fuzz (equivalent to Chrome's filter_fuzz_stub)\n" |
| 57 |
"image_decode\n" |
| 58 |
"image_decode_incremental\n" |
| 59 |
"image_mode\n" |
| 60 |
"image_scale\n" |
| 61 |
"json\n" |
| 62 |
"path_deserialize\n" |
| 63 |
"region_deserialize\n" |
| 64 |
"region_set_path\n" |
| 65 |
"skdescriptor_deserialize\n" |
| 66 |
"skmeshspecialization\n" |
| 67 |
#if defined(SK_ENABLE_SKOTTIE) |
| 68 |
"skottie_json\n" |
| 69 |
#endif |
| 70 |
"skp\n" |
| 71 |
"skruntimeblender\n" |
| 72 |
"skruntimecolorfilter\n" |
| 73 |
"skruntimeeffect\n" |
| 74 |
"sksl2glsl\n" |
| 75 |
"sksl2metal\n" |
| 76 |
"sksl2pipeline\n" |
| 77 |
"sksl2spirv\n" |
| 78 |
"sksl2wgsl\n" |
| 79 |
"svg_dom\n" |
| 80 |
"textblob"; |
| 81 |
|
| 82 |
static DEFINE_string2(type, t, "", g_type_message); |
| 83 |
|
| 84 |
static int fuzz_file(const SkString& path, SkString type); |
| 85 |
static uint8_t calculate_option(SkData*); |
| 86 |
static SkString try_auto_detect(const SkString& path, SkString* name); |
| 87 |
|
| 88 |
static void fuzz_android_codec(const sk_sp<SkData>&); |
| 89 |
static void fuzz_animated_img(const sk_sp<SkData>&); |
| 90 |
static void fuzz_api(const sk_sp<SkData>&, const SkString& name); |
| 91 |
static void fuzz_color_deserialize(const sk_sp<SkData>&); |
| 92 |
static void fuzz_colrv1(const sk_sp<SkData>&); |
| 93 |
static void fuzz_filter_fuzz(const sk_sp<SkData>&); |
| 94 |
static void fuzz_image_decode(const sk_sp<SkData>&); |
| 95 |
static void fuzz_image_decode_incremental(const sk_sp<SkData>&); |
| 96 |
static void fuzz_img(const sk_sp<SkData>&, uint8_t, uint8_t); |
| 97 |
static void fuzz_json(const sk_sp<SkData>&); |
| 98 |
static void fuzz_parse_path(const sk_sp<SkData>&); |
| 99 |
static void fuzz_path_deserialize(const sk_sp<SkData>&); |
| 100 |
static void fuzz_region_deserialize(const sk_sp<SkData>&); |
| 101 |
static void fuzz_region_set_path(const sk_sp<SkData>&); |
| 102 |
static void fuzz_skdescriptor_deserialize(const sk_sp<SkData>&); |
| 103 |
static void fuzz_skmeshspecification(const sk_sp<SkData>&); |
| 104 |
static void fuzz_skp(const sk_sp<SkData>&); |
| 105 |
static void fuzz_skruntimeblender(const sk_sp<SkData>&); |
| 106 |
static void fuzz_skruntimecolorfilter(const sk_sp<SkData>&); |
| 107 |
static void fuzz_skruntimeeffect(const sk_sp<SkData>&); |
| 108 |
static void fuzz_sksl2glsl(const sk_sp<SkData>&); |
| 109 |
static void fuzz_sksl2metal(const sk_sp<SkData>&); |
| 110 |
static void fuzz_sksl2pipeline(const sk_sp<SkData>&); |
| 111 |
static void fuzz_sksl2spirv(const sk_sp<SkData>&); |
| 112 |
static void fuzz_sksl2wgsl(const sk_sp<SkData>&); |
| 113 |
static void fuzz_textblob_deserialize(const sk_sp<SkData>&); |
| 114 |
|
| 115 |
static void print_api_names(); |
| 116 |
|
| 117 |
#if defined(SK_ENABLE_SVG) |
| 118 |
static void fuzz_svg_dom(const sk_sp<SkData>&); |
| 119 |
#endif |
| 120 |
|
| 121 |
#if defined(SK_ENABLE_SKOTTIE) |
| 122 |
static void fuzz_skottie_json(const sk_sp<SkData>&); |
| 123 |
#endif |
| 124 |
|
| 125 |
int main(int argc, char** argv) { |
| 126 |
CommandLineFlags::SetUsage( |
| 127 |
"Usage: fuzz -t <type> -b <path/to/file> [-n api-to-fuzz]\n" |
| 128 |
" fuzz -b <path/to/file>\n" |
| 129 |
"--help lists the valid types. If type is not specified,\n" |
| 130 |
"fuzz will make a guess based on the name of the file.\n"); |
| 131 |
CommandLineFlags::Parse(argc, argv); |
| 132 |
ToolUtils::UsePortableFontMgr(); |
| 133 |
|
| 134 |
SkString path = SkString(FLAGS_bytes.isEmpty() ? argv[0] : FLAGS_bytes[0]); |
| 135 |
SkString type = SkString(FLAGS_type.isEmpty() ? "" : FLAGS_type[0]); |
| 136 |
|
| 137 |
int loopCount = std::max(FLAGS_loops, 1); |
| 138 |
|
| 139 |
if (!sk_isdir(path.c_str())) { |
| 140 |
for (int i = 0; i < loopCount; ++i) { |
| 141 |
int rv = fuzz_file(path, type); |
| 142 |
if (rv != 0) { |
| 143 |
return rv; |
| 144 |
} |
| 145 |
} |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
SkOSFile::Iter it(path.c_str()); |
| 150 |
for (SkString file; it.next(&file); ) { |
| 151 |
SkString p = SkOSPath::Join(path.c_str(), file.c_str()); |
| 152 |
SkDebugf("Fuzzing %s\n", p.c_str()); |
| 153 |
for (int i = 0; i < loopCount; ++i) { |
| 154 |
int rv = fuzz_file(p, type); |
| 155 |
if (rv != 0) { |
| 156 |
return rv; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static int fuzz_file(const SkString& path, SkString type) { |
| 164 |
sk_sp<SkData> bytes(SkData::MakeFromFileName(path.c_str())); |
| 165 |
if (!bytes) { |
| 166 |
SkDebugf("Could not read %s\n", path.c_str()); |
| 167 |
return 1; |
| 168 |
} |
| 169 |
|
| 170 |
SkString name = SkString(FLAGS_name.isEmpty() ? "" : FLAGS_name[0]); |
| 171 |
|
| 172 |
if (type.isEmpty()) { |
| 173 |
type = try_auto_detect(path, &name); |
| 174 |
} |
| 175 |
|
| 176 |
if (type.isEmpty()) { |
| 177 |
SkDebugf("Could not autodetect type of %s\n", path.c_str()); |
| 178 |
return 1; |
| 179 |
} |
| 180 |
if (type.equals("android_codec")) { |
| 181 |
fuzz_android_codec(std::move(bytes)); |
| 182 |
return 0; |
| 183 |
} |
| 184 |
if (type.equals("animated_image_decode")) { |
| 185 |
fuzz_animated_img(std::move(bytes)); |
| 186 |
return 0; |
| 187 |
} |
| 188 |
if (type.equals("api")) { |
| 189 |
fuzz_api(bytes, name); |
| 190 |
return 0; |
| 191 |
} |
| 192 |
if (type.equals("color_deserialize")) { |
| 193 |
fuzz_color_deserialize(std::move(bytes)); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
if (type.equals("colrv1")) { |
| 197 |
fuzz_colrv1(std::move(bytes)); |
| 198 |
return 0; |
| 199 |
} |
| 200 |
if (type.equals("filter_fuzz")) { |
| 201 |
fuzz_filter_fuzz(std::move(bytes)); |
| 202 |
return 0; |
| 203 |
} |
| 204 |
if (type.equals("image_decode")) { |
| 205 |
fuzz_image_decode(std::move(bytes)); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
if (type.equals("image_decode_incremental")) { |
| 209 |
fuzz_image_decode_incremental(std::move(bytes)); |
| 210 |
return 0; |
| 211 |
} |
| 212 |
if (type.equals("image_scale")) { |
| 213 |
uint8_t option = calculate_option(bytes.get()); |
| 214 |
fuzz_img(std::move(bytes), option, 0); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
if (type.equals("image_mode")) { |
| 218 |
uint8_t option = calculate_option(bytes.get()); |
| 219 |
fuzz_img(std::move(bytes), 0, option); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
if (type.equals("json")) { |
| 223 |
fuzz_json(std::move(bytes)); |
| 224 |
return 0; |
| 225 |
} |
| 226 |
if (type.equals("parse_path")) { |
| 227 |
fuzz_parse_path(std::move(bytes)); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
if (type.equals("path_deserialize")) { |
| 231 |
fuzz_path_deserialize(std::move(bytes)); |
| 232 |
return 0; |
| 233 |
} |
| 234 |
if (type.equals("region_deserialize")) { |
| 235 |
fuzz_region_deserialize(std::move(bytes)); |
| 236 |
return 0; |
| 237 |
} |
| 238 |
if (type.equals("region_set_path")) { |
| 239 |
fuzz_region_set_path(std::move(bytes)); |
| 240 |
return 0; |
| 241 |
} |
| 242 |
if (type.equals("pipe")) { |
| 243 |
SkDebugf("I would prefer not to.\n"); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
if (type.equals("skdescriptor_deserialize")) { |
| 247 |
fuzz_skdescriptor_deserialize(std::move(bytes)); |
| 248 |
return 0; |
| 249 |
} |
| 250 |
#if defined(SK_ENABLE_SKOTTIE) |
| 251 |
if (type.equals("skottie_json")) { |
| 252 |
fuzz_skottie_json(std::move(bytes)); |
| 253 |
return 0; |
| 254 |
} |
| 255 |
#endif |
| 256 |
if (type.equals("skmeshspecification")) { |
| 257 |
fuzz_skmeshspecification(std::move(bytes)); |
| 258 |
return 0; |
| 259 |
} |
| 260 |
if (type.equals("skp")) { |
| 261 |
fuzz_skp(std::move(bytes)); |
| 262 |
return 0; |
| 263 |
} |
| 264 |
if (type.equals("skruntimeblender")) { |
| 265 |
fuzz_skruntimeblender(std::move(bytes)); |
| 266 |
return 0; |
| 267 |
} |
| 268 |
if (type.equals("skruntimecolorfilter")) { |
| 269 |
fuzz_skruntimecolorfilter(std::move(bytes)); |
| 270 |
return 0; |
| 271 |
} |
| 272 |
if (type.equals("skruntimeeffect")) { |
| 273 |
fuzz_skruntimeeffect(std::move(bytes)); |
| 274 |
return 0; |
| 275 |
} |
| 276 |
if (type.equals("sksl2glsl")) { |
| 277 |
fuzz_sksl2glsl(std::move(bytes)); |
| 278 |
return 0; |
| 279 |
} |
| 280 |
if (type.equals("sksl2metal")) { |
| 281 |
fuzz_sksl2metal(std::move(bytes)); |
| 282 |
return 0; |
| 283 |
} |
| 284 |
if (type.equals("sksl2pipeline")) { |
| 285 |
fuzz_sksl2pipeline(std::move(bytes)); |
| 286 |
return 0; |
| 287 |
} |
| 288 |
if (type.equals("sksl2spirv")) { |
| 289 |
fuzz_sksl2spirv(std::move(bytes)); |
| 290 |
return 0; |
| 291 |
} |
| 292 |
if (type.equals("sksl2wgsl")) { |
| 293 |
fuzz_sksl2wgsl(std::move(bytes)); |
| 294 |
return 0; |
| 295 |
} |
| 296 |
#if defined(SK_ENABLE_SVG) |
| 297 |
if (type.equals("svg_dom")) { |
| 298 |
fuzz_svg_dom(std::move(bytes)); |
| 299 |
return 0; |
| 300 |
} |
| 301 |
#endif |
| 302 |
if (type.equals("textblob")) { |
| 303 |
fuzz_textblob_deserialize(std::move(bytes)); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
SkDebugf("Unknown type %s\n", type.c_str()); |
| 307 |
CommandLineFlags::PrintUsage(); |
| 308 |
return 1; |
| 309 |
} |
| 310 |
|
| 311 |
static std::map<std::string, std::string> cf_api_map = { |
| 312 |
{"api_create_ddl", "CreateDDL"}, |
| 313 |
{"api_draw_functions", "DrawFunctions"}, |
| 314 |
{"api_ddl_threading", "DDLThreadingGL"}, |
| 315 |
{"api_gradients", "Gradients"}, |
| 316 |
{"api_image_filter", "ImageFilter"}, |
| 317 |
{"api_mock_gpu_canvas", "MockGPUCanvas"}, |
| 318 |
{"api_null_canvas", "NullCanvas"}, |
| 319 |
{"api_path_measure", "PathMeasure"}, |
| 320 |
{"api_pathop", "Pathop"}, |
| 321 |
{"api_polyutils", "PolyUtils"}, |
| 322 |
#if defined(SK_GRAPHITE) && defined(SK_ENABLE_PRECOMPILE) |
| 323 |
{"api_precompile", "Precompile"}, |
| 324 |
#endif |
| 325 |
{"api_raster_n32_canvas", "RasterN32Canvas"}, |
| 326 |
{"api_skparagraph", "SkParagraph"}, |
| 327 |
{"api_svg_canvas", "SVGCanvas"}, |
| 328 |
{"cubic_quad_roots", "CubicQuadRoots"}, |
| 329 |
{"jpeg_encoder", "JPEGEncoder"}, |
| 330 |
// TODO(https://crbug.com/459478411): Add OSS-ClusterFuzz coverage of Rust |
| 331 |
// PNG encoder. (And also decoder? See earlier discussion about these map |
| 332 |
// entries at https://review.skia.org/1091836/comment/db7930d5_d2e1f030/) |
| 333 |
{"png_encoder", "PNGEncoder"}, |
| 334 |
{"skia_pathop_fuzzer", "LegacyChromiumPathop"}, |
| 335 |
{"webp_encoder", "WEBPEncoder"} |
| 336 |
}; |
| 337 |
|
| 338 |
// maps clusterfuzz/oss-fuzz -> Skia's name |
| 339 |
static std::map<std::string, std::string> cf_map = { |
| 340 |
{"android_codec", "android_codec"}, |
| 341 |
{"animated_image_decode", "animated_image_decode"}, |
| 342 |
{"colrv1", "colrv1"}, |
| 343 |
{"image_decode", "image_decode"}, |
| 344 |
{"image_decode_incremental", "image_decode_incremental"}, |
| 345 |
{"image_filter_deserialize", "filter_fuzz"}, |
| 346 |
{"image_filter_deserialize_width", "filter_fuzz"}, |
| 347 |
{"path_deserialize", "path_deserialize"}, |
| 348 |
{"region_deserialize", "region_deserialize"}, |
| 349 |
{"region_set_path", "region_set_path"}, |
| 350 |
{"skdescriptor_deserialize", "skdescriptor_deserialize"}, |
| 351 |
{"skjson", "json"}, |
| 352 |
{"skmeshspecification", "skmeshspecification"}, |
| 353 |
{"skp", "skp"}, |
| 354 |
{"skruntimeeffect", "skruntimeeffect"}, |
| 355 |
{"sksl2glsl", "sksl2glsl"}, |
| 356 |
{"sksl2metal", "sksl2metal"}, |
| 357 |
{"sksl2spirv", "sksl2spirv"}, |
| 358 |
{"sksl2pipeline", "sksl2pipeline"}, |
| 359 |
#if defined(SK_ENABLE_SKOTTIE) |
| 360 |
{"skottie_json", "skottie_json"}, |
| 361 |
#endif |
| 362 |
#if defined(SK_ENABLE_SVG) |
| 363 |
{"svg_dom", "svg_dom"}, |
| 364 |
#endif |
| 365 |
{"textblob_deserialize", "textblob"} |
| 366 |
}; |
| 367 |
|
| 368 |
static SkString try_auto_detect(const SkString& path, SkString* name) { |
| 369 |
std::cmatch m; |
| 370 |
std::regex clusterfuzz("clusterfuzz-testcase(-minimized)?-([a-z0-9_]+)-[\\d]+"); |
| 371 |
std::regex skiafuzzer("(api-)?(\\w+)-[a-f0-9]+"); |
| 372 |
|
| 373 |
if (std::regex_search(path.c_str(), m, clusterfuzz)) { |
| 374 |
std::string type = m.str(2); |
| 375 |
|
| 376 |
if (cf_api_map.find(type) != cf_api_map.end()) { |
| 377 |
*name = SkString(cf_api_map[type].c_str()); |
| 378 |
return SkString("api"); |
| 379 |
} else { |
| 380 |
if (cf_map.find(type) != cf_map.end()) { |
| 381 |
return SkString(cf_map[type].c_str()); |
| 382 |
} |
| 383 |
} |
| 384 |
} else if (std::regex_search(path.c_str(), m, skiafuzzer)) { |
| 385 |
std::string a1 = m.str(1); |
| 386 |
std::string typeOrName = m.str(2); |
| 387 |
if (a1.length() > 0) { |
| 388 |
// it's an api fuzzer |
| 389 |
*name = SkString(typeOrName.c_str()); |
| 390 |
return SkString("api"); |
| 391 |
} else { |
| 392 |
return SkString(typeOrName.c_str()); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
return SkString(); |
| 397 |
} |
| 398 |
|
| 399 |
void FuzzJSON(const uint8_t *data, size_t size); |
| 400 |
|
| 401 |
static void fuzz_json(const sk_sp<SkData>& data){ |
| 402 |
FuzzJSON(data->bytes(), data->size()); |
| 403 |
SkDebugf("[terminated] Done parsing!\n"); |
| 404 |
} |
| 405 |
|
| 406 |
#if defined(SK_ENABLE_SKOTTIE) |
| 407 |
void FuzzSkottieJSON(const uint8_t *data, size_t size); |
| 408 |
|
| 409 |
static void fuzz_skottie_json(const sk_sp<SkData>& data){ |
| 410 |
FuzzSkottieJSON(data->bytes(), data->size()); |
| 411 |
SkDebugf("[terminated] Done animating!\n"); |
| 412 |
} |
| 413 |
#endif |
| 414 |
|
| 415 |
#if defined(SK_ENABLE_SVG) |
| 416 |
void FuzzSVG(const uint8_t *data, size_t size); |
| 417 |
|
| 418 |
static void fuzz_svg_dom(const sk_sp<SkData>& data){ |
| 419 |
FuzzSVG(data->bytes(), data->size()); |
| 420 |
SkDebugf("[terminated] Done DOM!\n"); |
| 421 |
} |
| 422 |
#endif |
| 423 |
|
| 424 |
void FuzzCOLRv1(const uint8_t* data, size_t size); |
| 425 |
|
| 426 |
static void fuzz_colrv1(const sk_sp<SkData>& data) { |
| 427 |
FuzzCOLRv1(data->bytes(), data->size()); |
| 428 |
SkDebugf("[terminated] Done COLRv1!\n"); |
| 429 |
} |
| 430 |
|
| 431 |
// This adds up the first 1024 bytes and returns it as an 8 bit integer. This allows afl-fuzz to |
| 432 |
// deterministically excercise different paths, or *options* (such as different scaling sizes or |
| 433 |
// different image modes) without needing to introduce a parameter. This way we don't need a |
| 434 |
// image_scale1, image_scale2, image_scale4, etc fuzzer, we can just have a image_scale fuzzer. |
| 435 |
// Clients are expected to transform this number into a different range, e.g. with modulo (%). |
| 436 |
static uint8_t calculate_option(SkData* bytes) { |
| 437 |
uint8_t total = 0; |
| 438 |
const uint8_t* data = bytes->bytes(); |
| 439 |
for (size_t i = 0; i < 1024 && i < bytes->size(); i++) { |
| 440 |
total += data[i]; |
| 441 |
} |
| 442 |
return total; |
| 443 |
} |
| 444 |
|
| 445 |
static void print_api_names(){ |
| 446 |
SkDebugf("When using --type api, please choose an API to fuzz with --name/-n:\n"); |
| 447 |
for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) { |
| 448 |
SkDebugf("\t%s\n", fuzzable.name); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
static void fuzz_api(const sk_sp<SkData>& data, const SkString& name) { |
| 453 |
for (const Fuzzable& fuzzable : sk_tools::Registry<Fuzzable>::Range()) { |
| 454 |
if (name.equals(fuzzable.name)) { |
| 455 |
SkDebugf("Fuzzing %s...\n", fuzzable.name); |
| 456 |
Fuzz fuzz(data->bytes(), data->size()); |
| 457 |
fuzzable.fn(&fuzz); |
| 458 |
SkDebugf("[terminated] Success!\n"); |
| 459 |
return; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
print_api_names(); |
| 464 |
} |
| 465 |
|
| 466 |
static void dump_png(const SkBitmap& bitmap) { |
| 467 |
if (!FLAGS_dump.isEmpty()) { |
| 468 |
SkFILEWStream file(FLAGS_dump[0]); |
| 469 |
#if defined(SK_CODEC_DECODES_PNG_WITH_RUST) |
| 470 |
SkPngRustEncoder::Encode(&file, bitmap.pixmap(), {}); |
| 471 |
#else |
| 472 |
SkPngEncoder::Encode(&file, bitmap.pixmap(), {}); |
| 473 |
#endif |
| 474 |
SkDebugf("Dumped to %s\n", FLAGS_dump[0]); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
bool FuzzAnimatedImage(const uint8_t *data, size_t size); |
| 479 |
|
| 480 |
static void fuzz_animated_img(const sk_sp<SkData>& data) { |
| 481 |
if (FuzzAnimatedImage(data->bytes(), data->size())) { |
| 482 |
SkDebugf("[terminated] Success from decoding/drawing animated image!\n"); |
| 483 |
return; |
| 484 |
} |
| 485 |
SkDebugf("[terminated] Could not decode or draw animated image.\n"); |
| 486 |
} |
| 487 |
|
| 488 |
bool FuzzImageDecode(const uint8_t *data, size_t size); |
| 489 |
|
| 490 |
static void fuzz_image_decode(const sk_sp<SkData>& data) { |
| 491 |
if (FuzzImageDecode(data->bytes(), data->size())) { |
| 492 |
SkDebugf("[terminated] Success from decoding/drawing image!\n"); |
| 493 |
return; |
| 494 |
} |
| 495 |
SkDebugf("[terminated] Could not decode or draw image.\n"); |
| 496 |
} |
| 497 |
|
| 498 |
bool FuzzIncrementalImageDecode(const uint8_t *data, size_t size); |
| 499 |
|
| 500 |
static void fuzz_image_decode_incremental(const sk_sp<SkData>& data) { |
| 501 |
if (FuzzIncrementalImageDecode(data->bytes(), data->size())) { |
| 502 |
SkDebugf("[terminated] Success using incremental decode!\n"); |
| 503 |
return; |
| 504 |
} |
| 505 |
SkDebugf("[terminated] Could not incrementally decode and image.\n"); |
| 506 |
} |
| 507 |
|
| 508 |
bool FuzzAndroidCodec(const uint8_t *fuzzData, size_t fuzzSize, uint8_t sampleSize); |
| 509 |
|
| 510 |
static void fuzz_android_codec(const sk_sp<SkData>& data) { |
| 511 |
Fuzz fuzz(data->bytes(), data->size()); |
| 512 |
uint8_t sampleSize; |
| 513 |
fuzz.nextRange(&sampleSize, 1, 64); |
| 514 |
if (FuzzAndroidCodec(fuzz.remainingData(), fuzz.remainingSize(), sampleSize)) { |
| 515 |
SkDebugf("[terminated] Success on Android Codec sampleSize=%u!\n", sampleSize); |
| 516 |
return; |
| 517 |
} |
| 518 |
SkDebugf("[terminated] Could not use Android Codec sampleSize=%u!\n", sampleSize); |
| 519 |
} |
| 520 |
|
| 521 |
// This is a "legacy" fuzzer that likely does too much. It was based off of how |
| 522 |
// DM reads in images. image_decode, image_decode_incremental and android_codec |
| 523 |
// are more targeted fuzzers that do a subset of what this one does. |
| 524 |
static void fuzz_img(const sk_sp<SkData>& bytes, uint8_t scale, uint8_t mode) { |
| 525 |
// We can scale 1x, 2x, 4x, 8x, 16x |
| 526 |
scale = scale % 5; |
| 527 |
float fscale = (float)pow(2.0f, scale); |
| 528 |
SkDebugf("Scaling factor: %f\n", fscale); |
| 529 |
|
| 530 |
// We have 5 different modes of decoding. |
| 531 |
mode = mode % 5; |
| 532 |
SkDebugf("Mode: %d\n", mode); |
| 533 |
|
| 534 |
// This is mostly copied from DMSrcSink's CodecSrc::draw method. |
| 535 |
SkDebugf("Decoding\n"); |
| 536 |
std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(bytes)); |
| 537 |
if (nullptr == codec) { |
| 538 |
SkDebugf("[terminated] Couldn't create codec.\n"); |
| 539 |
return; |
| 540 |
} |
| 541 |
|
| 542 |
SkImageInfo decodeInfo = codec->getInfo(); |
| 543 |
SkISize size = codec->getScaledDimensions(fscale); |
| 544 |
decodeInfo = decodeInfo.makeDimensions(size); |
| 545 |
|
| 546 |
SkBitmap bitmap; |
| 547 |
SkCodec::Options options; |
| 548 |
options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 549 |
|
| 550 |
if (!bitmap.tryAllocPixelsFlags(decodeInfo, SkBitmap::kZeroPixels_AllocFlag)) { |
| 551 |
SkDebugf("[terminated] Could not allocate memory. Image might be too large (%d x %d)", |
| 552 |
decodeInfo.width(), decodeInfo.height()); |
| 553 |
return; |
| 554 |
} |
| 555 |
|
| 556 |
switch (mode) { |
| 557 |
case 0: {//kCodecZeroInit_Mode, kCodec_Mode |
| 558 |
switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), &options)) { |
| 559 |
case SkCodec::kSuccess: |
| 560 |
SkDebugf("[terminated] Success!\n"); |
| 561 |
break; |
| 562 |
case SkCodec::kIncompleteInput: |
| 563 |
SkDebugf("[terminated] Partial Success\n"); |
| 564 |
break; |
| 565 |
case SkCodec::kErrorInInput: |
| 566 |
SkDebugf("[terminated] Partial Success with error\n"); |
| 567 |
break; |
| 568 |
case SkCodec::kInvalidConversion: |
| 569 |
SkDebugf("Incompatible colortype conversion\n"); |
| 570 |
// Crash to allow afl-fuzz to know this was a bug. |
| 571 |
raise(SIGSEGV); |
| 572 |
break; |
| 573 |
default: |
| 574 |
SkDebugf("[terminated] Couldn't getPixels.\n"); |
| 575 |
return; |
| 576 |
} |
| 577 |
break; |
| 578 |
} |
| 579 |
case 1: {//kScanline_Mode |
| 580 |
if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo)) { |
| 581 |
SkDebugf("[terminated] Could not start scanline decoder\n"); |
| 582 |
return; |
| 583 |
} |
| 584 |
|
| 585 |
void* dst = bitmap.getAddr(0, 0); |
| 586 |
size_t rowBytes = bitmap.rowBytes(); |
| 587 |
uint32_t height = decodeInfo.height(); |
| 588 |
// We do not need to check the return value. On an incomplete |
| 589 |
// image, memory will be filled with a default value. |
| 590 |
codec->getScanlines(dst, height, rowBytes); |
| 591 |
SkDebugf("[terminated] Success!\n"); |
| 592 |
break; |
| 593 |
} |
| 594 |
case 2: { //kStripe_Mode |
| 595 |
const int height = decodeInfo.height(); |
| 596 |
// This value is chosen arbitrarily. We exercise more cases by choosing a value that |
| 597 |
// does not align with image blocks. |
| 598 |
const int stripeHeight = 37; |
| 599 |
const int numStripes = (height + stripeHeight - 1) / stripeHeight; |
| 600 |
|
| 601 |
// Decode odd stripes |
| 602 |
if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo) |
| 603 |
|| SkCodec::kTopDown_SkScanlineOrder != codec->getScanlineOrder()) { |
| 604 |
// This mode was designed to test the new skip scanlines API in libjpeg-turbo. |
| 605 |
// Jpegs have kTopDown_SkScanlineOrder, and at this time, it is not interesting |
| 606 |
// to run this test for image types that do not have this scanline ordering. |
| 607 |
SkDebugf("[terminated] Could not start top-down scanline decoder\n"); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
for (int i = 0; i < numStripes; i += 2) { |
| 612 |
// Skip a stripe |
| 613 |
const int linesToSkip = std::min(stripeHeight, height - i * stripeHeight); |
| 614 |
codec->skipScanlines(linesToSkip); |
| 615 |
|
| 616 |
// Read a stripe |
| 617 |
const int startY = (i + 1) * stripeHeight; |
| 618 |
const int linesToRead = std::min(stripeHeight, height - startY); |
| 619 |
if (linesToRead > 0) { |
| 620 |
codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
// Decode even stripes |
| 625 |
const SkCodec::Result startResult = codec->startScanlineDecode(decodeInfo); |
| 626 |
if (SkCodec::kSuccess != startResult) { |
| 627 |
SkDebugf("[terminated] Failed to restart scanline decoder with same parameters.\n"); |
| 628 |
return; |
| 629 |
} |
| 630 |
for (int i = 0; i < numStripes; i += 2) { |
| 631 |
// Read a stripe |
| 632 |
const int startY = i * stripeHeight; |
| 633 |
const int linesToRead = std::min(stripeHeight, height - startY); |
| 634 |
codec->getScanlines(bitmap.getAddr(0, startY), linesToRead, bitmap.rowBytes()); |
| 635 |
|
| 636 |
// Skip a stripe |
| 637 |
const int linesToSkip = std::min(stripeHeight, height - (i + 1) * stripeHeight); |
| 638 |
if (linesToSkip > 0) { |
| 639 |
codec->skipScanlines(linesToSkip); |
| 640 |
} |
| 641 |
} |
| 642 |
SkDebugf("[terminated] Success!\n"); |
| 643 |
break; |
| 644 |
} |
| 645 |
case 3: { //kSubset_Mode |
| 646 |
// Arbitrarily choose a divisor. |
| 647 |
int divisor = 2; |
| 648 |
// Total width/height of the image. |
| 649 |
const int W = codec->getInfo().width(); |
| 650 |
const int H = codec->getInfo().height(); |
| 651 |
if (divisor > W || divisor > H) { |
| 652 |
SkDebugf("[terminated] Cannot codec subset: divisor %d is too big " |
| 653 |
"with dimensions (%d x %d)\n", divisor, W, H); |
| 654 |
return; |
| 655 |
} |
| 656 |
// subset dimensions |
| 657 |
// SkWebpCodec, the only one that supports subsets, requires even top/left boundaries. |
| 658 |
const int w = SkAlign2(W / divisor); |
| 659 |
const int h = SkAlign2(H / divisor); |
| 660 |
SkIRect subset; |
| 661 |
SkCodec::Options opts; |
| 662 |
opts.fSubset = ⊂ |
| 663 |
SkBitmap subsetBm; |
| 664 |
// We will reuse pixel memory from bitmap. |
| 665 |
void* pixels = bitmap.getPixels(); |
| 666 |
for (int x = 0; x < W; x += w) { |
| 667 |
for (int y = 0; y < H; y+= h) { |
| 668 |
// Do not make the subset go off the edge of the image. |
| 669 |
const int preScaleW = std::min(w, W - x); |
| 670 |
const int preScaleH = std::min(h, H - y); |
| 671 |
subset.setXYWH(x, y, preScaleW, preScaleH); |
| 672 |
// And fscale |
| 673 |
// FIXME: Should we have a version of getScaledDimensions that takes a subset |
| 674 |
// into account? |
| 675 |
decodeInfo = decodeInfo.makeWH( |
| 676 |
std::max(1, SkScalarRoundToInt(preScaleW * fscale)), |
| 677 |
std::max(1, SkScalarRoundToInt(preScaleH * fscale))); |
| 678 |
size_t rowBytes = decodeInfo.minRowBytes(); |
| 679 |
if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes)) { |
| 680 |
SkDebugf("[terminated] Could not install pixels.\n"); |
| 681 |
return; |
| 682 |
} |
| 683 |
const SkCodec::Result result = codec->getPixels(decodeInfo, pixels, rowBytes, |
| 684 |
&opts); |
| 685 |
switch (result) { |
| 686 |
case SkCodec::kSuccess: |
| 687 |
case SkCodec::kIncompleteInput: |
| 688 |
case SkCodec::kErrorInInput: |
| 689 |
SkDebugf("okay\n"); |
| 690 |
break; |
| 691 |
case SkCodec::kInvalidConversion: |
| 692 |
if (0 == (x|y)) { |
| 693 |
// First subset is okay to return unimplemented. |
| 694 |
SkDebugf("[terminated] Incompatible colortype conversion\n"); |
| 695 |
return; |
| 696 |
} |
| 697 |
// If the first subset succeeded, a later one should not fail. |
| 698 |
[[fallthrough]]; |
| 699 |
case SkCodec::kUnimplemented: |
| 700 |
if (0 == (x|y)) { |
| 701 |
// First subset is okay to return unimplemented. |
| 702 |
SkDebugf("[terminated] subset codec not supported\n"); |
| 703 |
return; |
| 704 |
} |
| 705 |
// If the first subset succeeded, why would a later one fail? |
| 706 |
[[fallthrough]]; |
| 707 |
default: |
| 708 |
SkDebugf("[terminated] subset codec failed to decode (%d, %d, %d, %d) " |
| 709 |
"with dimensions (%d x %d)\t error %d\n", |
| 710 |
x, y, decodeInfo.width(), decodeInfo.height(), |
| 711 |
W, H, result); |
| 712 |
return; |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
SkDebugf("[terminated] Success!\n"); |
| 717 |
break; |
| 718 |
} |
| 719 |
case 4: { //kAnimated_Mode |
| 720 |
std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo(); |
| 721 |
if (frameInfos.size() == 0) { |
| 722 |
SkDebugf("[terminated] Not an animated image\n"); |
| 723 |
break; |
| 724 |
} |
| 725 |
|
| 726 |
for (size_t i = 0; i < frameInfos.size(); i++) { |
| 727 |
options.fFrameIndex = i; |
| 728 |
auto result = codec->startIncrementalDecode(decodeInfo, bitmap.getPixels(), |
| 729 |
bitmap.rowBytes(), &options); |
| 730 |
if (SkCodec::kSuccess != result) { |
| 731 |
SkDebugf("[terminated] failed to start incremental decode " |
| 732 |
"in frame %zu with error %d\n", i, result); |
| 733 |
return; |
| 734 |
} |
| 735 |
|
| 736 |
result = codec->incrementalDecode(); |
| 737 |
if (result == SkCodec::kIncompleteInput || result == SkCodec::kErrorInInput) { |
| 738 |
SkDebugf("okay\n"); |
| 739 |
// Frames beyond this one will not decode. |
| 740 |
break; |
| 741 |
} |
| 742 |
if (result == SkCodec::kSuccess) { |
| 743 |
SkDebugf("okay - decoded frame %zu\n", i); |
| 744 |
} else { |
| 745 |
SkDebugf("[terminated] incremental decode failed with " |
| 746 |
"error %d\n", result); |
| 747 |
return; |
| 748 |
} |
| 749 |
} |
| 750 |
SkDebugf("[terminated] Success!\n"); |
| 751 |
break; |
| 752 |
} |
| 753 |
default: |
| 754 |
SkDebugf("[terminated] Mode not implemented yet\n"); |
| 755 |
} |
| 756 |
|
| 757 |
dump_png(bitmap); |
| 758 |
} |
| 759 |
|
| 760 |
void FuzzSKP(const uint8_t *data, size_t size); |
| 761 |
|
| 762 |
static void fuzz_skp(const sk_sp<SkData>& data) { |
| 763 |
FuzzSKP(data->bytes(), data->size()); |
| 764 |
SkDebugf("[terminated] Finished SKP\n"); |
| 765 |
} |
| 766 |
|
| 767 |
void FuzzColorspace(const uint8_t *data, size_t size); |
| 768 |
|
| 769 |
static void fuzz_color_deserialize(const sk_sp<SkData>& data) { |
| 770 |
FuzzColorspace(data->bytes(), data->size()); |
| 771 |
SkDebugf("[terminated] Finished SkColorspace\n"); |
| 772 |
} |
| 773 |
|
| 774 |
void FuzzParsePath(const uint8_t *data, size_t size); |
| 775 |
|
| 776 |
static void fuzz_parse_path(const sk_sp<SkData>& data) { |
| 777 |
FuzzParsePath(data->bytes(), data->size()); |
| 778 |
SkDebugf("[terminated] parse_path didn't crash!\n"); |
| 779 |
} |
| 780 |
|
| 781 |
void FuzzPathDeserialize(const uint8_t *data, size_t size); |
| 782 |
|
| 783 |
static void fuzz_path_deserialize(const sk_sp<SkData>& data) { |
| 784 |
FuzzPathDeserialize(data->bytes(), data->size()); |
| 785 |
SkDebugf("[terminated] path_deserialize didn't crash!\n"); |
| 786 |
} |
| 787 |
|
| 788 |
bool FuzzRegionDeserialize(const uint8_t *data, size_t size); |
| 789 |
|
| 790 |
static void fuzz_region_deserialize(const sk_sp<SkData>& data) { |
| 791 |
if (!FuzzRegionDeserialize(data->bytes(), data->size())) { |
| 792 |
SkDebugf("[terminated] Couldn't initialize SkRegion.\n"); |
| 793 |
return; |
| 794 |
} |
| 795 |
SkDebugf("[terminated] Success! Initialized SkRegion.\n"); |
| 796 |
} |
| 797 |
|
| 798 |
void FuzzTextBlobDeserialize(const uint8_t *data, size_t size); |
| 799 |
|
| 800 |
static void fuzz_textblob_deserialize(const sk_sp<SkData>& data) { |
| 801 |
FuzzTextBlobDeserialize(data->bytes(), data->size()); |
| 802 |
SkDebugf("[terminated] textblob didn't crash!\n"); |
| 803 |
} |
| 804 |
|
| 805 |
void FuzzRegionSetPath(Fuzz* fuzz); |
| 806 |
|
| 807 |
static void fuzz_region_set_path(const sk_sp<SkData>& data) { |
| 808 |
Fuzz fuzz(data->bytes(), data->size()); |
| 809 |
FuzzRegionSetPath(&fuzz); |
| 810 |
SkDebugf("[terminated] region_set_path didn't crash!\n"); |
| 811 |
} |
| 812 |
|
| 813 |
void FuzzImageFilterDeserialize(const uint8_t *data, size_t size); |
| 814 |
|
| 815 |
static void fuzz_filter_fuzz(const sk_sp<SkData>& data) { |
| 816 |
FuzzImageFilterDeserialize(data->bytes(), data->size()); |
| 817 |
SkDebugf("[terminated] filter_fuzz didn't crash!\n"); |
| 818 |
} |
| 819 |
|
| 820 |
void FuzzSkMeshSpecification(const uint8_t *fuzzData, size_t fuzzSize); |
| 821 |
|
| 822 |
static void fuzz_skmeshspecification(const sk_sp<SkData>& data) { |
| 823 |
FuzzSkMeshSpecification(data->bytes(), data->size()); |
| 824 |
SkDebugf("[terminated] SkMeshSpecification::Make didn't crash!\n"); |
| 825 |
} |
| 826 |
|
| 827 |
bool FuzzSkRuntimeBlender(const uint8_t *data, size_t size); |
| 828 |
|
| 829 |
static void fuzz_skruntimeblender(const sk_sp<SkData>& data) { |
| 830 |
if (FuzzSkRuntimeBlender(data->bytes(), data->size())) { |
| 831 |
SkDebugf("[terminated] Success! Compiled and executed SkSL blender.\n"); |
| 832 |
} else { |
| 833 |
SkDebugf("[terminated] Could not compile or execute SkSL blender.\n"); |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
bool FuzzSkRuntimeColorFilter(const uint8_t *data, size_t size); |
| 838 |
|
| 839 |
static void fuzz_skruntimecolorfilter(const sk_sp<SkData>& data) { |
| 840 |
if (FuzzSkRuntimeColorFilter(data->bytes(), data->size())) { |
| 841 |
SkDebugf("[terminated] Success! Compiled and executed SkSL color filter.\n"); |
| 842 |
} else { |
| 843 |
SkDebugf("[terminated] Could not compile or execute SkSL color filter.\n"); |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
bool FuzzSkRuntimeEffect(const uint8_t *data, size_t size); |
| 848 |
|
| 849 |
static void fuzz_skruntimeeffect(const sk_sp<SkData>& data) { |
| 850 |
if (FuzzSkRuntimeEffect(data->bytes(), data->size())) { |
| 851 |
SkDebugf("[terminated] Success! Compiled and executed SkSL shader.\n"); |
| 852 |
} else { |
| 853 |
SkDebugf("[terminated] Could not compile or execute SkSL shader.\n"); |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
bool FuzzSKSL2GLSL(const uint8_t *data, size_t size); |
| 858 |
|
| 859 |
static void fuzz_sksl2glsl(const sk_sp<SkData>& data) { |
| 860 |
if (FuzzSKSL2GLSL(data->bytes(), data->size())) { |
| 861 |
SkDebugf("[terminated] Success! Compiled input to GLSL.\n"); |
| 862 |
} else { |
| 863 |
SkDebugf("[terminated] Could not compile input to GLSL.\n"); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
bool FuzzSKSL2Metal(const uint8_t *data, size_t size); |
| 868 |
|
| 869 |
static void fuzz_sksl2metal(const sk_sp<SkData>& data) { |
| 870 |
if (FuzzSKSL2Metal(data->bytes(), data->size())) { |
| 871 |
SkDebugf("[terminated] Success! Compiled input to Metal.\n"); |
| 872 |
} else { |
| 873 |
SkDebugf("[terminated] Could not compile input to Metal.\n"); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
bool FuzzSKSL2Pipeline(const uint8_t *data, size_t size); |
| 878 |
|
| 879 |
static void fuzz_sksl2pipeline(const sk_sp<SkData>& data) { |
| 880 |
if (FuzzSKSL2Pipeline(data->bytes(), data->size())) { |
| 881 |
SkDebugf("[terminated] Success! Compiled input to pipeline stage.\n"); |
| 882 |
} else { |
| 883 |
SkDebugf("[terminated] Could not compile input to pipeline stage.\n"); |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
bool FuzzSKSL2SPIRV(const uint8_t *data, size_t size); |
| 888 |
|
| 889 |
static void fuzz_sksl2spirv(const sk_sp<SkData>& data) { |
| 890 |
if (FuzzSKSL2SPIRV(data->bytes(), data->size())) { |
| 891 |
SkDebugf("[terminated] Success! Compiled input to SPIR-V.\n"); |
| 892 |
} else { |
| 893 |
SkDebugf("[terminated] Could not compile input to SPIR-V.\n"); |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
bool FuzzSKSL2WGSL(const uint8_t *data, size_t size); |
| 898 |
|
| 899 |
static void fuzz_sksl2wgsl(const sk_sp<SkData>& data) { |
| 900 |
if (FuzzSKSL2WGSL(data->bytes(), data->size())) { |
| 901 |
SkDebugf("[terminated] Success! Compiled input to WGSL.\n"); |
| 902 |
} else { |
| 903 |
SkDebugf("[terminated] Could not compile input to WGSL.\n"); |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
void FuzzSkDescriptorDeserialize(const uint8_t *data, size_t size); |
| 908 |
|
| 909 |
static void fuzz_skdescriptor_deserialize(const sk_sp<SkData>& data) { |
| 910 |
FuzzSkDescriptorDeserialize(data->bytes(), data->size()); |
| 911 |
SkDebugf("[terminated] Did not crash while deserializing an SkDescriptor.\n"); |
| 912 |
} |