[CommandLine][Linux] Don't read argv from /proc/self/cmdline (take 2). #71781
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.
Instead of reading from
/proc/self/cmdline
, take advantage of the fact that the initial stack layout is ABI specified, and that we already have a pointer into it (environ
). This lets us walk up the stack until we findargc
, at which point we also know whereargv
is.We do this from a static initializer because a
setenv()
orputenv()
can changeenviron
(if you add a new environment variable), and it's even permissible to just outright changeenviron
yourself too. It seems reasonable to suggest to people that they shouldn't be doing those things from a static initializer, and as long as they don't, they won't run before we've had a chance to findargv
.Just in case someone does do this, we also check that
environ
points into the stack. If it doesn't, they won't get any arguments, so if that happens, that's a clue that they're messing withenviron
too early.This works around a problem (#69658) with Docker Desktop 4.25.0 and Rosetta, wherein we end up with an extra argument visible in
/proc/self/cmdline
, and also avoids allocating memory for the command line arguments.rdar://117963394