Skip to content
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

Add shims for env var emulation in Windows #1225

Merged
merged 9 commits into from
Mar 27, 2020
Merged

Conversation

JOE1994
Copy link
Contributor

@JOE1994 JOE1994 commented Mar 13, 2020

This PR attempts to implement the final step of the instructions laid out in #707 (comment) , and is yet a work in progress.

STATUS

  • Add general target methods for read_str/alloc_str that dispatch to either c_str or wide_str variants
    (helpers.rs)
  • Implement shims fn getenvironmentvariablew/fn setenvironmentvariablew
    (std::env::var(), std::env::set_var())
  • Implement shim GetEnvironmentStringsW (std::env::vars())
  • Implement shim FreeEnvironmentStringsW

ISSUES (updated on 03/21/2020)

  • MIRI errors while running std::env::remove_var() in Windows.
    MIRI complaining about raw pointer usage in
    Rust standard library src/libstd/sys/windows/os.rs.

TODO (probably on a separate PR)

  • Move string helpers into a new file to avoid bloating src/helpers.rs too much. (shims/os_str.rs)

src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

Move string helpers into a new file to avoid bloating src/helpers.rs too much.
(shims/str.rs ?)

That sounds like a good idea, do you want to do that in a separate PR?
I propose shims/os_str.rs.

src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
@JOE1994
Copy link
Contributor Author

JOE1994 commented Mar 13, 2020

Move string helpers into a new file to avoid bloating src/helpers.rs too much.
(shims/str.rs ?)

That sounds like a good idea, do you want to do that in a separate PR?
I propose shims/os_str.rs.

It's actually the idea that you suggested last year 😃 I will open a separate PR to take care of it after this PR is merged.

src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
@bors

This comment has been minimized.

src/helpers.rs Outdated Show resolved Hide resolved
src/helpers.rs Outdated Show resolved Hide resolved
@JOE1994 JOE1994 changed the title WIP: Add 'read/write/alloc wide_str' functions to support env var emulation in Windows Add 'read/write/alloc wide_str' functions to support env var emulation in Windows Mar 21, 2020
@JOE1994
Copy link
Contributor Author

JOE1994 commented Mar 21, 2020

Testing MIRI on tests/run-pass/env.rs gives an evaluation error.
error: Miri evaluation error: invalid use of NULL pointer

error is about the source code of standard library, and not from the added changes in this PR

image

@JOE1994 JOE1994 changed the title Add 'read/write/alloc wide_str' functions to support env var emulation in Windows Add helper functions and shims for env var emulation in Windows Mar 21, 2020
@JOE1994 JOE1994 requested a review from RalfJung March 21, 2020 21:13
@bjorn3
Copy link
Member

bjorn3 commented Mar 22, 2020

value_ptr seems to be set to a nullptr when unsetting an env var on windows. The setenv function doesn't check for this.

@bors
Copy link
Collaborator

bors commented Mar 22, 2020

☔ The latest upstream changes (presumably #1250) made this pull request unmergeable. Please resolve the merge conflicts.

@JOE1994 JOE1994 force-pushed the rw_widestr branch 4 times, most recently from 72aa9c2 to 3bb5d85 Compare March 23, 2020 03:44
@JOE1994 JOE1994 changed the title Add helper functions and shims for env var emulation in Windows Add shims for env var emulation in Windows Mar 27, 2020
src/shims/env.rs Outdated Show resolved Hide resolved
src/shims/env.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

Okay, I think the code is fine now except for that WinHeap problem. I will look into that later today or tomorrow.

@RalfJung
Copy link
Member

Ah I forgot, there are some more tests that can have their "ignore-windows" removed:

  • tests/run-pass/env-exclude.rs
  • tests/run-pass/env-without-isolation.rs

Also, in tests/compile-fail/environ-gets-deallocated.rs, the comment should be updated to something like

//ignore-windows: Windows does not have a global environ list that the program can access directly

src/shims/env.rs Outdated Show resolved Hide resolved
src/shims/env.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

All right, thanks for your persistence and well done. :)
@bors r+

@bors
Copy link
Collaborator

bors commented Mar 27, 2020

📌 Commit 579b3c4 has been approved by RalfJung

@bors
Copy link
Collaborator

bors commented Mar 27, 2020

⌛ Testing commit 579b3c4 with merge 01bc08a...

@bors
Copy link
Collaborator

bors commented Mar 27, 2020

☀️ Test successful - checks-travis, status-appveyor
Approved by: RalfJung
Pushing 01bc08a to master...

@bors bors merged commit 01bc08a into rust-lang:master Mar 27, 2020
@JOE1994 JOE1994 deleted the rw_widestr branch March 27, 2020 20:43
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 30, 2020
avoid creating unnecessary reference in Windows Env iterator

Discovered in rust-lang/miri#1225: the Windows `Env` iterator violates Stacked Borrows by creating an `&u16`, turning it into a raw pointer, and then accessing memory outside the range of that type.

There is no need to create a reference here in the first place, so the fix is trivial.
Cc @JOE1994
Cc rust-lang/unsafe-code-guidelines#134
JOE1994 added a commit to JOE1994/miri that referenced this pull request Jun 8, 2020
A small fix was made to libstd in rust-lang/rust#70479 (back in March).
(Miri reported UB due to Stacked Borrows violation - [link to Miri error log](rust-lang#1225 (comment)))

Thank you for reviewing 👍
bors added a commit that referenced this pull request Jun 8, 2020
Add a case to list of 'StackedBorrows violations'

A small fix was made to libstd in rust-lang/rust#70479 (back in March).
(Miri reported UB due to Stacked Borrows violation - [link to Miri error log](#1225 (comment)))

Thank you for reviewing 👍
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.

None yet

6 participants