Chromium Code Reviews| 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 #ifdef MEMORY_SANITIZER | |
|
kcc2
2017/04/17 18:14:28
Why #ifdef MEMORY_SANITIZER?
Isn't this relevant
scroggo_chromium
2017/04/17 18:51:53
I would have thought so, too, but https://bugs.chr
| |
| 16 void* limited_malloc(png_structp, png_alloc_size_t size) { | |
| 17 // libpng may allocate large amounts of memory that the fuzzer reports as | |
| 18 // an error. In order to silence these errors, make libpng fail when trying | |
| 19 // to allocate a large amount. | |
| 20 // This number is chosen to match the default png_user_chunk_malloc_max. | |
| 21 if (size > 8000000) | |
| 22 return nullptr; | |
| 23 | |
| 24 return malloc(size); | |
| 25 } | |
| 26 | |
| 27 void default_free(png_structp, png_voidp ptr) { | |
| 28 return free(ptr); | |
| 29 } | |
| 30 #endif // MEMORY_SANITIZER | |
| 31 | |
| 15 #ifndef PNG_FUZZ_PROGRESSIVE | 32 #ifndef PNG_FUZZ_PROGRESSIVE |
| 16 | 33 |
| 17 // Read sequentially, with png_read_row. | 34 // Read sequentially, with png_read_row. |
| 18 struct BufState { | 35 struct BufState { |
| 19 const uint8_t* data; | 36 const uint8_t* data; |
| 20 size_t bytes_left; | 37 size_t bytes_left; |
| 21 }; | 38 }; |
| 22 | 39 |
| 23 void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { | 40 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)); | 41 BufState* buf_state = static_cast<BufState*>(png_get_io_ptr(png_ptr)); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 49 } | 66 } |
| 50 | 67 |
| 51 png_structp png_ptr = png_create_read_struct | 68 png_structp png_ptr = png_create_read_struct |
| 52 (PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); | 69 (PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 53 assert(png_ptr); | 70 assert(png_ptr); |
| 54 | 71 |
| 55 #ifdef MEMORY_SANITIZER | 72 #ifdef MEMORY_SANITIZER |
| 56 // To avoid OOM with MSan (crbug.com/648073). These values are recommended as | 73 // 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 | 74 // safe settings by https://github.com/glennrp/libpng/blob/libpng16/pngusr.dfa |
| 58 png_set_user_limits(png_ptr, 65535, 65535); | 75 png_set_user_limits(png_ptr, 65535, 65535); |
| 76 | |
| 77 // Not all potential OOM are due to images with large widths and heights. | |
| 78 // Use a custom allocator that fails for large allocations. | |
| 79 png_set_mem_fn(png_ptr, nullptr, limited_malloc, default_free); | |
| 59 #endif | 80 #endif |
| 60 | 81 |
| 61 png_set_crc_action(png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE); | 82 png_set_crc_action(png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE); |
| 62 | 83 |
| 63 png_infop info_ptr = png_create_info_struct(png_ptr); | 84 png_infop info_ptr = png_create_info_struct(png_ptr); |
| 64 assert(info_ptr); | 85 assert(info_ptr); |
| 65 | 86 |
| 66 base::ScopedClosureRunner struct_deleter(base::Bind( | 87 base::ScopedClosureRunner struct_deleter(base::Bind( |
| 67 &png_destroy_read_struct, &png_ptr, &info_ptr, nullptr)); | 88 &png_destroy_read_struct, &png_ptr, &info_ptr, nullptr)); |
| 68 | 89 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 | 137 |
| 117 for (int pass = 0; pass < passes; ++pass) { | 138 for (int pass = 0; pass < passes; ++pass) { |
| 118 for (png_uint_32 y = 0; y < height; ++y) { | 139 for (png_uint_32 y = 0; y < height; ++y) { |
| 119 png_read_row(png_ptr, static_cast<png_bytep>(row), NULL); | 140 png_read_row(png_ptr, static_cast<png_bytep>(row), NULL); |
| 120 } | 141 } |
| 121 } | 142 } |
| 122 #endif // PNG_FUZZ_PROGRESSIVE | 143 #endif // PNG_FUZZ_PROGRESSIVE |
| 123 | 144 |
| 124 return 0; | 145 return 0; |
| 125 } | 146 } |
| OLD | NEW |