Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__attribute__((packed)) incorrectly handled when used in member of a struct #8443

Open
nianzelee opened this issue Sep 5, 2024 · 0 comments · May be fixed by #8454
Open

__attribute__((packed)) incorrectly handled when used in member of a struct #8443

nianzelee opened this issue Sep 5, 2024 · 0 comments · May be fixed by #8454

Comments

@nianzelee
Copy link

CBMC version: 5.95.1
Operating system: Ubuntu 22.04
Exact command line resulting in the issue: cbmc --trace <example program below>
What behaviour did you expect: The size of the second struct should be 16 bytes.
What happened instead: CBMC thought the size is 6 bytes and reported a false alarm.

Example program:

#include <assert.h>
#include <stdio.h>

typedef struct Example_packed_s {
  char c;
  int i;
} __attribute__((packed)) Example_packed_t;

typedef struct Example_aligned_s {
  char c;
  Example_packed_t i __attribute__((aligned(8)));
} Example_aligned_t;

int main() {
  printf("Size of example packed struct = %ld\n", sizeof(Example_packed_t));
  printf("Size of example aligned struct = %ld\n", sizeof(Example_aligned_t));
  assert(sizeof(Example_packed_t) == 5);
  assert(sizeof(Example_aligned_t) == 16);
  return 0;
}

It seems the packed attribute in the first struct also influenced the second struct and caused CBMC to pack the second member i in Example_aligned_t. (Therefore, the size is 1 + 5 = 6 bytes).

Violated property:
  file compiler_atrribute_nested.c function main line 18 thread 0
  assertion sizeof(Example_aligned_t) == 16
  sizeof(Example_aligned_t) /*6ul*/  == (unsigned long int)16

GCC version gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 confirms that the size of the second struct is 16 bytes.

tautschnig added a commit to tautschnig/cbmc that referenced this issue Sep 13, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants