1543c37cbSEmese Revfy /*
2543c37cbSEmese Revfy * Copyright 2011-2016 by Emese Revfy <re.emese@gmail.com>
3543c37cbSEmese Revfy * Licensed under the GPL v2, or (at your option) v3
4543c37cbSEmese Revfy *
5543c37cbSEmese Revfy * Homepage:
6543c37cbSEmese Revfy * https://github.com/ephox-gcc-plugins/sancov
7543c37cbSEmese Revfy *
8543c37cbSEmese Revfy * This plugin inserts a __sanitizer_cov_trace_pc() call at the start of basic blocks.
9543c37cbSEmese Revfy * It supports all gcc versions with plugin support (from gcc-4.5 on).
10543c37cbSEmese Revfy * It is based on the commit "Add fuzzing coverage support" by Dmitry Vyukov <dvyukov@google.com>.
11543c37cbSEmese Revfy *
12543c37cbSEmese Revfy * You can read about it more here:
13543c37cbSEmese Revfy * https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296
14496b24ecSAlexander A. Klimov * https://lwn.net/Articles/674854/
15543c37cbSEmese Revfy * https://github.com/google/syzkaller
16543c37cbSEmese Revfy * https://lwn.net/Articles/677764/
17543c37cbSEmese Revfy *
18543c37cbSEmese Revfy * Usage:
19543c37cbSEmese Revfy * make run
20543c37cbSEmese Revfy */
21543c37cbSEmese Revfy
22543c37cbSEmese Revfy #include "gcc-common.h"
23543c37cbSEmese Revfy
24da7389acSKees Cook __visible int plugin_is_GPL_compatible;
25543c37cbSEmese Revfy
26543c37cbSEmese Revfy tree sancov_fndecl;
27543c37cbSEmese Revfy
28543c37cbSEmese Revfy static struct plugin_info sancov_plugin_info = {
29*d37aa2efSMasahiro Yamada .version = PLUGIN_VERSION,
30543c37cbSEmese Revfy .help = "sancov plugin\n",
31543c37cbSEmese Revfy };
32543c37cbSEmese Revfy
sancov_execute(void)33543c37cbSEmese Revfy static unsigned int sancov_execute(void)
34543c37cbSEmese Revfy {
35543c37cbSEmese Revfy basic_block bb;
36543c37cbSEmese Revfy
37543c37cbSEmese Revfy /* Remove this line when this plugin and kcov will be in the kernel.
38543c37cbSEmese Revfy if (!strcmp(DECL_NAME_POINTER(current_function_decl), DECL_NAME_POINTER(sancov_fndecl)))
39543c37cbSEmese Revfy return 0;
40543c37cbSEmese Revfy */
41543c37cbSEmese Revfy
42543c37cbSEmese Revfy FOR_EACH_BB_FN(bb, cfun) {
43543c37cbSEmese Revfy const_gimple stmt;
44543c37cbSEmese Revfy gcall *gcall;
45543c37cbSEmese Revfy gimple_stmt_iterator gsi = gsi_after_labels(bb);
46543c37cbSEmese Revfy
47543c37cbSEmese Revfy if (gsi_end_p(gsi))
48543c37cbSEmese Revfy continue;
49543c37cbSEmese Revfy
50543c37cbSEmese Revfy stmt = gsi_stmt(gsi);
51543c37cbSEmese Revfy gcall = as_a_gcall(gimple_build_call(sancov_fndecl, 0));
52543c37cbSEmese Revfy gimple_set_location(gcall, gimple_location(stmt));
53543c37cbSEmese Revfy gsi_insert_before(&gsi, gcall, GSI_SAME_STMT);
54543c37cbSEmese Revfy }
55543c37cbSEmese Revfy return 0;
56543c37cbSEmese Revfy }
57543c37cbSEmese Revfy
58543c37cbSEmese Revfy #define PASS_NAME sancov
59543c37cbSEmese Revfy
60543c37cbSEmese Revfy #define NO_GATE
61543c37cbSEmese Revfy #define TODO_FLAGS_FINISH TODO_dump_func | TODO_verify_stmts | TODO_update_ssa_no_phi | TODO_verify_flow
62543c37cbSEmese Revfy
63543c37cbSEmese Revfy #include "gcc-generate-gimple-pass.h"
64543c37cbSEmese Revfy
sancov_start_unit(void __unused * gcc_data,void __unused * user_data)65543c37cbSEmese Revfy static void sancov_start_unit(void __unused *gcc_data, void __unused *user_data)
66543c37cbSEmese Revfy {
67543c37cbSEmese Revfy tree leaf_attr, nothrow_attr;
68543c37cbSEmese Revfy tree BT_FN_VOID = build_function_type_list(void_type_node, NULL_TREE);
69543c37cbSEmese Revfy
70543c37cbSEmese Revfy sancov_fndecl = build_fn_decl("__sanitizer_cov_trace_pc", BT_FN_VOID);
71543c37cbSEmese Revfy
72543c37cbSEmese Revfy DECL_ASSEMBLER_NAME(sancov_fndecl);
73543c37cbSEmese Revfy TREE_PUBLIC(sancov_fndecl) = 1;
74543c37cbSEmese Revfy DECL_EXTERNAL(sancov_fndecl) = 1;
75543c37cbSEmese Revfy DECL_ARTIFICIAL(sancov_fndecl) = 1;
76543c37cbSEmese Revfy DECL_PRESERVE_P(sancov_fndecl) = 1;
77543c37cbSEmese Revfy DECL_UNINLINABLE(sancov_fndecl) = 1;
78543c37cbSEmese Revfy TREE_USED(sancov_fndecl) = 1;
79543c37cbSEmese Revfy
80543c37cbSEmese Revfy nothrow_attr = tree_cons(get_identifier("nothrow"), NULL, NULL);
81543c37cbSEmese Revfy decl_attributes(&sancov_fndecl, nothrow_attr, 0);
82543c37cbSEmese Revfy gcc_assert(TREE_NOTHROW(sancov_fndecl));
83543c37cbSEmese Revfy leaf_attr = tree_cons(get_identifier("leaf"), NULL, NULL);
84543c37cbSEmese Revfy decl_attributes(&sancov_fndecl, leaf_attr, 0);
85543c37cbSEmese Revfy }
86543c37cbSEmese Revfy
plugin_init(struct plugin_name_args * plugin_info,struct plugin_gcc_version * version)87da7389acSKees Cook __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
88543c37cbSEmese Revfy {
89543c37cbSEmese Revfy int i;
90543c37cbSEmese Revfy const char * const plugin_name = plugin_info->base_name;
91543c37cbSEmese Revfy const int argc = plugin_info->argc;
92543c37cbSEmese Revfy const struct plugin_argument * const argv = plugin_info->argv;
93543c37cbSEmese Revfy bool enable = true;
94543c37cbSEmese Revfy
95543c37cbSEmese Revfy static const struct ggc_root_tab gt_ggc_r_gt_sancov[] = {
96543c37cbSEmese Revfy {
97543c37cbSEmese Revfy .base = &sancov_fndecl,
98543c37cbSEmese Revfy .nelt = 1,
99543c37cbSEmese Revfy .stride = sizeof(sancov_fndecl),
100543c37cbSEmese Revfy .cb = >_ggc_mx_tree_node,
101543c37cbSEmese Revfy .pchw = >_pch_nx_tree_node
102543c37cbSEmese Revfy },
103543c37cbSEmese Revfy LAST_GGC_ROOT_TAB
104543c37cbSEmese Revfy };
105543c37cbSEmese Revfy
106543c37cbSEmese Revfy /* BBs can be split afterwards?? */
1075a45a4c5SKees Cook PASS_INFO(sancov, "asan", 0, PASS_POS_INSERT_BEFORE);
108543c37cbSEmese Revfy
109543c37cbSEmese Revfy if (!plugin_default_version_check(version, &gcc_version)) {
110543c37cbSEmese Revfy error(G_("incompatible gcc/plugin versions"));
111543c37cbSEmese Revfy return 1;
112543c37cbSEmese Revfy }
113543c37cbSEmese Revfy
114543c37cbSEmese Revfy for (i = 0; i < argc; ++i) {
115543c37cbSEmese Revfy if (!strcmp(argv[i].key, "no-sancov")) {
116543c37cbSEmese Revfy enable = false;
117543c37cbSEmese Revfy continue;
118543c37cbSEmese Revfy }
1199165dabbSMasanari Iida error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
120543c37cbSEmese Revfy }
121543c37cbSEmese Revfy
122543c37cbSEmese Revfy register_callback(plugin_name, PLUGIN_INFO, NULL, &sancov_plugin_info);
123543c37cbSEmese Revfy
124543c37cbSEmese Revfy if (!enable)
125543c37cbSEmese Revfy return 0;
126543c37cbSEmese Revfy
127543c37cbSEmese Revfy #if BUILDING_GCC_VERSION < 6000
128543c37cbSEmese Revfy register_callback(plugin_name, PLUGIN_START_UNIT, &sancov_start_unit, NULL);
129543c37cbSEmese Revfy register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL, (void *)>_ggc_r_gt_sancov);
130dcc23527SArnd Bergmann register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &sancov_pass_info);
131543c37cbSEmese Revfy #endif
132543c37cbSEmese Revfy
133543c37cbSEmese Revfy return 0;
134543c37cbSEmese Revfy }
135