-
Notifications
You must be signed in to change notification settings - Fork 349
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
Implement environment variable emulation on Windows #707
Comments
Hello, I'm interested in working on this issue. @RalfJung , may I ask which file I should be working on for this issue?? Thank you :) |
@JOE1994 great! Do you have a Windows system to do the development on? Without that, the testing will be a bit annoying as we'll have to use AppVeyor and bors for that. OTOH, I have zero experience with development on Windows, so you might have to do some experiments to get Miri to build and run locally. The env var implementation for Unix is at miri/src/shims/foreign_items.rs Lines 414 to 493 in ce5e910
It would probably be a good idea to start by moving as much of this as possible into a new env.rs module (inside src/shims ). foreign_items.rs is already way too long anyway. ;) And then you'll have to figure out which foreign functions libstd calls on Windows for reading and writing environment variables, and emulate them. You can use this test case for that, running it in Miri will tell you which foreign function gets called that is not implemented. GetEnvironmentVariableW will likely be the first one.
|
Hi, @JOE1994 did you get a chance to look at this? I have been working on the linux side of this and I could give you a hand. |
Hello @christianpoveda ,
That is really nice of you! 👍 I apologize for being irresponsible in working on this issue.. |
Don't worry, that happens from time to time.
Sure, feel free to ask any questions :) |
Yes so |
@christianpoveda Thanks for your help!
|
Hi. So the problem seems to be that What In contrast, what So what we can do here is implement the windows shim in a different function first, be sure that it works. and then try to deduplicate the code that is also used in the Unix shim. About asking questions, is not embarrassing at all, we are all learning here :) I'll write more detailed instructions about how to write to a buffer and such tomorrow. I'm in my phone right now :P |
It totally isn't! Asking legitimate questions like yours is never embarrassing. Also when I assigned the E-easy label, that was a guess. And even if this issue is easy as Miri issues go, there's still a lot of API surface to learn here. Another problem you might run into is that |
@JOE1994 some tips: The signature of this function is: DWORD GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize); A brief description of the arguments:
Instructions:
After this we "just" need to write the actual value to PD: I wrote this in the wrong issue, silly me. |
@christianpoveda @RalfJung You guys are so awesome! 😸 I'm on it 😄 |
Just a brief update.. The latest version of MIRI builds and runs successfully in Ubuntu (under Windows Subsystem for Linux), but fails to build in my Windows machine. On Windows, there is an issue with linking the syntax crate which seems to be a rustc_private feature. I haven't been able to find out the cause of this issue.. I've also tried the latest version of rustc nightly, and the result is the same as below. |
You need to run |
Thank you for all your help! 😄 Is there a way to convert |
Good point. So how do these strings work? Are they terminated by two null-bytes? If yes, I think we should add a new method
Oh, |
@JOE1994 Maybe it would make sense, as a "warm-up exercise", to do some work on the existing env var emulation. To share code between Unix and Windows, we probably want to use
Lines 83 to 87 in 94732aa
That can all be one PR, and only affects Miri -- no rustc changes needed. Then, in a next step, we need to have support for reading/writing host Lines 132 to 141 in 94732aa
With those methods in place, we can then have higher-level methods At this point, it should not be hard to implement support for For the third PR, we tackle env var emulation. I think env var simulation should be able to use the new "target_str" methods to have fairly uniform code paths for Unix and Windows. |
mir-interpret: add method to read wide strings from Memory Implemented *step2* from [instructions](rust-lang/miri#707 (comment)) laid out in rust-lang/miri#707. Added 2 new methods to struct `rustc_mir::interpret::InterpCx`. * `read_os_str_from_wide_str` (src/librustc_mir/interpret/operand.rs) * `write_os_str_to_wide_str` (src/librustc_mir/interpret/place.rs) - used existing logic implemented in [MIRI/src/eval.rs](https://github.com/rust-lang/miri/blob/94732aaf7bf79fd01a4a48d11155c6586b937514/src/eval.rs#L132-L141) These methods are intended to be used for environment variable emulation in Windows.
rust-lang/rust#69326 landed upstream. :) I think the rest of this can happen in Miri, starting with having |
Add helper functions and shims for env var emulation in Windows This PR attempts to implement the final step of the instructions laid out in #707 (comment) , and is yet a work in progress. ### STATUS - [x] Add general **_target_** methods for **read_str/alloc_str** that dispatch to either **c_str** or **wide_str** variants (**helpers.rs**) - [x] Implement shims `fn getenvironmentvariablew`/`fn setenvironmentvariablew` (`std::env::var()`, `std::env::set_var()`) - [x] Implement shim `GetEnvironmentStringsW` (`std::env::vars()`) - [x] 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*](#1225 (comment)). ### 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**)
Add helper functions and shims for env var emulation in Windows This PR attempts to implement the final step of the instructions laid out in #707 (comment) , and is yet a work in progress. ### STATUS - [x] Add general **_target_** methods for **read_str/alloc_str** that dispatch to either **c_str** or **wide_str** variants (**helpers.rs**) - [x] Implement shims `fn getenvironmentvariablew`/`fn setenvironmentvariablew` (`std::env::var()`, `std::env::set_var()`) - [x] Implement shim `GetEnvironmentStringsW` (`std::env::vars()`) - [x] 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*](#1225 (comment)). ### 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**)
Add shims for env var emulation in Windows This PR attempts to implement the final step of the instructions laid out in #707 (comment) , and is yet a work in progress. ### STATUS - [x] Add general **_target_** methods for **read_str/alloc_str** that dispatch to either **c_str** or **wide_str** variants (**helpers.rs**) - [x] Implement shims `fn getenvironmentvariablew`/`fn setenvironmentvariablew` (`std::env::var()`, `std::env::set_var()`) - [x] Implement shim `GetEnvironmentStringsW` (`std::env::vars()`) - [x] 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*](#1225 (comment)). ### 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**)
Fixed by #1225 :) |
Currently,
GetEnvironmentVariableW
just always returns "env var not found".Mentoring instructions available
The text was updated successfully, but these errors were encountered: