Skip to content
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

apps/nshlib: Using lib_get_tempbuffer() to save stack space #2943

Conversation

JianyuWang0623
Copy link
Contributor

Summary

Using lib_get_tempbuffer() to save stack space

  1. apps/nshlib: Using lib_get_tempbuffer() to save stack space
  2. apps/nshlib: replace CONFIG_NSH_LINELEN to LINE_MAX
  3. apps/system: replace CONFIG_NSH_LINELEN to LINE_MAX (Picked from apps/system: replace CONFIG_NSH_LINELEN to LINE_MAX #2918 by @anchao )

Impact

  • apps/nshlib
  • apps/system

Testing

  1. Selftest as commit message shown
    • Config: "esp32s3-devkit:adb" with CONFIG_LINE_MAX=512

    • Test CMD: ls | cat | cat | cat

    • Without this patch

      Before Test CMD After Test CMD
      nsh_main.STACK 0002624/0008096 0002624/0008096
      sh.STACK 0003360/0004008 0003360/0004008
      Free/Total 355288/403116 355288/403116
    • With this patch

      Before Test CMD After Test CMD
      nsh_main.STACK 0001616/0008096 0001616/0008096
      sh.STACK 0002352/0004008 0002352/0004008
      Free/Total 355288/403116 354760/403116
  2. CI

anchao and others added 2 commits January 14, 2025 10:25
Applications should not depend on any properties of nshlib

Signed-off-by: chao an <[email protected]>
@nuttxpr
Copy link

nuttxpr commented Jan 14, 2025

[Experimental Bot, please feedback here]

This PR description mostly meets the NuttX requirements but could be improved. Here's a breakdown:

Strengths:

  • Clear Summary of Changes: The summary clearly states what was changed and why. The use of bullet points and references to related issues/PRs is good.
  • Impact Section Started: Addressing the impact is started, but needs more detail.
  • Testing Information Provided: The testing section includes the host, target, config, commands used, and results. This is excellent.

Weaknesses:

  • Summary Lacks "How": While the what is clear, the summary doesn't fully explain how lib_get_tempbuffer() saves stack space. Explain the mechanism briefly (e.g., it allocates buffers from the heap instead of the stack).
  • Incomplete Impact Assessment: The Impact section is too brief. It lists affected components but doesn't fully address all the required points. Every "YES" needs a description. For each "NO", explicitly state "NO". Example for "Impact on user": "YES, users of NSH will see slightly reduced heap space availability but improved stack usage." (Even if seemingly minor, describe it).
  • Missing "Before" and "After" Logs in Testing Section: The tables show the stack usage before and after the test command, but the actual console output/logs from the tests are missing.
  • Unclear CI Status: Mentioning "CI" is insufficient. State whether CI passed or provide a link to the CI run.

Suggested Improvements:

Summary

  • Why: NSH commands often use large buffers on the stack, potentially leading to stack overflows, especially with deeply nested pipelines. Using lib_get_tempbuffer() allocates these buffers from the heap, reducing stack usage and mitigating this risk.
  • What: This change modifies the NSH library and related applications to use lib_get_tempbuffer() for buffer allocation. It also replaces CONFIG_NSH_LINELEN with LINE_MAX for consistency.
  • How: Instead of declaring large character arrays on the stack, lib_get_tempbuffer() is used to dynamically allocate memory from the heap for these buffers. This memory is then freed when no longer needed.
  • Related Issues/PRs: (List any related issues/PRs)
  • apps/nshlib: Using lib_get_tempbuffer() to save stack space
  • **apps/nshlib: replace CONFIG_NSH_LINELEN to LINE_MAX
  • **apps/system: replace CONFIG_NSH_LINELEN to LINE_MAX (Picked from apps/system: replace CONFIG_NSH_LINELEN to LINE_MAX #2918 by @anchao )

Impact

  • New Feature Added: NO
  • Existing Feature Changed: YES, NSH buffer management.
  • Impact on User: YES, users of NSH will see slightly reduced heap space availability due to dynamic buffer allocation, but significantly improved stack safety, reducing the risk of stack overflows.
  • Impact on Build: NO
  • Impact on Hardware: NO
  • Impact on Documentation: YES, documentation should be updated to reflect the use of lib_get_tempbuffer() and the change from CONFIG_NSH_LINELEN to LINE_MAX.
  • Impact on Security: YES, This change improves security by reducing the risk of stack overflow vulnerabilities.
  • Impact on Compatibility: NO
  • Anything else to consider: None

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): OS: Linux, CPU: Intel, Compiler: GCC (Provide version)
  • Target(s): arch: sim (esp32s3-devkit:adb), board: esp32s3-devkit, config: adb, CONFIG_LINE_MAX=512

Testing logs before change:

# Actual command output and any relevant messages here, not just stack usage.
ls
...output...
cat | cat | cat
...output...
free
...output... # Showing heap usage before

Testing logs after change:

# Actual command output and any relevant messages here
ls
...output...
cat | cat | cat
...output...
free
...output... # Showing heap usage after

Stack Usage Comparison:

(Include your tables as before)

CI Status: [Link to CI run] or "CI passed successfully."

By making these improvements, the PR description will be clearer, more complete, and better aligned with the NuttX requirements. This will make it easier for reviewers to understand and approve the changes.

@JianyuWang0623
Copy link
Contributor Author

@xiaoxiang781216 @anchao Could you review this PR please?

@JianyuWang0623
Copy link
Contributor Author

JianyuWang0623 commented Jan 14, 2025

TODO: Remove NSH_LINELEN from Kconfig after this PR was merged

  1. Replace NSH_LINELEN with LINE_MAX for nuttx/configs -- boards: Replace CONFIG_NSH_LINELEN with CONFIG_LINE_MAX nuttx#15541
  2. Remove NSH_LINELEN from apps/nshlib/Kconfig -- apps/nshlib: Remove the deprecated config NSH_LINELEN #2945

@JianyuWang0623 JianyuWang0623 force-pushed the br_wjy_nshlib_using_LINE_MAX_save_stack_250114_apache branch from c97e003 to 07f57b3 Compare January 14, 2025 04:28
Copy link

@cederom cederom left a comment

Choose a reason for hiding this comment

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

Thank you @JianyuWang0623 amazing work squeezing every bit and byte out of hw :-)

Lets just wait for the CI to complete test builds :-)

nshlib/nsh_parse.c Outdated Show resolved Hide resolved
nshlib/nsh_parse.c Outdated Show resolved Hide resolved
nshlib/nsh_parse.c Outdated Show resolved Hide resolved
nshlib/nsh_parse.c Outdated Show resolved Hide resolved
Comparison

  Config: "esp32s3-devkit:adb" with `CONFIG_LINE_MAX=512`

  Test CMD: `ls | cat | cat | cat`

  Without this patch

    |                | Before Test CMD | After Test CMD  |
    |---------------:|----------------:|----------------:|
    | nsh_main.STACK | 0002624/0008096 | 0002624/0008096 |
    |       sh.STACK | 0003360/0004008 | 0003360/0004008 |
    |     Free/Total |   355288/403116 |   355288/403116 |

  With this patch

    |                | Before Test CMD | After Test CMD  |
    |---------------:|----------------:|----------------:|
    | nsh_main.STACK | 0001616/0008096 | 0001616/0008096 |
    |       sh.STACK | 0002352/0004008 | 0002352/0004008 |
    |     Free/Total |   355288/403116 |   354760/403116 |

Signed-off-by: wangjianyu3 <[email protected]>
@JianyuWang0623 JianyuWang0623 force-pushed the br_wjy_nshlib_using_LINE_MAX_save_stack_250114_apache branch from 07f57b3 to a2c74cf Compare January 14, 2025 07:01
@xiaoxiang781216 xiaoxiang781216 merged commit a3049aa into apache:master Jan 14, 2025
37 checks passed
JianyuWang0623 added a commit to JianyuWang0623/nuttx that referenced this pull request Jan 14, 2025
JianyuWang0623 added a commit to JianyuWang0623/nuttx that referenced this pull request Jan 14, 2025
Details: apache/nuttx-apps#2943

1. find boards/ -name defconfig | xargs sed -i 's/CONFIG_NSH_LINELEN/CONFIG_LINE_MAX/g'
2. ./tools/refresh.sh --silent --defaults all

Signed-off-by: wangjianyu3 <[email protected]>
xiaoxiang781216 pushed a commit to apache/nuttx that referenced this pull request Jan 14, 2025
Details: apache/nuttx-apps#2943

1. find boards/ -name defconfig | xargs sed -i 's/CONFIG_NSH_LINELEN/CONFIG_LINE_MAX/g'
2. ./tools/refresh.sh --silent --defaults all

Signed-off-by: wangjianyu3 <[email protected]>
JianyuWang0623 added a commit to JianyuWang0623/nuttx-apps that referenced this pull request Jan 15, 2025
JianyuWang0623 added a commit to JianyuWang0623/nuttx that referenced this pull request Jan 15, 2025
JianyuWang0623 added a commit to JianyuWang0623/nuttx-apps that referenced this pull request Jan 15, 2025
NSH_LINELEN is replaced by POSIX standard LINE_MAX.

apache/nuttx#15541
apache#2943

Signed-off-by: wangjianyu3 <[email protected]>
linguini1 pushed a commit to linguini1/nuttx that referenced this pull request Jan 16, 2025
Details: apache/nuttx-apps#2943

1. find boards/ -name defconfig | xargs sed -i 's/CONFIG_NSH_LINELEN/CONFIG_LINE_MAX/g'
2. ./tools/refresh.sh --silent --defaults all

Signed-off-by: wangjianyu3 <[email protected]>
xiaoxiang781216 pushed a commit that referenced this pull request Jan 16, 2025
NSH_LINELEN is replaced by POSIX standard LINE_MAX.

apache/nuttx#15541
#2943

Signed-off-by: wangjianyu3 <[email protected]>
JianyuWang0623 added a commit to JianyuWang0623/openvela-nuttx that referenced this pull request Jan 16, 2025
Details: apache/nuttx-apps#2943

1. find boards/ -name defconfig | xargs sed -i 's/CONFIG_NSH_LINELEN/CONFIG_LINE_MAX/g'
2. ./tools/refresh.sh --silent --defaults all

Signed-off-by: wangjianyu3 <[email protected]>
JianyuWang0623 added a commit to JianyuWang0623/openvela-nuttx-apps that referenced this pull request Jan 16, 2025
NSH_LINELEN is replaced by POSIX standard LINE_MAX.

apache/nuttx#15541
apache/nuttx-apps#2943

Signed-off-by: wangjianyu3 <[email protected]>
xiaoxiang781216 pushed a commit to open-vela/nuttx that referenced this pull request Jan 17, 2025
Details: apache/nuttx-apps#2943

1. find boards/ -name defconfig | xargs sed -i 's/CONFIG_NSH_LINELEN/CONFIG_LINE_MAX/g'
2. ./tools/refresh.sh --silent --defaults all

Signed-off-by: wangjianyu3 <[email protected]>
JianyuWang0623 added a commit to JianyuWang0623/openvela-nuttx-apps that referenced this pull request Jan 18, 2025
NSH_LINELEN is replaced by POSIX standard LINE_MAX.

apache/nuttx#15541
apache/nuttx-apps#2943

Signed-off-by: wangjianyu3 <[email protected]>
xiaoxiang781216 pushed a commit to open-vela/nuttx-apps that referenced this pull request Jan 18, 2025
NSH_LINELEN is replaced by POSIX standard LINE_MAX.

apache/nuttx#15541
apache/nuttx-apps#2943

Signed-off-by: wangjianyu3 <[email protected]>
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.

5 participants