Skip to content

[CommandLine][Linux] Don't read argv from /proc/self/cmdline (take 2). #71781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2024

Conversation

al45tair
Copy link
Contributor

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

In `/proc/self/maps`, the stack isn't necessarily a single range, even
immediately after process start-up.  Apparently sometimes it's possible
for the kernel to allocate extra ranges below the first one, and these
show up separately in `/proc/self/maps`.

The upshot was that we were finding that `environ` didn't point into
the stack range we found, and then returning no arguments as a result.

The fix is to merge contiguous ranges after the first stack range we
find in `/proc/self/maps`.

rdar://117963394
@al45tair al45tair requested a review from a team as a code owner February 21, 2024 14:32
@al45tair
Copy link
Contributor Author

@swift-ci Please smoke test

@al45tair
Copy link
Contributor Author

@swift-ci Please test Linux platform

@al45tair al45tair requested a review from mikeash February 21, 2024 14:32
@al45tair
Copy link
Contributor Author

Original PR was #71611. This is a replacement for that, with a bug fix.

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.

2 participants