diff options
author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2018-01-20 10:43:18 +0100 |
---|---|---|
committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2020-12-29 14:39:38 +0100 |
commit | 13696250ed930848668538d06201da9a5cab9012 (patch) | |
tree | deb2d5b03f3ed932173168126c0a05044d52b416 | |
parent | 29cdbe161a46437751d14a72e2580c889f6f8a27 (diff) | |
download | sparse-13696250ed930848668538d06201da9a5cab9012.tar.gz |
add testcases for packed bitfields
Currently, packed bitfields are not handled correctly.
Add some testcases for them.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r-- | validation/packed-bitfield0.c | 59 | ||||
-rw-r--r-- | validation/packed-bitfield1.c | 28 | ||||
-rw-r--r-- | validation/packed-bitfield2.c | 16 | ||||
-rw-r--r-- | validation/packed-bitfield3.c | 29 | ||||
-rw-r--r-- | validation/packed-bitfield4.c | 19 | ||||
-rw-r--r-- | validation/packed-bitfield5.c | 21 |
6 files changed, 172 insertions, 0 deletions
diff --git a/validation/packed-bitfield0.c b/validation/packed-bitfield0.c new file mode 100644 index 00000000..f84e7b90 --- /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 00000000..208a3dc5 --- /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 00000000..4587ebec --- /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 00000000..c06e7c41 --- /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 00000000..0342b241 --- /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 00000000..8f44d4c2 --- /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 + */ |