1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * Play with a file system image quickly to find UBSAN problems
3*6a54128fSAndroid Build Coastguard Worker *
4*6a54128fSAndroid Build Coastguard Worker * Run a file system through some of the libext2fs functions used by
5*6a54128fSAndroid Build Coastguard Worker * some fuzzer reports.
6*6a54128fSAndroid Build Coastguard Worker */
7*6a54128fSAndroid Build Coastguard Worker
8*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
9*6a54128fSAndroid Build Coastguard Worker #include <string.h>
10*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
11*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
12*6a54128fSAndroid Build Coastguard Worker #include <time.h>
13*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
14*6a54128fSAndroid Build Coastguard Worker #include <sys/time.h>
15*6a54128fSAndroid Build Coastguard Worker
16*6a54128fSAndroid Build Coastguard Worker #include <ext2fs/ext2_fs.h>
17*6a54128fSAndroid Build Coastguard Worker #include <ext2fs/ext2fs.h>
18*6a54128fSAndroid Build Coastguard Worker
main(int argc,char * argv[])19*6a54128fSAndroid Build Coastguard Worker int main (int argc, char *argv[])
20*6a54128fSAndroid Build Coastguard Worker {
21*6a54128fSAndroid Build Coastguard Worker errcode_t retval = 0;
22*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs;
23*6a54128fSAndroid Build Coastguard Worker int exit_status = 1;
24*6a54128fSAndroid Build Coastguard Worker
25*6a54128fSAndroid Build Coastguard Worker initialize_ext2_error_table();
26*6a54128fSAndroid Build Coastguard Worker
27*6a54128fSAndroid Build Coastguard Worker if (argc != 2) {
28*6a54128fSAndroid Build Coastguard Worker fprintf(stderr, "%s: Usage <device|filesystem>\n", argv[0]);
29*6a54128fSAndroid Build Coastguard Worker exit(1);
30*6a54128fSAndroid Build Coastguard Worker }
31*6a54128fSAndroid Build Coastguard Worker
32*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_open(argv[1], 0, 0, 0,
33*6a54128fSAndroid Build Coastguard Worker unix_io_manager, &fs);
34*6a54128fSAndroid Build Coastguard Worker if (retval) {
35*6a54128fSAndroid Build Coastguard Worker com_err(argv[0], retval, "while trying to open '%s'",
36*6a54128fSAndroid Build Coastguard Worker argv[1]);
37*6a54128fSAndroid Build Coastguard Worker exit(1);
38*6a54128fSAndroid Build Coastguard Worker }
39*6a54128fSAndroid Build Coastguard Worker
40*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_inode_bitmap(fs);
41*6a54128fSAndroid Build Coastguard Worker if (retval) {
42*6a54128fSAndroid Build Coastguard Worker com_err(argv[0], retval, "while trying to read inode bitmaps");
43*6a54128fSAndroid Build Coastguard Worker goto errout;
44*6a54128fSAndroid Build Coastguard Worker }
45*6a54128fSAndroid Build Coastguard Worker
46*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_block_bitmap(fs);
47*6a54128fSAndroid Build Coastguard Worker if (retval) {
48*6a54128fSAndroid Build Coastguard Worker com_err(argv[0], retval, "while trying to read inode bitmaps");
49*6a54128fSAndroid Build Coastguard Worker goto errout;
50*6a54128fSAndroid Build Coastguard Worker }
51*6a54128fSAndroid Build Coastguard Worker
52*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_check_directory(fs, EXT2_ROOT_INO);
53*6a54128fSAndroid Build Coastguard Worker if (retval) {
54*6a54128fSAndroid Build Coastguard Worker com_err(argv[0], retval, "while trying to read inode bitmaps");
55*6a54128fSAndroid Build Coastguard Worker goto errout;
56*6a54128fSAndroid Build Coastguard Worker }
57*6a54128fSAndroid Build Coastguard Worker exit_status = 0;
58*6a54128fSAndroid Build Coastguard Worker errout:
59*6a54128fSAndroid Build Coastguard Worker ext2fs_close(fs);
60*6a54128fSAndroid Build Coastguard Worker return exit_status;
61*6a54128fSAndroid Build Coastguard Worker }
62