Files
swift-mirror/stdlib/public/CommandLineSupport/CommandLine.cpp
Alastair Houghton 04c496c1e9 [CommandLine][Linux] Don't read the command line from /proc/self/cmdline.
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
find `argc`, at which point we also know where `argv` is.

We do this from a static initializer because a `setenv()` or `putenv()`
can change `environ` (if you add a new environment variable), and it's
even permissible to just outright change `environ` 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 find `argv`.

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 with `environ`
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
2024-02-14 17:32:47 +00:00

16 KiB