[LLVMbugs] [Bug 9253] New: clang: wrong alignment for fields / wrong total struct size

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Feb 19 02:15:23 PST 2011


http://llvm.org/bugs/show_bug.cgi?id=9253

           Summary: clang: wrong alignment for fields / wrong total struct
                    size
           Product: clang
           Version: trunk
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: edwintorok at gmail.com
                CC: llvmbugs at cs.uiuc.edu
        Depends on: 4068


clang gives this error during kernel compile:
kernel/trace/blktrace.c:649:7: error: duplicate case value '3225948787'
        case BLKTRACESETUP32:
             ^
kernel/trace/blktrace.c:644:7: note: previous case defined here                 
        case BLKTRACESETUP:
             ^

However this is a clang bug, because the 2 sizes of the two structs used in
calculating those constants are equal in clang, while they are different in
gcc. This is an ABI issue.

Testcase:
#include <stdio.h>

typedef unsigned short u16;
typedef unsigned short __u16;
typedef unsigned int __u32;
typedef unsigned int u32;
typedef unsigned long long u64;
__extension__ typedef unsigned long long __u64;
typedef u64 __attribute__((aligned(4))) compat_u64;
struct compat_blk_user_trace_setup {
 char name[32];
 u16 act_mask;
 u32 buf_size;
 u32 buf_nr;
 compat_u64 start_lba;
 compat_u64 end_lba;
 u32 pid;
};

struct blk_user_trace_setup {
 char name[32];
 __u16 act_mask;
 __u32 buf_size;
 __u32 buf_nr;
 __u64 start_lba;
 __u64 end_lba;
 __u32 pid;
};

int main()
{
    struct compat_blk_user_trace_setup b1;
    struct blk_user_trace_setup b2;
    printf("compat_blk_user_trace_setup = %d, blk_user_trace_setup = %d\n",
           sizeof(struct compat_blk_user_trace_setup),
           sizeof(struct blk_user_trace_setup));
}

Prints:
$ gcc -m64 x.c && ./a.out
compat_blk_user_trace_setup = 64, blk_user_trace_setup = 72
$ clang -m64 x.c && ./a.out
compat_blk_user_trace_setup = 72, blk_user_trace_setup = 72

Looks like clang doesn't honour the aligned(4) attribute, and tries to use an
8-byte alignment for compat_u64, when it should use just a 4-byte alignment.

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list