Skip to content

Conversation

Rbb666
Copy link
Member

@Rbb666 Rbb666 commented Sep 1, 2025

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

[

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

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

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

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 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/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions github-actions bot added Kernel PR has src relate code testcase labels Sep 1, 2025
Copy link

github-actions bot commented Sep 1, 2025

📌 Code Review Assignment

🏷️ Tag: kernel

Reviewers: GorrayLi ReviewSun hamburger-os lianux-mm wdfk-prog xu18838022837

Changed Files (Click to expand)
  • src/scheduler_comm.c

📊 Current Review Status (Last Updated: 2025-09-01 18:40 CST)

  • GorrayLi Pending Review
  • ReviewSun Pending Review
  • hamburger-os Pending Review
  • lianux-mm Pending Review
  • wdfk-prog Pending Review
  • xu18838022837 Pending Review

📝 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.

@Rbb666 Rbb666 requested a review from supperthomas September 1, 2025 07:32
@Rbb666 Rbb666 force-pushed the th_overflow branch 3 times, most recently from 9c55da4 to bf9e122 Compare September 1, 2025 07:53
@illustriousness
Copy link
Contributor

lgtm

@Rbb666 Rbb666 changed the title [Add][kernel/thread]Add thread overflow hook and testcases:thread_overflow_tc.c. [add][kernel/thread]Add thread overflow hook and testcases:thread_overflow_tc.c. Sep 1, 2025
@supperthomas supperthomas requested a review from Copilot September 1, 2025 09:19
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 adds stack overflow hook functionality to RT-Thread's kernel, allowing developers to register custom callbacks when thread stack overflow is detected. The implementation provides a recovery mechanism that can prevent system halt when overflow is handled successfully.

Key changes include:

  • Stack overflow hook registration and callback mechanism in scheduler
  • Enhanced stack overflow detection with optional recovery
  • Comprehensive test suite covering hook functionality and stack checking

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
src/scheduler_comm.c Implements stack overflow hook registration and callback mechanism with recovery logic
include/rtthread.h Adds public API declaration for stack overflow hook registration
examples/utest/testcases/kernel/thread_overflow_tc.c Comprehensive test suite for stack overflow detection and hook functionality
examples/utest/testcases/kernel/SConscript Includes new test file in build system

@@ -411,6 +412,35 @@ rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t pri
}

#ifdef RT_USING_OVERFLOW_CHECK

#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
static rt_err_t (*rt_stack_overflow_hook)(struct rt_thread *thread);
Copy link
Preview

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

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

Naming/命名: The variable name rt_stack_overflow_hook should follow RT-Thread naming conventions for static variables. It should be prefixed with underscore.\n\nEnglish: Static function pointers should use the naming pattern _[class]_[action] according to RT-Thread coding standards.\n中文:静态函数指针应该遵循RT-Thread编码标准,使用_[class]_[action]命名模式。\n\nExample/示例:\nc\nstatic rt_err_t (*_scheduler_stack_overflow_hook)(struct rt_thread *thread);\n

Copilot generated this review using guidance from copilot-instructions.md.

Comment on lines 241 to +242
#ifdef RT_USING_HOOK
void rt_scheduler_stack_overflow_sethook(rt_err_t (*hook)(struct rt_thread *thread));
Copy link
Preview

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

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

API Design/API设计: The function declaration should be conditionally included only when hook functionality is available to maintain API consistency.\n\nEnglish: The API should be wrapped with the same conditional compilation guards as its implementation to prevent compilation errors when hooks are disabled.\n中文:API应该使用与其实现相同的条件编译保护,以防止在钩子功能禁用时出现编译错误。\n\nExample/示例:\nc\n#ifdef RT_USING_HOOK\n#ifdef RT_HOOK_USING_FUNC_PTR\nvoid rt_scheduler_stack_overflow_sethook(rt_err_t (*hook)(struct rt_thread *thread));\n#endif /* RT_HOOK_USING_FUNC_PTR */\n#endif /* RT_USING_HOOK */\n

Suggested change
#ifdef RT_USING_HOOK
void rt_scheduler_stack_overflow_sethook(rt_err_t (*hook)(struct rt_thread *thread));
#ifdef RT_USING_HOOK
#ifdef RT_HOOK_USING_FUNC_PTR
void rt_scheduler_stack_overflow_sethook(rt_err_t (*hook)(struct rt_thread *thread));
#endif /* RT_HOOK_USING_FUNC_PTR */

Copilot uses AI. Check for mistakes.

rt_kprintf("Corrupting fake thread stack with pattern 0x11...\n");
rt_memset(fake_thread->stack_addr, 0x11, fake_thread->stack_size);

/* Also corrupt the magic number area if stack checking is enabled */
Copy link
Preview

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

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

Security/安全性: Corrupting the entire stack including the stack pointer area could cause undefined behavior or crashes when the thread context is accessed.\n\nEnglish: Overwriting the entire stack can corrupt critical thread state. Consider only corrupting the magic number area or stack guard region to safely trigger overflow detection.\n中文:覆盖整个栈可能会破坏关键的线程状态。建议只破坏魔数区域或栈保护区域来安全地触发溢出检测。\n\nExample/示例:\nc\n/* Only corrupt the magic number area for safe testing */\n#ifdef ARCH_CPU_STACK_GROWS_UPWARD\nrt_memset((rt_uint8_t *)fake_thread->stack_addr + fake_thread->stack_size - 4, 0x11, 4);\n#else\nrt_memset(fake_thread->stack_addr, 0x11, 4);\n#endif\n

Suggested change
rt_kprintf("Corrupting fake thread stack with pattern 0x11...\n");
rt_memset(fake_thread->stack_addr, 0x11, fake_thread->stack_size);
/* Also corrupt the magic number area if stack checking is enabled */
rt_kprintf("Corrupting fake thread stack magic number area with pattern 0x11...\n");
/* Corrupt the magic number area if stack checking is enabled */

Copilot uses AI. Check for mistakes.


#ifdef RT_USING_OVERFLOW_CHECK
/* Call the RT-Thread stack check function */
extern void rt_scheduler_stack_check(struct rt_thread *thread);
Copy link
Preview

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

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

Best Practices/最佳实践: External function declarations should be placed at the top of the file or in a header file, not inside function scope.\n\nEnglish: Function declarations inside function scope can lead to maintenance issues and inconsistent prototypes. Move to file scope or include from appropriate header.\n中文:在函数作用域内的函数声明可能导致维护问题和不一致的原型。应移至文件作用域或从适当的头文件中包含。\n\nExample/示例:\nc\n/* At top of file */\nextern void rt_scheduler_stack_check(struct rt_thread *thread);\n\nstatic void manual_stack_check_test(void)\n{\n /* ... function body without extern declaration ... */\n}\n

Copilot uses AI. Check for mistakes.

rt_kprintf("Performing stack check on corrupted fake thread...\n");

#ifdef RT_USING_OVERFLOW_CHECK
extern void rt_scheduler_stack_check(struct rt_thread *thread);
Copy link
Preview

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

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

Best Practices/最佳实践: Duplicate external function declaration. This should be declared once at file scope rather than repeated in multiple functions.\n\nEnglish: Multiple extern declarations of the same function should be avoided. Declare once at file scope for consistency.\n中文:应该避免对同一函数进行多次extern声明。在文件作用域声明一次以保持一致性。

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Kernel PR has src relate code testcase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants