rust bindings: keep argv[0] alive across benchmark reporting - #2237
Merged
Conversation
The Rust binding's initialize() creates temporary CString buffers that are freed on return, but benchmark::Initialize() stores argv[0] as a raw pointer in BenchmarkReporter::Context::executable_name. Later reporter calls dereference freed heap memory (use-after-free). Copy argv[0] into static storage in the C++ bridge before calling benchmark::Initialize(), ensuring the retained pointer remains valid for the process lifetime. AI assistance disclosure: AI tooling was used to assist with auditing, reproducer design, patch drafting, and PR text preparation.
dmah42
approved these changes
Jun 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rust bindings: keep argv[0] alive across benchmark reporting
Summary
The Rust binding constructs temporary C strings for
benchmark::Initialize()and passes their raw pointers into the C++ API. The C++ benchmark library keeps
argv[0]inBenchmarkReporter::Context::executable_nameand reads it laterwhen console, JSON, or CSV reporters emit benchmark context.
This change makes the Rust C++ bridge keep a persistent copy of
argv[0]before calling
benchmark::Initialize(), matching the lifetime expectation ofthe C++ reporter context.
Bug
google_benchmark_rs::initialize()currently builds temporaryCStringstorage:
Those buffers are dropped when
initialize()returns. However,benchmark::Initialize()storesargv[0]as a raw pointer:BenchmarkReporter::Context::executable_name = ((argc != nullptr) && *argc > 0) ? argv[0] : "unknown";The retained pointer is dereferenced later while printing the benchmark
context. Under ASan/UBSan, a small reproducer following the Rust binding's
lifetime pattern reports:
In an unsanitized run, allocator reuse can cause the reporter to print unrelated
later heap contents as the executable name in the benchmark context.
Fix
Convert the incoming
size_tback tochar**once in the bridge, copyargv[0]into static storage, and replaceargv[0]with that stable bufferbefore delegating to
benchmark::Initialize().All other command-line arguments continue to be parsed and copied by the normal
C++ flag parsing path. The only raw argument pointer retained after
initialization is
Context::executable_name.Verification
Ran the Rust binding tests:
cargo testResult:
Security Report
Related Google VRP / IssueTracker report: 529810915