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