[add][kernel/thread]Add thread overflow hook and testcases:thread_overflow_tc.c.#10655
[add][kernel/thread]Add thread overflow hook and testcases:thread_overflow_tc.c.#10655Rbb666 merged 1 commit intoRT-Thread:masterfrom
Conversation
📌 Code Review Assignment🏷️ Tag: kernelReviewers: GorrayLi ReviewSun hamburger-os lianux-mm wdfk-prog xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2025-09-02 12:11 CST)
📝 Review Instructions
|
9c55da4 to
bf9e122
Compare
|
lgtm |
There was a problem hiding this comment.
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 |
| #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); |
There was a problem hiding this comment.
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
| #ifdef RT_USING_HOOK | ||
| void rt_scheduler_stack_overflow_sethook(rt_err_t (*hook)(struct rt_thread *thread)); |
There was a problem hiding this comment.
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
| #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 */ |
| 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 */ |
There was a problem hiding this comment.
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
| 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 */ |
|
|
||
| #ifdef RT_USING_OVERFLOW_CHECK | ||
| /* Call the RT-Thread stack check function */ | ||
| extern void rt_scheduler_stack_check(struct rt_thread *thread); |
There was a problem hiding this comment.
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
| 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); |
There was a problem hiding this comment.
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声明。在文件作用域声明一次以保持一致性。
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
你的解决方案是什么 (what is your solution)
请提供验证的bsp和config (provide the config and bsp)
]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up