| 1 |
/* |
| 2 |
* HEIF codec. |
| 3 |
* Copyright (c) 2017 struktur AG, Joachim Bauch <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of libheif. |
| 6 |
* |
| 7 |
* libheif is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of |
| 10 |
* the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* libheif is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with libheif. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <assert.h> |
| 22 |
#include <limits.h> |
| 23 |
#include <stdlib.h> |
| 24 |
|
| 25 |
#include "libheif/heif.h" |
| 26 |
|
| 27 |
static const enum heif_colorspace kFuzzColorSpace = heif_colorspace_YCbCr; |
| 28 |
static const enum heif_chroma kFuzzChroma = heif_chroma_420; |
| 29 |
|
| 30 |
static void TestDecodeImage(struct heif_context* ctx, |
| 31 |
const struct heif_image_handle* handle, size_t filesize) |
| 32 |
{ |
| 33 |
struct heif_image* image = nullptr; |
| 34 |
struct heif_error err; |
| 35 |
|
| 36 |
bool primary = heif_image_handle_is_primary_image(handle); |
| 37 |
(void) primary; |
| 38 |
int width = heif_image_handle_get_width(handle); |
| 39 |
int height = heif_image_handle_get_height(handle); |
| 40 |
(void)width; (void)height; |
| 41 |
assert(width >= 0); |
| 42 |
assert(height >= 0); |
| 43 |
int metadata_count = heif_image_handle_get_number_of_metadata_blocks(handle, nullptr); |
| 44 |
assert(metadata_count >= 0); |
| 45 |
assert(static_cast<size_t>(metadata_count) < filesize / sizeof(heif_item_id)); |
| 46 |
heif_item_id* metadata_ids = static_cast<heif_item_id*>(malloc(metadata_count * sizeof(heif_item_id))); |
| 47 |
assert(metadata_ids); |
| 48 |
int metadata_ids_count = heif_image_handle_get_list_of_metadata_block_IDs(handle, nullptr, metadata_ids, |
| 49 |
metadata_count); |
| 50 |
assert(metadata_count == metadata_ids_count); |
| 51 |
(void)metadata_ids_count; |
| 52 |
for (int i = 0; i < metadata_count; i++) { |
| 53 |
heif_image_handle_get_metadata_type(handle, metadata_ids[i]); |
| 54 |
heif_image_handle_get_metadata_content_type(handle, metadata_ids[i]); |
| 55 |
size_t metadata_size = heif_image_handle_get_metadata_size(handle, metadata_ids[i]); |
| 56 |
|
| 57 |
// This assertion is invalid. Metadata can in fact be larger than the file if there are several |
| 58 |
// overlapping iloc extents. Does not make much sense, but it is technically valid. |
| 59 |
//assert(metadata_size < filesize); |
| 60 |
|
| 61 |
uint8_t* metadata_data = static_cast<uint8_t*>(malloc(metadata_size)); |
| 62 |
assert(metadata_data); |
| 63 |
heif_image_handle_get_metadata(handle, metadata_ids[i], metadata_data); |
| 64 |
free(metadata_data); |
| 65 |
} |
| 66 |
free(metadata_ids); |
| 67 |
|
| 68 |
err = heif_decode_image(handle, &image, kFuzzColorSpace, kFuzzChroma, nullptr); |
| 69 |
if (err.code != heif_error_Ok) { |
| 70 |
heif_image_release(image); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
assert(heif_image_get_colorspace(image) == kFuzzColorSpace); |
| 75 |
assert(heif_image_get_chroma_format(image) == kFuzzChroma); |
| 76 |
|
| 77 |
// TODO(fancycode): Should we also check the planes? |
| 78 |
|
| 79 |
heif_image_release(image); |
| 80 |
} |
| 81 |
|
| 82 |
static int clip_int(size_t size) |
| 83 |
{ |
| 84 |
return size > INT_MAX ? INT_MAX : static_cast<int>(size); |
| 85 |
} |
| 86 |
|
| 87 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
| 88 |
{ |
| 89 |
struct heif_context* ctx; |
| 90 |
struct heif_error err; |
| 91 |
struct heif_image_handle* primary_handle = nullptr; |
| 92 |
int images_count; |
| 93 |
heif_item_id* image_IDs = NULL; |
| 94 |
bool explicit_init = size == 0 || data[size - 1] & 1; |
| 95 |
|
| 96 |
if (explicit_init) { |
| 97 |
heif_init(nullptr); |
| 98 |
} |
| 99 |
|
| 100 |
heif_check_filetype(data, clip_int(size)); |
| 101 |
heif_main_brand(data, clip_int(size)); |
| 102 |
heif_get_file_mime_type(data, clip_int(size)); |
| 103 |
|
| 104 |
ctx = heif_context_alloc(); |
| 105 |
heif_context_get_security_limits(ctx)->max_total_memory = UINT64_C(2) * 1024 * 1024 * 1024; |
| 106 |
assert(ctx); |
| 107 |
|
| 108 |
auto* limits = heif_context_get_security_limits(ctx); |
| 109 |
limits->max_memory_block_size = 128 * 1024 * 1024; // 128 MB |
| 110 |
|
| 111 |
err = heif_context_read_from_memory(ctx, data, size, nullptr); |
| 112 |
if (err.code != heif_error_Ok) { |
| 113 |
// Not a valid HEIF file passed (which is most likely while fuzzing). |
| 114 |
goto quit; |
| 115 |
} |
| 116 |
|
| 117 |
err = heif_context_get_primary_image_handle(ctx, &primary_handle); |
| 118 |
if (err.code == heif_error_Ok) { |
| 119 |
assert(heif_image_handle_is_primary_image(primary_handle)); |
| 120 |
TestDecodeImage(ctx, primary_handle, size); |
| 121 |
heif_image_handle_release(primary_handle); |
| 122 |
primary_handle = nullptr; |
| 123 |
} |
| 124 |
|
| 125 |
images_count = heif_context_get_number_of_top_level_images(ctx); |
| 126 |
if (!images_count) { |
| 127 |
// File doesn't contain any images. |
| 128 |
goto quit; |
| 129 |
} |
| 130 |
|
| 131 |
image_IDs = (heif_item_id*) malloc(images_count * sizeof(heif_item_id)); |
| 132 |
assert(image_IDs); |
| 133 |
images_count = heif_context_get_list_of_top_level_image_IDs(ctx, image_IDs, images_count); |
| 134 |
if (!images_count) { |
| 135 |
// Could not get list of image ids. |
| 136 |
goto quit; |
| 137 |
} |
| 138 |
|
| 139 |
for (int i = 0; i < images_count; ++i) { |
| 140 |
struct heif_image_handle* image_handle = nullptr; |
| 141 |
err = heif_context_get_image_handle(ctx, image_IDs[i], &image_handle); |
| 142 |
if (err.code != heif_error_Ok) { |
| 143 |
heif_image_handle_release(image_handle); |
| 144 |
// Ignore, we are only interested in crashes here. |
| 145 |
continue; |
| 146 |
} |
| 147 |
|
| 148 |
TestDecodeImage(ctx, image_handle, size); |
| 149 |
|
| 150 |
int num_thumbnails = heif_image_handle_get_number_of_thumbnails(image_handle); |
| 151 |
for (int t = 0; t < num_thumbnails; ++t) { |
| 152 |
struct heif_image_handle* thumbnail_handle = nullptr; |
| 153 |
heif_image_handle_get_thumbnail(image_handle, t, &thumbnail_handle); |
| 154 |
if (thumbnail_handle) { |
| 155 |
TestDecodeImage(ctx, thumbnail_handle, size); |
| 156 |
heif_image_handle_release(thumbnail_handle); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
heif_image_handle_release(image_handle); |
| 161 |
} |
| 162 |
|
| 163 |
quit: |
| 164 |
heif_image_handle_release(primary_handle); |
| 165 |
heif_context_free(ctx); |
| 166 |
free(image_IDs); |
| 167 |
|
| 168 |
if (explicit_init) { |
| 169 |
heif_deinit(); |
| 170 |
} |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |