enc_dec_fuzzer: use WebPDecode()

rather than WebPDecodeRGBA(). This allows finer grained error detection
and avoids an abort() when running under the nallocfuzz engine.

Change-Id: I8ff37f2fe7e1c8b39bd4a8bfe7b26ac41149ba42
This commit is contained in:
James Zern 2023-07-18 23:02:24 +00:00
parent 0fcb311cfc
commit 0081693d61

View File

@ -100,44 +100,61 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) {
} }
// Try decoding the result. // Try decoding the result.
int w, h;
const uint8_t* const out_data = memory_writer.mem; const uint8_t* const out_data = memory_writer.mem;
const size_t out_size = memory_writer.size; const size_t out_size = memory_writer.size;
uint8_t* const rgba = WebPDecodeBGRA(out_data, out_size, &w, &h); WebPDecoderConfig dec_config;
if (rgba == nullptr || w != pic.width || h != pic.height) { if (!WebPInitDecoderConfig(&dec_config)) {
fprintf(stderr, "WebPDecodeBGRA failed.\n"); fprintf(stderr, "WebPInitDecoderConfig failed.\n");
WebPFree(rgba);
WebPMemoryWriterClear(&memory_writer); WebPMemoryWriterClear(&memory_writer);
WebPPictureFree(&pic); WebPPictureFree(&pic);
abort(); abort();
} }
// Compare the results if exact encoding. dec_config.output.colorspace = MODE_BGRA;
if (pic.use_argb && config.lossless && config.near_lossless == 100) { const VP8StatusCode status = WebPDecode(out_data, out_size, &dec_config);
const uint32_t* src1 = (const uint32_t*)rgba; if ((status != VP8_STATUS_OK && status != VP8_STATUS_OUT_OF_MEMORY &&
const uint32_t* src2 = pic.argb; status != VP8_STATUS_USER_ABORT) ||
for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) { (status == VP8_STATUS_OK && (dec_config.output.width != pic.width ||
for (int x = 0; x < w; ++x) { dec_config.output.height != pic.height))) {
uint32_t v1 = src1[x], v2 = src2[x]; fprintf(stderr, "WebPDecode failed. status: %d.\n", status);
if (!config.exact) { WebPFreeDecBuffer(&dec_config.output);
if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) { WebPMemoryWriterClear(&memory_writer);
// Only keep alpha for comparison of fully transparent area. WebPPictureFree(&pic);
v1 &= 0xff000000u; abort();
v2 &= 0xff000000u; }
if (status == VP8_STATUS_OK) {
const uint8_t* const rgba = dec_config.output.u.RGBA.rgba;
const int w = dec_config.output.width;
const int h = dec_config.output.height;
// Compare the results if exact encoding.
if (pic.use_argb && config.lossless && config.near_lossless == 100) {
const uint32_t* src1 = (const uint32_t*)rgba;
const uint32_t* src2 = pic.argb;
for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) {
for (int x = 0; x < w; ++x) {
uint32_t v1 = src1[x], v2 = src2[x];
if (!config.exact) {
if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) {
// Only keep alpha for comparison of fully transparent area.
v1 &= 0xff000000u;
v2 &= 0xff000000u;
}
}
if (v1 != v2) {
fprintf(stderr, "Lossless compression failed pixel-exactness.\n");
WebPFreeDecBuffer(&dec_config.output);
WebPMemoryWriterClear(&memory_writer);
WebPPictureFree(&pic);
abort();
} }
}
if (v1 != v2) {
fprintf(stderr, "Lossless compression failed pixel-exactness.\n");
WebPFree(rgba);
WebPMemoryWriterClear(&memory_writer);
WebPPictureFree(&pic);
abort();
} }
} }
} }
} }
WebPFree(rgba); WebPFreeDecBuffer(&dec_config.output);
WebPMemoryWriterClear(&memory_writer); WebPMemoryWriterClear(&memory_writer);
WebPPictureFree(&pic); WebPPictureFree(&pic);
return 0; return 0;