forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C front-end: fix processing of alignment and packing attributes
Packing needs to be evaluated as part of the definition of the type while alignment applies also when using a type. The position of the attribute also requires extra care as some positions are accepted by GCC (and warned about by Clang) while actually being ignored. Fixes: diffblue#8443
- Loading branch information
1 parent
2212cd6
commit 5eea045
Showing
4 changed files
with
274 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
#include <stdint.h> | ||
|
||
struct S1 | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} __attribute__((packed)); | ||
struct S1 s1; | ||
|
||
struct S1a | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} __attribute__((packed)) __attribute__((aligned(16))); | ||
struct S1a s1a; | ||
|
||
struct __attribute__((packed)) S2 | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
}; | ||
struct S2 s2; | ||
|
||
struct __attribute__((packed)) __attribute__((aligned(16))) S2a | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
}; | ||
struct S2a s2a; | ||
|
||
// "packed" will be ignored | ||
__attribute__((packed)) struct S3 | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
}; | ||
struct S3 s3; | ||
|
||
// both "packed" and "aligned" | ||
__attribute__((packed)) __attribute__((aligned(16))) struct S3a | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
}; | ||
struct S3a s3a; | ||
|
||
struct S4 | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} __attribute__((packed)) s4; | ||
|
||
struct S4a | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} __attribute__((packed)) __attribute__((aligned(16))) s4a; | ||
|
||
struct __attribute__((packed)) S5 | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} s5; | ||
|
||
struct __attribute__((packed)) __attribute__((aligned(16))) S5a | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} s5a; | ||
|
||
typedef struct __attribute__((packed)) S6 | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} s6; | ||
|
||
typedef struct __attribute__((packed)) __attribute__((aligned(16))) S6a | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} s6a; | ||
|
||
// "packed" will be ignored in the following by both GCC and Clang | ||
struct S7 | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} s7 __attribute__((packed)); | ||
|
||
// "packed" will be ignored in the following by both GCC and Clang | ||
struct S7a | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} s7a __attribute__((packed)) __attribute__((aligned(16))); | ||
|
||
typedef struct T1 | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} __attribute__((packed)) T1_t; | ||
|
||
typedef struct T1a | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} __attribute__((packed)) __attribute__((aligned(16))) T1a_t; | ||
|
||
typedef struct __attribute__((packed)) T2 | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} T2_t; | ||
|
||
typedef struct __attribute__((packed)) __attribute__((aligned(16))) T2a | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
} T2a_t; | ||
|
||
struct U | ||
{ | ||
uint8_t c; | ||
int32_t i; | ||
}; | ||
|
||
struct S | ||
{ | ||
uint8_t c; | ||
// attribute ignored by GCC | ||
struct S1 __attribute((packed)) i1; | ||
struct U __attribute((packed)) i2; | ||
uint8_t c2; | ||
// alignment has to be a power of 2 | ||
// struct S2 __attribute((aligned(5))) i2_5; | ||
struct S2a __attribute((aligned(8))) i3; | ||
uint8_t c3; | ||
struct S3a __attribute((aligned(8))) i4; | ||
uint8_t c4; | ||
T1a_t __attribute((aligned(8))) i5; | ||
T1_t __attribute((aligned(8))) i6; | ||
}; | ||
|
||
int32_t main() | ||
{ | ||
_Static_assert(sizeof(struct S1) == 5, ""); | ||
_Static_assert(sizeof(s1) == 5, ""); | ||
_Static_assert(sizeof(struct S1a) == 16, ""); | ||
_Static_assert(sizeof(s1a) == 16, ""); | ||
|
||
_Static_assert(sizeof(struct S2) == 5, ""); | ||
_Static_assert(sizeof(s2) == 5, ""); | ||
_Static_assert(sizeof(struct S2a) == 16, ""); | ||
_Static_assert(sizeof(s2a) == 16, ""); | ||
|
||
_Static_assert(sizeof(struct S3) == 8, ""); | ||
_Static_assert(sizeof(s3) == 8, ""); | ||
_Static_assert(sizeof(struct S3a) == 8, ""); | ||
_Static_assert(sizeof(s3a) == 8, ""); | ||
|
||
_Static_assert(sizeof(struct S4) == 5, ""); | ||
_Static_assert(sizeof(s4) == 5, ""); | ||
_Static_assert(sizeof(struct S4a) == 16, ""); | ||
_Static_assert(sizeof(s4a) == 16, ""); | ||
|
||
_Static_assert(sizeof(struct S5) == 5, ""); | ||
_Static_assert(sizeof(s5) == 5, ""); | ||
_Static_assert(sizeof(struct S5a) == 16, ""); | ||
_Static_assert(sizeof(s5a) == 16, ""); | ||
|
||
_Static_assert(sizeof(struct S6) == 5, ""); | ||
_Static_assert(sizeof(s6) == 5, ""); | ||
_Static_assert(sizeof(struct S6a) == 16, ""); | ||
_Static_assert(sizeof(s6a) == 16, ""); | ||
|
||
_Static_assert(sizeof(struct S7) == 8, ""); | ||
_Static_assert(sizeof(s7) == 8, ""); | ||
_Static_assert(sizeof(struct S7a) == 8, ""); | ||
_Static_assert(sizeof(s7a) == 8, ""); | ||
|
||
_Static_assert(sizeof(struct T1) == 5, ""); | ||
_Static_assert(sizeof(T1_t) == 5, ""); | ||
_Static_assert(sizeof(struct T1a) == 16, ""); | ||
_Static_assert(sizeof(T1a_t) == 16, ""); | ||
|
||
_Static_assert(sizeof(struct T2) == 5, ""); | ||
_Static_assert(sizeof(T2_t) == 5, ""); | ||
_Static_assert(sizeof(struct T2a) == 16, ""); | ||
_Static_assert(sizeof(T2a_t) == 16, ""); | ||
|
||
_Static_assert(sizeof(struct S) == 96, ""); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE gcc-only | ||
main.c | ||
|
||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
^warning: ignoring | ||
^CONVERSION ERROR$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters