/src/libphdi/ossfuzz/snapshot_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libphdi snapshot 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 | 767 | { |
60 | 767 | uint8_t guid_data[ 16 ]; |
61 | | |
62 | 767 | libbfio_handle_t *file_io_handle = NULL; |
63 | 767 | libbfio_pool_t *file_io_pool = NULL; |
64 | 767 | libphdi_handle_t *handle = NULL; |
65 | 767 | libphdi_snapshot_t *snapshot = NULL; |
66 | 767 | int entry_index = 0; |
67 | 767 | int number_of_snapshots = 0; |
68 | 767 | int snapshot_index = 0; |
69 | | |
70 | 767 | if( libbfio_memory_range_initialize( |
71 | 767 | &file_io_handle, |
72 | 767 | NULL ) != 1 ) |
73 | 0 | { |
74 | 0 | return( 0 ); |
75 | 0 | } |
76 | 767 | if( libbfio_memory_range_set( |
77 | 767 | file_io_handle, |
78 | 767 | (uint8_t *) data, |
79 | 767 | size, |
80 | 767 | NULL ) != 1 ) |
81 | 0 | { |
82 | 0 | goto on_error_libbfio; |
83 | 0 | } |
84 | 767 | if( libbfio_pool_initialize( |
85 | 767 | &file_io_pool, |
86 | 767 | 0, |
87 | 767 | 0, |
88 | 767 | NULL ) != 1 ) |
89 | 0 | { |
90 | 0 | goto on_error_libbfio; |
91 | 0 | } |
92 | 767 | if( libphdi_handle_initialize( |
93 | 767 | &handle, |
94 | 767 | NULL ) != 1 ) |
95 | 0 | { |
96 | 0 | goto on_error_libbfio; |
97 | 0 | } |
98 | 767 | if( libphdi_handle_open_file_io_handle( |
99 | 767 | handle, |
100 | 767 | file_io_handle, |
101 | 767 | LIBPHDI_OPEN_READ, |
102 | 767 | NULL ) != 1 ) |
103 | 767 | { |
104 | 767 | goto on_error_libphdi; |
105 | 767 | } |
106 | 0 | if( libbfio_pool_append_handle( |
107 | 0 | file_io_pool, |
108 | 0 | &entry_index, |
109 | 0 | file_io_handle, |
110 | 0 | LIBBFIO_OPEN_READ, |
111 | 0 | NULL ) != 1 ) |
112 | 0 | { |
113 | 0 | goto on_error_libphdi; |
114 | 0 | } |
115 | | /* The file IO pool takes over management of the file IO handle |
116 | | */ |
117 | 0 | file_io_handle = NULL; |
118 | | |
119 | | /* Note that the extent data files can only be opened if the number |
120 | | * of file IO handles in the pool matches the number of images |
121 | | * defined in the descriptor file. |
122 | | */ |
123 | 0 | libphdi_handle_open_extent_data_files_file_io_pool( |
124 | 0 | handle, |
125 | 0 | file_io_pool, |
126 | 0 | NULL ); |
127 | |
|
128 | 0 | if( libphdi_handle_get_number_of_snapshots( |
129 | 0 | handle, |
130 | 0 | &number_of_snapshots, |
131 | 0 | NULL ) != 1 ) |
132 | 0 | { |
133 | 0 | goto on_error_libphdi; |
134 | 0 | } |
135 | 0 | for( snapshot_index = 0; |
136 | 0 | snapshot_index < number_of_snapshots; |
137 | 0 | snapshot_index++ ) |
138 | 0 | { |
139 | 0 | if( libphdi_handle_get_snapshot_by_index( |
140 | 0 | handle, |
141 | 0 | snapshot_index, |
142 | 0 | &snapshot, |
143 | 0 | NULL ) != 1 ) |
144 | 0 | { |
145 | 0 | goto on_error_libphdi; |
146 | 0 | } |
147 | 0 | if( libphdi_snapshot_get_identifier( |
148 | 0 | snapshot, |
149 | 0 | guid_data, |
150 | 0 | 16, |
151 | 0 | NULL ) != 1 ) |
152 | 0 | { |
153 | 0 | goto on_error_snapshot; |
154 | 0 | } |
155 | | /* Note that the parent identifier is not available |
156 | | * for a snapshot without a parent snapshot. |
157 | | */ |
158 | 0 | if( libphdi_snapshot_get_parent_identifier( |
159 | 0 | snapshot, |
160 | 0 | guid_data, |
161 | 0 | 16, |
162 | 0 | NULL ) == -1 ) |
163 | 0 | { |
164 | 0 | goto on_error_snapshot; |
165 | 0 | } |
166 | 0 | on_error_snapshot: |
167 | 0 | libphdi_snapshot_free( |
168 | 0 | &snapshot, |
169 | 0 | NULL ); |
170 | 0 | } |
171 | 0 | libphdi_handle_close( |
172 | 0 | handle, |
173 | 0 | NULL ); |
174 | |
|
175 | 767 | on_error_libphdi: |
176 | 767 | libphdi_handle_free( |
177 | 767 | &handle, |
178 | 767 | NULL ); |
179 | | |
180 | 767 | on_error_libbfio: |
181 | | /* Note that on error the handle still has a reference to file_io_pool |
182 | | * that will be closed. Therefore the file IO pool and handle need to |
183 | | * be freed after closing or freeing the handle. |
184 | | */ |
185 | 767 | if( file_io_pool != NULL ) |
186 | 767 | { |
187 | 767 | libbfio_pool_free( |
188 | 767 | &file_io_pool, |
189 | 767 | NULL ); |
190 | 767 | } |
191 | 767 | if( file_io_handle != NULL ) |
192 | 767 | { |
193 | 767 | libbfio_handle_free( |
194 | 767 | &file_io_handle, |
195 | | NULL ); |
196 | 767 | } |
197 | 767 | return( 0 ); |
198 | 767 | } |
199 | | |
200 | | } /* extern "C" */ |