Skip to content

feat: add snprintf/sprintf hook#1210

Open
codesensei-tushar wants to merge 4 commits intomandiant:masterfrom
codesensei-tushar:feat/hook-snprintf
Open

feat: add snprintf/sprintf hook#1210
codesensei-tushar wants to merge 4 commits intomandiant:masterfrom
codesensei-tushar:feat/hook-snprintf

Conversation

@codesensei-tushar
Copy link
Copy Markdown

Add SnprintfHook implementation

Fixes #318

Changes

  • Implement snprintf/sprintf hook using Python's native % formatting
  • Handle pointer dereferencing for %s format specifiers by reading strings from emulator memory
  • Support signed/unsigned integer conversion for %d, %i, %u, %x, %X, %o
  • Support width and precision modifiers (e.g., %10s, %.5s, %08x)
  • Handle both sprintf(buf, fmt, ...) and snprintf(buf, size, fmt, ...) variants

Testing

Tested with this sample C file: test_snprintf_comprehensive.c

@google-cla
Copy link
Copy Markdown

google-cla bot commented Jan 27, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Comment thread floss/api_hooks.py
fu.call_return(emu, api, argv, ptr)
return True

class SnprintfHook:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please add some tests that demonstrate this routine works the way we think it does

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the review! I'd like to clarify what kind of tests you're looking for:

  • Unit tests - pytest tests that mock the emulator and verify SnprintfHook._prepare_args() and .call() work correctly with various format specifiers.

  • Integration tests - Tests that run FLOSS on an actual binary (like the C test file I shared) and verify decoded strings are extracted.

@codesensei-tushar
Copy link
Copy Markdown
Author

Added tests/test_snprintf_hooks.py in the latest commit!
All 11 tests passing. Let me know if you'd like any additional test cases!

Copy link
Copy Markdown
Collaborator

@williballenthin williballenthin 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 @codesensei-tushar!

the unit tests look good

@codesensei-tushar
Copy link
Copy Markdown
Author

I did run black previously, but it might not have run properly. In the latest commit, I've verified that all pre-commit checks:

The mypy check was failing due to a pre-existing issue in floss/utils.py (unrelated to the SnprintfHook changes). The error was a false positive — the code is correct, but mypy couldn't verify it statically due to the dynamic **kwargs pattern used for platform-specific mmap arguments:
Without a type annotation, mypy sees **kwargs and can't determine whether it contains valid arguments for mmap, so it flags it as an error. I added # type: ignore[arg-type] to suppress this false positive.

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.

apicall hook snprintf function and variants

2 participants