/src/libfsapfs/ossfuzz/volume_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * OSS-Fuzz target for libfsapfs volume type |
3 | | * |
4 | | * Copyright (C) 2018-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_libfsapfs.h" |
31 | | |
32 | | #if !defined( LIBFSAPFS_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a container using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBFSAPFS_EXTERN \ |
38 | | int libfsapfs_container_open_file_io_handle( |
39 | | libfsapfs_container_t *container, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libfsapfs_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBFSAPFS_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 3.22k | { |
50 | 3.22k | uint8_t volume_identifier[ 16 ]; |
51 | 3.22k | uint8_t string_value[ 64 ]; |
52 | | |
53 | 3.22k | libbfio_handle_t *file_io_handle = NULL; |
54 | 3.22k | libfsapfs_container_t *container = NULL; |
55 | 3.22k | libfsapfs_volume_t *volume = NULL; |
56 | 3.22k | size64_t volume_size = 0; |
57 | 3.22k | size_t string_size = 0; |
58 | 3.22k | uint64_t compatible_features_flags = 0; |
59 | 3.22k | uint64_t incompatible_features_flags = 0; |
60 | 3.22k | uint64_t read_only_compatible_features_flags = 0; |
61 | 3.22k | int number_of_volumes = 0; |
62 | 3.22k | int result = 0; |
63 | | |
64 | 3.22k | if( libbfio_memory_range_initialize( |
65 | 3.22k | &file_io_handle, |
66 | 3.22k | NULL ) != 1 ) |
67 | 0 | { |
68 | 0 | return( 0 ); |
69 | 0 | } |
70 | 3.22k | if( libbfio_memory_range_set( |
71 | 3.22k | file_io_handle, |
72 | 3.22k | (uint8_t *) data, |
73 | 3.22k | size, |
74 | 3.22k | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | goto on_error_libbfio; |
77 | 0 | } |
78 | 3.22k | if( libfsapfs_container_initialize( |
79 | 3.22k | &container, |
80 | 3.22k | NULL ) != 1 ) |
81 | 0 | { |
82 | 0 | goto on_error_libbfio; |
83 | 0 | } |
84 | 3.22k | if( libfsapfs_container_open_file_io_handle( |
85 | 3.22k | container, |
86 | 3.22k | file_io_handle, |
87 | 3.22k | LIBFSAPFS_OPEN_READ, |
88 | 3.22k | NULL ) != 1 ) |
89 | 1.31k | { |
90 | 1.31k | goto on_error_libfsapfs_container; |
91 | 1.31k | } |
92 | 1.91k | if( libfsapfs_container_get_number_of_volumes( |
93 | 1.91k | container, |
94 | 1.91k | &number_of_volumes, |
95 | 1.91k | NULL ) != 1 ) |
96 | 0 | { |
97 | 0 | goto on_error_libfsapfs_container; |
98 | 0 | } |
99 | 1.91k | if( number_of_volumes > 0 ) |
100 | 1.91k | { |
101 | 1.91k | result = libfsapfs_container_get_volume_by_index( |
102 | 1.91k | container, |
103 | 1.91k | 0, |
104 | 1.91k | &volume, |
105 | 1.91k | NULL ); |
106 | | |
107 | 1.91k | if( result != -1 ) |
108 | 23 | { |
109 | 23 | libfsapfs_volume_get_features_flags( |
110 | 23 | volume, |
111 | 23 | &compatible_features_flags, |
112 | 23 | &incompatible_features_flags, |
113 | 23 | &read_only_compatible_features_flags, |
114 | 23 | NULL ); |
115 | | |
116 | 23 | libfsapfs_volume_get_size( |
117 | 23 | volume, |
118 | 23 | &volume_size, |
119 | 23 | NULL ); |
120 | | |
121 | 23 | libfsapfs_volume_get_identifier( |
122 | 23 | volume, |
123 | 23 | volume_identifier, |
124 | 23 | 16, |
125 | 23 | NULL ); |
126 | | |
127 | 23 | libfsapfs_volume_get_utf8_name_size( |
128 | 23 | volume, |
129 | 23 | &string_size, |
130 | 23 | NULL ); |
131 | | |
132 | 23 | libfsapfs_volume_get_utf8_name( |
133 | 23 | volume, |
134 | 23 | string_value, |
135 | 23 | 64, |
136 | 23 | NULL ); |
137 | | |
138 | 23 | libfsapfs_volume_is_locked( |
139 | 23 | volume, |
140 | 23 | NULL ); |
141 | | |
142 | 23 | libfsapfs_volume_free( |
143 | 23 | &volume, |
144 | 23 | NULL ); |
145 | 23 | } |
146 | 1.91k | } |
147 | 3.22k | on_error_libfsapfs_container: |
148 | 3.22k | libfsapfs_container_free( |
149 | 3.22k | &container, |
150 | 3.22k | NULL ); |
151 | | |
152 | 3.22k | on_error_libbfio: |
153 | 3.22k | libbfio_handle_free( |
154 | 3.22k | &file_io_handle, |
155 | 3.22k | NULL ); |
156 | | |
157 | 3.22k | return( 0 ); |
158 | 3.22k | } |
159 | | |
160 | | } /* extern "C" */ |
161 | | |