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