Coverage Report

Created: 2026-10-30 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jbig2_fuzzer.cc
Line
Count
Source
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
391M
#define ALIGNMENT ((size_t) 16)
27
48.9M
#define KBYTE ((size_t) 1024)
28
48.9M
#define MBYTE (1024 * KBYTE)
29
#define GBYTE (1024 * MBYTE)
30
48.9M
#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
1.44k
{
36
1.44k
  if (oldsize == 0)
37
1.38k
    fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte allocation: %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, size);
38
68
  else
39
68
    fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte reallocation: %zu -> %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, oldsize, size);
40
1.44k
  fflush(0);
41
1.44k
  return NULL;
42
1.44k
}
43
44
static void *jbig2_fuzzer_alloc(Jbig2Allocator *allocator, size_t size)
45
48.9M
{
46
48.9M
  char *ptr = NULL;
47
48
48.9M
  if (size == 0)
49
5.80k
    return NULL;
50
48.9M
  if (size > SIZE_MAX - ALIGNMENT)
51
0
    return NULL;
52
48.9M
  if (size + ALIGNMENT > MAX_ALLOCATION - used)
53
1.38k
    return jbig2_fuzzer_reached_limit(0, size + ALIGNMENT);
54
55
48.9M
  ptr = (char *) malloc(size + ALIGNMENT);
56
48.9M
  if (ptr == NULL)
57
0
    return NULL;
58
59
48.9M
  memcpy(ptr, &size, sizeof(size));
60
48.9M
  used += size + ALIGNMENT;
61
62
48.9M
  return ptr + ALIGNMENT;
63
48.9M
}
64
65
static void jbig2_fuzzer_free(Jbig2Allocator *allocator, void *ptr)
66
49.0M
{
67
49.0M
  size_t size;
68
69
49.0M
  if (ptr == NULL)
70
117k
    return;
71
48.9M
  if (ptr < (void *) ALIGNMENT)
72
0
    return;
73
74
48.9M
  ptr = (char *) ptr - ALIGNMENT;
75
48.9M
  memcpy(&size, ptr, sizeof(size));
76
77
48.9M
  used -= size + ALIGNMENT;
78
48.9M
  free(ptr);
79
48.9M
}
80
81
static void *jbig2_fuzzer_realloc(Jbig2Allocator *allocator, void *old, size_t size)
82
12.7k
{
83
12.7k
  size_t oldsize;
84
12.7k
  char *ptr;
85
86
12.7k
  if (old == NULL)
87
0
    return jbig2_fuzzer_alloc(allocator, size);
88
12.7k
  if (old < (void *) ALIGNMENT)
89
0
    return NULL;
90
91
12.7k
  if (size == 0) {
92
0
    jbig2_fuzzer_free(allocator, old);
93
0
    return NULL;
94
0
  }
95
12.7k
  if (size > SIZE_MAX - ALIGNMENT)
96
0
    return NULL;
97
98
12.7k
  old = (char *) old - ALIGNMENT;
99
12.7k
  memcpy(&oldsize, old, sizeof(oldsize));
100
101
12.7k
  if (size + ALIGNMENT > MAX_ALLOCATION - used + oldsize + ALIGNMENT)
102
68
    return jbig2_fuzzer_reached_limit(oldsize + ALIGNMENT, size + ALIGNMENT);
103
104
12.6k
  ptr = (char *) realloc(old, size + ALIGNMENT);
105
12.6k
  if (ptr == NULL)
106
0
    return NULL;
107
108
12.6k
  used -= oldsize + ALIGNMENT;
109
12.6k
  memcpy(ptr, &size, sizeof(size));
110
12.6k
  used += size + ALIGNMENT;
111
112
12.6k
  return ptr + ALIGNMENT;
113
12.6k
}
114
115
7.14k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
116
7.14k
  Jbig2Allocator allocator;
117
7.14k
  Jbig2Ctx *ctx = NULL;
118
119
7.14k
  used = 0;
120
121
7.14k
  allocator.alloc = jbig2_fuzzer_alloc;
122
7.14k
  allocator.free = jbig2_fuzzer_free;
123
7.14k
  allocator.realloc = jbig2_fuzzer_realloc;
124
125
7.14k
  ctx = jbig2_ctx_new(&allocator, (Jbig2Options) 0, NULL, NULL, NULL);
126
7.14k
  if (ctx == NULL)
127
0
    return 0;
128
7.14k
  if (jbig2_data_in(ctx, data, size) == 0)
129
6.09k
  {
130
6.09k
    if (jbig2_complete_page(ctx) == 0)
131
621
    {
132
621
      Jbig2Image *image = jbig2_page_out(ctx);
133
621
      if (image != NULL)
134
621
      {
135
621
        size_t sum = 0;
136
1.02G
        for (int i = 0; i < image->height * image->stride; i++)
137
1.02G
          sum += image->data[i];
138
621
        printf("sum of image data bytes: %zu\n", sum);
139
621
      }
140
621
      jbig2_release_page(ctx, image);
141
621
    }
142
6.09k
  }
143
7.14k
  jbig2_ctx_free(ctx);
144
145
7.14k
  return 0;
146
7.14k
}