Skip to content

[arm_compiler_v4/v5] define rt_packed as __packed #10553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2025

Conversation

cmbjxxiao
Copy link
Contributor

@cmbjxxiao cmbjxxiao commented Jul 28, 2025

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

使用MDK5编译器编译工程后,在运行find_valid_gpt函数时, 因sizeof (legacy_mbr)得出的长度未按1字节对齐而出现内存覆盖问题

你的解决方案是什么 (what is your solution)

1、当 __ARMCC_VERSION < 6010050 时定义 rt_packed 为 __packed
2、参考文档:
1)ARM Compiler toolchain Using the Compiler Version 5.01
https://developer.arm.com/documentation/dui0472/g/compiler-coding-practices/the---packed-qualifier-and-unaligned-data-access-in-c-and-c---code?lang=en
2)ARM Compiler toolchain Using the Compiler Version 4.1
https://developer.arm.com/documentation/dui0472/c/compiler-coding-practices/the---packed-qualifier-and-unaligned-data-access-in-c-and-c---code?lang=en
3)ARM Software Development Toolkit Reference Guide Version 2.50
https://developer.arm.com/documentation/dui0041/c/ARM-Compiler-Reference/Compiler-specific-features/Type-qualifiers?lang=en

请提供验证的bsp和config (provide the config and bsp)

  • BSP:qemu-vexpress-a9
  • .config:CONFIG_RT_USING_DFS=y,CONFIG_RT_USING_BLK=y,CONFIG_RT_BLK_PARTITION_EFI=y,CONFIG_BSP_USING_SDIO=y,CONFIG_BSP_USING_SDIO1=y

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/workflows/bsp_buildings.yml 详细请参考链接BSP自查

Copy link

github-actions bot commented Jul 28, 2025

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/drivers/block/partitions/efi.h
  • components/drivers/include/drivers/nvme.h

📊 Current Review Status (Last Updated: 2025-08-06 10:27 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@illustriousness
Copy link
Contributor

The __packed qualifier sets the alignment of any valid type to 1.
看起来不太对啊 这个宏我猜想本意是想支持任意字节紧凑对齐的(虽然大家常用的基本只是1字节)
据我了解 ac5任意字节对齐的方式可以是下面这样

#pragma pack(2)  // 强制后续所有结构体按 2 字节对齐

typedef struct {
    uint8_t  a;    // 偏移 0
    uint16_t b;    // 偏移 2 (自动跳过 1 字节)
    uint32_t c;    // 偏移 4
} MyStruct;        // 总大小 = 8 字节

#pragma pack()     // 恢复默认对齐

@illustriousness
Copy link
Contributor

illustriousness commented Aug 1, 2025

The __packed qualifier sets the alignment of any valid type to 1. 看起来不太对啊 这个宏我猜想本意是想支持任意字节紧凑对齐的(虽然大家常用的基本只是1字节) 据我了解 ac5任意字节对齐的方式可以是下面这样

#pragma pack(2)  // 强制后续所有结构体按 2 字节对齐

typedef struct {
    uint8_t  a;    // 偏移 0
    uint16_t b;    // 偏移 2 (自动跳过 1 字节)
    uint32_t c;    // 偏移 4
} MyStruct;        // 总大小 = 8 字节

#pragma pack()     // 恢复默认对齐

抱歉 我的理解可能有误 似乎这个宏只是为了1字节对齐 如果是这样的话 当前这个是可以的

#define rt_packed(declare)          __packed declare

不过建议一个PR只改一个 我看你还修改了其他文件

@Rbb666 Rbb666 requested a review from Copilot August 5, 2025 10:57
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes memory alignment issues in ARM Compiler v4/v5 by properly defining the rt_packed macro to use __packed instead of an empty definition. The change addresses memory corruption problems that occur when using MDK5 compiler due to improper byte alignment in data structures.

  • Updates rt_packed macro definition for ARM Compiler versions below 6.01.05 to use __packed
  • Fixes existing struct definitions to use the corrected packed attribute syntax
  • Resolves memory corruption issue in the find_valid_gpt function caused by incorrect sizeof calculations

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
include/rtcompiler.h Updates rt_packed macro to use __packed for ARM Compiler v4/v5
components/drivers/include/drivers/nvme.h Converts struct definition to use rt_packed macro correctly
components/drivers/block/partitions/efi.h Adds packed attribute to guid_t typedef struct
Comments suppressed due to low confidence (1)

components/drivers/block/partitions/efi.h:33

  • The syntax 'typedef struct rt_packed' is incorrect. Based on the rt_packed macro definition change, this should be 'rt_packed(typedef struct' with a corresponding closing parenthesis, or use 'typedef rt_packed(struct' to match the pattern used in nvme.h.
typedef struct rt_packed

@Maihuanyi
Copy link
Contributor

LGTM

@Rbb666 Rbb666 merged commit 37d9025 into RT-Thread:master Aug 6, 2025
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants