Coverage Report

Created: 2026-03-05 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvslvm/ossfuzz/logical_volume_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvslvm handle type
3
 *
4
 * Copyright (C) 2014-2025, 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_libvslvm.h"
31
32
#if !defined( LIBVSLVM_HAVE_BFIO )
33
34
/* Opens a handle using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBVSLVM_EXTERN \
38
int libvslvm_handle_open_file_io_handle(
39
     libvslvm_handle_t *handle,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libvslvm_error_t **error );
43
44
/* Opens the physical volume files
45
 * This function assumes the physical volume files are in same order as defined by the metadata
46
 * Returns 1 if successful or -1 on error
47
 */
48
LIBVSLVM_EXTERN \
49
int libvslvm_handle_open_physical_volume_files_file_io_pool(
50
     libvslvm_handle_t *handle,
51
     libbfio_pool_t *file_io_pool,
52
     libvslvm_error_t **error );
53
54
#endif /* !defined( LIBVSLVM_HAVE_BFIO ) */
55
56
int LLVMFuzzerTestOneInput(
57
     const uint8_t *data,
58
     size_t size )
59
3.09k
{
60
3.09k
  libbfio_handle_t *file_io_handle          = NULL;
61
3.09k
  libbfio_pool_t *file_io_pool              = NULL;
62
3.09k
  libvslvm_handle_t *handle                 = NULL;
63
3.09k
  libvslvm_logical_volume_t *logical_volume = NULL;
64
3.09k
  libvslvm_volume_group_t *volume_group     = NULL;
65
3.09k
  int entry_index                           = 0;
66
3.09k
  int number_of_logical_volumes             = 0;
67
68
3.09k
  if( libbfio_memory_range_initialize(
69
3.09k
       &file_io_handle,
70
3.09k
       NULL ) != 1 )
71
0
  {
72
0
    return( 0 );
73
0
  }
74
3.09k
  if( libbfio_memory_range_set(
75
3.09k
       file_io_handle,
76
3.09k
       (uint8_t *) data,
77
3.09k
       size,
78
3.09k
       NULL ) != 1 )
79
0
  {
80
0
    goto on_error_libbfio;
81
0
  }
82
3.09k
  if( libbfio_pool_initialize(
83
3.09k
       &file_io_pool,
84
3.09k
       0,
85
3.09k
       0,
86
3.09k
       NULL ) != 1 )
87
0
  {
88
0
    goto on_error_libbfio;
89
0
  }
90
3.09k
  if( libvslvm_handle_initialize(
91
3.09k
       &handle,
92
3.09k
       NULL ) != 1 )
93
0
  {
94
0
    goto on_error_libbfio;
95
0
  }
96
3.09k
  if( libvslvm_handle_open_file_io_handle(
97
3.09k
       handle,
98
3.09k
       file_io_handle,
99
3.09k
       LIBVSLVM_OPEN_READ,
100
3.09k
       NULL ) != 1 )
101
1.15k
  {
102
1.15k
    goto on_error_libvslvm_handle;
103
1.15k
  }
104
1.93k
  if( libbfio_pool_append_handle(
105
1.93k
       file_io_pool,
106
1.93k
       &entry_index,
107
1.93k
       file_io_handle,
108
1.93k
       LIBBFIO_OPEN_READ,
109
1.93k
       NULL ) != 1 )
110
0
  {
111
0
    goto on_error_libvslvm_handle;
112
0
  }
113
  /* The file IO pool takes over management of the file IO handle
114
   */
115
1.93k
  file_io_handle = NULL;
116
117
1.93k
  if( libvslvm_handle_open_physical_volume_files_file_io_pool(
118
1.93k
       handle,
119
1.93k
       file_io_pool,
120
1.93k
       NULL ) != 1 )
121
1.48k
  {
122
1.48k
    goto on_error_libvslvm_handle;
123
1.48k
  }
124
455
  if( libvslvm_handle_get_volume_group(
125
455
       handle,
126
455
       &volume_group,
127
455
       NULL ) == 1 )
128
455
  {
129
455
    if( libvslvm_volume_group_get_number_of_logical_volumes(
130
455
         volume_group,
131
455
         &number_of_logical_volumes,
132
455
         NULL ) != 1 )
133
0
    {
134
0
      goto on_error_libvslvm_volume_group;
135
0
    }
136
455
    if( number_of_logical_volumes > 0 )
137
280
    {
138
280
      if( libvslvm_volume_group_get_logical_volume(
139
280
           volume_group,
140
280
           0,
141
280
           &logical_volume,
142
280
           NULL ) == 1 )
143
47
      {
144
47
        libvslvm_logical_volume_free(
145
47
         &logical_volume,
146
47
         NULL );
147
47
      }
148
280
    }
149
455
on_error_libvslvm_volume_group:
150
455
    libvslvm_volume_group_free(
151
455
     &volume_group,
152
455
     NULL );
153
455
  }
154
455
  libvslvm_handle_close(
155
455
   handle,
156
455
   NULL );
157
158
3.09k
on_error_libvslvm_handle:
159
3.09k
  libvslvm_handle_free(
160
3.09k
   &handle,
161
3.09k
   NULL );
162
163
3.09k
on_error_libbfio:
164
  /* Note that on error the handle still has a reference to file_io_pool
165
   * that will be closed. Therefore the file IO pool and handle need to
166
   * be freed after closing or freeing the handle.
167
   */
168
3.09k
  if( file_io_pool != NULL )
169
3.09k
  {
170
3.09k
    libbfio_pool_free(
171
3.09k
     &file_io_pool,
172
3.09k
     NULL );
173
3.09k
  }
174
3.09k
  if( file_io_handle != NULL )
175
1.15k
  {
176
1.15k
    libbfio_handle_free(
177
1.15k
     &file_io_handle,
178
     NULL );
179
1.15k
  }
180
3.09k
  return( 0 );
181
3.09k
}
182
183
} /* extern "C" */
184