diff mbox series

[pseudo,1/2] pseudo_ipc.h: make 8 byte alignment for structure pseudo_msg_t

Message ID 20251111135951.2121065-1-hongxu.jia@windriver.com
State New
Headers show
Series [pseudo,1/2] pseudo_ipc.h: make 8 byte alignment for structure pseudo_msg_t | expand

Commit Message

Hongxu Jia Nov. 11, 2025, 1:59 p.m. UTC
typedef struct {
    pseudo_msg_type_t type;
    ...
    char path[];
} pseudo_msg_t;

The [] item at the end of the struct is a C99 feature:
Flexible Array Members in a structure in C [1]. The size
of path in structure should be 0.

On 64-bit arch, sizeof(pseudo_msg_t) = 80, but PSEUDO_HEADER_SIZE =
offsetof(pseudo_msg_t, path) = 76, the reason is 8 byte alignment
in 64-bit arch.

The differ between sizeof(pseudo_msg_t) and PSEUDO_HEADER_SIZE
made memory overlab in memcpy, such as

  memcpy(oldmsg, msg, PSEUDO_HEADER_SIZE);
  memcpy(newmsg, old, sizeof(pseudo_msg_t) + old->pathlen);

Add 4 chars to structure as reserved for 8 byte alignment, then
PSEUDO_HEADER_SIZE = sizeof(pseudo_msg_t) = 80.

[1] https://www.geeksforgeeks.org/c/flexible-array-members-structure-c/

Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
---
 pseudo_ipc.h | 1 +
 1 file changed, 1 insertion(+)

Comments

Mark Hatle Nov. 12, 2025, 5:46 p.m. UTC | #1
On 11/11/25 7:59 AM, Hongxu Jia wrote:
> typedef struct {
>      pseudo_msg_type_t type;
>      ...
>      char path[];
> } pseudo_msg_t;
> 
> The [] item at the end of the struct is a C99 feature:
> Flexible Array Members in a structure in C [1]. The size
> of path in structure should be 0.
> 
> On 64-bit arch, sizeof(pseudo_msg_t) = 80, but PSEUDO_HEADER_SIZE =
> offsetof(pseudo_msg_t, path) = 76, the reason is 8 byte alignment
> in 64-bit arch.
> 
> The differ between sizeof(pseudo_msg_t) and PSEUDO_HEADER_SIZE
> made memory overlab in memcpy, such as
> 
>    memcpy(oldmsg, msg, PSEUDO_HEADER_SIZE);
>    memcpy(newmsg, old, sizeof(pseudo_msg_t) + old->pathlen);
> 
> Add 4 chars to structure as reserved for 8 byte alignment, then
> PSEUDO_HEADER_SIZE = sizeof(pseudo_msg_t) = 80.
> 
> [1] https://www.geeksforgeeks.org/c/flexible-array-members-structure-c/
> 
> Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
> ---
>   pseudo_ipc.h | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/pseudo_ipc.h b/pseudo_ipc.h
> index d945257..2836cc4 100644
> --- a/pseudo_ipc.h
> +++ b/pseudo_ipc.h
> @@ -26,6 +26,7 @@ typedef struct {
>   	unsigned int pathlen;
>   	int nlink;
>   	int deleting;
> +	char reserved[4];
>   	char path[];
>   } pseudo_msg_t;
>   

I think it would be better to use the alignment operator instead, specifically:

         unsigned int pathlen;
         int nlink;
         int deleting;
         char path[];
   } __attribute__((aligned(8))) pseudo_msg_t;

In my testing this results in the same 80 byte length for both 64-bit and 32-bit systems.

I wrote a quick test program for the addressing:

64-bit:
Address: 0x4379d100
       type: 0x0
         op: 0x4
     result: 0x8
     access: 0xc
     client: 0x10
         fd: 0x14
        dev: 0x18
        ino: 0x20
        uid: 0x28
        gid: 0x2c
       mode: 0x30
       rdev: 0x38
    pathlen: 0x40
      nlink: 0x44
   deleting: 0x48
       path: 0x4c
Length: 80

32-bit:
Address: 0xffc2a828
       type: 0x0
         op: 0x4
     result: 0x8
     access: 0xc
     client: 0x10
         fd: 0x14
        dev: 0x18
        ino: 0x20
        uid: 0x28
        gid: 0x2c
       mode: 0x30
       rdev: 0x38
    pathlen: 0x40
      nlink: 0x44
   deleting: 0x48
       path: 0x4c
Length: 80

This seems to address the issue and the offsets within the structure are maintained as expected.

--Mark
Hongxu Jia Nov. 13, 2025, 11:16 a.m. UTC | #2
On 11/13/25 01:46, Mark Hatle wrote:
> CAUTION: This email comes from a non Wind River email account!
> Do not click links or open attachments unless you recognize the sender 
> and know the content is safe.
>
> On 11/11/25 7:59 AM, Hongxu Jia wrote:
>> typedef struct {
>>      pseudo_msg_type_t type;
>>      ...
>>      char path[];
>> } pseudo_msg_t;
>>
>> The [] item at the end of the struct is a C99 feature:
>> Flexible Array Members in a structure in C [1]. The size
>> of path in structure should be 0.
>>
>> On 64-bit arch, sizeof(pseudo_msg_t) = 80, but PSEUDO_HEADER_SIZE =
>> offsetof(pseudo_msg_t, path) = 76, the reason is 8 byte alignment
>> in 64-bit arch.
>>
>> The differ between sizeof(pseudo_msg_t) and PSEUDO_HEADER_SIZE
>> made memory overlab in memcpy, such as
>>
>>    memcpy(oldmsg, msg, PSEUDO_HEADER_SIZE);
>>    memcpy(newmsg, old, sizeof(pseudo_msg_t) + old->pathlen);
>>
>> Add 4 chars to structure as reserved for 8 byte alignment, then
>> PSEUDO_HEADER_SIZE = sizeof(pseudo_msg_t) = 80.
>>
>> [1] https://www.geeksforgeeks.org/c/flexible-array-members-structure-c/
>>
>> Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
>> ---
>>   pseudo_ipc.h | 1 +
>>   1 file changed, 1 insertion(+)
>>
>> diff --git a/pseudo_ipc.h b/pseudo_ipc.h
>> index d945257..2836cc4 100644
>> --- a/pseudo_ipc.h
>> +++ b/pseudo_ipc.h
>> @@ -26,6 +26,7 @@ typedef struct {
>>       unsigned int pathlen;
>>       int nlink;
>>       int deleting;
>> +     char reserved[4];
>>       char path[];
>>   } pseudo_msg_t;
>>
>
> I think it would be better to use the alignment operator instead, 
> specifically:
>
>         unsigned int pathlen;
>         int nlink;
>         int deleting;
>         char path[];
>   } __attribute__((aligned(8))) pseudo_msg_t;
>
The `__attribute__((aligned(8)))' still make PSEUDO_HEADER_SIZE = 
offsetof(pseudo_msg_t, path) = 72,

Pseudo use PSEUDO_HEADER_SIZE for ipc message send and receive

The PSEUDO_HEADER_SIZE is supposed to be equal the size of struct 
pseudo_msg_t, but sizeof(pseudo_msg_t) = 80 which is 16 byte alignment  
because of a long long member (16 byte) in struct pseudo_msg_t

//Hongxu




> In my testing this results in the same 80 byte length for both 64-bit 
> and 32-bit systems.
>
> I wrote a quick test program for the addressing:
>
> 64-bit:
> Address: 0x4379d100
>       type: 0x0
>         op: 0x4
>     result: 0x8
>     access: 0xc
>     client: 0x10
>         fd: 0x14
>        dev: 0x18
>        ino: 0x20
>        uid: 0x28
>        gid: 0x2c
>       mode: 0x30
>       rdev: 0x38
>    pathlen: 0x40
>      nlink: 0x44
>   deleting: 0x48
>       path: 0x4c
> Length: 80
>
> 32-bit:
> Address: 0xffc2a828
>       type: 0x0
>         op: 0x4
>     result: 0x8
>     access: 0xc
>     client: 0x10
>         fd: 0x14
>        dev: 0x18
>        ino: 0x20
>        uid: 0x28
>        gid: 0x2c
>       mode: 0x30
>       rdev: 0x38
>    pathlen: 0x40
>      nlink: 0x44
>   deleting: 0x48
>       path: 0x4c
> Length: 80
>
> This seems to address the issue and the offsets within the structure 
> are maintained as expected.
>
> --Mark
Hongxu Jia Nov. 13, 2025, noon UTC | #3
On 11/13/25 19:16, hongxu via lists.yoctoproject.org wrote:
> On 11/13/25 01:46, Mark Hatle wrote:
>> CAUTION: This email comes from a non Wind River email account!
>> Do not click links or open attachments unless you recognize the 
>> sender and know the content is safe.
>>
>> On 11/11/25 7:59 AM, Hongxu Jia wrote:
>>> typedef struct {
>>>      pseudo_msg_type_t type;
>>>      ...
>>>      char path[];
>>> } pseudo_msg_t;
>>>
>>> The [] item at the end of the struct is a C99 feature:
>>> Flexible Array Members in a structure in C [1]. The size
>>> of path in structure should be 0.
>>>
>>> On 64-bit arch, sizeof(pseudo_msg_t) = 80, but PSEUDO_HEADER_SIZE =
>>> offsetof(pseudo_msg_t, path) = 76, the reason is 8 byte alignment
>>> in 64-bit arch.
>>>
>>> The differ between sizeof(pseudo_msg_t) and PSEUDO_HEADER_SIZE
>>> made memory overlab in memcpy, such as
>>>
>>>    memcpy(oldmsg, msg, PSEUDO_HEADER_SIZE);
>>>    memcpy(newmsg, old, sizeof(pseudo_msg_t) + old->pathlen);
>>>
>>> Add 4 chars to structure as reserved for 8 byte alignment, then
>>> PSEUDO_HEADER_SIZE = sizeof(pseudo_msg_t) = 80.
>>>
>>> [1] https://www.geeksforgeeks.org/c/flexible-array-members-structure-c/
>>>
>>> Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
>>> ---
>>>   pseudo_ipc.h | 1 +
>>>   1 file changed, 1 insertion(+)
>>>
>>> diff --git a/pseudo_ipc.h b/pseudo_ipc.h
>>> index d945257..2836cc4 100644
>>> --- a/pseudo_ipc.h
>>> +++ b/pseudo_ipc.h
>>> @@ -26,6 +26,7 @@ typedef struct {
>>>       unsigned int pathlen;
>>>       int nlink;
>>>       int deleting;
>>> +     char reserved[4];
>>>       char path[];
>>>   } pseudo_msg_t;
>>>
>>
>> I think it would be better to use the alignment operator instead, 
>> specifically:
>>
>>         unsigned int pathlen;
>>         int nlink;
>>         int deleting;
>>         char path[];
>>   } __attribute__((aligned(8))) pseudo_msg_t;
>>
> The `__attribute__((aligned(8)))' still make PSEUDO_HEADER_SIZE = 
> offsetof(pseudo_msg_t, path) = 72,
>
> Pseudo use PSEUDO_HEADER_SIZE for ipc message send and receive
>
> The PSEUDO_HEADER_SIZE is supposed to be equal the size of struct 
> pseudo_msg_t, but sizeof(pseudo_msg_t) = 80 which is 16 byte 
> alignment  because of a long long member (16 byte) in struct pseudo_msg_t
>
Correct one thing,

The `__attribute__((aligned(8)))' still make PSEUDO_HEADER_SIZE = 
offsetof(pseudo_msg_t, path) = 76,

It is 8 byte alignment, because of the size of a long long member in 
struct pseudo_msg_t is 8 byte. Then 76 is aligned to 80

//Hongxu

> //Hongxu
>
>
>
>
>> In my testing this results in the same 80 byte length for both 64-bit 
>> and 32-bit systems.
>>
>> I wrote a quick test program for the addressing:
>>
>> 64-bit:
>> Address: 0x4379d100
>>       type: 0x0
>>         op: 0x4
>>     result: 0x8
>>     access: 0xc
>>     client: 0x10
>>         fd: 0x14
>>        dev: 0x18
>>        ino: 0x20
>>        uid: 0x28
>>        gid: 0x2c
>>       mode: 0x30
>>       rdev: 0x38
>>    pathlen: 0x40
>>      nlink: 0x44
>>   deleting: 0x48
>>       path: 0x4c
>> Length: 80
>>
>> 32-bit:
>> Address: 0xffc2a828
>>       type: 0x0
>>         op: 0x4
>>     result: 0x8
>>     access: 0xc
>>     client: 0x10
>>         fd: 0x14
>>        dev: 0x18
>>        ino: 0x20
>>        uid: 0x28
>>        gid: 0x2c
>>       mode: 0x30
>>       rdev: 0x38
>>    pathlen: 0x40
>>      nlink: 0x44
>>   deleting: 0x48
>>       path: 0x4c
>> Length: 80
>>
>> This seems to address the issue and the offsets within the structure 
>> are maintained as expected.
>>
>> --Mark
>
>
>
>
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#2502): 
> https://lists.yoctoproject.org/g/yocto-patches/message/2502
> Mute This Topic: https://lists.yoctoproject.org/mt/116238032/3617049
> Group Owner: yocto-patches+owner@lists.yoctoproject.org
> Unsubscribe: 
> https://lists.yoctoproject.org/g/yocto-patches/leave/13176071/3617049/43253763/xyzzy 
> [hongxu.jia@windriver.com]
> -=-=-=-=-=-=-=-=-=-=-=-
>
>
diff mbox series

Patch

diff --git a/pseudo_ipc.h b/pseudo_ipc.h
index d945257..2836cc4 100644
--- a/pseudo_ipc.h
+++ b/pseudo_ipc.h
@@ -26,6 +26,7 @@  typedef struct {
 	unsigned int pathlen;
 	int nlink;
 	int deleting;
+	char reserved[4];
 	char path[];
 } pseudo_msg_t;