From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Ramsay Jones <ramsay@ramsayjones.plus.com>,
Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH v3 06/16] add testcases for packed bitfields
Date: Thu, 31 Dec 2020 11:10:24 +0100 [thread overview]
Message-ID: <20201231101034.59978-7-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20201231101034.59978-1-luc.vanoostenryck@gmail.com>
Currently, packed bitfields are not handled correctly.
Add some testcases for them.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
validation/packed-bitfield0.c | 59 +++++++++++++++++++++++++++++++++++
validation/packed-bitfield1.c | 28 +++++++++++++++++
validation/packed-bitfield2.c | 16 ++++++++++
validation/packed-bitfield3.c | 29 +++++++++++++++++
validation/packed-bitfield4.c | 19 +++++++++++
validation/packed-bitfield5.c | 21 +++++++++++++
6 files changed, 172 insertions(+)
create mode 100644 validation/packed-bitfield0.c
create mode 100644 validation/packed-bitfield1.c
create mode 100644 validation/packed-bitfield2.c
create mode 100644 validation/packed-bitfield3.c
create mode 100644 validation/packed-bitfield4.c
create mode 100644 validation/packed-bitfield5.c
diff --git a/validation/packed-bitfield0.c b/validation/packed-bitfield0.c
new file mode 100644
index 000000000000..f84e7b904a82
--- /dev/null
+++ b/validation/packed-bitfield0.c
@@ -0,0 +1,59 @@
+#define alignof(X) __alignof__(X)
+#define __packed __attribute__((packed))
+
+struct sa {
+ int a:7;
+ int c:10;
+ int b:2;
+} __packed;
+_Static_assert(alignof(struct sa) == 1, "alignof(struct sa)");
+_Static_assert( sizeof(struct sa) == 3, "sizeof(struct sa)");
+
+
+static int get_size(void)
+{
+ return sizeof(struct sa);
+}
+
+static void chk_align(struct sa sa, struct sa *p)
+{
+ _Static_assert(alignof(sa) == 1, "alignof(sa)");
+ _Static_assert(alignof(*p) == 1, "alignof(*p)");
+}
+
+static int fp0(struct sa *sa)
+{
+ return sa->c;
+}
+
+static int fpx(struct sa *sa, int idx)
+{
+ return sa[idx].c;
+}
+
+static int fglobal(void)
+{
+ extern struct sa g;
+ return g.c;
+}
+
+static struct sa l;
+static int flocal(void)
+{
+ return l.c;
+}
+
+
+int main(void)
+{
+ extern void fun(struct sa *);
+ struct sa sa = { 0 };
+
+ fun(&sa);
+ return 0;
+}
+
+/*
+ * check-name: packed-bitfield0
+ * check-known-to-fail
+ */
diff --git a/validation/packed-bitfield1.c b/validation/packed-bitfield1.c
new file mode 100644
index 000000000000..208a3dc5127c
--- /dev/null
+++ b/validation/packed-bitfield1.c
@@ -0,0 +1,28 @@
+#define __packed __attribute__((packed))
+
+struct s {
+ unsigned int f0:1;
+ unsigned int f1:1;
+ unsigned int pad:6;
+} __packed;
+_Static_assert(sizeof(struct s) == 1, "sizeof(struct s)");
+
+extern struct s g;
+
+static int foo(struct s *ptr)
+{
+ int f = 0;
+
+ f += g.f0;
+ f += g.f1;
+
+ f += ptr->f0;
+ f += ptr->f1;
+
+ return f;
+}
+
+/*
+ * check-name: packed-bitfield1
+ * check-known-to-fail
+ */
diff --git a/validation/packed-bitfield2.c b/validation/packed-bitfield2.c
new file mode 100644
index 000000000000..4587ebec5c90
--- /dev/null
+++ b/validation/packed-bitfield2.c
@@ -0,0 +1,16 @@
+struct bf2 {
+ unsigned p1:2;
+ unsigned i1:32;
+ unsigned p2:2;
+ unsigned s9:9;
+ unsigned s9:9;
+ unsigned s9:9;
+ unsigned b1:1;
+} __attribute__((packed));
+
+_Static_assert(sizeof(struct bf2) == 8);
+
+/*
+ * check-name: packed-bitfield2
+ * check-known-to-fail
+ */
diff --git a/validation/packed-bitfield3.c b/validation/packed-bitfield3.c
new file mode 100644
index 000000000000..c06e7c41cbcd
--- /dev/null
+++ b/validation/packed-bitfield3.c
@@ -0,0 +1,29 @@
+#define __packed __attribute__((packed))
+
+typedef unsigned char u8;
+typedef __UINT16_TYPE__ u16;
+typedef __UINT32_TYPE__ u32;
+typedef __UINT64_TYPE__ u64;
+
+struct b {
+ u32 a:1;
+ u32 b:2;
+ u32 c:4;
+ u32 d:8;
+ u32 e:16;
+} __packed;
+_Static_assert(__alignof(struct b) == 1);
+_Static_assert( sizeof(struct b) == 4);
+
+struct c {
+ u8 a;
+ u8 b;
+ u64 c:48;
+} __packed;
+_Static_assert(__alignof(struct c) == 1);
+_Static_assert( sizeof(struct c) == 8);
+
+/*
+ * check-name: packed-bitfield3
+ * check-known-to-fail
+ */
diff --git a/validation/packed-bitfield4.c b/validation/packed-bitfield4.c
new file mode 100644
index 000000000000..0342b2414b6e
--- /dev/null
+++ b/validation/packed-bitfield4.c
@@ -0,0 +1,19 @@
+#define __packed __attribute__((packed))
+
+typedef __UINT32_TYPE__ u32;
+
+struct s {
+ u32 f:24;
+} __packed;
+_Static_assert(sizeof(struct s) == 3);
+
+static int ld(struct s *s)
+{
+ return s->f;
+}
+
+/*
+ * check-name: packed-bitfield4
+ * check-description: Is check_access() OK with short packed bitfields?
+ * check-known-to-fail
+ */
diff --git a/validation/packed-bitfield5.c b/validation/packed-bitfield5.c
new file mode 100644
index 000000000000..8f44d4c2c277
--- /dev/null
+++ b/validation/packed-bitfield5.c
@@ -0,0 +1,21 @@
+#define __packed __attribute__((packed))
+
+typedef __UINT32_TYPE__ u32;
+
+struct s {
+ u32 a:5;
+ u32 f:30;
+ u32 z:5;
+} __packed;
+_Static_assert(sizeof(struct s) == 5);
+
+static int ld(struct s *s)
+{
+ return s->f;
+}
+
+/*
+ * check-name: packed-bitfield5
+ * check-description: is check_access() OK with 'overlapping' packed bitfields?
+ * check-known-to-fail
+ */
--
2.29.2
next prev parent reply other threads:[~2020-12-31 10:12 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-31 10:10 [PATCH v3 00/16] support __packed struct Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 01/16] add testcases for dubious enum values Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 02/16] add testcases for exotic " Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 03/16] add testcases for enum attributes Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 04/16] add testcases for type attributes Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 05/16] add testcases for packed structures Luc Van Oostenryck
2020-12-31 10:10 ` Luc Van Oostenryck [this message]
2020-12-31 10:10 ` [PATCH v3 07/16] apply_ctype: use self-explanatory argument name Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 08/16] apply_ctype: reverse the order of arguments Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 09/16] apply_ctype: move up its declaration Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 10/16] struct-attr: prepare to handle attributes at the end of struct definitions (1) Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 11/16] struct-attr: prepare to handle attributes at the end of struct definitions (2) Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 12/16] struct-attr: prepare to handle attributes at the end of struct definitions (3) Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 13/16] struct-attr: fix type attribute like 'struct __attr { ... }' Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 14/16] struct-attr: fix: do not ignore struct/union/enum type attributes Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 15/16] packed: no out-of-bound access of packed bitfields Luc Van Oostenryck
2020-12-31 10:10 ` [PATCH v3 16/16] packed: add support for __packed struct Luc Van Oostenryck
2020-12-31 15:30 ` [PATCH v3 00/16] support " Ramsay Jones
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=20201231101034.59978-7-luc.vanoostenryck@gmail.com \
--to=luc.vanoostenryck@gmail.com \
--cc=linux-sparse@vger.kernel.org \
--cc=ramsay@ramsayjones.plus.com \
/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 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).