Coverage Report

Created: 2024-10-02 06:58

/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
2.98k
{
60
2.98k
  libbfio_handle_t *file_io_handle          = NULL;
61
2.98k
  libbfio_pool_t *file_io_pool              = NULL;
62
2.98k
  libvslvm_handle_t *handle                 = NULL;
63
2.98k
  libvslvm_logical_volume_t *logical_volume = NULL;
64
2.98k
  libvslvm_volume_group_t *volume_group     = NULL;
65
2.98k
  int entry_index                           = 0;
66
2.98k
  int number_of_logical_volumes             = 0;
67
68
2.98k
  if( libbfio_memory_range_initialize(
69
2.98k
       &file_io_handle,
70
2.98k
       NULL ) != 1 )
71
0
  {
72
0
    return( 0 );
73
0
  }
74
2.98k
  if( libbfio_memory_range_set(
75
2.98k
       file_io_handle,
76
2.98k
       (uint8_t *) data,
77
2.98k
       size,
78
2.98k
       NULL ) != 1 )
79
0
  {
80
0
    goto on_error_libbfio;
81
0
  }
82
2.98k
  if( libbfio_pool_initialize(
83
2.98k
       &file_io_pool,
84
2.98k
       0,
85
2.98k
       0,
86
2.98k
       NULL ) != 1 )
87
0
  {
88
0
    goto on_error_libbfio;
89
0
  }
90
2.98k
  if( libvslvm_handle_initialize(
91
2.98k
       &handle,
92
2.98k
       NULL ) != 1 )
93
0
  {
94
0
    goto on_error_libbfio;
95
0
  }
96
2.98k
  if( libvslvm_handle_open_file_io_handle(
97
2.98k
       handle,
98
2.98k
       file_io_handle,
99
2.98k
       LIBVSLVM_OPEN_READ,
100
2.98k
       NULL ) != 1 )
101
1.04k
  {
102
1.04k
    goto on_error_libvslvm_handle;
103
1.04k
  }
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.51k
  {
122
1.51k
    goto on_error_libvslvm_handle;
123
1.51k
  }
124
428
  if( libvslvm_handle_get_volume_group(
125
428
       handle,
126
428
       &volume_group,
127
428
       NULL ) == 1 )
128
428
  {
129
428
    if( libvslvm_volume_group_get_number_of_logical_volumes(
130
428
         volume_group,
131
428
         &number_of_logical_volumes,
132
428
         NULL ) != 1 )
133
0
    {
134
0
      goto on_error_libvslvm_volume_group;
135
0
    }
136
428
    if( number_of_logical_volumes > 0 )
137
247
    {
138
247
      if( libvslvm_volume_group_get_logical_volume(
139
247
           volume_group,
140
247
           0,
141
247
           &logical_volume,
142
247
           NULL ) == 1 )
143
32
      {
144
32
        libvslvm_logical_volume_free(
145
32
         &logical_volume,
146
32
         NULL );
147
32
      }
148
247
    }
149
428
on_error_libvslvm_volume_group:
150
428
    libvslvm_volume_group_free(
151
428
     &volume_group,
152
428
     NULL );
153
428
  }
154
428
  libvslvm_handle_close(
155
428
   handle,
156
428
   NULL );
157
158
2.98k
on_error_libvslvm_handle:
159
2.98k
  libvslvm_handle_free(
160
2.98k
   &handle,
161
2.98k
   NULL );
162
163
2.98k
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
2.98k
  if( file_io_pool != NULL )
169
2.98k
  {
170
2.98k
    libbfio_pool_free(
171
2.98k
     &file_io_pool,
172
2.98k
     NULL );
173
2.98k
  }
174
2.98k
  if( file_io_handle != NULL )
175
1.04k
  {
176
1.04k
    libbfio_handle_free(
177
1.04k
     &file_io_handle,
178
1.04k
     NULL );
179
1.04k
  }
180
2.98k
  return( 0 );
181
2.98k
}
182
183
} /* extern "C" */
184