/src/graphicsmagick/fuzzing/utils.cc
Line | Count | Source |
1 | | // |
2 | | // See https://llvm.org/docs/LibFuzzer.html |
3 | | // |
4 | | |
5 | | #define USE_DYNAMIC_INITIALIZATION 1 |
6 | | |
7 | | #if USE_DYNAMIC_INITIALIZATION |
8 | | |
9 | | // A custom fuzzer initialization function |
10 | 252 | extern "C" int LLVMFuzzerInitialize(int *argcp,char ***argvp) { |
11 | 252 | int argc = *argcp; |
12 | 252 | const char* exe_path = (*argvp)[0]; |
13 | | // fprintf(stderr, "LLVMFuzzerInitialize(): argc=%d, exe_path = %s\n", argc, exe_path); |
14 | 252 | Magick::InitializeMagick(exe_path); |
15 | | |
16 | | // Trace exception events to help diagnose issues. |
17 | 252 | if (MagickLib::SetLogEventMask(NULL) == MagickLib::NoEventsMask) |
18 | 252 | MagickLib::SetLogEventMask("exception"); |
19 | | |
20 | | // Oss-fuzz itself (ASAN/UBSAN) seems to require memory and |
21 | | // the memory limit may be total virtual memory and not based |
22 | | // only on memory allocations and actual RSS. Formats like |
23 | | // SVG/MVG may make arbitrary requests. Provide lots of |
24 | | // headroom. Was 1000000000. |
25 | | // |
26 | | // A Q16 image with dimensions 2048x2048 requires 40,960k of |
27 | | // RAM. Provide enough memory for 6 images, which seems like |
28 | | // enough for any reasonable fuzzing purpose. |
29 | | // |
30 | | // Don't allow using disk-based pixel cache files when the |
31 | | // memory resource runs out. |
32 | 252 | MagickLib::SetMagickResourceLimit(MagickLib::MemoryResource, 268435456); |
33 | 252 | MagickLib::SetMagickResourceLimit(MagickLib::WidthResource, 2048); |
34 | 252 | MagickLib::SetMagickResourceLimit(MagickLib::HeightResource, 2048); |
35 | 252 | MagickLib::SetMagickResourceLimit(MagickLib::PixelsResource, |
36 | 252 | MagickLib::GetMagickResourceLimit(MagickLib::WidthResource)* |
37 | 252 | MagickLib::GetMagickResourceLimit(MagickLib::HeightResource)); |
38 | 252 | MagickLib::SetMagickResourceLimit(MagickLib::DiskResource, 0); |
39 | 252 | MagickLib::SetMagickResourceLimit(MagickLib::ReadResource, 900000); |
40 | 252 | MagickLib::SetMagickResourceLimit(MagickLib::WriteResource, 1118720); |
41 | 252 | MagickLib::SetMagickResourceLimit(MagickLib::ImagesResource, 1024); |
42 | | |
43 | 252 | return 0; |
44 | 252 | } |
45 | | |
46 | | #else |
47 | | |
48 | | /* |
49 | | Use static initialization |
50 | | */ |
51 | | class MagickState { |
52 | | public: |
53 | | MagickState() { |
54 | | Magick::InitializeMagick(nullptr); |
55 | | |
56 | | // Trace exception events to help diagnose issues. |
57 | | if (MagickLib::SetLogEventMask(NULL) == MagickLib::NoEventsMask) |
58 | | MagickLib::SetLogEventMask("exception"); |
59 | | |
60 | | // Oss-fuzz itself (ASAN/UBSAN) seems to require memory and |
61 | | // the memory limit may be total virtual memory and not based |
62 | | // only on memory allocations and actual RSS. Formats like |
63 | | // SVG/MVG may make arbitrary requests. Provide lots of |
64 | | // headroom. Was 1000000000. |
65 | | // |
66 | | // A Q16 image with dimensions 2048x2048 requires 40,960k of |
67 | | // RAM. Provide enough memory for 6 images, which seems like |
68 | | // enough for any reasonable fuzzing purpose. |
69 | | // |
70 | | // Don't allow using disk-based pixel cache files when the |
71 | | // memory resource runs out. |
72 | | MagickLib::SetMagickResourceLimit(MagickLib::MemoryResource, 268435456); |
73 | | MagickLib::SetMagickResourceLimit(MagickLib::WidthResource, 2048); |
74 | | MagickLib::SetMagickResourceLimit(MagickLib::HeightResource, 2048); |
75 | | MagickLib::SetMagickResourceLimit(MagickLib::PixelsResource, |
76 | | MagickLib::GetMagickResourceLimit(MagickLib::WidthResource)* |
77 | | MagickLib::GetMagickResourceLimit(MagickLib::HeightResource)); |
78 | | MagickLib::SetMagickResourceLimit(MagickLib::DiskResource, 0); |
79 | | MagickLib::SetMagickResourceLimit(MagickLib::ReadResource, 900000); |
80 | | MagickLib::SetMagickResourceLimit(MagickLib::WriteResource, 1118720); |
81 | | MagickLib::SetMagickResourceLimit(MagickLib::ImagesResource, 1024); |
82 | | } |
83 | | }; |
84 | | |
85 | | // Static initializer so this code is run once at startup. |
86 | | MagickState kMagickState; |
87 | | |
88 | | #endif |