Introduce GCC plugin infrastructure
Subsystems (4)
v3 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 architecture enables plugins.
This infrastructure was ported from grsecurity/PaX. It is a CII project supported by the Linux Foundation.
Emese Revfy (3):
GCC plugin infrastructure
Add Cyclomatic complexity plugin
Documentations of the GCC plugin infrastructre
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
--- Documentation/dontdiff | 1 + Documentation/gcc-plugins.txt | 80 ++++ Makefile | 41 +- arch/Kconfig | 24 + arch/x86/Kconfig | 1 + init/Makefile | 3 + scripts/Makefile.build | 2 +- scripts/Makefile.clean | 3 +- scripts/Makefile.gcc-plugins | 30 ++ scripts/Makefile.host | 69 ++- scripts/gcc-plugin.sh | 51 ++ scripts/link-vmlinux.sh | 2 +- scripts/package/builddeb | 1 + tools/gcc/Makefile | 19 + tools/gcc/cyc_complexity_plugin.c | 70 +++ tools/gcc/gcc-common.h | 794 +++++++++++++++++++++++++++++++ tools/gcc/gcc-generate-gimple-pass.h | 172 +++++++ tools/gcc/gcc-generate-ipa-pass.h | 286 +++++++++++ tools/gcc/gcc-generate-rtl-pass.h | 172 +++++++ tools/gcc/gcc-generate-simple_ipa-pass.h | 172 +++++++ 20 files changed, 1978 insertions(+), 15 deletions(-)
PATCH v3 1/3 GCC plugin infrastructure
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 but currently only the x86 architecture enables plugins.
The directory of the gcc plugins is tools/gcc. 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 tools/gcc/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.
--- Documentation/dontdiff | 1 + Makefile | 41 +- arch/Kconfig | 11 + arch/x86/Kconfig | 1 + init/Makefile | 3 + scripts/Makefile.build | 2 +- scripts/Makefile.clean | 3 +- scripts/Makefile.gcc-plugins | 26 + scripts/Makefile.host | 69 ++- scripts/gcc-plugin.sh | 51 ++ scripts/link-vmlinux.sh | 2 +- scripts/package/builddeb | 1 + tools/gcc/Makefile | 17 + tools/gcc/gcc-common.h | 794 +++++++++++++++++++++++++++++++ tools/gcc/gcc-generate-gimple-pass.h | 172 +++++++ tools/gcc/gcc-generate-ipa-pass.h | 286 +++++++++++ tools/gcc/gcc-generate-rtl-pass.h | 172 +++++++ tools/gcc/gcc-generate-simple_ipa-pass.h | 172 +++++++ 18 files changed, 1809 insertions(+), 15 deletions(-) create mode 100644 scripts/Makefile.gcc-plugins create mode 100644 scripts/gcc-plugin.sh create mode 100644 tools/gcc/Makefile create mode 100644 tools/gcc/gcc-common.h create mode 100644 tools/gcc/gcc-generate-gimple-pass.h create mode 100644 tools/gcc/gcc-generate-ipa-pass.h create mode 100644 tools/gcc/gcc-generate-rtl-pass.h create mode 100644 tools/gcc/gcc-generate-simple_ipa-pass.h
Documentation/dontdiff +1 -0
@@ -3,6 +3,7 @@ *.bc *.bin *.bz2 +*.c.[012]*.* *.cis *.cpio *.csp
Makefile +30 -11
121 lines not on this page · raw message
arch/Kconfig +11 -0
@@ -357,6 +357,17 @@ config SECCOMP_FILTER See Documentation/prctl/seccomp_filter.txt for details. +menu "GCC plugins" + +config HAVE_GCC_PLUGINS + bool + help + An arch should select this symbol if it supports building with + gcc plugins. + + +endmenu # "GCC plugins" + config HAVE_CC_STACKPROTECTOR bool help
arch/x86/Kconfig +1 -0
@@ -157,6 +157,7 @@ config X86 select VIRT_TO_BUS select X86_DEV_DMA_OPS if X86_64 select X86_FEATURE_NAMES if PROC_FS + select HAVE_GCC_PLUGINS config INSTRUCTION_DECODER def_bool y
init/Makefile +3 -0
@@ -2,6 +2,9 @@ # Makefile for the linux kernel. # +ccflags-y := $(GCC_PLUGINS_CFLAGS) +asflags-y := $(GCC_PLUGINS_AFLAGS) + obj-y := main.o version.o mounts.o ifneq ($(CONFIG_BLK_DEV_INITRD),y) obj-y += noinitramfs.o
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 +2 -1
@@ -38,7 +38,8 @@ 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-) __clean-files := $(filter-out $(no-clean-files), $(__clean-files))
scripts/Makefile.gcc-plugins new file +26 -0
@@ -0,0 +1,26 @@ +ifeq ($(call cc-ifversion, -ge, 0408, y), y) +PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCXX)" "$(HOSTCXX)" "$(CC)") +else +PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)") +endif +ifneq ($(PLUGINCC),) +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS +ifeq ($(KBUILD_EXTMOD),) +gcc-plugins: + $(Q)$(MAKE) $(build)=tools/gcc +else +gcc-plugins: ; +endif +else +gcc-plugins: +ifeq ($(call cc-ifversion, -ge, 0405, y), y) + $(warning warning, your gcc installation does not support plugins, perhaps the necessary headers are missing?) +ifeq ($(call cc-ifversion, -ge, 0408, y), y) + $(CONFIG_SHELL) -x $(srctree)/scripts/gcc-plugin.sh "$(HOSTCXX)" "$(HOSTCXX)" "$(CC)" +else + $(CONFIG_SHELL) -x $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)" +endif +else + $(warning warning, your gcc version does not support plugins, you should upgrade it to gcc 4.5 at least) +endif +endif
scripts/Makefile.host +68 -1
92 lines not on this page · raw message
scripts/gcc-plugin.sh new file +51 -0
51 lines not on this page · raw message
scripts/link-vmlinux.sh +1 -1
@@ -184,7 +184,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}" GCC_PLUGINS_AFLAGS="${GCC_PLUGINS_AFLAGS}" kallsymso="" kallsyms_vmlinux=""
scripts/package/builddeb +1 -0
@@ -326,6 +326,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 tools/gcc -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 -)
tools/gcc/Makefile new file +17 -0
@@ -0,0 +1,17 @@ +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 -fno-exceptions -fasynchronous-unwind-tables -ggdb -Wno-narrowing -Wno-unused-variable +export HOST_EXTRACXXFLAGS +endif + +export GCCPLUGINS_DIR HOSTLIBS + +always := $($(HOSTLIBS)-y) + +cyc_complexity_plugin-objs := cyc_complexity_plugin.o
tools/gcc/gcc-common.h new file +794 -0
794 lines not on this page · raw message
tools/gcc/gcc-generate-gimple-pass.h new file +172 -0
172 lines not on this page · raw message
tools/gcc/gcc-generate-ipa-pass.h new file +286 -0
286 lines not on this page · raw message
tools/gcc/gcc-generate-rtl-pass.h new file +172 -0
172 lines not on this page · raw message
tools/gcc/gcc-generate-simple_ipa-pass.h new file +172 -0
172 lines not on this page · raw message
Signature · 1 line
2.4.1
PATCH v3 2/3 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 | 11 ++++++ scripts/Makefile.gcc-plugins | 6 +++- tools/gcc/Makefile | 2 ++ tools/gcc/cyc_complexity_plugin.c | 70 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tools/gcc/cyc_complexity_plugin.c
arch/Kconfig +11 -0
@@ -365,6 +365,17 @@ config HAVE_GCC_PLUGINS An arch should select this symbol if it supports building with gcc plugins. +config GCC_PLUGIN_CYC_COMPLEXITY + bool "Compute the cyclomatic complexity of a function" + depends on HAVE_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). endmenu # "GCC plugins"
scripts/Makefile.gcc-plugins +5 -1
@@ -4,7 +4,11 @@ else PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)") endif ifneq ($(PLUGINCC),) -export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS +ifdef CONFIG_GCC_PLUGIN_CYC_COMPLEXITY +GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS := -fplugin=$(objtree)/tools/gcc/cyc_complexity_plugin.so +endif +GCC_PLUGINS_CFLAGS := $(GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS) +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS GCC_PLUGIN_CYC_COMPLEXITY ifeq ($(KBUILD_EXTMOD),) gcc-plugins: $(Q)$(MAKE) $(build)=tools/gcc
tools/gcc/Makefile +2 -0
@@ -12,6 +12,8 @@ endif export GCCPLUGINS_DIR HOSTLIBS +$(HOSTLIBS)-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) := cyc_complexity_plugin.so + always := $($(HOSTLIBS)-y) cyc_complexity_plugin-objs := cyc_complexity_plugin.o
tools/gcc/cyc_complexity_plugin.c new file +70 -0
70 lines not on this page · raw message
Signature · 1 line
2.4.1
PATCH v3 3/3 Documentation for the GCC plugin infrastructure
In reply to Emese RevfyThis is the GCC infrastructure documentation about its operation, how to add and use a new plugin with an example.
--- Documentation/gcc-plugins.txt | 82 +++++++++++++++++++++++++++++++++++++++++++ arch/Kconfig | 2 ++ 2 files changed, 84 insertions(+) create mode 100644 Documentation/gcc-plugins.txt
Documentation/gcc-plugins.txt new file +82 -0
@@ -0,0 +1,82 @@ +GCC plugin infrastructure +========================= + + +1. Introduction +=============== + +GCC plugins are loadable modules that provide extra features to the +compiler [1]. They are useful for runtime instrumentation and static analysis. +We can analyse, change and add further code during compilation via +callbacks [2], GIMPLE [3], IPA [4] and RTL passes [5]. + +The GCC plugin infrastructure of the kernel supports all gcc versions from +4.5 to 6.0, building out-of-tree modules, cross-compilation and building in a +separate directory. + +Currently the GCC plugin infrastructure supports only the x86 architecture. + +This infrastructure was ported from grsecurity [6] and PaX [7]. + +-- +[1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html +[2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API +[3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html +[4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html +[5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html +[6] https://grsecurity.net/ +[7] https://pax.grsecurity.net/ + + +2. Files +======== + +$(src)/tools/gcc + This is the directory of the GCC plugins. + +$(src)/tools/gcc/gcc-common.h + This is a compatibility header for GCC plugins. + It should be always included instead of individual gcc headers. + +$(src)/scripts/gcc-plugin.sh + This script checks the availability of the included headers in + gcc-common.h and chooses the proper host compiler to build the plugins + (gcc-4.7 can be built by either gcc or g++). + +$(src)/tools/gcc/gcc-generate-gimple-pass.h +$(src)/tools/gcc/gcc-generate-ipa-pass.h +$(src)/tools/gcc/gcc-generate-simple_ipa-pass.h +$(src)/tools/gcc/gcc-generate-rtl-pass.h + These headers automatically generate the registration structures for + GIMPLE, SIMPLE_IPA, IPA and RTL passes. They support all gcc versions + from 4.5 to 6.0. + They should be preferred to creating the structures by hand. + + +3. Usage +======== + +You must install the gcc plugin headers for your gcc version, +e.g., on Ubuntu for gcc-4.9: + + apt-get install gcc-4.9-plugin-dev + +Enable a GCC plugin based feature in the kernel config: + + CONFIG_GCC_PLUGIN_CYC_COMPLEXITY = y + +To compile only the plugin(s): + + make gcc-plugins + +or just run the kernel make and compile the whole kernel with +the cyclomatic complexity GCC plugin. + + +4. How to add a new GCC plugin +============================== + +The GCC plugins are in $(src)/tools/gcc/. You can use a file or a directory +here. It must be added to $(src)/tools/gcc/Makefile, +$(src)/scripts/Makefile.gcc-plugins and $(src)/arch/Kconfig. +See the cyc_complexity_plugin.c (CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) GCC plugin.
arch/Kconfig +2 -0
@@ -377,6 +377,8 @@ config GCC_PLUGIN_CYC_COMPLEXITY N = the number of nodes P = the number of connected components (exit nodes). + See Documentation/gcc-plugins.txt for details. + endmenu # "GCC plugins" config HAVE_CC_STACKPROTECTOR
Signature · 1 line
2.4.1
Quoted 26 lines · Emese Revfy
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 architecture enables plugins.
This infrastructure was ported from grsecurity/PaX. It is a CII project supported by the Linux Foundation.
Emese Revfy (3):
GCC plugin infrastructure
Add Cyclomatic complexity plugin
Documentations of the GCC plugin infrastructreChanges from v2:
* Fixed incorrectly encoded characters
* Generate the GIMPLE, IPA,SIMPLE_IPAand 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)
Quoted 6 lines
Changes from v1:
* Move the gcc-plugins make target into a separateMakefilebecause 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
Oh, I totally missed a detail from v2 that still applies to v3:
You're missing your Signed-off-by lines in the patches.
Running each patch through scripts/checkpatch.pl produces a number of other warnings (I ignored all the C++isms), mostly around lines >80 characters, function definitions (missing void), and the suggestion to create yourself a MAINTAINTERS line for this directory.
-Kees
Quoted 22 lines
--- Documentation/dontdiff | 1 + Documentation/gcc-plugins.txt | 80 ++++ Makefile | 41 +- arch/Kconfig | 24 + arch/x86/Kconfig | 1 + init/Makefile | 3 + scripts/Makefile.build | 2 +- scripts/Makefile.clean | 3 +- scripts/Makefile.gcc-plugins | 30 ++ scripts/Makefile.host | 69 ++- scripts/gcc-plugin.sh | 51 ++ scripts/link-vmlinux.sh | 2 +- scripts/package/builddeb | 1 + tools/gcc/Makefile | 19 + tools/gcc/cyc_complexity_plugin.c | 70 +++ tools/gcc/gcc-common.h | 794 +++++++++++++++++++++++++++++++ tools/gcc/gcc-generate-gimple-pass.h | 172 +++++++ tools/gcc/gcc-generate-ipa-pass.h | 286 +++++++++++ tools/gcc/gcc-generate-rtl-pass.h | 172 +++++++ tools/gcc/gcc-generate-simple_ipa-pass.h | 172 +++++++ 20 files changed, 1978 insertions(+), 15 deletions(-)
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 2 lines · On Mon, 22 Feb 2016 16:20:40 -0800 Kees Cook <[email protected]> wrote:
characters, function definitions (missing void), and the suggestion to create yourself a MAINTAINTERS line for this directory.
I think the maintainer should be a more experienced kernel developer. Would you like to be the maintainer? :)
Signature · 1 line
Emese
Quoted 8 lines · Emese Revfy
Quoted 2 lines · On Mon, 22 Feb 2016 16:20:40 -0800 Kees Cook <[email protected]> wrote:
characters, function definitions (missing void), and the suggestion to create yourself a MAINTAINTERS line for this directory.
I think the maintainer should be a more experienced kernel developer. Would you like to be the maintainer? :)
I'm happy to help ("R:" entry), but I think you should be the maintainer ("M:" entry) so you're notified if anyone tries to make changes to the code.
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 3 lines · On Mon, 22 Feb 2016 16:20:40 -0800 Kees Cook <[email protected]> wrote:
Running each patch through
scripts/checkpatch.plproduces a number of other warnings (I ignored all the C++isms), mostly around lines >80 characters, function definitions (missing void), and the suggestion to
I fixed only those >80 line type warnings that didn't impair the readability of the code:
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next/commit/77c8e286fb
Signature · 1 line
Emese