sancov_plugin.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2011-2016 by Emese Revfy <[email protected]>
  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 <[email protected]>.
  11. *
  12. * You can read about it more here:
  13. * https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296
  14. * https://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 = PLUGIN_VERSION,
  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. leaf_attr = tree_cons(get_identifier("leaf"), NULL, NULL);
  68. decl_attributes(&sancov_fndecl, leaf_attr, 0);
  69. }
  70. __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
  71. {
  72. int i;
  73. const char * const plugin_name = plugin_info->base_name;
  74. const int argc = plugin_info->argc;
  75. const struct plugin_argument * const argv = plugin_info->argv;
  76. bool enable = true;
  77. static const struct ggc_root_tab gt_ggc_r_gt_sancov[] = {
  78. {
  79. .base = &sancov_fndecl,
  80. .nelt = 1,
  81. .stride = sizeof(sancov_fndecl),
  82. .cb = &gt_ggc_mx_tree_node,
  83. .pchw = &gt_pch_nx_tree_node
  84. },
  85. LAST_GGC_ROOT_TAB
  86. };
  87. /* BBs can be split afterwards?? */
  88. PASS_INFO(sancov, "asan", 0, PASS_POS_INSERT_BEFORE);
  89. if (!plugin_default_version_check(version, &gcc_version)) {
  90. error(G_("incompatible gcc/plugin versions"));
  91. return 1;
  92. }
  93. for (i = 0; i < argc; ++i) {
  94. if (!strcmp(argv[i].key, "no-sancov")) {
  95. enable = false;
  96. continue;
  97. }
  98. error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
  99. }
  100. register_callback(plugin_name, PLUGIN_INFO, NULL, &sancov_plugin_info);
  101. if (!enable)
  102. return 0;
  103. #if BUILDING_GCC_VERSION < 6000
  104. register_callback(plugin_name, PLUGIN_START_UNIT, &sancov_start_unit, NULL);
  105. register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL, (void *)&gt_ggc_r_gt_sancov);
  106. register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &sancov_pass_info);
  107. #endif
  108. return 0;
  109. }