| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #define PNG_INTERNAL | 12 #define PNG_INTERNAL |
| 13 #include "third_party/libpng/png.h" | 13 #include "third_party/libpng/png.h" |
| 14 | 14 |
| 15 void* limited_malloc(png_structp, png_alloc_size_t size) { |
| 16 // libpng may allocate large amounts of memory that the fuzzer reports as |
| 17 // an error. In order to silence these errors, make libpng fail when trying |
| 18 // to allocate a large amount. |
| 19 // This number is chosen to match the default png_user_chunk_malloc_max. |
| 20 if (size > 8000000) |
| 21 return nullptr; |
| 22 |
| 23 return malloc(size); |
| 24 } |
| 25 |
| 26 void default_free(png_structp, png_voidp ptr) { |
| 27 return free(ptr); |
| 28 } |
| 29 |
| 15 #ifndef PNG_FUZZ_PROGRESSIVE | 30 #ifndef PNG_FUZZ_PROGRESSIVE |
| 16 | 31 |
| 17 // Read sequentially, with png_read_row. | 32 // Read sequentially, with png_read_row. |
| 18 struct BufState { | 33 struct BufState { |
| 19 const uint8_t* data; | 34 const uint8_t* data; |
| 20 size_t bytes_left; | 35 size_t bytes_left; |
| 21 }; | 36 }; |
| 22 | 37 |
| 23 void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { | 38 void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { |
| 24 BufState* buf_state = static_cast<BufState*>(png_get_io_ptr(png_ptr)); | 39 BufState* buf_state = static_cast<BufState*>(png_get_io_ptr(png_ptr)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 51 png_structp png_ptr = png_create_read_struct | 66 png_structp png_ptr = png_create_read_struct |
| 52 (PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); | 67 (PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 53 assert(png_ptr); | 68 assert(png_ptr); |
| 54 | 69 |
| 55 #ifdef MEMORY_SANITIZER | 70 #ifdef MEMORY_SANITIZER |
| 56 // To avoid OOM with MSan (crbug.com/648073). These values are recommended as | 71 // To avoid OOM with MSan (crbug.com/648073). These values are recommended as |
| 57 // safe settings by https://github.com/glennrp/libpng/blob/libpng16/pngusr.dfa | 72 // safe settings by https://github.com/glennrp/libpng/blob/libpng16/pngusr.dfa |
| 58 png_set_user_limits(png_ptr, 65535, 65535); | 73 png_set_user_limits(png_ptr, 65535, 65535); |
| 59 #endif | 74 #endif |
| 60 | 75 |
| 76 // Not all potential OOM are due to images with large widths and heights. |
| 77 // Use a custom allocator that fails for large allocations. |
| 78 png_set_mem_fn(png_ptr, nullptr, limited_malloc, default_free); |
| 79 |
| 61 png_set_crc_action(png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE); | 80 png_set_crc_action(png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE); |
| 62 | 81 |
| 63 png_infop info_ptr = png_create_info_struct(png_ptr); | 82 png_infop info_ptr = png_create_info_struct(png_ptr); |
| 64 assert(info_ptr); | 83 assert(info_ptr); |
| 65 | 84 |
| 66 base::ScopedClosureRunner struct_deleter(base::Bind( | 85 base::ScopedClosureRunner struct_deleter(base::Bind( |
| 67 &png_destroy_read_struct, &png_ptr, &info_ptr, nullptr)); | 86 &png_destroy_read_struct, &png_ptr, &info_ptr, nullptr)); |
| 68 | 87 |
| 69 #ifdef PNG_FUZZ_PROGRESSIVE | 88 #ifdef PNG_FUZZ_PROGRESSIVE |
| 70 if (setjmp(png_jmpbuf(png_ptr))) { | 89 if (setjmp(png_jmpbuf(png_ptr))) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 135 |
| 117 for (int pass = 0; pass < passes; ++pass) { | 136 for (int pass = 0; pass < passes; ++pass) { |
| 118 for (png_uint_32 y = 0; y < height; ++y) { | 137 for (png_uint_32 y = 0; y < height; ++y) { |
| 119 png_read_row(png_ptr, static_cast<png_bytep>(row), NULL); | 138 png_read_row(png_ptr, static_cast<png_bytep>(row), NULL); |
| 120 } | 139 } |
| 121 } | 140 } |
| 122 #endif // PNG_FUZZ_PROGRESSIVE | 141 #endif // PNG_FUZZ_PROGRESSIVE |
| 123 | 142 |
| 124 return 0; | 143 return 0; |
| 125 } | 144 } |
| OLD | NEW |