aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmese Revfy <re.emese@gmail.com>2016-05-23 18:11:37 -0400
committerMichal Marek <mmarek@suse.com>2016-06-07 16:57:10 -0400
commit543c37cb165049c3be24a0d4733e67caa2b33eef (patch)
tree91f3e0bb972a6283b607da394268e7e5872292b5
parent0dae776c6bf31e779c172753f6e2d6426eb42523 (diff)
Add sancov plugin
The sancov gcc plugin inserts a __sanitizer_cov_trace_pc() call at the start of basic blocks. This plugin is a helper plugin for the kcov feature. It supports all gcc versions with plugin support (from gcc-4.5 on). It is based on the gcc commit "Add fuzzing coverage support" by Dmitry Vyukov (https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296). Signed-off-by: Emese Revfy <re.emese@gmail.com> Acked-by: Kees Cook <keescook@chromium.org> Signed-off-by: Michal Marek <mmarek@suse.com>
-rw-r--r--Makefile10
-rw-r--r--arch/Kconfig9
-rw-r--r--arch/x86/purgatory/Makefile2
-rw-r--r--lib/Kconfig.debug2
-rw-r--r--scripts/Makefile.gcc-plugins21
-rw-r--r--scripts/gcc-plugins/Makefile6
-rw-r--r--scripts/gcc-plugins/sancov_plugin.c144
7 files changed, 184 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index 20fcf4b26fbc..c3d494ba3ae1 100644
--- a/Makefile
+++ b/Makefile
@@ -369,7 +369,7 @@ LDFLAGS_MODULE =
369CFLAGS_KERNEL = 369CFLAGS_KERNEL =
370AFLAGS_KERNEL = 370AFLAGS_KERNEL =
371CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized 371CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized
372CFLAGS_KCOV = -fsanitize-coverage=trace-pc 372CFLAGS_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
691endif 691endif
692KBUILD_CFLAGS += $(stackp-flag) 692KBUILD_CFLAGS += $(stackp-flag)
693 693
694ifdef 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
700endif
701
702ifeq ($(cc-name),clang) 694ifeq ($(cc-name),clang)
703KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,) 695KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
704KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,) 696KBUILD_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
387config 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
387config HAVE_CC_STACKPROTECTOR 396config 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))
8LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib 8LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib
9targets += purgatory.ro 9targets += purgatory.ro
10 10
11KCOV_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
15export GCCPLUGINS_DIR HOSTLIBS 15export GCCPLUGINS_DIR HOSTLIBS
16 16
17ifneq ($(CFLAGS_KCOV), $(SANCOV_PLUGIN))
18 GCC_PLUGIN := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGIN))
19endif
20
17$(HOSTLIBS)-y := $(GCC_PLUGIN) 21$(HOSTLIBS)-y := $(GCC_PLUGIN)
18always := $($(HOSTLIBS)-y) 22always := $($(HOSTLIBS)-y)
19 23
20cyc_complexity_plugin-objs := cyc_complexity_plugin.o 24cyc_complexity_plugin-objs := cyc_complexity_plugin.o
25sancov_plugin-objs := sancov_plugin.o
26
21clean-files += *.so 27clean-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
24int plugin_is_GPL_compatible;
25
26tree sancov_fndecl;
27
28static struct plugin_info sancov_plugin_info = {
29 .version = "20160402",
30 .help = "sancov plugin\n",
31};
32
33static 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
65static 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
89int 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 = &gt_ggc_mx_tree_node,
104 .pchw = &gt_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 *)&gt_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}