All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
To: u-boot@lists.denx.de
Subject: [RFC PATCH 2/2] global-data.h: add build-time sanity check of sizeof(struct global_data)
Date: Tue, 18 May 2021 11:19:47 +0200	[thread overview]
Message-ID: <20210518091947.63513-3-rasmus.villemoes@prevas.dk> (raw)
In-Reply-To: <20210518091947.63513-1-rasmus.villemoes@prevas.dk>

The layout and contents of struct global_data depends on a lot of
CONFIG_* preprocessor macros, not all of which are entirely converted
to Kconfig - not to mention weird games played here and there. This
can result in one translation unit using one definition of struct
global_data while the actual layout is another.

That can be very hard to debug. But we already have a mechanism that
can help catch such bugs at build time, namely the asm-offsets
machinery which is necessary anyway to provide assembly code with the
necessary constants. So make sure that every C translation unit that
include global_data.h actually sees the same size of struct
global_data as that which was seen by the asm-offsets.c TU.

It is likely that this patch will break the build of some boards. For
example, without the patch from Matt Merhar
(https://lists.denx.de/pipermail/u-boot/2021-May/450135.html) or some
other fix, this breaks P2041RDB_defconfig:

  CC      arch/powerpc/lib/traps.o
  AS      arch/powerpc/cpu/mpc85xx/start.o
In file included from include/asm-generic/global_data.h:26,
                 from ./arch/powerpc/include/asm/global_data.h:109,
                 from include/init.h:21,
                 from arch/powerpc/lib/traps.c:7:
include/linux/build_bug.h:99:41: error: static assertion failed: "sizeof(struct global_data) == GD_SIZE"
   99 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
      |                                         ^~~~~~~~~~~~~~
include/linux/build_bug.h:98:34: note: in expansion of macro ?__static_assert?
   98 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
      |                                  ^~~~~~~~~~~~~~~
include/asm-generic/global_data.h:470:1: note: in expansion of macro ?static_assert?
  470 | static_assert(sizeof(struct global_data) == GD_SIZE);
      | ^~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:266: arch/powerpc/lib/traps.o] Error 1
make: *** [Makefile:1753: arch/powerpc/lib] Error 2
make: *** Waiting for unfinished jobs....

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
---
 include/asm-generic/global_data.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/asm-generic/global_data.h b/include/asm-generic/global_data.h
index 47921d27b1..1233ec0ed6 100644
--- a/include/asm-generic/global_data.h
+++ b/include/asm-generic/global_data.h
@@ -23,6 +23,8 @@
 #include <fdtdec.h>
 #include <membuff.h>
 #include <linux/list.h>
+#include <linux/build_bug.h>
+#include <asm-offsets.h>
 
 struct acpi_ctx;
 struct driver_rt;
@@ -464,6 +466,9 @@ struct global_data {
 	char *smbios_version;
 #endif
 };
+#ifndef DO_DEPS_ONLY
+static_assert(sizeof(struct global_data) == GD_SIZE);
+#endif
 
 /**
  * gd_board_type() - retrieve board type
-- 
2.29.2

  parent reply	other threads:[~2021-05-18  9:19 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-17 17:32 [PATCH] powerpc: fix regression in arch_initr_trap() Matt Merhar
2021-05-18  5:50 ` Stefan Roese
2021-05-18  7:04   ` Rasmus Villemoes
2021-05-18  9:19     ` [RFC PATCH 0/2] sizeof(gd_t) sanity checking Rasmus Villemoes
2021-05-18  9:19       ` [RFC PATCH 1/2] build_bug.h: add wrapper for _Static_assert Rasmus Villemoes
2021-05-19 15:34         ` Simon Glass
2021-07-01 21:54         ` Tom Rini
2021-05-18  9:19       ` Rasmus Villemoes [this message]
2021-05-19 15:34         ` [RFC PATCH 2/2] global-data.h: add build-time sanity check of sizeof(struct global_data) Simon Glass
2021-07-01 21:54         ` Tom Rini
2021-06-03 13:38       ` [PATCH 1/4] powerpc: Don't use relative include for config.h in global_data.h Tom Rini
2021-06-03 13:39         ` [PATCH 2/4] Revert "powerpc: fix regression in arch_initr_trap()" Tom Rini
2021-06-24 13:15           ` Tom Rini
2021-06-03 13:39         ` [PATCH 3/4] socfpga64: Do not define CONFIG_SYS_MEM_RESERVE_SECURE to 0 Tom Rini
2021-06-03 15:07           ` Rasmus Villemoes
2021-06-03 15:14             ` Tom Rini
2021-06-24 13:15           ` Tom Rini
2021-06-03 13:39         ` [PATCH 4/4] global_data: Ensure we have <config.h> when symbols are not in Kconfig yet Tom Rini
2021-06-24 13:15           ` Tom Rini
2021-06-05 19:18         ` [PATCH 1/4] powerpc: Don't use relative include for config.h in global_data.h Matt Merhar
2021-06-24 13:15         ` Tom Rini
2021-05-27 11:42 ` [PATCH] powerpc: fix regression in arch_initr_trap() Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210518091947.63513-3-rasmus.villemoes@prevas.dk \
    --to=rasmus.villemoes@prevas.dk \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.