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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/*
* Copyright (C) 2022 libass contributors
*
* This file is part of libass.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include "ass.h"
#include "ass_types.h"
#define FUZZMODE_STANDALONE 0
#define FUZZMODE_AFLXX_SHAREDMEM 1
#define FUZZMODE_LIBFUZZER 2
#ifndef ASS_FUZZMODE
#define ASS_FUZZMODE FUZZMODE_STANDALONE
#endif
// Limit maximal processed size in continuous fuzzing;
// to be used when the fuzzing setup doesn't expose its own max size control.
// Has no effect in standalone builds
#ifdef ASSFUZZ_MAX_LEN
#define LEN_IN_RANGE(s) ((s) <= (ASSFUZZ_MAX_LEN))
#else
#define LEN_IN_RANGE(s) true
#endif
// MSAN: will trigger MSAN if any pixel in bitmap not written to (costly)
#ifndef ASSFUZZ_HASH_WHOLEBITMAP
#define ASSFUZZ_HASH_WHOLEBITMAP 0
#endif
ASS_Library *ass_library = NULL;
ASS_Renderer *ass_renderer = NULL;
bool quiet = false;
uint8_t hval = 0;
#if ASSFUZZ_HASH_WHOLEBITMAP
static inline void hash(const void *buf, size_t len)
{
const uint8_t *ptr = buf;
const uint8_t *end = ptr + len;
while (ptr < end)
hval ^= *ptr++;
// MSAN doesn't trigger on the XORs, but will on conditional branches
if (hval)
hval ^= 57;
}
#endif
void msg_callback(int level, const char *fmt, va_list va, void *data)
{
#if ASS_FUZZMODE == FUZZMODE_STANDALONE
if (level > 6 || quiet) return;
printf("libass: ");
vprintf(fmt, va);
printf("\n");
#else
// still check for type-mismatches even when not printing
// (seems to be cheap enough from some simple perormance tests)
char msg[2048];
int l = vsnprintf(msg, sizeof(msg), fmt, va) - 1;
l = l >= sizeof(msg) ? sizeof(msg) - 1 : l;
l = l < 0 ? 0 : l;
hval ^= *(msg + l);
#endif
}
static const int RWIDTH = 854;
static const int RHEIGHT = 480;
static bool init_renderer(void)
{
if (ass_renderer)
return true;
ass_renderer = ass_renderer_init(ass_library);
if (!ass_renderer)
return false;
ass_set_fonts(ass_renderer, NULL, "sans-serif",
ASS_FONTPROVIDER_AUTODETECT, NULL, 1);
ass_set_frame_size(ass_renderer, RWIDTH, RHEIGHT);
ass_set_storage_size(ass_renderer, RWIDTH, RHEIGHT);
return true;
}
static bool init(void)
{
ass_library = ass_library_init();
if (!ass_library) {
printf("ass_library_init failed!\n");
return false;
}
ass_set_message_cb(ass_library, msg_callback, NULL);
if (!init_renderer()) {
ass_library_done(ass_library);
ass_library = NULL;
printf("ass_renderer_init failed!\n");
return false;
}
return true;
}
static inline void process_image(ASS_Image *imgs)
{
for (; imgs; imgs = imgs->next) {
assert(imgs->w >= 0 && imgs->h >= 0 &&
imgs->dst_x >= 0 && imgs->dst_y >= 0 &&
imgs->dst_x + imgs->w <= RWIDTH &&
imgs->dst_y + imgs->h <= RHEIGHT &&
imgs->stride >= imgs->w);
#if !ASSFUZZ_HASH_WHOLEBITMAP
// Check last pixel to probe for out-of-bounds errors
if (imgs->w && imgs->h)
hval ^= *(imgs->bitmap + imgs->stride * (imgs->h - 1) + imgs->w - 1);
#else
unsigned char *src = imgs->bitmap;
for (int y = 0; y < imgs->h; ++y) {
hash(src, imgs->w);
src += imgs->stride;
}
#endif
}
}
static void consume_track(ASS_Renderer *renderer, ASS_Track *track)
{
for (int n = 0; n < track->n_events; ++n) {
int change;
ASS_Event event = track->events[n];
process_image(ass_render_frame(ass_renderer, track, event.Start, &change));
if (event.Duration > 1) {
process_image(ass_render_frame(ass_renderer, track, event.Start + event.Duration/2, &change));
process_image(ass_render_frame(ass_renderer, track, event.Start + event.Duration-1, &change));
}
}
}
#if ASS_FUZZMODE == FUZZMODE_STANDALONE
#include "writeout.h"
struct settings {
enum {
CONSUME_INPUT,
WRITEOUT_TRACK
} mode;
const char *input; // path or "-" for stdin
const char *output; // path or NULL for tmp file
};
#ifdef _WIN32
#define READ_RET int
#else
#define READ_RET ssize_t
#endif
static ASS_Track *read_track_from_stdin(void)
{
size_t smax = 4096;
char *buf = malloc(smax);
if (!buf)
goto error;
size_t s = 0;
READ_RET read_b = 0;
do {
// AFL++ docs recommend using raw file descriptors
// to avoid buffering issues with stdin
read_b = read(fileno(stdin), buf + s, smax - s);
s += read_b > 0 ? read_b : 0;
if (s == smax) {
size_t new_smax = smax > SIZE_MAX / 2 ? SIZE_MAX : smax * 2;
char *new_buf = realloc(buf, new_smax);
if (!new_buf || new_smax <= smax) {
free(new_buf ? new_buf : buf);
goto error;
}
smax = new_smax;
buf = new_buf;
}
} while (read_b > 0);
buf[s] = '\0';
ASS_Track *track = ass_read_memory(ass_library, buf, s, NULL);
free(buf);
return track;
error:
printf("Input too large!\n");
return NULL;
}
/**
* \param argc
* \param argv
* \param settings will be filled according to parsed args or defaults
* \return whether CLI args could be parsed successfully
*/
static bool parse_cmdline(int argc, char *argv[], struct settings *settings)
{
// defaults
settings->mode = CONSUME_INPUT;
settings->input = NULL;
settings->output = NULL;
int i;
for (i = 1; i < argc; i++) {
const char *param = argv[i];
if (!param || param[0] != '-' || !param[1])
goto no_more_args;
switch (param[1]) {
case 'q':
quiet = true;
break;
case 'o':
settings->mode = WRITEOUT_TRACK;
// optional argument
if (argc - i > 1 && argv[i + 1][0] != '-') {
settings->output = argv[i + 1];
i++;
}
break;
case '-':
if (param[2]) {
return false;
} else {
i++;
goto no_more_args;
}
default:
return false;
}
continue;
no_more_args:
break;
}
if (argc < 2 || argc - i > 1 || argc == i)
return false;
settings->input = argv[argc - 1];
return !!settings->input;
}
int main(int argc, char *argv[])
{
/* Default failure code of sanitisers is 1, unless
* changed via env (A|UB|..)SAN_OPTIONS=exitcode=21
* Except, LLVM's UBSAN always exits with 0 (unless using
* -fsanitize-undefined-trap-on-error which will SIGILL without an
* error report being printed), see https://reviews.llvm.org/D35085
*/
enum {
FUZZ_OK = 0,
//SANITISER_FAIL = 1,
// Invalid parameters passed etc
FUZZ_BAD_USAGE = 2,
// Error before rendering starts
FUZZ_INIT_ERR = 0
};
ASS_Track *track = NULL;
int retval = FUZZ_OK;
struct settings settings;
if (!parse_cmdline(argc, argv, &settings)) {
printf("usage: %s [-q] [-o [output_file]] [--] <subtitle file>\n"
" -q:\n"
" Hide libass log messages\n"
"\n"
" -o [FILE]:\n"
" Write out parsed file content in a standardized form\n"
" into FILE or if omitted a generated temporary file.\n"
" If used the input file will not be processed, only parsed.\n",
argc ? argv[0] : "fuzz");
return FUZZ_BAD_USAGE;
}
if (!init()) {
printf("library init failed!\n");
retval = FUZZ_INIT_ERR;
goto cleanup;
}
if (strcmp(settings.input, "-"))
track = ass_read_file(ass_library, settings.input, NULL);
else
track = read_track_from_stdin();
if (!track) {
printf("track init failed!\n");
retval = FUZZ_INIT_ERR;
goto cleanup;
}
switch (settings.mode) {
case CONSUME_INPUT:
consume_track(ass_renderer, track);
break;
case WRITEOUT_TRACK:
write_out_track(track, settings.output);
break;
}
cleanup:
if (track) ass_free_track(track);
if (ass_renderer) ass_renderer_done(ass_renderer);
if (ass_library) ass_library_done(ass_library);
return retval;
}
#elif ASS_FUZZMODE == FUZZMODE_AFLXX_SHAREDMEM
__AFL_FUZZ_INIT();
/*
* AFL++ docs recommend to disable optimisation for the main function
* and GCC and Clang are the only AFL compilers.
*/
#pragma clang optimize off
#pragma GCC optimize("O0")
int main(int argc, char *argv[])
{
// AFLs buffer and length macros should not be used directly
ssize_t len;
unsigned char *buf;
if (!init()) {
printf("library init failed!\n");
return 1;
}
__AFL_INIT();
buf = __AFL_FUZZ_TESTCASE_BUF;
while (__AFL_LOOP(100000)) {
len = __AFL_FUZZ_TESTCASE_LEN;
if (!LEN_IN_RANGE(len))
continue;
if (!init_renderer()) {
printf("Failing renderer init, skipping a sample!\n");
continue;
}
ASS_Track *track = ass_read_memory(ass_library, (char *)buf, len, NULL);
if (!track)
continue;
consume_track(ass_renderer, track);
ass_free_track(track);
ass_renderer_done(ass_renderer);
ass_renderer = NULL;
ass_clear_fonts(ass_library);
}
ass_renderer_done(ass_renderer);
ass_library_done(ass_library);
return 0;
}
#elif ASS_FUZZMODE == FUZZMODE_LIBFUZZER
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
// OSS Fuzz docs recommend just returning 0 on too large input
// libFuzzer docs tell us to use -1 to prevent addition to corpus
// BUT HongFuzz' LLVMFuzzerTestOneInput hook can't handle it and
// neither does OSS Fuzz convert this in their wrapper, see:
// https://github.com/google/oss-fuzz/issues/11983
if (!LEN_IN_RANGE(size))
return 0;
ASS_Track *track = NULL;
// All return values but zero and -1 are reserved
if (!init())
return 0;
track = ass_read_memory(ass_library, (char *)data, size, NULL);
if (track) {
consume_track(ass_renderer, track);
ass_free_track(track);
}
ass_renderer_done(ass_renderer);
ass_library_done(ass_library);
ass_renderer = NULL;
ass_library = NULL;
return 0;
}
#else
#error Unknown fuzzer mode selected!
#endif
|