All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>,
	Jacob Keller <jacob.e.keller@intel.com>
Subject: [PATCH v2 15/16] packed: no out-of-bound access of packed bitfields
Date: Sat, 26 Dec 2020 18:51:28 +0100	[thread overview]
Message-ID: <20201226175129.9621-16-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20201226175129.9621-1-luc.vanoostenryck@gmail.com>

There is (at least) 2 ways by which packed bitfields doesn't
follow normal layout/access rules and as consequence can't (always)
be accessed the usual way (load the whole underlying word, then shift
and mask to isolate the bitfield).

At least two different cases are a concern:
1) there is no padding at the end of a bitfield sequence. For example,
   the following struct is only 3 bytes width:
	struct s {
		int f:24;
	} __packed;
   So, trying to access the bitfield by first doing a 32-bit load
   will create an out-of-bound access.

2) a bitfield smaller than one word may need more than one word to be
   accessed. For example, with the following struct
	struct {
		int a:5;
		int f:30;
		int z:5;
	} __packed;
   the bitfield 'f', while smaller than one 32-bit word, can't be accessed
   with a single 32-bit access.

At machine level, these bitfields should be accessed with several, possibly
smaller, loads and their corresponding values reconstructed form these,
making things much more complicated than for non-packed bitfields.

But at IR level, things can be a little more flexible and things can stay
simple by using sub-word or super-word accesses (until these need to
be lowered to be usable at machine level). In other words, the example here
can be safely accessed with respectively a 24-bit and a 40-bit load.
This is what is done in this patch.
---
 linearize.c | 13 +++++++++++--
 symbol.h    |  1 +
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/linearize.c b/linearize.c
index 0250c6bb17ef..e80715ab2458 100644
--- a/linearize.c
+++ b/linearize.c
@@ -977,8 +977,17 @@ static struct symbol *bitfield_base_type(struct symbol *sym)
 	if (sym) {
 		if (sym->type == SYM_NODE)
 			base = base->ctype.base_type;
-		if (base->type == SYM_BITFIELD)
-			return base->ctype.base_type;
+		if (base->type == SYM_BITFIELD) {
+			base = base->ctype.base_type;
+			if (sym->packed) {
+				int size = bits_to_bytes(sym->bit_offset + sym->bit_size);
+				sym = __alloc_symbol(0);
+				*sym = *base;
+				sym->bit_size = bytes_to_bits(size);
+				return sym;
+			}
+			return base;
+		}
 	}
 	return sym;
 }
diff --git a/symbol.h b/symbol.h
index 5c5a7f12affa..866d57522f49 100644
--- a/symbol.h
+++ b/symbol.h
@@ -192,6 +192,7 @@ struct symbol {
 					accessed:1,
 					builtin:1,
 					torename:1,
+					packed:1,
 					transparent_union:1;
 			int		rank:3;	// arithmetic's rank
 			struct expression *array_size;
-- 
2.29.2


  parent reply	other threads:[~2020-12-26 17:53 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-26 17:51 [PATCH 00/16] support __packed struct Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 01/16] add testcases for dubious enum values Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 02/16] add testcases for exotic " Luc Van Oostenryck
2020-12-28 16:10   ` Ramsay Jones
2020-12-28 20:00     ` Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 03/16] add testcases for enum attributes Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 04/16] add testcases for type attributes Luc Van Oostenryck
2020-12-28 16:13   ` Ramsay Jones
2020-12-28 19:59     ` Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 05/16] add testcases for packed structures Luc Van Oostenryck
2020-12-28 16:17   ` Ramsay Jones
2020-12-28 20:01     ` Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 06/16] add testcases for packed bitfields Luc Van Oostenryck
2020-12-28 16:28   ` Ramsay Jones
2020-12-28 20:05     ` Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 07/16] apply_ctype: use self-explanatory argument name Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 08/16] apply_ctype: reverse the order of arguments Luc Van Oostenryck
2020-12-28 16:47   ` Ramsay Jones
2020-12-28 20:37     ` Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 09/16] apply_ctype: move up its declaration Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 10/16] struct-attr: prepare to handle attributes at the end of struct definitions (1) Luc Van Oostenryck
2020-12-28 16:54   ` Ramsay Jones
2020-12-28 20:49     ` Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 11/16] struct-attr: prepare to handle attributes at the end of struct definitions (2) Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 12/16] struct-attr: prepare to handle attributes at the end of struct definitions (3) Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 13/16] struct-attr: fix type attribute like 'struct __attr { ... }' Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 14/16] struct-attr: fix: do not ignore struct/union/enum type attributes Luc Van Oostenryck
2020-12-26 17:51 ` Luc Van Oostenryck [this message]
2020-12-28 17:10   ` [PATCH v2 15/16] packed: no out-of-bound access of packed bitfields Ramsay Jones
2020-12-28 21:12     ` Luc Van Oostenryck
2020-12-26 17:51 ` [PATCH v2 16/16] packed: add support for __packed struct Luc Van Oostenryck
2020-12-28 17:18 ` [PATCH 00/16] support " Ramsay Jones
2020-12-28 21:33   ` Luc Van Oostenryck
2021-01-05 17:56     ` Jacob Keller
2021-01-05 20:39       ` Luc Van Oostenryck
2021-01-05 22:07         ` Jacob Keller
2021-01-05 17:55 ` Jacob Keller

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=20201226175129.9621-16-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=jacob.e.keller@intel.com \
    --cc=linux-sparse@vger.kernel.org \
    /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.