/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.32k  | { | 
50  | 3.32k  |   uint8_t volume_identifier[ 16 ];  | 
51  | 3.32k  |   uint8_t string_value[ 64 ];  | 
52  |  |  | 
53  | 3.32k  |   libbfio_handle_t *file_io_handle             = NULL;  | 
54  | 3.32k  |   libfsapfs_container_t *container             = NULL;  | 
55  | 3.32k  |   libfsapfs_volume_t *volume                   = NULL;  | 
56  | 3.32k  |   size64_t volume_size                         = 0;  | 
57  | 3.32k  |   size_t string_size                           = 0;  | 
58  | 3.32k  |   uint64_t compatible_features_flags           = 0;  | 
59  | 3.32k  |   uint64_t incompatible_features_flags         = 0;  | 
60  | 3.32k  |   uint64_t read_only_compatible_features_flags = 0;  | 
61  | 3.32k  |   int number_of_volumes                        = 0;  | 
62  | 3.32k  |   int result                                   = 0;  | 
63  |  |  | 
64  | 3.32k  |   if( libbfio_memory_range_initialize(  | 
65  | 3.32k  |        &file_io_handle,  | 
66  | 3.32k  |        NULL ) != 1 )  | 
67  | 0  |   { | 
68  | 0  |     return( 0 );  | 
69  | 0  |   }  | 
70  | 3.32k  |   if( libbfio_memory_range_set(  | 
71  | 3.32k  |        file_io_handle,  | 
72  | 3.32k  |        (uint8_t *) data,  | 
73  | 3.32k  |        size,  | 
74  | 3.32k  |        NULL ) != 1 )  | 
75  | 0  |   { | 
76  | 0  |     goto on_error_libbfio;  | 
77  | 0  |   }  | 
78  | 3.32k  |   if( libfsapfs_container_initialize(  | 
79  | 3.32k  |        &container,  | 
80  | 3.32k  |        NULL ) != 1 )  | 
81  | 0  |   { | 
82  | 0  |     goto on_error_libbfio;  | 
83  | 0  |   }  | 
84  | 3.32k  |   if( libfsapfs_container_open_file_io_handle(  | 
85  | 3.32k  |        container,  | 
86  | 3.32k  |        file_io_handle,  | 
87  | 3.32k  |        LIBFSAPFS_OPEN_READ,  | 
88  | 3.32k  |        NULL ) != 1 )  | 
89  | 1.36k  |   { | 
90  | 1.36k  |     goto on_error_libfsapfs_container;  | 
91  | 1.36k  |   }  | 
92  | 1.96k  |   if( libfsapfs_container_get_number_of_volumes(  | 
93  | 1.96k  |        container,  | 
94  | 1.96k  |        &number_of_volumes,  | 
95  | 1.96k  |        NULL ) != 1 )  | 
96  | 0  |   { | 
97  | 0  |     goto on_error_libfsapfs_container;  | 
98  | 0  |   }  | 
99  | 1.96k  |   if( number_of_volumes > 0 )  | 
100  | 1.96k  |   { | 
101  | 1.96k  |     result = libfsapfs_container_get_volume_by_index(  | 
102  | 1.96k  |               container,  | 
103  | 1.96k  |               0,  | 
104  | 1.96k  |               &volume,  | 
105  | 1.96k  |               NULL );  | 
106  |  |  | 
107  | 1.96k  |     if( result != -1 )  | 
108  | 29  |     { | 
109  | 29  |       libfsapfs_volume_get_features_flags(  | 
110  | 29  |        volume,  | 
111  | 29  |        &compatible_features_flags,  | 
112  | 29  |        &incompatible_features_flags,  | 
113  | 29  |        &read_only_compatible_features_flags,  | 
114  | 29  |        NULL );  | 
115  |  |  | 
116  | 29  |       libfsapfs_volume_get_size(  | 
117  | 29  |        volume,  | 
118  | 29  |        &volume_size,  | 
119  | 29  |        NULL );  | 
120  |  |  | 
121  | 29  |       libfsapfs_volume_get_identifier(  | 
122  | 29  |        volume,  | 
123  | 29  |        volume_identifier,  | 
124  | 29  |        16,  | 
125  | 29  |        NULL );  | 
126  |  |  | 
127  | 29  |       libfsapfs_volume_get_utf8_name_size(  | 
128  | 29  |        volume,  | 
129  | 29  |        &string_size,  | 
130  | 29  |        NULL );  | 
131  |  |  | 
132  | 29  |       libfsapfs_volume_get_utf8_name(  | 
133  | 29  |        volume,  | 
134  | 29  |        string_value,  | 
135  | 29  |        64,  | 
136  | 29  |        NULL );  | 
137  |  |  | 
138  | 29  |       libfsapfs_volume_is_locked(  | 
139  | 29  |        volume,  | 
140  | 29  |        NULL );  | 
141  |  |  | 
142  | 29  |       libfsapfs_volume_free(  | 
143  | 29  |        &volume,  | 
144  | 29  |        NULL );  | 
145  | 29  |     }  | 
146  | 1.96k  |   }  | 
147  | 3.32k  | on_error_libfsapfs_container:  | 
148  | 3.32k  |   libfsapfs_container_free(  | 
149  | 3.32k  |    &container,  | 
150  | 3.32k  |    NULL );  | 
151  |  |  | 
152  | 3.32k  | on_error_libbfio:  | 
153  | 3.32k  |   libbfio_handle_free(  | 
154  | 3.32k  |    &file_io_handle,  | 
155  | 3.32k  |    NULL );  | 
156  |  |  | 
157  | 3.32k  |   return( 0 );  | 
158  | 3.32k  | }  | 
159  |  |  | 
160  |  | } /* extern "C" */  | 
161  |  |  |