Skip to content

[unix] Don't clone command-line args on startup #47165

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 1 commit into from
Jan 6, 2018
Merged

Conversation

mbrubeck
Copy link
Contributor

@mbrubeck mbrubeck commented Jan 3, 2018

Fixes part of #47164 and simplifies the args code on non-Apple Unix platforms.

Note: This could change behavior for programs that use both std::env::args and unsafe code that mutates argv directly. However, these programs already behave differently on different platforms. The new behavior on non-Apple platforms is closer to the existing behavior on Apple platforms.

@rust-highfive
Copy link
Contributor

r? @shepmaster

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton alexcrichton added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 4, 2018
@alexcrichton
Copy link
Member

Thanks! I think though this may run a risk of memory unsafety? There can be a brief moment after the main thread has returned that other threads could try to access env::args but after main has returned they may become invalid.

I'm not sure if there's a way around that other than cloning on startup. Does cloning on startup cause problems though?

@mbrubeck
Copy link
Contributor Author

mbrubeck commented Jan 4, 2018

Thanks! I think though this may run a risk of memory unsafety? There can be a brief moment after the main thread has returned that other threads could try to access env::args but after main has returned they may become invalid.

Ah, this is because argv may point to data on the main thread's stack, right? Does this mean the current Mac and iOS implementations are unsafe, since they may try to read from argv after main thread exit?

I'm not sure if there's a way around that other than cloning on startup.

We could reset the pointer to null in cleanup, which is called on the main thread before it exits. When the pointer is null, std::env::args could either panic or returns an empty iterator. (The current Unix implementation returns an empty iterator in this situation, but a panic might be more appropriate.)

Does cloning on startup cause problems though?

It's a small time and memory overhead for every Rust binary that uses libstd, though probably too tiny to matter for most such programs. Removing it also simplifies the code somewhat.

@alexcrichton
Copy link
Member

Ah yeah I dunno about the OSX implementation. I think we're praying that the library function gets it right? I'm not sure of the exact semantics of it though.

You're right though in that I think nulling out before main returns could work! It seems reasonable to me to panic in other threads at that time.

@mbrubeck
Copy link
Contributor Author

mbrubeck commented Jan 4, 2018

Update: I re-added a cleanup function locally as discussed above, but now the unit test env::tests::args_debug seems to be deadlocking, and I don't yet know why.

@mbrubeck
Copy link
Contributor Author

mbrubeck commented Jan 4, 2018

Oh, now I see why. :) Fix coming up.

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Collaborator

bors commented Jan 5, 2018

📌 Commit 91c3eee has been approved by alexcrichton

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 5, 2018
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jan 6, 2018
[unix] Don't clone command-line args on startup

Fixes part of rust-lang#47164 and simplifies the `args` code on non-Apple Unix platforms.

Note: This could change behavior for programs that use both `std::env::args` *and* unsafe code that mutates `argv` directly.  However, these programs already behave differently on different platforms.  The new behavior on non-Apple platforms is closer to the existing behavior on Apple platforms.
bors added a commit that referenced this pull request Jan 6, 2018
Rollup of 5 pull requests

- Successful merges: #46987, #47165, #47173, #47202, #47216
- Failed merges:
@bors bors merged commit 91c3eee into rust-lang:master Jan 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants