Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: samplecode/SampleFilterFuzz.cpp

Issue 92793002: Fixed bad bitmap size crashes (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Changed getSize name to getAllocatedSizeInBytes Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 #include "SampleCode.h" 7 #include "SampleCode.h"
8 #include "SkBicubicImageFilter.h" 8 #include "SkBicubicImageFilter.h"
9 #include "SkBitmapDevice.h" 9 #include "SkBitmapDevice.h"
10 #include "SkBitmapSource.h" 10 #include "SkBitmapSource.h"
11 #include "SkBlurImageFilter.h" 11 #include "SkBlurImageFilter.h"
12 #include "SkCanvas.h" 12 #include "SkCanvas.h"
13 #include "SkColorFilter.h" 13 #include "SkColorFilter.h"
14 #include "SkColorFilterImageFilter.h" 14 #include "SkColorFilterImageFilter.h"
15 #include "SkComposeImageFilter.h" 15 #include "SkComposeImageFilter.h"
16 #include "SkData.h" 16 #include "SkData.h"
17 #include "SkDisplacementMapEffect.h" 17 #include "SkDisplacementMapEffect.h"
18 #include "SkDropShadowImageFilter.h" 18 #include "SkDropShadowImageFilter.h"
19 #include "SkFlattenableSerialization.h" 19 #include "SkFlattenableSerialization.h"
20 #include "SkLightingImageFilter.h" 20 #include "SkLightingImageFilter.h"
21 #include "SkMagnifierImageFilter.h" 21 #include "SkMagnifierImageFilter.h"
22 #include "SkMergeImageFilter.h" 22 #include "SkMergeImageFilter.h"
23 #include "SkMorphologyImageFilter.h" 23 #include "SkMorphologyImageFilter.h"
24 #include "SkOffsetImageFilter.h" 24 #include "SkOffsetImageFilter.h"
25 #include "SkPerlinNoiseShader.h" 25 #include "SkPerlinNoiseShader.h"
26 #include "SkRandom.h" 26 #include "SkRandom.h"
27 #include "SkRectShaderImageFilter.h" 27 #include "SkRectShaderImageFilter.h"
28 #include "SkTileImageFilter.h"
28 #include "SkView.h" 29 #include "SkView.h"
29 #include "SkXfermodeImageFilter.h" 30 #include "SkXfermodeImageFilter.h"
31 #include <stdio.h>
32 #include <time.h>
30 33
31 static const uint32_t kSeed = 1; 34 static const uint32_t kSeed = (uint32_t)(time(NULL));
32 static SkRandom gRand(kSeed); 35 static SkRandom gRand(kSeed);
33 static bool return_large = false; 36 static bool return_large = false;
34 static bool return_undef = false; 37 static bool return_undef = false;
35 38
36 static const int kBitmapSize = 24; 39 static const int kBitmapSize = 24;
37 40
38 static int R(float x) { 41 static int R(float x) {
39 return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x); 42 return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
40 } 43 }
41 44
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 } 79 }
77 80
78 if (!positiveOnly && (R(4) == 1)) v = -v; 81 if (!positiveOnly && (R(4) == 1)) v = -v;
79 return v; 82 return v;
80 } 83 }
81 84
82 static SkScalar make_scalar(bool positiveOnly = false) { 85 static SkScalar make_scalar(bool positiveOnly = false) {
83 return make_number(positiveOnly); 86 return make_number(positiveOnly);
84 } 87 }
85 88
86 static SkRect make_rect(int offset = 1) { 89 static SkRect make_rect() {
87 return SkRect::MakeWH(SkIntToScalar(R(static_cast<float>(kBitmapSize))+offse t), 90 return SkRect::MakeWH(SkIntToScalar(R(static_cast<float>(kBitmapSize))),
88 SkIntToScalar(R(static_cast<float>(kBitmapSize))+offse t)); 91 SkIntToScalar(R(static_cast<float>(kBitmapSize))));
89 } 92 }
90 93
91 static SkXfermode::Mode make_xfermode() { 94 static SkXfermode::Mode make_xfermode() {
92 return static_cast<SkXfermode::Mode>(R(SkXfermode::kLastMode+1)); 95 return static_cast<SkXfermode::Mode>(R(SkXfermode::kLastMode+1));
93 } 96 }
94 97
95 static SkColor make_color() { 98 static SkColor make_color() {
96 return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090; 99 return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090;
97 } 100 }
98 101
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 } 159 }
157 160
158 static SkImageFilter* make_image_filter(bool canBeNull = true) { 161 static SkImageFilter* make_image_filter(bool canBeNull = true) {
159 SkImageFilter* filter = 0; 162 SkImageFilter* filter = 0;
160 163
161 // Add a 1 in 3 chance to get a NULL input 164 // Add a 1 in 3 chance to get a NULL input
162 if (canBeNull && (R(3) == 1)) { return filter; } 165 if (canBeNull && (R(3) == 1)) { return filter; }
163 166
164 enum { BICUBIC, MERGE, COLOR, BLUR, MAGNIFIER, XFERMODE, OFFSET, COMPOSE, 167 enum { BICUBIC, MERGE, COLOR, BLUR, MAGNIFIER, XFERMODE, OFFSET, COMPOSE,
165 DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW, 168 DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
166 MORPHOLOGY, BITMAP, DISPLACE, NUM_FILTERS }; 169 MORPHOLOGY, BITMAP, DISPLACE, TILE, NUM_FILTERS };
167 170
168 switch (R(NUM_FILTERS)) { 171 switch (R(NUM_FILTERS)) {
169 case BICUBIC: 172 case BICUBIC:
170 // Scale is set to 1 here so that it can fit in the DAG without resizing the output 173 // Scale is set to 1 here so that it can fit in the DAG without resizing the output
171 filter = SkBicubicImageFilter::CreateMitchell(SkSize::Make(1, 1), make_i mage_filter()); 174 filter = SkBicubicImageFilter::CreateMitchell(SkSize::Make(1, 1), make_i mage_filter());
172 break; 175 break;
173 case MERGE: 176 case MERGE:
174 filter = new SkMergeImageFilter(make_image_filter(), make_image_filter() , make_xfermode()); 177 filter = new SkMergeImageFilter(make_image_filter(), make_image_filter() , make_xfermode());
175 break; 178 break;
176 case COLOR: 179 case COLOR:
177 { 180 {
178 SkAutoTUnref<SkColorFilter> cf((R(2) == 1) ? 181 SkAutoTUnref<SkColorFilter> cf((R(2) == 1) ?
179 SkColorFilter::CreateModeFilter(make_color(), make_xfermode()) : 182 SkColorFilter::CreateModeFilter(make_color(), make_xfermode()) :
180 SkColorFilter::CreateLightingFilter(make_color(), make_color()) ); 183 SkColorFilter::CreateLightingFilter(make_color(), make_color()) );
181 filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filt er()) : 0; 184 filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filt er()) : 0;
182 } 185 }
183 break; 186 break;
184 case BLUR: 187 case BLUR:
185 filter = new SkBlurImageFilter(make_scalar(true), make_scalar(true), mak e_image_filter()); 188 filter = new SkBlurImageFilter(make_scalar(true), make_scalar(true), mak e_image_filter());
186 break; 189 break;
187 case MAGNIFIER: 190 case MAGNIFIER:
188 filter = new SkMagnifierImageFilter(make_rect(0), make_scalar(true)); 191 filter = new SkMagnifierImageFilter(make_rect(), make_scalar(true));
189 break; 192 break;
190 case XFERMODE: 193 case XFERMODE:
191 { 194 {
192 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(make_xfermode())); 195 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(make_xfermode()));
193 filter = new SkXfermodeImageFilter(mode, make_image_filter(), make_image _filter()); 196 filter = new SkXfermodeImageFilter(mode, make_image_filter(), make_image _filter());
194 } 197 }
195 break; 198 break;
196 case OFFSET: 199 case OFFSET:
197 filter = new SkOffsetImageFilter(make_scalar(), make_scalar(), make_imag e_filter()); 200 filter = new SkOffsetImageFilter(make_scalar(), make_scalar(), make_imag e_filter());
198 break; 201 break;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 R(static_cast<float>(kBitmapSize)), make_image_filter()); 252 R(static_cast<float>(kBitmapSize)), make_image_filter());
250 break; 253 break;
251 case BITMAP: 254 case BITMAP:
252 filter = new SkBitmapSource(make_bitmap()); 255 filter = new SkBitmapSource(make_bitmap());
253 break; 256 break;
254 case DISPLACE: 257 case DISPLACE:
255 filter = new SkDisplacementMapEffect(make_channel_selector_type(), 258 filter = new SkDisplacementMapEffect(make_channel_selector_type(),
256 make_channel_selector_type(), make_scalar(), 259 make_channel_selector_type(), make_scalar(),
257 make_image_filter(false), make_image_filter()); 260 make_image_filter(false), make_image_filter());
258 break; 261 break;
262 case TILE:
263 filter = new SkTileImageFilter(make_rect(), make_rect(), make_image_filt er(false));
264 break;
259 default: 265 default:
260 break; 266 break;
261 } 267 }
262 return (filter || canBeNull) ? filter : make_image_filter(canBeNull); 268 return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
263 } 269 }
264 270
265 static SkImageFilter* make_serialized_image_filter() { 271 static SkImageFilter* make_serialized_image_filter() {
266 SkAutoTUnref<SkImageFilter> filter(make_image_filter(false)); 272 SkAutoTUnref<SkImageFilter> filter(make_image_filter(false));
267 SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter)); 273 SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter));
268 const unsigned char* ptr = static_cast<const unsigned char*>(data->data()); 274 const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
269 size_t len = data->size(); 275 size_t len = data->size();
270 #ifdef SK_ADD_RANDOM_BIT_FLIPS 276 #ifdef SK_ADD_RANDOM_BIT_FLIPS
271 unsigned char* p = const_cast<unsigned char*>(ptr); 277 unsigned char* p = const_cast<unsigned char*>(ptr);
272 for (size_t i = 0; i < len; ++i, ++p) { 278 for (size_t i = 0; i < len; ++i, ++p) {
273 if ((R(1000) == 1)) { // 0.1% of the time, flip a bit 279 if (R(250) == 1) { // 0.4% of the time, flip a bit or byte
274 *p ^= (1 << R(8)); 280 if (R(10) == 1) { // Then 10% of the time, change a whole byte
281 switch(R(3)) {
282 case 0:
283 *p ^= 0xFF; // Flip entire byte
284 break;
285 case 1:
286 *p = 0xFF; // Set all bits to 1
287 break;
288 case 2:
289 *p = 0x00; // Set all bits to 0
290 break;
291 }
292 } else {
293 *p ^= (1 << R(8));
294 }
275 } 295 }
276 } 296 }
277 #endif // SK_ADD_RANDOM_BIT_FLIPS 297 #endif // SK_ADD_RANDOM_BIT_FLIPS
278 SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len, 298 SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len,
279 SkImageFilter::GetFlattenableType()); 299 SkImageFilter::GetFlattenableType());
280 SkASSERT(NULL != flattenable);
281 return static_cast<SkImageFilter*>(flattenable); 300 return static_cast<SkImageFilter*>(flattenable);
282 } 301 }
283 302
284 static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& pai nt) { 303 static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& pai nt) {
285 canvas->save(); 304 canvas->save();
286 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), 305 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
287 SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize))); 306 SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
288 canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint ); 307 canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint );
289 canvas->restore(); 308 canvas->restore();
290 } 309 }
291 310
292 static void do_fuzz(SkCanvas* canvas) { 311 static void do_fuzz(SkCanvas* canvas) {
312 #ifdef SK_FUZZER_IS_VERBOSE
313 static uint32_t filterId = 0;
314 if (0 == filterId) {
315 printf("Fuzzing with %u\n", kSeed);
316 }
317 printf("Filter no %u\r", filterId);
318 fflush(stdout);
319 filterId++;
320 #endif
321
293 SkPaint paint; 322 SkPaint paint;
294 paint.setImageFilter(make_serialized_image_filter())->unref(); 323 SkSafeUnref(paint.setImageFilter(make_serialized_image_filter()));
295 drawClippedBitmap(canvas, 0, 0, paint); 324 drawClippedBitmap(canvas, 0, 0, paint);
296 } 325 }
297 326
298 ////////////////////////////////////////////////////////////////////////////// 327 //////////////////////////////////////////////////////////////////////////////
299 328
300 class ImageFilterFuzzView : public SampleView { 329 class ImageFilterFuzzView : public SampleView {
301 public: 330 public:
302 ImageFilterFuzzView() { 331 ImageFilterFuzzView() {
303 this->setBGColor(0xFFDDDDDD); 332 this->setBGColor(0xFFDDDDDD);
304 } 333 }
(...skipping 18 matching lines...) Expand all
323 } 352 }
324 353
325 private: 354 private:
326 typedef SkView INHERITED; 355 typedef SkView INHERITED;
327 }; 356 };
328 357
329 ////////////////////////////////////////////////////////////////////////////// 358 //////////////////////////////////////////////////////////////////////////////
330 359
331 static SkView* MyFactory() { return new ImageFilterFuzzView; } 360 static SkView* MyFactory() { return new ImageFilterFuzzView; }
332 static SkViewRegister reg(MyFactory); 361 static SkViewRegister reg(MyFactory);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698