1 /*
2 * Copyright (C) 2017 Oracle.
3 *
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.
8 *
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.
13 *
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
16 */
17
18 /*
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.
22 *
23 */
24
25 #include "smatch.h"
26 #include "smatch_slist.h"
27
28 static int my_id;
29
30 FILE *sysc_fd;
31
gen_custom_struct(int nr,struct symbol * arg)32 static int gen_custom_struct(int nr, struct symbol *arg)
33 {
34 return 0;
35 }
36
print_arg(int nr,struct symbol * arg)37 static void print_arg(int nr, struct symbol *arg)
38 {
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));
41 }
42
match_return(struct expression * ret_value)43 static void match_return(struct expression *ret_value)
44 {
45 struct symbol *arg;
46 int num_args;
47 char *name;
48 int i;
49 char buf[256];
50 int has_custom_struct[6];
51
52 if (!get_function() || !cur_func_sym)
53 return;
54 if (strncmp(get_function(), "SYSC_", 5) != 0)
55 return;
56
57 num_args = ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
58 name = get_function() + 5;
59
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;
65 }
66
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);
75
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);
79
80 i = 0;
81 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
82 if (has_custom_struct[i])
83 ;
84 else
85 print_arg(i++, arg);
86 } END_FOR_EACH_PTR(arg);
87
88 fprintf(sysc_fd, "};\n");
89 }
90
check_trinity_generator(int id)91 void check_trinity_generator(int id)
92 {
93 my_id = id;
94
95 if (option_project != PROJ_KERNEL)
96 return;
97 add_hook(&match_return, RETURN_HOOK);
98 }
99