/src/libphdi/ossfuzz/handle_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libphdi file 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 | | #endif /* !defined( LIBPHDI_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 849 | { |
50 | 849 | uint8_t buffer[ 512 ]; |
51 | 849 | uint8_t utf8_string[ 64 ]; |
52 | 849 | uint16_t utf16_string[ 64 ]; |
53 | | |
54 | 849 | libbfio_handle_t *file_io_handle = NULL; |
55 | 849 | libphdi_handle_t *handle = NULL; |
56 | 849 | off64_t media_offset = 0; |
57 | 849 | size64_t media_size = 0; |
58 | 849 | size_t string_size = 0; |
59 | 849 | int number_of_extents = 0; |
60 | 849 | int number_of_snapshots = 0; |
61 | 849 | int read_iterator = 0; |
62 | | |
63 | 849 | if( libbfio_memory_range_initialize( |
64 | 849 | &file_io_handle, |
65 | 849 | NULL ) != 1 ) |
66 | 0 | { |
67 | 0 | return( 0 ); |
68 | 0 | } |
69 | 849 | if( libbfio_memory_range_set( |
70 | 849 | file_io_handle, |
71 | 849 | (uint8_t *) data, |
72 | 849 | size, |
73 | 849 | NULL ) != 1 ) |
74 | 0 | { |
75 | 0 | goto on_error_libbfio; |
76 | 0 | } |
77 | 849 | if( libphdi_handle_initialize( |
78 | 849 | &handle, |
79 | 849 | NULL ) != 1 ) |
80 | 0 | { |
81 | 0 | goto on_error_libbfio; |
82 | 0 | } |
83 | 849 | if( libphdi_handle_open_file_io_handle( |
84 | 849 | handle, |
85 | 849 | file_io_handle, |
86 | 849 | LIBPHDI_OPEN_READ, |
87 | 849 | NULL ) != 1 ) |
88 | 849 | { |
89 | 849 | goto on_error_libphdi; |
90 | 849 | } |
91 | 0 | if( libphdi_handle_get_utf8_name_size( |
92 | 0 | handle, |
93 | 0 | &string_size, |
94 | 0 | NULL ) == -1 ) |
95 | 0 | { |
96 | 0 | goto on_error_libphdi; |
97 | 0 | } |
98 | 0 | if( libphdi_handle_get_utf8_name( |
99 | 0 | handle, |
100 | 0 | utf8_string, |
101 | 0 | 64, |
102 | 0 | NULL ) == -1 ) |
103 | 0 | { |
104 | 0 | goto on_error_libphdi; |
105 | 0 | } |
106 | 0 | if( libphdi_handle_get_utf16_name_size( |
107 | 0 | handle, |
108 | 0 | &string_size, |
109 | 0 | NULL ) == -1 ) |
110 | 0 | { |
111 | 0 | goto on_error_libphdi; |
112 | 0 | } |
113 | 0 | if( libphdi_handle_get_utf16_name( |
114 | 0 | handle, |
115 | 0 | utf16_string, |
116 | 0 | 64, |
117 | 0 | NULL ) == -1 ) |
118 | 0 | { |
119 | 0 | goto on_error_libphdi; |
120 | 0 | } |
121 | 0 | if( libphdi_handle_get_number_of_extents( |
122 | 0 | handle, |
123 | 0 | &number_of_extents, |
124 | 0 | NULL ) != 1 ) |
125 | 0 | { |
126 | 0 | goto on_error_libphdi; |
127 | 0 | } |
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 | if( libphdi_handle_get_media_size( |
136 | 0 | handle, |
137 | 0 | &media_size, |
138 | 0 | NULL ) != 1 ) |
139 | 0 | { |
140 | 0 | goto on_error_libphdi; |
141 | 0 | } |
142 | 0 | for( read_iterator = 0; |
143 | 0 | read_iterator < 128; |
144 | 0 | read_iterator++ ) |
145 | 0 | { |
146 | 0 | if( media_offset >= media_size ) |
147 | 0 | { |
148 | 0 | break; |
149 | 0 | } |
150 | 0 | if( libphdi_handle_read_buffer_at_offset( |
151 | 0 | handle, |
152 | 0 | buffer, |
153 | 0 | 497, |
154 | 0 | media_offset, |
155 | 0 | NULL ) == -1 ) |
156 | 0 | { |
157 | 0 | goto on_error_libphdi; |
158 | 0 | } |
159 | 0 | media_offset += 497; |
160 | 0 | } |
161 | 0 | libphdi_handle_close( |
162 | 0 | handle, |
163 | 0 | NULL ); |
164 | |
|
165 | 849 | on_error_libphdi: |
166 | 849 | libphdi_handle_free( |
167 | 849 | &handle, |
168 | 849 | NULL ); |
169 | | |
170 | 849 | on_error_libbfio: |
171 | 849 | libbfio_handle_free( |
172 | 849 | &file_io_handle, |
173 | 849 | NULL ); |
174 | | |
175 | 849 | return( 0 ); |
176 | 849 | } |
177 | | |
178 | | } /* extern "C" */ |
179 | | |