11885 – [3.3 Regression] Problem with bitfields in packed structs (original) (raw)

Description Stan Rost 2003-08-11 21:09:52 UTC

Hi,

I have been hacking for Berkley Motes using nesC, which produces code later compiled by AVR-GCC. There is a problem with bitfields within packed structures, which is also reproducible on the native platform (i686-pc-linux-gnu).

The bug is illustrated by the following simple program:

include <stdio.h> #include <stdint.h>

typedef struct {

uint8_t flag1:2;
uint8_t flag2:1;
uint8_t flag3:1;

uint8_t flag4;

} attribute ((packed)) MyType;

int main(void) {

MyType a;
MyType *b = &a;

b->flag1 = 0;
b->flag2 = 0;
b->flag3 = 0;

b->flag4 = 0;

b->flag4++;

if (b->flag1)
printf("Set\n");
else
printf("Clear\n");

}


This is clearly an error because flag4 should be allocated its own byte and not packed into the byte with other variables.

Please look into this, as the current methods of circumvention force me to pad the structures with "reserved" bitfields.

Respectfully,

Stan Rost

Comment 1 Drea Pinski 2003-08-11 21:14:39 UTC

I can reproduce this in 3.3.1 (20030707). It is already fixed on the mainline and also in 3.2.3 so making this a regression.

Comment 3 Stan Rost 2003-08-11 23:27:08 UTC

Just tried the patch, recompiled and reinstalled, but the patch you suggested did not fix the problem.

Additionally, I double-checked everything so that the last sentence is not in error.

Comment 4 Drea Pinski 2003-08-11 23:40:37 UTC

Thanks for trying this patch, this helps us figure out which patch fixes this on the mainline. Janis, can you run your regression finder on this one as it looks like a bad regression (which is already fixed on the mainline) as prevents any one to use reserved fields in packed structors with bitfields.

Comment 5 janis187 2003-08-12 00:02:49 UTC

Subject: Re: [3.3 Regression] Problem with bitfields in packed structs

This looked like an interesting bug so I started a hunt on the 3.3 branch a couple of hours ago, and it's now down to a single day (2003-03-31). When I identify where it broke on the branch I'll check to see if it was also broken on mainline, and then what fixed it on mainline.

Comment 6 janis187 2003-08-12 16:51:25 UTC

The regression in PR 11885 was introduced or exposed on the 3.3 branch and mainline by this patch:

--- gcc/gcc/ChangeLog ---

2003-03-31 Jason Merrill <jason@redhat.com>

    * stor-layout.c (update_alignment_for_field): Respect
    DECL_USER_ALIGN for zero-length bitfields, too.
    * c-decl.c (finish_struct): Don't set DECL_ALIGN for normal
    fields.

--- gcc/gcc/cp/ChangeLog ---

2003-03-31 Jason Merrill <jason@redhat.com>

    * class.c (check_field_decl): Don't set DECL_ALIGN.

The test case passes for mainline after this large patch:

--- gcc/gcc/ChangeLog ---

2003-04-03 Jason Merrill <jason@redhat.com>

    * stor-layout.c (do_type_align): New fn, split out from...
    (layout_decl): ...here.  Do all alignment calculations for
    FIELD_DECLs here.
    (update_alignment_for_field): Not here.
    (start_record_layout, debug_rli): Remove unpadded_align.
    * tree.h (struct record_layout_info_s): Remove unpadded_align.
    * c-decl.c (finish_enum): Don't set DECL_SIZE, DECL_ALIGN
    or DECL_MODE on the CONST_DECLs.
    (finish_struct): Don't mess with DECL_ALIGN.

--- gcc/gcc/cp/ChangeLog ---

2003-04-03 Jason Merrill <jason@redhat.com>

    * class.c (build_vtable): Set DECL_ALIGN here.
    (get_vtable_decl): Not here.
    (layout_vtable_decl): Or here.
    (create_vtable_ptr): Or here.
    (layout_class_type): Or here.
    (check_bitfield_decl): Don't mess with field alignment.

The hunts took place on i686-pc-linux-gnu using the submitter's test case.

Comment 8 Drea Pinski 2003-10-14 18:51:01 UTC

Fixed for 3.3.2.