Coverage Report

Created: 2026-10-22 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvhdi/ossfuzz/file_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvhdi file type
3
 *
4
 * Copyright (C) 2012-2026, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <stddef.h>
23
#include <stdint.h>
24
25
/* Note that some of the OSS-Fuzz engines use C++
26
 */
27
extern "C" {
28
29
#include "ossfuzz_libbfio.h"
30
#include "ossfuzz_libvhdi.h"
31
32
#if !defined( LIBVHDI_HAVE_BFIO )
33
34
/* Opens a file using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBVHDI_EXTERN \
38
int libvhdi_file_open_file_io_handle(
39
     libvhdi_file_t *file,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libvhdi_error_t **error );
43
44
#endif /* !defined( LIBVHDI_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
1.85k
{
50
1.85k
  uint8_t buffer[ 512 ];
51
1.85k
  uint8_t guid[ 16 ];
52
1.85k
  uint8_t utf8_string[ 64 ];
53
1.85k
  uint16_t utf16_string[ 64 ];
54
55
1.85k
  libbfio_handle_t *file_io_handle = NULL;
56
1.85k
  libvhdi_file_t *file             = NULL;
57
1.85k
  off64_t media_offset             = 0;
58
1.85k
  size64_t media_size              = 0;
59
1.85k
  size_t string_size               = 0;
60
1.85k
  uint32_t value_32bit             = 0;
61
1.85k
  uint16_t major_version           = 0;
62
1.85k
  uint16_t minor_version           = 0;
63
1.85k
  int file_type                    = 0;
64
1.85k
  int read_iterator                = 0;
65
66
1.85k
  if( libbfio_memory_range_initialize(
67
1.85k
       &file_io_handle,
68
1.85k
       NULL ) != 1 )
69
0
  {
70
0
    return( 0 );
71
0
  }
72
1.85k
  if( libbfio_memory_range_set(
73
1.85k
       file_io_handle,
74
1.85k
       (uint8_t *) data,
75
1.85k
       size,
76
1.85k
       NULL ) != 1 )
77
0
  {
78
0
    goto on_error_libbfio;
79
0
  }
80
1.85k
  if( libvhdi_file_initialize(
81
1.85k
       &file,
82
1.85k
       NULL ) != 1 )
83
0
  {
84
0
    goto on_error_libbfio;
85
0
  }
86
1.85k
  if( libvhdi_file_open_file_io_handle(
87
1.85k
       file,
88
1.85k
       file_io_handle,
89
1.85k
       LIBVHDI_OPEN_READ,
90
1.85k
       NULL ) != 1 )
91
1.29k
  {
92
1.29k
    goto on_error_libvhdi;
93
1.29k
  }
94
557
  if( libvhdi_file_get_file_type(
95
557
       file,
96
557
       &file_type,
97
557
       NULL ) != 1 )
98
0
  {
99
0
    goto on_error_libvhdi;
100
0
  }
101
557
  if( libvhdi_file_get_format_version(
102
557
       file,
103
557
       &major_version,
104
557
       &minor_version,
105
557
       NULL ) != 1 )
106
0
  {
107
0
    goto on_error_libvhdi;
108
0
  }
109
557
  if( libvhdi_file_get_disk_type(
110
557
       file,
111
557
       &value_32bit,
112
557
       NULL ) != 1 )
113
0
  {
114
0
    goto on_error_libvhdi;
115
0
  }
116
557
  if( libvhdi_file_get_bytes_per_sector(
117
557
       file,
118
557
       &value_32bit,
119
557
       NULL ) != 1 )
120
0
  {
121
0
    goto on_error_libvhdi;
122
0
  }
123
557
  if( libvhdi_file_get_identifier(
124
557
       file,
125
557
       guid,
126
557
       16,
127
557
       NULL ) != 1 )
128
0
  {
129
0
    goto on_error_libvhdi;
130
0
  }
131
557
  if( libvhdi_file_get_parent_identifier(
132
557
       file,
133
557
       guid,
134
557
       16,
135
557
       NULL ) == -1 )
136
0
  {
137
0
    goto on_error_libvhdi;
138
0
  }
139
557
  if( libvhdi_file_get_utf8_parent_filename_size(
140
557
       file,
141
557
       &string_size,
142
557
       NULL ) == -1 )
143
36
  {
144
36
    goto on_error_libvhdi;
145
36
  }
146
521
  if( libvhdi_file_get_utf8_parent_filename(
147
521
       file,
148
521
       utf8_string,
149
521
       64,
150
521
       NULL ) == -1 )
151
33
  {
152
33
    goto on_error_libvhdi;
153
33
  }
154
488
  if( libvhdi_file_get_utf16_parent_filename_size(
155
488
       file,
156
488
       &string_size,
157
488
       NULL ) == -1 )
158
0
  {
159
0
    goto on_error_libvhdi;
160
0
  }
161
488
  if( libvhdi_file_get_utf16_parent_filename(
162
488
       file,
163
488
       utf16_string,
164
488
       64,
165
488
       NULL ) == -1 )
166
0
  {
167
0
    goto on_error_libvhdi;
168
0
  }
169
488
  if( libvhdi_file_get_media_size(
170
488
       file,
171
488
       &media_size,
172
488
       NULL ) != 1 )
173
0
  {
174
0
    goto on_error_libvhdi;
175
0
  }
176
488
  for( read_iterator = 0;
177
12.4k
       read_iterator < 128;
178
11.9k
       read_iterator++ )
179
12.4k
  {
180
12.4k
    if( media_offset >= media_size )
181
38
    {
182
38
      break;
183
38
    }
184
12.3k
    if( libvhdi_file_read_buffer_at_offset(
185
12.3k
         file,
186
12.3k
         buffer,
187
12.3k
         497,
188
12.3k
         media_offset,
189
12.3k
         NULL ) == -1 )
190
407
    {
191
407
      goto on_error_libvhdi;
192
407
    }
193
11.9k
    media_offset += 497;
194
11.9k
  }
195
81
  libvhdi_file_close(
196
81
   file,
197
81
   NULL );
198
199
1.85k
on_error_libvhdi:
200
1.85k
  libvhdi_file_free(
201
1.85k
   &file,
202
1.85k
   NULL );
203
204
1.85k
on_error_libbfio:
205
1.85k
  libbfio_handle_free(
206
1.85k
   &file_io_handle,
207
1.85k
   NULL );
208
209
1.85k
  return( 0 );
210
1.85k
}
211
212
} /* extern "C" */
213