linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gcov: fail build on gcov_info size mismatch
@ 2021-03-11 13:03 Peter Oberparleiter
  2021-03-11 18:38 ` Linus Torvalds
  2021-03-11 19:33 ` kernel test robot
  0 siblings, 2 replies; 14+ messages in thread
From: Peter Oberparleiter @ 2021-03-11 13:03 UTC (permalink / raw)
  To: akpm; +Cc: torvalds, oberpar, linux-kernel

gcov kernel profiling works by emulating parts of GCC's libgcov in the
kernel, including a definition of struct gcov_info used by profiling
code to handle coverage data. The original definition of this data type
is not available outside of GCC's source tree, and when it changes with
new GCC versions the result may be hard-to-debug kernel failures [1].

This patch adds a compile-time check to ensure that the kernel's version
of struct gcov_info has the same length as the one used by GCC as
determined by looking at GCC's assembler output. This check should help
reduce the number of run-time failures when using gcov kernel support
with new GCC versions that include updates to struct gcov_info.

Tested with various GCC versions between 4.9 and 10, and also Clang 11.

[1] https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1891288

Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
---
 kernel/gcov/Makefile       |  9 +++++++++
 kernel/gcov/gcc_4_7.c      |  3 +++
 kernel/gcov/geninfosize.sh | 19 +++++++++++++++++++
 3 files changed, 31 insertions(+)
 create mode 100755 kernel/gcov/geninfosize.sh

diff --git a/kernel/gcov/Makefile b/kernel/gcov/Makefile
index 16f8ecc7d882..1cbeb0de944e 100644
--- a/kernel/gcov/Makefile
+++ b/kernel/gcov/Makefile
@@ -1,6 +1,15 @@
 # SPDX-License-Identifier: GPL-2.0
 ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
 
+ifeq ($(CONFIG_CC_IS_GCC),y)
+  GCOV_INFO_SIZE := $(shell $(srctree)/$(src)/geninfosize.sh)
+  ifneq ($(GCOV_INFO_SIZE),)
+    ccflags-y += -DGCC_GCOV_INFO_SIZE=$(GCOV_INFO_SIZE)
+  else
+    $(error Could not determine size of GCC's struct gcov_info)
+  endif
+endif
+
 obj-y := base.o fs.o
 obj-$(CONFIG_CC_IS_GCC) += gcc_base.o gcc_4_7.o
 obj-$(CONFIG_CC_IS_CLANG) += clang.o
diff --git a/kernel/gcov/gcc_4_7.c b/kernel/gcov/gcc_4_7.c
index c53408a00d0b..fce003cfc790 100644
--- a/kernel/gcov/gcc_4_7.c
+++ b/kernel/gcov/gcc_4_7.c
@@ -94,6 +94,9 @@ struct gcov_info {
 	struct gcov_fn_info **functions;
 };
 
+_Static_assert(sizeof(struct gcov_info) == GCC_GCOV_INFO_SIZE,
+	       "struct gcov_info must be updated");
+
 /**
  * gcov_info_filename - return info filename
  * @info: profiling data set
diff --git a/kernel/gcov/geninfosize.sh b/kernel/gcov/geninfosize.sh
new file mode 100755
index 000000000000..c05f1374fcb3
--- /dev/null
+++ b/kernel/gcov/geninfosize.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# Determine size of GCC's internal struct gcov_info. CC must be set correctly.
+#
+# Note: GCC adds an instance of its built-in version of struct gcov_info under
+#       the label .LPBX0 to assembler output when compiling with --coverage
+#
+
+echo "void fn(void) {}" | $CC -S --coverage -x c - -o - | \
+while read a b c ; do
+	# .size	.LPBX0, 112
+	if [ "$a" = ".size" -a "$b" = ".LPBX0," ] ; then
+		echo "$c"
+		break
+	fi
+done
+
+exit 0
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2021-03-16  9:52 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-11 13:03 [PATCH] gcov: fail build on gcov_info size mismatch Peter Oberparleiter
2021-03-11 18:38 ` Linus Torvalds
2021-03-12 17:46   ` Peter Oberparleiter
2021-03-11 19:33 ` kernel test robot
2021-03-11 20:02   ` Linus Torvalds
2021-03-12  3:49     ` [kbuild-all] " Rong Chen
2021-03-12 17:52       ` Linus Torvalds
2021-03-15  2:31         ` Rong Chen
2021-03-15 19:12           ` Linus Torvalds
2021-03-15 20:32             ` Jamie Heilman
2021-03-15 20:57               ` Linus Torvalds
2021-03-15 23:09                 ` Herbert Xu
2021-03-15 23:22                   ` Linus Torvalds
2021-03-16  9:51         ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).