Ignore:
Timestamp:
2023-10-22T15:53:32Z (7 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
71b4444, abb70fc3
Parents:
f4a42661
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-10-22 15:39:53)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-10-22 15:53:32)
Message:

Remove some inappropriate uses of attribute((packed))

attribute((packed)) means "ignore all alignment requirements
on members of this structure". This is useful if one needs to
map onto a structure in memory that has misaligned fields on purpose,
but those cases are extremely rare.

The side effect of the attribute is that taking a pointer to any
field longer than a single byte, and dereferencing that pointer,
is unsound and may crash the program on architectures that care
about memory alignment when reading/writing multibyte values.

Newer GCC versions can detect some of those unsafe cases and
produce a warning for it. This commit only removes those cases.

However, most, if not all, uses of ((packed)) in HelenOS
are unnecessary and a product of misunderstanding what the
attribute actually does. A common misconception is that it is
needed to avoid compiler adding arbitrary padding into the
structure, but that is simply not true. There is exactly one
correct memory layout for any C structure, because there must
be one layout for binary interoperability to exist and the one
everyone uses (except perhaps some goblin who just wants to break
things for fun) is the trivial best layout possible with given
constraints.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/xhci/hw_struct/context.h

    rf4a42661 r590cb6d2  
    5353        xhci_dword_t data3;
    5454        xhci_dword_t reserved[3];
     55} xhci_ep_ctx_t;
    5556
    5657#define XHCI_EP_COUNT 31
     
    107108#define XHCI_EP_MAX_ESIT_PAYLOAD_HI(ctx) XHCI_DWORD_EXTRACT((ctx).data[0], 31, 24)
    108109
    109 } __attribute__((packed)) xhci_ep_ctx_t;
    110 
    111110enum {
    112111        EP_STATE_DISABLED = 0,
     
    123122        xhci_dword_t data [4];
    124123        xhci_dword_t reserved [4];
     124} xhci_slot_ctx_t;
    125125
    126126#define XHCI_SLOT_ROUTE_STRING_SET(ctx, val) \
     
    165165#define XHCI_SLOT_STATE(ctx)            XHCI_DWORD_EXTRACT((ctx).data[3], 31, 27)
    166166
    167 } __attribute__((packed)) xhci_slot_ctx_t;
    168 
    169167enum {
    170168        SLOT_STATE_DISABLED = 0,
     
    213211typedef struct xhci_stream_ctx {
    214212        uint64_t data [2];
     213} xhci_stream_ctx_t;
     214
    215215#define XHCI_STREAM_DCS(ctx)     XHCI_QWORD_EXTRACT((ctx).data[0],  0, 0)
    216216#define XHCI_STREAM_SCT(ctx)     XHCI_QWORD_EXTRACT((ctx).data[0],  3, 1)
     
    222222#define XHCI_STREAM_DEQ_PTR_SET(ctx, val) \
    223223        xhci_qword_set_bits(&(ctx).data[0], (val >> 4), 63, 4)
    224 } __attribute__((packed)) xhci_stream_ctx_t;
    225224
    226225/**
     
    234233typedef struct xhci_input_ctrl_ctx {
    235234        uint32_t data [8];
     235} __attribute__((packed)) xhci_input_ctrl_ctx_t;
     236
    236237#define XHCI_INPUT_CTRL_CTX_DROP(ctx, idx) \
    237238    XHCI_DWORD_EXTRACT((ctx).data[0], (idx), (idx))
     
    252253#define XHCI_INPUT_CTRL_CTX_ALTER_SETTING(ctx) \
    253254    XHCI_DWORD_EXTRACT((ctx).data[7], 23, 16)
    254 } __attribute__((packed)) xhci_input_ctrl_ctx_t;
    255255
    256256/**
     
    281281        uint8_t reserved;
    282282        uint8_t ports [];
    283 } __attribute__((packed)) xhci_port_bandwidth_ctx_t;
     283} xhci_port_bandwidth_ctx_t;
    284284
    285285#endif
Note: See TracChangeset for help on using the changeset viewer.