简体   繁体   English

32位和64位架构中的结构成员对齐混乱

[英]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. 在64位linux内核中运行32位应用程序(用C语言编写)时,我遇到一些问题。 The same application runs without any issues in 32 bit kernel. 相同的应用程序在32位内核中运行时没有任何问题。

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. 两个位字段srtp_mki_sideA_en和srtp_mki_sideB_en的值经常损坏。

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? 共享数据结构时,成员对齐在32位和64位体系结构中是否存在任何差异,可能导致内存损坏?

Is there any other known issues when sharing data structures between 64 bit kernel and 32 bit applications? 在64位内核和32位应用程序之间共享数据结构时,还有其他已知问题吗?

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. 它没有声明为压缩的,它具有intshort部件,几乎每个建筑都具有不同的大小。 Also the bitfields have compiler dependent catches to them (One compiler I used in the past allowed only 8 bit long bitfields). 位域也具有依赖于编译器的捕获(我过去使用的一个编译器仅允许8位长的位域)。

I would totally reorganize the struct. 我将完全重组该结构。 First of all, order the contents by size (biggest first). 首先,按大小排序内容(从大到大)。 Use packing, use types declared in stdint.h. 使用包装,使用在stdint.h中声明的类型。 int32_t for example is guaranteed to have 32 bits on every architecture. 例如,保证int32_t在每个体系结构上都具有32位。

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. 如果您无法控制该结构并且无法对其进行更改,请对调试所使用的内存布局进行反向工程,然后在您的应用程序中设计一个类似于给定结构的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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