Introduce GCC plugin infrastructure
Subsystems (11)
v8 cover Introduce GCC plugin infrastructure
This patch set introduce the GCC plugin infrastructure with examples for testing and documentation.
GCC plugins are loadable modules that provide extra features to the compiler. They are useful for runtime instrumentation and static analysis.
The infrastructure supports all gcc versions from 4.5 to 6.0, building out-of-tree modules and building in a separate directory. Cross-compilation
is supported too but currently only the x86, arm, arm64 and uml architectures enable plugins.
This infrastructure was ported from grsecurity/PaX. It is a CII project supported by the Linux Foundation.
Emese Revfy (4):
Shared library support
GCC plugin infrastructure
Add Cyclomatic complexity plugin
Add sancov plugin
Changes from v7:
* Moved the "The GCC plugin infrastructure supports the arm and arm64 architectures too" and the "Documentations of the GCC plugin infrastructre" patches
into the "GCC plugin infrastructure" patch
(Suggested-by: Masahiro Yamada <[email protected]>)
* Add gcc-plugin.sh to MAINTAINERS
(Suggested-by: Masahiro Yamada <[email protected]>)
* Makefile.host: Remove *shobjs from multi-depend
(Suggested-by: Masahiro Yamada <[email protected]>)
* Have distclean and mrproper targets clean the *.so files
(Suggested-by: Masahiro Yamada <[email protected]>)
* Simplied gcc-plugin-y to add plugins to HOSTLIBS
(Suggested-by: Masahiro Yamada <[email protected]>)
* Simplified Makefile.host
(Suggested-by: Masahiro Yamada <[email protected]>)
* Clean *.c.[012]*.*
(Suggested-by: Michal Marek <[email protected]>)
* Documentation: gcc plugins have to compilable as C and C++
* Enable GCC plugins for UML
Changes from v6:
* Disable the sancov plugin whenever KCOV_INSTRUMENT is disabled (Reported-by: Huang Ying <[email protected]>)
* Disable KCOV/sancov plugin because this is not a regular kernel code (Reported-by: Huang Ying <[email protected]>)
* Removed unnecessary gcc plugin cflags
(Signed-off-by: Masahiro Yamada <[email protected]>)
* Removed unnecessary gcc plugin aflags
Changes from v5:
* Set important properties on the external fndecl (Add sancov plugin)
* Revert documentation change too (Shared library support)
(Suggested-by: Kees Cook <[email protected]>)
* The GCC plugin infrastructure now supports the arm and arm64 architectures too (Signed-off-by: David Brown <[email protected]>)
* Simplify the computation of PLUGINCC (GCC plugin infrastructure) (Suggested-by: Masahiro Yamada <[email protected]>)
* Simplify the invocation of gcc-plugin.sh (GCC plugin infrastructure) (Suggested-by: Masahiro Yamada <[email protected]>)
* Make use of multi-depend (Shared library support)
(Suggested-by: Masahiro Yamada <[email protected]>)
* Remove unnecessary exports (GCC plugin infrastructure)
(Suggested-by: Masahiro Yamada <[email protected]>)
* Simplify Makefile by using addprefix (GCC plugin infrastructure) (Suggested-by: Masahiro Yamada <[email protected]>)
* Moved the gcc plugins from tools/ to scripts/ (GCC plugin infrastructure) (Suggested-by: Masahiro Yamada <[email protected]>)
* Removed plugins from KBUILD_CFLAGS_32 (GCC plugin infrastructure)
* Remove gcc-plugin target everywhere
(Suggested-by and partly Written-by: Masahiro Yamada <[email protected]>)
* There is no leaf gcc attribute in gcc-4.5 (Add sancov plugin)
* Added support to the sancov plugin with kcov (Add sancov plugin)
Changes from v4:
* Moved shared library support from the GCC plugin infrastructure patch into a different patch
* Update gcc-*.h from PaX
* Fixed gcc-common.h for gcc 6
* Added pass cloning support to the gcc pass generators
* Disable all plugins in vdso because it is userland code
* Add sancov gcc plugin: another use case for gcc plugin support in the kernel is when there is a feature in the latest gcc version and we would like to use it with older gcc versions as well (e.g., distros).
Changes from v3:
* Fix some indentation related warnings
(Suggested by checkpatch.pl)
* Add maintainer entries
* Don't run gcc_plugin.sh when the GCC_PLUGINS option is disabled or unsupported (Reported-by: Fengguang Wu <[email protected]>)
Changes from v2:
* Fixed incorrectly encoded characters
* Generate the GIMPLE, IPA, SIMPLE_IPA and RTL pass structures (Suggested-by: Rasmus Villemoes <[email protected]>)
* Write plugin related warning messages to stderr instead of stdout (Suggested-by: Kees Cook <[email protected]>)
* Mention the installation of the gcc plugin headers (Documentation)
Changes from v1:
* Move the gcc-plugins make target into a separate Makefile because there may be a lot of plugins (Suggested-by: Rasmus Villemoes)
* Simplify the dependencies of the plugin related config option (Suggested-by: Kees Cook <[email protected]>)
* Removed the unnecessary example plugin
--- .gitignore | 1 + Documentation/dontdiff | 1 + Documentation/gcc-plugins.txt | 87 +++ MAINTAINERS | 9 + Makefile | 24 +- arch/Kconfig | 36 + arch/arm/Kconfig | 1 + arch/arm64/Kconfig | 1 + arch/um/Kconfig.common | 1 + arch/x86/Kconfig | 1 + arch/x86/entry/vdso/Makefile | 3 +- arch/x86/purgatory/Makefile | 2 + lib/Kconfig.debug | 2 + scripts/Makefile | 2 +- scripts/Makefile.build | 2 +- scripts/Makefile.clean | 4 +- scripts/Makefile.gcc-plugins | 41 + scripts/Makefile.host | 55 +- scripts/gcc-plugin.sh | 51 ++ scripts/gcc-plugins/Makefile | 27 + scripts/gcc-plugins/cyc_complexity_plugin.c | 73 ++ scripts/gcc-plugins/gcc-common.h | 830 +++++++++++++++++++++ scripts/gcc-plugins/gcc-generate-gimple-pass.h | 175 +++++ scripts/gcc-plugins/gcc-generate-ipa-pass.h | 289 +++++++ scripts/gcc-plugins/gcc-generate-rtl-pass.h | 175 +++++ scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h | 175 +++++ scripts/gcc-plugins/sancov_plugin.c | 144 ++++ scripts/link-vmlinux.sh | 2 +- scripts/package/builddeb | 1 + 29 files changed, 2199 insertions(+), 16 deletions(-)
PATCH v8 1/4 Shared library support
Infrastructure for building independent shared library targets.
--- scripts/Makefile.build | 2 +- scripts/Makefile.clean | 4 +++- scripts/Makefile.host | 55 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-)
scripts/Makefile.build +1 -1
@@ -60,7 +60,7 @@ endif endif # Do not include host rules unless needed -ifneq ($(hostprogs-y)$(hostprogs-m),) +ifneq ($(hostprogs-y)$(hostprogs-m)$(hostlibs-y)$(hostlibs-m)$(hostcxxlibs-y)$(hostcxxlibs-m),) include scripts/Makefile.host endif
scripts/Makefile.clean +3 -1
@@ -38,7 +38,9 @@ subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn)) __clean-files := $(extra-y) $(extra-m) $(extra-) \ $(always) $(targets) $(clean-files) \ $(host-progs) \ - $(hostprogs-y) $(hostprogs-m) $(hostprogs-) + $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \ + $(hostlibs-y) $(hostlibs-m) $(hostlibs-) \ + $(hostcxxlibs-y) $(hostcxxlibs-m) __clean-files := $(filter-out $(no-clean-files), $(__clean-files))
scripts/Makefile.host +54 -1
78 lines not on this page · raw message
Signature · 1 line
2.8.1
PATCH v8 2/4 GCC plugin infrastructure
In reply to Emese RevfyThis patch allows to build the whole kernel with GCC plugins. It was ported from grsecurity/PaX. The infrastructure supports building out-of-tree modules and building in a separate directory. Cross-compilation is supported too. Currently the x86, arm, arm64 and uml architectures enable plugins.
The directory of the gcc plugins is scripts/gcc-plugins. You can use a file or a directory there. The plugins compile with these options:
* -fno-rtti: gcc is compiled with this option so the plugins must use it too
* -fno-exceptions: this is inherited from gcc too
* -fasynchronous-unwind-tables: this is inherited from gcc too
* -ggdb: it is useful for debugging a plugin (better backtrace on internal errors)
* -Wno-narrowing: to suppress warnings from gcc headers (ipa-utils.h)
* -Wno-unused-variable: to suppress warnings from gcc headers (gcc_version variable, plugin-version.h)
The infrastructure introduces a new Makefile target called gcc-plugins. It supports all gcc versions from 4.5 to 6.0. The scripts/gcc-plugin.sh script chooses the proper host compiler (gcc-4.7 can be built by either gcc or g++). This script also checks the availability of the included headers in scripts/gcc-plugins/gcc-common.h.
The gcc-common.h header contains frequently included headers for GCC plugins and it has a compatibility layer for the supported gcc versions.
The gcc-generate-*-pass.h headers automatically generate the registration structures for GIMPLE, SIMPLE_IPA, IPA and RTL passes.
Note that 'make clean' keeps the *.so files (only the distclean or mrproper targets clean all) because they are needed for out-of-tree modules.
The arm and arm64 architectures were tested by David Brown <[email protected]>.
--- .gitignore | 1 + Documentation/dontdiff | 1 + Documentation/gcc-plugins.txt | 87 +++ MAINTAINERS | 9 + Makefile | 16 +- arch/Kconfig | 15 + arch/arm/Kconfig | 1 + arch/arm64/Kconfig | 1 + arch/um/Kconfig.common | 1 + arch/x86/Kconfig | 1 + arch/x86/entry/vdso/Makefile | 3 +- scripts/Makefile | 2 +- scripts/Makefile.gcc-plugins | 21 + scripts/gcc-plugin.sh | 51 ++ scripts/gcc-plugins/Makefile | 20 + scripts/gcc-plugins/gcc-common.h | 830 +++++++++++++++++++++ scripts/gcc-plugins/gcc-generate-gimple-pass.h | 175 +++++ scripts/gcc-plugins/gcc-generate-ipa-pass.h | 289 +++++++ scripts/gcc-plugins/gcc-generate-rtl-pass.h | 175 +++++ scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h | 175 +++++ scripts/link-vmlinux.sh | 2 +- scripts/package/builddeb | 1 + 22 files changed, 1872 insertions(+), 5 deletions(-) create mode 100644 Documentation/gcc-plugins.txt create mode 100644 scripts/Makefile.gcc-plugins create mode 100755 scripts/gcc-plugin.sh create mode 100644 scripts/gcc-plugins/Makefile create mode 100644 scripts/gcc-plugins/gcc-common.h create mode 100644 scripts/gcc-plugins/gcc-generate-gimple-pass.h create mode 100644 scripts/gcc-plugins/gcc-generate-ipa-pass.h create mode 100644 scripts/gcc-plugins/gcc-generate-rtl-pass.h create mode 100644 scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h
.gitignore +1 -0
@@ -37,6 +37,7 @@ modules.builtin Module.symvers *.dwo *.su +*.c.[012]*.* # # Top-level generic files
Documentation/dontdiff +1 -0
@@ -3,6 +3,7 @@ *.bc *.bin *.bz2 +*.c.[012]*.* *.cis *.cpio *.csp
Documentation/gcc-plugins.txt new file +87 -0
87 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -4978,6 +4978,15 @@ L: [email protected] S: Odd Fixes (e.g., new signatures) F: drivers/scsi/fdomain.* +GCC PLUGINS +M: Kees Cook <[email protected]> +R: Emese Revfy <[email protected]> +L: [email protected] +S: Maintained +F: scripts/gcc-plugins/ +F: scripts/gcc-plugin.sh +F: Documentation/gcc-plugins.txt + GCOV BASED KERNEL PROFILING M: Peter Oberparleiter <[email protected]> S: Maintained
Makefile +14 -2
@@ -422,6 +422,8 @@ export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL export KBUILD_ARFLAGS +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN + # When compiling out-of-tree modules, put MODVERDIR in the module # tree rather than in the kernel tree. The kernel tree might # even be read-only.
@@ -552,7 +554,7 @@ ifeq ($(KBUILD_EXTMOD),) # in parallel PHONY += scripts scripts: scripts_basic include/config/auto.conf include/config/tristate.conf \ - asm-generic + asm-generic gcc-plugins $(Q)$(MAKE) $(build)=$(@) # Objects we will link into vmlinux / subdirs we need to visit
@@ -631,6 +633,15 @@ endif # Tell gcc to never replace conditional load with a non-conditional one KBUILD_CFLAGS += $(call cc-option,--param=allow-store-data-races=0) +PHONY += gcc-plugins +gcc-plugins: scripts_basic +ifdef CONFIG_GCC_PLUGINS + $(Q)$(MAKE) $(build)=scripts/gcc-plugins +endif + @: + +include scripts/Makefile.gcc-plugins + ifdef CONFIG_READABLE_ASM # Disable optimizations that make assembler listings hard to read. # reorder blocks reorders the control in the function
@@ -1026,7 +1037,7 @@ prepare1: prepare2 $(version_h) include/generated/utsrelease.h \ archprepare: archheaders archscripts prepare1 scripts_basic -prepare0: archprepare +prepare0: archprepare gcc-plugins $(Q)$(MAKE) $(build)=. # All the preparing..
@@ -1507,6 +1518,7 @@ clean: $(clean-dirs) -o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \ -o -name '*.symtypes' -o -name 'modules.order' \ -o -name modules.builtin -o -name '.tmp_*.o.*' \ + -o -name '*.c.[012]*.*' \ -o -name '*.gcno' \) -type f -print | xargs rm -f # Generate tags for editors
arch/Kconfig +15 -0
@@ -357,6 +357,21 @@ config SECCOMP_FILTER See Documentation/prctl/seccomp_filter.txt for details. +config HAVE_GCC_PLUGINS + bool + help + An arch should select this symbol if it supports building with + GCC plugins. + +menuconfig GCC_PLUGINS + bool "GCC plugins" + depends on HAVE_GCC_PLUGINS + help + GCC plugins are loadable modules that provide extra features to the + compiler. They are useful for runtime instrumentation and static analysis. + + See Documentation/gcc-plugins.txt for details. + config HAVE_CC_STACKPROTECTOR bool help
arch/arm/Kconfig +1 -0
@@ -55,6 +55,7 @@ config ARM select HAVE_FUNCTION_GRAPH_TRACER if (!THUMB2_KERNEL) select HAVE_FUNCTION_TRACER if (!XIP_KERNEL) select HAVE_GENERIC_DMA_COHERENT + select HAVE_GCC_PLUGINS select HAVE_HW_BREAKPOINT if (PERF_EVENTS && (CPU_V6 || CPU_V6K || CPU_V7)) select HAVE_IDE if PCI || ISA || PCMCIA select HAVE_IRQ_TIME_ACCOUNTING
arch/arm64/Kconfig +1 -0
@@ -76,6 +76,7 @@ config ARM64 select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER + select HAVE_GCC_PLUGINS select HAVE_GENERIC_DMA_COHERENT select HAVE_HW_BREAKPOINT if PERF_EVENTS select HAVE_IRQ_TIME_ACCOUNTING
arch/um/Kconfig.common +1 -0
@@ -9,6 +9,7 @@ config UML select GENERIC_CPU_DEVICES select GENERIC_IO select GENERIC_CLOCKEVENTS + select HAVE_GCC_PLUGINS select TTY # Needed for line.c config MMU
arch/x86/Kconfig +1 -0
@@ -156,6 +156,7 @@ config X86 select SYSCTL_EXCEPTION_TRACE select USER_STACKTRACE_SUPPORT select VIRT_TO_BUS + select HAVE_GCC_PLUGINS select X86_DEV_DMA_OPS if X86_64 select X86_FEATURE_NAMES if PROC_FS select HAVE_STACK_VALIDATION if X86_64
arch/x86/entry/vdso/Makefile +2 -1
@@ -75,7 +75,7 @@ CFL := $(PROFILING) -mcmodel=small -fPIC -O2 -fasynchronous-unwind-tables -m64 \ -fno-omit-frame-pointer -foptimize-sibling-calls \ -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO -$(vobjs): KBUILD_CFLAGS += $(CFL) +$(vobjs): KBUILD_CFLAGS := $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS)) $(CFL) # # vDSO code runs in userspace and -pg doesn't help with profiling anyway.
@@ -145,6 +145,7 @@ KBUILD_CFLAGS_32 := $(filter-out -m64,$(KBUILD_CFLAGS)) KBUILD_CFLAGS_32 := $(filter-out -mcmodel=kernel,$(KBUILD_CFLAGS_32)) KBUILD_CFLAGS_32 := $(filter-out -fno-pic,$(KBUILD_CFLAGS_32)) KBUILD_CFLAGS_32 := $(filter-out -mfentry,$(KBUILD_CFLAGS_32)) +KBUILD_CFLAGS_32 := $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS_32)) KBUILD_CFLAGS_32 += -m32 -msoft-float -mregparm=0 -fpic KBUILD_CFLAGS_32 += $(call cc-option, -fno-stack-protector) KBUILD_CFLAGS_32 += $(call cc-option, -foptimize-sibling-calls)
scripts/Makefile +1 -1
@@ -47,4 +47,4 @@ subdir-$(CONFIG_DTC) += dtc subdir-$(CONFIG_GDB_SCRIPTS) += gdb # Let clean descend into subdirs -subdir- += basic kconfig package +subdir- += basic kconfig package gcc-plugins
scripts/Makefile.gcc-plugins new file +21 -0
@@ -0,0 +1,21 @@ +ifdef CONFIG_GCC_PLUGINS + __PLUGINCC := $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC)) + PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") + + GCC_PLUGINS_CFLAGS := $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) + + ifeq ($(PLUGINCC),) + ifneq ($(GCC_PLUGINS_CFLAGS),) + ifeq ($(call cc-ifversion, -ge, 0405, y), y) + PLUGINCC := $(shell $(CONFIG_SHELL) -x $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") + $(error error, your gcc installation does not support plugins, perhaps the necessary headers are missing?) + else + $(error error, your gcc version does not support plugins, you should upgrade it to gcc 4.5 at least) + endif + endif + endif + + KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS) + GCC_PLUGIN := $(gcc-plugin-y) + +endif
scripts/gcc-plugin.sh new file +51 -0
51 lines not on this page · raw message
scripts/gcc-plugins/Makefile new file +20 -0
@@ -0,0 +1,20 @@ +GCC_PLUGINS_DIR := $(shell $(CC) -print-file-name=plugin) + +ifeq ($(PLUGINCC),$(HOSTCC)) + HOSTLIBS := hostlibs + HOST_EXTRACFLAGS += -I$(GCC_PLUGINS_DIR)/include -I$(src) -std=gnu99 -ggdb + export HOST_EXTRACFLAGS +else + HOSTLIBS := hostcxxlibs + HOST_EXTRACXXFLAGS += -I$(GCC_PLUGINS_DIR)/include -I$(src) -std=gnu++98 -fno-rtti + HOST_EXTRACXXFLAGS += -fno-exceptions -fasynchronous-unwind-tables -ggdb + HOST_EXTRACXXFLAGS += -Wno-narrowing -Wno-unused-variable + export HOST_EXTRACXXFLAGS +endif + +export GCCPLUGINS_DIR HOSTLIBS + +$(HOSTLIBS)-y := $(GCC_PLUGIN) +always := $($(HOSTLIBS)-y) + +clean-files += *.so
scripts/gcc-plugins/gcc-common.h new file +830 -0
830 lines not on this page · raw message
scripts/gcc-plugins/gcc-generate-gimple-pass.h new file +175 -0
175 lines not on this page · raw message
scripts/gcc-plugins/gcc-generate-ipa-pass.h new file +289 -0
289 lines not on this page · raw message
scripts/gcc-plugins/gcc-generate-rtl-pass.h new file +175 -0
175 lines not on this page · raw message
scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h new file +175 -0
175 lines not on this page · raw message
scripts/link-vmlinux.sh +1 -1
@@ -180,7 +180,7 @@ else fi; # final build of init/ -${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init +${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}" kallsymso="" kallsyms_vmlinux=""
scripts/package/builddeb +1 -0
@@ -329,6 +329,7 @@ fi (cd $srctree; find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform) >> "$objtree/debian/hdrsrcfiles" (cd $srctree; find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f) >> "$objtree/debian/hdrsrcfiles" (cd $objtree; find arch/$SRCARCH/include Module.symvers include scripts -type f) >> "$objtree/debian/hdrobjfiles" +(cd $objtree; find scripts/gcc-plugins -name \*.so -o -name gcc-common.h) >> "$objtree/debian/hdrobjfiles" destdir=$kernel_headers_dir/usr/src/linux-headers-$version mkdir -p "$destdir" (cd $srctree; tar -c -f - -T -) < "$objtree/debian/hdrsrcfiles" | (cd $destdir; tar -xf -)
Signature · 1 line
2.8.1
PATCH v8 3/4 Add Cyclomatic complexity GCC plugin
In reply to Emese RevfyAdd a very simple plugin to demonstrate the GCC plugin infrastructure. This GCC plugin computes the cyclomatic complexity of each function.
The complexity M of a function's control flow graph is defined as:
M = E - N + 2P
where
E = the number of edges
N = the number of nodes
P = the number of connected components (exit nodes).
--- arch/Kconfig | 12 +++++ scripts/Makefile.gcc-plugins | 1 + scripts/gcc-plugins/Makefile | 1 + scripts/gcc-plugins/cyc_complexity_plugin.c | 73 +++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 scripts/gcc-plugins/cyc_complexity_plugin.c
arch/Kconfig +12 -0
@@ -372,6 +372,18 @@ menuconfig GCC_PLUGINS See Documentation/gcc-plugins.txt for details. +config GCC_PLUGIN_CYC_COMPLEXITY + bool "Compute the cyclomatic complexity of a function" + depends on GCC_PLUGINS + help + The complexity M of a function's control flow graph is defined as: + M = E - N + 2P + where + + E = the number of edges + N = the number of nodes + P = the number of connected components (exit nodes). + config HAVE_CC_STACKPROTECTOR bool help
scripts/Makefile.gcc-plugins +1 -0
@@ -2,6 +2,7 @@ ifdef CONFIG_GCC_PLUGINS __PLUGINCC := $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC)) PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") + gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so GCC_PLUGINS_CFLAGS := $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) ifeq ($(PLUGINCC),)
scripts/gcc-plugins/Makefile +1 -0
@@ -17,4 +17,5 @@ export GCCPLUGINS_DIR HOSTLIBS $(HOSTLIBS)-y := $(GCC_PLUGIN) always := $($(HOSTLIBS)-y) +cyc_complexity_plugin-objs := cyc_complexity_plugin.o clean-files += *.so
scripts/gcc-plugins/cyc_complexity_plugin.c new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.8.1
PATCH v8 4/4 Add sancov plugin
In reply to Emese RevfyThe 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).
--- Makefile | 12 +-- arch/Kconfig | 9 +++ arch/x86/purgatory/Makefile | 2 + lib/Kconfig.debug | 2 + scripts/Makefile.gcc-plugins | 19 +++++ scripts/gcc-plugins/Makefile | 6 ++ scripts/gcc-plugins/sancov_plugin.c | 144 ++++++++++++++++++++++++++++++++++++ 7 files changed, 184 insertions(+), 10 deletions(-) create mode 100644 scripts/gcc-plugins/sancov_plugin.c
Makefile +2 -10
@@ -369,7 +369,7 @@ LDFLAGS_MODULE = CFLAGS_KERNEL = AFLAGS_KERNEL = CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized -CFLAGS_KCOV = -fsanitize-coverage=trace-pc +CFLAGS_KCOV := $(call cc-option,-fsanitize-coverage=trace-pc,) # Use USERINCLUDE when you must reference the UAPI directories only.
@@ -422,7 +422,7 @@ export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL export KBUILD_ARFLAGS -export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN +export PLUGINCC GCC_PLUGINS_CFLAGS SANCOV_PLUGIN GCC_PLUGIN # When compiling out-of-tree modules, put MODVERDIR in the module # tree rather than in the kernel tree. The kernel tree might
@@ -693,14 +693,6 @@ endif endif KBUILD_CFLAGS += $(stackp-flag) -ifdef CONFIG_KCOV - ifeq ($(call cc-option, $(CFLAGS_KCOV)),) - $(warning Cannot use CONFIG_KCOV: \ - -fsanitize-coverage=trace-pc is not supported by compiler) - CFLAGS_KCOV = - endif -endif - ifeq ($(cc-name),clang) KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,) KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
arch/Kconfig +9 -0
@@ -384,6 +384,15 @@ config GCC_PLUGIN_CYC_COMPLEXITY N = the number of nodes P = the number of connected components (exit nodes). +config GCC_PLUGIN_SANCOV + bool + depends on GCC_PLUGINS + help + This plugin inserts a __sanitizer_cov_trace_pc() call at the start of + basic blocks. It supports all gcc versions with plugin support (from + gcc-4.5 on). It is based on the commit "Add fuzzing coverage support" + by Dmitry Vyukov <[email protected]>. + config HAVE_CC_STACKPROTECTOR bool help
arch/x86/purgatory/Makefile +2 -0
@@ -8,6 +8,8 @@ PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y)) LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib targets += purgatory.ro +KCOV_INSTRUMENT := n + # Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That # in turn leaves some undefined symbols like __fentry__ in purgatory and not # sure how to relocate those. Like kexec-tools, use custom flags.
lib/Kconfig.debug +2 -0
@@ -712,6 +712,8 @@ config KCOV bool "Code coverage for fuzzing" depends on ARCH_HAS_KCOV select DEBUG_FS + select GCC_PLUGINS + select GCC_PLUGIN_SANCOV help KCOV exposes kernel code coverage information in a form suitable for coverage-guided fuzzing (randomized testing).
scripts/Makefile.gcc-plugins +19 -0
@@ -2,7 +2,23 @@ ifdef CONFIG_GCC_PLUGINS __PLUGINCC := $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC)) PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") + SANCOV_PLUGIN := -fplugin=$(objtree)/scripts/gcc-plugins/sancov_plugin.so + gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so + + ifdef CONFIG_GCC_PLUGIN_SANCOV + ifeq ($(CFLAGS_KCOV),) + # It is needed because of the gcc-plugin.sh and gcc version checks. + gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so + + ifneq ($(PLUGINCC),) + CFLAGS_KCOV := $(SANCOV_PLUGIN) + else + $(error Cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler) + endif + endif + endif + GCC_PLUGINS_CFLAGS := $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) ifeq ($(PLUGINCC),)
@@ -14,6 +30,9 @@ ifdef CONFIG_GCC_PLUGINS $(error error, your gcc version does not support plugins, you should upgrade it to gcc 4.5 at least) endif endif + else + # SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication. + GCC_PLUGINS_CFLAGS := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGINS_CFLAGS)) endif KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS)
scripts/gcc-plugins/Makefile +6 -0
@@ -14,8 +14,14 @@ endif export GCCPLUGINS_DIR HOSTLIBS +ifneq ($(CFLAGS_KCOV), $(SANCOV_PLUGIN)) + GCC_PLUGIN := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGIN)) +endif + $(HOSTLIBS)-y := $(GCC_PLUGIN) always := $($(HOSTLIBS)-y) cyc_complexity_plugin-objs := cyc_complexity_plugin.o +sancov_plugin-objs := sancov_plugin.o + clean-files += *.so
scripts/gcc-plugins/sancov_plugin.c new file +144 -0
144 lines not on this page · raw message
Signature · 1 line
2.8.1
Re: [PATCH v8 2/4] GCC plugin infrastructure
In reply to Emese RevfyDne 13.5.2016 v 01:57 Emese Revfy napsal(a):
Quoted 14 lines
scripts/Makefile.gcc-plugins new file +11
-0@@ -0,0 +1,21 @@ +ifdef CONFIG_GCC_PLUGINS + __PLUGINCC := $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC)) + PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") + + GCC_PLUGINS_CFLAGS := $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) + + ifeq ($(PLUGINCC),) + ifneq ($(GCC_PLUGINS_CFLAGS),) + ifeq ($(call cc-ifversion, -ge, 0405, y), y) + PLUGINCC := $(shell $(CO, NFIG_SHELL) -x $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") + $(error error, your gcc installation does not support plugins, perhaps the necessary headers are missing?)
scripts/Makefile.gcc-plugins line 21
Quoted 10 lines
scripts/Makefile.gcc-plugins new file +10
-0+ else + $(error error, your gcc version does not support plugins, you should upgrade it to gcc 4.5 at least) + endif + endif + endif + + KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS) + GCC_PLUGIN := $(gcc-plugin-y) + +endif
Hi Emese,
sorry for the late feedback. A problem I ran into now is that if you have a compiler that is either too old or built with plugin support disabled, scripts/Makefile.gcc-plugins errors out immediately. This is going to be an issue with allmodconfig/allyesconfig test builds. A solution would either be to make the respective options depend on !COMPILE_TEST, or turn the errors into warnings and do nothing if the compiler lacks plugin support. We are handling CONFIG_CC_STACKPROTECTOR on x86 this way already.
Michal
Re: [kernel-hardening] [PATCH v8 3/4] Add Cyclomatic complexity GCC plugin
In reply to Emese RevfyQuoted 2 lines · Emese Revfy
Add a very simple plugin to demonstrate the GCC plugin infrastructure. This GCC plugin computes the cyclomatic complexity of each function.
...
arch/Kconfig menuconfig GCC_PLUGINS · line 386
Quoted 12 lines
arch/Kconfig +12
-0+config GCC_PLUGIN_CYC_COMPLEXITY + bool "Compute the cyclomatic complexity of a function" + depends on GCC_PLUGINS + help + The complexity M of a function's control flow graph is defined as: + M = E - N + 2P + where + + E = the number of edges + N = the number of nodes + P = the number of connected components (exit nodes). +
If this plugin is intended primarily as a demonstration it's probably worth mentioning this in the Kconfig description.
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
Re: [kernel-hardening] [PATCH v8 2/4] GCC plugin infrastructure
In reply to Emese RevfyQuoted 28 lines · Emese Revfy
This patch allows to build the whole kernel with GCC plugins. It was ported from grsecurity/PaX. The infrastructure supports building out-of-tree modules and building in a separate directory. Cross-compilation is supported too. Currently the x86, arm, arm64 and uml architectures enable plugins.
The directory of the gcc plugins is
scripts/gcc-plugins. You can use a file or a directory there. The plugins compile with these options:
* -fno-rtti: gcc is compiled with this option so the plugins must use it too
* -fno-exceptions: this is inherited from gcc too
* -fasynchronous-unwind-tables: this is inherited from gcc too
* -ggdb: it is useful for debugging a plugin (better backtrace on internal errors)
* -Wno-narrowing: to suppress warnings from gcc headers (ipa-utils.h)
* -Wno-unused-variable: to suppress warnings from gcc headers (gcc_versionvariable,plugin-version.h)The infrastructure introduces a new
Makefiletarget called gcc-plugins. It supports all gcc versions from 4.5 to 6.0. Thescripts/gcc-plugin.shscript chooses the proper host compiler (gcc-4.7 can be built by either gcc or g++). This script also checks the availability of the included headers inscripts/gcc-plugins/gcc-common.h.The
gcc-common.hheader contains frequently included headers for GCC plugins and it has a compatibility layer for the supported gcc versions.The gcc-generate-*-pass.h headers automatically generate the registration structures for GIMPLE,
SIMPLE_IPA, IPA and RTL passes.
Quoted 6 lines
Note that 'make clean' keeps the *.so files (only the distclean or mrproper targets clean all) because they are needed for out-of-tree modules.
The arm and arm64 architectures were tested by David Brown <[email protected]>.
Signed-off-by Emese Revfy <[email protected]>
I've done some basic sanity testing on powerpc with the cyclomatic complexity plugin (with LE native + cross-compilers) and it seems to work with the patch below.
arch/powerpc/Kconfig +1 -0
@@ -97,6 +97,7 @@ config PPC select HAVE_DYNAMIC_FTRACE_WITH_REGS if MPROFILE_KERNEL select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER + select HAVE_GCC_PLUGINS select SYSCTL_EXCEPTION_TRACE select ARCH_WANT_OPTIONAL_GPIOLIB select VIRT_TO_BUS if !PPC64
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
Re: [kernel-hardening] [PATCH v8 2/4] GCC plugin infrastructure
Quoted 11 lines
I've done some basic sanity testing on powerpc with the cyclomatic complexity plugin (with LE native + cross-compilers) and it seems to work with the patch below.
Signed-off-by Andrew Donnellan <[email protected]>arch/powerpc/Kconfig +0
-0
arch/powerpc/Kconfig config PPC · line 103
Quoted 7 lines
arch/powerpc/Kconfig +1
-0@@ -97,6 +97,7 @@ config PPC select HAVE_DYNAMIC_FTRACE_WITH_REGS if MPROFILE_KERNEL select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER + select HAVE_GCC_PLUGINS select SYSCTL_EXCEPTION_TRACE select ARCH_WANT_OPTIONAL_GPIOLIB select VIRT_TO_BUS if !PPC64
Hi,
Did you test the plugins with all gcc versions (4.5-6)?
Signature · 1 line
Emese
Re: [kernel-hardening] [PATCH v8 3/4] Add Cyclomatic complexity GCC plugin
In reply to Andrew DonnellanQuoted 6 lines · On Wed, 18 May 2016 18:25:00 +1000 Andrew Donnellan…
Quoted 2 lines · Emese Revfy
Add a very simple plugin to demonstrate the GCC plugin infrastructure. This GCC plugin computes the cyclomatic complexity of each function.
...
Quoted 15 lines
arch/Kconfig menuconfig GCC_PLUGINS · line 386
Quoted 12 lines
arch/Kconfig +12
-0+config GCC_PLUGIN_CYC_COMPLEXITY + bool "Compute the cyclomatic complexity of a function" + depends on GCC_PLUGINS + help + The complexity M of a function's control flow graph is defined as: + M = E - N + 2P + where + + E = the number of edges + N = the number of nodes + P = the number of connected components (exit nodes). +If this plugin is intended primarily as a demonstration it's probably worth mentioning this in the
Kconfigdescription.
Hi,
Before sancov this plugin demonstrated the gcc plugin infrastructure but otherwise this is a fully working plugin not a test plugin. Also it doesn't demonstrate all aspects of writing gcc plugins.
Signature · 1 line
Emese
Re: [PATCH v8 2/4] GCC plugin infrastructure
In reply to Michal MarekQuoted 28 lines · On Tue, 17 May 2016 16:28:05 +0200 Michal Marek <[email protected]> wrote:
Dne 13.5.2016 v 01:57 Emese Revfy napsal(a):
Quoted 14 lines
scripts/Makefile.gcc-plugins new file +11
-0@@ -0,0 +1,21 @@ +ifdef CONFIG_GCC_PLUGINS + __PLUGINCC := $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC)) + PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") + + GCC_PLUGINS_CFLAGS := $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) + + ifeq ($(PLUGINCC),) + ifneq ($(GCC_PLUGINS_CFLAGS),) + ifeq ($(call cc-ifversion, -ge, 0405, y), y) + PLUGINCC := $(shell $(CO, NFIG_SHELL) -x $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)") + $(error error, your gcc installation does not support plugins, perhaps the necessary headers are missing?)scripts/Makefile.gcc-plugins line 21
Quoted 10 lines
scripts/Makefile.gcc-plugins new file +10
-0+ else + $(error error, your gcc version does not support plugins, you should upgrade it to gcc 4.5 at least) + endif + endif + endif + + KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS) + GCC_PLUGIN := $(gcc-plugin-y) + +endifHi Emese,
Quoted 8 lines
sorry for the late feedback. A problem I ran into now is that if you have a compiler that is either too old or built with plugin support disabled,
scripts/Makefile.gcc-pluginserrors out immediately. This is going to be an issue with allmodconfig/allyesconfig test builds. A solution would either be to make the respective options depend on !COMPILE_TEST, or turn the errors into warnings and do nothing if the compiler lacks plugin support. We are handlingCONFIG_CC_STACKPROTECTORon x86 this way already.
Hi,
I'll change the errors to warnings in the next patch set.
Signature · 1 line
Emese
Re: [kernel-hardening] [PATCH v8 2/4] GCC plugin infrastructure
In reply to Emese RevfyQuoted 1 line · Emese Revfy
Did you test the plugins with all gcc versions (4.5-6)?
No, I tested with 4.8 and 5.2 as those are the toolchains I have on hand - I'll try to test with the rest of 4.5 - 6.1.
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
Michael Ellerman
reviewerRe: [kernel-hardening] [PATCH v8 2/4] GCC plugin infrastructure
In reply to Emese RevfyQuoted 21 lines · Emese Revfy
Quoted 11 lines
I've done some basic sanity testing on powerpc with the cyclomatic complexity plugin (with LE native + cross-compilers) and it seems to work with the patch below.
Signed-off-by Andrew Donnellan <[email protected]>arch/powerpc/Kconfig +0
-0arch/powerpc/Kconfig config PPC · line 103
Quoted 7 lines
arch/powerpc/Kconfig +1
-0@@ -97,6 +97,7 @@ config PPC select HAVE_DYNAMIC_FTRACE_WITH_REGS if MPROFILE_KERNEL select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER + select HAVE_GCC_PLUGINS select SYSCTL_EXCEPTION_TRACE select ARCH_WANT_OPTIONAL_GPIOLIB select VIRT_TO_BUS if !PPC64Hi,
Quoted 1 line
Did you test the plugins with all gcc versions (4.5-6)?
What's the concern about gcc versions? Just not breaking the build on old compilers?
I'm pretty sure powerpc big endian still builds with gcc 4.4.
However if Andrew's only tested on little endian, then that select should be guarded with an "if CPU_LITTLE_ENDIAN". And to build LE you need gcc >= 4.9.
cheers
Re: [kernel-hardening] [PATCH v8 2/4] GCC plugin infrastructure
Quoted 5 lines · Michael Ellerman
Quoted 1 line
Did you test the plugins with all gcc versions (4.5-6)?
What's the concern about gcc versions? Just not breaking the build on old compilers?
I'm pretty sure powerpc big endian still builds with gcc 4.4.
gcc's plugin support only landed in 4.5, so we don't care about <=4.4.
Quoted 2 lines
However if Andrew's only tested on little endian, then that select should be guarded with an "if
CPU_LITTLE_ENDIAN". And to build LE you need gcc >= 4.9.
I'm going to give BE a test too just to be sure.
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
Re: [kernel-hardening] [PATCH v8 2/4] GCC plugin infrastructure
In reply to Michael EllermanQuoted 5 lines · Michael Ellerman
Quoted 1 line · Emese Revfy
Did you test the plugins with all gcc versions (4.5-6)?
What's the concern about gcc versions? Just not breaking the build on old compilers?
the earlier plugin capable gcc versions used to install gcc headers in a somewhat ad-hoc manner resulting in compile time breakage for plugins and since some of those potentially missing headers are target specific, each target arch should be verified before enabling plugin support on them. things have much improved with gcc 5 (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61176) though there's still an occasional missing header but with wider use of plugins they will hopefully be discovered earlier now. perhaps linux-arch should be cc'ed on the plugin infrastructure so that arch maintainers are aware of this?
Quoted 4 lines
I'm pretty sure powerpc big endian still builds with gcc 4.4.
However if Andrew's only tested on little endian, then that select should be guarded with an "if
CPU_LITTLE_ENDIAN". And to build LE you need gcc >= 4.9.
i guess that's part of the target tuple so in general arch maintainers should test the target tuples used on their arch with all the supported gcc versions (speaking of CC, not HOSTCC/HOSTCXX).
cheers,
PaX Team
Re: [kernel-hardening] [PATCH v8 2/4] GCC plugin infrastructure
Quoted 8 lines · PaX Team
the earlier plugin capable gcc versions used to install gcc headers in a somewhat ad-hoc manner resulting in compile time breakage for plugins and since some of those potentially missing headers are target specific, each target arch should be verified before enabling plugin support on them. things have much improved with gcc 5 (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61176) though there's still an occasional missing header but with wider use of plugins they will hopefully be discovered earlier now. perhaps linux-arch should be cc'ed on the plugin infrastructure so that arch maintainers are aware of this?
Upon further testing it does seem we've got header issues as well as hitting a segfault on 4.7.0. Looking into it further...
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
Michael Ellerman
reviewerRe: [kernel-hardening] [PATCH v8 2/4] GCC plugin infrastructure
In reply to PaX TeamQuoted 7 lines · PaX Team
Quoted 5 lines · Michael Ellerman
Quoted 1 line · Emese Revfy
Did you test the plugins with all gcc versions (4.5-6)?
What's the concern about gcc versions? Just not breaking the build on old compilers?
Quoted 7 lines
the earlier plugin capable gcc versions used to install gcc headers in a somewhat ad-hoc manner resulting in compile time breakage for plugins and since some of those potentially missing headers are target specific, each target arch should be verified before enabling plugin support on them. things have much improved with gcc 5 (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61176) though there's still an occasional missing header but with wider use of plugins they will hopefully be discovered earlier now.
OK thanks.
Quoted 2 lines
perhaps linux-arch should be cc'ed on the plugin infrastructure so that arch maintainers are aware of this?
linux-arch is still fairly high traffic, so it's no guarantee arch maintainers will see it, but it's probably worth a try.
Quoted 8 lines
Quoted 4 lines
I'm pretty sure powerpc big endian still builds with gcc 4.4.
However if Andrew's only tested on little endian, then that select should be guarded with an "if
CPU_LITTLE_ENDIAN". And to build LE you need gcc >= 4.9.i guess that's part of the target tuple so in general arch maintainers should test the target tuples used on their arch with all the supported gcc versions (speaking of CC, not HOSTCC/HOSTCXX).
Yeah. I think we'll probably enable it gradually as folks get time to test it. ie. ppc64le first, then ppc64 (BE), and then 32-bit if someone is interested.
cheers