0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include "gcc-common.h"
0023
0024 __visible int plugin_is_GPL_compatible;
0025
0026 tree sancov_fndecl;
0027
0028 static struct plugin_info sancov_plugin_info = {
0029 .version = PLUGIN_VERSION,
0030 .help = "sancov plugin\n",
0031 };
0032
0033 static unsigned int sancov_execute(void)
0034 {
0035 basic_block bb;
0036
0037
0038
0039
0040
0041
0042 FOR_EACH_BB_FN(bb, cfun) {
0043 const_gimple stmt;
0044 gcall *gcall;
0045 gimple_stmt_iterator gsi = gsi_after_labels(bb);
0046
0047 if (gsi_end_p(gsi))
0048 continue;
0049
0050 stmt = gsi_stmt(gsi);
0051 gcall = as_a_gcall(gimple_build_call(sancov_fndecl, 0));
0052 gimple_set_location(gcall, gimple_location(stmt));
0053 gsi_insert_before(&gsi, gcall, GSI_SAME_STMT);
0054 }
0055 return 0;
0056 }
0057
0058 #define PASS_NAME sancov
0059
0060 #define NO_GATE
0061 #define TODO_FLAGS_FINISH TODO_dump_func | TODO_verify_stmts | TODO_update_ssa_no_phi | TODO_verify_flow
0062
0063 #include "gcc-generate-gimple-pass.h"
0064
0065 static void sancov_start_unit(void __unused *gcc_data, void __unused *user_data)
0066 {
0067 tree leaf_attr, nothrow_attr;
0068 tree BT_FN_VOID = build_function_type_list(void_type_node, NULL_TREE);
0069
0070 sancov_fndecl = build_fn_decl("__sanitizer_cov_trace_pc", BT_FN_VOID);
0071
0072 DECL_ASSEMBLER_NAME(sancov_fndecl);
0073 TREE_PUBLIC(sancov_fndecl) = 1;
0074 DECL_EXTERNAL(sancov_fndecl) = 1;
0075 DECL_ARTIFICIAL(sancov_fndecl) = 1;
0076 DECL_PRESERVE_P(sancov_fndecl) = 1;
0077 DECL_UNINLINABLE(sancov_fndecl) = 1;
0078 TREE_USED(sancov_fndecl) = 1;
0079
0080 nothrow_attr = tree_cons(get_identifier("nothrow"), NULL, NULL);
0081 decl_attributes(&sancov_fndecl, nothrow_attr, 0);
0082 gcc_assert(TREE_NOTHROW(sancov_fndecl));
0083 leaf_attr = tree_cons(get_identifier("leaf"), NULL, NULL);
0084 decl_attributes(&sancov_fndecl, leaf_attr, 0);
0085 }
0086
0087 __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
0088 {
0089 int i;
0090 const char * const plugin_name = plugin_info->base_name;
0091 const int argc = plugin_info->argc;
0092 const struct plugin_argument * const argv = plugin_info->argv;
0093 bool enable = true;
0094
0095 static const struct ggc_root_tab gt_ggc_r_gt_sancov[] = {
0096 {
0097 .base = &sancov_fndecl,
0098 .nelt = 1,
0099 .stride = sizeof(sancov_fndecl),
0100 .cb = >_ggc_mx_tree_node,
0101 .pchw = >_pch_nx_tree_node
0102 },
0103 LAST_GGC_ROOT_TAB
0104 };
0105
0106
0107 PASS_INFO(sancov, "asan", 0, PASS_POS_INSERT_BEFORE);
0108
0109 if (!plugin_default_version_check(version, &gcc_version)) {
0110 error(G_("incompatible gcc/plugin versions"));
0111 return 1;
0112 }
0113
0114 for (i = 0; i < argc; ++i) {
0115 if (!strcmp(argv[i].key, "no-sancov")) {
0116 enable = false;
0117 continue;
0118 }
0119 error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
0120 }
0121
0122 register_callback(plugin_name, PLUGIN_INFO, NULL, &sancov_plugin_info);
0123
0124 if (!enable)
0125 return 0;
0126
0127 #if BUILDING_GCC_VERSION < 6000
0128 register_callback(plugin_name, PLUGIN_START_UNIT, &sancov_start_unit, NULL);
0129 register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL, (void *)>_ggc_r_gt_sancov);
0130 register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &sancov_pass_info);
0131 #endif
0132
0133 return 0;
0134 }