Coverage Report

Created: 2026-08-31 07:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvsgpt/ossfuzz/partition_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvsgpt volume type
3
 *
4
 * Copyright (C) 2019-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_libvsgpt.h"
31
32
#if !defined( LIBVSGPT_HAVE_BFIO )
33
34
/* Opens a volume using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBVSGPT_EXTERN \
38
int libvsgpt_volume_open_file_io_handle(
39
     libvsgpt_volume_t *volume,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libvsgpt_error_t **error );
43
44
#endif /* !defined( LIBVSGPT_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
1.09k
{
50
1.09k
  uint8_t buffer[ 512 ];
51
1.09k
  uint8_t guid[ 16 ];
52
53
1.09k
  libbfio_handle_t *file_io_handle = NULL;
54
1.09k
  libvsgpt_partition_t *partition  = NULL;
55
1.09k
  libvsgpt_volume_t *volume        = NULL;
56
1.09k
  off64_t partition_offset         = 0;
57
1.09k
  off64_t volume_offset            = 0;
58
1.09k
  size64_t partition_size          = 0;
59
1.09k
  uint32_t value_32bit             = 0;
60
1.09k
  uint8_t value_8bit               = 0;
61
1.09k
  int number_of_partitions         = 0;
62
1.09k
  int read_iterator                = 0;
63
64
1.09k
  if( libbfio_memory_range_initialize(
65
1.09k
       &file_io_handle,
66
1.09k
       NULL ) != 1 )
67
0
  {
68
0
    return( 0 );
69
0
  }
70
1.09k
  if( libbfio_memory_range_set(
71
1.09k
       file_io_handle,
72
1.09k
       (uint8_t *) data,
73
1.09k
       size,
74
1.09k
       NULL ) != 1 )
75
0
  {
76
0
    goto on_error_libbfio;
77
0
  }
78
1.09k
  if( libvsgpt_volume_initialize(
79
1.09k
       &volume,
80
1.09k
       NULL ) != 1 )
81
0
  {
82
0
    goto on_error_libbfio;
83
0
  }
84
1.09k
  if( libvsgpt_volume_open_file_io_handle(
85
1.09k
       volume,
86
1.09k
       file_io_handle,
87
1.09k
       LIBVSGPT_OPEN_READ,
88
1.09k
       NULL ) != 1 )
89
944
  {
90
944
    goto on_error_libvsgpt_volume;
91
944
  }
92
154
  if( libvsgpt_volume_get_number_of_partitions(
93
154
       volume,
94
154
       &number_of_partitions,
95
154
       NULL ) != 1 )
96
0
  {
97
0
    goto on_error_libvsgpt_volume;
98
0
  }
99
154
  if( number_of_partitions > 0 )
100
140
  {
101
140
    if( libvsgpt_volume_get_partition_by_index(
102
140
         volume,
103
140
         0,
104
140
         &partition,
105
140
         NULL ) != 1 )
106
0
    {
107
0
      goto on_error_libvsgpt_volume;
108
0
    }
109
140
    if( libvsgpt_partition_get_entry_index(
110
140
         partition,
111
140
         &value_32bit,
112
140
         NULL ) != 1 )
113
0
    {
114
0
      goto on_error_libvsgpt_partition;
115
0
    }
116
140
    if( libvsgpt_partition_get_identifier(
117
140
         partition,
118
140
         guid,
119
140
         16,
120
140
         NULL ) != 1 )
121
0
    {
122
0
      goto on_error_libvsgpt_partition;
123
0
    }
124
140
    if( libvsgpt_partition_get_type_identifier(
125
140
         partition,
126
140
         guid,
127
140
         16,
128
140
         NULL ) != 1 )
129
0
    {
130
0
      goto on_error_libvsgpt_partition;
131
0
    }
132
140
    if( libvsgpt_partition_get_type(
133
140
         partition,
134
140
         &value_8bit,
135
140
         NULL ) != 1 )
136
0
    {
137
0
      goto on_error_libvsgpt_partition;
138
0
    }
139
140
    if( libvsgpt_partition_get_volume_offset(
140
140
         partition,
141
140
         &volume_offset,
142
140
         NULL ) != 1 )
143
0
    {
144
0
      goto on_error_libvsgpt_partition;
145
0
    }
146
140
    if( libvsgpt_partition_get_size(
147
140
         partition,
148
140
         &partition_size,
149
140
         NULL ) != 1 )
150
0
    {
151
0
      goto on_error_libvsgpt_partition;
152
0
    }
153
140
    for( read_iterator = 0;
154
5.56k
         read_iterator < 128;
155
5.42k
         read_iterator++ )
156
5.54k
    {
157
5.54k
      if( partition_offset >= partition_size )
158
53
      {
159
53
        break;
160
53
      }
161
5.49k
      if( libvsgpt_partition_read_buffer_at_offset(
162
5.49k
           partition,
163
5.49k
           buffer,
164
5.49k
           497,
165
5.49k
           partition_offset,
166
5.49k
           NULL ) == -1 )
167
71
      {
168
71
        goto on_error_libvsgpt_partition;
169
71
      }
170
5.42k
      partition_offset += 497;
171
5.42k
    }
172
140
on_error_libvsgpt_partition:
173
140
    libvsgpt_partition_free(
174
140
     &partition,
175
140
     NULL );
176
140
  }
177
154
  libvsgpt_volume_close(
178
154
   volume,
179
154
   NULL );
180
181
1.09k
on_error_libvsgpt_volume:
182
1.09k
  libvsgpt_volume_free(
183
1.09k
   &volume,
184
1.09k
   NULL );
185
186
1.09k
on_error_libbfio:
187
1.09k
  libbfio_handle_free(
188
1.09k
   &file_io_handle,
189
1.09k
   NULL );
190
191
1.09k
  return( 0 );
192
1.09k
}
193
194
} /* extern "C" */
195