Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | # Copyright 2018 Google Inc. |
3 | | # |
4 | | # Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | # you may not use this file except in compliance with the License. |
6 | | # You may obtain a copy of the License at |
7 | | # |
8 | | # http://www.apache.org/licenses/LICENSE-2.0 |
9 | | # |
10 | | # Unless required by applicable law or agreed to in writing, software |
11 | | # distributed under the License is distributed on an "AS IS" BASIS, |
12 | | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | # See the License for the specific language governing permissions and |
14 | | # limitations under the License. |
15 | | # |
16 | | ################################################################################ |
17 | | */ |
18 | | |
19 | | #include <cstdint> |
20 | | #include <stdlib.h> |
21 | | #include <string.h> |
22 | | #include <stdio.h> |
23 | | |
24 | | #include "jbig2.h" |
25 | | |
26 | 139M | #define ALIGNMENT ((size_t) 16) |
27 | 17.4M | #define KBYTE ((size_t) 1024) |
28 | 17.4M | #define MBYTE (1024 * KBYTE) |
29 | | #define GBYTE (1024 * MBYTE) |
30 | 17.4M | #define MAX_ALLOCATION (32 * MBYTE) |
31 | | |
32 | | static size_t used; |
33 | | |
34 | | static void *jbig2_fuzzer_reached_limit(size_t oldsize, size_t size) |
35 | 447 | { |
36 | 447 | if (oldsize == 0) |
37 | 426 | fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte allocation: %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, size); |
38 | 21 | else |
39 | 21 | fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte reallocation: %zu -> %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, oldsize, size); |
40 | 447 | fflush(0); |
41 | 447 | return NULL; |
42 | 447 | } |
43 | | |
44 | | static void *jbig2_fuzzer_alloc(Jbig2Allocator *allocator, size_t size) |
45 | 17.4M | { |
46 | 17.4M | char *ptr = NULL; |
47 | | |
48 | 17.4M | if (size == 0) |
49 | 4.53k | return NULL; |
50 | 17.4M | if (size > SIZE_MAX - ALIGNMENT) |
51 | 0 | return NULL; |
52 | 17.4M | if (size + ALIGNMENT > MAX_ALLOCATION - used) |
53 | 426 | return jbig2_fuzzer_reached_limit(0, size + ALIGNMENT); |
54 | | |
55 | 17.4M | ptr = (char *) malloc(size + ALIGNMENT); |
56 | 17.4M | if (ptr == NULL) |
57 | 0 | return NULL; |
58 | | |
59 | 17.4M | memcpy(ptr, &size, sizeof(size)); |
60 | 17.4M | used += size + ALIGNMENT; |
61 | | |
62 | 17.4M | return ptr + ALIGNMENT; |
63 | 17.4M | } |
64 | | |
65 | | static void jbig2_fuzzer_free(Jbig2Allocator *allocator, void *ptr) |
66 | 17.5M | { |
67 | 17.5M | size_t size; |
68 | | |
69 | 17.5M | if (ptr == NULL) |
70 | 116k | return; |
71 | 17.4M | if (ptr < (void *) ALIGNMENT) |
72 | 0 | return; |
73 | | |
74 | 17.4M | ptr = (char *) ptr - ALIGNMENT; |
75 | 17.4M | memcpy(&size, ptr, sizeof(size)); |
76 | | |
77 | 17.4M | used -= size + ALIGNMENT; |
78 | 17.4M | free(ptr); |
79 | 17.4M | } |
80 | | |
81 | | static void *jbig2_fuzzer_realloc(Jbig2Allocator *allocator, void *old, size_t size) |
82 | 2.69k | { |
83 | 2.69k | size_t oldsize; |
84 | 2.69k | char *ptr; |
85 | | |
86 | 2.69k | if (old == NULL) |
87 | 0 | return jbig2_fuzzer_alloc(allocator, size); |
88 | 2.69k | if (old < (void *) ALIGNMENT) |
89 | 0 | return NULL; |
90 | | |
91 | 2.69k | if (size == 0) { |
92 | 0 | jbig2_fuzzer_free(allocator, old); |
93 | 0 | return NULL; |
94 | 0 | } |
95 | 2.69k | if (size > SIZE_MAX - ALIGNMENT) |
96 | 0 | return NULL; |
97 | | |
98 | 2.69k | old = (char *) old - ALIGNMENT; |
99 | 2.69k | memcpy(&oldsize, old, sizeof(oldsize)); |
100 | | |
101 | 2.69k | if (size + ALIGNMENT > MAX_ALLOCATION - used + oldsize + ALIGNMENT) |
102 | 21 | return jbig2_fuzzer_reached_limit(oldsize + ALIGNMENT, size + ALIGNMENT); |
103 | | |
104 | 2.67k | ptr = (char *) realloc(old, size + ALIGNMENT); |
105 | 2.67k | if (ptr == NULL) |
106 | 0 | return NULL; |
107 | | |
108 | 2.67k | used -= oldsize + ALIGNMENT; |
109 | 2.67k | memcpy(ptr, &size, sizeof(size)); |
110 | 2.67k | used += size + ALIGNMENT; |
111 | | |
112 | 2.67k | return ptr + ALIGNMENT; |
113 | 2.67k | } |
114 | | |
115 | 1.86k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
116 | 1.86k | Jbig2Allocator allocator; |
117 | 1.86k | Jbig2Ctx *ctx = NULL; |
118 | | |
119 | 1.86k | used = 0; |
120 | | |
121 | 1.86k | allocator.alloc = jbig2_fuzzer_alloc; |
122 | 1.86k | allocator.free = jbig2_fuzzer_free; |
123 | 1.86k | allocator.realloc = jbig2_fuzzer_realloc; |
124 | | |
125 | 1.86k | ctx = jbig2_ctx_new(&allocator, (Jbig2Options) 0, NULL, NULL, NULL); |
126 | 1.86k | if (jbig2_data_in(ctx, data, size) == 0) |
127 | 1.40k | { |
128 | 1.40k | if (jbig2_complete_page(ctx) == 0) |
129 | 419 | { |
130 | 419 | Jbig2Image *image = jbig2_page_out(ctx); |
131 | 419 | if (image != NULL) |
132 | 419 | { |
133 | 419 | int sum = 0; |
134 | 703M | for (int i = 0; i < image->height * image->stride; i++) |
135 | 703M | sum += image->data[i]; |
136 | 419 | printf("sum of image data bytes: %d\n", sum); |
137 | 419 | } |
138 | 419 | jbig2_release_page(ctx, image); |
139 | 419 | } |
140 | 1.40k | } |
141 | 1.86k | jbig2_ctx_free(ctx); |
142 | | |
143 | 1.86k | return 0; |
144 | 1.86k | } |