/src/libfsxfs/ossfuzz/volume_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * OSS-Fuzz target for libfsxfs volume type |
3 | | * |
4 | | * Copyright (C) 2020-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_libfsxfs.h" |
31 | | |
32 | | #if !defined( LIBFSXFS_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a volume using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBFSXFS_EXTERN \ |
38 | | int libfsxfs_volume_open_file_io_handle( |
39 | | libfsxfs_volume_t *volume, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libfsxfs_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBFSXFS_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 677 | { |
50 | 677 | uint8_t string_value[ 64 ]; |
51 | | |
52 | 677 | libbfio_handle_t *file_io_handle = NULL; |
53 | 677 | libfsxfs_volume_t *volume = NULL; |
54 | 677 | size_t string_size = 0; |
55 | 677 | uint8_t format_version = 0; |
56 | 677 | int result = 0; |
57 | | |
58 | 677 | if( libbfio_memory_range_initialize( |
59 | 677 | &file_io_handle, |
60 | 677 | NULL ) != 1 ) |
61 | 0 | { |
62 | 0 | return( 0 ); |
63 | 0 | } |
64 | 677 | if( libbfio_memory_range_set( |
65 | 677 | file_io_handle, |
66 | 677 | (uint8_t *) data, |
67 | 677 | size, |
68 | 677 | NULL ) != 1 ) |
69 | 0 | { |
70 | 0 | goto on_error_libbfio; |
71 | 0 | } |
72 | 677 | if( libfsxfs_volume_initialize( |
73 | 677 | &volume, |
74 | 677 | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | goto on_error_libbfio; |
77 | 0 | } |
78 | 677 | result = libfsxfs_volume_open_file_io_handle( |
79 | 677 | volume, |
80 | 677 | file_io_handle, |
81 | 677 | LIBFSXFS_OPEN_READ, |
82 | 677 | NULL ); |
83 | | |
84 | 677 | if( result != -1 ) |
85 | 122 | { |
86 | 122 | libfsxfs_volume_get_format_version( |
87 | 122 | volume, |
88 | 122 | &format_version, |
89 | 122 | NULL ); |
90 | | |
91 | 122 | libfsxfs_volume_get_utf8_label_size( |
92 | 122 | volume, |
93 | 122 | &string_size, |
94 | 122 | NULL ); |
95 | | |
96 | 122 | libfsxfs_volume_get_utf8_label( |
97 | 122 | volume, |
98 | 122 | string_value, |
99 | 122 | 64, |
100 | 122 | NULL ); |
101 | | |
102 | 122 | libfsxfs_volume_close( |
103 | 122 | volume, |
104 | 122 | NULL ); |
105 | 122 | } |
106 | 677 | libfsxfs_volume_free( |
107 | 677 | &volume, |
108 | 677 | NULL ); |
109 | | |
110 | 677 | on_error_libbfio: |
111 | 677 | libbfio_handle_free( |
112 | 677 | &file_io_handle, |
113 | 677 | NULL ); |
114 | | |
115 | 677 | return( 0 ); |
116 | 677 | } |
117 | | |
118 | | } /* extern "C" */ |
119 | | |