sancov_plugin.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2011-2016 by Emese Revfy <re.emese@gmail.com>
  3. * Licensed under the GPL v2, or (at your option) v3
  4. *
  5. * Homepage:
  6. * https://github.com/ephox-gcc-plugins/sancov
  7. *
  8. * This plugin inserts a __sanitizer_cov_trace_pc() call at the start of basic blocks.
  9. * It supports all gcc versions with plugin support (from gcc-4.5 on).
  10. * It is based on the commit "Add fuzzing coverage support" by Dmitry Vyukov <dvyukov@google.com>.
  11. *
  12. * You can read about it more here:
  13. * https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296
  14. * http://lwn.net/Articles/674854/
  15. * https://github.com/google/syzkaller
  16. * https://lwn.net/Articles/677764/
  17. *
  18. * Usage:
  19. * make run
  20. */
  21. #include "gcc-common.h"
  22. __visible int plugin_is_GPL_compatible;
  23. tree sancov_fndecl;
  24. static struct plugin_info sancov_plugin_info = {
  25. .version = "20160402",
  26. .help = "sancov plugin\n",
  27. };
  28. static unsigned int sancov_execute(void)
  29. {
  30. basic_block bb;
  31. /* Remove this line when this plugin and kcov will be in the kernel.
  32. if (!strcmp(DECL_NAME_POINTER(current_function_decl), DECL_NAME_POINTER(sancov_fndecl)))
  33. return 0;
  34. */
  35. FOR_EACH_BB_FN(bb, cfun) {
  36. const_gimple stmt;
  37. gcall *gcall;
  38. gimple_stmt_iterator gsi = gsi_after_labels(bb);
  39. if (gsi_end_p(gsi))
  40. continue;
  41. stmt = gsi_stmt(gsi);
  42. gcall = as_a_gcall(gimple_build_call(sancov_fndecl, 0));
  43. gimple_set_location(gcall, gimple_location(stmt));
  44. gsi_insert_before(&gsi, gcall, GSI_SAME_STMT);
  45. }
  46. return 0;
  47. }
  48. #define PASS_NAME sancov
  49. #define NO_GATE
  50. #define TODO_FLAGS_FINISH TODO_dump_func | TODO_verify_stmts | TODO_update_ssa_no_phi | TODO_verify_flow
  51. #include "gcc-generate-gimple-pass.h"
  52. static void sancov_start_unit(void __unused *gcc_data, void __unused *user_data)
  53. {
  54. tree leaf_attr, nothrow_attr;
  55. tree BT_FN_VOID = build_function_type_list(void_type_node, NULL_TREE);
  56. sancov_fndecl = build_fn_decl("__sanitizer_cov_trace_pc", BT_FN_VOID);
  57. DECL_ASSEMBLER_NAME(sancov_fndecl);
  58. TREE_PUBLIC(sancov_fndecl) = 1;
  59. DECL_EXTERNAL(sancov_fndecl) = 1;
  60. DECL_ARTIFICIAL(sancov_fndecl) = 1;
  61. DECL_PRESERVE_P(sancov_fndecl) = 1;
  62. DECL_UNINLINABLE(sancov_fndecl) = 1;
  63. TREE_USED(sancov_fndecl) = 1;
  64. nothrow_attr = tree_cons(get_identifier("nothrow"), NULL, NULL);
  65. decl_attributes(&sancov_fndecl, nothrow_attr, 0);
  66. gcc_assert(TREE_NOTHROW(sancov_fndecl));
  67. #if BUILDING_GCC_VERSION > 4005
  68. leaf_attr = tree_cons(get_identifier("leaf"), NULL, NULL);
  69. decl_attributes(&sancov_fndecl, leaf_attr, 0);
  70. #endif
  71. }
  72. __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
  73. {
  74. int i;
  75. struct register_pass_info sancov_plugin_pass_info;
  76. const char * const plugin_name = plugin_info->base_name;
  77. const int argc = plugin_info->argc;
  78. const struct plugin_argument * const argv = plugin_info->argv;
  79. bool enable = true;
  80. static const struct ggc_root_tab gt_ggc_r_gt_sancov[] = {
  81. {
  82. .base = &sancov_fndecl,
  83. .nelt = 1,
  84. .stride = sizeof(sancov_fndecl),
  85. .cb = &gt_ggc_mx_tree_node,
  86. .pchw = &gt_pch_nx_tree_node
  87. },
  88. LAST_GGC_ROOT_TAB
  89. };
  90. /* BBs can be split afterwards?? */
  91. sancov_plugin_pass_info.pass = make_sancov_pass();
  92. #if BUILDING_GCC_VERSION >= 4009
  93. sancov_plugin_pass_info.reference_pass_name = "asan";
  94. #else
  95. sancov_plugin_pass_info.reference_pass_name = "nrv";
  96. #endif
  97. sancov_plugin_pass_info.ref_pass_instance_number = 0;
  98. sancov_plugin_pass_info.pos_op = PASS_POS_INSERT_BEFORE;
  99. if (!plugin_default_version_check(version, &gcc_version)) {
  100. error(G_("incompatible gcc/plugin versions"));
  101. return 1;
  102. }
  103. for (i = 0; i < argc; ++i) {
  104. if (!strcmp(argv[i].key, "no-sancov")) {
  105. enable = false;
  106. continue;
  107. }
  108. error(G_("unkown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
  109. }
  110. register_callback(plugin_name, PLUGIN_INFO, NULL, &sancov_plugin_info);
  111. if (!enable)
  112. return 0;
  113. #if BUILDING_GCC_VERSION < 6000
  114. register_callback(plugin_name, PLUGIN_START_UNIT, &sancov_start_unit, NULL);
  115. register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL, (void *)&gt_ggc_r_gt_sancov);
  116. register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &sancov_plugin_pass_info);
  117. #endif
  118. return 0;
  119. }