/src/libphdi/ossfuzz/extent_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libphdi extent descriptor type |
3 | | * |
4 | | * Copyright (C) 2015-2026, 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_libphdi.h" |
31 | | |
32 | | #if !defined( LIBPHDI_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 | | LIBPHDI_EXTERN \ |
38 | | int libphdi_handle_open_file_io_handle( |
39 | | libphdi_handle_t *handle, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libphdi_error_t **error ); |
43 | | |
44 | | /* Opens the extent data files using a Basic File IO (bfio) pool |
45 | | * This function assumes the extent data files are in same order as defined by the descriptor file |
46 | | * Returns 1 if successful or -1 on error |
47 | | */ |
48 | | LIBPHDI_EXTERN \ |
49 | | int libphdi_handle_open_extent_data_files_file_io_pool( |
50 | | libphdi_handle_t *handle, |
51 | | libbfio_pool_t *file_io_pool, |
52 | | libphdi_error_t **error ); |
53 | | |
54 | | #endif /* !defined( LIBPHDI_HAVE_BFIO ) */ |
55 | | |
56 | | int LLVMFuzzerTestOneInput( |
57 | | const uint8_t *data, |
58 | | size_t size ) |
59 | 721 | { |
60 | 721 | uint8_t utf8_string[ 64 ]; |
61 | 721 | uint16_t utf16_string[ 64 ]; |
62 | | |
63 | 721 | libbfio_handle_t *file_io_handle = NULL; |
64 | 721 | libbfio_pool_t *file_io_pool = NULL; |
65 | 721 | libphdi_handle_t *handle = NULL; |
66 | 721 | libphdi_extent_descriptor_t *extent_descriptor = NULL; |
67 | 721 | libphdi_image_descriptor_t *image_descriptor = NULL; |
68 | 721 | off64_t extent_offset = 0; |
69 | 721 | size64_t extent_size = 0; |
70 | 721 | size_t string_size = 0; |
71 | 721 | int entry_index = 0; |
72 | 721 | int extent_index = 0; |
73 | 721 | int image_index = 0; |
74 | 721 | int image_type = 0; |
75 | 721 | int number_of_extents = 0; |
76 | 721 | int number_of_images = 0; |
77 | | |
78 | 721 | if( libbfio_memory_range_initialize( |
79 | 721 | &file_io_handle, |
80 | 721 | NULL ) != 1 ) |
81 | 0 | { |
82 | 0 | return( 0 ); |
83 | 0 | } |
84 | 721 | if( libbfio_memory_range_set( |
85 | 721 | file_io_handle, |
86 | 721 | (uint8_t *) data, |
87 | 721 | size, |
88 | 721 | NULL ) != 1 ) |
89 | 0 | { |
90 | 0 | goto on_error_libbfio; |
91 | 0 | } |
92 | 721 | if( libbfio_pool_initialize( |
93 | 721 | &file_io_pool, |
94 | 721 | 0, |
95 | 721 | 0, |
96 | 721 | NULL ) != 1 ) |
97 | 0 | { |
98 | 0 | goto on_error_libbfio; |
99 | 0 | } |
100 | 721 | if( libphdi_handle_initialize( |
101 | 721 | &handle, |
102 | 721 | NULL ) != 1 ) |
103 | 0 | { |
104 | 0 | goto on_error_libbfio; |
105 | 0 | } |
106 | 721 | if( libphdi_handle_open_file_io_handle( |
107 | 721 | handle, |
108 | 721 | file_io_handle, |
109 | 721 | LIBPHDI_OPEN_READ, |
110 | 721 | NULL ) != 1 ) |
111 | 721 | { |
112 | 721 | goto on_error_libphdi; |
113 | 721 | } |
114 | 0 | if( libbfio_pool_append_handle( |
115 | 0 | file_io_pool, |
116 | 0 | &entry_index, |
117 | 0 | file_io_handle, |
118 | 0 | LIBBFIO_OPEN_READ, |
119 | 0 | NULL ) != 1 ) |
120 | 0 | { |
121 | 0 | goto on_error_libphdi; |
122 | 0 | } |
123 | | /* The file IO pool takes over management of the file IO handle |
124 | | */ |
125 | 0 | file_io_handle = NULL; |
126 | | |
127 | | /* Note that the extent data files can only be opened if the number |
128 | | * of file IO handles in the pool matches the number of images |
129 | | * defined in the descriptor file. |
130 | | */ |
131 | 0 | libphdi_handle_open_extent_data_files_file_io_pool( |
132 | 0 | handle, |
133 | 0 | file_io_pool, |
134 | 0 | NULL ); |
135 | |
|
136 | 0 | if( libphdi_handle_get_number_of_extents( |
137 | 0 | handle, |
138 | 0 | &number_of_extents, |
139 | 0 | NULL ) != 1 ) |
140 | 0 | { |
141 | 0 | goto on_error_libphdi; |
142 | 0 | } |
143 | 0 | for( extent_index = 0; |
144 | 0 | extent_index < number_of_extents; |
145 | 0 | extent_index++ ) |
146 | 0 | { |
147 | 0 | if( libphdi_handle_get_extent_descriptor_by_index( |
148 | 0 | handle, |
149 | 0 | extent_index, |
150 | 0 | &extent_descriptor, |
151 | 0 | NULL ) != 1 ) |
152 | 0 | { |
153 | 0 | goto on_error_libphdi; |
154 | 0 | } |
155 | 0 | if( libphdi_extent_descriptor_get_range( |
156 | 0 | extent_descriptor, |
157 | 0 | &extent_offset, |
158 | 0 | &extent_size, |
159 | 0 | NULL ) != 1 ) |
160 | 0 | { |
161 | 0 | goto on_error_extent_descriptor; |
162 | 0 | } |
163 | 0 | if( libphdi_extent_descriptor_get_number_of_images( |
164 | 0 | extent_descriptor, |
165 | 0 | &number_of_images, |
166 | 0 | NULL ) != 1 ) |
167 | 0 | { |
168 | 0 | goto on_error_extent_descriptor; |
169 | 0 | } |
170 | 0 | for( image_index = 0; |
171 | 0 | image_index < number_of_images; |
172 | 0 | image_index++ ) |
173 | 0 | { |
174 | 0 | if( libphdi_extent_descriptor_get_image_descriptor_by_index( |
175 | 0 | extent_descriptor, |
176 | 0 | image_index, |
177 | 0 | &image_descriptor, |
178 | 0 | NULL ) != 1 ) |
179 | 0 | { |
180 | 0 | goto on_error_extent_descriptor; |
181 | 0 | } |
182 | 0 | if( libphdi_image_descriptor_get_type( |
183 | 0 | image_descriptor, |
184 | 0 | &image_type, |
185 | 0 | NULL ) != 1 ) |
186 | 0 | { |
187 | 0 | goto on_error_image_descriptor; |
188 | 0 | } |
189 | 0 | if( libphdi_image_descriptor_get_utf8_filename_size( |
190 | 0 | image_descriptor, |
191 | 0 | &string_size, |
192 | 0 | NULL ) == -1 ) |
193 | 0 | { |
194 | 0 | goto on_error_image_descriptor; |
195 | 0 | } |
196 | 0 | if( libphdi_image_descriptor_get_utf8_filename( |
197 | 0 | image_descriptor, |
198 | 0 | utf8_string, |
199 | 0 | 64, |
200 | 0 | NULL ) == -1 ) |
201 | 0 | { |
202 | 0 | goto on_error_image_descriptor; |
203 | 0 | } |
204 | 0 | if( libphdi_image_descriptor_get_utf16_filename_size( |
205 | 0 | image_descriptor, |
206 | 0 | &string_size, |
207 | 0 | NULL ) == -1 ) |
208 | 0 | { |
209 | 0 | goto on_error_image_descriptor; |
210 | 0 | } |
211 | 0 | if( libphdi_image_descriptor_get_utf16_filename( |
212 | 0 | image_descriptor, |
213 | 0 | utf16_string, |
214 | 0 | 64, |
215 | 0 | NULL ) == -1 ) |
216 | 0 | { |
217 | 0 | goto on_error_image_descriptor; |
218 | 0 | } |
219 | 0 | on_error_image_descriptor: |
220 | 0 | libphdi_image_descriptor_free( |
221 | 0 | &image_descriptor, |
222 | 0 | NULL ); |
223 | 0 | } |
224 | 0 | on_error_extent_descriptor: |
225 | 0 | libphdi_extent_descriptor_free( |
226 | 0 | &extent_descriptor, |
227 | 0 | NULL ); |
228 | 0 | } |
229 | 0 | libphdi_handle_close( |
230 | 0 | handle, |
231 | 0 | NULL ); |
232 | |
|
233 | 721 | on_error_libphdi: |
234 | 721 | libphdi_handle_free( |
235 | 721 | &handle, |
236 | 721 | NULL ); |
237 | | |
238 | 721 | on_error_libbfio: |
239 | | /* Note that on error the handle still has a reference to file_io_pool |
240 | | * that will be closed. Therefore the file IO pool and handle need to |
241 | | * be freed after closing or freeing the handle. |
242 | | */ |
243 | 721 | if( file_io_pool != NULL ) |
244 | 721 | { |
245 | 721 | libbfio_pool_free( |
246 | 721 | &file_io_pool, |
247 | 721 | NULL ); |
248 | 721 | } |
249 | 721 | if( file_io_handle != NULL ) |
250 | 721 | { |
251 | 721 | libbfio_handle_free( |
252 | 721 | &file_io_handle, |
253 | | NULL ); |
254 | 721 | } |
255 | 721 | return( 0 ); |
256 | 721 | } |
257 | | |
258 | | } /* extern "C" */ |