简体   繁体   中英

Confusion in structure member alignment in 32 bit and 64 bit architecture

I am facing some issue when running a 32bit application(Written in C) in a 64 bit linux kernel. The same application runs without any issues in 32 bit kernel.

After spending a lot time for debugging the picture became little more clearer. There is a structure shared between the userspace application and a kernel module. The values of members in the structure variable gets corrupted when passed from user space to kernel space.

Here is the defenition of the structure

struct entry
{
    unsigned active:1;
    unsigned strict:1;
    unsigned AB_is_ipv6:1;
    unsigned XY_is_ipv6:1;
    unsigned srtp_sideA_en:2;
    unsigned srtp_sideB_en:2;
    unsigned srtp_mki_sideA_en:2;
    unsigned srtp_mki_sideB_en:2;
    unsigned cnt_fdnat, cnt_fsnat, cnt_rdnat, cnt_rsnat;
    unsigned short Apt, Bpt, Xpt, Ypt;
    unsigned int err[2];
};

The values of the two bit fields srtp_mki_sideA_en and srtp_mki_sideB_en becomes corrupted frequently.

Is there any issue in sharing structure like this when there are bit field members inside it?

Is there any difference in the member alignment in 32 bit and 64 bit architecture that could result in memory corruption when sharing data structures?

Is there any other known issues when sharing data structures between 64 bit kernel and 32 bit applications?

I would not trust the memory layout of that struct at all. It is not declared as packed, it has int and short parts in it, which are of different size for almost every architekture. Also the bitfields have compiler dependent catches to them (One compiler I used in the past allowed only 8 bit long bitfields).

I would totally reorganize the struct. First of all, order the contents by size (biggest first). Use packing, use types declared in stdint.h. int32_t for example is guaranteed to have 32 bits on every architecture.

If you do not have control over the struct and can not change it, reverse engineer the memory layout used by debugging, and then craft a struct in your application which resembles the given struct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM