Introduce GCC plugin infrastructure
Subsystems (10)
v9 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. Based on work created by the PaX Team. 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 v8:
* Use warnings instead of errors because of allmodconfig/allyesconfig builds with old gcc versions
(Suggested-by: Michal Marek <[email protected]>)
* Order HAVE_GCC_PLUGINS alphabetically
* Moved exported variables from the top level Makefile to scripts/Makefile.gcc-plugins
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 | 43 ++ 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, 2200 insertions(+), 17 deletions(-)
PATCH v9 1/4 Shared library support
Infrastructure for building independent shared library targets.
Based on work created by the PaX Team.
--- 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 v9 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.
Based on work created by the PaX Team.
--- .gitignore | 1 + Documentation/dontdiff | 1 + Documentation/gcc-plugins.txt | 87 +++ MAINTAINERS | 9 + Makefile | 14 +- 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 | 23 + 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 +12 -2
@@ -552,7 +552,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 +631,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 +1035,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 +1516,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
@@ -54,6 +54,7 @@ config ARM select HAVE_FTRACE_MCOUNT_RECORD if (!XIP_KERNEL) select HAVE_FUNCTION_GRAPH_TRACER if (!THUMB2_KERNEL) select HAVE_FUNCTION_TRACER if (!XIP_KERNEL) + select HAVE_GCC_PLUGINS select HAVE_GENERIC_DMA_COHERENT select HAVE_HW_BREAKPOINT if (PERF_EVENTS && (CPU_V6 || CPU_V6K || CPU_V7)) select HAVE_IDE if PCI || ISA || PCMCIA
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
@@ -111,6 +111,7 @@ config X86 select HAVE_FUNCTION_GRAPH_FP_TEST select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FUNCTION_TRACER + select HAVE_GCC_PLUGINS select HAVE_GENERIC_DMA_COHERENT if X86_32 select HAVE_HW_BREAKPOINT select HAVE_IDE
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 +23 -0
@@ -0,0 +1,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)") + + GCC_PLUGINS_CFLAGS := $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) + + export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN + + 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)") + $(warning warning: your gcc installation does not support plugins, perhaps the necessary headers are missing?) + else + $(warning warning: 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 v9 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)) export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN
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 v9 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 | 10 +-- arch/Kconfig | 9 +++ arch/x86/purgatory/Makefile | 2 + lib/Kconfig.debug | 2 + scripts/Makefile.gcc-plugins | 21 +++++- 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 +1 -9
@@ -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.
@@ -691,14 +691,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 +20 -1
@@ -2,10 +2,26 @@ 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 + $(warning warning: 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)) - export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN + export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN SANCOV_PLUGIN ifeq ($(PLUGINCC),) ifneq ($(GCC_PLUGINS_CFLAGS),)
@@ -16,6 +32,9 @@ ifdef CONFIG_GCC_PLUGINS $(warning warning: 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
Quoted 10 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, arm, arm64 and uml architectures enable plugins.
Quoted 8 lines
This infrastructure was ported from grsecurity/PaX. Based on work created by the PaX Team. 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
Michal, once -rc1 is out, can you carry this for -next? I'm happy to use my tree for it, if you want me to do it.
-Kees
Quoted 126 lines
Changes from v8:
* Use warnings instead of errors because of allmodconfig/allyesconfig builds with old gcc versions
(Suggested-by: Michal Marek <[email protected]>)
* OrderHAVE_GCC_PLUGINSalphabetically
* Moved exported variables from the top levelMakefiletoscripts/Makefile.gcc-pluginsChanges 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]>)
* Addgcc-plugin.shtoMAINTAINERS
(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 UMLChanges from v6:
* Disable the sancov plugin wheneverKCOV_INSTRUMENTis 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 aflagsChanges 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 ofgcc-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]>)
* SimplifyMakefileby using addprefix (GCC plugin infrastructure) (Suggested-by: Masahiro Yamada <[email protected]>)
* Moved the gcc plugins fromtools/toscripts/(GCC plugin infrastructure) (Suggested-by: Masahiro Yamada <[email protected]>)
* Removed plugins fromKBUILD_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
* Fixedgcc-common.hfor 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 rungcc_plugin.shwhen theGCC_PLUGINSoption is disabled or unsupported (Reported-by: Fengguang Wu <[email protected]>)Changes 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)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--- .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 | 43 ++ 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, 2200 insertions(+), 17 deletions(-)
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 14 lines · Kees Cook
Quoted 13 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, arm, arm64 and uml architectures enable plugins.This infrastructure was ported from grsecurity/PaX. Based on work created by the PaX Team. It is a CII project supported by the Linux Foundation.
Quoted 7 lines
Quoted 5 lines
Emese Revfy (4):
Shared library support
GCC plugin infrastructure
Add Cyclomatic complexity plugin
Add sancov pluginMichal, once -rc1 is out, can you carry this for -next?
Yes.
Michal
Kees Cook
Acked-byQuoted 21 lines · Michal Marek
Quoted 20 lines · Kees Cook
Quoted 18 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, arm, arm64 and uml architectures enable plugins.This infrastructure was ported from grsecurity/PaX. Based on work created by the PaX Team. 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
Quoted 3 lines
Quoted 1 line
Michal, once -rc1 is out, can you carry this for -next?
Yes.
Awesome! Please consider it:
If it's any help, I have it in my tree here:
http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=kspp/gcc-plugins/infrastructure
Thanks!
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Dne 25.5.2016 v 19:12 Kees Cook napsal(a):
Quoted 22 lines · Michal Marek
Quoted 21 lines · Kees Cook
Quoted 20 lines · Emese Revfy
Quoted 18 lines
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. Based on work created by the PaX Team. 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
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Michal, once -rc1 is out, can you carry this for -next?
Yes.
Awesome! Please consider it:
Acked-by Kees Cook <[email protected]>
I applied the series to kbuild.git#kbuild now, sorry for the delay.
Michal
Quoted 25 lines · Michal Marek
Dne 25.5.2016 v 19:12 Kees Cook napsal(a):
Quoted 24 lines · Michal Marek
Quoted 23 lines · Kees Cook
Quoted 21 lines · Emese Revfy
Quoted 10 lines
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.Quoted 8 lines
This infrastructure was ported from grsecurity/PaX. Based on work created by the PaX Team. 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 pluginMichal, once -rc1 is out, can you carry this for -next?
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Yes.
Awesome! Please consider it:
Acked-by Kees Cook <[email protected]>I applied the series to kbuild.git#kbuild now, sorry for the delay.
Awesome! Thank you very much. I'll prepare the latent entropy plugin for -next now too.
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 5 lines · On Tue, 7 Jun 2016 13:59:53 -0700 Kees Cook <[email protected]> wrote:
Quoted 2 lines
I applied the series to kbuild.git#kbuild now, sorry for the delay.
Awesome! Thank you very much. I'll prepare the latent entropy plugin for -next now too.
I'll send a new patch series this week because I'm developing a small feature and I'm fixing the handling of tail calls.
Signature · 1 line
Emese
Quoted 11 lines · Emese Revfy
Quoted 5 lines · On Tue, 7 Jun 2016 13:59:53 -0700 Kees Cook <[email protected]> wrote:
Quoted 2 lines
I applied the series to kbuild.git#kbuild now, sorry for the delay.
Awesome! Thank you very much. I'll prepare the latent entropy plugin for -next now too.
I'll send a new patch series this week because I'm developing a small feature and I'm fixing the handling of tail calls.
Depending on timing, it may be best to send a delta patch from kbuild.git#kbuild. I assume that would be easier for Michal to carry.
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Dne 7.6.2016 v 23:58 Kees Cook napsal(a):
Quoted 7 lines · Emese Revfy
Quoted 6 lines
Quoted 3 lines · On Tue, 7 Jun 2016 13:59:53 -0700 Kees Cook <[email protected]> wrote:
Quoted 2 lines
I applied the series to kbuild.git#kbuild now, sorry for the delay.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Awesome! Thank you very much. I'll prepare the latent entropy plugin for -next now too.
I'll send a new patch series this week because I'm developing a small feature and I'm fixing the handling of tail calls.
Depending on timing, it may be best to send a delta patch from kbuild.git#kbuild. I assume that would be easier for Michal to carry.
Right.
Michal
Re: [PATCH v9 3/4] Add Cyclomatic complexity GCC plugin
In reply to Emese RevfyQuoted 10 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).
This has some weird interaction with ftrace. On x86
1) make mrproper
2) make defconfig
3) enable GCC_PLUGINS, GCC_PLUGIN_CYC_COMPLEXITY
4) enable FUNCTION_TRACER (it will select other options as well)
5) make && make modules
MODPOST 18 modules
ERROR: "__fentry__" [net/netfilter/xt_nat.ko] undefined!
ERROR: "__fentry__" [net/netfilter/xt_mark.ko] undefined!
ERROR: "__fentry__" [net/netfilter/xt_addrtype.ko] undefined!
ERROR: "__fentry__" [net/netfilter/xt_LOG.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_nat_sip.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_nat_irc.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_nat_ftp.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_nat.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_log_common.ko] undefined!
ERROR: "__fentry__" [net/ipv6/netfilter/nf_log_ipv6.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/nf_nat_masquerade_ipv4.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/nf_nat_ipv4.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/nf_log_ipv4.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/nf_log_arp.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/iptable_nat.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/ipt_MASQUERADE.ko] undefined!
ERROR: "__fentry__" [fs/efivarfs/efivarfs.ko] undefined!
ERROR: "__fentry__" [drivers/thermal/x86_pkg_temp_thermal.ko] undefined! scripts/Makefile.modpost:91: recipe for target '__modpost' failed make[1]: *** [__modpost] Error 1
Verified with gcc-5.3.1 and gcc-6.1.1. You need to do a clean build with 'make mrproper' first to see this. Subsequent builds will succeed oddly enough. I suspect this
Thanks,
Laura
Quoted 134 lines
Signed-off-by Emese Revfy <[email protected]>--- 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.carch/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 helpscripts/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)) export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINscripts/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 += *.soscripts/gcc-plugins/cyc_complexity_plugin.c new file +73
-0@@ -0,0 +1,73 @@ +/* +* 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 ="20160225" , + .help ="Cyclomatic Complexity\n" , +}; + +static unsigned int cyc_complexity_execute (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 ; +} + +#define PASS_NAME cyc_complexity + +#define NO_GATE +#define TODO_FLAGS_FINISH TODO_dump_func + +#include "gcc-generate-gimple-pass.h" + +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 ; +}
Re: [PATCH v9 3/4] Add Cyclomatic complexity GCC plugin
Quoted 30 lines · On Tue, 14 Jun 2016 12:29:59 -0700 Laura Abbott <[email protected]> wrote:
This has some weird interaction with ftrace. On x86
1) make mrproper
2) make defconfig
3) enableGCC_PLUGINS,GCC_PLUGIN_CYC_COMPLEXITY
4) enableFUNCTION_TRACER(it will select other options as well)
5) make && make modulesMODPOST 18 modules
ERROR: "__fentry__" [net/netfilter/xt_nat.ko] undefined!
ERROR: "__fentry__" [net/netfilter/xt_mark.ko] undefined!
ERROR: "__fentry__" [net/netfilter/xt_addrtype.ko] undefined!
ERROR: "__fentry__" [net/netfilter/xt_LOG.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_nat_sip.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_nat_irc.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_nat_ftp.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_nat.ko] undefined!
ERROR: "__fentry__" [net/netfilter/nf_log_common.ko] undefined!
ERROR: "__fentry__" [net/ipv6/netfilter/nf_log_ipv6.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/nf_nat_masquerade_ipv4.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/nf_nat_ipv4.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/nf_log_ipv4.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/nf_log_arp.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/iptable_nat.ko] undefined!
ERROR: "__fentry__" [net/ipv4/netfilter/ipt_MASQUERADE.ko] undefined!
ERROR: "__fentry__" [fs/efivarfs/efivarfs.ko] undefined!
ERROR: "__fentry__" [drivers/thermal/x86_pkg_temp_thermal.ko] undefined!scripts/Makefile.modpost:91: recipe for target '__modpost' failed make[1]: *** [__modpost] Error 1
Quoted 3 lines
Verified with gcc-5.3.1 and gcc-6.1.1. You need to do a clean build with 'make mrproper' first to see this. Subsequent builds will succeed oddly enough. I suspect this
Thanks for the report, I'm looking into it.
Signature · 1 line
Emese
Re: [PATCH v9 3/4] Add Cyclomatic complexity GCC plugin
In reply to Laura AbbottQuoted 7 lines · On Tue, 14 Jun 2016 12:29:59 -0700 Laura Abbott <[email protected]> wrote:
On 05/23/2016 03:10 PM, Emese Revfy wrote:
1) make mrproper
2) make defconfig
3) enableGCC_PLUGINS,GCC_PLUGIN_CYC_COMPLEXITY
4) enableFUNCTION_TRACER(it will select other options as well)
5) make && make modules
Quoted 5 lines
ERROR: "
__fentry__" [net/ipv4/netfilter/ipt_MASQUERADE.ko] undefined!
ERROR: "__fentry__" [fs/efivarfs/efivarfs.ko] undefined!
ERROR: "__fentry__" [drivers/thermal/x86_pkg_temp_thermal.ko] undefined!scripts/Makefile.modpost:91: recipe for target '__modpost' failed make[1]: *** [__modpost] Error 1
The patch below fixes this bug, but I think it is ugly.
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next/commit/73e2d117daa7f3c6aee8935ecbb318354ff9cd82
If anyone has a better idea to fix this, please let me know.
Thanks
Signature · 1 line
Emese
Re: [PATCH v9 3/4] Add Cyclomatic complexity GCC plugin
Quoted 16 lines · Emese Revfy
Quoted 7 lines · On Tue, 14 Jun 2016 12:29:59 -0700 Laura Abbott <[email protected]> wrote:
On 05/23/2016 03:10 PM, Emese Revfy wrote:
1) make mrproper
2) make defconfig
3) enableGCC_PLUGINS,GCC_PLUGIN_CYC_COMPLEXITY
4) enableFUNCTION_TRACER(it will select other options as well)
5) make && make modulesQuoted 5 lines
ERROR: "
__fentry__" [net/ipv4/netfilter/ipt_MASQUERADE.ko] undefined!
ERROR: "__fentry__" [fs/efivarfs/efivarfs.ko] undefined!
ERROR: "__fentry__" [drivers/thermal/x86_pkg_temp_thermal.ko] undefined!scripts/Makefile.modpost:91: recipe for target '__modpost' failed make[1]: *** [__modpost] Error 1
Quoted 4 lines
The patch below fixes this bug, but I think it is ugly.
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next/commit/73e2d117daa7f3c6aee8935ecbb318354ff9cd82If anyone has a better idea to fix this, please let me know.
I think the logic looks fine, but I think I would name it more generically. For example, call it something plugin-agnostic like KBUILD_CFLAGS_MINIMUM.
I actually wonder if I can use this same logic to fix the stack protector detection code...
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security