diff options
| -rw-r--r-- | Makefile | 10 | ||||
| -rw-r--r-- | arch/Kconfig | 9 | ||||
| -rw-r--r-- | arch/x86/purgatory/Makefile | 2 | ||||
| -rw-r--r-- | lib/Kconfig.debug | 2 | ||||
| -rw-r--r-- | scripts/Makefile.gcc-plugins | 21 | ||||
| -rw-r--r-- | scripts/gcc-plugins/Makefile | 6 | ||||
| -rw-r--r-- | scripts/gcc-plugins/sancov_plugin.c | 144 |
7 files changed, 184 insertions, 10 deletions
| @@ -369,7 +369,7 @@ LDFLAGS_MODULE = | |||
| 369 | CFLAGS_KERNEL = | 369 | CFLAGS_KERNEL = |
| 370 | AFLAGS_KERNEL = | 370 | AFLAGS_KERNEL = |
| 371 | CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized | 371 | CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized |
| 372 | CFLAGS_KCOV = -fsanitize-coverage=trace-pc | 372 | CFLAGS_KCOV := $(call cc-option,-fsanitize-coverage=trace-pc,) |
| 373 | 373 | ||
| 374 | 374 | ||
| 375 | # Use USERINCLUDE when you must reference the UAPI directories only. | 375 | # Use USERINCLUDE when you must reference the UAPI directories only. |
| @@ -691,14 +691,6 @@ endif | |||
| 691 | endif | 691 | endif |
| 692 | KBUILD_CFLAGS += $(stackp-flag) | 692 | KBUILD_CFLAGS += $(stackp-flag) |
| 693 | 693 | ||
| 694 | ifdef CONFIG_KCOV | ||
| 695 | ifeq ($(call cc-option, $(CFLAGS_KCOV)),) | ||
| 696 | $(warning Cannot use CONFIG_KCOV: \ | ||
| 697 | -fsanitize-coverage=trace-pc is not supported by compiler) | ||
| 698 | CFLAGS_KCOV = | ||
| 699 | endif | ||
| 700 | endif | ||
| 701 | |||
| 702 | ifeq ($(cc-name),clang) | 694 | ifeq ($(cc-name),clang) |
| 703 | KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,) | 695 | KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,) |
| 704 | KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,) | 696 | KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,) |
diff --git a/arch/Kconfig b/arch/Kconfig index 04ca45262ad1..05f1e95b796d 100644 --- a/arch/Kconfig +++ b/arch/Kconfig | |||
| @@ -384,6 +384,15 @@ config GCC_PLUGIN_CYC_COMPLEXITY | |||
| 384 | N = the number of nodes | 384 | N = the number of nodes |
| 385 | P = the number of connected components (exit nodes). | 385 | P = the number of connected components (exit nodes). |
| 386 | 386 | ||
| 387 | config GCC_PLUGIN_SANCOV | ||
| 388 | bool | ||
| 389 | depends on GCC_PLUGINS | ||
| 390 | help | ||
| 391 | This plugin inserts a __sanitizer_cov_trace_pc() call at the start of | ||
| 392 | basic blocks. It supports all gcc versions with plugin support (from | ||
| 393 | gcc-4.5 on). It is based on the commit "Add fuzzing coverage support" | ||
| 394 | by Dmitry Vyukov <dvyukov@google.com>. | ||
| 395 | |||
| 387 | config HAVE_CC_STACKPROTECTOR | 396 | config HAVE_CC_STACKPROTECTOR |
| 388 | bool | 397 | bool |
| 389 | help | 398 | help |
diff --git a/arch/x86/purgatory/Makefile b/arch/x86/purgatory/Makefile index 12734a96df47..ac58c1616408 100644 --- a/arch/x86/purgatory/Makefile +++ b/arch/x86/purgatory/Makefile | |||
| @@ -8,6 +8,8 @@ PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y)) | |||
| 8 | LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib | 8 | LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib |
| 9 | targets += purgatory.ro | 9 | targets += purgatory.ro |
| 10 | 10 | ||
| 11 | KCOV_INSTRUMENT := n | ||
| 12 | |||
| 11 | # Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That | 13 | # Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That |
| 12 | # in turn leaves some undefined symbols like __fentry__ in purgatory and not | 14 | # in turn leaves some undefined symbols like __fentry__ in purgatory and not |
| 13 | # sure how to relocate those. Like kexec-tools, use custom flags. | 15 | # sure how to relocate those. Like kexec-tools, use custom flags. |
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 77d7d034bac3..b7827dca3fec 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug | |||
| @@ -708,6 +708,8 @@ config KCOV | |||
| 708 | bool "Code coverage for fuzzing" | 708 | bool "Code coverage for fuzzing" |
| 709 | depends on ARCH_HAS_KCOV | 709 | depends on ARCH_HAS_KCOV |
| 710 | select DEBUG_FS | 710 | select DEBUG_FS |
| 711 | select GCC_PLUGINS | ||
| 712 | select GCC_PLUGIN_SANCOV | ||
| 711 | help | 713 | help |
| 712 | KCOV exposes kernel code coverage information in a form suitable | 714 | KCOV exposes kernel code coverage information in a form suitable |
| 713 | for coverage-guided fuzzing (randomized testing). | 715 | for coverage-guided fuzzing (randomized testing). |
diff --git a/scripts/Makefile.gcc-plugins b/scripts/Makefile.gcc-plugins index b4a189c95a8a..5e22b60589c1 100644 --- a/scripts/Makefile.gcc-plugins +++ b/scripts/Makefile.gcc-plugins | |||
| @@ -2,10 +2,26 @@ ifdef CONFIG_GCC_PLUGINS | |||
| 2 | __PLUGINCC := $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC)) | 2 | __PLUGINCC := $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC)) |
| 3 | PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") | 3 | PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") |
| 4 | 4 | ||
| 5 | SANCOV_PLUGIN := -fplugin=$(objtree)/scripts/gcc-plugins/sancov_plugin.so | ||
| 6 | |||
| 5 | gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so | 7 | gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so |
| 8 | |||
| 9 | ifdef CONFIG_GCC_PLUGIN_SANCOV | ||
| 10 | ifeq ($(CFLAGS_KCOV),) | ||
| 11 | # It is needed because of the gcc-plugin.sh and gcc version checks. | ||
| 12 | gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so | ||
| 13 | |||
| 14 | ifneq ($(PLUGINCC),) | ||
| 15 | CFLAGS_KCOV := $(SANCOV_PLUGIN) | ||
| 16 | else | ||
| 17 | $(warning warning: cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler) | ||
| 18 | endif | ||
| 19 | endif | ||
| 20 | endif | ||
| 21 | |||
| 6 | GCC_PLUGINS_CFLAGS := $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) | 22 | GCC_PLUGINS_CFLAGS := $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) |
| 7 | 23 | ||
| 8 | export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN | 24 | export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN SANCOV_PLUGIN |
| 9 | 25 | ||
| 10 | ifeq ($(PLUGINCC),) | 26 | ifeq ($(PLUGINCC),) |
| 11 | ifneq ($(GCC_PLUGINS_CFLAGS),) | 27 | ifneq ($(GCC_PLUGINS_CFLAGS),) |
| @@ -16,6 +32,9 @@ ifdef CONFIG_GCC_PLUGINS | |||
| 16 | $(warning warning: your gcc version does not support plugins, you should upgrade it to gcc 4.5 at least) | 32 | $(warning warning: your gcc version does not support plugins, you should upgrade it to gcc 4.5 at least) |
| 17 | endif | 33 | endif |
| 18 | endif | 34 | endif |
| 35 | else | ||
| 36 | # SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication. | ||
| 37 | GCC_PLUGINS_CFLAGS := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGINS_CFLAGS)) | ||
| 19 | endif | 38 | endif |
| 20 | 39 | ||
| 21 | KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS) | 40 | KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS) |
diff --git a/scripts/gcc-plugins/Makefile b/scripts/gcc-plugins/Makefile index c60ba4bef57c..88c8ec47232b 100644 --- a/scripts/gcc-plugins/Makefile +++ b/scripts/gcc-plugins/Makefile | |||
| @@ -14,8 +14,14 @@ endif | |||
| 14 | 14 | ||
| 15 | export GCCPLUGINS_DIR HOSTLIBS | 15 | export GCCPLUGINS_DIR HOSTLIBS |
| 16 | 16 | ||
| 17 | ifneq ($(CFLAGS_KCOV), $(SANCOV_PLUGIN)) | ||
| 18 | GCC_PLUGIN := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGIN)) | ||
| 19 | endif | ||
| 20 | |||
| 17 | $(HOSTLIBS)-y := $(GCC_PLUGIN) | 21 | $(HOSTLIBS)-y := $(GCC_PLUGIN) |
| 18 | always := $($(HOSTLIBS)-y) | 22 | always := $($(HOSTLIBS)-y) |
| 19 | 23 | ||
| 20 | cyc_complexity_plugin-objs := cyc_complexity_plugin.o | 24 | cyc_complexity_plugin-objs := cyc_complexity_plugin.o |
| 25 | sancov_plugin-objs := sancov_plugin.o | ||
| 26 | |||
| 21 | clean-files += *.so | 27 | clean-files += *.so |
diff --git a/scripts/gcc-plugins/sancov_plugin.c b/scripts/gcc-plugins/sancov_plugin.c new file mode 100644 index 000000000000..aedd6113cb73 --- /dev/null +++ b/scripts/gcc-plugins/sancov_plugin.c | |||
| @@ -0,0 +1,144 @@ | |||
| 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 | |||
| 22 | #include "gcc-common.h" | ||
| 23 | |||
| 24 | int plugin_is_GPL_compatible; | ||
| 25 | |||
| 26 | tree sancov_fndecl; | ||
| 27 | |||
| 28 | static struct plugin_info sancov_plugin_info = { | ||
| 29 | .version = "20160402", | ||
| 30 | .help = "sancov plugin\n", | ||
| 31 | }; | ||
| 32 | |||
| 33 | static unsigned int sancov_execute(void) | ||
| 34 | { | ||
| 35 | basic_block bb; | ||
| 36 | |||
| 37 | /* Remove this line when this plugin and kcov will be in the kernel. | ||
| 38 | if (!strcmp(DECL_NAME_POINTER(current_function_decl), DECL_NAME_POINTER(sancov_fndecl))) | ||
| 39 | return 0; | ||
| 40 | */ | ||
| 41 | |||
| 42 | FOR_EACH_BB_FN(bb, cfun) { | ||
| 43 | const_gimple stmt; | ||
| 44 | gcall *gcall; | ||
| 45 | gimple_stmt_iterator gsi = gsi_after_labels(bb); | ||
| 46 | |||
| 47 | if (gsi_end_p(gsi)) | ||
| 48 | continue; | ||
| 49 | |||
| 50 | stmt = gsi_stmt(gsi); | ||
| 51 | gcall = as_a_gcall(gimple_build_call(sancov_fndecl, 0)); | ||
| 52 | gimple_set_location(gcall, gimple_location(stmt)); | ||
| 53 | gsi_insert_before(&gsi, gcall, GSI_SAME_STMT); | ||
| 54 | } | ||
| 55 | return 0; | ||
| 56 | } | ||
| 57 | |||
| 58 | #define PASS_NAME sancov | ||
| 59 | |||
| 60 | #define NO_GATE | ||
| 61 | #define TODO_FLAGS_FINISH TODO_dump_func | TODO_verify_stmts | TODO_update_ssa_no_phi | TODO_verify_flow | ||
| 62 | |||
| 63 | #include "gcc-generate-gimple-pass.h" | ||
| 64 | |||
| 65 | static void sancov_start_unit(void __unused *gcc_data, void __unused *user_data) | ||
| 66 | { | ||
| 67 | tree leaf_attr, nothrow_attr; | ||
| 68 | tree BT_FN_VOID = build_function_type_list(void_type_node, NULL_TREE); | ||
| 69 | |||
| 70 | sancov_fndecl = build_fn_decl("__sanitizer_cov_trace_pc", BT_FN_VOID); | ||
| 71 | |||
| 72 | DECL_ASSEMBLER_NAME(sancov_fndecl); | ||
| 73 | TREE_PUBLIC(sancov_fndecl) = 1; | ||
| 74 | DECL_EXTERNAL(sancov_fndecl) = 1; | ||
| 75 | DECL_ARTIFICIAL(sancov_fndecl) = 1; | ||
| 76 | DECL_PRESERVE_P(sancov_fndecl) = 1; | ||
| 77 | DECL_UNINLINABLE(sancov_fndecl) = 1; | ||
| 78 | TREE_USED(sancov_fndecl) = 1; | ||
| 79 | |||
| 80 | nothrow_attr = tree_cons(get_identifier("nothrow"), NULL, NULL); | ||
| 81 | decl_attributes(&sancov_fndecl, nothrow_attr, 0); | ||
| 82 | gcc_assert(TREE_NOTHROW(sancov_fndecl)); | ||
| 83 | #if BUILDING_GCC_VERSION > 4005 | ||
| 84 | leaf_attr = tree_cons(get_identifier("leaf"), NULL, NULL); | ||
| 85 | decl_attributes(&sancov_fndecl, leaf_attr, 0); | ||
| 86 | #endif | ||
| 87 | } | ||
| 88 | |||
| 89 | int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version) | ||
| 90 | { | ||
| 91 | int i; | ||
| 92 | struct register_pass_info sancov_plugin_pass_info; | ||
| 93 | const char * const plugin_name = plugin_info->base_name; | ||
| 94 | const int argc = plugin_info->argc; | ||
| 95 | const struct plugin_argument * const argv = plugin_info->argv; | ||
| 96 | bool enable = true; | ||
| 97 | |||
| 98 | static const struct ggc_root_tab gt_ggc_r_gt_sancov[] = { | ||
| 99 | { | ||
| 100 | .base = &sancov_fndecl, | ||
| 101 | .nelt = 1, | ||
| 102 | .stride = sizeof(sancov_fndecl), | ||
| 103 | .cb = >_ggc_mx_tree_node, | ||
| 104 | .pchw = >_pch_nx_tree_node | ||
| 105 | }, | ||
| 106 | LAST_GGC_ROOT_TAB | ||
| 107 | }; | ||
| 108 | |||
| 109 | /* BBs can be split afterwards?? */ | ||
| 110 | sancov_plugin_pass_info.pass = make_sancov_pass(); | ||
| 111 | #if BUILDING_GCC_VERSION >= 4009 | ||
| 112 | sancov_plugin_pass_info.reference_pass_name = "asan"; | ||
| 113 | #else | ||
| 114 | sancov_plugin_pass_info.reference_pass_name = "nrv"; | ||
| 115 | #endif | ||
| 116 | sancov_plugin_pass_info.ref_pass_instance_number = 0; | ||
| 117 | sancov_plugin_pass_info.pos_op = PASS_POS_INSERT_BEFORE; | ||
| 118 | |||
| 119 | if (!plugin_default_version_check(version, &gcc_version)) { | ||
| 120 | error(G_("incompatible gcc/plugin versions")); | ||
| 121 | return 1; | ||
| 122 | } | ||
| 123 | |||
| 124 | for (i = 0; i < argc; ++i) { | ||
| 125 | if (!strcmp(argv[i].key, "no-sancov")) { | ||
| 126 | enable = false; | ||
| 127 | continue; | ||
| 128 | } | ||
| 129 | error(G_("unkown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key); | ||
| 130 | } | ||
| 131 | |||
| 132 | register_callback(plugin_name, PLUGIN_INFO, NULL, &sancov_plugin_info); | ||
| 133 | |||
| 134 | if (!enable) | ||
| 135 | return 0; | ||
| 136 | |||
| 137 | #if BUILDING_GCC_VERSION < 6000 | ||
| 138 | register_callback(plugin_name, PLUGIN_START_UNIT, &sancov_start_unit, NULL); | ||
| 139 | register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL, (void *)>_ggc_r_gt_sancov); | ||
| 140 | register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &sancov_plugin_pass_info); | ||
| 141 | #endif | ||
| 142 | |||
| 143 | return 0; | ||
| 144 | } | ||
