Coverage Report

Created: 2024-02-25 07:20

/src/libvslvm/ossfuzz/handle_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * OSS-Fuzz target for libvslvm handle type
3
 *
4
 * Copyright (C) 2014-2023, 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
4.49k
{
60
4.49k
  libbfio_handle_t *file_io_handle = NULL;
61
4.49k
  libbfio_pool_t *file_io_pool     = NULL;
62
4.49k
  libvslvm_handle_t *handle        = NULL;
63
4.49k
  int entry_index                  = 0;
64
65
4.49k
  if( libbfio_memory_range_initialize(
66
4.49k
       &file_io_handle,
67
4.49k
       NULL ) != 1 )
68
0
  {
69
0
    return( 0 );
70
0
  }
71
4.49k
  if( libbfio_memory_range_set(
72
4.49k
       file_io_handle,
73
4.49k
       (uint8_t *) data,
74
4.49k
       size,
75
4.49k
       NULL ) != 1 )
76
0
  {
77
0
    goto on_error_libbfio;
78
0
  }
79
4.49k
  if( libbfio_pool_initialize(
80
4.49k
       &file_io_pool,
81
4.49k
       0,
82
4.49k
       0,
83
4.49k
       NULL ) != 1 )
84
0
  {
85
0
    goto on_error_libbfio;
86
0
  }
87
4.49k
  if( libvslvm_handle_initialize(
88
4.49k
       &handle,
89
4.49k
       NULL ) != 1 )
90
0
  {
91
0
    goto on_error_libbfio;
92
0
  }
93
4.49k
  if( libvslvm_handle_open_file_io_handle(
94
4.49k
       handle,
95
4.49k
       file_io_handle,
96
4.49k
       LIBVSLVM_OPEN_READ,
97
4.49k
       NULL ) != 1 )
98
2.57k
  {
99
2.57k
    goto on_error_libvslvm;
100
2.57k
  }
101
1.91k
  if( libbfio_pool_append_handle(
102
1.91k
       file_io_pool,
103
1.91k
       &entry_index,
104
1.91k
       file_io_handle,
105
1.91k
       LIBBFIO_OPEN_READ,
106
1.91k
       NULL ) != 1 )
107
0
  {
108
0
    goto on_error_libvslvm;
109
0
  }
110
  /* The file IO pool takes over management of the file IO handle
111
   */
112
1.91k
  file_io_handle = NULL;
113
114
1.91k
  if( libvslvm_handle_open_physical_volume_files_file_io_pool(
115
1.91k
       handle,
116
1.91k
       file_io_pool,
117
1.91k
       NULL ) != 1 )
118
1.87k
  {
119
1.87k
    goto on_error_libvslvm;
120
1.87k
  }
121
43
  libvslvm_handle_close(
122
43
   handle,
123
43
   NULL );
124
125
4.49k
on_error_libvslvm:
126
4.49k
  libvslvm_handle_free(
127
4.49k
   &handle,
128
4.49k
   NULL );
129
130
4.49k
on_error_libbfio:
131
  /* Note that on error the handle still has a reference to file_io_pool
132
   * that will be closed. Therefore the file IO pool and handle need to
133
   * be freed after closing or freeing the handle.
134
   */
135
4.49k
  if( file_io_pool != NULL )
136
4.49k
  {
137
4.49k
    libbfio_pool_free(
138
4.49k
     &file_io_pool,
139
4.49k
     NULL );
140
4.49k
  }
141
4.49k
  if( file_io_handle != NULL )
142
2.57k
  {
143
2.57k
    libbfio_handle_free(
144
2.57k
     &file_io_handle,
145
2.57k
     NULL );
146
2.57k
  }
147
4.49k
  return( 0 );
148
4.49k
}
149
150
} /* extern "C" */
151