Skip to content

add static stack usage analysis script#257

Draft
xvuko wants to merge 1 commit into
masterfrom
xvuko/stack-usage
Draft

add static stack usage analysis script#257
xvuko wants to merge 1 commit into
masterfrom
xvuko/stack-usage

Conversation

@xvuko
Copy link
Copy Markdown

@xvuko xvuko commented Mar 18, 2026

Description

Motivation and Context

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: (list targets here).

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

  • This PR needs additional PRs to work (list the PRs, preferably in merge-order).
  • I will merge this PR by myself when appropriate.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new static stack usage analysis tool designed to help identify potential stack overflows in embedded systems. The tool analyzes ELF files, allowing for detailed configuration of thread entry points, stack limits, and complex call graph scenarios. It provides both command-line output and a web-based visualization via puncover, offering a comprehensive view of stack consumption and highlighting areas of concern.

Highlights

  • New Static Stack Usage Analysis Tool: Introduced a new Python script for static stack usage analysis, leveraging puncover to inspect ELF files.
  • Comprehensive Documentation: Added a detailed README.md explaining the tool's usage, configuration options, and useful naming conventions for effective analysis.
  • Configurable Stack Limit Definitions: Enabled flexible definition of thread stack limits, supporting direct values, expressions, GNU_STACK detection, and symbol-size based limits.
  • Advanced Call Graph Handling: Implemented mechanisms to manually define extra-calls for function pointers and ignored-functions to refine call graph analysis.
  • Output and Visualization Options: Provided options for command-line output, JSON results for programmatic integration, and a web-based visualization via puncover.
  • Orphan Function Detection: Included functionality to identify and warn about unreachable functions that are not part of any call chain from defined entry points.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a valuable static stack usage analysis script. The implementation is well-structured and includes helpful documentation. My review highlights a critical bug in parsing ELF file information that would cause the script to fail, along with suggestions for performance improvements and several corrections to the README file for better clarity and accuracy.

Comment thread stack-usage/analyze.py
Comment on lines +33 to +40
for line in raw.splitlines():
if "GNU_STACK" not in line:
continue

m = re.fullmatch(" +GNU_STACK +(0x[0-9a-fA-F]+ ){5}.*", line)
assert m
stack_size = int(m.group(1).rstrip(), 16)
return stack_size
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The regex used to parse the readelf output is incorrect and will fail. The GNU_STACK program header line contains non-hexadecimal fields (e.g., RWE), which will cause the re.fullmatch to fail and trigger the assertion on line 38. A more robust approach is to split the line and parse the columns to extract the MemSiz value.

Suggested change
for line in raw.splitlines():
if "GNU_STACK" not in line:
continue
m = re.fullmatch(" +GNU_STACK +(0x[0-9a-fA-F]+ ){5}.*", line)
assert m
stack_size = int(m.group(1).rstrip(), 16)
return stack_size
for line in raw.splitlines():
if "GNU_STACK" in line:
parts = line.split()
if len(parts) > 5:
try:
stack_size = int(parts[5], 16)
return stack_size
except ValueError:
pass

Comment thread stack-usage/README.md

## Useful naming conventions

There are some naming / coding pracitces that can help to define and keep config up to date.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

There is a typo in this sentence. pracitces should be practices.

Suggested change
There are some naming / coding pracitces that can help to define and keep config up to date.
There are some naming / coding practices that can help to define and keep config up to date.

Comment thread stack-usage/README.md
Comment on lines +32 to +38
void * module_nameThread(void *arg) {
return NULL
}

void module_init(void) {
beginthread(module_nameThread, 4, module_nameStack, sizeof(module_nameStack), NULL)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The C code example is missing semicolons at the end of the return statement on line 33 and the beginthread call on line 37.

Suggested change
void * module_nameThread(void *arg) {
return NULL
}
void module_init(void) {
beginthread(module_nameThread, 4, module_nameStack, sizeof(module_nameStack), NULL)
}
void * module_nameThread(void *arg) {
return NULL;
}
void module_init(void) {
beginthread(module_nameThread, 4, module_nameStack, sizeof(module_nameStack), NULL);
}

Comment thread stack-usage/README.md
Comment on lines +72 to +82
page-size
: Platform specyfic page size used for stack size definitions (default `_start` stack size in phoenix is 3 * page-size)

reserve
: Additional bytes reserved on stack

src-base
: Path to source code root relative to config file. This is used as base for referencing symbols from specyfic files

src-base
: Path to source code root relative to config file. This is used as base for referencing symbols from specyfic files
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

There are a couple of issues in this documentation section:

  1. The src-base option is defined twice (lines 78-79 and 81-82). The duplicate should be removed.
  2. There is a typo: specyfic should be specific. This occurs on lines 73, 79, and 82.

Comment thread stack-usage/analyze.py
def enhance_call_tree(self):
super().enhance_call_tree()

errors = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The errors variable is incremented when a symbol resolution fails, but its value is never used. To avoid silently ignoring unresolved symbols, consider logging the total number of errors at the end of this method or raising an exception if any errors occurred.

Comment thread stack-usage/analyze.py
children.append(func)

while children:
symbol = children.pop(0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using list.pop(0) for a queue is inefficient due to its O(n) time complexity. For better performance, especially with large call graphs, you should use collections.deque, which provides an O(1) popleft() operation.

To implement this, you would need to:

  1. Import deque at the top of the file: from collections import deque
  2. Initialize children as a deque on line 127: children = deque(c.symbols_by_name[name] for name in entry_points)
  3. Use popleft() here: symbol = children.popleft()

@github-actions
Copy link
Copy Markdown

Unit Test Results

9 553 tests  ±0   8 961 ✅ ±0   53m 0s ⏱️ +19s
  591 suites ±0     592 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit 28e6bc3. ± Comparison against base commit c855109.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant