Introduce GCC plugin infrastructure
Subsystems (3)
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
--- Documentation/dontdiff | 1 + Documentation/example_gcc_plugin.c | 103 +++++ Documentation/gcc-plugins.txt | 76 ++++ Makefile | 70 +++- arch/Kconfig | 26 ++ arch/x86/Kconfig | 1 + init/Makefile | 3 + scripts/Makefile.build | 2 +- scripts/Makefile.clean | 3 +- 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 | 120 ++++++ tools/gcc/gcc-common.h | 794 +++++++++++++++++++++++++++++++++++++ 16 files changed, 1326 insertions(+), 15 deletions(-)
PATCH 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.
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 | 66 +++- arch/Kconfig | 10 + arch/x86/Kconfig | 1 + init/Makefile | 3 + scripts/Makefile.build | 2 +- scripts/Makefile.clean | 3 +- scripts/Makefile.host | 69 +++- scripts/gcc-plugin.sh | 51 +++ scripts/link-vmlinux.sh | 2 +- scripts/package/builddeb | 1 + tools/gcc/Makefile | 15 + tools/gcc/gcc-common.h | 794 +++++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 1003 insertions(+), 15 deletions(-) create mode 100644 scripts/gcc-plugin.sh create mode 100644 tools/gcc/Makefile create mode 100644 tools/gcc/gcc-common.h
Documentation/dontdiff +1 -0
@@ -3,6 +3,7 @@ *.bc *.bin *.bz2 +*.c.[012]*.* *.cis *.cpio *.csp
Makefile +55 -11
146 lines not on this page · raw message
arch/Kconfig +10 -0
@@ -357,6 +357,16 @@ 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
@@ -156,6 +156,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.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
@@ -183,7 +183,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 +15 -0
@@ -0,0 +1,15 @@ +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)
tools/gcc/gcc-common.h new file +794 -0
794 lines not on this page · raw message
[kernel-hardening] Re: [PATCH 1/3] GCC plugin infrastructure
Dne 7.2.2016 v 22:28 Emese Revfy napsal(a):
Quoted 16 lines
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_versionvariable,plugin-version.h)
Quoted 8 lines
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 intools/gcc/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 changelog is missing an explanation as to why this needs to be part of the kernel build system. To me it looks like building the kernel with a modified build system and non-default compiler flags, which can be achieved by doing make CC=my-gcc-wrapper or somesuch. But I'd love to be corrected.
Michal
[kernel-hardening] Re: [PATCH 1/3] GCC plugin infrastructure
Quoted 23 lines · On Mon, 8 Feb 2016 21:28:13 +0100 Michal Marek <[email protected]> wrote:
Dne 7.2.2016 v 22:28 Emese Revfy napsal(a):
Quoted 22 lines
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_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 intools/gcc/gcc-common.h.
Quoted 8 lines
Quoted 2 lines
The
gcc-common.hheader contains frequently included headers for GCC plugins and it has a compatibility layer for the supported gcc versions.The changelog is missing an explanation as to why this needs to be part of the kernel build system. To me it looks like building the kernel with a modified build system and non-default compiler flags, which can be achieved by doing make CC=my-gcc-wrapper or somesuch. But I'd love to be corrected.
These compiler options compile the gcc plugins not the kernel. The new gcc option used for building the kernel is the -fplugin option.
Signature · 1 line
Emese
PATCH 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).
--- Makefile | 6 +- arch/Kconfig | 16 +++++ tools/gcc/Makefile | 4 ++ tools/gcc/cyc_complexity_plugin.c | 120 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 tools/gcc/cyc_complexity_plugin.c
Makefile +5 -1
@@ -628,7 +628,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 -DGCC_PLUGIN_CYC_COMPLEXITY +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
arch/Kconfig +16 -0
@@ -365,6 +365,22 @@ config HAVE_GCC_PLUGINS An arch should select this symbol if it supports building with gcc plugins. +if HAVE_GCC_PLUGINS + +config GCC_PLUGIN_CYC_COMPLEXITY + bool "Compute the cyclomatic complexity of a function" + 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). + + See Documentation/gcc-plugins.txt for details. + +endif # HAVE_GCC_PLUGINS endmenu # "GCC plugins" config HAVE_CC_STACKPROTECTOR
tools/gcc/Makefile +4 -0
@@ -12,4 +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 +120 -0
120 lines not on this page · raw message
[kernel-hardening] Re: [PATCH 2/3] Add Cyclomatic complexity GCC plugin
Quoted 23 lines · Emese Revfy
Add 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).--- Makefile | 6 +- arch/Kconfig | 16 +++++ tools/gcc/Makefile | 4 ++ tools/gcc/cyc_complexity_plugin.c | 120 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 tools/gcc/cyc_complexity_plugin.cMakefile +0
-0
Makefile else · line 635
Quoted 9 lines
Makefile +5
-1@@ -628,7 +628,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 -DGCC_PLUGIN_CYC_COMPLEXITY +endif +GCC_PLUGINS_CFLAGS := $(GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS) +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS GCC_PLUGIN_CYC_COMPLEXITY
Hm, when the number of plugins grow, I think it'll be somewhat ugly having all this in the main makefile. Can't all the plugin-related stuff live in a separate makefile? The same goes for the Kconfig part.
Also, I think the compile command lines are already complicated enough (I sometimes use V=1 to see what's going on), so I think it's better that the plugins that need to make themselves known do so "from within" gcc; something like
static void define_feature_macro(void __unused *event_data, void __unused *data)
{
cpp_define(parse_in, "HAVE_ATTRIBUTE_FOOBAR");
}...
register_callback(plugin_name, PLUGIN_START_UNIT, &define_feature_macro, NULL);
Quoted 48 lines
Makefile +48
-0+ +#include "gcc-common.h" + +int plugin_is_GPL_compatible; + +static struct plugin_info cyc_complexity_plugin_info = { + .version = "20150523", + .help = "Cyclomatic Complexity\n", +}; + +static unsigned int handle_function(void) +{ + int complexity; + expanded_location xloc; + + // M = E - N + 2P + complexity = n_edges_for_fn(cfun) - n_basic_blocks_for_fn(cfun) + 2; + + xloc = expand_location(DECL_SOURCE_LOCATION(current_function_decl)); + fprintf(stderr, "Cyclomatic Complexity %d %s:%s\n", complexity, xloc.file, DECL_NAME_POINTER(current_function_decl)); + + return 0; +} + +#if BUILDING_GCC_VERSION >= 4009 +namespace { +static const struct pass_data cyc_complexity_pass_data = { +#else +static struct gimple_opt_pass cyc_complexity_pass = { + .pass = { +#endif + .type = GIMPLE_PASS, + .name = "cyc_complexity", +#if BUILDING_GCC_VERSION >= 4008 + .optinfo_flags = OPTGROUP_NONE, +#endif +#if BUILDING_GCC_VERSION >= 5000 +#elif BUILDING_GCC_VERSION >= 4009 + .has_gate = false, + .has_execute = true, +#else + .gate = NULL, + .execute = handle_function, + .sub = NULL, + .next = NULL, + .static_pass_number = 0, +#endif + .tv_id = TV_NONE,
Makefile
Quoted 10 lines
Makefile +10
-0+ .properties_required = 0, + .properties_provided = 0, + .properties_destroyed = 0, + .todo_flags_start = 0, + .todo_flags_finish = TODO_dump_func +#if BUILDING_GCC_VERSION < 4009 + } +#endif +}; +
This is really yucky. Is there any way a mere mortal could figure out how to write this? Or could the compatibility layer provide some utility macros that would take care of the boilerplate?
Rasmus
[kernel-hardening] Re: [PATCH 2/3] Add Cyclomatic complexity GCC plugin
Quoted 2 lines · On Mon, 08 Feb 2016 00:05:15 +0100 Rasmus Villemoes <[email protected]>…
having all this in the main makefile. Can't all the plugin-related stuff live in a separate makefile? The same goes for the
Kconfigpart.
I will do the Makefile separation but I'm not sure about a separate arch/Kconfig.gcc-plugins as arch/Kconfig seems to be a general collection of arch/toolchain dependent features so I would rather stick to it until there is a general decision to break it up into smaller pieces.
Quoted 9 lines
Also, I think the compile command lines are already complicated enough (I sometimes use V=1 to see what's going on), so I think it's better that the plugins that need to make themselves known do so "from within" gcc; something like
static void
define_feature_macro(void __unused *event_data, void __unused *data){ cpp_define(parse_in, "HAVE_ATTRIBUTE_FOOBAR"); }
I think it is better in the Makefile because -DPLUGIN doesn't matter much in the compile command compared to -fplugin and -fplugin-arg. An exmaple compile command with more plugins looks like this:
gcc -Wp,-MD,fs/.exec.o.d -nostdinc -isystem /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include -I./arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -maccumulate-outgoing-args -ffreestanding -DCONFIG_X86_X32_ABI -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -fno-delete-null-pointer-checks -Os -Wno-maybe-uninitialized --param=allow-store-data-races=0 -fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining -Wframe-larger-than=2048 -fno-stack-protector -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments -fno-inline-functions-called-once -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -Werror=date-time -DCC_HAVE_ASM_GOTO -fplugin=./tools/gcc/stackleak_plugin.so -DSTACKLEAK_PLUGIN -fplugin-arg-stackleak_plugin-track-lowest-sp=100 -fplugin=./tools/gcc/colorize_plugin.so -fplugin=./tools/gcc/size_overflow_plugin/size_overflow_plugin.so -DSIZE_OVERFLOW_PLUGIN -fplugin=./tools/gcc/latent_entropy_plugin.so -DLATENT_ENTROPY_PLUGIN -fplugin=./tools/gcc/structleak_plugin.so -DSTRUCTLEAK_PLUGIN -fplugin=./tools/gcc/initify_plugin.so -DINITIFY_PLUGIN -fplugin=./tools/gcc/randomize_layout_plugin.so
-DRANDSTRUCT_PLUGIN -fplugin-arg-randomize_layout_plugin-performance-mode -D"KBUILD_STR(s)=#s"
-D"KBUILD_BASENAME=KBUILD_STR(exec)" -D"KBUILD_MODNAME=KBUILD_STR(exec)" -c -o fs/.tmp_exec.o fs/exec.c
Quoted 30 lines
Quoted 30 lines
tools/gcc/cyc_complexity_plugin.c new file +30
-0+#if BUILDING_GCC_VERSION >=4009 +namespace { +static const struct pass_data cyc_complexity_pass_data = { +#else +static struct gimple_opt_pass cyc_complexity_pass = { + .pass = { +#endif + .type =GIMPLE_PASS , + .name ="cyc_complexity" , +#if BUILDING_GCC_VERSION >= 4008 + .optinfo_flags =OPTGROUP_NONE , +#endif +#if BUILDING_GCC_VERSION >= 5000 +#elif BUILDING_GCC_VERSION >= 4009 + .has_gate = false, + .has_execute = true, +#else + .gate =NULL , + .execute = handle_function, + .sub =NULL , + .next =NULL , + .static_pass_number =0 , +#endif + .tv_id =TV_NONE , + .properties_required =0 , + .properties_provided =0 , + .properties_destroyed =0 , + .todo_flags_start =0 , + .todo_flags_finish = TODO_dump_func +#if BUILDING_GCC_VERSION < 4009
Quoted 8 lines
tools/gcc/cyc_complexity_plugin.c line 77
Quoted 4 lines
tools/gcc/cyc_complexity_plugin.c new file +4
-0+ } +#endif +}; +This is really yucky. Is there any way a mere mortal could figure out how to write this? Or could the compatibility layer provide some utility macros that would take care of the boilerplate?
Yes, it isn't too nice but if I put them into macros (for GIMPLE, IPA and RTL) then they would have a lot of parameters because the user can and must define too many values (especially for an IPA pass which has even more fields).
The compatibility with all gcc versions makes it further complicated so I think the end result would be uglier than it is now.
Signature · 1 line
Emese
[kernel-hardening] Re: [PATCH 2/3] Add Cyclomatic complexity GCC plugin
In reply to Emese RevfyQuoted 40 lines · Emese Revfy
Add 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).--- Makefile | 6 +- arch/Kconfig | 16 +++++ tools/gcc/Makefile | 4 ++ tools/gcc/cyc_complexity_plugin.c | 120 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 tools/gcc/cyc_complexity_plugin.cMakefile +5
-1@@ -628,7 +628,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 -DGCC_PLUGIN_CYC_COMPLEXITY +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/gccarch/Kconfig +0
-0
arch/Kconfig config HAVE_GCC_PLUGINS · line 372
Quoted 8 lines
arch/Kconfig +5
-0@@ -365,6 +365,22 @@ config HAVE_GCC_PLUGINS An arch should select this symbol if it supports building with gcc plugins. +if HAVE_GCC_PLUGINS + +config GCC_PLUGIN_CYC_COMPLEXITY + bool "Compute the cyclomatic complexity of a function" + help
Rather than the "if HAVE_GCC_PLUGINS" / endif section, I would add "depends HAVE_GCC_PLUGINS" to the config, which makes things more discoverable.
arch/Kconfig config HAVE_GCC_PLUGINS · line 381
Quoted 9 lines
arch/Kconfig +9
-0+ 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). + + See Documentation/gcc-plugins.txt for details.
Is this plugin documented there? I don't see a chunk for that in this patch.
Quoted 145 lines
arch/Kconfig +2
-0+ +endif # HAVE_GCC_PLUGINS endmenu # "GCC plugins" config HAVE_CC_STACKPROTECTORtools/gcc/Makefile +4
-0@@ -12,4 +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.otools/gcc/cyc_complexity_plugin.c new file +120
-0@@ -0,0 +1,120 @@ +/* +* Copyright 2011-2016 by Emese Revfy <[email protected]> +* Licensed under the GPL v2, or (at your option) v3 +* +* Homepage: +* https://github.com/ephox-gcc-plugins/cyclomatic_complexity +* +* http://en.wikipedia.org/wiki/Cyclomatic_complexity +* The complexity M is then defined as: +* M = E - N + 2P +* where +* +* E = the number of edges of the graph +* N = the number of nodes of the graph +* P = the number of connected components (exit nodes). +* +* Usage (4.5 - 5): +* $ make clean; make run +*/ + +#include "gcc-common.h" + +int plugin_is_GPL_compatible; + +static struct plugin_info cyc_complexity_plugin_info = { + .version ="20150523" , + .help ="Cyclomatic Complexity\n" , +}; + +static unsigned int handle_function (void ) +{ +int complexity; +expanded_location xloc; + +// M = E - N + 2P + complexity =n_edges_for_fn (cfun) -n_basic_blocks_for_fn (cfun) +2 ; + + xloc =expand_location (DECL_SOURCE_LOCATION (current_function_decl)); +fprintf (stderr,"Cyclomatic Complexity %d %s:%s\n" , complexity, xloc.file,DECL_NAME_POINTER (current_function_decl)); + +return 0 ; +} + +#if BUILDING_GCC_VERSION >=4009 +namespace { +static const struct pass_data cyc_complexity_pass_data = { +#else +static struct gimple_opt_pass cyc_complexity_pass = { + .pass = { +#endif + .type =GIMPLE_PASS , + .name ="cyc_complexity" , +#if BUILDING_GCC_VERSION >= 4008 + .optinfo_flags =OPTGROUP_NONE , +#endif +#if BUILDING_GCC_VERSION >= 5000 +#elif BUILDING_GCC_VERSION >= 4009 + .has_gate = false, + .has_execute = true, +#else + .gate =NULL , + .execute = handle_function, + .sub =NULL , + .next =NULL , + .static_pass_number =0 , +#endif + .tv_id =TV_NONE , + .properties_required =0 , + .properties_provided =0 , + .properties_destroyed =0 , + .todo_flags_start =0 , + .todo_flags_finish = TODO_dump_func +#if BUILDING_GCC_VERSION < 4009 + } +#endif +}; + +#if BUILDING_GCC_VERSION >=4009 +class cyc_complexity_pass : public gimple_opt_pass { +public: + cyc_complexity_pass() : gimple_opt_pass(cyc_complexity_pass_data, g) {} +#if BUILDING_GCC_VERSION >=5000 + virtual unsigned int execute(function *) { return handle_function(); } +#else +unsigned int execute () {return handle_function (); } +#endif +}; +} + +static struct opt_pass *make_cyc_complexity_pass (void ) +{ + return new cyc_complexity_pass(); +} +#else +static struct opt_pass *make_cyc_complexity_pass (void ) +{ +return &cyc_complexity_pass.pass; +} +#endif + +int plugin_init (struct plugin_name_args *plugin_info,struct plugin_gcc_version *version) +{ +const char *const plugin_name = plugin_info->base_name; +struct register_pass_info cyc_complexity_pass_info; + + cyc_complexity_pass_info.pass =make_cyc_complexity_pass (); + cyc_complexity_pass_info.reference_pass_name ="ssa" ; + cyc_complexity_pass_info.ref_pass_instance_number =1 ; + cyc_complexity_pass_info.pos_op =PASS_POS_INSERT_AFTER ; + +if (!plugin_default_version_check (version, &gcc_version)) { +error (G_ ("incompatible gcc/plugin versions" )); +return 1 ; + } + +register_callback (plugin_name,PLUGIN_INFO ,NULL , &cyc_complexity_plugin_info); +register_callback (plugin_name,PLUGIN_PASS_MANAGER_SETUP ,NULL , &cyc_complexity_pass_info); + +return 0 ; +}
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
[kernel-hardening] Re: [PATCH 2/3] Add Cyclomatic complexity GCC plugin
Quoted 11 lines · On Mon, 8 Feb 2016 20:23:12 -0800 Kees Cook <[email protected]> wrote:
arch/Kconfig config HAVE_GCC_PLUGINS · line 381
Quoted 9 lines
arch/Kconfig +9
-0+ 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). + + See Documentation/gcc-plugins.txt for details.Is this plugin documented there? I don't see a chunk for that in this patch.
No yet, please see my other response.
--
Emese
PATCH 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/example_gcc_plugin.c | 103 +++++++++++++++++++++++++++++++++++++ Documentation/gcc-plugins.txt | 76 +++++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 Documentation/example_gcc_plugin.c create mode 100644 Documentation/gcc-plugins.txt
Documentation/example_gcc_plugin.c new file +103 -0
@@ -0,0 +1,103 @@ +/* +* Copyright 2011-2016 by Emese Revfy <[email protected]> +* Licensed under the GPL v2, or (at your option) v3 +*/ + +#include "gcc-common.h" + +int plugin_is_GPL_compatible; + +static struct plugin_info example_plugin_info = { + .version ="0" , + .help ="example plugin\n" , +}; + +static unsigned int handle_function (void ) +{ +gimple_stmt_iterator gsi; +basic_block bb; + + FOR_ALL_BB_FN(bb, cfun) { +for (gsi =gsi_start_bb (bb); !gsi_end_p (gsi);gsi_next (&gsi)) +debug_gimple_stmt (gsi_stmt (gsi)); + } +return 0 ; +} + +#if BUILDING_GCC_VERSION >=4009 +namespace { +static const struct pass_data example_plugin_pass_data = { +#else +static struct gimple_opt_pass example_plugin_pass = { + .pass = { +#endif + .type =GIMPLE_PASS , + .name ="example_plugin" , +#if BUILDING_GCC_VERSION >= 4008 + .optinfo_flags =OPTGROUP_NONE , +#endif +#if BUILDING_GCC_VERSION >= 5000 +#elif BUILDING_GCC_VERSION >= 4009 + .has_gate = false, + .has_execute = true, +#else + .gate =NULL , + .execute = handle_function, + .sub =NULL , + .next =NULL , + .static_pass_number =0 , +#endif + .tv_id =TV_NONE , + .properties_required =0 , + .properties_provided =0 , + .properties_destroyed =0 , + .todo_flags_start =0 , + .todo_flags_finish = TODO_dump_func +#if BUILDING_GCC_VERSION < 4009 + } +#endif +}; + +#if BUILDING_GCC_VERSION >=4009 +class example_plugin_pass : public gimple_opt_pass { +public: + example_plugin_pass() : gimple_opt_pass(example_plugin_pass_data, g) {} +#if BUILDING_GCC_VERSION >=5000 + virtual unsigned int execute(function *) { return handle_function(); } +#else +unsigned int execute () {return handle_function (); } +#endif +}; +} + +static opt_pass *make_example_plugin_pass (void ) +{ + return new example_plugin_pass(); +} +#else +static struct opt_pass *make_example_plugin_pass (void ) +{ +return &example_plugin_pass.pass; +} +#endif + +int plugin_init (struct plugin_name_args *plugin_info,struct plugin_gcc_version *version) +{ +const char *const plugin_name = plugin_info->base_name; +struct register_pass_info example_plugin_pass_info; + + example_plugin_pass_info.pass =make_example_plugin_pass (); + example_plugin_pass_info.reference_pass_name ="ssa" ; + example_plugin_pass_info.ref_pass_instance_number =1 ; + example_plugin_pass_info.pos_op =PASS_POS_INSERT_AFTER ; + +if (!plugin_default_version_check (version, &gcc_version)) { +error (G_ ("incompatible gcc/plugin versions" )); +return 1 ; + } + +register_callback (plugin_name,PLUGIN_INFO ,NULL , &example_plugin_info); +register_callback (plugin_name,PLUGIN_PASS_MANAGER_SETUP ,NULL , &example_plugin_pass_info); + +return 0 ; +}
Documentation/gcc-plugins.txt new file +76 -0
76 lines not on this page · raw message
[kernel-hardening] Re: [PATCH 3/3] Documentation for the GCC plugin infrastructure
Quoted 5 lines · Emese Revfy
This is the GCC infrastructure documentation about its operation, how to add and use a new plugin with an example.
--- Documentation/example_gcc_plugin.c | 103 +++++++++++++++++++++++++++++++++++++
Perhaps this example should live in samples/gcc_plugin/ ?
Quoted 104 lines
Documentation/gcc-plugins.txt | 76 +++++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 Documentation/example_gcc_plugin.c create mode 100644 Documentation/gcc-plugins.txtDocumentation/example_gcc_plugin.c new file +93
-0@@ -0,0 +1,103 @@ +/* +* Copyright 2011-2016 by Emese Revfy <[email protected]> +* Licensed under the GPL v2, or (at your option) v3 +*/ + +#include "gcc-common.h" + +int plugin_is_GPL_compatible; + +static struct plugin_info example_plugin_info = { + .version ="0" , + .help ="example plugin\n" , +}; + +static unsigned int handle_function (void ) +{ +gimple_stmt_iterator gsi; +basic_block bb; + + FOR_ALL_BB_FN(bb, cfun) { +for (gsi =gsi_start_bb (bb); !gsi_end_p (gsi);gsi_next (&gsi)) +debug_gimple_stmt (gsi_stmt (gsi)); + } +return 0 ; +} + +#if BUILDING_GCC_VERSION >=4009 +namespace { +static const struct pass_data example_plugin_pass_data = { +#else +static struct gimple_opt_pass example_plugin_pass = { + .pass = { +#endif + .type =GIMPLE_PASS , + .name ="example_plugin" , +#if BUILDING_GCC_VERSION >= 4008 + .optinfo_flags =OPTGROUP_NONE , +#endif +#if BUILDING_GCC_VERSION >= 5000 +#elif BUILDING_GCC_VERSION >= 4009 + .has_gate = false, + .has_execute = true, +#else + .gate =NULL , + .execute = handle_function, + .sub =NULL , + .next =NULL , + .static_pass_number =0 , +#endif + .tv_id =TV_NONE , + .properties_required =0 , + .properties_provided =0 , + .properties_destroyed =0 , + .todo_flags_start =0 , + .todo_flags_finish = TODO_dump_func +#if BUILDING_GCC_VERSION < 4009 + } +#endif +}; + +#if BUILDING_GCC_VERSION >=4009 +class example_plugin_pass : public gimple_opt_pass { +public: + example_plugin_pass() : gimple_opt_pass(example_plugin_pass_data, g) {} +#if BUILDING_GCC_VERSION >=5000 + virtual unsigned int execute(function *) { return handle_function(); } +#else +unsigned int execute () {return handle_function (); } +#endif +}; +} + +static opt_pass *make_example_plugin_pass (void ) +{ + return new example_plugin_pass(); +} +#else +static struct opt_pass *make_example_plugin_pass (void ) +{ +return &example_plugin_pass.pass; +} +#endif + +int plugin_init (struct plugin_name_args *plugin_info,struct plugin_gcc_version *version) +{ +const char *const plugin_name = plugin_info->base_name; +struct register_pass_info example_plugin_pass_info; + + example_plugin_pass_info.pass =make_example_plugin_pass (); + example_plugin_pass_info.reference_pass_name ="ssa" ; + example_plugin_pass_info.ref_pass_instance_number =1 ; + example_plugin_pass_info.pos_op =PASS_POS_INSERT_AFTER ; +
Documentation/example_gcc_plugin.c line 103
Quoted 10 lines
Documentation/example_gcc_plugin.c new file +10
-0+if (!plugin_default_version_check (version, &gcc_version)) { +error (G_ ("incompatible gcc/plugin versions" )); +return 1 ; + } + +register_callback (plugin_name,PLUGIN_INFO ,NULL , &example_plugin_info); +register_callback (plugin_name,PLUGIN_PASS_MANAGER_SETUP ,NULL , &example_plugin_pass_info); + +return 0 ; +}
For an example, I'd expect verbose comments. :) Imagine someone reading it who knows very little about plugins or compiler internals.
Quoted 72 lines
Documentation/gcc-plugins.txt new file +66
-0@@ -0,0 +1,76 @@ +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++). + + +3. Usage +======== + +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)/Makefile and
Documentation/gcc-plugins.txt line 76
Quoted 10 lines
Documentation/gcc-plugins.txt new file +10
-0+$(src)/arch/Kconfig. +See the cyc_complexity_plugin.c (CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) GCC plugin. + + +5. Example GCC plugin +===================== + +You can find an example plugin under $(src)/Documentation/example_gcc_plugin.c . +This plugin has a GIMPLE pass that is inserted after the ssa GCC pass. +It prints out all the GIMPLE statements in a translation unit.
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
[kernel-hardening] Re: [PATCH 3/3] Documentation for the GCC plugin infrastructure
Quoted 8 lines · On Mon, 8 Feb 2016 20:27:31 -0800 Kees Cook <[email protected]> wrote:
Quoted 5 lines · Emese Revfy
This is the GCC infrastructure documentation about its operation, how to add and use a new plugin with an example.
--- Documentation/example_gcc_plugin.c | 103 +++++++++++++++++++++++++++++++++++++Perhaps this example should live in
samples/gcc_plugin/?
Perhaps the example plugin is unnecessary. I should use the cyclomatic complexity plugin because I already wrote about it in the sections "Usage" and "How to add a new GCC plugin" and they differ by only two lines.
Quoted 2 lines
For an example, I'd expect verbose comments. :) Imagine someone reading it who knows very little about plugins or compiler internals.
I think gcc has very good documentation about plugins, I reference them from gcc-plugins.txt, you can see it below. If you think it isn't enough tell me please what you would like to see as additional documentation.
Quoted 24 lines
Quoted 24 lines
+1. Introduction
+===============Documentation/gcc-plugins.txt new file +22
-0+ +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/
Signature · 1 line
Emese