/src/libodraw/ossfuzz/data_file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libodraw data file type |
3 | | * |
4 | | * Copyright (C) 2010-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_libodraw.h" |
31 | | |
32 | | #if !defined( LIBODRAW_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 | | LIBODRAW_EXTERN \ |
38 | | int libodraw_handle_open_file_io_handle( |
39 | | libodraw_handle_t *handle, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libodraw_error_t **error ); |
43 | | |
44 | | /* Opens the data files using a Basic File IO (bfio) pool |
45 | | * This function assumes the data files are in same order as defined by the table of contents (TOC) file |
46 | | * Returns 1 if successful or -1 on error |
47 | | */ |
48 | | LIBODRAW_EXTERN \ |
49 | | int libodraw_handle_open_data_files_file_io_pool( |
50 | | libodraw_handle_t *handle, |
51 | | libbfio_pool_t *file_io_pool, |
52 | | libodraw_error_t **error ); |
53 | | |
54 | | #endif /* !defined( LIBODRAW_HAVE_BFIO ) */ |
55 | | |
56 | | int LLVMFuzzerTestOneInput( |
57 | | const uint8_t *data, |
58 | | size_t size ) |
59 | 1.25k | { |
60 | 1.25k | char string[ 64 ]; |
61 | | |
62 | 1.25k | libodraw_data_file_t *data_file = NULL; |
63 | 1.25k | libbfio_handle_t *file_io_handle = NULL; |
64 | 1.25k | libbfio_pool_t *file_io_pool = NULL; |
65 | 1.25k | libodraw_handle_t *handle = NULL; |
66 | 1.25k | uint8_t data_file_type = 0; |
67 | 1.25k | size_t filename_set_size = 0; |
68 | 1.25k | size_t string_size = 0; |
69 | 1.25k | int data_file_index = 0; |
70 | 1.25k | int entry_index = 0; |
71 | 1.25k | int number_of_data_files = 0; |
72 | | |
73 | 1.25k | if( libbfio_memory_range_initialize( |
74 | 1.25k | &file_io_handle, |
75 | 1.25k | NULL ) != 1 ) |
76 | 0 | { |
77 | 0 | return( 0 ); |
78 | 0 | } |
79 | 1.25k | if( libbfio_memory_range_set( |
80 | 1.25k | file_io_handle, |
81 | 1.25k | (uint8_t *) data, |
82 | 1.25k | size, |
83 | 1.25k | NULL ) != 1 ) |
84 | 0 | { |
85 | 0 | goto on_error_libbfio; |
86 | 0 | } |
87 | 1.25k | if( libbfio_pool_initialize( |
88 | 1.25k | &file_io_pool, |
89 | 1.25k | 0, |
90 | 1.25k | 0, |
91 | 1.25k | NULL ) != 1 ) |
92 | 0 | { |
93 | 0 | goto on_error_libbfio; |
94 | 0 | } |
95 | 1.25k | if( libodraw_handle_initialize( |
96 | 1.25k | &handle, |
97 | 1.25k | NULL ) != 1 ) |
98 | 0 | { |
99 | 0 | goto on_error_libbfio; |
100 | 0 | } |
101 | 1.25k | if( libodraw_handle_open_file_io_handle( |
102 | 1.25k | handle, |
103 | 1.25k | file_io_handle, |
104 | 1.25k | LIBODRAW_OPEN_READ, |
105 | 1.25k | NULL ) != 1 ) |
106 | 673 | { |
107 | 673 | goto on_error_libodraw; |
108 | 673 | } |
109 | 579 | if( libbfio_pool_append_handle( |
110 | 579 | file_io_pool, |
111 | 579 | &entry_index, |
112 | 579 | file_io_handle, |
113 | 579 | LIBBFIO_OPEN_READ, |
114 | 579 | NULL ) != 1 ) |
115 | 0 | { |
116 | 0 | goto on_error_libodraw; |
117 | 0 | } |
118 | | /* The file IO pool takes over management of the file IO handle |
119 | | */ |
120 | 579 | file_io_handle = NULL; |
121 | | |
122 | 579 | if( libodraw_handle_open_data_files_file_io_pool( |
123 | 579 | handle, |
124 | 579 | file_io_pool, |
125 | 579 | NULL ) != 1 ) |
126 | 376 | { |
127 | 376 | goto on_error_libodraw; |
128 | 376 | } |
129 | 203 | if( libodraw_handle_get_number_of_data_files( |
130 | 203 | handle, |
131 | 203 | &number_of_data_files, |
132 | 203 | NULL ) == 1 ) |
133 | 203 | { |
134 | 203 | if( number_of_data_files > 0 ) |
135 | 203 | { |
136 | 203 | for( data_file_index = 0; |
137 | 1.73k | data_file_index < number_of_data_files; |
138 | 1.53k | data_file_index++ ) |
139 | 1.59k | { |
140 | 1.59k | if( data_file_index >= 16 ) |
141 | 63 | { |
142 | 63 | break; |
143 | 63 | } |
144 | 1.53k | if( libodraw_handle_get_data_file( |
145 | 1.53k | handle, |
146 | 1.53k | data_file_index, |
147 | 1.53k | &data_file, |
148 | 1.53k | NULL ) != 1 ) |
149 | 0 | { |
150 | 0 | goto on_error_libodraw_data_file; |
151 | 0 | } |
152 | 1.53k | if( libodraw_data_file_get_filename_size( |
153 | 1.53k | data_file, |
154 | 1.53k | &string_size, |
155 | 1.53k | NULL ) != 1 ) |
156 | 0 | { |
157 | 0 | goto on_error_libodraw_data_file; |
158 | 0 | } |
159 | 1.53k | if( libodraw_data_file_get_filename( |
160 | 1.53k | data_file, |
161 | 1.53k | string, |
162 | 1.53k | 64, |
163 | 1.53k | NULL ) != 1 ) |
164 | 140 | { |
165 | 140 | goto on_error_libodraw_data_file; |
166 | 140 | } |
167 | 1.39k | if( libodraw_data_file_get_type( |
168 | 1.39k | data_file, |
169 | 1.39k | &data_file_type, |
170 | 1.39k | NULL ) != 1 ) |
171 | 0 | { |
172 | 0 | goto on_error_libodraw_data_file; |
173 | 0 | } |
174 | | /* Set the filename of the data file using the fuzz data |
175 | | * to exercise the name conversion in the data file descriptor |
176 | | */ |
177 | 1.39k | if( size > 0 ) |
178 | 1.39k | { |
179 | 1.39k | filename_set_size = (size_t) 64; |
180 | | |
181 | 1.39k | if( size < filename_set_size ) |
182 | 73 | { |
183 | 73 | filename_set_size = size; |
184 | 73 | } |
185 | 1.39k | libodraw_data_file_set_filename( |
186 | 1.39k | data_file, |
187 | 1.39k | (char *) data, |
188 | 1.39k | filename_set_size, |
189 | 1.39k | NULL ); |
190 | 1.39k | } |
191 | 1.53k | on_error_libodraw_data_file: |
192 | 1.53k | libodraw_data_file_free( |
193 | 1.53k | &data_file, |
194 | 1.53k | NULL ); |
195 | 1.53k | } |
196 | 203 | } |
197 | 203 | } |
198 | 203 | libodraw_handle_close( |
199 | 203 | handle, |
200 | 203 | NULL ); |
201 | | |
202 | 1.25k | on_error_libodraw: |
203 | 1.25k | libodraw_handle_free( |
204 | 1.25k | &handle, |
205 | 1.25k | NULL ); |
206 | | |
207 | 1.25k | on_error_libbfio: |
208 | | /* Note that on error the handle still has a reference to file_io_pool |
209 | | * that will be closed. Therefore the file IO pool and handle need to |
210 | | * be freed after closing or freeing the handle. |
211 | | */ |
212 | 1.25k | if( file_io_pool != NULL ) |
213 | 1.25k | { |
214 | 1.25k | libbfio_pool_free( |
215 | 1.25k | &file_io_pool, |
216 | 1.25k | NULL ); |
217 | 1.25k | } |
218 | 1.25k | if( file_io_handle != NULL ) |
219 | 673 | { |
220 | 673 | libbfio_handle_free( |
221 | 673 | &file_io_handle, |
222 | | NULL ); |
223 | 673 | } |
224 | 1.25k | return( 0 ); |
225 | 1.25k | } |
226 | | |
227 | | } /* extern "C" */ |