18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright 2011-2016 by Emese Revfy <re.emese@gmail.com> 38c2ecf20Sopenharmony_ci * Licensed under the GPL v2, or (at your option) v3 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Homepage: 68c2ecf20Sopenharmony_ci * https://github.com/ephox-gcc-plugins/sancov 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * This plugin inserts a __sanitizer_cov_trace_pc() call at the start of basic blocks. 98c2ecf20Sopenharmony_ci * It supports all gcc versions with plugin support (from gcc-4.5 on). 108c2ecf20Sopenharmony_ci * It is based on the commit "Add fuzzing coverage support" by Dmitry Vyukov <dvyukov@google.com>. 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * You can read about it more here: 138c2ecf20Sopenharmony_ci * https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296 148c2ecf20Sopenharmony_ci * https://lwn.net/Articles/674854/ 158c2ecf20Sopenharmony_ci * https://github.com/google/syzkaller 168c2ecf20Sopenharmony_ci * https://lwn.net/Articles/677764/ 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * Usage: 198c2ecf20Sopenharmony_ci * make run 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include "gcc-common.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci__visible int plugin_is_GPL_compatible; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_citree sancov_fndecl; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistatic struct plugin_info sancov_plugin_info = { 298c2ecf20Sopenharmony_ci .version = "20160402", 308c2ecf20Sopenharmony_ci .help = "sancov plugin\n", 318c2ecf20Sopenharmony_ci}; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic unsigned int sancov_execute(void) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci basic_block bb; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci /* Remove this line when this plugin and kcov will be in the kernel. 388c2ecf20Sopenharmony_ci if (!strcmp(DECL_NAME_POINTER(current_function_decl), DECL_NAME_POINTER(sancov_fndecl))) 398c2ecf20Sopenharmony_ci return 0; 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci FOR_EACH_BB_FN(bb, cfun) { 438c2ecf20Sopenharmony_ci const_gimple stmt; 448c2ecf20Sopenharmony_ci gcall *gcall; 458c2ecf20Sopenharmony_ci gimple_stmt_iterator gsi = gsi_after_labels(bb); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci if (gsi_end_p(gsi)) 488c2ecf20Sopenharmony_ci continue; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci stmt = gsi_stmt(gsi); 518c2ecf20Sopenharmony_ci gcall = as_a_gcall(gimple_build_call(sancov_fndecl, 0)); 528c2ecf20Sopenharmony_ci gimple_set_location(gcall, gimple_location(stmt)); 538c2ecf20Sopenharmony_ci gsi_insert_before(&gsi, gcall, GSI_SAME_STMT); 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci return 0; 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define PASS_NAME sancov 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#define NO_GATE 618c2ecf20Sopenharmony_ci#define TODO_FLAGS_FINISH TODO_dump_func | TODO_verify_stmts | TODO_update_ssa_no_phi | TODO_verify_flow 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci#include "gcc-generate-gimple-pass.h" 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic void sancov_start_unit(void __unused *gcc_data, void __unused *user_data) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci tree leaf_attr, nothrow_attr; 688c2ecf20Sopenharmony_ci tree BT_FN_VOID = build_function_type_list(void_type_node, NULL_TREE); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci sancov_fndecl = build_fn_decl("__sanitizer_cov_trace_pc", BT_FN_VOID); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci DECL_ASSEMBLER_NAME(sancov_fndecl); 738c2ecf20Sopenharmony_ci TREE_PUBLIC(sancov_fndecl) = 1; 748c2ecf20Sopenharmony_ci DECL_EXTERNAL(sancov_fndecl) = 1; 758c2ecf20Sopenharmony_ci DECL_ARTIFICIAL(sancov_fndecl) = 1; 768c2ecf20Sopenharmony_ci DECL_PRESERVE_P(sancov_fndecl) = 1; 778c2ecf20Sopenharmony_ci DECL_UNINLINABLE(sancov_fndecl) = 1; 788c2ecf20Sopenharmony_ci TREE_USED(sancov_fndecl) = 1; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci nothrow_attr = tree_cons(get_identifier("nothrow"), NULL, NULL); 818c2ecf20Sopenharmony_ci decl_attributes(&sancov_fndecl, nothrow_attr, 0); 828c2ecf20Sopenharmony_ci gcc_assert(TREE_NOTHROW(sancov_fndecl)); 838c2ecf20Sopenharmony_ci#if BUILDING_GCC_VERSION > 4005 848c2ecf20Sopenharmony_ci leaf_attr = tree_cons(get_identifier("leaf"), NULL, NULL); 858c2ecf20Sopenharmony_ci decl_attributes(&sancov_fndecl, leaf_attr, 0); 868c2ecf20Sopenharmony_ci#endif 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci__visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci int i; 928c2ecf20Sopenharmony_ci const char * const plugin_name = plugin_info->base_name; 938c2ecf20Sopenharmony_ci const int argc = plugin_info->argc; 948c2ecf20Sopenharmony_ci const struct plugin_argument * const argv = plugin_info->argv; 958c2ecf20Sopenharmony_ci bool enable = true; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci static const struct ggc_root_tab gt_ggc_r_gt_sancov[] = { 988c2ecf20Sopenharmony_ci { 998c2ecf20Sopenharmony_ci .base = &sancov_fndecl, 1008c2ecf20Sopenharmony_ci .nelt = 1, 1018c2ecf20Sopenharmony_ci .stride = sizeof(sancov_fndecl), 1028c2ecf20Sopenharmony_ci .cb = >_ggc_mx_tree_node, 1038c2ecf20Sopenharmony_ci .pchw = >_pch_nx_tree_node 1048c2ecf20Sopenharmony_ci }, 1058c2ecf20Sopenharmony_ci LAST_GGC_ROOT_TAB 1068c2ecf20Sopenharmony_ci }; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci /* BBs can be split afterwards?? */ 1098c2ecf20Sopenharmony_ci#if BUILDING_GCC_VERSION >= 4009 1108c2ecf20Sopenharmony_ci PASS_INFO(sancov, "asan", 0, PASS_POS_INSERT_BEFORE); 1118c2ecf20Sopenharmony_ci#else 1128c2ecf20Sopenharmony_ci PASS_INFO(sancov, "nrv", 1, PASS_POS_INSERT_BEFORE); 1138c2ecf20Sopenharmony_ci#endif 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci if (!plugin_default_version_check(version, &gcc_version)) { 1168c2ecf20Sopenharmony_ci error(G_("incompatible gcc/plugin versions")); 1178c2ecf20Sopenharmony_ci return 1; 1188c2ecf20Sopenharmony_ci } 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci for (i = 0; i < argc; ++i) { 1218c2ecf20Sopenharmony_ci if (!strcmp(argv[i].key, "no-sancov")) { 1228c2ecf20Sopenharmony_ci enable = false; 1238c2ecf20Sopenharmony_ci continue; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key); 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci register_callback(plugin_name, PLUGIN_INFO, NULL, &sancov_plugin_info); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci if (!enable) 1318c2ecf20Sopenharmony_ci return 0; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci#if BUILDING_GCC_VERSION < 6000 1348c2ecf20Sopenharmony_ci register_callback(plugin_name, PLUGIN_START_UNIT, &sancov_start_unit, NULL); 1358c2ecf20Sopenharmony_ci register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL, (void *)>_ggc_r_gt_sancov); 1368c2ecf20Sopenharmony_ci register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &sancov_pass_info); 1378c2ecf20Sopenharmony_ci#endif 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci return 0; 1408c2ecf20Sopenharmony_ci} 141