[BUG] Potential bug when size of free Heap is too large and sets allocation-bit mask

未关闭 适合新手
#1,494 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
62/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
c

调研方向

从 portable/MemMang/heap_4.c 开始,先查看 heapBLOCK_ALLOCATED_BITMASK 和 heapBLOCK_SIZE_IS_VALID 的定义,然后阅读 prvHeapInit(),其中 pxFirstFreeBlock->xBlockSize 是根据 uxEndAddress 设置的。由于该 issue 提供了两种方案,请决定是对计算出的大小添加 configASSERT,还是只记录这一限制。完成标准是检查或说明已就位,并且仓库中的堆测试仍然通过。

由索引模型根据 Issue 内容生成。

描述

bug

Describe the bug
heap_4.c reserves the most significant bit of BlockLink_t.xBlockSize as an allocation-status flag:

#define heapBLOCK_ALLOCATED_BITMASK  \
 ( ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 ) )

prvHeapInit() does not explicitly validate the computed size of the initial free block:

pxFirstFreeBlock->xBlockSize = ( size_t ) ( uxEndAddress - ( portPOINTER_SIZE_TYPE ) pxFirstFreeBlock );

If an implementation permitted a heap whose usable size was greater than or equal to heapBLOCK_ALLOCATED_BITMASK (i.e. 0x8000000 in 32 bit systems), the initial free block would already have the allocation-status bit set as part of its size.

The allocator would then be unable to distinguish that size bit from the allocation flag.

Similarly, coalescing free blocks assumes that the resulting size remains below heapBLOCK_ALLOCATED_BITMASK. That assumption follows from the initial-heap-size, but that is not checked explicitly.

Target

  • Development board: N/A; source-level and compile-time investigation
  • Instruction Set Architecture: Armv7E-M/Thumb-2, 32-bit size_t
  • IDE and version: N/A; command-line Makefile
  • Toolchain and version: Arm GNU Toolchain 15.2.Rel1, GCC 15.2.1

Host

  • Host OS: Linux under WSL2
  • Version: Debian GNU/Linux 13 (trixie) Linux 6.18.33.2-microsoft-standard-WSL2, x86_64

To Reproduce

  1. Use portable/MemMang/heap_4.c from latest FreeRTOS-Kernel commit.
  2. Select a 32-bit Arm port where sizeof(size_t) == 4.
  3. Observe that bit 31 is reserved by heapBLOCK_ALLOCATED_BITMASK.
  4. Observe that prvHeapInit() assigns the initial free-block size without applying heapBLOCK_SIZE_IS_VALID() to the result.
  5. Attempt to configure a heap large enough for the initial usable block to be at least 0x80000000 bytes.

The concern would apply only to a compiler or nonstandard heap-placement mechanism that permits an object large enough to use the reserved
bit.

If tried using standard heap allocation

extern uint8_t ucHeap[ 0x80000000ULL ];

Would produce the following compiler output:

error: size of array 'ucHeap' is too large

Expected behavior
The size limitation should either be documented explicitly or checked by the implementation. For example, after calculating the initial free-block size, prvHeapInit() could assert the representation invariant:

configASSERT( heapBLOCK_SIZE_IS_VALID( pxFirstFreeBlock->xBlockSize ) != 0);                                                                                              

Screenshots
Not applicable

Additional context
On a target with 32-bit size_t:

heapBLOCK_ALLOCATED_BITMASK = 0x80000000
maximum representable block size without the flag = 0x7fffffff

The invalid condition is specifically when computed initial usable block size >= heapBLOCK_ALLOCATED_BITMASK

主要语言
C
星标
4.6k
派生
1.6k
平均合并
2 天 22 小时
30 天内合并 PR
4

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

FreeRTOS/FreeRTOS-Kernel 的其他 Issue

查看 FreeRTOS/FreeRTOS-Kernel 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。