unreachable: delete an extra space character
[smatch.git] / check_trinity_generator.c
blob0a52aa5bb7d72b14b62db74327a617e21e256d2d
1 /*
2 * Copyright (C) 2017 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * The idea is to generate syscall templates for the Trinity fuzzer. There
20 * isn't currently quite enough information to do it right but I want to start
21 * and see how far I can get.
25 #include "smatch.h"
26 #include "smatch_slist.h"
28 static int my_id;
30 FILE *sysc_fd;
32 static int gen_custom_struct(int nr, struct symbol *arg)
34 return 0;
37 static void print_arg(int nr, struct symbol *arg)
39 fprintf(sysc_fd, "\t.arg%dname = \"%s\",\n", nr + 1, arg->ident->name);
40 fprintf(sysc_fd, "\t.arg%dtype = %s,\n", nr + 1, get_syscall_arg_type(arg));
43 static void match_return(struct expression *ret_value)
45 struct symbol *arg;
46 int num_args;
47 char *name;
48 int i;
49 char buf[256];
50 int has_custom_struct[6];
52 if (!get_function() || !cur_func_sym)
53 return;
54 if (strncmp(get_function(), "SYSC_", 5) != 0)
55 return;
57 num_args = ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
58 name = get_function() + 5;
60 snprintf(buf, sizeof(buf), "smatch_trinity_%s", name);
61 sysc_fd = fopen(buf, "w");
62 if (!sm_outfd) {
63 printf("Error: Cannot open %s\n", buf);
64 return;
67 i = 0;
68 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
69 if (gen_custom_struct(i, arg))
70 has_custom_struct[i] = true;
71 else
72 has_custom_struct[i] = false;
73 i++;
74 } END_FOR_EACH_PTR(arg);
76 fprintf(sysc_fd, "struct syscallentry sm_%s = {\n", name);
77 fprintf(sysc_fd, "\t.name = \"%s\",\n", name);
78 fprintf(sysc_fd, "\t.num_args = %d,\n", num_args);
80 i = 0;
81 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
82 if (has_custom_struct[i])
84 else
85 print_arg(i++, arg);
86 } END_FOR_EACH_PTR(arg);
88 fprintf(sysc_fd, "};\n");
91 void check_trinity_generator(int id)
93 my_id = id;
95 if (option_project != PROJ_KERNEL)
96 return;
97 add_hook(&match_return, RETURN_HOOK);