Coverage Report

Created: 2025-06-13 07:22

/src/libvslvm/ossfuzz/logical_volume_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * OSS-Fuzz target for libvslvm handle type
3
 *
4
 * Copyright (C) 2014-2024, 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.11k
{
60
3.11k
  libbfio_handle_t *file_io_handle          = NULL;
61
3.11k
  libbfio_pool_t *file_io_pool              = NULL;
62
3.11k
  libvslvm_handle_t *handle                 = NULL;
63
3.11k
  libvslvm_logical_volume_t *logical_volume = NULL;
64
3.11k
  libvslvm_volume_group_t *volume_group     = NULL;
65
3.11k
  int entry_index                           = 0;
66
3.11k
  int number_of_logical_volumes             = 0;
67
68
3.11k
  if( libbfio_memory_range_initialize(
69
3.11k
       &file_io_handle,
70
3.11k
       NULL ) != 1 )
71
0
  {
72
0
    return( 0 );
73
0
  }
74
3.11k
  if( libbfio_memory_range_set(
75
3.11k
       file_io_handle,
76
3.11k
       (uint8_t *) data,
77
3.11k
       size,
78
3.11k
       NULL ) != 1 )
79
0
  {
80
0
    goto on_error_libbfio;
81
0
  }
82
3.11k
  if( libbfio_pool_initialize(
83
3.11k
       &file_io_pool,
84
3.11k
       0,
85
3.11k
       0,
86
3.11k
       NULL ) != 1 )
87
0
  {
88
0
    goto on_error_libbfio;
89
0
  }
90
3.11k
  if( libvslvm_handle_initialize(
91
3.11k
       &handle,
92
3.11k
       NULL ) != 1 )
93
0
  {
94
0
    goto on_error_libbfio;
95
0
  }
96
3.11k
  if( libvslvm_handle_open_file_io_handle(
97
3.11k
       handle,
98
3.11k
       file_io_handle,
99
3.11k
       LIBVSLVM_OPEN_READ,
100
3.11k
       NULL ) != 1 )
101
1.15k
  {
102
1.15k
    goto on_error_libvslvm_handle;
103
1.15k
  }
104
1.95k
  if( libbfio_pool_append_handle(
105
1.95k
       file_io_pool,
106
1.95k
       &entry_index,
107
1.95k
       file_io_handle,
108
1.95k
       LIBBFIO_OPEN_READ,
109
1.95k
       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.95k
  file_io_handle = NULL;
116
117
1.95k
  if( libvslvm_handle_open_physical_volume_files_file_io_pool(
118
1.95k
       handle,
119
1.95k
       file_io_pool,
120
1.95k
       NULL ) != 1 )
121
1.51k
  {
122
1.51k
    goto on_error_libvslvm_handle;
123
1.51k
  }
124
445
  if( libvslvm_handle_get_volume_group(
125
445
       handle,
126
445
       &volume_group,
127
445
       NULL ) == 1 )
128
445
  {
129
445
    if( libvslvm_volume_group_get_number_of_logical_volumes(
130
445
         volume_group,
131
445
         &number_of_logical_volumes,
132
445
         NULL ) != 1 )
133
0
    {
134
0
      goto on_error_libvslvm_volume_group;
135
0
    }
136
445
    if( number_of_logical_volumes > 0 )
137
263
    {
138
263
      if( libvslvm_volume_group_get_logical_volume(
139
263
           volume_group,
140
263
           0,
141
263
           &logical_volume,
142
263
           NULL ) == 1 )
143
38
      {
144
38
        libvslvm_logical_volume_free(
145
38
         &logical_volume,
146
38
         NULL );
147
38
      }
148
263
    }
149
445
on_error_libvslvm_volume_group:
150
445
    libvslvm_volume_group_free(
151
445
     &volume_group,
152
445
     NULL );
153
445
  }
154
445
  libvslvm_handle_close(
155
445
   handle,
156
445
   NULL );
157
158
3.11k
on_error_libvslvm_handle:
159
3.11k
  libvslvm_handle_free(
160
3.11k
   &handle,
161
3.11k
   NULL );
162
163
3.11k
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.11k
  if( file_io_pool != NULL )
169
3.11k
  {
170
3.11k
    libbfio_pool_free(
171
3.11k
     &file_io_pool,
172
3.11k
     NULL );
173
3.11k
  }
174
3.11k
  if( file_io_handle != NULL )
175
1.15k
  {
176
1.15k
    libbfio_handle_free(
177
1.15k
     &file_io_handle,
178
1.15k
     NULL );
179
1.15k
  }
180
3.11k
  return( 0 );
181
3.11k
}
182
183
} /* extern "C" */
184